forked from okx/xlayer-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tree.go
354 lines (298 loc) · 9.86 KB
/
tree.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
package merkletree
import (
"context"
"fmt"
"math/big"
"strings"
"github.com/0xPolygonHermez/zkevm-node/hex"
"github.com/0xPolygonHermez/zkevm-node/merkletree/hashdb"
"github.com/ethereum/go-ethereum/common"
)
// StateTree provides methods to access and modify state in merkletree
type StateTree struct {
grpcClient hashdb.HashDBServiceClient
}
// NewStateTree creates new StateTree.
func NewStateTree(client hashdb.HashDBServiceClient) *StateTree {
return &StateTree{
grpcClient: client,
}
}
// GetBalance returns balance.
func (tree *StateTree) GetBalance(ctx context.Context, address common.Address, root []byte) (*big.Int, error) {
r := new(big.Int).SetBytes(root)
key, err := KeyEthAddrBalance(address)
if err != nil {
return nil, err
}
k := new(big.Int).SetBytes(key)
proof, err := tree.get(ctx, scalarToh4(r), scalarToh4(k))
if err != nil {
return nil, err
}
if proof == nil || proof.Value == nil {
return big.NewInt(0), nil
}
return fea2scalar(proof.Value), nil
}
// GetNonce returns nonce.
func (tree *StateTree) GetNonce(ctx context.Context, address common.Address, root []byte) (*big.Int, error) {
r := new(big.Int).SetBytes(root)
key, err := KeyEthAddrNonce(address)
if err != nil {
return nil, err
}
k := new(big.Int).SetBytes(key)
proof, err := tree.get(ctx, scalarToh4(r), scalarToh4(k))
if err != nil {
return nil, err
}
if proof == nil || proof.Value == nil {
return big.NewInt(0), nil
}
return fea2scalar(proof.Value), nil
}
// GetCodeHash returns code hash.
func (tree *StateTree) GetCodeHash(ctx context.Context, address common.Address, root []byte) ([]byte, error) {
r := new(big.Int).SetBytes(root)
key, err := KeyContractCode(address)
if err != nil {
return nil, err
}
// this code gets only the hash of the smart contract code from the merkle tree
k := new(big.Int).SetBytes(key)
proof, err := tree.get(ctx, scalarToh4(r), scalarToh4(k))
if err != nil {
return nil, err
}
if proof.Value == nil {
return nil, nil
}
valueBi := fea2scalar(proof.Value)
return ScalarToFilledByteSlice(valueBi), nil
}
// GetCode returns code.
func (tree *StateTree) GetCode(ctx context.Context, address common.Address, root []byte) ([]byte, error) {
scCodeHash, err := tree.GetCodeHash(ctx, address, root)
if err != nil {
return nil, err
}
k := new(big.Int).SetBytes(scCodeHash)
// this code gets actual smart contract code from sc code storage
scCode, err := tree.getProgram(ctx, scalarToh4(k))
if err != nil {
return nil, err
}
return scCode.Data, nil
}
// GetStorageAt returns Storage Value at specified position.
func (tree *StateTree) GetStorageAt(ctx context.Context, address common.Address, position *big.Int, root []byte) (*big.Int, error) {
r := new(big.Int).SetBytes(root)
key, err := KeyContractStorage(address, position.Bytes())
if err != nil {
return nil, err
}
k := new(big.Int).SetBytes(key)
proof, err := tree.get(ctx, scalarToh4(r), scalarToh4(k))
if err != nil {
return nil, err
}
if proof == nil || proof.Value == nil {
return big.NewInt(0), nil
}
return fea2scalar(proof.Value), nil
}
// SetBalance sets balance.
func (tree *StateTree) SetBalance(ctx context.Context, address common.Address, balance *big.Int, root []byte, uuid string) (newRoot []byte, proof *UpdateProof, err error) {
if balance.Cmp(big.NewInt(0)) == -1 {
return nil, nil, fmt.Errorf("invalid balance")
}
r := new(big.Int).SetBytes(root)
key, err := KeyEthAddrBalance(address)
if err != nil {
return nil, nil, err
}
k := new(big.Int).SetBytes(key)
balanceH8 := scalar2fea(balance)
updateProof, err := tree.set(ctx, scalarToh4(r), scalarToh4(k), balanceH8, uuid)
if err != nil {
return nil, nil, err
}
return h4ToFilledByteSlice(updateProof.NewRoot), updateProof, nil
}
// SetNonce sets nonce.
func (tree *StateTree) SetNonce(ctx context.Context, address common.Address, nonce *big.Int, root []byte, uuid string) (newRoot []byte, proof *UpdateProof, err error) {
if nonce.Cmp(big.NewInt(0)) == -1 {
return nil, nil, fmt.Errorf("invalid nonce")
}
r := new(big.Int).SetBytes(root)
key, err := KeyEthAddrNonce(address)
if err != nil {
return nil, nil, err
}
k := new(big.Int).SetBytes(key)
nonceH8 := scalar2fea(nonce)
updateProof, err := tree.set(ctx, scalarToh4(r), scalarToh4(k), nonceH8, uuid)
if err != nil {
return nil, nil, err
}
return h4ToFilledByteSlice(updateProof.NewRoot), updateProof, nil
}
// SetCode sets smart contract code.
func (tree *StateTree) SetCode(ctx context.Context, address common.Address, code []byte, root []byte, uuid string) (newRoot []byte, proof *UpdateProof, err error) {
// calculating smart contract code hash
scCodeHash4, err := HashContractBytecode(code)
if err != nil {
return nil, nil, err
}
// store smart contract code by its hash
err = tree.setProgram(ctx, scCodeHash4, code, true, uuid)
if err != nil {
return nil, nil, err
}
// set smart contract code hash as a leaf value in merkle tree
r := new(big.Int).SetBytes(root)
key, err := KeyContractCode(address)
if err != nil {
return nil, nil, err
}
k := new(big.Int).SetBytes(key)
scCodeHash, err := hex.DecodeHex(H4ToString(scCodeHash4))
if err != nil {
return nil, nil, err
}
scCodeHashBI := new(big.Int).SetBytes(scCodeHash)
scCodeHashH8 := scalar2fea(scCodeHashBI)
updateProof, err := tree.set(ctx, scalarToh4(r), scalarToh4(k), scCodeHashH8, uuid)
if err != nil {
return nil, nil, err
}
// set code length as a leaf value in merkle tree
key, err = KeyCodeLength(address)
if err != nil {
return nil, nil, err
}
k = new(big.Int).SetBytes(key)
scCodeLengthBI := new(big.Int).SetInt64(int64(len(code)))
scCodeLengthH8 := scalar2fea(scCodeLengthBI)
updateProof, err = tree.set(ctx, updateProof.NewRoot, scalarToh4(k), scCodeLengthH8, uuid)
if err != nil {
return nil, nil, err
}
return h4ToFilledByteSlice(updateProof.NewRoot), updateProof, nil
}
// SetStorageAt sets storage value at specified position.
func (tree *StateTree) SetStorageAt(ctx context.Context, address common.Address, position *big.Int, value *big.Int, root []byte, uuid string) (newRoot []byte, proof *UpdateProof, err error) {
r := new(big.Int).SetBytes(root)
key, err := KeyContractStorage(address, position.Bytes())
if err != nil {
return nil, nil, err
}
k := new(big.Int).SetBytes(key)
valueH8 := scalar2fea(value)
updateProof, err := tree.set(ctx, scalarToh4(r), scalarToh4(k), valueH8, uuid)
if err != nil {
return nil, nil, err
}
return h4ToFilledByteSlice(updateProof.NewRoot), updateProof, nil
}
func (tree *StateTree) get(ctx context.Context, root, key []uint64) (*Proof, error) {
result, err := tree.grpcClient.Get(ctx, &hashdb.GetRequest{
Root: &hashdb.Fea{Fe0: root[0], Fe1: root[1], Fe2: root[2], Fe3: root[3]},
Key: &hashdb.Fea{Fe0: key[0], Fe1: key[1], Fe2: key[2], Fe3: key[3]},
})
if err != nil {
return nil, err
}
value, err := string2fea(result.Value)
if err != nil {
return nil, err
}
return &Proof{
Root: []uint64{root[0], root[1], root[2], root[3]},
Key: key,
Value: value,
}, nil
}
func (tree *StateTree) getProgram(ctx context.Context, key []uint64) (*ProgramProof, error) {
result, err := tree.grpcClient.GetProgram(ctx, &hashdb.GetProgramRequest{
Key: &hashdb.Fea{Fe0: key[0], Fe1: key[1], Fe2: key[2], Fe3: key[3]},
})
if err != nil {
return nil, err
}
return &ProgramProof{
Data: result.Data,
}, nil
}
func (tree *StateTree) set(ctx context.Context, oldRoot, key, value []uint64, uuid string) (*UpdateProof, error) {
feaValue := fea2string(value)
if strings.HasPrefix(feaValue, "0x") { // nolint
feaValue = feaValue[2:]
}
result, err := tree.grpcClient.Set(ctx, &hashdb.SetRequest{
OldRoot: &hashdb.Fea{Fe0: oldRoot[0], Fe1: oldRoot[1], Fe2: oldRoot[2], Fe3: oldRoot[3]},
Key: &hashdb.Fea{Fe0: key[0], Fe1: key[1], Fe2: key[2], Fe3: key[3]},
Value: feaValue,
Details: false,
Persistence: hashdb.Persistence_PERSISTENCE_DATABASE,
BatchUuid: uuid,
TxIndex: 0,
BlockIndex: 0,
})
if err != nil {
return nil, err
}
var newValue []uint64
if result.NewValue != "" {
newValue, err = string2fea(result.NewValue)
if err != nil {
return nil, err
}
}
return &UpdateProof{
OldRoot: oldRoot,
NewRoot: []uint64{result.NewRoot.Fe0, result.NewRoot.Fe1, result.NewRoot.Fe2, result.NewRoot.Fe3},
Key: key,
NewValue: newValue,
}, nil
}
func (tree *StateTree) setProgram(ctx context.Context, key []uint64, data []byte, persistent bool, uuid string) error {
persistence := hashdb.Persistence_PERSISTENCE_TEMPORARY
if persistent {
persistence = hashdb.Persistence_PERSISTENCE_DATABASE
}
_, err := tree.grpcClient.SetProgram(ctx, &hashdb.SetProgramRequest{
Key: &hashdb.Fea{Fe0: key[0], Fe1: key[1], Fe2: key[2], Fe3: key[3]},
Data: data,
Persistence: persistence,
BatchUuid: uuid,
TxIndex: 0,
BlockIndex: 0,
})
return err
}
// Flush flushes all changes to the persistent storage.
func (tree *StateTree) Flush(ctx context.Context, newStateRoot common.Hash, uuid string) error {
flushRequest := &hashdb.FlushRequest{BatchUuid: uuid, NewStateRoot: newStateRoot.String(), Persistence: hashdb.Persistence_PERSISTENCE_DATABASE}
_, err := tree.grpcClient.Flush(ctx, flushRequest)
return err
}
// StartBlock starts a new block.
func (tree *StateTree) StartBlock(ctx context.Context, oldRoot common.Hash, uuid string) error {
startBlockRequest := &hashdb.StartBlockRequest{
BatchUuid: uuid,
OldStateRoot: oldRoot.String(),
Persistence: hashdb.Persistence_PERSISTENCE_DATABASE}
_, err := tree.grpcClient.StartBlock(ctx, startBlockRequest)
return err
}
// FinishBlock finishes a block.
func (tree *StateTree) FinishBlock(ctx context.Context, newRoot common.Hash, uuid string) error {
finishBlockRequest := &hashdb.FinishBlockRequest{
BatchUuid: uuid,
NewStateRoot: newRoot.String(),
Persistence: hashdb.Persistence_PERSISTENCE_DATABASE}
_, err := tree.grpcClient.FinishBlock(ctx, finishBlockRequest)
return err
}