From 713bbdd60cbb6dbc2b7c3803d2ab344a46b340bd Mon Sep 17 00:00:00 2001 From: Jim McDonald Date: Tue, 13 Jun 2023 00:22:32 +0100 Subject: [PATCH] Add epoch parameter to validator yield. --- CHANGELOG.md | 3 ++- cmd/validator/yield/command.go | 4 +++- cmd/validator/yield/process.go | 16 ++++++++++------ cmd/validatoryield.go | 4 ++++ docs/usage.md | 1 + 5 files changed, 20 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 574a314..efe0cc4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,8 @@ dev: - fix incorrect error when "deposit veirfy" is not given a withdrawal address - allow truncated mnemonics (first four characters of each word) - - add deneb informatoin to "block info" + - add deneb information to "block info" + - add epoch parameter to "validator yield" 1.31.0: - initial support for deneb diff --git a/cmd/validator/yield/command.go b/cmd/validator/yield/command.go index 3678b76..22418c1 100644 --- a/cmd/validator/yield/command.go +++ b/cmd/validator/yield/command.go @@ -1,4 +1,4 @@ -// Copyright © 2022 Weald Technology Trading. +// Copyright © 2022, 2023 Weald Technology Trading. // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at @@ -36,6 +36,7 @@ type command struct { // Input. validators string + epoch string // Data access. eth2Client eth2client.Service @@ -63,6 +64,7 @@ func newCommand(_ context.Context) (*command, error) { verbose: viper.GetBool("verbose"), debug: viper.GetBool("debug"), json: viper.GetBool("json"), + epoch: viper.GetString("epoch"), results: &output{}, } diff --git a/cmd/validator/yield/process.go b/cmd/validator/yield/process.go index ac57153..c992bbe 100644 --- a/cmd/validator/yield/process.go +++ b/cmd/validator/yield/process.go @@ -1,4 +1,4 @@ -// Copyright © 2022 Weald Technology Trading. +// Copyright © 2022, 2023 Weald Technology Trading. // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at @@ -138,17 +138,21 @@ func (c *command) setup(ctx context.Context) error { return errors.New("connection does not provide validator information") } - validators, err := validatorsProvider.Validators(ctx, "head", nil) + epoch, err := util.ParseEpoch(ctx, chainTime, c.epoch) if err != nil { - return errors.Wrap(err, "failed to obtain validators") + return errors.Wrap(err, "failed to parse epoch") + } + + validators, err := validatorsProvider.Validators(ctx, fmt.Sprintf("%d", chainTime.FirstSlotOfEpoch(epoch)), nil) + if err != nil { + return err } - currentEpoch := chainTime.CurrentEpoch() activeValidators := decimal.Zero activeValidatorBalance := decimal.Zero for _, validator := range validators { - if validator.Validator.ActivationEpoch <= currentEpoch && - validator.Validator.ExitEpoch > currentEpoch { + if validator.Validator.ActivationEpoch <= epoch && + validator.Validator.ExitEpoch > epoch { activeValidators = activeValidators.Add(one) activeValidatorBalance = activeValidatorBalance.Add(decimal.NewFromInt(int64(validator.Validator.EffectiveBalance))) } diff --git a/cmd/validatoryield.go b/cmd/validatoryield.go index 8f4fc0d..d8e716c 100644 --- a/cmd/validatoryield.go +++ b/cmd/validatoryield.go @@ -48,10 +48,14 @@ func init() { validatorCmd.AddCommand(validatorYieldCmd) validatorFlags(validatorYieldCmd) validatorYieldCmd.Flags().String("validators", "", "Number of active validators (default fetches from chain)") + validatorYieldCmd.Flags().String("epoch", "", "Epoch at which to calculate yield") } func validatorYieldBindings(cmd *cobra.Command) { if err := viper.BindPFlag("validators", cmd.Flags().Lookup("validators")); err != nil { panic(err) } + if err := viper.BindPFlag("epoch", cmd.Flags().Lookup("epoch")); err != nil { + panic(err) + } } diff --git a/docs/usage.md b/docs/usage.md index 373a539..5b4259a 100644 --- a/docs/usage.md +++ b/docs/usage.md @@ -757,6 +757,7 @@ Withdrawal expected at 2023-04-17T15:08:35 in block 6243041 `ethdo validator yield` calculates the expected yield given the number of validators. Options include: - `validators` use a specified number of validators rather than the current number of active validators +- `epoch` the epoch for which to calculate yield; defaults to the current epoch - `json` obtain detailed information in JSON format ```sh