api

package
v0.2010.1 Latest Latest
Warning

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

Go to latest
Published: Oct 2, 2020 License: Apache-2.0 Imports: 27 Imported by: 28

Documentation

Overview

Package consensus provides the implementation agnostic consensus backend.

Index

Constants

View Source
const (

	// HeightLatest is the height that represents the most recent block height.
	HeightLatest int64 = 0
)

Variables

View Source
var (
	// ErrNoCommittedBlocks is the error returned when there are no committed
	// blocks and as such no state can be queried.
	ErrNoCommittedBlocks = errors.New(moduleName, 1, "consensus: no committed blocks")

	// ErrOversizedTx is the error returned when the given transaction is too big to be processed.
	ErrOversizedTx = errors.New(moduleName, 2, "consensus: oversized transaction")

	// ErrVersionNotFound is the error returned when the given version (height) cannot be found,
	// possibly because it was pruned.
	ErrVersionNotFound = errors.New(moduleName, 3, "consensus: version not found")

	// ErrUnsupported is the error returned when the given method is not supported by the consensus
	// backend.
	ErrUnsupported = errors.New(moduleName, 4, "consensus: method not supported")

	// ErrDuplicateTx is the error returned when the transaction already exists in the mempool.
	ErrDuplicateTx = errors.New(moduleName, 5, "consensus: duplicate transaction")
)

Functions

func RegisterLightService

func RegisterLightService(server *grpc.Server, service LightClientBackend)

RegisterLightService registers a new light client backend service with the given gRPC server.

func RegisterService

func RegisterService(server *grpc.Server, service ClientBackend)

RegisterService registers a new client backend service with the given gRPC server.

func SignAndSubmitTx

func SignAndSubmitTx(ctx context.Context, backend Backend, signer signature.Signer, tx *transaction.Transaction) error

SignAndSubmitTx is a helper function that signs and submits a transaction to the consensus backend.

If the nonce is set to zero, it will be automatically filled in based on the current consensus state.

If the fee is set to nil, it will be automatically filled in based on gas estimation and current gas price discovery.

Types

type Backend

type Backend interface {
	service.BackgroundService
	ServicesBackend

	// SupportedFeatures returns the features supported by this consensus backend.
	SupportedFeatures() FeatureMask

	// Synced returns a channel that is closed once synchronization is
	// complete.
	Synced() <-chan struct{}

	// ConsensusKey returns the consensus signing key.
	ConsensusKey() signature.PublicKey

	// GetAddresses returns the consensus backend addresses.
	GetAddresses() ([]node.ConsensusAddress, error)
}

Backend is an interface that a consensus backend must provide.

type BaseBackend

type BaseBackend struct {
}

BaseBackend is a consensus backend that supports no features and panics for all methods.

func (*BaseBackend) Beacon

func (b *BaseBackend) Beacon() beacon.Backend

Implements Backend.

func (*BaseBackend) Cleanup

func (b *BaseBackend) Cleanup()

Implements Backend.

func (*BaseBackend) ConsensusKey

func (b *BaseBackend) ConsensusKey() signature.PublicKey

Implements Backend.

func (*BaseBackend) EpochTime

func (b *BaseBackend) EpochTime() epochtime.Backend

Implements Backend.

func (*BaseBackend) EstimateGas

func (b *BaseBackend) EstimateGas(ctx context.Context, req *EstimateGasRequest) (transaction.Gas, error)

Implements Backend.

func (*BaseBackend) GetAddresses

func (b *BaseBackend) GetAddresses() ([]node.ConsensusAddress, error)

Implements Backend.

func (*BaseBackend) GetBlock

func (b *BaseBackend) GetBlock(ctx context.Context, height int64) (*Block, error)

Implements Backend.

func (*BaseBackend) GetEpoch

func (b *BaseBackend) GetEpoch(ctx context.Context, height int64) (epochtime.EpochTime, error)

Implements Backend.

func (*BaseBackend) GetGenesisDocument

func (b *BaseBackend) GetGenesisDocument(ctx context.Context) (*genesis.Document, error)

Implements Backend.

func (*BaseBackend) GetLightBlock added in v0.2010.0

func (b *BaseBackend) GetLightBlock(ctx context.Context, height int64) (*LightBlock, error)

Implements Backend.

func (*BaseBackend) GetParameters

func (b *BaseBackend) GetParameters(ctx context.Context, height int64) (*Parameters, error)

Implements Backend.

func (*BaseBackend) GetSignerNonce

func (b *BaseBackend) GetSignerNonce(ctx context.Context, req *GetSignerNonceRequest) (uint64, error)

