-
Notifications
You must be signed in to change notification settings - Fork 25
/
sync_bits.go
174 lines (139 loc) · 5.38 KB
/
sync_bits.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package altair
import (
"bytes"
"fmt"
"github.com/protolambda/zrnt/eth2/beacon/common"
"github.com/protolambda/ztyp/bitfields"
"github.com/protolambda/ztyp/codec"
"github.com/protolambda/ztyp/conv"
"github.com/protolambda/ztyp/tree"
. "github.com/protolambda/ztyp/view"
)
// SyncCommitteeSubnetBits is formatted as a serialized SSZ bitvector,
// with trailing zero bits if length does not align with byte length.
type SyncCommitteeSubnetBits []byte
func (li SyncCommitteeSubnetBits) View(spec *common.Spec) *SyncCommitteeSubnetBitsView {
v, _ := SyncCommitteeSubnetBitsType(spec).Deserialize(codec.NewDecodingReader(bytes.NewReader(li), uint64(len(li))))
return &SyncCommitteeSubnetBitsView{v.(*BitVectorView)}
}
func (li *SyncCommitteeSubnetBits) Deserialize(spec *common.Spec, dr *codec.DecodingReader) error {
return dr.BitVector((*[]byte)(li), uint64(spec.SYNC_COMMITTEE_SIZE)/common.SYNC_COMMITTEE_SUBNET_COUNT)
}
func (li SyncCommitteeSubnetBits) Serialize(spec *common.Spec, w *codec.EncodingWriter) error {
return w.BitVector(li[:])
}
func (li SyncCommitteeSubnetBits) ByteLength(spec *common.Spec) uint64 {
return (uint64(spec.SYNC_COMMITTEE_SIZE) + 7) / 8
}
func (li *SyncCommitteeSubnetBits) FixedLength(spec *common.Spec) uint64 {
return (uint64(spec.SYNC_COMMITTEE_SIZE) + 7) / 8
}
func (li SyncCommitteeSubnetBits) HashTreeRoot(spec *common.Spec, hFn tree.HashFn) common.Root {
return hFn.BitVectorHTR(li)
}
func (li SyncCommitteeSubnetBits) MarshalText() ([]byte, error) {
return conv.BytesMarshalText(li[:])
}
func (li *SyncCommitteeSubnetBits) UnmarshalText(text []byte) error {
return conv.DynamicBytesUnmarshalText((*[]byte)(li), text)
}
func (li SyncCommitteeSubnetBits) String() string {
return conv.BytesString(li[:])
}
func (li SyncCommitteeSubnetBits) GetBit(i uint64) bool {
return bitfields.GetBit(li, i)
}
func (li SyncCommitteeSubnetBits) SetBit(i uint64, v bool) {
bitfields.SetBit(li, i, v)
}
func (li SyncCommitteeSubnetBits) OnesCount() uint64 {
return bitfields.BitlistOnesCount(li)
}
type SyncCommitteeSubnetBitsView struct {
*BitVectorView
}
func AsSyncCommitteeSubnetBits(v View, err error) (*SyncCommitteeSubnetBitsView, error) {
c, err := AsBitVector(v, err)
return &SyncCommitteeSubnetBitsView{c}, err
}
func (v *SyncCommitteeSubnetBitsView) Raw(spec *common.Spec) (SyncCommitteeSubnetBits, error) {
byteLen := int((spec.SYNC_COMMITTEE_SIZE/common.SYNC_COMMITTEE_SUBNET_COUNT + 7) / 8)
var buf bytes.Buffer
buf.Grow(byteLen)
if err := v.Serialize(codec.NewEncodingWriter(&buf)); err != nil {
return nil, err
}
out := SyncCommitteeSubnetBits(buf.Bytes())
if len(out) != byteLen {
return nil, fmt.Errorf("failed to convert sync committee tree bits view to raw bits")
}
return out, nil
}
func SyncCommitteeSubnetBitsType(spec *common.Spec) *BitVectorTypeDef {
return BitVectorType(uint64(spec.SYNC_COMMITTEE_SIZE) / common.SYNC_COMMITTEE_SUBNET_COUNT)
}
// SyncCommitteeBits is formatted as a serialized SSZ bitvector,
// with trailing zero bits if length does not align with byte length.
type SyncCommitteeBits []byte
func (li SyncCommitteeBits) View(spec *common.Spec) *SyncCommitteeBitsView {
v, _ := SyncCommitteeBitsType(spec).Deserialize(codec.NewDecodingReader(bytes.NewReader(li), uint64(len(li))))
return &SyncCommitteeBitsView{v.(*BitVectorView)}
}
func (li *SyncCommitteeBits) Deserialize(spec *common.Spec, dr *codec.DecodingReader) error {
return dr.BitVector((*[]byte)(li), uint64(spec.SYNC_COMMITTEE_SIZE))
}
func (li SyncCommitteeBits) Serialize(spec *common.Spec, w *codec.EncodingWriter) error {
return w.BitVector(li[:])
}
func (li SyncCommitteeBits) ByteLength(spec *common.Spec) uint64 {
return (uint64(spec.SYNC_COMMITTEE_SIZE) + 7) / 8
}
func (li *SyncCommitteeBits) FixedLength(spec *common.Spec) uint64 {
return (uint64(spec.SYNC_COMMITTEE_SIZE) + 7) / 8
}
func (li SyncCommitteeBits) HashTreeRoot(spec *common.Spec, hFn tree.HashFn) common.Root {
if li == nil {
// By default, we should initialize the full bits.
// We can't do that in the struct case dynamically based on preset, but we can at least output the correct HTR.
return SyncCommitteeBitsType(spec).New().HashTreeRoot(hFn)
}
return hFn.BitVectorHTR(li)
}
func (li SyncCommitteeBits) MarshalText() ([]byte, error) {
return conv.BytesMarshalText(li[:])
}
func (li *SyncCommitteeBits) UnmarshalText(text []byte) error {
return conv.DynamicBytesUnmarshalText((*[]byte)(li), text)
}
func (li SyncCommitteeBits) String() string {
return conv.BytesString(li[:])
}
func (li SyncCommitteeBits) GetBit(i uint64) bool {
return bitfields.GetBit(li, i)
}
func (li SyncCommitteeBits) SetBit(i uint64, v bool) {
bitfields.SetBit(li, i, v)
}
type SyncCommitteeBitsView struct {
*BitVectorView
}
func AsSyncCommitteeBits(v View, err error) (*SyncCommitteeBitsView, error) {
c, err := AsBitVector(v, err)
return &SyncCommitteeBitsView{c}, err
}
func (v *SyncCommitteeBitsView) Raw(spec *common.Spec) (SyncCommitteeBits, error) {
byteLen := int((spec.SYNC_COMMITTEE_SIZE + 7) / 8)
var buf bytes.Buffer
buf.Grow(byteLen)
if err := v.Serialize(codec.NewEncodingWriter(&buf)); err != nil {
return nil, err
}
out := SyncCommitteeBits(buf.Bytes())
if len(out) != byteLen {
return nil, fmt.Errorf("failed to convert sync committee tree bits view to raw bits")
}
return out, nil
}
func SyncCommitteeBitsType(spec *common.Spec) *BitVectorTypeDef {
return BitVectorType(uint64(spec.SYNC_COMMITTEE_SIZE))
}