Skip to content

Latest commit

 

History

History
733 lines (431 loc) · 19.1 KB

File metadata and controls

733 lines (431 loc) · 19.1 KB

@ethereumjs/blockchain / Blockchain

Class: Blockchain

This class stores and interacts with blocks.

Implements

Table of contents

Properties

Accessors

Methods

Properties

_common

_common: Common

Defined in

blockchain.ts:53


consensus

consensus: Consensus

Implementation of

BlockchainInterface.consensus

Defined in

blockchain.ts:25


db

db: AbstractLevel<string | Uint8Array | Buffer, string | Buffer, string | Buffer>

Defined in

blockchain.ts:26


dbManager

dbManager: DBManager

Defined in

blockchain.ts:27

Accessors

genesisBlock

get genesisBlock(): Block

The genesis Block for the blockchain.

Returns

Block

Defined in

blockchain.ts:1230

Methods

copy

copy(): Blockchain

Returns a deep copy of this Blockchain instance.

Note: this does not make a copy of the underlying db since it is unknown if the source is on disk or in memory. This should not be a significant issue in most usage since the queries will only reflect the instance's known data. If you would like this copied blockchain to use another db set the db of this returned instance to a copy of the original.

Returns

Blockchain

Implementation of

BlockchainInterface.copy

Defined in

blockchain.ts:173


createGenesisBlock

createGenesisBlock(stateRoot): Block

Creates a genesis Block for the blockchain with params from Common.genesis

Parameters

Name Type Description
stateRoot Buffer The genesis stateRoot

Returns

Block

Defined in

blockchain.ts:1239


delBlock

delBlock(blockHash): Promise<void>

Completely deletes a block from the blockchain including any references to this block. If this block was in the canonical chain, then also each child block of this block is deleted Also, if this was a canonical block, each head header which is part of this now stale chain will be set to the parentHeader of this block An example reason to execute is when running the block in the VM invalidates this block: this will then reset the canonical head to the past block (which has been validated in the past by the VM, so we can be sure it is correct).

Parameters

Name Type Description
blockHash Buffer The hash of the block to be deleted

Returns

Promise<void>

Implementation of

BlockchainInterface.delBlock

Defined in

blockchain.ts:821


genesisState

genesisState(): GenesisState

Returns the genesis state of the blockchain. All values are provided as hex-prefixed strings.

Returns

GenesisState

Implementation of

BlockchainInterface.genesisState

Defined in

blockchain.ts:1264


getBlock

getBlock(blockId): Promise<Block>

Gets a block by its hash.

Parameters

Name Type Description
blockId number | bigint | Buffer The block's hash or number. If a hash is provided, then this will be immediately looked up, otherwise it will wait until we have unlocked the DB

Returns

Promise<Block>

Implementation of

BlockchainInterface.getBlock

Defined in

blockchain.ts:705


getBlocks

getBlocks(blockId, maxBlocks, skip, reverse): Promise<Block[]>

Looks up many blocks relative to blockId Note: due to GetBlockHeaders (0x03) (ETH wire protocol) we have to support skip/reverse as well.

Parameters

Name Type Description
blockId number | bigint | Buffer The block's hash or number
maxBlocks number Max number of blocks to return
skip number Number of blocks to skip apart
reverse boolean Fetch blocks in reverse

Returns

Promise<Block[]>

Defined in

blockchain.ts:739


getCanonicalHeadBlock

getCanonicalHeadBlock(): Promise<Block>

Returns the latest full block in the canonical chain.

Returns

Promise<Block>

Implementation of

BlockchainInterface.getCanonicalHeadBlock

Defined in

blockchain.ts:356


getCanonicalHeadHeader

getCanonicalHeadHeader(): Promise<BlockHeader>

Returns the latest header in the canonical chain.

Returns

Promise<BlockHeader>

Defined in

blockchain.ts:345


getCanonicalHeader

getCanonicalHeader(number): Promise<BlockHeader>

Gets a header by number. Header must be in the canonical chain

Parameters

Name Type
number bigint

Returns

Promise<BlockHeader>

Defined in

blockchain.ts:1204


getHead

getHead(name?): Promise<Block>

Returns the specified iterator head.

Deprecated

use getIteratorHead instead. Note that getIteratorHead doesn't return the headHeader but the genesis hash as an initial iterator head value (now matching the behavior of iterator on a first run)

Parameters

Name Type Default value Description
name string 'vm' Optional name of the iterator head (default: 'vm')

Returns

Promise<Block>

Defined in

blockchain.ts:332


getIteratorHead

getIteratorHead(name?): Promise<Block>

Returns the specified iterator head.

This function replaces the old getHead method. Note that the function deviates from the old behavior and returns the genesis hash instead of the current head block if an iterator has not been run. This matches the behavior of iterator.

Parameters

Name Type Default value Description
name string 'vm' Optional name of the iterator head (default: 'vm')

Returns

Promise<Block>

Implementation of

BlockchainInterface.getIteratorHead

Defined in

blockchain.ts:312


getTotalDifficulty

getTotalDifficulty(hash, number?): Promise<bigint>

Gets total difficulty for a block specified by hash and number

Parameters

Name Type
hash Buffer
number? bigint

Returns

Promise<bigint>

Implementation of

BlockchainInterface.getTotalDifficulty

