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: race condition when querying registered operators data #343

Merged
merged 5 commits into from
Sep 24, 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
14 changes: 12 additions & 2 deletions chainio/clients/avsregistry/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,12 @@ func (r *ChainReader) QueryExistingRegisteredOperatorPubKeys(

operatorAddresses := make([]types.OperatorAddr, 0)
operatorPubkeys := make([]types.OperatorPubkeys, 0)
for i := startBlock; i.Cmp(stopBlock) <= 0; i.Add(i, blockRange) {
// QueryExistingRegisteredOperatorPubKeys and QueryExistingRegisteredOperatorSockets
// both run in parallel and they read and mutate the same variable startBlock,
// so we clone it to prevent the race condition.
// TODO: we might want to eventually change the function signature to pass a uint,
// but that would be a breaking change
for i := new(big.Int).Set(startBlock); i.Cmp(stopBlock) <= 0; i.Add(i, blockRange) {
ricomateo marked this conversation as resolved.
Show resolved Hide resolved
// Subtract 1 since FilterQuery is inclusive
toBlock := big.NewInt(0).Add(i, big.NewInt(0).Sub(blockRange, big.NewInt(1)))
if toBlock.Cmp(stopBlock) > 0 {
Expand Down Expand Up @@ -521,7 +526,12 @@ func (r *ChainReader) QueryExistingRegisteredOperatorSockets(
}

operatorIdToSocketMap := make(map[types.OperatorId]types.Socket)
for i := startBlock; i.Cmp(stopBlock) <= 0; i.Add(i, blockRange) {
// QueryExistingRegisteredOperatorPubKeys and QueryExistingRegisteredOperatorSockets
// both run in parallel and they read and mutate the same variable startBlock,
// so we clone it to prevent the race condition.
// TODO: we might want to eventually change the function signature to pass a uint,
// but that would be a breaking change
for i := new(big.Int).Set(startBlock); i.Cmp(stopBlock) <= 0; i.Add(i, blockRange) {
// Subtract 1 since FilterQuery is inclusive
toBlock := big.NewInt(0).Add(i, big.NewInt(0).Sub(blockRange, big.NewInt(1)))
if toBlock.Cmp(stopBlock) > 0 {
Expand Down