syncer

package
v0.5.2 Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2019 License: GPL-3.0 Imports: 21 Imported by: 8

Documentation

Index

Constants

View Source
const (
	SyncTimeout      = 60 * time.Second
	SyncStateTimeout = 60 * time.Second
)
View Source
const (
	MaxBlockFetch   = 128 // Amount of blocks to be fetched per retrieval request
	MaxHashFetch    = 512 // Amount of hashes to be fetched per retrieval request
	MaxHeaderFetch  = 192 // Amount of block headers to be fetched per retrieval request
	MaxReceiptFetch = 256 // Amount of transaction receipts to allow fetching per request
	MaxStateFetch   = 384 // Amount of node state values to allow fetching per request

)

Variables

View Source
var (
	MaxQueueSize  = 200 // Max size of blocks queue
	MinFullBlocks = configs.DefaultFullSyncPivot
)
View Source
var (
	ErrUnknownPeer     = errors.New("unknown peer")
	ErrSlowPeer        = errors.New("too slow peer")
	ErrTimeout         = errors.New("timeout")
	ErrInvalidChain    = errors.New("retrieved invalid chain")
	ErrReceiptValidate = errors.New("fastsync: receipts validate failure")
	ErrBodiesValidate  = errors.New("fastsync: bodies validate failure")
)
View Source
var (
	ErrQueueFull = errors.New("queue is full")
)

Functions

This section is empty.

Types

type BlockChain

type BlockChain interface {

	// 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

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

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

	GetBlockByNumber(uint64) *types.Block

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

	// CurrentFastBlock retrieves the head fast block from the local chain.
	CurrentFastBlock() *types.Block

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

	// KnownHead returns hash and number of current head block (maybe not in local chain)
	KnownHead() (common.Hash, uint64)

	// SetKnownHead sets the known head block hash and number
	SetKnownHead(common.Hash, uint64)

	// GetReceiptsByHash retrieves the receipts for all transactions in a given block.
	GetReceiptsByHash(hash common.Hash) types.Receipts

	// InsertReceiptChain attempts to complete an already existing header chain with
	// transaction and receipt data.
	InsertReceiptChain(blockChain types.Blocks, receiptChain []types.Receipts) (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.
	InsertHeaderChain(chain []*types.Header, checkFreq int) (int, error)

	// State returns a new mutable state(public) based on the current HEAD block.
	// just for test.
	State() (*state.StateDB, error)

	StateAt(root common.Hash) (*state.StateDB, error)

	Database() database.Database

	// TrieNode retrieves a blob of data associated with a trie node (or code hash)
	// either from ephemeral in-memory cache, or from persistent storage.
	TrieNode(hash common.Hash) ([]byte, error)

	// SetSyncMode set syncMode
	SetSyncMode(mode SyncMode)

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

	// FastSyncCommitHead sets the current head block to the one defined by the hash
	// irrelevant what the chain contents were prior.
	FastSyncCommitHead(hash common.Hash) error
}

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

type DoneEvent

type DoneEvent struct{}

type DropPeer

type DropPeer func(id string)

type FailedEvent

type FailedEvent struct{ Err error }

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 Syncer, 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 int

SyncMode : Full, Fast

const (
	FullSync SyncMode = iota // Synchronise the entire blockchain history from full blocks
	FastSync                 // Quickly download the headers, full sync only at the chain head
)

FullSync, FastSync

type SyncPeer

type SyncPeer interface {
	IDString() string

	// Head returns head block of remote sync peer
	Head() (hash common.Hash, ht *big.Int)

	SendGetBlocks(start uint64) error

	RequestReceipts(hashes []common.Hash) error

	RequestNodeData(hashes []common.Hash) error

	RequestHeadersByNumber(origin uint64, amount int, skip int, reverse bool) error

	RequestBodies([]common.Hash) error
}

SyncPeer represents a remote peer that i can sync with

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 Syncer

type Syncer interface {
	// Synchronise syncs blocks from remote peer with given id
	// from current block to latest block with hash as `head`
	// and number as `height`.
	Synchronise(p SyncPeer, head common.Hash, height *big.Int, mode SyncMode) error

	// Cancel cancels sync process from remote peer
	Cancel(id string)

	Progress() cpchain.SyncProgress

	// Synchronising returns if synchronising now
	Synchronising() bool

	// Terminate terminates all sync process and benchmark calculations
	Terminate()

	// DeliverBlocks delivers blocks from remote peer with id to syncer
	DeliverBlocks(id string, blocks types.Blocks) error

	// DeliverReceipts delivers blocks from remote peer with id to syncer
	DeliverReceipts(id string, receipts []types.Receipts) error

	// DeliverNodeData injects a new batch of node state data received from a remote node.
	DeliverNodeData(id string, data [][]byte) error

	// DeliverHeaders injects a new batch of block headers received from a remote
	// node into the download schedule.
	DeliverHeaders(id string, headers []*types.Header) error

	// DeliverBodies injects a new batch of block bodies received from a remote node.
	DeliverBodies(id string, transactions [][]*types.Transaction) error

	// AddPeer add the peer to the pool
	AddPeer(p SyncPeer) error

	// RemovePeer remove the peer
	RemovePeer(peer string) error
}

Syncer will do all sync related works

type Synchronizer

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

Synchronizer is responsible for syncing local chain to latest block

func New

func New(chain BlockChain, dropPeer DropPeer, mux *event.TypeMux) *Synchronizer

func (*Synchronizer) AddPeer

func (s *Synchronizer) AddPeer(p SyncPeer) error

AddPeer add the peer to the pool

func (*Synchronizer) Cancel

func (s *Synchronizer) Cancel(id string)

Cancel cancels sync process from remote peer

func (*Synchronizer) DeliverBlocks

func (s *Synchronizer) DeliverBlocks(id string, blocks types.Blocks) error

DeliverBlocks delivers blocks from remote peer with id to syncer

func (*Synchronizer) DeliverBodies

func (s *Synchronizer) DeliverBodies(id string, transactions [][]*types.Transaction) error

DeliverBodies injects a new batch of block bodies received from a remote node.

func (*Synchronizer) DeliverHeaders

func (s *Synchronizer) DeliverHeaders(id string, headers []*types.Header) error

DeliverHeaders injects a new batch of block headers received from a remote node into the download schedule.

func (*Synchronizer) DeliverNodeData

func (s *Synchronizer) DeliverNodeData(id string, data [][]byte) error

DeliverNodeData injects a new batch of node state data received from a remote node.

func (*Synchronizer) DeliverReceipts

func (s *Synchronizer) DeliverReceipts(id string, receipts []types.Receipts) error

DeliverReceipts delivers blocks from remote peer with id to syncer

func (*Synchronizer) FetchBodies

func (s *Synchronizer) FetchBodies(hashes []common.Hash) error

FetchBodies sends a block body retrieval request to the remote peer.

func (*Synchronizer) FetchHeaders

func (s *Synchronizer) FetchHeaders(from uint64, count int) error

FetchHeaders sends a header retrieval request to the remote peer.

func (*Synchronizer) Progress

func (s *Synchronizer) Progress() cpchain.SyncProgress

Progress report the progress

func (*Synchronizer) RemovePeer

func (s *Synchronizer) RemovePeer(peer string) error

RemovePeer remove the peer

func (*Synchronizer) Synchronise

func (s *Synchronizer) Synchronise(p SyncPeer, head common.Hash, height *big.Int, mode SyncMode) (err error)

func (*Synchronizer) Synchronising

func (s *Synchronizer) Synchronising() bool

func (*Synchronizer) Terminate

func (s *Synchronizer) Terminate()

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