client

package module
v0.6.6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 29, 2020 License: Apache-2.0 Imports: 4 Imported by: 154

README

go-eth2-client

Tag License GoDoc Lint Go Report Card

Go library providing an abstraction to multiple Ethereum 2 beacon nodes. Its external API follows the official Ethereum 2 APIs specification.

This library is under development; expect APIs and data structures to change until it reaches version 1.0.

Table of Contents

Install

go-eth2-client is a standard Go module which can be installed with:

go get github.com/attestantio/go-eth2-client

Support

go-eth2-client supports multiple beacon nodes. At current it provides support for the following:

  • Prysm using its GRPC interface
  • Lighthouse using its HTTP interface
  • Teku using its HTTP interface

Usage

go-eth2-client provides independent implementations for each beacon node interface, however it is generally easier to use the auto interface, as that will automatically select the correct client given the supplied address.

Please read the Go documentation for this library for interface information.

Example

Below is a complete annotated example to access a beacon node.

package main

import (
    "context"
    "fmt"
    
    eth2client "github.com/attestantio/go-eth2-client"
    "github.com/attestantio/go-eth2-client/auto"
    "github.com/rs/zerolog"
)

func main() {
    // Provide a cancellable context to the creation function.
    ctx, cancel := context.WithCancel(context.Background())
    client, err := auto.New(ctx,
        // WithAddress supplies the address of the beacon node, in host:port format.
        auto.WithAddress("localhost:4000"),
        // LogLevel supplies the level of logging to carry out.
        auto.WithLogLevel(zerolog.WarnLevel),
    )
    if err != nil {
        panic(err)
    }
    
    fmt.Printf("Connected to %s\n", client.Name())
    
    // Client functions have their own interfaces.  Not all functions are
    // supported by all clients, so checks should be made for each function when
    // casting the service to the relevant interface.
    if provider, isProvider := client.(eth2client.SlotsPerEpochProvider); isProvider {
        slotsPerEpoch, err := provider.SlotsPerEpoch(ctx)
        if err != nil {
            panic(err)
        }
        fmt.Printf("Slots per epochs is %d\n", slotsPerEpoch)
    }

    // Cancelling the context passed to New() frees up resources held by the
    // client, closes connections, clears handlers, etc.
    cancel()
}

Maintainers

Jim McDonald: @mcdee.

Contribute

Contributions welcome. Please check out the issues.

License

Apache-2.0 © 2020 Attestant Limited

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AggregateAndProofDomainProvider

type AggregateAndProofDomainProvider interface {
	// AggregateAndProofDomain provides the aggregate and proof domain.
	AggregateAndProofDomain(ctx context.Context) ([]byte, error)
}

AggregateAndProofDomainProvider is the interface for providing the aggregate and proof domain.

type AggregateAttestationProvider

type AggregateAttestationProvider interface {
	// AggregateAttestation fetches the aggregate attestation given an attestation.
	NonSpecAggregateAttestation(ctx context.Context, attestation *spec.Attestation, validatorPubKey []byte, slotSignature []byte) (*spec.Attestation, error)
	AggregateAttestation(ctx context.Context, slot uint64, attestationDataRoot []byte) (*spec.Attestation, error)
}

AggregateAttestationProvider is the interface for providing aggregate attestations.

type AggregateAttestationsSubmitter

type AggregateAttestationsSubmitter interface {
	// SubmitAggregateAttestations submits aggregate attestations.
	SubmitAggregateAttestations(ctx context.Context, aggregateAndProofs []*spec.SignedAggregateAndProof) error
}

AggregateAttestationsSubmitter is the interface for submitting aggregate attestations.

type AttestationDataProvider

type AttestationDataProvider interface {
	// AttestationData fetches the attestation data for the given slot and committee index.
	AttestationData(ctx context.Context, slot uint64, committeeIndex uint64) (*spec.AttestationData, error)
}

AttestationDataProvider is the interface for providing attestation data.

type AttestationSubmitter

type AttestationSubmitter interface {
	// SubmitAttestation submits an attestation.
	SubmitAttestation(ctx context.Context, attestation *spec.Attestation) error
}

AttestationSubmitter is the interface for submitting attestations.

type AttesterDutiesProvider

