downloader

package
v0.0.0-...-a1ef408 Latest Latest
Warning

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

Go to latest
Published: Sep 10, 2024 License: GPL-3.0 Imports: 28 Imported by: 0

Documentation

Overview

Package downloader contains the manual full chain synchronisation.

Index

Constants

This section is empty.

Variables

View Source
var (
	MaxBlockFetch   = 128 // Number of blocks to be fetched per retrieval request
	MaxHeaderFetch  = 192 // Number of block headers to be fetched per retrieval request
	MaxReceiptFetch = 256 // Number of transaction receipts to allow fetching per request

)

Functions

This section is empty.

Types

type BlockChain

type BlockChain interface {
	// HasHeader verifies a header's presence in the local chain.
	HasHeader(common.Hash, uint64) bool

	// GetHeaderByHash retrieves a header from the local chain.
	GetHeaderByHash(common.Hash) *types.Header

	// CurrentHeader retrieves the head header from the local chain.
	CurrentHeader() *types.Header

	// GetTd returns the total difficulty of a local block.
	GetTd(common.Hash, uint64) *big.Int

	// InsertHeaderChain inserts a batch of headers into the local chain.
	InsertHeaderChain([]*types.Header) (int, error)

	// SetHead rewinds the local chain to a new head.
	SetHead(uint64) error

	// HasBlock verifies a block's presence in the local chain.
	HasBlock(common.Hash, uint64) bool

	// HasFastBlock verifies a snap block's presence in the local chain.
	HasFastBlock(common.Hash, uint64) bool

	// GetBlockByHash retrieves a block from the local chain.
	GetBlockByHash(common.Hash) *types.Block

	// CurrentBlock retrieves the head block from the local chain.
	CurrentBlock() *types.Header

	// CurrentSnapBlock retrieves the head snap block from the local chain.
	CurrentSnapBlock() *types.Header

	// SnapSyncCommitHead directly commits the head block to a certain entity.
	SnapSyncCommitHead(common.Hash) error

	// InsertChain inserts a batch of blocks into the local chain.
	InsertChain(types.Blocks) (int, error)

	// InsertReceiptChain inserts a batch of receipts into the local chain.
	InsertReceiptChain(types.Blocks, []types.Receipts, uint64) (int, error)

	// Snapshots returns the blockchain snapshot tree to paused it during sync.
	Snapshots() *snapshot.Tree

	// TrieDB retrieves the low level trie database used for interacting
	// with trie nodes.
	TrieDB() *triedb.Database
}

BlockChain encapsulates functions required to sync a (full or snap) blockchain.

type DoneEvent

type DoneEvent struct {
	Latest *types.Header
}

type Downloader

type Downloader struct {
	SnapSyncer *snap.Syncer // TODO(karalabe): make private! hack for now
	// contains filtered or unexported fields
}

func New

func New(
	stateDb ethdb.Database, mux *event.TypeMux, chain BlockChain, dropPeer peerDropFn, success func(),
) *Downloader

New creates a new downloader to fetch hashes and blocks from remote peers.

func (*Downloader) BeaconDevSync

func (d *Downloader) BeaconDevSync(mode SyncMode, hash common.Hash, stop chan struct{}) error

BeaconDevSync is a development helper to test synchronization by providing a block hash instead of header to run the beacon sync against.

The method will reach out to the network to retrieve the header of the sync target instead of receiving it from the consensus node.

Note, this must not be used in live code. If the forkchcoice endpoint where to use this instead of giving us the payload first, then essentially nobody in the network would have the block yet that we'd attempt to retrieve.

func (*Downloader) BeaconExtend

func (d *Downloader) BeaconExtend(mode SyncMode, head *types.Header) error

BeaconExtend is an optimistic version of BeaconSync, where an attempt is made to extend the current beacon chain with a new header, but in case of a mismatch, the old sync will not be terminated and reorged, rather the new head is dropped.

This is useful if a beacon client is feeding us large chunks of payloads to run, but is not setting the head after each.

func (*Downloader) BeaconSync

func (d *Downloader) BeaconSync(mode SyncMode, head *types.Header, final *types.Header) error

BeaconSync is the post-merge version of the chain synchronization, where the chain is not downloaded from genesis onward, rather from trusted head announces backwards.

Internally backfilling and state sync is done the same way, but the header retrieval and scheduling is replaced.

func (*Downloader) Cancel

func (d *Downloader) Cancel()

Cancel aborts all of the operations and waits for all download goroutines to finish before returning.

func (*Downloader) DeliverSnapPacket