Implements Backend.

func (*BaseBackend) GetStatus

func (b *BaseBackend) GetStatus(ctx context.Context) (*Status, error)

Implements Backend.

func (*BaseBackend) GetTransactions

func (b *BaseBackend) GetTransactions(ctx context.Context, height int64) ([][]byte, error)

Implements Backend.

func (*BaseBackend) GetTransactionsWithResults

func (b *BaseBackend) GetTransactionsWithResults(ctx context.Context, height int64) (*TransactionsWithResults, error)

Implements Backend.

func (*BaseBackend) GetUnconfirmedTransactions added in v0.2010.0

func (b *BaseBackend) GetUnconfirmedTransactions(ctx context.Context) ([][]byte, error)

Implements Backend.

func (*BaseBackend) KeyManager

func (b *BaseBackend) KeyManager() keymanager.Backend

Implements Backend.

func (*BaseBackend) Name

func (b *BaseBackend) Name() string

Implements Backend.

func (*BaseBackend) Quit

func (b *BaseBackend) Quit() <-chan struct{}

Implements Backend.

func (*BaseBackend) RegisterHaltHook

func (b *BaseBackend) RegisterHaltHook(func(ctx context.Context, blockHeight int64, epoch epochtime.EpochTime))

Implements Backend.

func (*BaseBackend) Registry

func (b *BaseBackend) Registry() registry.Backend

Implements Backend.

func (*BaseBackend) RootHash

func (b *BaseBackend) RootHash() roothash.Backend

Implements Backend.

func (*BaseBackend) Scheduler

func (b *BaseBackend) Scheduler() scheduler.Backend

Implements Backend.

func (*BaseBackend) Staking

func (b *BaseBackend) Staking() staking.Backend

Implements Backend.

func (*BaseBackend) Start

func (b *BaseBackend) Start() error

Implements Backend.

func (*BaseBackend) State

func (b *BaseBackend) State() syncer.ReadSyncer

Implements Backend.

func (*BaseBackend) StateToGenesis

func (b *BaseBackend) StateToGenesis(ctx context.Context, height int64) (*genesis.Document, error)

Implements Backend.

func (*BaseBackend) Stop

func (b *BaseBackend) Stop()

Implements Backend.

func (*BaseBackend) SubmissionManager

func (b *BaseBackend) SubmissionManager() SubmissionManager

Implements Backend.

func (*BaseBackend) SubmitEvidence

func (b *BaseBackend) SubmitEvidence(ctx context.Context, evidence *Evidence) error

Implements Backend.

func (*BaseBackend) SubmitTx

Implements Backend.

func (*BaseBackend) SubmitTxNoWait

func (b *BaseBackend) SubmitTxNoWait(ctx context.Context, tx *transaction.SignedTransaction) error

Implements Backend.

func (*BaseBackend) SupportedFeatures

func (b *BaseBackend) SupportedFeatures() FeatureMask

Implements Backend.

func (*BaseBackend) Synced

func (b *BaseBackend) Synced() <-chan struct{}

Implements Backend.

func (*BaseBackend) WaitEpoch

func (b *BaseBackend) WaitEpoch(ctx context.Context, epoch epochtime.EpochTime) error

Implements Backend.

func (*BaseBackend) WatchBlocks

func (b *BaseBackend) WatchBlocks(ctx context.Context) (<-chan *Block, pubsub.ClosableSubscription, error)

Implements Backend.

type Block

type Block struct {
	// Height contains the block height.
	Height int64 `json:"height"`
	// Hash contains the block header hash.
	Hash []byte `json:"hash"`
	// Time is the second-granular consensus time.
	Time time.Time `json:"time"`
	// StateRoot is the Merkle root of the consensus state tree.
	StateRoot mkvsNode.Root `json:"state_root"`
	// Meta contains the consensus backend specific block metadata.
	Meta cbor.RawMessage `json:"meta"`
}

Block is a consensus block.

While some common fields are provided, most of the structure is dependent on the actual backend implementation.

type ClientBackend

