-
Notifications
You must be signed in to change notification settings - Fork 978
/
historical_roots.go
81 lines (69 loc) · 2.05 KB
/
historical_roots.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
package customtypes
import (
"fmt"
fssz "github.com/prysmaticlabs/fastssz"
)
var _ fssz.HashRoot = (HistoricalRoots)([][32]byte{})
var _ fssz.Marshaler = (*HistoricalRoots)(nil)
var _ fssz.Unmarshaler = (*HistoricalRoots)(nil)
// HistoricalRoots represents a 32 bytes HistoricalRoots object in Ethereum beacon chain consensus.
type HistoricalRoots [][32]byte
// HashTreeRoot returns calculated hash root.
func (r HistoricalRoots) HashTreeRoot() ([32]byte, error) {
return fssz.HashWithDefaultHasher(r)
}
// HashTreeRootWith hashes a HistoricalRoots object with a Hasher from the default HasherPool.
func (r HistoricalRoots) HashTreeRootWith(hh *fssz.Hasher) error {
index := hh.Index()
for _, sRoot := range r {
hh.Append(sRoot[:])
}
hh.Merkleize(index)
return nil
}
// UnmarshalSSZ deserializes the provided bytes buffer into the HistoricalRoots object.
func (r *HistoricalRoots) UnmarshalSSZ(buf []byte) error {
if len(buf) != r.SizeSSZ() {
return fmt.Errorf("expected buffer of length %d received %d", r.SizeSSZ(), len(buf))
}
mixes := make([][32]byte, len(buf)/32)
for i := range mixes {
copy(mixes[i][:], buf[i*32:(i+1)*32])
}
*r = mixes
return nil
}
// MarshalSSZTo marshals HistoricalRoots with the provided byte slice.
func (r HistoricalRoots) MarshalSSZTo(dst []byte) ([]byte, error) {
marshalled, err := r.MarshalSSZ()
if err != nil {
return nil, err
}
return append(dst, marshalled...), nil
}
// MarshalSSZ marshals HistoricalRoots into a serialized object.
func (r HistoricalRoots) MarshalSSZ() ([]byte, error) {
marshalled := make([]byte, len(r)*32)
for i, r32 := range r {
for j, rr := range r32 {
marshalled[i*32+j] = rr
}
}
return marshalled, nil
}
// SizeSSZ returns the size of the serialized object.
func (r HistoricalRoots) SizeSSZ() int {
return len(r) * 32
}
// Slice converts a customtypes.HistoricalRoots object into a 2D byte slice.
func (r HistoricalRoots) Slice() [][]byte {
if r == nil {
return nil
}
hRoots := make([][]byte, len(r))
for i, root := range r {
tmp := root
hRoots[i] = tmp[:]
}
return hRoots
}