light

package
v0.0.0-...-1df7544 Latest Latest
Warning

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

Go to latest
Published: May 12, 2021 License: GPL-3.0 Imports: 21 Imported by: 1

Documentation

Overview

Package light implements on-demand retrieval capable state and chain objects for the MoacNode Light Client.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNoTrustedCht = errors.New("No trusted canonical hash trie")
	ErrNoHeader     = errors.New("Header not found")

	ChtFrequency     = uint64(4096)
	ChtConfirmations = uint64(2048)
)

NoOdr is the default context passed to an ODR capable function when the ODR service is not required.

Functions

func DeleteTrustedCht

func DeleteTrustedCht(db mcdb.Database)

func GetBlock

func GetBlock(ctx context.Context, odr OdrBackend, hash common.Hash, number uint64) (*types.Block, error)

GetBlock retrieves an entire block corresponding to the hash, assembling it back from the stored header and body.

func GetBlockReceipts

func GetBlockReceipts(ctx context.Context, odr OdrBackend, hash common.Hash, number uint64) (types.Receipts, error)

GetBlockReceipts retrieves the receipts generated by the transactions included in a block given by its hash.

func GetBody

func GetBody(ctx context.Context, odr OdrBackend, hash common.Hash, number uint64) (*types.Body, error)

GetBody retrieves the block body (transactons, uncles) corresponding to the hash.

func GetBodyRLP

func GetBodyRLP(ctx context.Context, odr OdrBackend, hash common.Hash, number uint64) (rlp.RawValue, error)

GetBodyRLP retrieves the block body (transactions and uncles) in RLP encoding.

func GetCanonicalHash

func GetCanonicalHash(ctx context.Context, odr OdrBackend, number uint64) (common.Hash, error)

func GetHeaderByNumber

func GetHeaderByNumber(ctx context.Context, odr OdrBackend, number uint64) (*types.Header, error)

func NewState

func NewState(ctx context.Context, head *types.Header, odr OdrBackend) *state.StateDB

func NewStateDatabase

func NewStateDatabase(ctx context.Context, head *types.Header, odr OdrBackend) state.Database

func WriteTrustedCht

func WriteTrustedCht(db mcdb.Database, cht TrustedCht)

Types

type BlockRequest

type BlockRequest struct {
	OdrRequest
	Hash   common.Hash
	Number uint64
	Rlp    []byte
}

BlockRequest is the ODR request type for retrieving block bodies

func (*BlockRequest) StoreResult

func (req *BlockRequest) StoreResult(db mcdb.Database)

StoreResult stores the retrieved data in local database

type ChtNode

type ChtNode struct {
	Hash common.Hash
	Td   *big.Int
}

type ChtRequest

type ChtRequest struct {
	OdrRequest
	ChtNum, BlockNum uint64
	ChtRoot          common.Hash
	Header           *types.Header
	Td               *big.Int
	Proof            []rlp.RawValue
}

TrieRequest is the ODR request type for state/storage trie entries

func (*ChtRequest) StoreResult

func (req *ChtRequest) StoreResult(db mcdb.Database)

StoreResult stores the retrieved data in local database

type CodeRequest

type CodeRequest struct {
	OdrRequest
	Id   *TrieID // references storage trie of the account
	Hash common.Hash
	Data []byte
}

CodeRequest is the ODR request type for retrieving contract code

func (*CodeRequest) StoreResult

func (req *CodeRequest) StoreResult(db mcdb.Database)

StoreResult stores the retrieved data in local database

type LightChain

type LightChain struct {
	// contains filtered or unexported fields
}

LightChain represents a canonical chain that by default only handles block headers, downloading block bodies and receipts on demand through an ODR interface. It only does header validation during chain insertion.

func NewLightChain

func NewLightChain(odr OdrBackend, config *params.ChainConfig, engine consensus.Engine) (*LightChain, error)

NewLightChain returns a fully initialised light chain using information available in the database. It initialises the default MoacNode header validator.

func (*LightChain) CurrentHeader

func (self *LightChain) CurrentHeader() *types.Header

CurrentHeader retrieves the current head header of the canonical chain. The header is retrieved from the HeaderChain's internal cache.

func (*LightChain) Engine

