forked from ledgerwatch/erigon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
trie_from_witness.go
95 lines (86 loc) · 2.14 KB
/
trie_from_witness.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
package trie
import (
"fmt"
"github.com/holiman/uint256"
"github.com/nebojsa94/erigon/turbo/rlphacks"
)
func BuildTrieFromWitness(witness *Witness, trace bool) (*Trie, error) {
hb := NewHashBuilder(false)
for _, operator := range witness.Operators {
switch op := operator.(type) {
case *OperatorLeafValue:
if trace {
fmt.Printf("LEAF ")
}
keyHex := op.Key
val := op.Value
if err := hb.leaf(len(op.Key), keyHex, rlphacks.RlpSerializableBytes(val)); err != nil {
return nil, err
}
case *OperatorExtension:
if trace {
fmt.Printf("EXTENSION ")
}
if err := hb.extension(op.Key); err != nil {
return nil, err
}
case *OperatorBranch:
if trace {
fmt.Printf("BRANCH ")
}
if err := hb.branch(uint16(op.Mask)); err != nil {
return nil, err
}
case *OperatorHash:
if trace {
fmt.Printf("HASH ")
}
if err := hb.hash(op.Hash[:]); err != nil {
return nil, err
}
case *OperatorCode:
if trace {
fmt.Printf("CODE ")
}
if err := hb.code(op.Code); err != nil {
return nil, err
}
case *OperatorLeafAccount:
if trace {
fmt.Printf("ACCOUNTLEAF(code=%v storage=%v) ", op.HasCode, op.HasStorage)
}
balance := uint256.NewInt(0)
balance.SetBytes(op.Balance.Bytes())
nonce := op.Nonce
// FIXME: probably not needed, fix hb.accountLeaf
fieldSet := uint32(3)
if op.HasCode && op.HasStorage {
fieldSet = 15
}
// Incarnation is always needed for a hashbuilder.
// but it is just our implementation detail needed for contract self-descruction support with our
// db structure. Stateless clients don't access the DB so we can just pass 0 here.
incarnation := uint64(0)
if err := hb.accountLeaf(len(op.Key), op.Key, balance, nonce, incarnation, fieldSet, int(op.CodeSize)); err != nil {
return nil, err
}
case *OperatorEmptyRoot:
if trace {
fmt.Printf("EMPTYROOT ")
}
hb.emptyRoot()
default:
return nil, fmt.Errorf("unknown operand type: %T", operator)
}
}
if trace {
fmt.Printf("\n")
}
if !hb.hasRoot() {
return New(EmptyRoot), nil
}
r := hb.root()
tr := New(hb.rootHash())
tr.root = r
return tr, nil
}