-
Notifications
You must be signed in to change notification settings - Fork 7
/
types.go
427 lines (370 loc) · 13.8 KB
/
types.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
package eth2api
import (
"bytes"
"encoding/json"
"fmt"
"strings"
"github.com/protolambda/zrnt/eth2/beacon"
"github.com/protolambda/zrnt/eth2/beacon/altair"
"github.com/protolambda/zrnt/eth2/beacon/bellatrix"
"github.com/protolambda/zrnt/eth2/beacon/capella"
"github.com/protolambda/zrnt/eth2/beacon/common"
"github.com/protolambda/zrnt/eth2/beacon/phase0"
"github.com/protolambda/ztyp/codec"
"github.com/protolambda/ztyp/view"
)
type GenesisResponse struct {
GenesisTime common.Timestamp `json:"genesis_time"`
GenesisValidatorsRoot common.Root `json:"genesis_validators_root"`
GenesisForkVersion common.Version `json:"genesis_fork_version"`
}
type DepositContractResponse struct {
// Id of Eth1 chain on which contract is deployed.
ChainID view.Uint64View `json:"chain_id"`
// Hex encoded deposit contract address with 0x prefix
Address common.Eth1Address `json:"address"`
}
type ChainHead struct {
Root common.Root `json:"root"`
Slot common.Slot `json:"slot"`
}
type RootResponse struct {
Root common.Root `json:"root"`
}
type FinalityCheckpoints struct {
PreviousJustified common.Checkpoint `json:"previous_justified"`
CurrentJustified common.Checkpoint `json:"current_justified"`
Finalized common.Checkpoint `json:"finalized"`
}
type BeaconBlockHeaderAndInfo struct {
Root common.Root `json:"root"`
Canonical bool `json:"canonical"`
Header common.SignedBeaconBlockHeader `json:"header"`
}
type SyncingStatus struct {
// Head slot node is trying to reach
HeadSlot common.Slot `json:"head_slot"`
// How many slots node needs to process to reach head. 0 if synced.
SyncDistance common.Slot `json:"sync_distance"`
}
// Wrapper around the original ProposerDuty response
type DependentProposerDuty struct {
// Duties are valid only on the chain with this given block root
DependentRoot common.Root `json:"dependent_root"`
Data []ProposerDuty `json:"data"`
}
type ProposerDuty struct {
Pubkey common.BLSPubkey `json:"pubkey"`
// Index of validator in validator registry
ValidatorIndex common.ValidatorIndex `json:"validator_index"`
// The slot at which the validator must propose block.
Slot common.Slot
}
type SyncCommitteeDuty struct {
Pubkey common.BLSPubkey `json:"pubkey"`
ValidatorIndex common.ValidatorIndex `json:"validator_index"`
ValidatorSyncCommitteeIndices []view.Uint64View `json:"validator_sync_committee_indices"`
}
// Wrapper around the original AttesterDuty response
type DependentAttesterDuties struct {
// Duties are valid only on the chain with this given block root
DependentRoot common.Root `json:"dependent_root"`
Data []AttesterDuty `json:"data"`
}
type AttesterDuty struct {
Pubkey common.BLSPubkey `json:"pubkey"`
// Index of validator in validator registry
ValidatorIndex common.ValidatorIndex `json:"validator_index"`
// The committee index
CommitteeIndex common.CommitteeIndex `json:"committee_index"`
// Number of validators in committee
CommitteeLength view.Uint64View `json:"committee_length"`
// Number of committees at the provided slot
CommitteesAtSlot view.Uint64View `json:"committees_at_slot"`
// Index of validator in committee
ValidatorCommitteeIndex view.Uint64View `json:"validator_committee_index"`
// The slot at which the validator must attest.
Slot common.Slot `json:"slot"`
}
type BeaconCommitteeSubscribeSignal struct {
ValidatorIndex common.ValidatorIndex `json:"validator_index"`
CommitteeIndex common.CommitteeIndex `json:"committee_index"`
// Number of committees at the returned slot
CommitteesAtSlot view.Uint64View `json:"committees_at_slot"`
// Should be slot at which validator is assigned to attest
Slot common.Slot `json:"slot"`
// Signals to BN that a validator on the VC has been chosen for aggregator role.
IsAggregator view.BoolView `json:"is_aggregator"`
}
type SyncCommitteeSubscribeSignal struct {
ValidatorIndex common.ValidatorIndex `json:"validator_index"`
// positions of the validator-index relative to the complete sync committee (a validator can have multiple)
SyncCommitteeIndices []view.Uint64View `json:"sync_committee_indices"`
UntilEpoch common.Epoch `json:"until_epoch"`
}
type NodeVersionResponse struct {
Version string `json:"version"`
}
type ValidatorResponse struct {
// Index of validator in validator registry.
Index common.ValidatorIndex `json:"index"`
// Current validator balance in gwei
Balance common.Gwei `json:"balance"`
// TODO
Status ValidatorStatus `json:"status"`
// The validator as defined in the registry in the BeaconState
Validator phase0.Validator `json:"validator"`
}
type ValidatorBalanceResponse struct {
// Index of validator in validator registry.
Index common.ValidatorIndex `json:"index"`
// Current validator balance in gwei
Balance common.Gwei `json:"balance"`
}
// TODO enum with or without additional values?
type ValidatorStatus string
func (vs ValidatorStatus) String() string {
return string(vs)
}
// status names known from lighthouse, differ from spec, TODO
const (
ValidatorStatusUnknown ValidatorStatus = "unknown"
ValidatorStatusWaitingForEligibility ValidatorStatus = "waiting_for_eligibility"
ValidatorStatusWaitingForFinality ValidatorStatus = "waiting_for_finality"
ValidatorStatusWaitingInQueue ValidatorStatus = "waiting_in_queue"
ValidatorStatusStandbyForActive ValidatorStatus = "standby_for_active"
ValidatorStatusActive ValidatorStatus = "active"
ValidatorStatusActiveAwaitingVoluntaryExit ValidatorStatus = "active_awaiting_voluntary_exit"
ValidatorStatusActiveAwaitingSlashedExit ValidatorStatus = "active_awaiting_slashed_exit"
ValidatorStatusExitedVoluntarily ValidatorStatus = "exited_voluntarily"
ValidatorStatusExitedSlashed ValidatorStatus = "exited_slashed"
ValidatorStatusWithdrawable ValidatorStatus = "withdrawable"
ValidatorStatusWithdrawn ValidatorStatus = "withdrawn"
)
type Committee struct {
// Committee index at a slot
Index common.CommitteeIndex `json:"index"`
// Slot the committee performs duties on
Slot common.Slot `json:"slot"`
// List of validator indices assigned to this committee
Validators []common.ValidatorIndex `json:"validators"`
}
type SyncCommittees struct {
// All of the validator indices in the current sync committee
Validators []common.ValidatorIndex `json:"validators"`
// Subcommittee slices of the current sync committee
ValidatorAggregates [][]common.ValidatorIndex `json:"validator_aggregates"`
}
type versionStruct struct {
Version string `json:"version"`
}
type blockDataStruct struct {
Data common.SpecObj `json:"data"`
}
type VersionedBeaconBlock struct {
Version string `json:"version"`
// Data is *phase0.BeaconBlock, *altair.BeaconBlock,
// *bellatrix.BeaconBlock or *sharding.BeaconBlock.
Data common.SpecObj `json:"data"`
}
func (v *VersionedBeaconBlock) UnmarshalJSON(b []byte) error {
var version versionStruct
if err := json.Unmarshal(b, &version); err != nil {
return err
}
var data blockDataStruct
switch strings.ToLower(v.Version) {
case "phase0":
data.Data = new(phase0.BeaconBlock)
case "altair":
data.Data = new(altair.BeaconBlock)
case "bellatrix":
data.Data = new(bellatrix.BeaconBlock)
case "capella":
data.Data = new(capella.BeaconBlock)
default:
return fmt.Errorf("unrecognized version: %q", version.Version)
}
if err := json.Unmarshal(b, &data); err != nil {
return err
}
v.Data = data.Data
v.Version = version.Version
return nil
}
type SignedBeaconBlock interface {
beacon.OpaqueBlock
}
type signedBlockDataStruct struct {
Data SignedBeaconBlock `json:"data"`
}
type VersionedSignedBeaconBlock struct {
Version string `json:"version"`
// Data is *phase0.SignedBeaconBlock, *altair.SignedBeaconBlock,
// *bellatrix.SignedBeaconBlock or *sharding.SignedBeaconBlock.
Data SignedBeaconBlock `json:"data"`
}
func (v *VersionedSignedBeaconBlock) UnmarshalJSON(b []byte) error {
var version versionStruct
if err := json.Unmarshal(b, &version); err != nil {
return err
}
var data signedBlockDataStruct
switch strings.ToLower(version.Version) {
case "phase0":
data.Data = new(phase0.SignedBeaconBlock)
case "altair":
data.Data = new(altair.SignedBeaconBlock)
case "bellatrix":
data.Data = new(bellatrix.SignedBeaconBlock)
case "capella":
data.Data = new(capella.SignedBeaconBlock)
default:
return fmt.Errorf("unrecognized version: %q", version.Version)
}
if err := json.Unmarshal(b, &data); err != nil {
return err
}
v.Data = data.Data
v.Version = version.Version
return nil
}
type stateDataStruct struct {
Data common.SpecObj `json:"data"`
}
type VersionedBeaconState struct {
Version string `json:"version"`
// Data is *phase0.BeaconState, *altair.BeaconState, *bellatrix.BeaconState or *sharding.BeaconState.
// See the Tree(spec) method to transform into a binary-tree backed state for advanced processing.
Data common.SpecObj `json:"data"`
}
func (v *VersionedBeaconState) Tree(spec *common.Spec) (common.BeaconState, error) {
if v.Data == nil {
return nil, fmt.Errorf("no state (version: %q)", v.Version)
}
var buf bytes.Buffer
w := codec.NewEncodingWriter(&buf)
if err := v.Data.Serialize(spec, w); err != nil {
return nil, err
}
data := buf.Bytes()
r := codec.NewDecodingReader(bytes.NewReader(data), uint64(len(data)))
switch strings.ToLower(v.Version) {
case "phase0":
return phase0.AsBeaconStateView(phase0.BeaconStateType(spec).Deserialize(r))
case "altair":
return altair.AsBeaconStateView(altair.BeaconStateType(spec).Deserialize(r))
case "bellatrix":
return bellatrix.AsBeaconStateView(bellatrix.BeaconStateType(spec).Deserialize(r))
case "capella":
return capella.AsBeaconStateView(capella.BeaconStateType(spec).Deserialize(r))
default:
return nil, fmt.Errorf("unrecognized version: %q", v.Version)
}
}
func (v *VersionedBeaconState) UnmarshalJSON(b []byte) error {
var version versionStruct
if err := json.Unmarshal(b, &version); err != nil {
return err
}
var data stateDataStruct
switch strings.ToLower(version.Version) {
case "phase0":
data.Data = new(phase0.BeaconState)
case "altair":
data.Data = new(altair.BeaconState)
case "bellatrix":
data.Data = new(bellatrix.BeaconState)
case "capella":
data.Data = new(capella.BeaconState)
default:
return fmt.Errorf("unrecognized version: %q", version.Version)
}
if err := json.Unmarshal(b, &data); err != nil {
return err
}
v.Data = data.Data
v.Version = version.Version
return nil
}
// Network identity data, not typed in detail,
// using libp2p and discv5 dependencies for further processing is optional.
type NetworkIdentity struct {
PeerID ApiPeerId `json:"peer_id"`
// Ethereum node record. [Read more](https://eips.ethereum.org/EIPS/eip-778)
ENR string `json:"enr"`
// Node's addresses on which eth2 rpc requests are served. [Read more](https://docs.libp2p.io/reference/glossary/#multiaddr)
P2PAddresses []string `json:"p2p_addresses"`
// Node's addresses on which is listening for discv5 requests. [Read more](https://docs.libp2p.io/reference/glossary/#multiaddr)
DiscoveryAddresses []string `json:"discovery_addresses"`
// Based on eth2 Metadata object
Metadata common.MetaData `json:"metadata"`
}
// Retrieves number of known peers.
type PeerCountResponse struct {
Disconnected view.Uint64View `json:"disconnected"`
Connecting view.Uint64View `json:"connecting"`
Connected view.Uint64View `json:"connected"`
Disconnecting view.Uint64View `json:"disconnecting"`
}
type Peer struct {
PeerID ApiPeerId `json:"peer_id"`
ENR string `json:"enr"`
LastSeenP2PAddress string `json:"last_seen_p2p_address"`
State ConnectionState `json:"state"`
Direction ConnectionDirection `json:"direction"`
}
type ConnectionDirection string
const (
ConnectionDirectionInbound ConnectionDirection = "inbound"
ConnectionDirectionOutbound ConnectionDirection = "outbound"
)
type ConnectionState string
const (
ConnectionStateDisconnected ConnectionState = "disconnected"
ConnectionStateConnecting ConnectionState = "connecting"
ConnectionStateConnected ConnectionState = "connected"
ConnectionStateDisconnecting ConnectionState = "disconnecting"
)
// Cryptographic hash of a peer’s public key. [Read more](https://docs.libp2p.io/concepts/peer-id/)
// Further (optional) processing in Go with: https://github.com/libp2p/go-libp2p-core/blob/a39b84ea2e340466d57fdb342c7d62f12957d972/peer/peer.go#L42
type ApiPeerId string
type IndexedErrorMessageItem struct {
// Index of item in the request list that caused the error
Index uint `json:"index"`
// Message describing error
Message string `json:"message"`
}
type ErrorMessage struct {
// Either specific error code in case of invalid request or http status code
CodeValue uint `json:"code"`
// Message describing error
Message string `json:"message"`
// Optional stacktraces, sent when node is in debug mode
Stacktraces []string `json:"stacktraces,omitempty"`
// Optional list of individual items that have failed
Failures []IndexedErrorMessageItem `json:"failures,omitempty"`
}
func (err *ErrorMessage) Code() uint {
return err.CodeValue
}
func (err *ErrorMessage) Error() string {
out := fmt.Sprintf("Error(%d): %s", err.CodeValue, err.Message)
if len(err.Failures) > 0 {
out += fmt.Sprintf("\n%d failures:\n", len(err.Failures))
for i, item := range err.Failures {
out += fmt.Sprintf("failure %d item %d error: %s\n", i, item.Index, item.Message)
}
}
if len(err.Stacktraces) > 0 {
out += "\n" + strings.Join(err.Stacktraces, "\n")
}
return out
}
func (m *ErrorMessage) IndexedErrors() []IndexedErrorMessageItem {
return m.Failures
}
// Optional error enhancement, applicable for API methods that can have a partial success.
type IndexedError interface {
IndexedErrors() []IndexedErrorMessageItem
}