-
Notifications
You must be signed in to change notification settings - Fork 8
/
validator.go
80 lines (66 loc) · 2.09 KB
/
validator.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
package types
import (
fmt "fmt"
fssz "github.com/ferranbt/fastssz"
)
var _ fssz.HashRoot = (ValidatorIndex)(0)
var _ fssz.Marshaler = (*ValidatorIndex)(nil)
var _ fssz.Unmarshaler = (*ValidatorIndex)(nil)
// ValidatorIndex in eth2.
type ValidatorIndex uint64
// Div divides validator index by x.
func (v ValidatorIndex) Div(x uint64) ValidatorIndex {
if x == 0 {
panic("divbyzero")
}
return ValidatorIndex(uint64(v) / x)
}
// Add increases validator index by x.
func (v ValidatorIndex) Add(x uint64) ValidatorIndex {
return ValidatorIndex(uint64(v) + x)
}
// Sub subtracts x from the validator index.
func (v ValidatorIndex) Sub(x uint64) ValidatorIndex {
if uint64(v) < x {
panic("underflow")
}
return ValidatorIndex(uint64(v) - x)
}
// Mod returns result of `validator index % x`.
func (v ValidatorIndex) Mod(x uint64) ValidatorIndex {
return ValidatorIndex(uint64(v) % x)
}
// HashTreeRoot returns calculated hash root.
func (v ValidatorIndex) HashTreeRoot() ([32]byte, error) {
return fssz.HashWithDefaultHasher(v)
}
// HashWithDefaultHasher hashes a HashRoot object with a Hasher from the default HasherPool.
func (v ValidatorIndex) HashTreeRootWith(hh *fssz.Hasher) error {
hh.PutUint64(uint64(v))
return nil
}
// UnmarshalSSZ deserializes the provided bytes buffer into the validator index object.
func (v *ValidatorIndex) UnmarshalSSZ(buf []byte) error {
if len(buf) != v.SizeSSZ() {
return fmt.Errorf("expected buffer of length %d received %d", v.SizeSSZ(), len(buf))
}
*v = ValidatorIndex(fssz.UnmarshallUint64(buf))
return nil
}
// MarshalSSZTo marshals validator index with the provided byte slice.
func (v *ValidatorIndex) MarshalSSZTo(dst []byte) ([]byte, error) {
marshalled, err := v.MarshalSSZ()
if err != nil {
return nil, err
}
return append(dst, marshalled...), nil
}
// MarshalSSZ marshals validator index into a serialized object.
func (v *ValidatorIndex) MarshalSSZ() ([]byte, error) {
marshalled := fssz.MarshalUint64([]byte{}, uint64(*v))
return marshalled, nil
}
// SizeSSZ returns the size of the serialized object.
func (v *ValidatorIndex) SizeSSZ() int {
return 8
}