Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 16 additions & 8 deletions x/contracts/events/publisher/ws.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,18 @@ func NewWSPublisher(progressStore ProgressStore, logger *slog.Logger, evmClient
}

func (w *wsPublisher) AddContracts(addr ...common.Address) {
added := w.addContracts(addr...)
if added {
select {
case w.updateCh <- struct{}{}:
default:
}
}
}

func (w *wsPublisher) addContracts(addr ...common.Address) bool {
w.mu.Lock()
defer w.mu.Unlock()
added := false
for _, a := range addr {
if !slices.Contains(w.contracts, a) {
Expand All @@ -53,13 +64,7 @@ func (w *wsPublisher) AddContracts(addr ...common.Address) {
w.logger.Info("ws: added contract address", "address", a.Hex())
}
}
w.mu.Unlock()
if added {
select {
case w.updateCh <- struct{}{}:
default:
}
}
return added
}

func (w *wsPublisher) getContracts() []common.Address {
Expand All @@ -72,7 +77,10 @@ func (w *wsPublisher) getContracts() []common.Address {

func (w *wsPublisher) Start(ctx context.Context, contractAddr ...common.Address) <-chan struct{} {
doneChan := make(chan struct{})
w.AddContracts(contractAddr...)
added := w.addContracts(contractAddr...)
if !added {
w.logger.Warn("contracts were added before starting the publisher")
}

if len(contractAddr) == 0 {
w.logger.Info("ws: starting with no contracts; waiting for addresses to be added")
Expand Down
3 changes: 1 addition & 2 deletions x/contracts/events/publisher/ws_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,7 @@ func TestWSPublisher(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

doneChan := p.Start(ctx)
p.AddContracts(common.Address{})
doneChan := p.Start(ctx, common.Address{})

// Wait for first subscribe (will immediately error and cause resubscribe)
select {
Expand Down
Loading