type ClientBackend interface {
	LightClientBackend
	TransactionAuthHandler

	// SubmitTx submits a signed consensus transaction and waits for the transaction to be included
	// in a block. Use SubmitTxNoWait if you only need to broadcast the transaction.
	SubmitTx(ctx context.Context, tx *transaction.SignedTransaction) error

	// StateToGenesis returns the genesis state at the specified block height.
	StateToGenesis(ctx context.Context, height int64) (*genesis.Document, error)

	// EstimateGas calculates the amount of gas required to execute the given transaction.
	EstimateGas(ctx context.Context, req *EstimateGasRequest) (transaction.Gas, error)

	// WaitEpoch waits for consensus to reach an epoch.
	//
	// Note that an epoch is considered reached even if any epoch greater than
	// the one specified is reached (e.g., that the current epoch is already
	// in the future).
	WaitEpoch(ctx context.Context, epoch epochtime.EpochTime) error

	// GetEpoch returns the current epoch.
	GetEpoch(ctx context.Context, height int64) (epochtime.EpochTime, error)

	// GetBlock returns a consensus block at a specific height.
	GetBlock(ctx context.Context, height int64) (*Block, error)

	// GetTransactions returns a list of all transactions contained within a
	// consensus block at a specific height.
	//
	// NOTE: Any of these transactions could be invalid.
	GetTransactions(ctx context.Context, height int64) ([][]byte, error)

	// GetTransactionsWithResults returns a list of transactions and their
	// execution results, contained within a consensus block at a specific
	// height.
	GetTransactionsWithResults(ctx context.Context, height int64) (*TransactionsWithResults, error)

	// GetUnconfirmedTransactions returns a list of transactions currently in the local node's
	// mempool. These have not yet been included in a block.
	GetUnconfirmedTransactions(ctx context.Context) ([][]byte, error)

	// WatchBlocks returns a channel that produces a stream of consensus
	// blocks as they are being finalized.
	WatchBlocks(ctx context.Context) (<-chan *Block, pubsub.ClosableSubscription, error)

	// GetGenesisDocument returns the original genesis document.
	GetGenesisDocument(ctx context.Context) (*genesis.Document, error)

	// GetStatus returns the current status overview.
	GetStatus(ctx context.Context) (*Status, error)
}

ClientBackend is a limited consensus interface used by clients that connect to the local full node. This is separate from light clients which use the LightClientBackend interface.

func NewConsensusClient

func NewConsensusClient(c *grpc.ClientConn) ClientBackend

NewConsensusClient creates a new gRPC consensus client service.

type EstimateGasRequest

type EstimateGasRequest struct {
	Signer      signature.PublicKey      `json:"signer"`
	Transaction *transaction.Transaction `json:"transaction"`
}

EstimateGasRequest is a EstimateGas request.

type Evidence

type Evidence struct {
	// Meta contains the consensus backend specific evidence.
	Meta []byte `json:"meta"`
}

Evidence is evidence of a node's Byzantine behavior.

type FeatureMask

type FeatureMask uint8

FeatureMask is the consensus backend feature bitmask.

const (
	// FeatureServices indicates support for communicating with consensus services.
	FeatureServices FeatureMask = 1 << 0

	// FeatureFullNode indicates that the consensus backend is independently fully verifying all
	// consensus-layer blocks.
	FeatureFullNode FeatureMask = 1 << 1
)

func (FeatureMask) Has

func (m FeatureMask) Has(f FeatureMask) bool

Has checks whether the feature bitmask includes specific features.

func (FeatureMask) String

func (m FeatureMask) String() string

String returns a string representation of the consensus backend feature bitmask.

type GetSignerNonceRequest

type GetSignerNonceRequest struct {
	AccountAddress staking.Address `json:"account_address"`
	Height         int64           `json:"height"`
}

GetSignerNonceRequest is a GetSignerNonce request.

type LightBlock added in v0.2010.0

type LightBlock struct {
	// Height contains the block height.
	Height int64 `json:"height"`
	// Meta contains the consensus backend specific light block.
	Meta []byte `json:"meta"`
}

LightBlock is a light consensus block suitable for syncing light clients.

type LightClientBackend

type LightClientBackend interface {
	// GetLightBlock returns a light version of the consensus layer block that can be used for light
	// client verification.
	GetLightBlock(ctx context.Context, height int64) (*LightBlock, error)

	// GetParameters returns the consensus parameters for a specific height.
	GetParameters(ctx context.Context, height int64) (*Parameters, error)

	// State returns a MKVS read syncer that can be used to read consensus state from a remote node
	// and verify it against the trusted local root.
	State() syncer.ReadSyncer

	// SubmitTxNoWait submits a signed consensus transaction, but does not wait for the transaction
	// to be included in a block. Use SubmitTx if you need to wait for execution.
	SubmitTxNoWait(ctx context.Context, tx *transaction.SignedTransaction) error

	// SubmitEvidence submits evidence of misbehavior.
	SubmitEvidence(ctx context.Context, evidence *Evidence) error
}

LightClientBackend is the limited consensus interface used by light clients.

func NewConsensusLightClient

func NewConsensusLightClient(c *grpc.ClientConn) LightClientBackend

