Skip to content

Commit

Permalink
fix: race condition when querying registered operators data (#343)
Browse files Browse the repository at this point in the history
* fix: race condition when querying registered operators data

* fix: move the startBlock copying inside of each function

* change the loop index variable so that it is initialized from a copy of startBlock

* docs: add explanatory comment

* format
  • Loading branch information
ricomateo authored Sep 24, 2024
1 parent 773903b commit 6f241c3
Showing 1 changed file with 12 additions and 2 deletions.
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) {
// 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

0 comments on commit 6f241c3

Please sign in to comment.