Documentation ¶
Overview ¶
Package downloader contains the manual full chain synchronisation.
Index ¶
- Variables
- type BlockChain
- type DoneEvent
- type Downloader
- func (d *Downloader) Cancel()
- func (d *Downloader) DeliverSnapPacket(peer *snap.Peer, packet snap.Packet) error
- func (d *Downloader) Progress() ethereum.SyncProgress
- func (d *Downloader) RegisterLightPeer(id string, version uint, peer LightPeer) error
- func (d *Downloader) RegisterPeer(id string, version uint, peer Peer) error
- func (d *Downloader) Synchronise(id string, head common.Hash, td *big.Int, mode SyncMode) error
- func (d *Downloader) Synchronising() bool
- func (d *Downloader) Terminate()
- func (d *Downloader) UnregisterPeer(id string) error
- type FailedEvent
- type LightChain
- type LightPeer
- type Peer
- type PublicDownloaderAPI
- type StartEvent
- type SyncMode
- type SyncStatusSubscription
- type SyncingResult
Constants ¶
This section is empty.
Variables ¶
var ( MaxBlockFetch = 128 // Amount of blocks to be fetched per retrieval request MaxHeaderFetch = 192 // Amount of block headers to be fetched per retrieval request MaxSkeletonSize = 128 // Number of header fetches to need for a skeleton assembly MaxReceiptFetch = 256 // Amount of transaction receipts to allow fetching per request )
Functions ¶
This section is empty.
Types ¶
type BlockChain ¶
type BlockChain interface { LightChain // 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. // 同步模式是full的时候用来获取当前区块 CurrentBlock() *types.Block // CurrentFastBlock retrieves the head snap block from the local chain. CurrentFastBlock() *types.Block // 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 }
BlockChain encapsulates functions required to sync a (full or snap) blockchain.
type Downloader ¶
type Downloader struct { SnapSyncer *snap.Syncer // TODO(karalabe): make private! hack for now // contains filtered or unexported fields }
func New ¶
func New(checkpoint uint64, stateDb ethdb.Database, mux *event.TypeMux, chain BlockChain, lightchain LightChain, dropPeer peerDropFn) *Downloader
New creates a new downloader to fetch hashes and blocks from remote peers.
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 ¶
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) RegisterLightPeer ¶
func (d *Downloader) RegisterLightPeer(id string, version uint, peer LightPeer) error
RegisterLightPeer injects a light client peer, wrapping it so it appears as a regular peer.
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. 往d.peers里注册新的peer
func (*Downloader) Synchronise ¶
Synchronise tries to sync up our local block chain with a remote peer, both adding various sanity checks as well as wrapping it with various log entries.
func (*Downloader) Synchronising ¶
func (d *Downloader) Synchronising() bool
Synchronising returns whether the downloader is currently retrieving blocks. 获取当前是否在同步中
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 FailedEvent ¶
type FailedEvent struct{ Err error }
type LightChain ¶
type LightChain 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. // Td是total difficulty, 从创世区块到当前区块难度的总和 GetTd(common.Hash, uint64) *big.Int // InsertHeaderChain inserts a batch of headers into the local chain. InsertHeaderChain([]*types.Header, int) (int, error) // SetHead rewinds the local chain to a new head. SetHead(uint64) error }
LightChain encapsulates functions required to synchronise a light chain. 对应syncmode为light light chain只包括关于head的操作,因为light模式下应该不同步区块体
type LightPeer ¶
type LightPeer 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) }
LightPeer encapsulates the methods required to synchronise with a remote light peer. 远程节点是light类型
type Peer ¶
type Peer interface { LightPeer 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. 远程节点是full类型
type PublicDownloaderAPI ¶
type PublicDownloaderAPI struct {
// contains filtered or unexported fields
}
PublicDownloaderAPI 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 NewPublicDownloaderAPI ¶
func NewPublicDownloaderAPI(d *Downloader, m *event.TypeMux) *PublicDownloaderAPI
NewPublicDownloaderAPI create a new PublicDownloaderAPI. 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 (*PublicDownloaderAPI) SubscribeSyncStatus ¶
func (api *PublicDownloaderAPI) 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 (*PublicDownloaderAPI) Syncing ¶
func (api *PublicDownloaderAPI) Syncing(ctx context.Context) (*rpc.Subscription, error)
Syncing provides information when this nodes starts synchronising with the Ethereum network and when it's finished.
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. SyncMode表示四种节点同步方式 SyncMode其实就是一个uint32 实现了Stringer,TextMarshaler和TextUnmarshaler接口
func (*SyncMode) UnmarshalText ¶
根据字节数组转换成SyncMode类型
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.