NewConsensusLightClient creates a new gRPC consensus light client service.

type Parameters

type Parameters struct {
	// Height contains the block height these consensus parameters are for.
	Height int64 `json:"height"`
	// Meta contains the consensus backend specific consensus parameters.
	Meta []byte `json:"meta"`
}

Parameters are the consensus backend parameters.

type PriceDiscovery

type PriceDiscovery interface {
	// GasPrice returns the current consensus gas price.
	GasPrice(ctx context.Context) (*quantity.Quantity, error)
}

PriceDiscovery is the consensus fee price discovery interface.

func NewStaticPriceDiscovery

func NewStaticPriceDiscovery(price uint64) (PriceDiscovery, error)

NewStaticPriceDiscovery creates a price discovery mechanism which always returns the same static price specified at construction time.

type ServicesBackend

type ServicesBackend interface {
	ClientBackend

	// RegisterHaltHook registers a function to be called when the
	// consensus Halt epoch height is reached.
	RegisterHaltHook(func(ctx context.Context, blockHeight int64, epoch epochtime.EpochTime))

	// SubmissionManager returns the transaction submission manager.
	SubmissionManager() SubmissionManager

	// EpochTime returns the epochtime backend.
	EpochTime() epochtime.Backend

	// Beacon returns the beacon backend.
	Beacon() beacon.Backend

	// KeyManager returns the keymanager backend.
	KeyManager() keymanager.Backend

	// Registry returns the registry backend.
	Registry() registry.Backend

	// RootHash returns the roothash backend.
	RootHash() roothash.Backend

	// Staking returns the staking backend.
	Staking() staking.Backend

	// Scheduler returns the scheduler backend.
	Scheduler() scheduler.Backend
}

ServicesBackend is an interface for consensus backends which indicate support for communicating with consensus services.

In case the feature is absent, these methods may return nil or ErrUnsupported.

type Status

type Status struct {
	// ConsensusVersion is the version of the consensus protocol that the node is using.
	ConsensusVersion string `json:"consensus_version"`
	// Backend is the consensus backend identifier.
	Backend string `json:"backend"`
	// Features are the indicated consensus backend features.
	Features FeatureMask `json:"features"`

	// NodePeers is a list of node's peers.
	NodePeers []string `json:"node_peers"`

	// LatestHeight is the height of the latest block.
	LatestHeight int64 `json:"latest_height"`
	// LatestHash is the hash of the latest block.
	LatestHash []byte `json:"latest_hash"`
	// LatestTime is the timestamp of the latest block.
	LatestTime time.Time `json:"latest_time"`
	// LatestStateRoot is the Merkle root of the consensus state tree.
	LatestStateRoot mkvsNode.Root `json:"latest_state_root"`

	// GenesisHeight is the height of the genesis block.
	GenesisHeight int64 `json:"genesis_height"`
	// GenesisHash is the hash of the genesis block.
	GenesisHash []byte `json:"genesis_hash"`

	// LastRetainedHeight is the height of the oldest retained block.
	LastRetainedHeight int64 `json:"last_retained_height"`
	// LastRetainedHash is the hash of the oldest retained block.
	LastRetainedHash []byte `json:"last_retained_hash"`

	// IsValidator returns whether the current node is part of the validator set.
	IsValidator bool `json:"is_validator"`
}

Status is the current status overview.

type SubmissionManager

type SubmissionManager interface {
	// SignAndSubmitTx populates the nonce and fee fields in the transaction, signs the transaction
	// with the passed signer and submits it to consensus backend.
	//
	// It also automatically handles retries in case the nonce was incorrectly estimated.
	SignAndSubmitTx(ctx context.Context, signer signature.Signer, tx *transaction.Transaction) error
}

SubmissionManager is a transaction submission manager interface.

func NewSubmissionManager

func NewSubmissionManager(backend ClientBackend, priceDiscovery PriceDiscovery, maxFee uint64) SubmissionManager

NewSubmissionManager creates a new transaction submission manager.

type TransactionAuthHandler

type TransactionAuthHandler interface {
	// GetSignerNonce returns the nonce that should be used by the given
	// signer for transmitting the next transaction.
	GetSignerNonce(ctx context.Context, req *GetSignerNonceRequest) (uint64, error)
}

TransactionAuthHandler is the interface for handling transaction authentication (checking nonces and fees).

type TransactionsWithResults

type TransactionsWithResults struct {
	Transactions [][]byte          `json:"transactions"`
	Results      []*results.Result `json:"results"`
}

TransactionsWithResults is GetTransactionsWithResults response.

Results[i] are the results of executing Transactions[i].

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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