Skip to content

Commit

Permalink
system: NewMultiCallContractCaller uses temporary state
Browse files Browse the repository at this point in the history
  • Loading branch information
blukat29 committed Jul 9, 2024
1 parent c14f8c2 commit e51c56f
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 9 deletions.
2 changes: 1 addition & 1 deletion blockchain/system/multicall.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,6 @@ func (caller *ContractCallerForMultiCall) CallContract(ctx context.Context, call

// NewMultiCallContractCaller creates a new instance of ContractCaller for MultiCall contract.
func NewMultiCallContractCaller(state *state.StateDB, chain backends.BlockChainForCaller, header *types.Header) (*multicall.MultiCallContractCaller, error) {
c := &ContractCallerForMultiCall{state, chain, header}
c := &ContractCallerForMultiCall{state.Copy(), chain, header} // Copy the state to prevent the original state from being modified.
return multicall.NewMultiCallContractCaller(MultiCallAddr, c)
}
5 changes: 2 additions & 3 deletions blockchain/system/multicall_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,12 @@ func TestContractCallerForMultiCall(t *testing.T) {
header := backend.BlockChain().CurrentHeader()
chain := backend.BlockChain()

tempState, _ := backend.BlockChain().StateAt(header.Root)
caller, _ := NewMultiCallContractCaller(tempState, chain, header)
state, _ := backend.BlockChain().StateAt(header.Root)
caller, _ := NewMultiCallContractCaller(state, chain, header)
ret, err := caller.MultiCallStakingInfo(&bind.CallOpts{BlockNumber: header.Number})
assert.Nil(t, err)

// Does not affect the original state
state, _ := backend.BlockChain().StateAt(header.Root)
assert.Equal(t, []byte(nil), state.GetCode(MultiCallAddr))

// Mock data
Expand Down
10 changes: 5 additions & 5 deletions reward/staking_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ func PreloadStakingInfoWithState(header *types.Header, statedb *state.StateDB) e
}

num := header.Number.Uint64()
info, err := getStakingInfoFromMultiCallAtState(num, statedb.Copy(), header)
info, err := getStakingInfoFromMultiCallAtState(num, statedb, header)
if err != nil {
return fmt.Errorf("staking info preload failed. root err: %v", err)
}
Expand Down Expand Up @@ -334,17 +334,17 @@ func getStakingInfoFromMultiCall(blockNum uint64) (*StakingInfo, error) {
return nil, fmt.Errorf("failed to get header by number %d", blockNum)
}

tempState, err := stakingManager.blockchain.StateAt(header.Root)
statedb, err := stakingManager.blockchain.StateAt(header.Root)
if err != nil {
return nil, fmt.Errorf("failed to get state at number %d. root err: %s", blockNum, err)
}

return getStakingInfoFromMultiCallAtState(blockNum, tempState, header)
return getStakingInfoFromMultiCallAtState(blockNum, statedb, header)
}

func getStakingInfoFromMultiCallAtState(blockNum uint64, tempState *state.StateDB, header *types.Header) (*StakingInfo, error) {
func getStakingInfoFromMultiCallAtState(blockNum uint64, statedb *state.StateDB, header *types.Header) (*StakingInfo, error) {
// Get staking info from multicall contract
caller, err := system.NewMultiCallContractCaller(tempState, stakingManager.blockchain, header)
caller, err := system.NewMultiCallContractCaller(statedb, stakingManager.blockchain, header)
if err != nil {
return nil, fmt.Errorf("failed to create multicall contract caller. root err: %s", err)
}
Expand Down

0 comments on commit e51c56f

Please sign in to comment.