Ethereum  PoC-8
The C++ Implementation of Ethereum
dev::eth::BlockChain Class Reference

Implements the blockchain database. All data this gives is disk-backed. @threadsafe. More...

#include <BlockChain.h>

Classes

struct  Statistics
 

Public Member Functions

 BlockChain (ChainParams const &_p, boost::filesystem::path const &_path, WithExisting _we=WithExisting::Trust, ProgressCallback const &_pc=ProgressCallback())
 
 ~BlockChain ()
 
void reopen (WithExisting _we=WithExisting::Trust, ProgressCallback const &_pc=ProgressCallback())
 Reopen everything. More...
 
void reopen (ChainParams const &_p, WithExisting _we=WithExisting::Trust, ProgressCallback const &_pc=ProgressCallback())
 
void process ()
 
std::tuple< ImportRoute, bool, unsigned > sync (BlockQueue &_bq, OverlayDB const &_stateDB, unsigned _max)
 
std::pair< ImportResult, ImportRouteattemptImport (bytes const &_block, OverlayDB const &_stateDB, bool _mutBeNew=true) noexcept
 
ImportRoute import (bytes const &_block, OverlayDB const &_stateDB, bool _mustBeNew=true)
 
ImportRoute import (VerifiedBlockRef const &_block, OverlayDB const &_db, bool _mustBeNew=true)
 
void insert (bytes const &_block, bytesConstRef _receipts, bool _mustBeNew=true)
 
void insert (VerifiedBlockRef _block, bytesConstRef _receipts, bool _mustBeNew=true)
 
ImportRoute insertWithoutParent (bytes const &_block, bytesConstRef _receipts, u256 const &_totalDifficulty)
 Insert that doesn't require parent to be imported, useful when we don't have the full blockchain (like restoring from partial snapshot). More...
 
bool isKnown (h256 const &_hash, bool _isCurrent=true) const
 Returns true if the given block is known (though not necessarily a part of the canon chain). More...
 
BlockHeader info (h256 const &_hash) const
 Get the partial-header of a block (or the most recent mined if none given). Thread-safe. More...
 
BlockHeader info () const
 
bytes block (h256 const &_hash) const
 Get a block (RLP format) for the given hash (or the most recent mined if none given). Thread-safe. More...
 
bytes block () const
 
bytes headerData (h256 const &_hash) const
 Get a block (RLP format) for the given hash (or the most recent mined if none given). Thread-safe. More...
 
bytes headerData () const
 
BlockDetails details (h256 const &_hash) const
 Get the familial details concerning a block (or the most recent mined if none given). Thread-safe. More...
 
BlockDetails details () const
 
BlockLogBlooms logBlooms (h256 const &_hash) const
 Get the transactions' log blooms of a block (or the most recent mined if none given). Thread-safe. More...
 
BlockLogBlooms logBlooms () const
 
BlockReceipts receipts (h256 const &_hash) const
 
BlockReceipts receipts () const
 
TransactionReceipt transactionReceipt (h256 const &_blockHash, unsigned _i) const
 Get the transaction by block hash and index;. More...
 
TransactionReceipt transactionReceipt (h256 const &_transactionHash) const
 Get the transaction receipt by transaction hash. Thread-safe. More...
 
TransactionHashes transactionHashes (h256 const &_hash) const
 Get a list of transaction hashes for a given block. Thread-safe. More...
 
TransactionHashes transactionHashes () const
 
UncleHashes uncleHashes (h256 const &_hash) const
 Get a list of uncle hashes for a given block. Thread-safe. More...
 
UncleHashes uncleHashes () const
 
h256 numberHash (unsigned _i) const
 Get the hash for a given block's number. More...
 
LastBlockHashesFace const & lastBlockHashes () const
 
BlocksBlooms blocksBlooms (unsigned _level, unsigned _index) const
 
BlocksBlooms blocksBlooms (h256 const &_chunkId) const
 
LogBloom blockBloom (unsigned _number) const
 
std::vector< unsigned > withBlockBloom (LogBloom const &_b, unsigned _earliest, unsigned _latest) const
 
std::vector< unsigned > withBlockBloom (LogBloom const &_b, unsigned _earliest, unsigned _latest, unsigned _topLevel, unsigned _index) const
 
bool isKnownTransaction (h256 const &_transactionHash) const
 Returns true if transaction is known. Thread-safe. More...
 
bytes transaction (h256 const &_transactionHash) const
 Get a transaction from its hash. Thread-safe. More...
 
std::pair< h256, unsigned > transactionLocation (h256 const &_transactionHash) const
 
bytes transaction (h256 const &_blockHash, unsigned _i) const
 Get a block's transaction (RLP format) for the given block hash (or the most recent mined if none given) & index. Thread-safe. More...
 
bytes transaction (unsigned _i) const
 
std::vector< bytestransactions (h256 const &_blockHash) const
 Get all transactions from a block. More...
 
std::vector< bytestransactions () const
 
unsigned number (h256 const &_hash) const
 Get a number for the given hash (or the most recent mined if none given). Thread-safe. More...
 
unsigned number () const
 
h256 currentHash () const
 Get a given block (RLP format). Thread-safe. More...
 
h256 genesisHash () const
 Get the hash of the genesis block. Thread-safe. More...
 
h256Hash allKinFrom (h256 const &_parent, unsigned _generations) const
 
void rebuild (boost::filesystem::path const &_path, ProgressCallback const &_progress=std::function< void(unsigned, unsigned)>())
 
void rewind (unsigned _newHead)
 Alter the head of the chain to some prior block along it. More...
 
void rescue (OverlayDB const &_db)
 Rescue the database. More...
 
std::tuple< h256s, h256, unsigned > treeRoute (h256 const &_from, h256 const &_to, bool _common=true, bool _pre=true, bool _post=true) const
 
Statistics usage (bool _freshen=false) const
 
void garbageCollect (bool _force=false)
 Deallocate unused data. More...
 
void setOnBad (std::function< void(Exception &)> _t)
 Change the function that is called with a bad block. More...
 
void setOnBlockImport (std::function< void(BlockHeader const &)> _t)
 Change the function that is called when a new block is imported. More...
 
Block genesisBlock (OverlayDB const &_db) const
 Get a pre-made genesis State object. More...
 
VerifiedBlockRef verifyBlock (bytesConstRef _block, std::function< void(Exception &)> const &_onBad, ImportRequirements::value _ir=ImportRequirements::OutOfOrderChecks) const
 Verify block and prepare it for enactment. More...
 
std::string dumpDatabase () const
 Gives a dump of the blockchain database. For debug/test use only. More...
 
ChainParams const & chainParams () const
 
SealEngineFacesealEngine () const
 
BlockHeader const & genesis () const
 
unsigned chainStartBlockNumber () const
 
void setChainStartBlockNumber (unsigned _number)
 Change the chain start block. More...
 

Friends

std::ostream & operator<< (std::ostream &_out, BlockChain const &_bc)
 

Detailed Description

Implements the blockchain database. All data this gives is disk-backed. @threadsafe.

Definition at line 104 of file BlockChain.h.

Constructor & Destructor Documentation

◆ BlockChain()

BlockChain::BlockChain ( ChainParams const &  _p,
boost::filesystem::path const &  _path,
WithExisting  _we = WithExisting::Trust,
ProgressCallback const &  _pc = ProgressCallback() 
)

Doesn't open the database - if you want it open it's up to you to subclass this and open it in the constructor there.

Definition at line 172 of file BlockChain.cpp.

◆ ~BlockChain()

BlockChain::~BlockChain ( )

Definition at line 180 of file BlockChain.cpp.

Member Function Documentation

◆ allKinFrom()

h256Hash BlockChain::allKinFrom ( h256 const &  _parent,
unsigned  _generations 
) const

Get all blocks not allowed as uncles given a parent (i.e. featured as uncles/main in parent, parent + 1, ... parent + _generations).

Returns
set including the header-hash of every parent (including _parent) up to and including generation + _generations togther with all their quoted uncles.

Definition at line 1388 of file BlockChain.cpp.

◆ attemptImport()

pair< ImportResult, ImportRoute > BlockChain::attemptImport ( bytes const &  _block,
OverlayDB const &  _stateDB,
bool  _mutBeNew = true 
)
noexcept

Attempt to import the given block directly into the BlockChain and sync with the state DB.

Returns
the block hashes of any blocks that came into/went out of the canonical block chain.

Definition at line 507 of file BlockChain.cpp.

◆ block() [1/2]

bytes dev::eth::BlockChain::block ( ) const
inline

Definition at line 150 of file BlockChain.h.

◆ block() [2/2]

bytes BlockChain::block ( h256 const &  _hash) const

Get a block (RLP format) for the given hash (or the most recent mined if none given). Thread-safe.

Definition at line 1423 of file BlockChain.cpp.

◆ blockBloom()

LogBloom dev::eth::BlockChain::blockBloom ( unsigned  _number) const
inline

Definition at line 204 of file BlockChain.h.

◆ blocksBlooms() [1/2]

BlocksBlooms dev::eth::BlockChain::blocksBlooms ( h256 const &  _chunkId) const
inline

Definition at line 203 of file BlockChain.h.

◆ blocksBlooms() [2/2]

BlocksBlooms dev::eth::BlockChain::blocksBlooms ( unsigned  _level,
unsigned  _index 
) const
inline

Get the block blooms for a number of blocks. Thread-safe.

Returns
the object pertaining to the blocks: level 0: 0x, 0x + 1, .. (1x - 1) 1x, 1x + 1, .. (2x - 1) ... (255x .. (256x - 1)) level 1: 0x .. (1x - 1), 1x .. (2x - 1), ..., (255x .. (256x - 1)) 256x .. (257x - 1), 257x .. (258x - 1), ..., (511x .. (512x - 1)) ... level n, index i, offset o: i * (x ^ n) + o * x ^ (n - 1)

Definition at line 202 of file BlockChain.h.

◆ chainParams()

ChainParams const& dev::eth::BlockChain::chainParams ( ) const
inline

Definition at line 306 of file BlockChain.h.

◆ chainStartBlockNumber()

unsigned BlockChain::chainStartBlockNumber ( ) const
Returns
first block number of the chain, non-zero when we have partial chain e.g. after snapshot import.

Definition at line 1606 of file BlockChain.cpp.

◆ currentHash()

h256 dev::eth::BlockChain::currentHash ( ) const
inline

Get a given block (RLP format). Thread-safe.

Definition at line 228 of file BlockChain.h.

◆ details() [1/2]

BlockDetails dev::eth::BlockChain::details ( ) const
inline

Definition at line 158 of file BlockChain.h.

◆ details() [2/2]

BlockDetails dev::eth::BlockChain::details ( h256 const &  _hash) const
inline

Get the familial details concerning a block (or the most recent mined if none given). Thread-safe.

Definition at line 157 of file BlockChain.h.

◆ dumpDatabase()

string BlockChain::dumpDatabase ( ) const

Gives a dump of the blockchain database. For debug/test use only.

Definition at line 424 of file BlockChain.cpp.

◆ garbageCollect()

void BlockChain::garbageCollect ( bool  _force = false)

Deallocate unused data.

Definition at line 1211 of file BlockChain.cpp.

◆ genesis()

BlockHeader const & BlockChain::genesis ( ) const

Definition at line 185 of file BlockChain.cpp.

◆ genesisBlock()

Block BlockChain::genesisBlock ( OverlayDB const &  _db) const

Get a pre-made genesis State object.

Definition at line 1479 of file BlockChain.cpp.

◆ genesisHash()

h256 dev::eth::BlockChain::genesisHash ( ) const
inline

Get the hash of the genesis block. Thread-safe.

Definition at line 231 of file BlockChain.h.

◆ headerData() [1/2]

bytes dev::eth::BlockChain::headerData ( ) const
inline

Definition at line 154 of file BlockChain.h.

◆ headerData() [2/2]

bytes BlockChain::headerData ( h256 const &  _hash) const

Get a block (RLP format) for the given hash (or the most recent mined if none given). Thread-safe.

Definition at line 1451 of file BlockChain.cpp.

◆ import() [1/2]

ImportRoute BlockChain::import ( bytes const &  _block,
OverlayDB const &  _stateDB,
bool  _mustBeNew = true 
)

Import block into disk-backed DB.

Returns
the block hashes of any blocks that came into/went out of the canonical block chain.

Definition at line 533 of file BlockChain.cpp.

◆ import() [2/2]

ImportRoute BlockChain::import ( VerifiedBlockRef const &  _block,
OverlayDB const &  _db,
bool  _mustBeNew = true 
)

Definition at line 650 of file BlockChain.cpp.

◆ info() [1/2]

BlockHeader dev::eth::BlockChain::info ( ) const
inline

Definition at line 146 of file BlockChain.h.

◆ info() [2/2]

BlockHeader dev::eth::BlockChain::info ( h256 const &  _hash) const
inline

Get the partial-header of a block (or the most recent mined if none given). Thread-safe.

Definition at line 145 of file BlockChain.h.

◆ insert() [1/2]

void BlockChain::insert ( bytes const &  _block,
bytesConstRef  _receipts,
bool  _mustBeNew = true 
)

Import data into disk-backed DB. This will not execute the block and populate the state trie, but rather will simply add the block/header and receipts directly into the databases.

Definition at line 540 of file BlockChain.cpp.

◆ insert() [2/2]

void BlockChain::insert ( VerifiedBlockRef  _block,
bytesConstRef  _receipts,
bool  _mustBeNew = true 
)

Definition at line 547 of file BlockChain.cpp.

◆ insertWithoutParent()

ImportRoute BlockChain::insertWithoutParent ( bytes const &  _block,
bytesConstRef  _receipts,
u256 const &  _totalDifficulty 
)

Insert that doesn't require parent to be imported, useful when we don't have the full blockchain (like restoring from partial snapshot).

Definition at line 733 of file BlockChain.cpp.

◆ isKnown()

bool BlockChain::isKnown ( h256 const &  _hash,
bool  _isCurrent = true 
) const

Returns true if the given block is known (though not necessarily a part of the canon chain).

Definition at line 1404 of file BlockChain.cpp.

◆ isKnownTransaction()

bool dev::eth::BlockChain::isKnownTransaction ( h256 const &  _transactionHash) const
inline

Returns true if transaction is known. Thread-safe.

Definition at line 209 of file BlockChain.h.

◆ lastBlockHashes()

LastBlockHashesFace const& dev::eth::BlockChain::lastBlockHashes ( ) const
inline

Definition at line 186 of file BlockChain.h.

◆ logBlooms() [1/2]

BlockLogBlooms dev::eth::BlockChain::logBlooms ( ) const
inline

Definition at line 162 of file BlockChain.h.

◆ logBlooms() [2/2]

BlockLogBlooms dev::eth::BlockChain::logBlooms ( h256 const &  _hash) const
inline

Get the transactions' log blooms of a block (or the most recent mined if none given). Thread-safe.

Definition at line 161 of file BlockChain.h.

◆ number() [1/2]

unsigned dev::eth::BlockChain::number ( ) const
inline

Definition at line 225 of file BlockChain.h.

◆ number() [2/2]

unsigned dev::eth::BlockChain::number ( h256 const &  _hash) const
inline

Get a number for the given hash (or the most recent mined if none given). Thread-safe.

Definition at line 224 of file BlockChain.h.

◆ numberHash()

h256 dev::eth::BlockChain::numberHash ( unsigned  _i) const
inline

Get the hash for a given block's number.

Definition at line 184 of file BlockChain.h.

◆ process()

void dev::eth::BlockChain::process ( )

(Potentially) renders invalid existing bytesConstRef returned by lastBlock. To be called from main loop every 100ms or so.

◆ rebuild()

void BlockChain::rebuild ( boost::filesystem::path const &  _path,
ProgressCallback const &  _progress = std::function<void(unsigned, unsigned)>() 
)

Run through database and verify all blocks by reevaluating. Will call _progress with the progress in this operation first param done, second total.

Definition at line 341 of file BlockChain.cpp.

◆ receipts() [1/2]

BlockReceipts dev::eth::BlockChain::receipts ( ) const
inline

Definition at line 167 of file BlockChain.h.

◆ receipts() [2/2]

BlockReceipts dev::eth::BlockChain::receipts ( h256 const &  _hash) const
inline

Get the transactions' receipts of a block (or the most recent mined if none given). Thread-safe. receipts are given in the same order are in the same order as the transactions

Definition at line 166 of file BlockChain.h.

◆ reopen() [1/2]

void BlockChain::reopen ( ChainParams const &  _p,
WithExisting  _we = WithExisting::Trust,
ProgressCallback const &  _pc = ProgressCallback() 
)

Definition at line 311 of file BlockChain.cpp.

◆ reopen() [2/2]

void dev::eth::BlockChain::reopen ( WithExisting  _we = WithExisting::Trust,
ProgressCallback const &  _pc = ProgressCallback() 
)
inline

Reopen everything.

Definition at line 113 of file BlockChain.h.

◆ rescue()

void BlockChain::rescue ( OverlayDB const &  _db)

Rescue the database.

Definition at line 1041 of file BlockChain.cpp.

◆ rewind()

void BlockChain::rewind ( unsigned  _newHead)

Alter the head of the chain to some prior block along it.

Definition at line 1091 of file BlockChain.cpp.

◆ sealEngine()

SealEngineFace* dev::eth::BlockChain::sealEngine ( ) const
inline

Definition at line 308 of file BlockChain.h.

◆ setChainStartBlockNumber()

void BlockChain::setChainStartBlockNumber ( unsigned  _number)

Change the chain start block.

Definition at line 1587 of file BlockChain.cpp.

◆ setOnBad()

void dev::eth::BlockChain::setOnBad ( std::function< void(Exception &)>  _t)
inline

Change the function that is called with a bad block.

Definition at line 292 of file BlockChain.h.

◆ setOnBlockImport()

void dev::eth::BlockChain::setOnBlockImport ( std::function< void(BlockHeader const &)>  _t)
inline

Change the function that is called when a new block is imported.

Definition at line 295 of file BlockChain.h.

◆ sync()

tuple< ImportRoute, bool, unsigned > BlockChain::sync ( BlockQueue _bq,
OverlayDB const &  _stateDB,
unsigned  _max 
)

Sync the chain with any incoming blocks. All blocks should, if processed in order.

Returns
fresh blocks, dead blocks and true iff there are additional blocks to be processed waiting.

Definition at line 443 of file BlockChain.cpp.

◆ transaction() [1/3]

bytes dev::eth::BlockChain::transaction ( h256 const &  _blockHash,
unsigned  _i 
) const
inline

Get a block's transaction (RLP format) for the given block hash (or the most recent mined if none given) & index. Thread-safe.

Definition at line 216 of file BlockChain.h.

◆ transaction() [2/3]

bytes dev::eth::BlockChain::transaction ( h256 const &  _transactionHash) const
inline

Get a transaction from its hash. Thread-safe.

Definition at line 212 of file BlockChain.h.

◆ transaction() [3/3]

bytes dev::eth::BlockChain::transaction ( unsigned  _i) const
inline

Definition at line 217 of file BlockChain.h.

◆ transactionHashes() [1/2]

TransactionHashes dev::eth::BlockChain::transactionHashes ( ) const
inline

Definition at line 177 of file BlockChain.h.

◆ transactionHashes() [2/2]

TransactionHashes dev::eth::BlockChain::transactionHashes ( h256 const &  _hash) const
inline

Get a list of transaction hashes for a given block. Thread-safe.

Definition at line 176 of file BlockChain.h.

◆ transactionLocation()

std::pair<h256, unsigned> dev::eth::BlockChain::transactionLocation ( h256 const &  _transactionHash) const
inline

Definition at line 213 of file BlockChain.h.

◆ transactionReceipt() [1/2]

TransactionReceipt dev::eth::BlockChain::transactionReceipt ( h256 const &  _blockHash,
unsigned  _i 
) const
inline

Get the transaction by block hash and index;.

Definition at line 170 of file BlockChain.h.

◆ transactionReceipt() [2/2]

TransactionReceipt dev::eth::BlockChain::transactionReceipt ( h256 const &  _transactionHash) const
inline

Get the transaction receipt by transaction hash. Thread-safe.

Definition at line 173 of file BlockChain.h.

◆ transactions() [1/2]

