-
Notifications
You must be signed in to change notification settings - Fork 20
/
ssz.go
154 lines (128 loc) · 3.4 KB
/
ssz.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
package types
import (
"encoding/binary"
"fmt"
"github.com/attestantio/go-eth2-client/spec/bellatrix"
"github.com/attestantio/go-eth2-client/spec/capella"
ssz "github.com/ferranbt/fastssz"
)
type SSZUint64 uint64
func (s SSZUint64) GetTree() (*ssz.Node, error) {
return ssz.ProofTree(s)
}
func (s SSZUint64) HashTreeRootWith(hh ssz.HashWalker) error {
indx := hh.Index()
hh.PutUint64(uint64(s))
hh.Merkleize(indx)
return nil
}
// HashTreeRoot --
func (s SSZUint64) HashTreeRoot() ([32]byte, error) {
buf := make([]byte, 8)
binary.LittleEndian.PutUint64(buf, uint64(s))
var root [32]byte
copy(root[:], buf)
return root, nil
}
// SSZBytes --
type SSZBytes []byte
func (b SSZBytes) HashTreeRoot() ([32]byte, error) {
return ssz.HashWithDefaultHasher(b)
}
func (b SSZBytes) GetTree() (*ssz.Node, error) {
return ssz.ProofTree(b)
}
func (b SSZBytes) HashTreeRootWith(hh ssz.HashWalker) error {
indx := hh.Index()
hh.PutBytes(b)
hh.Merkleize(indx)
return nil
}
// SSZTransactions --
type SSZTransactions []bellatrix.Transaction
func (b SSZTransactions) GetTree() (*ssz.Node, error) {
return ssz.ProofTree(b)
}
func (b SSZTransactions) HashTreeRootWith(hh ssz.HashWalker) error {
// taken from https://github.com/prysmaticlabs/prysm/blob/develop/encoding/ssz/htrutils.go#L97-L119
subIndx := hh.Index()
num := uint64(len(b))
if num > 1048576 {
return ssz.ErrIncorrectListSize
}
for _, elem := range b {
{
elemIndx := hh.Index()
byteLen := uint64(len(elem))
if byteLen > 1073741824 {
return ssz.ErrIncorrectListSize
}
hh.AppendBytes32(elem)
hh.MerkleizeWithMixin(elemIndx, byteLen, (1073741824+31)/32)
}
}
hh.MerkleizeWithMixin(subIndx, num, 1048576)
return nil
}
// HashTreeRoot --
func (b SSZTransactions) HashTreeRoot() ([32]byte, error) {
return ssz.HashWithDefaultHasher(b)
}
// SSZWithdrawals --
type SSZWithdrawals []*capella.Withdrawal
func (b SSZWithdrawals) GetTree() (*ssz.Node, error) {
return ssz.ProofTree(b)
}
func (b SSZWithdrawals) HashTreeRootWith(hh ssz.HashWalker) error {
// taken from https://github.com/attestantio/go-eth2-client/blob/bc14358487b6d32cb45feef14b170458abc5d14a/spec/capella/executionpayload_ssz.go#L332-L346
subIndx := hh.Index()
num := uint64(len(b))
if num > 16 {
return ssz.ErrIncorrectListSize
}
for _, elem := range b {
if err := elem.HashTreeRootWith(hh); err != nil {
return err
}
}
hh.MerkleizeWithMixin(subIndx, num, 16)
return nil
}
// HashTreeRoot --
func (b SSZWithdrawals) HashTreeRoot() ([32]byte, error) {
return ssz.HashWithDefaultHasher(b)
}
// SSZ32Bytes --
type SSZ32Bytes [32]byte
func (b SSZ32Bytes) HashTreeRoot() ([32]byte, error) {
return ssz.HashWithDefaultHasher(b)
}
func (b SSZ32Bytes) GetTree() (*ssz.Node, error) {
return ssz.ProofTree(b)
}
func (b SSZ32Bytes) HashTreeRootWith(hh ssz.HashWalker) error {
indx := hh.Index()
hh.PutBytes(b[:])
hh.Merkleize(indx)
return nil
}
// UnmarshalSSZ --
func (b *SSZ32Bytes) UnmarshalSSZ(buf []byte) error {
if len(buf) != b.SizeSSZ() {
return fmt.Errorf("expected buffer of length %d received %d", b.SizeSSZ(), len(buf))
}
copy(b[:], buf[:])
return nil
}
// MarshalSSZTo --
func (b SSZ32Bytes) MarshalSSZTo(dst []byte) ([]byte, error) {
return append(dst, b[:]...), nil
}
// MarshalSSZ --
func (b SSZ32Bytes) MarshalSSZ() ([]byte, error) {
return b[:], nil
}
// SizeSSZ returns the size of the serialized object.
func (b SSZ32Bytes) SizeSSZ() int {
return 32
}