diff --git a/pkg/store/store_test.go b/pkg/store/store_test.go index 845d9ac..c91ba49 100644 --- a/pkg/store/store_test.go +++ b/pkg/store/store_test.go @@ -227,6 +227,23 @@ func TestStore(t *testing.T) { } }) + t.Run("IsSettled", func(t *testing.T) { + st, err := store.NewStore(db) + if err != nil { + t.Fatalf("Failed to create store: %s", err) + } + + for _, settlement := range settlements { + settled, err := st.IsSettled(context.Background(), settlement.CommitmentIdx) + if err != nil { + t.Fatalf("Failed to check if settled: %s", err) + } + if !settled { + t.Fatalf("Expected settlement to be settled") + } + } + }) + t.Run("SubscribeSettlements", func(t *testing.T) { st, err := store.NewStore(db) if err != nil { diff --git a/pkg/updater/updater.go b/pkg/updater/updater.go index 2683996..f64d5b9 100644 --- a/pkg/updater/updater.go +++ b/pkg/updater/updater.go @@ -9,6 +9,7 @@ import ( "math" "math/big" "strings" + "sync" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/types" @@ -63,6 +64,8 @@ type Updater struct { evtMgr events.EventManager l1BlockCache *lru.Cache[uint64, map[string]int] l2BlockTimeCache *lru.Cache[uint64, uint64] + encryptedMu sync.Mutex + openedCmtMu sync.Mutex metrics *metrics } @@ -124,6 +127,9 @@ func (u *Updater) subscribeEncryptedCommitments(ctx context.Context) error { ev := events.NewEventHandler( "EncryptedCommitmentStored", func(update *preconf.PreconfcommitmentstoreEncryptedCommitmentStored) error { + u.encryptedMu.Lock() + defer u.encryptedMu.Unlock() + err := u.winnerRegister.AddEncryptedCommitment( ctx, update.CommitmentIndex[:], @@ -164,6 +170,9 @@ func (u *Updater) subscribeOpenedCommitments(ctx context.Context) error { ev := events.NewEventHandler( "CommitmentStored", func(update *preconf.PreconfcommitmentstoreCommitmentStored) error { + u.openedCmtMu.Lock() + defer u.openedCmtMu.Unlock() + alreadySettled, err := u.winnerRegister.IsSettled(ctx, update.CommitmentIndex[:]) if err != nil { u.logger.Error( @@ -176,7 +185,10 @@ func (u *Updater) subscribeOpenedCommitments(ctx context.Context) error { if alreadySettled { // both bidders and providers could open commitments, so this could // be a duplicate event - u.logger.Info("duplicate open commitment event", "commitmentIdx", common.Bytes2Hex(update.CommitmentIndex[:])) + u.logger.Info( + "duplicate open commitment event", + "commitmentIdx", common.Bytes2Hex(update.CommitmentIndex[:]), + ) return nil }