 |
Ethereum
PoC-8
The C++ Implementation of Ethereum
|
Go to the documentation of this file.
41 #include <unordered_map>
42 #include <unordered_set>
43 #include <boost/filesystem/path.hpp>
47 template <>
struct hash<pair<
dev::
h256, unsigned>>
61 static const h256s NullH256s;
65 class ImportPerformanceLogger;
126 std::pair<ImportResult, ImportRoute>
attemptImport(
bytes const& _block,
OverlayDB const& _stateDB,
bool _mutBeNew =
true) noexcept;
142 bool isKnown(
h256 const& _hash,
bool _isCurrent = true) const;
157 BlockDetails details(
h256 const& _hash)
const {
return queryExtras<BlockDetails, ExtraDetails>(_hash, m_details, x_details, NullBlockDetails); }
161 BlockLogBlooms logBlooms(
h256 const& _hash)
const {
return queryExtras<BlockLogBlooms, ExtraLogBlooms>(_hash, m_logBlooms, x_logBlooms, NullBlockLogBlooms); }
166 BlockReceipts receipts(
h256 const& _hash)
const {
return queryExtras<BlockReceipts, ExtraReceipts>(_hash, m_receipts, x_receipts, NullBlockReceipts); }
184 h256 numberHash(
unsigned _i)
const {
if (!_i)
return genesisHash();
return queryExtras<BlockHash, uint64_t, ExtraBlockHash>(_i, m_blockHashes, x_blockHashes, NullBlockHash).value; }
203 BlocksBlooms blocksBlooms(
h256 const& _chunkId)
const {
return queryExtras<BlocksBlooms, ExtraBlocksBlooms>(_chunkId, m_blocksBlooms, x_blocksBlooms, NullBlocksBlooms); }
206 std::vector<unsigned>
withBlockBloom(
LogBloom const& _b,
unsigned _earliest,
unsigned _latest,
unsigned _topLevel,
unsigned _index)
const;
209 bool isKnownTransaction(
h256 const& _transactionHash)
const {
TransactionAddress ta = queryExtras<TransactionAddress, ExtraTransactionAddress>(_transactionHash, m_transactionAddresses, x_transactionAddresses, NullTransactionAddress);
return !!ta; }
213 std::pair<h256, unsigned>
transactionLocation(
h256 const& _transactionHash)
const {
TransactionAddress ta = queryExtras<TransactionAddress, ExtraTransactionAddress>(_transactionHash, m_transactionAddresses, x_transactionAddresses, NullTransactionAddress);
if (!ta)
return std::pair<h256, unsigned>(
h256(), 0);
return std::make_pair(ta.
blockHash, ta.
index); }
220 std::vector<bytes>
transactions(
h256 const& _blockHash)
const {
bytes b =
block(_blockHash); std::vector<bytes> ret;
for (
auto const& i:
RLP(b)[1]) ret.push_back(i.data().toBytes());
return ret; }
240 void rebuild(boost::filesystem::path
const& _path,
ProgressCallback const& _progress = std::function<
void(
unsigned,
unsigned)>());
243 void rewind(
unsigned _newHead);
272 std::tuple<h256s, h256, unsigned>
treeRoute(
h256 const& _from,
h256 const& _to,
bool _common =
true,
bool _pre =
true,
bool _post =
true)
const;
286 Statistics usage(
bool _freshen =
false)
const {
if (_freshen) updateStats();
return m_lastStats; }
318 static h256 chunkId(
unsigned _level,
unsigned _index) {
return h256(_index * 0xff + _level); }
321 void init(ChainParams
const& _p);
323 unsigned open(boost::filesystem::path
const& _path,
WithExisting _we);
329 ImportRoute insertBlockAndExtras(VerifiedBlockRef
const& _block,
bytesConstRef _receipts,
u256 const& _totalDifficulty, ImportPerformanceLogger& _performanceLogger);
330 void checkBlockIsNew(VerifiedBlockRef
const& _block)
const;
331 void checkBlockTimestamp(BlockHeader
const& _header)
const;
333 template <
class T,
class K,
unsigned N>
334 T queryExtras(K
const& _h, std::unordered_map<K, T>& _m, boost::shared_mutex& _x, T
const& _n,
339 auto it = _m.find(_h);
344 std::string
const s = (_extrasDB ? _extrasDB : m_extrasDB.get())->lookup(
toSlice(_h, N));
351 auto ret = _m.insert(std::make_pair(_h, T(RLP(s))));
352 return ret.first->second;
355 template <
class T,
unsigned N>
356 T queryExtras(
h256 const& _h, std::unordered_map<h256, T>& _m, boost::shared_mutex& _x,
357 T
const& _n, db::DatabaseFace* _extrasDB =
nullptr)
const
359 return queryExtras<T, h256, N>(_h, _m, _x, _n, _extrasDB);
362 void checkConsistency();
366 void clearCachesDuringChainReversion(
unsigned _firstInvalid);
367 void clearBlockBlooms(
unsigned _begin,
unsigned _end);
385 using CacheID = std::pair<h256, unsigned>;
386 mutable Mutex x_cacheUsage;
387 mutable std::deque<std::unordered_set<CacheID>> m_cacheUsage;
388 mutable std::unordered_set<CacheID> m_inUse;
389 void noteUsed(
h256 const& _h,
unsigned _extra = (
unsigned)-1)
const;
390 void noteUsed(uint64_t
const& _h,
unsigned _extra = (
unsigned)-1)
const { (void)_h; (void)_extra; }
391 std::chrono::system_clock::time_point m_lastCollection;
393 void noteCanonChanged()
const { m_lastBlockHashes->clear(); }
394 std::unique_ptr<LastBlockHashesFace> m_lastBlockHashes;
396 void updateStats()
const;
397 mutable Statistics m_lastStats;
400 std::unique_ptr<db::DatabaseFace> m_blocksDB;
401 std::unique_ptr<db::DatabaseFace> m_extrasDB;
404 mutable boost::shared_mutex x_lastBlockHash;
405 h256 m_lastBlockHash;
406 unsigned m_lastBlockNumber = 0;
408 ChainParams m_params;
409 std::shared_ptr<SealEngineFace> m_sealEngine;
411 mutable BlockHeader m_genesis;
412 mutable bytes m_genesisHeaderBytes;
413 mutable h256 m_genesisHash;
415 std::function<void(Exception&)> m_onBad;
416 std::function<void(BlockHeader
const&)> m_onBlockImport;
418 boost::filesystem::path m_dbPath;
h256 currentHash() const
Get a given block (RLP format). Thread-safe.
A queue of blocks. Sits between network or other I/O and the BlockChain. Sorts them ready for blockch...
boost::shared_mutex SharedMutex
void insert(bytes const &_block, bytesConstRef _receipts, bool _mustBeNew=true)
bool isKnownTransaction(h256 const &_transactionHash) const
Returns true if transaction is known. Thread-safe.
void rescue(OverlayDB const &_db)
Rescue the database.
std::vector< unsigned > withBlockBloom(LogBloom const &_b, unsigned _earliest, unsigned _latest) const
std::array< LogBloom, c_bloomIndexSize > blooms
void rebuild(boost::filesystem::path const &_path, ProgressCallback const &_progress=std::function< void(unsigned, unsigned)>())
std::tuple< h256s, h256, unsigned > treeRoute(h256 const &_from, h256 const &_to, bool _common=true, bool _pre=true, bool _post=true) const
std::pair< ImportResult, ImportRoute > attemptImport(bytes const &_block, OverlayDB const &_stateDB, bool _mutBeNew=true) noexcept
unsigned memTotal() const
UncleHashes uncleHashes() const
bool sha3(bytesConstRef _input, bytesRef o_output) noexcept
void reopen(WithExisting _we=WithExisting::Trust, ProgressCallback const &_pc=ProgressCallback())
Reopen everything.
h256 genesisHash() const
Get the hash of the genesis block. Thread-safe.
size_t operator()(pair< dev::h256, unsigned > const &_x) const
LogBloom blockBloom(unsigned _number) const
std::unordered_map< Address, Account > const & genesisState()
TransactionHashes transactionHashes(h256 const &_hash) const
Get a list of transaction hashes for a given block. Thread-safe.
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).
BlockLogBlooms logBlooms(h256 const &_hash) const
Get the transactions' log blooms of a block (or the most recent mined if none given)....
std::tuple< ImportRoute, bool, unsigned > sync(BlockQueue &_bq, OverlayDB const &_stateDB, unsigned _max)
unsigned number(h256 const &_hash) const
Get a number for the given hash (or the most recent mined if none given). Thread-safe.
bytes transaction(unsigned _i) const
std::unordered_map< uint64_t, BlockHash > BlockHashHash
Statistics usage(bool _freshen=false) const
SealEngineFace * sealEngine() const
bytes rlp(_T _t)
Export a single item in RLP format, returning a byte array.
std::function< void(unsigned, unsigned)> ProgressCallback
@ ExtraTransactionAddress
Base class for all exceptions.
boost::unique_lock< boost::shared_mutex > WriteGuard
BlockDetails details(h256 const &_hash) const
Get the familial details concerning a block (or the most recent mined if none given)....
ImportRoute import(bytes const &_block, OverlayDB const &_stateDB, bool _mustBeNew=true)
BlockLogBlooms logBlooms() const
unsigned chainStartBlockNumber() const
ChainParams const & chainParams() const
UncleHashes uncleHashes(h256 const &_hash) const
Get a list of uncle hashes for a given block. Thread-safe.
std::unordered_map< h256, BlockLogBlooms > BlockLogBloomsHash
std::unordered_set< h256 > h256Hash
std::vector< h256 > h256s
boost::log::sources::severity_channel_logger<> Logger
friend std::ostream & operator<<(std::ostream &_out, BlockChain const &_bc)
@ OutOfOrderChecks
Do all checks that can be done independently of prior blocks having been imported.
BlockDetails details() const
bytes transaction(h256 const &_transactionHash) const
Get a transaction from its hash. Thread-safe.
BlocksBlooms blocksBlooms(h256 const &_chunkId) const
h256Hash allKinFrom(h256 const &_parent, unsigned _generations) const
BlockReceipts receipts(h256 const &_hash) const
h256 numberHash(unsigned _i) const
Get the hash for a given block's number.
std::unordered_map< h256, BlocksBlooms > BlocksBloomsHash
std::vector< byte > bytes
Logger createLogger(int _severity, std::string const &_channel)
VerifiedBlockRef verifyBlock(bytesConstRef _block, std::function< void(Exception &)> const &_onBad, ImportRequirements::value _ir=ImportRequirements::OutOfOrderChecks) const
Verify block and prepare it for enactment.
TransactionReceipt transactionReceipt(h256 const &_blockHash, unsigned _i) const
Get the transaction by block hash and index;.
vector_ref< byte const > bytesConstRef
boost::shared_lock< boost::shared_mutex > ReadGuard
Implements the blockchain database. All data this gives is disk-backed. @threadsafe.
LastBlockHashesFace const & lastBlockHashes() const
void setOnBad(std::function< void(Exception &)> _t)
Change the function that is called with a bad block.
TransactionReceipts receipts
std::unordered_map< h256, BlockDetails > BlockDetailsHash
bytesConstRef data() const
The bare data of the RLP.
std::pair< h256, unsigned > transactionLocation(h256 const &_transactionHash) const
void setOnBlockImport(std::function< void(BlockHeader const &)> _t)
Change the function that is called when a new block is imported.
BlockHeader const & genesis() const
VersionChecker(boost::filesystem::path const &_dbPath, h256 const &_genesisHash)
std::unordered_map< h256, BlockReceipts > BlockReceiptsHash
std::vector< unsigned char > toBytes() const
std::unordered_map< h256, TransactionAddress > TransactionAddressHash
std::unordered_map< h256, bytes > BlocksHash
boost::multiprecision::number< boost::multiprecision::cpp_int_backend< 256, 256, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void > > u256
std::ostream & operator<<(std::ostream &_out, BlockHeader const &_bi)
void garbageCollect(bool _force=false)
Deallocate unused data.
Verified block info, does not hold block data, but a reference instead.
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 (lik...
DEV_SIMPLE_EXCEPTION(NoHashRecorded)
TransactionReceipt transactionReceipt(h256 const &_transactionHash) const
Get the transaction receipt by transaction hash. Thread-safe.
BlockChain(ChainParams const &_p, boost::filesystem::path const &_path, WithExisting _we=WithExisting::Trust, ProgressCallback const &_pc=ProgressCallback())
BlocksBlooms blocksBlooms(unsigned _level, unsigned _index) const
std::string dumpDatabase() const
Gives a dump of the blockchain database. For debug/test use only.
TransactionHashes transactionHashes() const
void rewind(unsigned _newHead)
Alter the head of the chain to some prior block along it.
db::Slice toSlice(h256 const &_h, unsigned _sub=0)
Interface for getting a list of recent block hashes @threadsafe.
void setChainStartBlockNumber(unsigned _number)
Change the chain start block.
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 giv...
Active model of a block within the block chain. Keeps track of all transactions, receipts and state f...
unsigned memTransactionAddresses
Block genesisBlock(OverlayDB const &_db) const
Get a pre-made genesis State object.
BlockReceipts receipts() const
std::vector< bytes > transactions(h256 const &_blockHash) const
Get all transactions from a block.
std::vector< bytes > transactions() const