type AttesterDutiesProvider interface {
	// AttesterDuties obtains attester duties.
	// If validators is nil it will return all duties for the given epoch.
	AttesterDuties(ctx context.Context, epoch uint64, validators []ValidatorIDProvider) ([]*api.AttesterDuty, error)
}

AttesterDutiesProvider is the interface for providing attester duties

type BeaconAttesterDomainProvider

type BeaconAttesterDomainProvider interface {
	// BeaconAttesterDomain provides the beacon attester domain.
	BeaconAttesterDomain(ctx context.Context) ([]byte, error)
}

BeaconAttesterDomainProvider is the interface for providing the beacon attester domain.

type BeaconBlockProposalProvider

type BeaconBlockProposalProvider interface {
	// BeaconBlockProposal fetches a proposed beacon block for signing.
	BeaconBlockProposal(ctx context.Context, slot uint64, randaoReveal []byte, graffiti []byte) (*spec.BeaconBlock, error)
}

BeaconBlockProposalProvider is the interface for providing beacon block proposals.

type BeaconBlockRootProvider

type BeaconBlockRootProvider interface {
	// BeaconBlockRootBySlot fetches a block's root given its slot.
	BeaconBlockRootBySlot(ctx context.Context, slot uint64) ([]byte, error)
}

BeaconBlockRootProvider is the interface for providing beacon block roots.

type BeaconBlockSubmitter

type BeaconBlockSubmitter interface {
	// SubmitBeaconBlock submits a beacon block.
	SubmitBeaconBlock(ctx context.Context, block *spec.SignedBeaconBlock) error
}

BeaconBlockSubmitter is the interface for submitting beacon blocks.

type BeaconChainHeadUpdatedHandler

type BeaconChainHeadUpdatedHandler interface {
	// OnBeaconChainHeadUpdated is called whenever we receive a notification of an update to the beacon chain head.
	OnBeaconChainHeadUpdated(ctx context.Context, slot uint64, blockRoot []byte, stateRoot []byte, epochTransition bool)
}

BeaconChainHeadUpdatedHandler is the interface that needs to be implemented by processes that wish to receive beacon chain head updated messages.

type BeaconChainHeadUpdatedSource

type BeaconChainHeadUpdatedSource interface {
	// AddOnBeaconChainHeadUpdatedHandler adds a handler provided with beacon chain head updates.
	AddOnBeaconChainHeadUpdatedHandler(ctx context.Context, handler BeaconChainHeadUpdatedHandler) error
}

BeaconChainHeadUpdatedSource is the interface for a service that provides beacon chain head updates.

type BeaconCommitteeSubscription

type BeaconCommitteeSubscription struct {
	// Slot is the slot for which the validator is attesting.
	Slot uint64
	// CommitteeIndex is the index of the committee of which the validator is a member at the given slot.
	CommitteeIndex uint64
	// CommitteeSize is the number of validators in the committee at the given slot.
	CommitteeSize uint64
	// ValidatorIndex is the index of the valdiator that wishes to subscribe.
	ValidatorIndex uint64
	// ValidatorPubKey is the public key of the valdiator that wishes to subscribe.
	ValidatorPubKey []byte
	// Aggregate is true if the validator that wishes to subscribe also needs to aggregate attestations.
	Aggregate bool
	// SlotSelectionSignature is the result of the validator signing the slot with the "selection proof" domain.
	SlotSelectionSignature []byte
}

BeaconCommitteeSubscription is the data required for a beacon committee subscription.

type BeaconCommitteeSubscriptionsSubmitter

type BeaconCommitteeSubscriptionsSubmitter interface {
	// SubmitBeaconCommitteeSubscriptions subscribes to beacon committees.
	SubmitBeaconCommitteeSubscriptions(ctx context.Context, subscriptions []*BeaconCommitteeSubscription) error
}

BeaconCommitteeSubscriptionsSubmitter is the interface for submitting beacon committee subnet subscription requests.

type BeaconCommitteesProvider

type BeaconCommitteesProvider interface {
	// BeaconCommittees fetches the chain's beacon committees given a state.
	BeaconCommittees(ctx context.Context, stateID string) ([]*api.BeaconCommittee, error)
}

BeaconCommitteesProvider is the interface for providing beacon committees.

type BeaconProposerDomainProvider

type BeaconProposerDomainProvider interface {
	// BeaconProposerDomain provides the beacon proposer domain.
	BeaconProposerDomain(ctx context.Context) ([]byte, error)
}

