Skip to content

Commit

Permalink
Merge pull request #8 from chainbound/lore/fix/multiple-relays-lock
Browse files Browse the repository at this point in the history
fix(builder): introduce `updateConstraintsCacheLock`
  • Loading branch information
thedevbirb authored Nov 12, 2024
2 parents 45fe6e9 + 6af9695 commit 28acd2f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
8 changes: 8 additions & 0 deletions builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ type Builder struct {

// constraintsCache is a map from slot to the decoded constraints made by proposers
constraintsCache *shardmap.FIFOMap[uint64, types.HashToConstraintDecoded]
// NOTE: `shardmap` already provides locks, however for handling multiple
// relay subscriptions for constraints we need a lock to protect the cache
// between the `Get` and `Put` operation
updateConstraintsCacheLock sync.Mutex

limiter *rate.Limiter
submissionOffsetFromEndOfSlot time.Duration
Expand Down Expand Up @@ -393,6 +397,9 @@ func (b *Builder) subscribeToRelayForConstraints(relayBaseEndpoint string) error
continue
}

// Take the lock to update the constraints cache: both `Get` and `Put` must be done by the same entity
b.updateConstraintsCacheLock.Lock()
// For every constraint, we need to check if it has already been seen for the associated slot
slotConstraints, _ := b.constraintsCache.Get(constraint.Message.Slot)
if len(slotConstraints) == 0 {
b.constraintsCache.Put(constraint.Message.Slot, decodedConstraints)
Expand All @@ -406,6 +413,7 @@ func (b *Builder) subscribeToRelayForConstraints(relayBaseEndpoint string) error
}

b.constraintsCache.Put(constraint.Message.Slot, slotConstraints)
b.updateConstraintsCacheLock.Unlock()
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion builder/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ func Register(stack *node.Node, backend *eth.Ethereum, cfg *Config) error {
return errors.New("neither local nor remote relay specified")
}

if len(cfg.SecondaryRemoteRelayEndpoints) > 0 && !(len(cfg.SecondaryRemoteRelayEndpoints) == 1 && cfg.SecondaryRemoteRelayEndpoints[0] == "") {
if len(cfg.SecondaryRemoteRelayEndpoints) > 0 && (len(cfg.SecondaryRemoteRelayEndpoints) != 1 || cfg.SecondaryRemoteRelayEndpoints[0] != "") {
secondaryRelays := make([]IRelay, len(cfg.SecondaryRemoteRelayEndpoints))
for i, endpoint := range cfg.SecondaryRemoteRelayEndpoints {
relayConfig, err := getRelayConfig(endpoint)
Expand Down

0 comments on commit 28acd2f

Please sign in to comment.