-
Notifications
You must be signed in to change notification settings - Fork 7
/
parameters.go
162 lines (132 loc) · 3.27 KB
/
parameters.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
package eth2api
import (
"strconv"
"strings"
"github.com/protolambda/zrnt/eth2/beacon/common"
)
type ValidatorId interface {
ValidatorId() string
}
type ValidatorIdPubkey common.BLSPubkey
func (v ValidatorIdPubkey) ValidatorId() string {
return common.BLSPubkey(v).String()
}
type ValidatorIdIndex common.ValidatorIndex
func (v ValidatorIdIndex) ValidatorId() string {
return strconv.FormatUint(uint64(v), 10)
}
func ParseValidatorId(v string) (ValidatorId, error) {
if strings.HasPrefix(v, "0x") {
var pub common.BLSPubkey
if err := pub.UnmarshalText([]byte(v)); err != nil {
return nil, err
}
return ValidatorIdPubkey(pub), nil
}
n, err := strconv.ParseUint(v, 10, 64)
if err != nil {
return nil, err
}
return ValidatorIdIndex(n), nil
}
type ValidatorIdFilter []ValidatorId
func (vidf ValidatorIdFilter) String() string {
var out strings.Builder
for i := range vidf {
out.WriteString(vidf[i].ValidatorId())
if i+1 < len(vidf) {
out.WriteRune(',')
}
}
return out.String()
}
type StateId interface {
StateId() string
}
type StateIdRoot common.Root
func (r StateIdRoot) StateId() string {
return common.Root(r).String()
}
type StateIdSlot common.Slot
func (s StateIdSlot) StateId() string {
return strconv.FormatUint(uint64(s), 10)
}
type StateIdStrMode string
func (m StateIdStrMode) StateId() string {
return string(m)
}
const (
StateHead StateIdStrMode = "head"
StateGenesis StateIdStrMode = "genesis"
StateFinalized StateIdStrMode = "finalized"
StateJustified StateIdStrMode = "justified"
)
func ParseStateId(v string) (StateId, error) {
if strings.HasPrefix(v, "0x") {
var root common.Root
if err := root.UnmarshalText([]byte(v)); err != nil {
return nil, err
}
return StateIdRoot(root), nil
}
asMode := StateIdStrMode(v)
if asMode == StateHead || asMode == StateGenesis || asMode == StateFinalized || asMode == StateJustified {
return asMode, nil
}
n, err := strconv.ParseUint(v, 10, 64)
if err != nil {
return nil, err
}
return StateIdSlot(n), nil
}
// Block identifier
// Can have different shapes. E.g. BlockHead, BlockIdSlot(123), BlockIdRoot(Root{0x...})
type BlockId interface {
BlockId() string
}
type BlockIdRoot common.Root
func (r BlockIdRoot) BlockId() string {
return common.Root(r).String()
}
type BlockIdSlot common.Slot
func (s BlockIdSlot) BlockId() string {
return strconv.FormatUint(uint64(s), 10)
}
type BlockIdStrMode string
func (m BlockIdStrMode) BlockId() string {
return string(m)
}
const (
BlockHead BlockIdStrMode = "head"
BlockGenesis BlockIdStrMode = "genesis"
BlockFinalized BlockIdStrMode = "finalized"
)
func ParseBlockId(v string) (BlockId, error) {
if strings.HasPrefix(v, "0x") {
var root common.Root
if err := root.UnmarshalText([]byte(v)); err != nil {
return nil, err
}
return BlockIdRoot(root), nil
}
asMode := BlockIdStrMode(v)
if asMode == BlockHead || asMode == BlockGenesis || asMode == BlockFinalized {
return asMode, nil
}
n, err := strconv.ParseUint(v, 10, 64)
if err != nil {
return nil, err
}
return BlockIdSlot(n), nil
}
type StatusFilter []ValidatorStatus
func (sf StatusFilter) String() string {
var out strings.Builder
for i := range sf {
out.WriteString(string(sf[i]))
if i+1 < len(sf) {
out.WriteRune(',')
}
}
return out.String()
}