/
state.go
77 lines (67 loc) · 1.74 KB
/
state.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package w3types
import (
"encoding/json"
"math/big"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/types"
"github.com/lmittmann/w3/internal/crypto"
)
type State map[common.Address]*Account
// SetGenesisAlloc copies the given [core.GenesisAlloc] to the state and
// returns it.
func (s State) SetGenesisAlloc(alloc core.GenesisAlloc) State {
s = make(State, len(alloc))
for addr, acc := range alloc {
s[addr] = &Account{
Nonce: acc.Nonce,
Balance: acc.Balance,
Code: acc.Code,
Storage: acc.Storage,
}
}
return s
}
type Account struct {
Nonce uint64
Balance *big.Int
Code []byte
Storage map[common.Hash]common.Hash
codeHash *common.Hash // caches the code hash
}
// CodeHash returns the hash of the account's code.
func (acc *Account) CodeHash() common.Hash {
if acc.codeHash != nil {
return *acc.codeHash
}
if len(acc.Code) == 0 {
return types.EmptyCodeHash
}
h := crypto.Keccak256Hash(acc.Code)
acc.codeHash = &h
return h
}
// MarshalJSON implements the [json.Marshaler].
func (acc *Account) MarshalJSON() ([]byte, error) {
type account struct {
Nonce hexutil.Uint64 `json:"nonce,omitempty"`
Balance *hexutil.Big `json:"balance,omitempty"`
Code hexutil.Bytes `json:"code,omitempty"`
Storage map[common.Hash]common.Hash `json:"stateDiff,omitempty"`
}
var enc account
if acc.Nonce > 0 {
enc.Nonce = hexutil.Uint64(acc.Nonce)
}
if acc.Balance != nil {
enc.Balance = (*hexutil.Big)(acc.Balance)
}
if len(acc.Code) > 0 {
enc.Code = acc.Code
}
if len(acc.Storage) > 0 {
enc.Storage = acc.Storage
}
return json.Marshal(&enc)
}