Add epoch parameter to validator yield.

This commit is contained in:
Jim McDonald
2023-06-13 00:22:32 +01:00
parent 46ca70a615
commit 713bbdd60c
5 changed files with 20 additions and 8 deletions

View File

@@ -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{},
}

View File

@@ -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)))
}

View File

@@ -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)
}
}