/
sync_committee.go
57 lines (50 loc) · 1.87 KB
/
sync_committee.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
package goclient
import (
"context"
"fmt"
"time"
"github.com/attestantio/go-eth2-client/api"
eth2apiv1 "github.com/attestantio/go-eth2-client/api/v1"
"github.com/attestantio/go-eth2-client/spec"
"github.com/attestantio/go-eth2-client/spec/altair"
"github.com/attestantio/go-eth2-client/spec/phase0"
)
// SyncCommitteeDuties returns sync committee duties for a given epoch
func (gc *goClient) SyncCommitteeDuties(ctx context.Context, epoch phase0.Epoch, validatorIndices []phase0.ValidatorIndex) ([]*eth2apiv1.SyncCommitteeDuty, error) {
resp, err := gc.client.SyncCommitteeDuties(ctx, &api.SyncCommitteeDutiesOpts{
Epoch: epoch,
Indices: validatorIndices,
})
if err != nil {
return nil, fmt.Errorf("failed to obtain sync committee duties: %w", err)
}
if resp == nil {
return nil, fmt.Errorf("sync committee duties response is nil")
}
return resp.Data, nil
}
// GetSyncMessageBlockRoot returns beacon block root for sync committee
func (gc *goClient) GetSyncMessageBlockRoot(slot phase0.Slot) (phase0.Root, spec.DataVersion, error) {
reqStart := time.Now()
resp, err := gc.client.BeaconBlockRoot(gc.ctx, &api.BeaconBlockRootOpts{
Block: "head",
})
if err != nil {
return phase0.Root{}, DataVersionNil, fmt.Errorf("failed to obtain beacon block root: %w", err)
}
if resp == nil {
return phase0.Root{}, DataVersionNil, fmt.Errorf("beacon block root response is nil")
}
if resp.Data == nil {
return phase0.Root{}, DataVersionNil, fmt.Errorf("beacon block root data is nil")
}
metricsSyncCommitteeDataRequest.Observe(time.Since(reqStart).Seconds())
return *resp.Data, spec.DataVersionAltair, nil
}
// SubmitSyncMessage submits a signed sync committee msg
func (gc *goClient) SubmitSyncMessage(msg *altair.SyncCommitteeMessage) error {
if err := gc.client.SubmitSyncCommitteeMessages(gc.ctx, []*altair.SyncCommitteeMessage{msg}); err != nil {
return err
}
return nil
}