light

package
v2.1.4+incompatible Latest Latest
Warning

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

Go to latest
Published: Aug 6, 2022 License: GPL-3.0 Imports: 27 Imported by: 1

Documentation

Overview

Package light implements on-demand retrieval capable state and chain objects for the Ethereum 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")

	ChtTablePrefix = "cht-"
)
View Source
var ErrNoPeers = errors.New("no suitable peers available")

ErrNoPeers is returned if no peers capable of serving a queued request are available

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

Functions

func GetBlock

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

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

func GetBody

func GetBody(ctx context.Context, odr OdrBackend, hash common.Hash, number uint64) (*types.SnailBody, 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 GetChtRoot

func GetChtRoot(db etruedb.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 GetFruit

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

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

func GetFruitBody

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

GetFruitBody retrieves the fruit body (transactons, uncles) corresponding to the hash.

func GetHeaderByNumber

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

func GetUntrustedHeaderByNumber

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

GetUntrustedHeaderByNumber fetches specified block header without correctness checking. Note this function should only be used in light client checkpoint syncing.

func NewChtIndexer

func NewChtIndexer(db etruedb.Database, odr OdrBackend, size, confirms uint64) *snailchain.ChainIndexer

NewChtIndexer creates a Cht chain indexer

func StoreChtRoot

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

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

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 etruedb.Database)

StoreResult stores the retrieved data in local database

type ChtIndexerBackend

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

ChtIndexerBackend implements core.ChainIndexerBackend.

func (*ChtIndexerBackend) Commit

func (c *ChtIndexerBackend) Commit() error

Commit implements core.ChainIndexerBackend

func (*ChtIndexerBackend) Process

func (c *ChtIndexerBackend) Process(ctx context.Context, header *types.SnailHeader) error

Process implements core.ChainIndexerBackend

func (*ChtIndexerBackend) Reset

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

Reset implements core.ChainIndexerBackend

type ChtNode

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

type ChtRequest struct {
	OdrRequest
	Untrusted        bool   // Indicator whether the result retrieved is trusted or not
	PeerId           string // The specified peer id from which to retrieve data.
	Config           *public.IndexerConfig
	ChtNum, BlockNum uint64
	ChtRoot          common.Hash
	Header           *types.SnailHeader
	Td               *big.Int
	Proof            *public.NodeSet
	Headers          []*types.SnailHeader
	Start            bool
	FHeader          *types.Header
	Dataset          [][]byte
	DatasetRoot      common.Hash
}

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

func (*ChtRequest) StoreResult

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

StoreResult stores the retrieved data in local database

type FruitRequest

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

BlockRequest is the ODR request type for retrieving block bodies

func (*FruitRequest) StoreResult

func (req *FruitRequest) StoreResult(db etruedb.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(fastchain *fast.LightChain, odr OdrBackend, config *params.ChainConfig, engine consensus.Engine, checkpoint *params.TrustedCheckpoint) (*LightChain, error)

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

func (*LightChain) AddTrustedCheckpoint

func (lc *LightChain) AddTrustedCheckpoint(cp *params.TrustedCheckpoint)

AddTrustedCheckpoint adds a trusted checkpoint to the blockchain

func (*LightChain) Config

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

Config retrieves the header chain's chain configuration.

func (*LightChain) CurrentHeader

func (lc *LightChain) CurrentHeader() *types.SnailHeader

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

func (*LightChain) DisableCheckFreq

func (lc *LightChain) DisableCheckFreq()

DisableCheckFreq disables header validation. This is used for ultralight mode.

func (*LightChain) EnableCheckFreq

func (lc *LightChain) EnableCheckFreq()

EnableCheckFreq enables header validation.

func (*LightChain) Engine

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

Engine retrieves the light chain's consensus engine.

func (*LightChain) Genesis

func (lc *LightChain) Genesis() *types.SnailBlock

Genesis returns the genesis block

func (*LightChain) GetAncestor

func (lc *LightChain) GetAncestor(hash common.Hash, number, ancestor uint64, maxNonCanonical *uint64) (common.Hash, uint64)

GetAncestor retrieves the Nth ancestor of a given block. It assumes that either the given block or a close ancestor of it is canonical. maxNonCanonical points to a downwards counter limiting the number of blocks to be individually checked before we reach the canonical chain.

Note: ancestor == 0 returns the same block, 1 returns its parent and so on.

func (*LightChain) GetBlock

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

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

func (*LightChain) GetBlockByHash

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

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

func (*LightChain) GetBlockByNumber

func (lc *LightChain) GetBlockByNumber(ctx context.Context, number uint64) (*types.SnailBlock, 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 (lc *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 (lc *LightChain) GetBody(ctx context.Context, hash common.Hash) (*types.SnailBody, 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 (lc *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) GetFruit

func (lc *LightChain) GetFruit(ctx context.Context, hash common.Hash) (*types.SnailBlock, error)

GetFruit retrieves a fruit from the database by FastHash

func (*LightChain) GetFruitHeaderByHash

func (lc *LightChain) GetFruitHeaderByHash(hash common.Hash) *types.SnailHeader

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

func (*LightChain) GetFruitsHead

func (lc *LightChain) GetFruitsHead(number uint64) []*types.SnailHeader

func (*LightChain) GetHeader

func (lc *LightChain) GetHeader(hash common.Hash, number uint64) *types.SnailHeader

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

func (*LightChain) GetHeaderByHash

func (lc *LightChain) GetHeaderByHash(hash common.Hash) *types.SnailHeader

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

func (*LightChain) GetHeaderByNumber

func (lc *LightChain) GetHeaderByNumber(number uint64) *types.SnailHeader

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

func (*LightChain) GetHeaderByNumberOdr

func (lc *LightChain) GetHeaderByNumberOdr(ctx context.Context, number uint64) (*types.SnailHeader, error)

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

func (*LightChain) GetHeaderChain

func (lc *LightChain) GetHeaderChain() *snailchain.HeaderChain

GetHeaderChain loads the last known chain state from the database. This method

func (*LightChain) GetTd

func (lc *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 (lc *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 (lc *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 (lc *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 (lc *LightChain) InsertHeaderChain(chain []*types.SnailHeader, fruitHeads [][]*types.SnailHeader, 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

func (lc *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 (lc *LightChain) Odr() OdrBackend

Odr returns the ODR backend of the chain

func (*LightChain) Reset

func (lc *LightChain) Reset()

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

func (*LightChain) ResetWithGenesisBlock

func (lc *LightChain) ResetWithGenesisBlock(genesis *types.SnailBlock)

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

func (*LightChain) Rollback

func (lc *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 (lc *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) Stop

func (lc *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 (lc *LightChain) SubscribeChainEvent(ch chan<- types.SnailChainEvent) event.Subscription

SubscribeChainEvent registers a subscription of ChainEvent.

func (*LightChain) SubscribeChainHeadEvent

func (lc *LightChain) SubscribeChainHeadEvent(ch chan<- types.SnailChainHeadEvent) event.Subscription

SubscribeChainHeadEvent registers a subscription of ChainHeadEvent.

func (*LightChain) SubscribeChainSideEvent

func (lc *LightChain) SubscribeChainSideEvent(ch chan<- types.SnailChainSideEvent) event.Subscription

SubscribeChainSideEvent registers a subscription of ChainSideEvent.

func (*LightChain) SyncCht

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

func (*LightChain) UnlockChain

func (lc *LightChain) UnlockChain()

UnlockChain unlocks the chain mutex

type OdrBackend

type OdrBackend interface {
	Database() etruedb.Database
	ChtIndexer() *snailchain.ChainIndexer
	Retrieve(ctx context.Context, req OdrRequest) error
	IndexerConfig() *public.IndexerConfig
}

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

type OdrRequest

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

OdrRequest is an interface for retrieval requests

Directories

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

Jump to

Keyboard shortcuts

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