Add --period to "synccommittee members".

This commit is contained in:
Jim McDonald
2021-10-30 20:54:11 +01:00
parent ac18cbab3e
commit f70abb2165
4 changed files with 44 additions and 12 deletions

View File

@@ -1,3 +1,6 @@
development:
- add --period to "synccommittee members", can be "current", "next"
1.14.0:
- add "chain verify signedcontributionandproof"
- show both block and body root in "block info"

View File

@@ -18,7 +18,6 @@ import (
"time"
eth2client "github.com/attestantio/go-eth2-client"
spec "github.com/attestantio/go-eth2-client/spec/phase0"
"github.com/pkg/errors"
"github.com/spf13/viper"
"github.com/wealdtech/ethdo/services/chaintime"
@@ -35,7 +34,8 @@ type dataIn struct {
// Operation.
eth2Client eth2client.Service
chainTime chaintime.Service
epoch spec.Epoch
epoch int64
period string
}
func input(ctx context.Context) (*dataIn, error) {
@@ -67,12 +67,8 @@ func input(ctx context.Context) (*dataIn, error) {
}
// Epoch
epoch := viper.GetInt64("epoch")
if epoch == -1 {
data.epoch = data.chainTime.CurrentEpoch()
} else {
data.epoch = spec.Epoch(epoch)
}
data.epoch = viper.GetInt64("epoch")
data.period = viper.GetString("period")
return data, nil
}

View File

@@ -16,8 +16,10 @@ package members
import (
"context"
"fmt"
"strings"
eth2client "github.com/attestantio/go-eth2-client"
"github.com/attestantio/go-eth2-client/spec/phase0"
"github.com/pkg/errors"
)
@@ -26,11 +28,12 @@ func process(ctx context.Context, data *dataIn) (*dataOut, error) {
return nil, errors.New("no data")
}
if data.epoch < data.chainTime.AltairInitialEpoch() {
return nil, errors.New("not an Altair epoch")
epoch, err := calculateEpoch(ctx, data)
if err != nil {
return nil, err
}
syncCommittee, err := data.eth2Client.(eth2client.SyncCommitteesProvider).SyncCommittee(ctx, fmt.Sprintf("%d", data.chainTime.FirstSlotOfEpoch(data.epoch)))
syncCommittee, err := data.eth2Client.(eth2client.SyncCommitteesProvider).SyncCommittee(ctx, fmt.Sprintf("%d", data.chainTime.FirstSlotOfEpoch(epoch)))
if err != nil {
return nil, errors.Wrap(err, "failed to obtain sync committee information")
}
@@ -48,3 +51,27 @@ func process(ctx context.Context, data *dataIn) (*dataOut, error) {
return results, nil
}
func calculateEpoch(ctx context.Context, data *dataIn) (phase0.Epoch, error) {
var epoch phase0.Epoch
if data.epoch != -1 {
epoch = phase0.Epoch(data.epoch)
} else {
switch strings.ToLower(data.period) {
case "", "current":
epoch = data.chainTime.CurrentEpoch()
case "next":
period := data.chainTime.SlotToSyncCommitteePeriod(data.chainTime.CurrentSlot())
nextPeriod := period + 1
epoch = data.chainTime.FirstEpochOfSyncPeriod(nextPeriod)
default:
return 0, fmt.Errorf("period %s not known", data.period)
}
}
if data.debug {
fmt.Printf("epoch is %d\n", epoch)
}
return epoch, nil
}

View File

@@ -28,7 +28,9 @@ var synccommitteeMembersCmd = &cobra.Command{
ethdo synccommittee members --epoch=12345
In quiet mode this will return 0 if the synccommittee members are found, otherwise 1.`,
In quiet mode this will return 0 if the synccommittee members are found, otherwise 1.
epoch can be a specific epoch. period can be 'current' for the current sync period or 'next' for the next sync period`,
RunE: func(cmd *cobra.Command, args []string) error {
res, err := synccommitteemembers.Run(cmd)
if err != nil {
@@ -48,10 +50,14 @@ func init() {
synccommitteeCmd.AddCommand(synccommitteeMembersCmd)
synccommitteeFlags(synccommitteeMembersCmd)
synccommitteeMembersCmd.Flags().Int64("epoch", -1, "the epoch for which to fetch sync committees")
synccommitteeMembersCmd.Flags().String("period", "", "the sync committee period for which to fetch sync committees ('current', 'next')")
}
func synccommitteeMembersBindings() {
if err := viper.BindPFlag("epoch", synccommitteeMembersCmd.Flags().Lookup("epoch")); err != nil {
panic(err)
}
if err := viper.BindPFlag("period", synccommitteeMembersCmd.Flags().Lookup("period")); err != nil {
panic(err)
}
}