func (d *Downloader) DeliverSnapPacket(peer *snap.Peer, packet snap.Packet) error

DeliverSnapPacket is invoked from a peer's message handler when it transmits a data packet for the local node to consume.

func (*Downloader) Progress

func (d *Downloader) Progress() ethereum.SyncProgress

Progress retrieves the synchronisation boundaries, specifically the origin block where synchronisation started at (may have failed/suspended); the block or header sync is currently at; and the latest known block which the sync targets.

In addition, during the state download phase of snap synchronisation the number of processed and the total number of known states are also returned. Otherwise these are zero.

func (*Downloader) RegisterPeer

func (d *Downloader) RegisterPeer(id string, version uint, peer Peer) error

RegisterPeer injects a new download peer into the set of block source to be used for fetching hashes and blocks from.

func (*Downloader) SetBadBlockCallback

func (d *Downloader) SetBadBlockCallback(onBadBlock badBlockFn)

SetBadBlockCallback sets the callback to run when a bad block is hit by the block processor. This method is not thread safe and should be set only once on startup before system events are fired.

func (*Downloader) Terminate

func (d *Downloader) Terminate()

Terminate interrupts the downloader, canceling all pending operations. The downloader cannot be reused after calling Terminate.

func (*Downloader) UnregisterPeer

func (d *Downloader) UnregisterPeer(id string) error

UnregisterPeer remove a peer from the known list, preventing any action from the specified peer. An effort is also made to return any pending fetches into the queue.

type DownloaderAPI

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

DownloaderAPI provides an API which gives information about the current synchronisation status. It offers only methods that operates on data that can be available to anyone without security risks.

func NewDownloaderAPI

func NewDownloaderAPI(d *Downloader, chain *core.BlockChain, m *event.TypeMux) *DownloaderAPI

NewDownloaderAPI creates a new DownloaderAPI. The API has an internal event loop that listens for events from the downloader through the global event mux. In case it receives one of these events it broadcasts it to all syncing subscriptions that are installed through the installSyncSubscription channel.

func (*DownloaderAPI) SubscribeSyncStatus

func (api *DownloaderAPI) SubscribeSyncStatus(status chan interface{}) *SyncStatusSubscription

SubscribeSyncStatus creates a subscription that will broadcast new synchronisation updates. The given channel must receive interface values, the result can either.

func (*DownloaderAPI) Syncing

func (api *DownloaderAPI) Syncing(ctx context.Context) (*rpc.Subscription, error)

Syncing provides information when this node starts synchronising with the Ethereum network and when it's finished.

type FailedEvent

type FailedEvent struct{ Err error }

type Peer

type Peer interface {
	Head() (common.Hash, *big.Int)
	RequestHeadersByHash(common.Hash, int, int, bool, chan *eth.Response) (*eth.Request, error)
	RequestHeadersByNumber(uint64, int, int, bool, chan *eth.Response) (*eth.Request, error)

	RequestBodies([]common.Hash, chan *eth.Response) (*eth.Request, error)
	RequestReceipts([]common.Hash, chan *eth.Response) (*eth.Request, error)
}

Peer encapsulates the methods required to synchronise with a remote full peer.

type StartEvent

type StartEvent struct{}

type SyncMode

type SyncMode uint32

SyncMode represents the synchronisation mode of the downloader. It is a uint32 as it is used with atomic operations.

const (
	FullSync SyncMode = iota // Synchronise the entire blockchain history from full blocks
	SnapSync                 // Download the chain and the state via compact snapshots
)

func (SyncMode) IsValid

func (mode SyncMode) IsValid() bool

func (SyncMode) MarshalText

func (mode SyncMode) MarshalText() ([]byte, error)

func (SyncMode) String

func (mode SyncMode) String() string

String implements the stringer interface.

func (*SyncMode) UnmarshalText

func (mode *SyncMode) UnmarshalText(text []byte) error

type SyncStatusSubscription

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

SyncStatusSubscription represents a syncing subscription.

func (*SyncStatusSubscription) Unsubscribe

func (s *SyncStatusSubscription) Unsubscribe()

Unsubscribe uninstalls the subscription from the DownloadAPI event loop. The status channel that was passed to subscribeSyncStatus isn't used anymore after this method returns.

type SyncingResult

type SyncingResult struct {
	Syncing bool                  `json:"syncing"`
	Status  ethereum.SyncProgress `json:"status"`
}

SyncingResult provides information about the current synchronisation status for this node.

Jump to

Keyboard shortcuts

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