Skip to content

Commit

Permalink
Update parameter names; add fallback connection.
Browse files Browse the repository at this point in the history
Update parameter names to meet newer standards.

Provide a fallback beacon node if none other supplied.
  • Loading branch information
mcdee committed Apr 9, 2023
1 parent df99d43 commit 2c01d18
Show file tree
Hide file tree
Showing 58 changed files with 490 additions and 520 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
dev:
1.29.0:
- allow use of keystores with validator credentials set
- tidy up various command options to provide more standard usage
- add mainnet fallback beacon node

1.28.4:
- allow validator exit to use a keystore as its validator parameter
Expand Down
51 changes: 25 additions & 26 deletions cmd/attester/duties/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ import (
"time"

eth2client "github.com/attestantio/go-eth2-client"
spec "github.com/attestantio/go-eth2-client/spec/phase0"
"github.com/attestantio/go-eth2-client/spec/phase0"
"github.com/pkg/errors"
"github.com/spf13/viper"
"github.com/wealdtech/ethdo/services/chaintime"
standardchaintime "github.com/wealdtech/ethdo/services/chaintime/standard"
"github.com/wealdtech/ethdo/util"
)

Expand All @@ -31,13 +33,11 @@ type dataIn struct {
verbose bool
debug bool
json bool
// Chain information.
slotsPerEpoch uint64
// Operation.
account string
pubKey string
validator string
eth2Client eth2client.Service
epoch spec.Epoch
chainTime chaintime.Service
epoch phase0.Epoch
}

