Skip to content

Commit

Permalink
Merge pull request #179 from stader-labs/fix-exit-fork-version
Browse files Browse the repository at this point in the history
make capella fork version for exits n/w dependant
  • Loading branch information
bharath-123 authored Jan 24, 2024
2 parents 6d5d886 + d58ff31 commit a109732
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 10 deletions.
4 changes: 2 additions & 2 deletions shared/services/bc-manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,9 +256,9 @@ func (m *BeaconClientManager) GetValidatorProposerDuties(indices []uint64, epoch
}

// Get the Beacon chain's domain data
func (m *BeaconClientManager) GetExitDomainData(domainType []byte) ([]byte, error) {
func (m *BeaconClientManager) GetExitDomainData(domainType []byte, network cfgtypes.Network) ([]byte, error) {
result, err := m.runFunction1(func(client beacon.Client) (interface{}, error) {
return client.GetExitDomainData(domainType)
return client.GetExitDomainData(domainType, network)
})
if err != nil {
return nil, err
Expand Down
3 changes: 2 additions & 1 deletion shared/services/beacon/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ package beacon
import (
"github.com/ethereum/go-ethereum/common"
"github.com/prysmaticlabs/go-bitfield"
"github.com/stader-labs/stader-node/shared/types/config"
"github.com/stader-labs/stader-node/stader-lib/types"
)

Expand Down Expand Up @@ -143,7 +144,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)
GetExitDomainData(domainType []byte, network config.Network) ([]byte, error)
ExitValidator(validatorIndex, epoch uint64, signature types.ValidatorSignature) error
Close() error
GetEth1DataForEth2Block(blockId string) (Eth1Data, bool, error)
Expand Down
17 changes: 12 additions & 5 deletions shared/services/beacon/client/std-http-client.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (

"github.com/ethereum/go-ethereum/common"
"github.com/prysmaticlabs/prysm/v3/crypto/bls"
"github.com/stader-labs/stader-node/shared/types/config"
"github.com/stader-labs/stader-node/stader-lib/types"
eth2types "github.com/wealdtech/go-eth2-types/v2"
"golang.org/x/sync/errgroup"
Expand Down Expand Up @@ -62,8 +63,6 @@ const (

MaxRequestValidatorsCount = 600
threadLimit int = 6

CapellaForkVersion = "0x03001020"
)

// Beacon client using the standard Beacon HTTP REST API (https://ethereum.github.io/beacon-APIs/)
Expand Down Expand Up @@ -411,22 +410,30 @@ 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) {
func (c *StandardHttpClient) GetExitDomainData(domainType []byte, network config.Network) ([]byte, error) {

var genesis GenesisResponse

genesis, err := c.getGenesis()

// Get fork version
forkVersion, err := hexutil.Decode(CapellaForkVersion)
var capellaForkVersion string
// TODO - we currently only support mainnet and testnet envs. We will have to update this as we change n/ws
if network == config.Network_Mainnet {
capellaForkVersion = eth2.MainnetCapellaForkVersion
} else {
capellaForkVersion = eth2.GoerliCapellaForkVersion
}

decodedForkVersion, 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
return eth2types.Domain(dt, decodedForkVersion, genesis.Data.GenesisValidatorsRoot), nil

}

Expand Down
5 changes: 5 additions & 0 deletions shared/utils/eth2/eth2.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ import (
"github.com/stader-labs/stader-node/shared/services/beacon"
)

const (
GoerliCapellaForkVersion = "0x03001020"
MainnetCapellaForkVersion = "0x03000000"
)

// Get an eth2 epoch number by time
func EpochAt(config beacon.Eth2Config, time uint64) uint64 {
return config.GenesisEpoch + (time-config.GenesisTime)/config.SecondsPerEpoch
Expand Down
10 changes: 9 additions & 1 deletion stader/api/validator/exit.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package validator

import (
"fmt"

"github.com/stader-labs/stader-node/shared/services"
"github.com/stader-labs/stader-node/shared/types/api"
"github.com/stader-labs/stader-node/shared/types/config"
"github.com/stader-labs/stader-node/shared/utils/eth2"
"github.com/stader-labs/stader-node/shared/utils/validator"
"github.com/stader-labs/stader-node/stader-lib/types"
Expand Down Expand Up @@ -83,6 +86,11 @@ func exitValidator(c *cli.Context, validatorPubKey types.ValidatorPubkey) (*api.
return nil, err
}

network, ok := cfg.StaderNode.Network.Value.(config.Network)
if !ok {
return nil, fmt.Errorf("invalid network configuration: %s", cfg.StaderNode.Network.Value)
}

// Response
response := api.ExitValidatorResponse{}

Expand All @@ -93,7 +101,7 @@ func exitValidator(c *cli.Context, validatorPubKey types.ValidatorPubkey) (*api.
}

// Get voluntary exit signature domain
signatureDomain, err := bc.GetExitDomainData(eth2types.DomainVoluntaryExit[:])
signatureDomain, err := bc.GetExitDomainData(eth2types.DomainVoluntaryExit[:], network)
if err != nil {
return nil, err
}
Expand Down
15 changes: 14 additions & 1 deletion stader/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"sync"
"time"

cfgtypes "github.com/stader-labs/stader-node/shared/types/config"
stader_backend "github.com/stader-labs/stader-node/shared/types/stader-backend"
"github.com/stader-labs/stader-node/shared/utils/crypto"
"github.com/stader-labs/stader-node/shared/utils/eth2"
Expand Down Expand Up @@ -153,6 +154,18 @@ func run(c *cli.Context) error {
}
}

cfg, err := services.GetConfig(c)
if err != nil {
errorLog.Printf("Failed to get config with error %s\n", err.Error())
continue
}

network, ok := cfg.StaderNode.Network.Value.(cfgtypes.Network)
if !ok {
errorLog.Printf("Failed to get network from config: %s\n", cfg.StaderNode.Network.Value)
continue
}

operatorId, err := node.GetOperatorId(pnr, nodeAccount.Address, nil)
if err != nil {
errorLog.Printf("Failed to get operator id: %s\n", err.Error())
Expand Down Expand Up @@ -257,7 +270,7 @@ func run(c *cli.Context) error {

exitEpoch := currentHead.Epoch

signatureDomain, err := bc.GetExitDomainData(eth2types.DomainVoluntaryExit[:])
signatureDomain, err := bc.GetExitDomainData(eth2types.DomainVoluntaryExit[:], network)
if err != nil {
errorLog.Printf("Failed to get the signature domain from beacon chain with err: %s\n", err.Error())
continue
Expand Down

0 comments on commit a109732

Please sign in to comment.