Skip to content

Commit

Permalink
Moved some types out of the Beacon client package for general usage
Browse files Browse the repository at this point in the history
  • Loading branch information
jclapis committed Jul 16, 2024
1 parent ccedd8f commit 814ef91
Show file tree
Hide file tree
Showing 5 changed files with 158 additions and 140 deletions.
7 changes: 4 additions & 3 deletions beacon/client/committees.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ import (
"sync"

"github.com/goccy/go-json"
"github.com/rocket-pool/node-manager-core/utils"
)

type Committee struct {
Index Uinteger `json:"index"`
Slot Uinteger `json:"slot"`
Validators []string `json:"validators"`
Index utils.Uinteger `json:"index"`
Slot utils.Uinteger `json:"slot"`
Validators []string `json:"validators"`
}

// Custom deserialization logic for Committee allows us to pool the validator
Expand Down
2 changes: 1 addition & 1 deletion beacon/client/std-client.go
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ func (c *StandardClient) GetDomainData(ctx context.Context, domainType []byte, e
func (c *StandardClient) ExitValidator(ctx context.Context, validatorIndex string, epoch uint64, signature beacon.ValidatorSignature) error {
return c.provider.Beacon_VoluntaryExits_Post(ctx, VoluntaryExitRequest{
Message: VoluntaryExitMessage{
Epoch: Uinteger(epoch),
Epoch: utils.Uinteger(epoch),
ValidatorIndex: validatorIndex,
},
Signature: signature[:],
Expand Down
153 changes: 49 additions & 104 deletions beacon/client/types.go
Original file line number Diff line number Diff line change
@@ -1,79 +1,76 @@
package client

import (
"strconv"

"github.com/ethereum/go-ethereum/common"
"github.com/goccy/go-json"
"github.com/rocket-pool/node-manager-core/utils"
)

// Request types
type VoluntaryExitMessage struct {
Epoch Uinteger `json:"epoch"`
ValidatorIndex string `json:"validator_index"`
Epoch utils.Uinteger `json:"epoch"`
ValidatorIndex string `json:"validator_index"`
}
type VoluntaryExitRequest struct {
Message VoluntaryExitMessage `json:"message"`
Signature ByteArray `json:"signature"`
Signature utils.ByteArray `json:"signature"`
}
type BLSToExecutionChangeMessage struct {
ValidatorIndex string `json:"validator_index"`
FromBLSPubkey ByteArray `json:"from_bls_pubkey"`
ToExecutionAddress ByteArray `json:"to_execution_address"`
ValidatorIndex string `json:"validator_index"`
FromBLSPubkey utils.ByteArray `json:"from_bls_pubkey"`
ToExecutionAddress utils.ByteArray `json:"to_execution_address"`
}
type BLSToExecutionChangeRequest struct {
Message BLSToExecutionChangeMessage `json:"message"`
Signature ByteArray `json:"signature"`
Signature utils.ByteArray `json:"signature"`
}

// Response types
type SyncStatusResponse struct {
Data struct {
IsSyncing bool `json:"is_syncing"`
HeadSlot Uinteger `json:"head_slot"`
SyncDistance Uinteger `json:"sync_distance"`
IsSyncing bool `json:"is_syncing"`
HeadSlot utils.Uinteger `json:"head_slot"`
SyncDistance utils.Uinteger `json:"sync_distance"`
} `json:"data"`
}
type Eth2ConfigResponse struct {
Data struct {
SecondsPerSlot Uinteger `json:"SECONDS_PER_SLOT"`
SlotsPerEpoch Uinteger `json:"SLOTS_PER_EPOCH"`
EpochsPerSyncCommitteePeriod Uinteger `json:"EPOCHS_PER_SYNC_COMMITTEE_PERIOD"`
CapellaForkVersion ByteArray `json:"CAPELLA_FORK_VERSION"`
SecondsPerSlot utils.Uinteger `json:"SECONDS_PER_SLOT"`
SlotsPerEpoch utils.Uinteger `json:"SLOTS_PER_EPOCH"`
EpochsPerSyncCommitteePeriod utils.Uinteger `json:"EPOCHS_PER_SYNC_COMMITTEE_PERIOD"`
CapellaForkVersion utils.ByteArray `json:"CAPELLA_FORK_VERSION"`
} `json:"data"`
}
type Eth2DepositContractResponse struct {
Data struct {
ChainID Uinteger `json:"chain_id"`
ChainID utils.Uinteger `json:"chain_id"`
Address common.Address `json:"address"`
} `json:"data"`
}
type GenesisResponse struct {
Data struct {
GenesisTime Uinteger `json:"genesis_time"`
GenesisForkVersion ByteArray `json:"genesis_fork_version"`
GenesisValidatorsRoot ByteArray `json:"genesis_validators_root"`
GenesisTime utils.Uinteger `json:"genesis_time"`
GenesisForkVersion utils.ByteArray `json:"genesis_fork_version"`
GenesisValidatorsRoot utils.ByteArray `json:"genesis_validators_root"`
} `json:"data"`
}
type FinalityCheckpointsResponse struct {
Data struct {
PreviousJustified struct {
Epoch Uinteger `json:"epoch"`
Epoch utils.Uinteger `json:"epoch"`
} `json:"previous_justified"`
CurrentJustified struct {
Epoch Uinteger `json:"epoch"`
Epoch utils.Uinteger `json:"epoch"`
} `json:"current_justified"`
Finalized struct {
Epoch Uinteger `json:"epoch"`
Epoch utils.Uinteger `json:"epoch"`
} `json:"finalized"`
} `json:"data"`
}
type ForkResponse struct {
Data struct {
PreviousVersion ByteArray `json:"previous_version"`
CurrentVersion ByteArray `json:"current_version"`
Epoch Uinteger `json:"epoch"`
PreviousVersion utils.ByteArray `json:"previous_version"`
CurrentVersion utils.ByteArray `json:"current_version"`
Epoch utils.Uinteger `json:"epoch"`
} `json:"data"`
}
type AttestationsResponse struct {
Expand All @@ -82,18 +79,18 @@ type AttestationsResponse struct {
type BeaconBlockResponse struct {
Data struct {
Message struct {
Slot Uinteger `json:"slot"`
ProposerIndex string `json:"proposer_index"`
Slot utils.Uinteger `json:"slot"`
ProposerIndex string `json:"proposer_index"`
Body struct {
Eth1Data struct {
DepositRoot ByteArray `json:"deposit_root"`
DepositCount Uinteger `json:"deposit_count"`
BlockHash ByteArray `json:"block_hash"`
DepositRoot utils.ByteArray `json:"deposit_root"`
DepositCount utils.Uinteger `json:"deposit_count"`
BlockHash utils.ByteArray `json:"block_hash"`
} `json:"eth1_data"`
Attestations []Attestation `json:"attestations"`
ExecutionPayload *struct {
FeeRecipient ByteArray `json:"fee_recipient"`
BlockNumber Uinteger `json:"block_number"`
FeeRecipient utils.ByteArray `json:"fee_recipient"`
BlockNumber utils.Uinteger `json:"block_number"`
} `json:"execution_payload"`
} `json:"body"`
} `json:"message"`
Expand All @@ -106,8 +103,8 @@ type BeaconBlockHeaderResponse struct {
Canonical bool `json:"canonical"`
Header struct {
Message struct {
Slot Uinteger `json:"slot"`
ProposerIndex string `json:"proposer_index"`
Slot utils.Uinteger `json:"slot"`
ProposerIndex string `json:"proposer_index"`
} `json:"message"`
} `json:"header"`
} `json:"data"`
Expand All @@ -116,27 +113,27 @@ type ValidatorsResponse struct {
Data []Validator `json:"data"`
}
type Validator struct {
Index string `json:"index"`
Balance Uinteger `json:"balance"`
Status string `json:"status"`
Index string `json:"index"`
Balance utils.Uinteger `json:"balance"`
Status string `json:"status"`
Validator struct {
Pubkey ByteArray `json:"pubkey"`
WithdrawalCredentials ByteArray `json:"withdrawal_credentials"`
EffectiveBalance Uinteger `json:"effective_balance"`
Slashed bool `json:"slashed"`
ActivationEligibilityEpoch Uinteger `json:"activation_eligibility_epoch"`
ActivationEpoch Uinteger `json:"activation_epoch"`
ExitEpoch Uinteger `json:"exit_epoch"`
WithdrawableEpoch Uinteger `json:"withdrawable_epoch"`
Pubkey utils.ByteArray `json:"pubkey"`
WithdrawalCredentials utils.ByteArray `json:"withdrawal_credentials"`
EffectiveBalance utils.Uinteger `json:"effective_balance"`
Slashed bool `json:"slashed"`
ActivationEligibilityEpoch utils.Uinteger `json:"activation_eligibility_epoch"`
ActivationEpoch utils.Uinteger `json:"activation_epoch"`
ExitEpoch utils.Uinteger `json:"exit_epoch"`
WithdrawableEpoch utils.Uinteger `json:"withdrawable_epoch"`
} `json:"validator"`
}
type SyncDutiesResponse struct {
Data []SyncDuty `json:"data"`
}
type SyncDuty struct {
Pubkey ByteArray `json:"pubkey"`
ValidatorIndex string `json:"validator_index"`
SyncCommitteeIndices []Uinteger `json:"validator_sync_committee_indices"`
Pubkey utils.ByteArray `json:"pubkey"`
ValidatorIndex string `json:"validator_index"`
SyncCommitteeIndices []utils.Uinteger `json:"validator_sync_committee_indices"`
}
type ProposerDutiesResponse struct {
Data []ProposerDuty `json:"data"`
Expand All @@ -152,59 +149,7 @@ type CommitteesResponse struct {
type Attestation struct {
AggregationBits string `json:"aggregation_bits"`
Data struct {
Slot Uinteger `json:"slot"`
Index Uinteger `json:"index"`
Slot utils.Uinteger `json:"slot"`
Index utils.Uinteger `json:"index"`
} `json:"data"`
}

// Unsigned integer type
type Uinteger uint64

func (i Uinteger) MarshalJSON() ([]byte, error) {
return json.Marshal(strconv.FormatUint(uint64(i), 10))
}
func (i *Uinteger) UnmarshalJSON(data []byte) error {

// Unmarshal string
var dataStr string
if err := json.Unmarshal(data, &dataStr); err != nil {
return err
}

// Parse integer value
value, err := strconv.ParseUint(dataStr, 10, 64)
if err != nil {
return err
}

// Set value and return
*i = Uinteger(value)
return nil

}

// Byte array type
type ByteArray []byte

func (b ByteArray) MarshalJSON() ([]byte, error) {
return json.Marshal(utils.EncodeHexWithPrefix(b))
}
func (b *ByteArray) UnmarshalJSON(data []byte) error {

// Unmarshal string
var dataStr string
if err := json.Unmarshal(data, &dataStr); err != nil {
return err
}

// Decode hex
value, err := utils.DecodeHex(dataStr)
if err != nil {
return err
}

// Set value and return
*b = value
return nil

}
53 changes: 21 additions & 32 deletions config/network-settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,42 +7,31 @@ import (
"os"

"github.com/ethereum/go-ethereum/common"
"github.com/rocket-pool/node-manager-core/utils"
"gopkg.in/yaml.v3"
)

const ()

var (
DefaultMainnetSettings = &NetworkSettings{
Key: "mainnet",
Name: "Ethereum Mainnet",
Description: "The Ethereum Mainnet network",
NetworkResources: &NetworkResources{
EthNetworkName: "mainnet",
ChainID: 1,
GenesisForkVersion: common.FromHex("0x00000000"), // https://github.com/eth-clients/eth2-networks/tree/master/shared/mainnet#genesis-information
MulticallAddress: common.HexToAddress("0x5BA1e12693Dc8F9c48aAD8770482f4739bEeD696"),
BalanceBatcherAddress: common.HexToAddress("0xb1f8e55c7f64d203c1400b9d8555d050f94adf39"),
TxWatchUrl: "https://etherscan.io/tx",
FlashbotsProtectUrl: "https://rpc.flashbots.net/",
},
DefaultConfigSettings: map[string]any{},
// Reference for Mainnet network resources, not used directly but helpful for testing
MainnetResourcesReference *NetworkResources = &NetworkResources{
EthNetworkName: "mainnet",
ChainID: 1,
GenesisForkVersion: common.FromHex("0x00000000"), // https://github.com/eth-clients/eth2-networks/tree/master/shared/mainnet#genesis-information
MulticallAddress: common.HexToAddress("0x5BA1e12693Dc8F9c48aAD8770482f4739bEeD696"),
BalanceBatcherAddress: common.HexToAddress("0xb1f8e55c7f64d203c1400b9d8555d050f94adf39"),
TxWatchUrl: "https://etherscan.io/tx",
FlashbotsProtectUrl: "https://rpc.flashbots.net/",
}

DefaultHoleskySettings = &NetworkSettings{
Key: "holesky",
Name: "Holesky Testnet",
Description: "The Ethereum holesky public test network",
NetworkResources: &NetworkResources{
EthNetworkName: "holesky",
ChainID: 17000,
GenesisForkVersion: common.FromHex("0x01017000"), // https://github.com/eth-clients/holesky
MulticallAddress: common.HexToAddress("0x0540b786f03c9491f3a2ab4b0e3ae4ecd4f63ce7"),
BalanceBatcherAddress: common.HexToAddress("0xfAa2e7C84eD801dd9D27Ac1ed957274530796140"),
TxWatchUrl: "https://holesky.etherscan.io/tx",
FlashbotsProtectUrl: "https://rpc-holesky.flashbots.net",
},
DefaultConfigSettings: map[string]any{},
// Reference for Holesky network resources, not used directly but helpful for testing
HoleskyResourcesReference *NetworkResources = &NetworkResources{
EthNetworkName: "holesky",
ChainID: 17000,
GenesisForkVersion: common.FromHex("0x01017000"), // https://github.com/eth-clients/holesky
MulticallAddress: common.HexToAddress("0x0540b786f03c9491f3a2ab4b0e3ae4ecd4f63ce7"),
BalanceBatcherAddress: common.HexToAddress("0xfAa2e7C84eD801dd9D27Ac1ed957274530796140"),
TxWatchUrl: "https://holesky.etherscan.io/tx",
FlashbotsProtectUrl: "https://rpc-holesky.flashbots.net",
}
)

Expand All @@ -55,7 +44,7 @@ type NetworkResources struct {
ChainID uint `yaml:"chainID" json:"chainID"`

// The genesis fork version for the network according to the Beacon config for the network
GenesisForkVersion []byte `yaml:"genesisForkVersion" json:"genesisForkVersion"`
GenesisForkVersion utils.ByteArray `yaml:"genesisForkVersion" json:"genesisForkVersion"`

// The address of the multicall contract
MulticallAddress common.Address `yaml:"multicallAddress" json:"multicallAddress"`
Expand Down Expand Up @@ -86,7 +75,7 @@ type NetworkSettings struct {

// A collection of default configuration settings to use for the network, which will override
// the standard "general-purpose" default value for the setting
DefaultConfigSettings map[string]any `yaml:"defaultConfigSettings" json:"defaultConfigSettings"`
DefaultConfigSettings map[string]any `yaml:"defaultConfigSettings,omitempty" json:"defaultConfigSettings,omitempty"`
}

// Load network settings from a file
Expand Down
Loading

0 comments on commit 814ef91

Please sign in to comment.