-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstate.go
194 lines (162 loc) · 3.92 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package state
import (
"bytes"
"encoding/hex"
"math/big"
"strings"
"github.com/ethereum/evmc/v10/bindings/go/evmc"
iradix "github.com/hashicorp/go-immutable-radix"
"github.com/umbracle/ethgo"
)
type Snapshot interface {
GetStorage(addr evmc.Address, root evmc.Hash, key evmc.Hash) evmc.Hash
GetAccount(addr evmc.Address) (*Account, error)
}
// StateObject is the internal representation of the account
type stateObject struct {
Account *Account
Code []byte
Suicide bool
Deleted bool
DirtyCode bool
Txn *iradix.Txn
}
func (s *stateObject) Empty() bool {
return s.Account.Nonce == 0 && s.Account.Balance.Sign() == 0 && bytes.Equal(s.Account.CodeHash, EmptyCodeHash[:])
}
// Copy makes a copy of the state object
func (s *stateObject) Copy() *stateObject {
ss := new(stateObject)
// copy account
ss.Account = s.Account.Copy()
ss.Suicide = s.Suicide
ss.Deleted = s.Deleted
ss.DirtyCode = s.DirtyCode
ss.Code = s.Code
if s.Txn != nil {
ss.Txn = s.Txn.CommitOnly().Txn()
}
return ss
}
// Object is the serialization of the radix object (can be merged to StateObject?).
type Object struct {
Address evmc.Address
CodeHash evmc.Hash
Balance *big.Int
Root evmc.Hash
Nonce uint64
Deleted bool
DirtyCode bool
Code []byte
Storage []*StorageObject
}
// StorageObject is an entry in the storage
type StorageObject struct {
Deleted bool
Key []byte
Val []byte
}
type Output struct {
Logs []*Log
Success bool
GasLeft uint64
ContractAddress evmc.Address
ReturnValue []byte
}
type Log struct {
Address evmc.Address
Topics []evmc.Hash
Data []byte
}
// Account is an object we can retrieve from the state
type Account struct {
Nonce uint64
Balance *big.Int
Root evmc.Hash
CodeHash []byte
Code []byte
}
func (a *Account) Copy() *Account {
aa := new(Account)
aa.Balance = big.NewInt(1).SetBytes(a.Balance.Bytes())
aa.Nonce = a.Nonce
aa.CodeHash = a.CodeHash
aa.Root = a.Root
return aa
}
const HashLength = 32
func bytesToHash(b []byte) evmc.Hash {
var h evmc.Hash
size := len(b)
min := min(size, HashLength)
copy(h[HashLength-min:], b[len(b)-min:])
return h
}
func StringToHash(str string) evmc.Hash {
return bytesToHash(stringToBytes(str))
}
func min(i, j int) int {
if i < j {
return i
}
return j
}
func stringToBytes(str string) []byte {
str = strings.TrimPrefix(str, "0x")
if len(str)%2 == 1 {
str = "0" + str
}
b, _ := hex.DecodeString(str)
return b
}
var (
EmptyCodeHash = bytesToHash(ethgo.Keccak256(nil))
// EmptyRootHash is the root when there are no transactions
EmptyRootHash = StringToHash("0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421")
)
type Message struct {
Nonce uint64
GasPrice *big.Int
Gas uint64
To *evmc.Address
Value *big.Int
Input []byte
From evmc.Address
}
func (t *Message) IsContractCreation() bool {
return t.To == nil
}
// Contract is the instance being called
type Contract struct {
Type evmc.CallKind
CodeAddress evmc.Address
Address evmc.Address
Caller evmc.Address
Depth int
Value *big.Int
Input []byte
Gas uint64
Static bool
Salt evmc.Hash
}
func NewContract(typ evmc.CallKind, depth int, from evmc.Address, to evmc.Address, value *big.Int, gas uint64, input []byte) *Contract {
f := &Contract{
Type: typ,
Caller: from,
CodeAddress: to,
Address: to,
Gas: gas,
Value: value,
Input: input,
Depth: depth,
}
return f
}
func NewContractCreation(depth int, from evmc.Address, to evmc.Address, value *big.Int, gas uint64, code []byte) *Contract {
c := NewContract(evmc.Create, depth, from, to, value, gas, code)
return c
}
func NewContractCall(depth int, from evmc.Address, to evmc.Address, value *big.Int, gas uint64, input []byte) *Contract {
c := NewContract(evmc.Call, depth, from, to, value, gas, input)
return c
}