forked from 0xPolygon/polygon-edge
/
utils.go
54 lines (42 loc) · 1.21 KB
/
utils.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
package storage
import (
"github.com/Gabulhas/polygon-external-consensus/types"
"github.com/umbracle/fastrlp"
)
type Forks []types.Hash
// MarshalRLPTo is a wrapper function for calling the type marshal implementation
func (f *Forks) MarshalRLPTo(dst []byte) []byte {
return types.MarshalRLPTo(f.MarshalRLPWith, dst)
}
// MarshalRLPWith is the actual RLP marshal implementation for the type
func (f *Forks) MarshalRLPWith(ar *fastrlp.Arena) *fastrlp.Value {
var vr *fastrlp.Value
if len(*f) == 0 {
vr = ar.NewNullArray()
} else {
vr = ar.NewArray()
for _, fork := range *f {
vr.Set(ar.NewCopyBytes(fork[:]))
}
}
return vr
}
// UnmarshalRLP is a wrapper function for calling the type unmarshal implementation
func (f *Forks) UnmarshalRLP(input []byte) error {
return types.UnmarshalRlp(f.UnmarshalRLPFrom, input)
}
// UnmarshalRLPFrom is the actual RLP unmarshal implementation for the type
func (f *Forks) UnmarshalRLPFrom(p *fastrlp.Parser, v *fastrlp.Value) error {
elems, err := v.GetElems()
if err != nil {
return err
}
forks := make([]types.Hash, len(elems))
for indx, elem := range elems {
if err := elem.GetHash(forks[indx][:]); err != nil {
return err
}
}
*f = forks
return nil
}