func input(ctx context.Context) (*dataIn, error) {
Expand All @@ -52,38 +52,37 @@ func input(ctx context.Context) (*dataIn, error) {
data.debug = viper.GetBool("debug")
data.json = viper.GetBool("json")

// Account or pubkey.
if viper.GetString("account") == "" && viper.GetString("pubkey") == "" {
return nil, errors.New("account or pubkey is required")
// Validator.
data.validator = viper.GetString("validator")
if data.validator == "" {
return nil, errors.New("validator is required")
}
data.account = viper.GetString("account")
data.pubKey = viper.GetString("pubkey")

// Ethereum 2 client.
var err error
data.eth2Client, err = util.ConnectToBeaconNode(ctx, viper.GetString("connection"), viper.GetDuration("timeout"), viper.GetBool("allow-insecure-connections"))
data.eth2Client, err = util.ConnectToBeaconNode(ctx, &util.ConnectOpts{
Address: viper.GetString("connection"),
Timeout: viper.GetDuration("timeout"),
AllowInsecure: viper.GetBool("allow-insecure-connections"),
LogFallback: !data.quiet,
})
if err != nil {
return nil, err
}

// Required data.
config, err := data.eth2Client.(eth2client.SpecProvider).Spec(ctx)
data.chainTime, err = standardchaintime.New(ctx,
standardchaintime.WithSpecProvider(data.eth2Client.(eth2client.SpecProvider)),
standardchaintime.WithGenesisTimeProvider(data.eth2Client.(eth2client.GenesisTimeProvider)),
)
if err != nil {
return nil, errors.Wrap(err, "failed to obtain beacon chain configuration")
return nil, errors.Wrap(err, "failed to set up chaintime service")
}
data.slotsPerEpoch = config["SLOTS_PER_EPOCH"].(uint64)

// Epoch
epoch := viper.GetInt64("epoch")
if epoch == -1 {
slotDuration := config["SECONDS_PER_SLOT"].(time.Duration)
genesis, err := data.eth2Client.(eth2client.GenesisProvider).Genesis(ctx)
if err != nil {
return nil, errors.Wrap(err, "failed to obtain genesis data")
}
epoch = int64(time.Since(genesis.GenesisTime).Seconds()) / (int64(slotDuration.Seconds()) * int64(data.slotsPerEpoch))
// Epoch.
data.epoch, err = util.ParseEpoch(ctx, data.chainTime, viper.GetString("epoch"))
if err != nil {
return nil, err
}
data.epoch = spec.Epoch(epoch)

return data, nil
}
15 changes: 9 additions & 6 deletions cmd/attester/duties/input_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"context"
"os"
"testing"
"time"

"github.com/spf13/viper"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -61,19 +62,21 @@ func TestInput(t *testing.T) {
err: "timeout is required",
},
{
name: "AccountMissing",
name: "ValidatorMissing",
vars: map[string]interface{}{
"timeout": "5s",
},
err: "account or pubkey is required",
err: "validator is required",
},
{
name: "ConnectionMissing",
name: "Good",
vars: map[string]interface{}{
"timeout": "5s",
"pubkey": "0xa99a76ed7796f7be22d5b7e85deeb7c5677e88e511e0b337618f8c4eb61349b4bf2d153f649f7b53359fe8b94a38e44c",
"timeout": "5s",
"validator": "0xa99a76ed7796f7be22d5b7e85deeb7c5677e88e511e0b337618f8c4eb61349b4bf2d153f649f7b53359fe8b94a38e44c",
},
res: &dataIn{
timeout: 5 * time.Second,
},
err: "failed to connect to any beacon node",
},
}

Expand Down
42 changes: 2 additions & 40 deletions cmd/attester/duties/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,60 +15,22 @@ package attesterduties

import (
"context"
"encoding/hex"
"fmt"
"strings"

eth2client "github.com/attestantio/go-eth2-client"
api "github.com/attestantio/go-eth2-client/api/v1"
spec "github.com/attestantio/go-eth2-client/spec/phase0"
"github.com/pkg/errors"
"github.com/wealdtech/ethdo/util"
e2wtypes "github.com/wealdtech/go-eth2-wallet-types/v2"
)

func process(ctx context.Context, data *dataIn) (*dataOut, error) {
if data == nil {
return nil, errors.New("no data")
}

var account e2wtypes.Account
var err error
if data.account != "" {
ctx, cancel := context.WithTimeout(ctx, data.timeout)
defer cancel()
_, account, err = util.WalletAndAccountFromPath(ctx, data.account)
if err != nil {
return nil, errors.Wrap(err, "failed to obtain account")
}
} else {
pubKeyBytes, err := hex.DecodeString(strings.TrimPrefix(data.pubKey, "0x"))
if err != nil {
return nil, errors.Wrap(err, fmt.Sprintf("failed to decode public key %s", data.pubKey))
}
account, err = util.NewScratchAccount(nil, pubKeyBytes)
if err != nil {
return nil, errors.Wrap(err, fmt.Sprintf("invalid public key %s", data.pubKey))
}
}

// Fetch validator
pubKeys := make([]spec.BLSPubKey, 1)
pubKey, err := util.BestPublicKey(account)
if err != nil {
return nil, errors.Wrap(err, "failed to obtain public key for account")
}
copy(pubKeys[0][:], pubKey.Marshal())
validators, err := data.eth2Client.(eth2client.ValidatorsProvider).ValidatorsByPubKey(ctx, fmt.Sprintf("%d", uint64(data.epoch)*data.slotsPerEpoch), pubKeys)
validator, err := util.ParseValidator(ctx, data.eth2Client.(eth2client.ValidatorsProvider), data.validator, "head")
if err != nil {
return nil, errors.New("failed to obtain validator information")
}
if len(validators) == 0 {
return nil, errors.New("validator is not known")
}
var validator *api.Validator
for _, v := range validators {
validator = v
return nil, err
}

results := &dataOut{
Expand Down
15 changes: 11 additions & 4 deletions cmd/attester/duties/process_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ import (
"os"
"testing"

eth2client "github.com/attestantio/go-eth2-client"
"github.com/attestantio/go-eth2-client/auto"
"github.com/rs/zerolog"
"github.com/stretchr/testify/require"
standardchaintime "github.com/wealdtech/ethdo/services/chaintime/standard"
)

func TestProcess(t *testing.T) {
Expand All @@ -33,6 +35,12 @@ func TestProcess(t *testing.T) {
)
require.NoError(t, err)

chainTime, err := standardchaintime.New(context.Background(),
standardchaintime.WithSpecProvider(eth2Client.(eth2client.SpecProvider)),
standardchaintime.WithGenesisTimeProvider(eth2Client.(eth2client.GenesisTimeProvider)),
)
require.NoError(t, err)

tests := []struct {
name string
dataIn *dataIn
Expand All @@ -45,10 +53,9 @@ func TestProcess(t *testing.T) {
{
name: "Client",
dataIn: &dataIn{
eth2Client: eth2Client,
slotsPerEpoch: 32,
pubKey: "0x933ad9491b62059dd065b560d256d8957a8c402cc6e8d8ee7290ae11e8f7329267a8811c397529dac52ae1342ba58c95",
epoch: 100,
eth2Client: eth2Client,
chainTime: chainTime,
validator: "0x933ad9491b62059dd065b560d256d8957a8c402cc6e8d8ee7290ae11e8f7329267a8811c397529dac52ae1342ba58c95",
},
},
}
Expand Down
55 changes: 19 additions & 36 deletions cmd/attester/inclusion/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ package attesterinclusion

import (
"context"
"fmt"
"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"
standardchaintime "github.com/wealdtech/ethdo/services/chaintime/standard"
"github.com/wealdtech/ethdo/util"
)

Expand All @@ -32,15 +32,11 @@ type dataIn struct {
quiet bool
verbose bool
debug bool
// Chain information.
slotsPerEpoch uint64
// Operation.
eth2Client eth2client.Service
chainTime chaintime.Service
epoch spec.Epoch
account string
pubKey string
index string
validator string
}

func input(ctx context.Context) (*dataIn, error) {
Expand All @@ -54,48 +50,35 @@ func input(ctx context.Context) (*dataIn, error) {
data.verbose = viper.GetBool("verbose")
data.debug = viper.GetBool("debug")

// Account.
data.account = viper.GetString("account")

// PubKey.
data.pubKey = viper.GetString("pubkey")

// ID.
data.index = viper.GetString("index")

if viper.GetString("account") == "" && viper.GetString("index") == "" && viper.GetString("pubkey") == "" {
return nil, errors.New("account, index or pubkey is required")
data.validator = viper.GetString("validator")
if data.validator == "" {
return nil, errors.New("validator is required")
}

// Ethereum 2 client.
var err error
data.eth2Client, err = util.ConnectToBeaconNode(ctx, viper.GetString("connection"), viper.GetDuration("timeout"), viper.GetBool("allow-insecure-connections"))
data.eth2Client, err = util.ConnectToBeaconNode(ctx, &util.ConnectOpts{
Address: viper.GetString("connection"),
Timeout: viper.GetDuration("timeout"),
AllowInsecure: viper.GetBool("allow-insecure-connections"),
LogFallback: !data.quiet,
})
if err != nil {
return nil, err
}

config, err := data.eth2Client.(eth2client.SpecProvider).Spec(ctx)
data.chainTime, err = standardchaintime.New(ctx,
standardchaintime.WithSpecProvider(data.eth2Client.(eth2client.SpecProvider)),
standardchaintime.WithGenesisTimeProvider(data.eth2Client.(eth2client.GenesisTimeProvider)),
)
if err != nil {
return nil, errors.Wrap(err, "failed to obtain beacon chain configuration")
return nil, errors.Wrap(err, "failed to set up chaintime service")
}
data.slotsPerEpoch = config["SLOTS_PER_EPOCH"].(uint64)

// Epoch.
epoch := viper.GetInt64("epoch")
if epoch == -1 {
slotDuration := config["SECONDS_PER_SLOT"].(time.Duration)
genesis, err := data.eth2Client.(eth2client.GenesisProvider).Genesis(ctx)
if err != nil {
return nil, errors.Wrap(err, "failed to obtain genesis data")
}
epoch = int64(time.Since(genesis.GenesisTime).Seconds()) / (int64(slotDuration.Seconds()) * int64(data.slotsPerEpoch))
if epoch > 0 {
epoch--
}
}
data.epoch = spec.Epoch(epoch)
if data.debug {
fmt.Printf("Epoch is %d\n", data.epoch)
data.epoch, err = util.ParseEpoch(ctx, data.chainTime, viper.GetString("epoch"))
if err != nil {
return nil, err
}

return data, nil
Expand Down
15 changes: 9 additions & 6 deletions cmd/attester/inclusion/input_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"context"
"os"
"testing"
"time"

"github.com/spf13/viper"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -61,19 +62,21 @@ func TestInput(t *testing.T) {
err: "timeout is required",
},
{
name: "IndexMissing",
name: "ValidatorMissing",
vars: map[string]interface{}{
"timeout": "5s",
},
err: "account, index or pubkey is required",
err: "validator is required",
},
{
name: "ConnectionMissing",
name: "Good",
vars: map[string]interface{}{
"timeout": "5s",
"pubkey": "0xa99a76ed7796f7be22d5b7e85deeb7c5677e88e511e0b337618f8c4eb61349b4bf2d153f649f7b53359fe8b94a38e44c",
"timeout": "5s",
"validator": "0xa99a76ed7796f7be22d5b7e85deeb7c5677e88e511e0b337618f8c4eb61349b4bf2d153f649f7b53359fe8b94a38e44c",
},
res: &dataIn{
timeout: 5 * time.Second,
},
err: "failed to connect to any beacon node",
},
}

Expand Down
Loading

0 comments on commit 2c01d18

Please sign in to comment.