Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(builder): updateConstraintsCacheLock to prevent race conditions #11

Merged
merged 1 commit into from
Nov 13, 2024
Merged
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
10 changes: 9 additions & 1 deletion 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 @@ -392,10 +396,13 @@ func (b *Builder) subscribeToRelayForConstraints(relayBaseEndpoint string) error
log.Error("Failed to decode constraint: ", err)
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)
b.updateConstraintsCacheLock.Unlock()
continue
}

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
Loading