light

package
v1.8.2 Latest Latest
Warning

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

Go to latest
Published: Mar 5, 2018 License: GPL-3.0 Imports: 23 Imported by: 0

Documentation

Overview

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

Index

Constants

View Source
const (
	// CHTFrequencyClient is the block frequency for creating CHTs on the client side.
	CHTFrequencyClient = 32768

	// CHTFrequencyServer is the block frequency for creating CHTs on the server side.
	// Eventually this can be merged back with the client version, but that requires a
	// full database upgrade, so that should be left for a suitable moment.
	CHTFrequencyServer = 4096

	HelperTrieConfirmations        = 2048 // number of confirmations before a server is expected to have the given HelperTrie available
	HelperTrieProcessConfirmations = 256  // number of confirmations before a HelperTrie is generated
)
View Source
const (
	BloomTrieFrequency = 32768
)

Variables

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

	ChtTablePrefix = "cht-"
)
View Source
var (
	BloomTrieTablePrefix = "blt-"
)

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

Functions

func GetBlock added in v1.5.0

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 GetBlockLogs added in v1.8.2

func GetBlockLogs(ctx context.Context, odr OdrBackend, hash common.Hash, number uint64) ([][]*types.Log, error)

GetBlockLogs retrieves the logs generated by the transactions included in a block given by its hash.

func GetBlockReceipts added in v1.5.0

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 GetBloomBits added in v1.7.3

func GetBloomBits(ctx context.Context, odr OdrBackend, bitIdx uint, sectionIdxList []uint64) ([][]byte, error)

GetBloomBits retrieves a batch of compressed bloomBits vectors belonging to the given bit index and section indexes

func GetBloomTrieRoot added in v1.7.3

func GetBloomTrieRoot(db ethdb.Database, sectionIdx uint64, sectionHead common.Hash) common.Hash

GetBloomTrieRoot reads the BloomTrie root assoctiated to the given section from the database

func GetBody added in v1.5.0

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 added in v1.5.0

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 added in v1.5.0

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

func GetChtRoot added in v1.7.3

func GetChtRoot(db ethdb.Database, sectionIdx uint64, sectionHead common.Hash) common.Hash

GetChtRoot reads the CHT root assoctiated to the given section from the database Note that sectionIdx is specified according to LES/1 CHT section size

func GetChtV2Root added in v1.7.3

func GetChtV2Root(db ethdb.Database, sectionIdx uint64, sectionHead common.Hash) common.Hash

GetChtV2Root reads the CHT root assoctiated to the given section from the database Note that sectionIdx is specified according to LES/2 CHT section size

func GetHeaderByNumber added in v1.5.0

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

func NewBloomTrieIndexer added in v1.7.3

func NewBloomTrieIndexer(db ethdb.Database, clientMode bool) *core.ChainIndexer

NewBloomTrieIndexer creates a BloomTrie chain indexer

func NewChtIndexer added in v1.7.3

func NewChtIndexer(db ethdb.Database, clientMode bool) *core.ChainIndexer

NewBloomTrieIndexer creates a BloomTrie chain indexer

func NewState added in v1.6.7

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

func NewStateDatabase added in v1.6.7

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

func StoreBloomTrieRoot added in v1.7.3

func StoreBloomTrieRoot(db ethdb.Database, sectionIdx uint64, sectionHead, root common.Hash)

StoreBloomTrieRoot writes the BloomTrie root assoctiated to the given section into the database

func StoreChtRoot added in v1.7.3

func StoreChtRoot(db ethdb.Database, sectionIdx uint64, sectionHead, root common.Hash)

StoreChtRoot writes the CHT root assoctiated to the given section into the database Note that sectionIdx is specified according to LES/1 CHT section size

Types

type BlockRequest added in v1.5.0

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

BlockRequest is the ODR request type for retrieving block bodies

func (*BlockRequest) StoreResult added in v1.5.0

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

StoreResult stores the retrieved data in local database

type BloomRequest added in v1.7.3

