forked from erigontech/erigon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
state_overrides.go
51 lines (44 loc) · 1.26 KB
/
state_overrides.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package ethapi
import (
"fmt"
"math/big"
"github.com/holiman/uint256"
libcommon "github.com/nebojsa94/erigon/erigon-lib/common"
"github.com/nebojsa94/erigon/core/state"
)
type StateOverrides map[libcommon.Address]Account
func (overrides *StateOverrides) Override(state *state.IntraBlockState) error {
for addr, account := range *overrides {
// Override account nonce.
if account.Nonce != nil {
state.SetNonce(addr, uint64(*account.Nonce))
}
// Override account(contract) code.
if account.Code != nil {
state.SetCode(addr, *account.Code)
}
// Override account balance.
if account.Balance != nil {
balance, overflow := uint256.FromBig((*big.Int)(*account.Balance))
if overflow {
return fmt.Errorf("account.Balance higher than 2^256-1")
}
state.SetBalance(addr, balance)
}
if account.State != nil && account.StateDiff != nil {
return fmt.Errorf("account %s has both 'state' and 'stateDiff'", addr.Hex())
}
// Replace entire state if caller requires.
if account.State != nil {
state.SetStorage(addr, *account.State)
}
// Apply state diff into specified accounts.
if account.StateDiff != nil {
for key, value := range *account.StateDiff {
key := key
state.SetState(addr, &key, value)
}
}
}
return nil
}