func (bc *LightChain) Engine() consensus.Engine

Engine retrieves the light chain's consensus engine.

func (*LightChain) GasLimit

func (self *LightChain) GasLimit() *big.Int

GasLimit returns the gas limit of the current HEAD block.

func (*LightChain) Genesis

func (bc *LightChain) Genesis() *types.Block

Genesis returns the genesis block

func (*LightChain) GetBlock

func (self *LightChain) GetBlock(ctx context.Context, hash common.Hash, number uint64) (*types.Block, error)

GetBlock retrieves a block from the database or ODR service by hash and number, caching it if found.

func (*LightChain) GetBlockByHash

func (self *LightChain) GetBlockByHash(ctx context.Context, hash common.Hash) (*types.Block, error)

GetBlockByHash retrieves a block from the database or ODR service by hash, caching it if found.

func (*LightChain) GetBlockByNumber

func (self *LightChain) GetBlockByNumber(ctx context.Context, number uint64) (*types.Block, error)

GetBlockByNumber retrieves a block from the database or ODR service by number, caching it (associated with its hash) if found.

func (*LightChain) GetBlockHashesFromHash

func (self *LightChain) GetBlockHashesFromHash(hash common.Hash, max uint64) []common.Hash

GetBlockHashesFromHash retrieves a number of block hashes starting at a given hash, fetching towards the genesis block.

func (*LightChain) GetBody

func (self *LightChain) GetBody(ctx context.Context, hash common.Hash) (*types.Body, error)

GetBody retrieves a block body (transactions and uncles) from the database or ODR service by hash, caching it if found.

func (*LightChain) GetBodyRLP

func (self *LightChain) GetBodyRLP(ctx context.Context, hash common.Hash) (rlp.RawValue, error)

GetBodyRLP retrieves a block body in RLP encoding from the database or ODR service by hash, caching it if found.

func (*LightChain) GetHeader

func (self *LightChain) GetHeader(hash common.Hash, number uint64) *types.Header

GetHeader retrieves a block header from the database by hash and number, caching it if found.

func (*LightChain) GetHeaderByHash

func (self *LightChain) GetHeaderByHash(hash common.Hash) *types.Header

GetHeaderByHash retrieves a block header from the database by hash, caching it if found.

func (*LightChain) GetHeaderByNumber

func (self *LightChain) GetHeaderByNumber(number uint64) *types.Header

GetHeaderByNumber retrieves a block header from the database by number, caching it (associated with its hash) if found.

func (*LightChain) GetHeaderByNumberOdr

func (self *LightChain) GetHeaderByNumberOdr(ctx context.Context, number uint64) (*types.Header, error)

GetHeaderByNumberOdr retrieves a block header from the database or network by number, caching it (associated with its hash) if found.

func (*LightChain) GetTd

func (self *LightChain) GetTd(hash common.Hash, number uint64) *big.Int

GetTd retrieves a block's total difficulty in the canonical chain from the database by hash and number, caching it if found.

func (*LightChain) GetTdByHash

func (self *LightChain) GetTdByHash(hash common.Hash) *big.Int

GetTdByHash retrieves a block's total difficulty in the canonical chain from the database by hash, caching it if found.

func (*LightChain) HasBlock

func (bc *LightChain) HasBlock(hash common.Hash, number uint64) bool

HasBlock checks if a block is fully present in the database or not, caching it if present.

func (*LightChain) HasHeader

func (bc *LightChain) HasHeader(hash common.Hash, number uint64) bool

HasHeader checks if a block header is present in the database or not, caching it if present.

func (*LightChain) InsertHeaderChain

func (self *LightChain) InsertHeaderChain(chain []*types.Header, checkFreq int) (int, error)

InsertHeaderChain attempts to insert the given header chain in to the local chain, possibly creating a reorg. If an error is returned, it will return the index number of the failing header as well an error describing what went wrong.

The verify parameter can be used to fine tune whether nonce verification should be done or not. The reason behind the optional check is because some of the header retrieval mechanisms already need to verfy nonces, as well as because nonces can be verified sparsely, not needing to check each.

In the case of a light chain, InsertHeaderChain also creates and posts light chain events when necessary.

func (*LightChain) LastBlockHash