type BloomRequest struct {
	OdrRequest
	BloomTrieNum   uint64
	BitIdx         uint
	SectionIdxList []uint64
	BloomTrieRoot  common.Hash
	BloomBits      [][]byte
	Proofs         *NodeSet
}

BloomRequest is the ODR request type for retrieving bloom filters from a CHT structure

func (*BloomRequest) StoreResult added in v1.7.3

func (req *BloomRequest) StoreResult(db ethdb.Database)

StoreResult stores the retrieved data in local database

type BloomTrieIndexerBackend added in v1.7.3

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

BloomTrieIndexerBackend implements core.ChainIndexerBackend

func (*BloomTrieIndexerBackend) Commit added in v1.7.3

func (b *BloomTrieIndexerBackend) Commit() error

Commit implements core.ChainIndexerBackend

func (*BloomTrieIndexerBackend) Process added in v1.7.3

func (b *BloomTrieIndexerBackend) Process(header *types.Header)

Process implements core.ChainIndexerBackend

func (*BloomTrieIndexerBackend) Reset added in v1.7.3

func (b *BloomTrieIndexerBackend) Reset(section uint64, lastSectionHead common.Hash) error

Reset implements core.ChainIndexerBackend

type ChtIndexerBackend added in v1.7.3

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

ChtIndexerBackend implements core.ChainIndexerBackend

func (*ChtIndexerBackend) Commit added in v1.7.3

func (c *ChtIndexerBackend) Commit() error

Commit implements core.ChainIndexerBackend

func (*ChtIndexerBackend) Process added in v1.7.3

func (c *ChtIndexerBackend) Process(header *types.Header)

Process implements core.ChainIndexerBackend

func (*ChtIndexerBackend) Reset added in v1.7.3

func (c *ChtIndexerBackend) Reset(section uint64, lastSectionHead common.Hash) error

Reset implements core.ChainIndexerBackend

type ChtNode added in v1.5.0

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

ChtNode structures are stored in the Canonical Hash Trie in an RLP encoded format

type ChtRequest added in v1.5.0

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

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

func (*ChtRequest) StoreResult added in v1.5.0

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

StoreResult stores the retrieved data in local database

type CodeRequest added in v1.5.0

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 added in v1.5.0

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

StoreResult stores the retrieved data in local database

type LightChain added in v1.5.0

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 added in v1.5.0

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 Ethereum header validator.

func (*LightChain) Config added in v1.8.0

func (self *LightChain) Config() *params.ChainConfig

Config retrieves the header chain's chain configuration.

func (*LightChain) CurrentHeader added in v1.5.0

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 added in v1.6.0

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

Engine retrieves the light chain's consensus engine.

func (*LightChain) GasLimit added in v1.5.0

func (self *LightChain) GasLimit() uint64

GasLimit returns the gas limit of the current HEAD block.

func (*LightChain) Genesis added in v1.5.0

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

Genesis returns the genesis block

func (*LightChain) GetBlock added in v1.5.0

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 added in v1.5.0

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 added in v1.5.0

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 added in v1.5.0

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 added in v1.5.0

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 added in v1.5.0

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 added in v1.5.0

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 added in v1.5.0

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 added in v1.5.0

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 added in v1.5.0

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 added in v1.5.0

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 added in v1.5.0

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 added in v1.5.0

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 added in v1.5.0

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 added in v1.5.0

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) LockChain added in v1.5.5

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 added in v1.5.0

func (self *LightChain) Odr() OdrBackend

Odr returns the ODR backend of the chain

func (*LightChain) Reset added in v1.5.0

func (bc *LightChain) Reset()

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

func (*LightChain) ResetWithGenesisBlock added in v1.5.0

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

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

func (*LightChain) Rollback added in v1.5.0

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 added in v1.5.0

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) State added in v1.5.0

func (bc *LightChain) State() (*state.StateDB, error)

State returns a new mutable state based on the current HEAD block.

func (*LightChain) Stop added in v1.5.0

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 added in v1.7.0

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

SubscribeChainEvent registers a subscription of ChainEvent.

func (*LightChain) SubscribeChainHeadEvent added in v1.7.0

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