BeaconProposerDomainProvider is the interface for providing the beacon proposer domain.

type DepositContractProvider

type DepositContractProvider interface {
	// DepositContractAddress provides the Ethereum 1 address of the deposit contract.
	DepositContractAddress(ctx context.Context) ([]byte, error)

	// DepositContractChainID provides the Ethereum 1 chain ID of the deposit contract.
	DepositContractChainID(ctx context.Context) (uint64, error)

	// DepositContractNetworkID provides the Ethereum 1 network ID of the deposit contract.
	DepositContractNetworkID(ctx context.Context) (uint64, error)
}

DepositContractProvider is the interface for providng details about the deposit contract.

type DepositDomainProvider

type DepositDomainProvider interface {
	// DepositDomain provides the deposit domain.
	DepositDomain(ctx context.Context) ([]byte, error)
}

DepositDomainProvider is the interface for providing the deposit domain.

type ForkProvider

type ForkProvider interface {
	// Fork fetches fork information for the given state.
	Fork(ctx context.Context, stateID string) (*spec.Fork, error)
}

ForkProvider is the interface for providing fork information.

type ForkScheduleProvider added in v0.6.5

type ForkScheduleProvider interface {
	// ForkSchedule provides details of past and future changes in the chain's fork version.
	ForkSchedule(ctx context.Context) ([]*spec.Fork, error)
}

ForkScheduleProvider is the interface for providing fork schedule data.

type GenesisProvider

type GenesisProvider interface {
	// Genesis fetches genesis information for the chain.
	Genesis(ctx context.Context) (*api.Genesis, error)
}

GenesisProvider is the interface for providing genesis information.

type GenesisTimeProvider

type GenesisTimeProvider interface {
	// GenesisTime provides the genesis time of the chain.
	GenesisTime(ctx context.Context) (time.Time, error)
}

GenesisTimeProvider is the interface for providing the genesis time of a chain.

type GenesisValidatorsRootProvider

type GenesisValidatorsRootProvider interface {
	// GenesisValidatorsRoot provides the genesis validators root of the chain.
	GenesisValidatorsRoot(ctx context.Context) ([]byte, error)
}

GenesisValidatorsRootProvider is the interface for providing the genesis validators root of a chain.

type NodeVersionProvider

type NodeVersionProvider interface {
	// NodeVersion returns a free-text string with the node version.
	NodeVersion(ctx context.Context) (string, error)
}

NodeVersionProvider is the interface for providing the node version.

type NonSpecAggregateAttestationProvider

type NonSpecAggregateAttestationProvider interface {
	// NonSpecAggregateAttestation fetches the aggregate attestation given an attestation.
	NonSpecAggregateAttestation(ctx context.Context, attestation *spec.Attestation, validatorPubKey []byte, slotSignature []byte) (*spec.Attestation, error)
}

NonSpecAggregateAttestationProvider is the interface for providing aggregate attestations.

type ProposerDutiesProvider

type ProposerDutiesProvider interface {
	// ProposerDuties obtains proposer duties.
	// If validators is nil it will return all duties for the given epoch.
	ProposerDuties(ctx context.Context, epoch uint64, validators []ValidatorIDProvider) ([]*api.ProposerDuty, error)
}

ProposerDutiesProvider is the interface for providing proposer duties.

type RANDAODomainProvider

type RANDAODomainProvider interface {
	// RANDAODomain provides the RANDAO domain.
	RANDAODomain(ctx context.Context) ([]byte, error)
}

RANDAODomainProvider is the interface for providing the RANDAO domain.

type SelectionProofDomainProvider

type SelectionProofDomainProvider interface {
	// SelectionProofDomain provides the selection proof domain.
	SelectionProofDomain(ctx context.Context) ([]byte, error)
}

SelectionProofDomainProvider is the interface for providing the selection proof domain.

type Service

type Service interface {
	// Name returns the name of the client.
	Name() string

	// EpochFromStateID converts a state ID to its epoch.
	EpochFromStateID(ctx context.Context, stateID string) (uint64, error)

	// SlotFromStateID converts a state ID to its slot.
	SlotFromStateID(ctx context.Context, stateID string) (uint64, error)
}

Service is the service providing a connection to an Ethereum 2 client.

type SignatureDomainProvider

