-
Notifications
You must be signed in to change notification settings - Fork 25
/
general.go
196 lines (149 loc) · 3.96 KB
/
general.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package common
import (
"fmt"
"github.com/protolambda/ztyp/codec"
"github.com/protolambda/ztyp/tree"
. "github.com/protolambda/ztyp/view"
)
type Root = tree.Root
type Bytes32 = Root
const Bytes32Type = RootType
type CommitteeIndex Uint64View
func (i *CommitteeIndex) Deserialize(dr *codec.DecodingReader) error {
return (*Uint64View)(i).Deserialize(dr)
}
func (i CommitteeIndex) Serialize(w *codec.EncodingWriter) error {
return w.WriteUint64(uint64(i))
}
func (CommitteeIndex) ByteLength() uint64 {
return 8
}
func (CommitteeIndex) FixedLength() uint64 {
return 8
}
func (i CommitteeIndex) HashTreeRoot(hFn tree.HashFn) Root {
return Uint64View(i).HashTreeRoot(hFn)
}
func (e CommitteeIndex) MarshalJSON() ([]byte, error) {
return Uint64View(e).MarshalJSON()
}
func (e *CommitteeIndex) UnmarshalJSON(b []byte) error {
return ((*Uint64View)(e)).UnmarshalJSON(b)
}
func (e CommitteeIndex) String() string {
return Uint64View(e).String()
}
const CommitteeIndexType = Uint64Type
func AsCommitteeIndex(v View, err error) (CommitteeIndex, error) {
i, err := AsUint64(v, err)
return CommitteeIndex(i), err
}
type Gwei Uint64View
func (g *Gwei) Deserialize(dr *codec.DecodingReader) error {
return (*Uint64View)(g).Deserialize(dr)
}
func (i Gwei) Serialize(w *codec.EncodingWriter) error {
return w.WriteUint64(uint64(i))
}
func (Gwei) ByteLength() uint64 {
return 8
}
func (g Gwei) FixedLength() uint64 {
return 8
}
func (g Gwei) HashTreeRoot(hFn tree.HashFn) Root {
return Uint64View(g).HashTreeRoot(hFn)
}
func (e Gwei) MarshalJSON() ([]byte, error) {
return Uint64View(e).MarshalJSON()
}
func (e *Gwei) UnmarshalJSON(b []byte) error {
return ((*Uint64View)(e)).UnmarshalJSON(b)
}
func (e Gwei) String() string {
return Uint64View(e).String()
}
const GweiType = Uint64Type
func AsGwei(v View, err error) (Gwei, error) {
i, err := AsUint64(v, err)
return Gwei(i), err
}
type Checkpoint struct {
Epoch Epoch `json:"epoch" yaml:"epoch"`
Root Root `json:"root" yaml:"root"`
}
func (c *Checkpoint) String() string {
return c.Root.String() + ":" + c.Epoch.String()
}
func (c *Checkpoint) Deserialize(dr *codec.DecodingReader) error {
return dr.FixedLenContainer(&c.Epoch, &c.Root)
}
func (a *Checkpoint) Serialize(w *codec.EncodingWriter) error {
return w.FixedLenContainer(a.Epoch, &a.Root)
}
func (a *Checkpoint) ByteLength() uint64 {
return 8 + 32
}
func (g *Checkpoint) FixedLength() uint64 {
return 8 + 32
}
func (c *Checkpoint) HashTreeRoot(hFn tree.HashFn) Root {
return hFn.HashTreeRoot(c.Epoch, c.Root)
}
func (c *Checkpoint) View() *CheckpointView {
r := RootView(c.Root)
res, _ := CheckpointType.FromFields(Uint64View(c.Epoch), &r)
return &CheckpointView{res}
}
var CheckpointType = ContainerType("Checkpoint", []FieldDef{
{"epoch", EpochType},
{"root", RootType},
})
func (c *Phase0Preset) CheckPoint() *ContainerTypeDef {
return ContainerType("Checkpoint", []FieldDef{
{"epoch", EpochType},
{"root", RootType},
})
}
type CheckpointView struct {
*ContainerView
}
func (v *CheckpointView) Set(ch *Checkpoint) error {
return v.SetBacking(ch.View().Backing())
}
func (v *CheckpointView) Epoch() (Epoch, error) {
return AsEpoch(v.Get(0))
}
func (v *CheckpointView) Root() (Root, error) {
return AsRoot(v.Get(0))
}
func (v *CheckpointView) Raw() (Checkpoint, error) {
epoch, err := AsEpoch(v.Get(0))
if err != nil {
return Checkpoint{}, err
}
root, err := AsRoot(v.Get(1))
if err != nil {
return Checkpoint{}, err
}
return Checkpoint{Epoch: epoch, Root: root}, nil
}
func AsCheckPoint(v View, err error) (*CheckpointView, error) {
c, err := AsContainer(v, err)
return &CheckpointView{c}, err
}
type NodeRef struct {
Slot Slot
// Block root, may be equal to parent root if empty
Root Root
}
func (n NodeRef) String() string {
return n.Root.String() + ":" + n.Slot.String()
}
type ExtendedNodeRef struct {
NodeRef
ParentRoot Root
}
func (n ExtendedNodeRef) String() string {
return fmt.Sprintf("%s:%d (parent %s)", n.Root, n.Slot, n.ParentRoot)
}