SubscribeChainHeadEvent registers a subscription of ChainHeadEvent.

func (*LightChain) SubscribeChainSideEvent added in v1.7.0

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

SubscribeChainSideEvent registers a subscription of ChainSideEvent.

func (*LightChain) SubscribeLogsEvent added in v1.7.0

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 added in v1.7.0

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 added in v1.5.0

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

func (*LightChain) UnlockChain added in v1.5.5

func (self *LightChain) UnlockChain()

UnlockChain unlocks the chain mutex

type NodeList added in v1.7.3

type NodeList []rlp.RawValue

NodeList stores an ordered list of trie nodes. It implements ethdb.Putter.

func (NodeList) DataSize added in v1.7.3

func (n NodeList) DataSize() int

DataSize returns the aggregated data size of nodes in the list

func (NodeList) NodeSet added in v1.7.3

func (n NodeList) NodeSet() *NodeSet

NodeSet converts the node list to a NodeSet

func (*NodeList) Put added in v1.7.3

func (n *NodeList) Put(key []byte, value []byte) error

Put stores a new node at the end of the list

func (NodeList) Store added in v1.7.3

func (n NodeList) Store(db ethdb.Putter)

Store writes the contents of the list to the given database

type NodeSet added in v1.7.3

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

NodeSet stores a set of trie nodes. It implements trie.Database and can also act as a cache for another trie.Database.

func NewNodeSet added in v1.7.3

func NewNodeSet() *NodeSet

NewNodeSet creates an empty node set

func (*NodeSet) DataSize added in v1.7.3

func (db *NodeSet) DataSize() int

DataSize returns the aggregated data size of nodes in the set

func (*NodeSet) Get added in v1.7.3

func (db *NodeSet) Get(key []byte) ([]byte, error)

Get returns a stored node

func (*NodeSet) Has added in v1.7.3

func (db *NodeSet) Has(key []byte) (bool, error)

Has returns true if the node set contains the given key

func (*NodeSet) KeyCount added in v1.7.3

func (db *NodeSet) KeyCount() int

KeyCount returns the number of nodes in the set

func (*NodeSet) NodeList added in v1.7.3

func (db *NodeSet) NodeList() NodeList

NodeList converts the node set to a NodeList

func (*NodeSet) Put added in v1.7.3

func (db *NodeSet) Put(key []byte, value []byte) error

Put stores a new node in the set

func (*NodeSet) Store added in v1.7.3

func (db *NodeSet) Store(target ethdb.Putter)

Store writes the contents of the set to the given database

type OdrBackend

type OdrBackend interface {
	Database() ethdb.Database
	ChtIndexer() *core.ChainIndexer
	BloomTrieIndexer() *core.ChainIndexer
	BloomIndexer() *core.ChainIndexer
	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 ethdb.Database)
}

OdrRequest is an interface for retrieval requests

type ReceiptsRequest added in v1.5.0

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 added in v1.5.0

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

StoreResult stores the retrieved data in local database

type TrieID added in v1.5.0

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

TrieID identifies a state or account storage trie

func StateTrieID added in v1.5.0

func StateTrieID(header *types.Header) *TrieID

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

func StorageTrieID added in v1.5.0

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 *NodeSet
}

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

func (*TrieRequest) StoreResult

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

StoreResult stores the retrieved data in local database

type TxPool added in v1.5.0

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 added in v1.5.0

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

NewTxPool creates a new light transaction pool

func (*TxPool) Add added in v1.5.0

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 added in v1.5.0

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 added in v1.5.0

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 added in v1.5.0

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 added in v1.5.0

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 added in v1.5.0

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 added in v1.5.0

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

RemoveTransactions removes all given transactions from the pool.

func (*TxPool) RemoveTx added in v1.5.0

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

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

func (*TxPool) Stats added in v1.5.0

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

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

func (*TxPool) Stop added in v1.5.0

func (pool *TxPool) Stop()

Stop stops the light transaction pool

func (*TxPool) SubscribeTxPreEvent added in v1.7.0

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.

type TxRelayBackend added in v1.5.0

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