forked from 0xPolygonHermez/zkevm-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fakedb.go
232 lines (185 loc) · 7.05 KB
/
fakedb.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package state
import (
"context"
"math/big"
"github.com/0xPolygonHermez/zkevm-node/log"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/params"
)
// FakeDB is the implementation of the fakeevm.FakeDB interface
type FakeDB struct {
State *State
stateRoot []byte
}
// SetStateRoot is the stateRoot setter.
func (f *FakeDB) SetStateRoot(stateRoot []byte) {
f.stateRoot = stateRoot
}
// CreateAccount not implemented
func (f *FakeDB) CreateAccount(common.Address) {
log.Error("FakeDB: CreateAccount method not implemented")
}
// SubBalance not implemented
func (f *FakeDB) SubBalance(common.Address, *big.Int) {
log.Error("FakeDB: SubBalance method not implemented")
}
// AddBalance not implemented
func (f *FakeDB) AddBalance(common.Address, *big.Int) {
log.Error("FakeDB: AddBalance method not implemented")
}
// GetBalance returns the balance of the given address.
func (f *FakeDB) GetBalance(address common.Address) *big.Int {
ctx := context.Background()
balance, err := f.State.GetTree().GetBalance(ctx, address, f.stateRoot)
if err != nil {
log.Errorf("error on FakeDB GetBalance for address %v", address)
}
log.Debugf("FakeDB GetBalance for address %v", address)
return balance
}
// GetNonce returns the nonce of the given address.
func (f *FakeDB) GetNonce(address common.Address) uint64 {
ctx := context.Background()
nonce, err := f.State.GetTree().GetNonce(ctx, address, f.stateRoot)
if err != nil {
log.Errorf("error on FakeDB GetNonce for address %v", address)
return 0
}
log.Debugf("FakeDB GetNonce for address %v", address)
return nonce.Uint64()
}
// SetNonce not implemented
func (f *FakeDB) SetNonce(common.Address, uint64) {
log.Error("FakeDB: SetNonce method not implemented")
}
// GetCodeHash gets the hash for the code at a given address
func (f *FakeDB) GetCodeHash(address common.Address) common.Hash {
ctx := context.Background()
hash, err := f.State.GetTree().GetCodeHash(ctx, address, f.stateRoot)
if err != nil {
log.Errorf("error on FakeDB GetCodeHash for address %v, err: %v", address, err)
}
log.Debugf("FakeDB GetCodeHash for address %v => %v", address, common.BytesToHash(hash))
return common.BytesToHash(hash)
}
// GetCode returns the SC code of the given address.
func (f *FakeDB) GetCode(address common.Address) []byte {
ctx := context.Background()
code, err := f.State.GetTree().GetCode(ctx, address, f.stateRoot)
if err != nil {
log.Errorf("error on FakeDB GetCode for address %v", address)
}
log.Debugf("FakeDB GetCode for address %v", address)
return code
}
// SetCode not implemented
func (f *FakeDB) SetCode(common.Address, []byte) {
log.Error("FakeDB: SetCode method not implemented")
}
// GetCodeSize get address code size
func (f *FakeDB) GetCodeSize(address common.Address) int {
return len(f.GetCode(address))
}
// AddRefund not implemented
func (f *FakeDB) AddRefund(uint64) {
log.Error("FakeDB: AddRefund method not implemented")
}
// SubRefund not implemented
func (f *FakeDB) SubRefund(uint64) {
log.Error("FakeDB: SubRefund method not implemented")
}
// GetRefund not implemented
func (f *FakeDB) GetRefund() uint64 {
log.Error("FakeDB: GetRefund method not implemented")
return 0
}
// GetCommittedState not implemented
func (f *FakeDB) GetCommittedState(common.Address, common.Hash) common.Hash {
log.Error("FakeDB: GetCommittedState method not implemented")
return ZeroHash
}
// GetState retrieves a value from the given account's storage trie.
func (f *FakeDB) GetState(address common.Address, hash common.Hash) common.Hash {
ctx := context.Background()
storage, err := f.State.GetTree().GetStorageAt(ctx, address, hash.Big(), f.stateRoot)
if err != nil {
log.Errorf("error on FakeDB GetState for address %v", address)
}
log.Debugf("FakeDB GetState for address %v", address)
return common.BytesToHash(storage.Bytes())
}
// SetState not implemented
func (f *FakeDB) SetState(common.Address, common.Hash, common.Hash) {
log.Error("FakeDB: SetState method not implemented")
}
// GetTransientState not implemented
func (f *FakeDB) GetTransientState(addr common.Address, key common.Hash) common.Hash {
log.Error("FakeDB: GetTransientState method not implemented")
return ZeroHash
}
// SetTransientState not implemented
func (f *FakeDB) SetTransientState(addr common.Address, key, value common.Hash) {
log.Error("FakeDB: SetTransientState method not implemented")
}
// Suicide not implemented
func (f *FakeDB) Suicide(common.Address) bool {
log.Error("FakeDB: Suicide method not implemented")
return false
}
// HasSuicided not implemented
func (f *FakeDB) HasSuicided(common.Address) bool {
log.Error("FakeDB: HasSuicided method not implemented")
return false
}
// Exist reports whether the given account exists in state.
// Notably this should also return true for suicided accounts.
func (f *FakeDB) Exist(address common.Address) bool {
return !(f.GetNonce(address) == 0 && f.GetBalance(address).Int64() == 0 && f.GetCodeHash(address) == ZeroHash)
}
// Empty returns whether the given account is empty. Empty
// is defined according to EIP161 (balance = nonce = code = 0).
func (f *FakeDB) Empty(address common.Address) bool {
return !(f.GetNonce(address) == 0 && f.GetBalance(address).Int64() == 0 && f.GetCodeHash(address) == ZeroHash)
}
// AddressInAccessList not implemented
func (f *FakeDB) AddressInAccessList(addr common.Address) bool {
log.Error("FakeDB: AddressInAccessList method not implemented")
return false
}
// SlotInAccessList not implemented
func (f *FakeDB) SlotInAccessList(addr common.Address, slot common.Hash) (addressOk bool, slotOk bool) {
log.Error("FakeDB: SlotInAccessList method not implemented")
return false, false
}
// AddAddressToAccessList adds the given address to the access list. This operation is safe to perform
// AddAddressToAccessList not implemented// even if the feature/fork is not active yet
func (f *FakeDB) AddAddressToAccessList(addr common.Address) {
log.Error("FakeDB: AddAddressToAccessList method not implemented")
}
// AddSlotToAccessList adds the given (address,slot) to the access list. This operation is safe to perform
// AddSlotToAccessList not implemented// even if the feature/fork is not active yet
func (f *FakeDB) AddSlotToAccessList(addr common.Address, slot common.Hash) {
log.Error("FakeDB: AddSlotToAccessList method not implemented")
}
// Prepare not implemented
func (f *FakeDB) Prepare(rules params.Rules, sender, coinbase common.Address, dest *common.Address, precompiles []common.Address, txAccesses types.AccessList) {
log.Error("FakeDB: Prepare method not implemented")
}
// RevertToSnapshot not implemented
func (f *FakeDB) RevertToSnapshot(int) {
log.Error("FakeDB: RevertToSnapshot method not implemented")
}
// Snapshot not implemented
func (f *FakeDB) Snapshot() int {
log.Error("FakeDB: Snapshot method not implemented")
return 0
}
// AddLog not implemented
func (f *FakeDB) AddLog(*types.Log) {
log.Error("FakeDB: AddLog method not implemented")
}
// AddPreimage not implemented
func (f *FakeDB) AddPreimage(common.Hash, []byte) {
log.Error("FakeDB: AddPreimage method not implemented")
}