-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathbeacon.go
185 lines (150 loc) · 4.87 KB
/
beacon.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
package http
import (
"fmt"
consensus "github.com/umbracle/go-eth-consensus"
)
type BeaconEndpoint struct {
c *Client
}
func (c *Client) Beacon() *BeaconEndpoint {
return &BeaconEndpoint{c: c}
}
type GenesisInfo struct {
Time uint64 `json:"genesis_time"`
Root [32]byte `json:"genesis_validators_root"`
Fork string `json:"genesis_fork_version"`
}
func (b *BeaconEndpoint) Genesis() (*GenesisInfo, error) {
var out GenesisInfo
err := b.c.Get("/eth/v1/beacon/genesis", &out)
return &out, err
}
func (b *BeaconEndpoint) SubmitCommitteeDuties(duties []*consensus.SyncCommitteeMessage) error {
err := b.c.Post("/eth/v1/beacon/pool/sync_committees", duties, nil)
return err
}
type ValidatorStatus string
const (
ValidatorStatusUnknown ValidatorStatus = "unknown"
ValidatorStatusActive ValidatorStatus = "active"
ValidatorStatusPending ValidatorStatus = "pending"
ValidatorStatusExited ValidatorStatus = "exited"
)
type Validator struct {
Index uint64 `json:"index"`
Balance uint64 `json:"balance"`
Status ValidatorStatus `json:"status"`
Validator *ValidatorMetadata `json:"validator"`
}
type ValidatorMetadata struct {
PubKey string `json:"pubkey"`
WithdrawalCredentials string `json:"withdrawal_credentials"`
EffectiveBalance uint64 `json:"effective_balance"`
Slashed bool `json:"slashed"`
ActivationElegibilityEpoch uint64 `json:"activation_eligibility_epoch"`
ActivationEpoch uint64 `json:"activation_epoch"`
ExitEpoch uint64 `json:"exit_epoch"`
WithdrawableEpoch uint64 `json:"withdrawable_epoch"`
}
type StateId interface {
StateID() string
}
type BlockId interface {
BlockID() string
}
type plainStateId string
func (s plainStateId) StateID() string {
return string(s)
}
func (s plainStateId) BlockID() string {
return s.StateID()
}
type Slot uint64
func (s Slot) StateID() string {
return fmt.Sprintf("%d", s)
}
func (s Slot) BlockID() string {
return s.StateID()
}
const (
Head plainStateId = "head"
Genesis plainStateId = "genesis"
Finalized plainStateId = "finalized"
)
func (b *BeaconEndpoint) GetRoot(id StateId) ([32]byte, error) {
var out struct {
Root [32]byte
}
err := b.c.Get("/eth/v1/beacon/states/"+id.StateID()+"/root", &out)
return out.Root, err
}
func (b *BeaconEndpoint) GetFork(id StateId) (*consensus.Fork, error) {
var out *consensus.Fork
err := b.c.Get("/eth/v1/beacon/states/"+id.StateID()+"/fork", &out)
return out, err
}
type FinalizedCheckpoints struct {
PreviousJustifiedCheckpoint *consensus.Checkpoint `json:"previous_justified"`
CurrentJustifiedCheckpoint *consensus.Checkpoint `json:"current_justified"`
FinalizedCheckpoint *consensus.Checkpoint `json:"finalized"`
}
func (b *BeaconEndpoint) GetFinalityCheckpoints(id StateId) (*FinalizedCheckpoints, error) {
var out *FinalizedCheckpoints
err := b.c.Get("/eth/v1/beacon/states/"+id.StateID()+"/finality_checkpoints", &out)
return out, err
}
func (b *BeaconEndpoint) GetValidators(id StateId) ([]*Validator, error) {
var out []*Validator
err := b.c.Get("/eth/v1/beacon/states/"+id.StateID()+"/validators", &out)
return out, err
}
func (b *BeaconEndpoint) GetValidatorByPubKey(pub string, id StateId) (*Validator, error) {
var out *Validator
err := b.c.Get("/eth/v1/beacon/states/"+id.StateID()+"/validators/"+pub, &out)
return out, err
}
func (b *BeaconEndpoint) PublishSignedBlock(block consensus.SignedBeaconBlock) error {
err := b.c.Post("/eth/v1/beacon/blocks", block, nil)
return err
}
func (b *BeaconEndpoint) PublishAttestations(data []*consensus.Attestation) error {
err := b.c.Post("/eth/v1/beacon/pool/attestations", data, nil)
return err
}
type Block struct {
Message consensus.BeaconBlock
Signature [96]byte
}
func (b *BeaconEndpoint) GetBlock(id BlockId, block consensus.BeaconBlock) (*Block, error) {
out := &Block{
Message: block,
}
err := b.c.Get("/eth/v2/beacon/blocks/"+id.BlockID(), out)
return out, err
}
type BlockHeaderResponse struct {
Root [32]byte `json:"root"`
Canonical bool `json:"canonical"`
Header *BlockHeader `json:"header"`
}
type BlockHeader struct {
Message *consensus.BeaconBlockHeader `json:"message"`
Signature [96]byte `json:"signature"`
}
func (b *BeaconEndpoint) GetBlockHeader(id BlockId) (*BlockHeaderResponse, error) {
var out *BlockHeaderResponse
err := b.c.Get("/eth/v1/beacon/headers/"+id.BlockID(), &out)
return out, err
}
func (b *BeaconEndpoint) GetBlockRoot(id BlockId) ([32]byte, error) {
var data struct {
Root [32]byte
}
err := b.c.Get("/eth/v1/beacon/blocks/"+id.BlockID()+"/root", &data)
return data.Root, err
}
func (b *BeaconEndpoint) GetBlockAttestations(id BlockId) ([]*consensus.Attestation, error) {
var out []*consensus.Attestation
err := b.c.Get("/eth/v1/beacon/blocks/"+id.BlockID()+"/attestations", &out)
return out, err
}