type SignatureDomainProvider interface {
	// SignatureDomain provides a signature domain for a given domain at a given epoch.
	SignatureDomain(ctx context.Context, domain []byte, epoch uint64) ([]byte, error)
}

SignatureDomainProvider provides a full signature domain for a given domain at an epoch.

type SignedBeaconBlockProvider

type SignedBeaconBlockProvider interface {
	// SignedBeaconBlockBySlot fetches a signed beacon block given its slot.
	SignedBeaconBlockBySlot(ctx context.Context, slot uint64) (*spec.SignedBeaconBlock, error)
}

SignedBeaconBlockProvider is the interface for providing beacon blocks.

type SlotDurationProvider

type SlotDurationProvider interface {
	// SlotDuration provides the duration of a slot of the chain.
	SlotDuration(ctx context.Context) (time.Duration, error)
}

SlotDurationProvider is the interface for providing the duration of each slot of a chain.

type SlotsPerEpochProvider

type SlotsPerEpochProvider interface {
	// SlotsPerEpoch provides the slots per epoch of the chain.
	SlotsPerEpoch(ctx context.Context) (uint64, error)
}

SlotsPerEpochProvider is the interface for providing the number of slots in each epoch of a chain.

type SpecProvider added in v0.6.5

type SpecProvider interface {
	// Spec provides the spec information of the chain.
	Spec(ctx context.Context) (map[string]interface{}, error)
}

SpecProvider is the interface for providing spec data.

type SyncStateProvider

type SyncStateProvider interface {
	// SyncState provides the state of the node's synchronization with the chain.
	SyncState(ctx context.Context) (*api.SyncState, error)
}

SyncStateProvider is the interface for providing synchronization state.

type TargetAggregatorsPerCommitteeProvider

type TargetAggregatorsPerCommitteeProvider interface {
	// TargetAggregatorsPerCommittee provides the target number of aggregators for each attestation committee.
	TargetAggregatorsPerCommittee(ctx context.Context) (uint64, error)
}

TargetAggregatorsPerCommitteeProvider is the interface for providing the target number of aggregators in each attestation committee.

type ValidatorBalancesProvider

type ValidatorBalancesProvider interface {
	// ValidatorBalances provides the validator balances for a given state.
	// stateID can be a slot number or state root, or one of the special values "genesis", "head", "justified" or "finalized".
	// validators is a list of validators to restrict the returned values.  If no validators are supplied no filter will be applied.
	ValidatorBalances(ctx context.Context, stateID string, validators []ValidatorIDProvider) (map[uint64]uint64, error)
}

ValidatorBalancesProvider is the interface for providing validator balances.

type ValidatorIDProvider

type ValidatorIDProvider interface {
	ValidatorIndexProvider
	ValidatorPubKeyProvider
}

ValidatorIDProvider is the interface that provides the identifiers (pubkey, index) of a validator.

type ValidatorIndexProvider

type ValidatorIndexProvider interface {
	// Index provides the index of the validator.
	Index(ctx context.Context) (uint64, error)
}

ValidatorIndexProvider is the interface for entities that can provide the index of a validator.

type ValidatorPubKeyProvider

type ValidatorPubKeyProvider interface {
	// PubKey provides the public key of the validator.
	PubKey(ctx context.Context) ([]byte, error)
}

ValidatorPubKeyProvider is the interface for entities that can provide the public key of a validator.

type ValidatorsProvider

type ValidatorsProvider interface {
	// Validators provides the validators, with their balance and status, for a given state.
	// stateID can be a slot number or state root, or one of the special values "genesis", "head", "justified" or "finalized".
	// validators is a list of validators to restrict the returned values.  If no validators are supplied no filter will be applied.
	Validators(ctx context.Context, stateID string, validators []ValidatorIDProvider) (map[uint64]*api.Validator, error)
}

ValidatorsProvider is the interface for providing validator information.

type VoluntaryExitDomainProvider

type VoluntaryExitDomainProvider interface {
	// VoluntaryExitDomain provides the voluntary exit domain.
	VoluntaryExitDomain(ctx context.Context) ([]byte, error)
}

VoluntaryExitDomainProvider is the interface for providing the voluntary exit domain.

Directories

Path Synopsis
api
v1
spec
phase0
Code generated by fastssz.
Code generated by fastssz.
standardhttp
v1

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL