Fabcoin Core
0.16.2
P2P Digital Currency
|
Implements the blockchain database. More...
#include <BlockChain.h>
Classes | |
struct | Statistics |
Public Member Functions | |
BlockChain (ChainParams const &_p, std::string 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. More... | |
~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 () |
(Potentially) renders invalid existing bytesConstRef returned by lastBlock. More... | |
std::tuple< ImportRoute, bool, unsigned > | sync (BlockQueue &_bq, OverlayDB const &_stateDB, unsigned _max) |
Sync the chain with any incoming blocks. More... | |
std::pair< ImportResult, ImportRoute > | 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. More... | |
ImportRoute | import (bytes const &_block, OverlayDB const &_stateDB, bool _mustBeNew=true) |
Import block into disk-backed DB. More... | |
ImportRoute | import (VerifiedBlockRef const &_block, OverlayDB const &_db, bool _mustBeNew=true) |
void | insert (bytes const &_block, bytesConstRef _receipts, bool _mustBeNew=true) |
Import data into disk-backed DB. More... | |
void | insert (VerifiedBlockRef _block, bytesConstRef _receipts, bool _mustBeNew=true) |
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 |
Get the transactions' receipts of a block (or the most recent mined if none given). More... | |
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... | |
LastHashes | lastHashes () const |
Get the last N hashes for a given block. (N is determined by the LastHashes type.) More... | |
LastHashes | lastHashes (h256 const &_mostRecentHash) const |
BlocksBlooms | blocksBlooms (unsigned _level, unsigned _index) const |
Get the block blooms for a number of blocks. More... | |
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< bytes > | transactions (h256 const &_blockHash) const |
Get all transactions from a block. More... | |
std::vector< bytes > | transactions () 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 |
Get all blocks not allowed as uncles given a parent (i.e. More... | |
void | rebuild (std::string const &_path, ProgressCallback const &_progress=std::function< void(unsigned, unsigned)>()) |
Run through database and verify all blocks by reevaluating. More... | |
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 |
SealEngineFace * | sealEngine () const |
BlockHeader const & | genesis () const |
Private Types | |
using | CacheID = std::pair< h256, unsigned > |
Private Member Functions | |
void | init (ChainParams const &_p) |
Initialise everything and ready for openning the database. More... | |
unsigned | open (std::string const &_path, WithExisting _we) |
Open the database. More... | |
void | open (std::string const &_path, WithExisting _we, ProgressCallback const &_pc) |
Open the database, rebuilding if necessary. More... | |
void | close () |
Finalise everything and close the database. More... | |
template<class T , class K , unsigned N> | |
T | queryExtras (K const &_h, std::unordered_map< K, T > &_m, boost::shared_mutex &_x, T const &_n, ldb::DB *_extrasDB=nullptr) const |
template<class T , unsigned N> | |
T | queryExtras (h256 const &_h, std::unordered_map< h256, T > &_m, boost::shared_mutex &_x, T const &_n, ldb::DB *_extrasDB=nullptr) const |
void | checkConsistency () |
void | clearCachesDuringChainReversion (unsigned _firstInvalid) |
Clears all caches from the tip of the chain up to (including) _firstInvalid. More... | |
void | clearBlockBlooms (unsigned _begin, unsigned _end) |
void | noteUsed (h256 const &_h, unsigned _extra=(unsigned)-1) const |
void | noteUsed (uint64_t const &_h, unsigned _extra=(unsigned)-1) const |
void | noteCanonChanged () const |
void | updateStats () const |
Static Private Member Functions | |
static h256 | chunkId (unsigned _level, unsigned _index) |
Friends | |
std::ostream & | operator<< (std::ostream &_out, BlockChain const &_bc) |
Implements the blockchain database.
All data this gives is disk-backed.
Definition at line 105 of file BlockChain.h.
|
private |
Definition at line 374 of file BlockChain.h.
BlockChain::BlockChain | ( | ChainParams const & | _p, |
std::string 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 146 of file BlockChain.cpp.
BlockChain::~BlockChain | ( | ) |
Get all blocks not allowed as uncles given a parent (i.e.
featured as uncles/main in parent, parent + 1, ... parent + _generations).
Definition at line 1338 of file BlockChain.cpp.
|
noexcept |
Attempt to import the given block directly into the BlockChain and sync with the state DB.
Definition at line 472 of file BlockChain.cpp.
Get a block (RLP format) for the given hash (or the most recent mined if none given). Thread-safe.
Definition at line 1379 of file BlockChain.cpp.
|
inline |
Definition at line 149 of file BlockChain.h.
|
inline |
|
inline |
Get the block blooms for a number of blocks.
Thread-safe.
Definition at line 203 of file BlockChain.h.
|
inline |
Definition at line 204 of file BlockChain.h.
|
inline |
|
private |
Definition at line 1231 of file BlockChain.cpp.
|
inlinestaticprivate |
|
private |
Definition at line 980 of file BlockChain.cpp.
|
private |
Clears all caches from the tip of the chain up to (including) _firstInvalid.
These include the blooms, the block hashes and the transaction lookup tables.
Definition at line 1254 of file BlockChain.cpp.
|
private |
Finalise everything and close the database.
Definition at line 277 of file BlockChain.cpp.
|
inline |
Get a given block (RLP format). Thread-safe.
Definition at line 229 of file BlockChain.h.
|
inline |
Get the familial details concerning a block (or the most recent mined if none given). Thread-safe.
Definition at line 156 of file BlockChain.h.
|
inline |
Definition at line 157 of file BlockChain.h.
string BlockChain::dumpDatabase | ( | ) | const |
Gives a dump of the blockchain database. For debug/test use only.
Definition at line 384 of file BlockChain.cpp.
void BlockChain::garbageCollect | ( | bool | _force = false | ) |
Deallocate unused data.
Definition at line 1182 of file BlockChain.cpp.
BlockHeader const & BlockChain::genesis | ( | ) | const |
Definition at line 158 of file BlockChain.cpp.
Get a pre-made genesis State object.
Definition at line 1439 of file BlockChain.cpp.
|
inline |
Get the hash of the genesis block. Thread-safe.
Definition at line 232 of file BlockChain.h.
Get a block (RLP format) for the given hash (or the most recent mined if none given). Thread-safe.
Definition at line 1409 of file BlockChain.cpp.
|
inline |
Definition at line 153 of file BlockChain.h.
ImportRoute BlockChain::import | ( | bytes const & | _block, |
OverlayDB const & | _stateDB, | ||
bool | _mustBeNew = true |
||
) |
Import block into disk-backed DB.
Definition at line 498 of file BlockChain.cpp.
ImportRoute BlockChain::import | ( | VerifiedBlockRef const & | _block, |
OverlayDB const & | _db, | ||
bool | _mustBeNew = true |
||
) |
|
inline |
Get the partial-header of a block (or the most recent mined if none given). Thread-safe.
Definition at line 144 of file BlockChain.h.
|
inline |
Definition at line 145 of file BlockChain.h.
|
private |
Initialise everything and ready for openning the database.
Definition at line 172 of file BlockChain.cpp.
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 522 of file BlockChain.cpp.
void BlockChain::insert | ( | VerifiedBlockRef | _block, |
bytesConstRef | _receipts, | ||
bool | _mustBeNew = true |
||
) |
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 1354 of file BlockChain.cpp.
|
inline |
Returns true if transaction is known. Thread-safe.
Definition at line 210 of file BlockChain.h.
|
inline |
Get the last N hashes for a given block. (N is determined by the LastHashes type.)
Definition at line 186 of file BlockChain.h.
LastHashes BlockChain::lastHashes | ( | h256 const & | _mostRecentHash | ) | const |
|
inline |
Get the transactions' log blooms of a block (or the most recent mined if none given). Thread-safe.
Definition at line 160 of file BlockChain.h.
|
inline |
Definition at line 161 of file BlockChain.h.
|
inlineprivate |
|
private |
|
inlineprivate |
Definition at line 379 of file BlockChain.h.
|
inline |
Get a number for the given hash (or the most recent mined if none given). Thread-safe.
Definition at line 225 of file BlockChain.h.
|
inline |
|
inline |
Get the hash for a given block's number.
Definition at line 183 of file BlockChain.h.
|
private |
Open the database.
Definition at line 185 of file BlockChain.cpp.
|
private |
Open the database, rebuilding if necessary.
Definition at line 264 of file BlockChain.cpp.
void dev::eth::BlockChain::process | ( | ) |
(Potentially) renders invalid existing bytesConstRef returned by lastBlock.
To be called from main loop every 100ms or so.
|
inlineprivate |
Definition at line 346 of file BlockChain.h.
void BlockChain::rebuild | ( | std::string 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 297 of file BlockChain.cpp.
|
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 165 of file BlockChain.h.
|
inline |
Definition at line 166 of file BlockChain.h.
|
inline |
Reopen everything.
Definition at line 114 of file BlockChain.h.
void BlockChain::reopen | ( | ChainParams const & | _p, |
WithExisting | _we = WithExisting::Trust , |
||
ProgressCallback const & | _pc = ProgressCallback() |
||
) |
void BlockChain::rescue | ( | OverlayDB const & | _db | ) |
Rescue the database.
Definition at line 1022 of file BlockChain.cpp.
void BlockChain::rewind | ( | unsigned | _newHead | ) |
Alter the head of the chain to some prior block along it.
Definition at line 1072 of file BlockChain.cpp.
|
inline |
|
inline |
Change the function that is called with a bad block.
Definition at line 293 of file BlockChain.h.
|
inline |
Change the function that is called when a new block is imported.
Definition at line 296 of file BlockChain.h.
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.
Definition at line 408 of file BlockChain.cpp.
Get a transaction from its hash. Thread-safe.
Definition at line 213 of file BlockChain.h.
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 217 of file BlockChain.h.
|
inline |
Definition at line 218 of file BlockChain.h.
|
inline |
Get a list of transaction hashes for a given block. Thread-safe.
Definition at line 175 of file BlockChain.h.
|
inline |
Definition at line 176 of file BlockChain.h.
|
inline |
Definition at line 214 of file BlockChain.h.
|
inline |
Get the transaction by block hash and index;.
Definition at line 169 of file BlockChain.h.
|
inline |
Get the transaction receipt by transaction hash. Thread-safe.
Definition at line 172 of file BlockChain.h.
Get all transactions from a block.
Definition at line 221 of file BlockChain.h.
|
inline |
Definition at line 222 of file BlockChain.h.
tuple< h256s, h256, unsigned > BlockChain::treeRoute | ( | h256 const & | _from, |
h256 const & | _to, | ||
bool | _common = true , |
||
bool | _pre = true , |
||
bool | _post = true |
||
) | const |
_common | if true, include the common ancestor in the returned vector. |
_pre | if true, include all block hashes running from _from until the common ancestor in the returned vector. |
_post | if 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:
Definition at line 1093 of file BlockChain.cpp.
|
inline |
Get a list of uncle hashes for a given block. Thread-safe.
Definition at line 179 of file BlockChain.h.
|
inline |
Definition at line 180 of file BlockChain.h.
|
private |
|
inline |
Definition at line 287 of file BlockChain.h.
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 1461 of file BlockChain.cpp.
vector< unsigned > BlockChain::withBlockBloom | ( | LogBloom const & | _b, |
unsigned | _earliest, | ||
unsigned | _latest | ||
) | const |
vector< unsigned > BlockChain::withBlockBloom | ( | LogBloom const & | _b, |
unsigned | _earliest, | ||
unsigned | _latest, | ||
unsigned | _topLevel, | ||
unsigned | _index | ||
) | const |
|
friend |
|
mutableprivate |
Definition at line 370 of file BlockChain.h.
|
mutableprivate |
Definition at line 360 of file BlockChain.h.
|
mutableprivate |
Definition at line 372 of file BlockChain.h.
|
private |
The disk DBs. Thread-safe, so no need for locks.
Definition at line 390 of file BlockChain.h.
|
mutableprivate |
Definition at line 376 of file BlockChain.h.
|
private |
Definition at line 411 of file BlockChain.h.
|
mutableprivate |
Definition at line 362 of file BlockChain.h.
|
private |
Definition at line 391 of file BlockChain.h.
|
mutableprivate |
Definition at line 404 of file BlockChain.h.
|
mutableprivate |
Definition at line 406 of file BlockChain.h.
|
mutableprivate |
Definition at line 405 of file BlockChain.h.
|
mutableprivate |
Definition at line 377 of file BlockChain.h.
|
private |
Definition at line 395 of file BlockChain.h.
|
private |
Definition at line 396 of file BlockChain.h.
|
private |
Definition at line 380 of file BlockChain.h.
|
mutableprivate |
Definition at line 384 of file BlockChain.h.
|
mutableprivate |
Definition at line 387 of file BlockChain.h.
|
mutableprivate |
Definition at line 364 of file BlockChain.h.
|
private |
Called if we have a block that doesn't verify.
Definition at line 408 of file BlockChain.h.
|
private |
Called if we have imported a new block into the db.
Definition at line 409 of file BlockChain.h.
|
private |
Definition at line 401 of file BlockChain.h.
|
private |
Definition at line 398 of file BlockChain.h.
|
mutableprivate |
Definition at line 366 of file BlockChain.h.
|
private |
Definition at line 402 of file BlockChain.h.
|
mutableprivate |
Definition at line 368 of file BlockChain.h.
|
private |
Definition at line 399 of file BlockChain.h.
|
mutableprivate |
Definition at line 369 of file BlockChain.h.
|
mutableprivate |
The caches of the disk DB and their locks.
Definition at line 359 of file BlockChain.h.
|
mutableprivate |
Definition at line 371 of file BlockChain.h.
|
mutableprivate |
Definition at line 375 of file BlockChain.h.
|
mutableprivate |
Definition at line 361 of file BlockChain.h.
|
mutableprivate |
Definition at line 403 of file BlockChain.h.
|
mutableprivate |
Hash of the last (valid) block on the longest chain.
Definition at line 394 of file BlockChain.h.
|
mutableprivate |
Definition at line 383 of file BlockChain.h.
|
mutableprivate |
Definition at line 363 of file BlockChain.h.
|
mutableprivate |
Definition at line 365 of file BlockChain.h.
|
mutableprivate |
Definition at line 367 of file BlockChain.h.