func (self *LightChain) LastBlockHash() common.Hash

LastBlockHash return the hash of the HEAD block.

func (*LightChain) LockChain

func (self *LightChain) LockChain()

LockChain locks the chain mutex for reading so that multiple canonical hashes can be retrieved while it is guaranteed that they belong to the same version of the chain

func (*LightChain) Odr

func (self *LightChain) Odr() OdrBackend

Odr returns the ODR backend of the chain

func (*LightChain) Reset

func (bc *LightChain) Reset()

Reset purges the entire blockchain, restoring it to its genesis state.

func (*LightChain) ResetWithGenesisBlock

func (bc *LightChain) ResetWithGenesisBlock(genesis *types.Block)

ResetWithGenesisBlock purges the entire blockchain, restoring it to the specified genesis state.

func (*LightChain) Rollback

func (self *LightChain) Rollback(chain []common.Hash)

Rollback is designed to remove a chain of links from the database that aren't certain enough to be valid.

func (*LightChain) SetHead

func (bc *LightChain) SetHead(head uint64)

SetHead rewinds the local chain to a new head. Everything above the new head will be deleted and the new one set.

func (*LightChain) Status

func (self *LightChain) Status() (td *big.Int, currentBlock common.Hash, genesisBlock common.Hash)

Status returns status information about the current chain such as the HEAD Td, the HEAD hash and the hash of the genesis block.

func (*LightChain) Stop

func (bc *LightChain) Stop()

Stop stops the blockchain service. If any imports are currently in progress it will abort them using the procInterrupt.

func (*LightChain) SubscribeChainEvent

func (self *LightChain) SubscribeChainEvent(ch chan<- core.ChainEvent) event.Subscription

SubscribeChainEvent registers a subscription of ChainEvent.

func (*LightChain) SubscribeChainHeadEvent

func (self *LightChain) SubscribeChainHeadEvent(ch chan<- core.ChainHeadEvent) event.Subscription

SubscribeChainHeadEvent registers a subscription of ChainHeadEvent.

func (*LightChain) SubscribeChainSideEvent

func (self *LightChain) SubscribeChainSideEvent(ch chan<- core.ChainSideEvent) event.Subscription

SubscribeChainSideEvent registers a subscription of ChainSideEvent.

func (*LightChain) SubscribeLogsEvent

func (self *LightChain) SubscribeLogsEvent(ch chan<- []*types.Log) event.Subscription

SubscribeLogsEvent implements the interface of filters.Backend LightChain does not send logs events, so return an empty subscription.

func (*LightChain) SubscribeRemovedLogsEvent

func (self *LightChain) SubscribeRemovedLogsEvent(ch chan<- core.RemovedLogsEvent) event.Subscription

SubscribeRemovedLogsEvent implements the interface of filters.Backend LightChain does not send core.RemovedLogsEvent, so return an empty subscription.

func (*LightChain) SyncCht

func (self *LightChain) SyncCht(ctx context.Context) bool

func (*LightChain) UnlockChain

func (self *LightChain) UnlockChain()

UnlockChain unlocks the chain mutex

type OdrBackend

type OdrBackend interface {
	Database() mcdb.Database
	Retrieve(ctx context.Context, req OdrRequest) error
}

OdrBackend is an interface to a backend service that handles ODR retrievals type

type OdrRequest

type OdrRequest interface {
	StoreResult(db mcdb.Database)
}

OdrRequest is an interface for retrieval requests

type ReceiptsRequest

type ReceiptsRequest struct {
	OdrRequest
	Hash     common.Hash
	Number   uint64
	Receipts types.Receipts
}

ReceiptsRequest is the ODR request type for retrieving block bodies

func (*ReceiptsRequest) StoreResult

func (req *ReceiptsRequest) StoreResult(db mcdb.Database)

StoreResult stores the retrieved data in local database

type TrieID

type TrieID struct {
	BlockHash, Root common.Hash
	BlockNumber     uint64
	AccKey          []byte
}

TrieID identifies a state or account storage trie

func StateTrieID

func StateTrieID(header *types.Header) *TrieID

StateTrieID returns a TrieID for a state trie belonging to a certain block header.

func StorageTrieID

func StorageTrieID(state *TrieID, addrHash, root common.Hash) *TrieID

StorageTrieID returns a TrieID for a contract storage trie at a given account of a given state trie. It also requires the root hash of the trie for checking Merkle proofs.

type TrieRequest

type TrieRequest struct {
	OdrRequest
	Id    *TrieID
	Key   []byte
	Proof []rlp.RawValue
}

TrieRequest is the ODR request type for state/storage trie entries

func (*TrieRequest) StoreResult

func (req *TrieRequest) StoreResult(db mcdb.Database)

StoreResult stores the retrieved data in local database

type TrustedCht

type TrustedCht struct {
	Number uint64
	Root   common.Hash
}

func GetTrustedCht

func GetTrustedCht(db mcdb.Database) TrustedCht

type TxPool

type TxPool struct {
	// contains filtered or unexported fields
}

TxPool implements the transaction pool for light clients, which keeps track of the status of locally created transactions, detecting if they are included in a block (mined) or rolled back. There are no queued transactions since we always receive all locally signed transactions in the same order as they are created.

func NewTxPool

func NewTxPool(config *params.ChainConfig, chain *LightChain, relay TxRelayBackend) *TxPool

NewTxPool creates a new light transaction pool

func (*TxPool) Add

func (self *TxPool) Add(ctx context.Context, tx *types.Transaction) error

Add adds a transaction to the pool if valid and passes it to the tx relay backend

func (*TxPool) AddBatch

func (self *TxPool) AddBatch(ctx context.Context, txs []*types.Transaction)

AddTransactions adds all valid transactions to the pool and passes them to the tx relay backend

func (*TxPool) Content

func (self *TxPool) Content() (map[common.Address]types.Transactions, map[common.Address]types.Transactions)

Content retrieves the data content of the transaction pool, returning all the pending as well as queued transactions, grouped by account and nonce.

func (*TxPool) GetNonce

func (pool *TxPool) GetNonce(ctx context.Context, addr common.Address) (uint64, error)

GetNonce returns the "pending" nonce of a given address. It always queries the nonce belonging to the latest header too in order to detect if another client using the same key sent a transaction.

func (*TxPool) GetTransaction

func (tp *TxPool) GetTransaction(hash common.Hash) *types.Transaction

GetTransaction returns a transaction if it is contained in the pool and nil otherwise.

func (*TxPool) GetTransactions

func (self *TxPool) GetTransactions() (txs types.Transactions, err error)

GetTransactions returns all currently processable transactions. The returned slice may be modified by the caller.

func (*TxPool) RemoveTransactions

func (self *TxPool) RemoveTransactions(txs types.Transactions)

RemoveTransactions removes all given transactions from the pool.

func (*TxPool) RemoveTx

func (pool *TxPool) RemoveTx(hash common.Hash)

RemoveTx removes the transaction with the given hash from the pool.

func (*TxPool) Stats

func (pool *TxPool) Stats() (pending int)

Stats returns the number of currently pending (locally created) transactions

func (*TxPool) Stop

func (pool *TxPool) Stop()

Stop stops the light transaction pool

func (*TxPool) SubscribeTxPreEvent

func (pool *TxPool) SubscribeTxPreEvent(ch chan<- core.TxPreEvent) event.Subscription

SubscribeTxPreEvent registers a subscription of core.TxPreEvent and starts sending event to the given channel.

func (*TxPool) SubscribeTxShardJoinEvent

func (pool *TxPool) SubscribeTxShardJoinEvent(ch chan<- core.TxShardJoinEvent) event.Subscription

type TxRelayBackend

type TxRelayBackend interface {
	Send(txs types.Transactions)
	NewHead(head common.Hash, mined []common.Hash, rollback []common.Hash)
	Discard(hashes []common.Hash)
}

TxRelayBackend provides an interface to the mechanism that forwards transacions to the ETH network. The implementations of the functions should be non-blocking.

Send instructs backend to forward new transactions NewHead notifies backend about a new head after processed by the tx pool,

including  mined and rolled back transactions since the last event

Discard notifies backend about transactions that should be discarded either

because they have been replaced by a re-send or because they have been mined
long ago and no rollback is expected

Jump to

Keyboard shortcuts

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