Skip to content

Commit

Permalink
fix: synchronize commitment insertion
Browse files Browse the repository at this point in the history
  • Loading branch information
Alok committed Apr 18, 2024
1 parent 5a9ce37 commit fb947bb
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
17 changes: 17 additions & 0 deletions pkg/store/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
14 changes: 13 additions & 1 deletion pkg/updater/updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"math"
"math/big"
"strings"
"sync"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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[:],
Expand Down Expand Up @@ -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(
Expand All @@ -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
}

Expand Down

0 comments on commit fb947bb

Please sign in to comment.