std::vector<bytes> dev::eth::BlockChain::transactions ( ) const
inline

Definition at line 221 of file BlockChain.h.

◆ transactions() [2/2]

std::vector<bytes> dev::eth::BlockChain::transactions ( h256 const &  _blockHash) const
inline

Get all transactions from a block.

Definition at line 220 of file BlockChain.h.

◆ treeRoute()

tuple< h256s, h256, unsigned > BlockChain::treeRoute ( h256 const &  _from,
h256 const &  _to,
bool  _common = true,
bool  _pre = true,
bool  _post = true 
) const
Returns
a tuple of:
  • an vector of hashes of all blocks between _from and _to, all blocks are ordered first by a number of blocks that are parent-to-child, then two sibling blocks, then a number of blocks that are child-to-parent;
  • the block hash of the latest common ancestor of both blocks;
  • the index where the latest common ancestor of both blocks would either be found or inserted, depending on whether it is included.
Parameters
_commonif true, include the common ancestor in the returned vector.
_preif true, include all block hashes running from _from until the common ancestor in the returned vector.
_postif true, include all block hashes running from the common ancestor until _to in the returned vector.

e.g. if the block tree is 3a -> 2a -> 1a -> g and 2b -> 1b -> g (g is genesis, *a, *b are competing chains), then:

treeRoute(3a, 2b, false) == make_tuple({ 3a, 2a, 1a, 1b, 2b }, g, 3);
treeRoute(2a, 1a, false) == make_tuple({ 2a, 1a }, 1a, 1)
treeRoute(1a, 2a, false) == make_tuple({ 1a, 2a }, 1a, 0)
treeRoute(1b, 2a, false) == make_tuple({ 1b, 1a, 2a }, g, 1)
treeRoute(3a, 2b, true) == make_tuple({ 3a, 2a, 1a, g, 1b, 2b }, g, 3);
treeRoute(2a, 1a, true) == make_tuple({ 2a, 1a }, 1a, 1)
treeRoute(1a, 2a, true) == make_tuple({ 1a, 2a }, 1a, 0)
treeRoute(1b, 2a, true) == make_tuple({ 1b, g, 1a, 2a }, g, 1)

Definition at line 1116 of file BlockChain.cpp.

◆ uncleHashes() [1/2]

UncleHashes dev::eth::BlockChain::uncleHashes ( ) const
inline

Definition at line 181 of file BlockChain.h.

◆ uncleHashes() [2/2]

UncleHashes dev::eth::BlockChain::uncleHashes ( h256 const &  _hash) const
inline

Get a list of uncle hashes for a given block. Thread-safe.

Definition at line 180 of file BlockChain.h.

◆ usage()

Statistics dev::eth::BlockChain::usage ( bool  _freshen = false) const
inline
Returns
statistics about memory usage.

Definition at line 286 of file BlockChain.h.

◆ verifyBlock()

VerifiedBlockRef BlockChain::verifyBlock ( bytesConstRef  _block,
std::function< void(Exception &)> const &  _onBad,
ImportRequirements::value  _ir = ImportRequirements::OutOfOrderChecks 
) const

Verify block and prepare it for enactment.

Definition at line 1501 of file BlockChain.cpp.

◆ withBlockBloom() [1/2]

vector< unsigned > BlockChain::withBlockBloom ( LogBloom const &  _b,
unsigned  _earliest,
unsigned  _latest 
) const

Definition at line 1334 of file BlockChain.cpp.

◆ withBlockBloom() [2/2]

vector< unsigned > BlockChain::withBlockBloom ( LogBloom const &  _b,
unsigned  _earliest,
unsigned  _latest,
unsigned  _topLevel,
unsigned  _index 
) const

Definition at line 1348 of file BlockChain.cpp.

Friends And Related Function Documentation

◆ operator<<

std::ostream& operator<< ( std::ostream &  _out,
BlockChain const &  _bc 
)
friend

The documentation for this class was generated from the following files:
dev::eth::BlockChain::treeRoute
std::tuple< h256s, h256, unsigned > treeRoute(h256 const &_from, h256 const &_to, bool _common=true, bool _pre=true, bool _post=true) const
Definition: BlockChain.cpp:1116