Defined in

blockchain.ts:724


iterator

iterator(name, onBlock, maxBlocks?): Promise<number>

Iterates through blocks starting at the specified iterator head and calls the onBlock function on each block. The current location of an iterator head can be retrieved using getIteratorHead.

Parameters

Name Type Description
name string Name of the state root head
onBlock OnBlock Function called on each block with params (block, reorg)
maxBlocks? number How many blocks to run. By default, run all unprocessed blocks in the canonical chain.

Returns

Promise<number>

number of blocks actually iterated

Implementation of

BlockchainInterface.iterator

Defined in

blockchain.ts:918


putBlock

putBlock(block): Promise<void>

Adds a block to the blockchain.

If the block is valid and has a higher total difficulty than the current max total difficulty, the canonical chain is rebuilt and any stale heads/hashes are overwritten.

Parameters

Name Type Description
block Block The block to be added to the blockchain

Returns

Promise<void>

Implementation of

BlockchainInterface.putBlock

Defined in

blockchain.ts:387


putBlocks

putBlocks(blocks): Promise<void>

Adds blocks to the blockchain.

If an invalid block is met the function will throw, blocks before will nevertheless remain in the DB. If any of the saved blocks has a higher total difficulty than the current max total difficulty the canonical chain is rebuilt and any stale heads/hashes are overwritten.

Parameters

Name Type Description
blocks Block[] The blocks to be added to the blockchain

Returns

Promise<void>

Defined in

blockchain.ts:373


putHeader

putHeader(header): Promise<void>

Adds a header to the blockchain.

If this header is valid and it has a higher total difficulty than the current max total difficulty, the canonical chain is rebuilt and any stale heads/hashes are overwritten.

Parameters

Name Type Description
header BlockHeader The header to be added to the blockchain

Returns

Promise<void>

Defined in

blockchain.ts:414


putHeaders

putHeaders(headers): Promise<void>

Adds many headers to the blockchain.

If an invalid header is met the function will throw, headers before will nevertheless remain in the DB. If any of the saved headers has a higher total difficulty than the current max total difficulty the canonical chain is rebuilt and any stale heads/hashes are overwritten.

Parameters

Name Type Description
headers any[] The headers to be added to the blockchain

Returns

Promise<void>

Defined in

blockchain.ts:400


safeNumberToHash

safeNumberToHash(number): Promise<false | Buffer>

This method either returns a Buffer if there exists one in the DB or if it does not exist (DB throws a NotFoundError) then return false If DB throws any other error, this function throws.

Parameters

Name Type
number bigint

Returns

Promise<false | Buffer>

Defined in

blockchain.ts:1215


selectNeededHashes

selectNeededHashes(hashes): Promise<Buffer[]>

Given an ordered array, returns an array of hashes that are not in the blockchain yet. Uses binary search to find out what hashes are missing. Therefore, the array needs to be ordered upon number.

Parameters

Name Type Description
hashes Buffer[] Ordered array of hashes (ordered on number).

Returns

Promise<Buffer[]>

Defined in

blockchain.ts:781


setIteratorHead

setIteratorHead(tag, headHash): Promise<void>

Set header hash of a certain tag. When calling the iterator, the iterator will start running the first child block after the header hash currently stored.

Parameters

Name Type Description
tag string The tag to save the headHash to
headHash Buffer The head hash to save

Returns

Promise<void>

Defined in

blockchain.ts:967


validateBlock

validateBlock(block): Promise<void>

Validates a block, by validating the header against the current chain, any uncle headers, and then whether the block is internally consistent

Parameters

Name Type Description
block Block block to be validated

Returns

Promise<void>

Defined in

blockchain.ts:609


validateHeader

validateHeader(header, height?): Promise<void>

Validates a block header, throwing if invalid. It is being validated against the reported parentHash. It verifies the current block against the parentHash:

  • The parentHash is part of the blockchain (it is a valid header)
  • Current block number is parent block number + 1
  • Current block has a strictly higher timestamp
  • Additional PoW checks ->
    • Current block has valid difficulty and gas limit
    • In case that the header is an uncle header, it should not be too old or young in the chain.
  • Additional PoA clique checks ->
    • Checks on coinbase and mixHash
    • Current block has a timestamp diff greater or equal to PERIOD
    • Current block has difficulty correctly marked as INTURN or NOTURN

Parameters

Name Type Description
header BlockHeader header to be validated
height? bigint If this is an uncle header, this is the height of the block that is including it

Returns

Promise<void>

Implementation of

BlockchainInterface.validateHeader

Defined in

blockchain.ts:546


create

Static create(opts?): Promise<Blockchain>

Safe creation of a new Blockchain object awaiting the initialization function, encouraged method to use when creating a blockchain object.

Parameters

Name Type Description
opts BlockchainOptions Constructor options, see BlockchainOptions

Returns

Promise<Blockchain>

Defined in

blockchain.ts:65


fromBlocksData

Static fromBlocksData(blocksData, opts?): Promise<Blockchain>

Creates a blockchain from a list of block objects, objects must be readable by Block.fromBlockData

Parameters

Name Type Description
blocksData BlockData[] -
opts BlockchainOptions Constructor options, see BlockchainOptions

Returns

Promise<Blockchain>

Defined in

blockchain.ts:78