From e00c4b76146491651485cc9025e9db1d8b7b6ef5 Mon Sep 17 00:00:00 2001 From: Bharath Vedartham Date: Thu, 18 Jan 2024 17:20:31 +0530 Subject: [PATCH 1/2] use capella fork version for exit signature --- shared/services/bc-manager.go | 11 ++++++ shared/services/beacon/client.go | 1 + .../services/beacon/client/std-http-client.go | 34 +++++++++++++++++++ shared/utils/hex/hex.go | 8 +++++ stader/api/validator/exit.go | 2 +- stader/node/node.go | 2 +- 6 files changed, 56 insertions(+), 2 deletions(-) diff --git a/shared/services/bc-manager.go b/shared/services/bc-manager.go index 2fa11124f..ac52e7b7c 100644 --- a/shared/services/bc-manager.go +++ b/shared/services/bc-manager.go @@ -266,6 +266,17 @@ func (m *BeaconClientManager) GetDomainData(domainType []byte, epoch uint64, use return result.([]byte), nil } +// Get the Beacon chain's domain data +func (m *BeaconClientManager) GetExitDomainData(domainType []byte) ([]byte, error) { + result, err := m.runFunction1(func(client beacon.Client) (interface{}, error) { + return client.GetExitDomainData(domainType) + }) + if err != nil { + return nil, err + } + return result.([]byte), nil +} + // Voluntarily exit a validator func (m *BeaconClientManager) ExitValidator(validatorIndex, epoch uint64, signature types.ValidatorSignature) error { err := m.runFunction0(func(client beacon.Client) error { diff --git a/shared/services/beacon/client.go b/shared/services/beacon/client.go index 273d82cbf..d0bc7fcba 100644 --- a/shared/services/beacon/client.go +++ b/shared/services/beacon/client.go @@ -143,6 +143,7 @@ type Client interface { GetValidatorIndex(pubkey types.ValidatorPubkey) (uint64, error) GetValidatorSyncDuties(indices []uint64, epoch uint64) (map[uint64]bool, error) GetValidatorProposerDuties(indices []uint64, epoch uint64) (map[uint64]uint64, error) + GetExitDomainData(domainType []byte) ([]byte, error) GetDomainData(domainType []byte, epoch uint64, useGenesisFork bool) ([]byte, error) ExitValidator(validatorIndex, epoch uint64, signature types.ValidatorSignature) error Close() error diff --git a/shared/services/beacon/client/std-http-client.go b/shared/services/beacon/client/std-http-client.go index 6bcb5628f..b4d8093d3 100644 --- a/shared/services/beacon/client/std-http-client.go +++ b/shared/services/beacon/client/std-http-client.go @@ -62,6 +62,8 @@ const ( MaxRequestValidatorsCount = 600 threadLimit int = 6 + + CapellaForkVersion = "0x03001020" ) // Beacon client using the standard Beacon HTTP REST API (https://ethereum.github.io/beacon-APIs/) @@ -408,6 +410,38 @@ func (c *StandardHttpClient) GetValidatorIndex(pubkey types.ValidatorPubkey) (ui } +// Get domain data for a domain type at a given epoch +func (c *StandardHttpClient) GetExitDomainData(domainType []byte) ([]byte, error) { + + // Data + var wg errgroup.Group + var genesis GenesisResponse + + // Get genesis + wg.Go(func() error { + var err error + genesis, err = c.getGenesis() + return err + }) + + // Wait for data + if err := wg.Wait(); err != nil { + return []byte{}, err + } + + // Get fork version + forkVersion, err := hexutil.Decode(CapellaForkVersion) + if err != nil { + return []byte{}, err + } + + // Compute & return domain + var dt [4]byte + copy(dt[:], domainType[:]) + return eth2types.Domain(dt, forkVersion, genesis.Data.GenesisValidatorsRoot), nil + +} + // Get domain data for a domain type at a given epoch func (c *StandardHttpClient) GetDomainData(domainType []byte, epoch uint64, useGenesisFork bool) ([]byte, error) { diff --git a/shared/utils/hex/hex.go b/shared/utils/hex/hex.go index 202892ae3..3f56c2733 100644 --- a/shared/utils/hex/hex.go +++ b/shared/utils/hex/hex.go @@ -19,6 +19,10 @@ along with this program. If not, see . */ package hex +import ( + "github.com/ethereum/go-ethereum/common/hexutil" +) + // Add a prefix to a hex string if not present func AddPrefix(value string) string { if len(value) < 2 || value[0:2] != "0x" { @@ -34,3 +38,7 @@ func RemovePrefix(value string) string { } return value } + +func Decode(value string) ([]byte, error) { + return hexutil.Decode(value) +} diff --git a/stader/api/validator/exit.go b/stader/api/validator/exit.go index a22a1a9d7..668e8f210 100644 --- a/stader/api/validator/exit.go +++ b/stader/api/validator/exit.go @@ -93,7 +93,7 @@ func exitValidator(c *cli.Context, validatorPubKey types.ValidatorPubkey) (*api. } // Get voluntary exit signature domain - signatureDomain, err := bc.GetDomainData(eth2types.DomainVoluntaryExit[:], head.Epoch, false) + signatureDomain, err := bc.GetExitDomainData(eth2types.DomainVoluntaryExit[:]) if err != nil { return nil, err } diff --git a/stader/node/node.go b/stader/node/node.go index 846de41b8..3cd4664a1 100644 --- a/stader/node/node.go +++ b/stader/node/node.go @@ -257,7 +257,7 @@ func run(c *cli.Context) error { exitEpoch := currentHead.Epoch - signatureDomain, err := bc.GetDomainData(eth2types.DomainVoluntaryExit[:], exitEpoch, false) + signatureDomain, err := bc.GetExitDomainData(eth2types.DomainVoluntaryExit[:]) if err != nil { errorLog.Printf("Failed to get the signature domain from beacon chain with err: %s\n", err.Error()) continue From 60698409a0aa85b3b35f3408eaef75f90a0d9387 Mon Sep 17 00:00:00 2001 From: Bharath Vedartham Date: Thu, 18 Jan 2024 18:15:55 +0530 Subject: [PATCH 2/2] refactor --- shared/services/bc-manager.go | 11 ---- shared/services/beacon/client.go | 1 - .../services/beacon/client/std-http-client.go | 58 +------------------ 3 files changed, 1 insertion(+), 69 deletions(-) diff --git a/shared/services/bc-manager.go b/shared/services/bc-manager.go index ac52e7b7c..0dd042bfa 100644 --- a/shared/services/bc-manager.go +++ b/shared/services/bc-manager.go @@ -255,17 +255,6 @@ func (m *BeaconClientManager) GetValidatorProposerDuties(indices []uint64, epoch return result.(map[uint64]uint64), nil } -// Get the Beacon chain's domain data -func (m *BeaconClientManager) GetDomainData(domainType []byte, epoch uint64, useGenesisFork bool) ([]byte, error) { - result, err := m.runFunction1(func(client beacon.Client) (interface{}, error) { - return client.GetDomainData(domainType, epoch, useGenesisFork) - }) - if err != nil { - return nil, err - } - return result.([]byte), nil -} - // Get the Beacon chain's domain data func (m *BeaconClientManager) GetExitDomainData(domainType []byte) ([]byte, error) { result, err := m.runFunction1(func(client beacon.Client) (interface{}, error) { diff --git a/shared/services/beacon/client.go b/shared/services/beacon/client.go index d0bc7fcba..cd261f283 100644 --- a/shared/services/beacon/client.go +++ b/shared/services/beacon/client.go @@ -144,7 +144,6 @@ type Client interface { GetValidatorSyncDuties(indices []uint64, epoch uint64) (map[uint64]bool, error) GetValidatorProposerDuties(indices []uint64, epoch uint64) (map[uint64]uint64, error) GetExitDomainData(domainType []byte) ([]byte, error) - GetDomainData(domainType []byte, epoch uint64, useGenesisFork bool) ([]byte, error) ExitValidator(validatorIndex, epoch uint64, signature types.ValidatorSignature) error Close() error GetEth1DataForEth2Block(blockId string) (Eth1Data, bool, error) diff --git a/shared/services/beacon/client/std-http-client.go b/shared/services/beacon/client/std-http-client.go index b4d8093d3..00c670f6e 100644 --- a/shared/services/beacon/client/std-http-client.go +++ b/shared/services/beacon/client/std-http-client.go @@ -413,21 +413,9 @@ func (c *StandardHttpClient) GetValidatorIndex(pubkey types.ValidatorPubkey) (ui // Get domain data for a domain type at a given epoch func (c *StandardHttpClient) GetExitDomainData(domainType []byte) ([]byte, error) { - // Data - var wg errgroup.Group var genesis GenesisResponse - // Get genesis - wg.Go(func() error { - var err error - genesis, err = c.getGenesis() - return err - }) - - // Wait for data - if err := wg.Wait(); err != nil { - return []byte{}, err - } + genesis, err := c.getGenesis() // Get fork version forkVersion, err := hexutil.Decode(CapellaForkVersion) @@ -442,50 +430,6 @@ func (c *StandardHttpClient) GetExitDomainData(domainType []byte) ([]byte, error } -// Get domain data for a domain type at a given epoch -func (c *StandardHttpClient) GetDomainData(domainType []byte, epoch uint64, useGenesisFork bool) ([]byte, error) { - - // Data - var wg errgroup.Group - var genesis GenesisResponse - var fork ForkResponse - - // Get genesis - wg.Go(func() error { - var err error - genesis, err = c.getGenesis() - return err - }) - - // Get fork - wg.Go(func() error { - var err error - fork, err = c.getFork("head") - return err - }) - - // Wait for data - if err := wg.Wait(); err != nil { - return []byte{}, err - } - - // Get fork version - var forkVersion []byte - if useGenesisFork { - forkVersion = genesis.Data.GenesisForkVersion - } else if epoch < uint64(fork.Data.Epoch) { - forkVersion = fork.Data.PreviousVersion - } else { - forkVersion = fork.Data.CurrentVersion - } - - // Compute & return domain - var dt [4]byte - copy(dt[:], domainType[:]) - return eth2types.Domain(dt, forkVersion, genesis.Data.GenesisValidatorsRoot), nil - -} - // Perform a voluntary exit on a validator func (c *StandardHttpClient) ExitValidator(validatorIndex, epoch uint64, signature types.ValidatorSignature) error { return c.postVoluntaryExit(VoluntaryExitRequest{