26 #include <unordered_map> 27 #include <unordered_set> 46 template <>
struct hash<pair<
dev::
h256, unsigned>>
60 static const h256s NullH256s;
75 std::unordered_map<Address, Account>
const&
genesisState();
77 ldb::Slice
toSlice(
h256 const& _h,
unsigned _sub = 0);
78 ldb::Slice
toSlice(uint64_t _n,
unsigned _sub = 0);
123 std::tuple<ImportRoute, bool, unsigned> sync(
BlockQueue& _bq,
OverlayDB const& _stateDB,
unsigned _max);
127 std::pair<ImportResult, ImportRoute> attemptImport(
bytes const& _block,
OverlayDB const& _stateDB,
bool _mutBeNew =
true) noexcept;
141 bool isKnown(
h256 const& _hash,
bool _isCurrent =
true)
const;
152 bytes headerData(
h256 const& _hash)
const;
156 BlockDetails details(
h256 const& _hash)
const {
return queryExtras<BlockDetails, ExtraDetails>(_hash, m_details, x_details, NullBlockDetails); }
160 BlockLogBlooms logBlooms(
h256 const& _hash)
const {
return queryExtras<BlockLogBlooms, ExtraLogBlooms>(_hash, m_logBlooms, x_logBlooms, NullBlockLogBlooms); }
165 BlockReceipts receipts(
h256 const& _hash)
const {
return queryExtras<BlockReceipts, ExtraReceipts>(_hash, m_receipts, x_receipts, NullBlockReceipts); }
183 h256 numberHash(
unsigned _i)
const {
if (!_i)
return genesisHash();
return queryExtras<BlockHash, uint64_t, ExtraBlockHash>(_i, m_blockHashes, x_blockHashes, NullBlockHash).value; }
204 BlocksBlooms blocksBlooms(
h256 const& _chunkId)
const {
return queryExtras<BlocksBlooms, ExtraBlocksBlooms>(_chunkId, m_blocksBlooms, x_blocksBlooms, NullBlocksBlooms); }
205 LogBloom blockBloom(
unsigned _number)
const {
return blocksBlooms(chunkId(0, _number / c_bloomIndexSize)).blooms[_number % c_bloomIndexSize]; }
206 std::vector<unsigned> withBlockBloom(
LogBloom const& _b,
unsigned _earliest,
unsigned _latest)
const;
207 std::vector<unsigned> withBlockBloom(
LogBloom const& _b,
unsigned _earliest,
unsigned _latest,
unsigned _topLevel,
unsigned _index)
const;
210 bool isKnownTransaction(
h256 const& _transactionHash)
const {
TransactionAddress ta = queryExtras<TransactionAddress, ExtraTransactionAddress>(_transactionHash, m_transactionAddresses, x_transactionAddresses, NullTransactionAddress);
return !!ta; }
214 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); }
221 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; }
225 unsigned number(
h256 const& _hash)
const {
return details(_hash).number; }
226 unsigned number()
const {
return m_lastBlockNumber; }
237 h256Hash allKinFrom(
h256 const& _parent,
unsigned _generations)
const;
244 void rewind(
unsigned _newHead);
273 std::tuple<h256s, h256, unsigned> treeRoute(
h256 const& _from,
h256 const& _to,
bool _common =
true,
bool _pre =
true,
bool _post =
true)
const;
283 unsigned memTotal()
const {
return memBlocks + memDetails + memLogBlooms + memReceipts + memTransactionAddresses + memBlockHashes; }
287 Statistics usage(
bool _freshen =
false)
const {
if (_freshen) updateStats();
return m_lastStats; }
290 void garbageCollect(
bool _force =
false);
305 std::string dumpDatabase()
const;
314 static h256 chunkId(
unsigned _level,
unsigned _index) {
return h256(_index * 0xff + _level); }
319 unsigned open(std::string
const& _path,
WithExisting _we);
325 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 329 auto it = _m.find(_h);
335 (_extrasDB ? _extrasDB : m_extrasDB)->Get(m_readOptions,
toSlice(_h, N), &s);
342 auto ret = _m.insert(std::make_pair(_h,
T(
RLP(s))));
343 return ret.first->second;
346 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 348 return queryExtras<T, h256, N>(_h, _m, _x, _n, _extrasDB);
351 void checkConsistency();
355 void clearCachesDuringChainReversion(
unsigned _firstInvalid);
356 void clearBlockBlooms(
unsigned _begin,
unsigned _end);
378 void noteUsed(
h256 const& _h,
unsigned _extra = (
unsigned)-1)
const;
379 void noteUsed(uint64_t
const& _h,
unsigned _extra = (
unsigned)-1)
const { (void)_h; (void)_extra; }
386 void updateStats()
const;
396 unsigned m_lastBlockNumber = 0;
UncleHashes uncleHashes() const
std::unordered_map< h256, BlocksBlooms > BlocksBloomsHash
std::pair< h256, unsigned > CacheID
Statistics usage(bool _freshen=false) const
DEV_SIMPLE_EXCEPTION(ChainParamsNotEthash)
Adapted from code found on http://stackoverflow.com/questions/180947/base64-decode-snippet-in-c Origi...
static h256 chunkId(unsigned _level, unsigned _index)
#define function(a, b, c, d, k, s)
T queryExtras(h256 const &_h, std::unordered_map< h256, T > &_m, boost::shared_mutex &_x, T const &_n, ldb::DB *_extrasDB=nullptr) const
ChainParams const & chainParams() const
std::vector< bytes > transactions() const
BlockDetails details(h256 const &_hash) const
Get the familial details concerning a block (or the most recent mined if none given). Thread-safe.
size_t operator()(pair< dev::h256, unsigned > const &_x) const
BlockHeader info(h256 const &_hash) const
Get the partial-header of a block (or the most recent mined if none given). Thread-safe.
bool isKnownTransaction(h256 const &_transactionHash) const
Returns true if transaction is known. Thread-safe.
std::chrono::system_clock::time_point m_lastCollection
std::unordered_set< CacheID > m_inUse
Implements the blockchain database.
BlocksBlooms blocksBlooms(h256 const &_chunkId) const
bytes rlp(_T _t)
Export a single item in RLP format, returning a byte array.
bytesConstRef data() const
The bare data of the RLP.
ldb::ReadOptions m_readOptions
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...
std::unordered_map< h256, BlockDetails > BlockDetailsHash
SharedMutex x_blocks
The caches of the disk DB and their locks.
BlockReceipts receipts(h256 const &_hash) const
Get the transactions' receipts of a block (or the most recent mined if none given).
std::unordered_map< Address, Account > const & genesisState()
T queryExtras(K const &_h, std::unordered_map< K, T > &_m, boost::shared_mutex &_x, T const &_n, ldb::DB *_extrasDB=nullptr) const
SharedMutex x_blockHashes
LastHashes lastHashes() const
Get the last N hashes for a given block. (N is determined by the LastHashes type.) ...
void noteUsed(uint64_t const &_h, unsigned _extra=(unsigned)-1) const
std::hash for asio::adress
ldb::WriteOptions m_writeOptions
LastHashes m_lastLastHashes
bytes transaction(unsigned _i) const
BlockLogBloomsHash m_logBlooms
unsigned memTransactionAddresses
std::deque< std::unordered_set< CacheID > > m_cacheUsage
BlocksBlooms blocksBlooms(unsigned _level, unsigned _index) const
Get the block blooms for a number of blocks.
void setOnBlockImport(std::function< void(BlockHeader const &)> _t)
Change the function that is called when a new block is imported.
std::shared_ptr< SealEngineFace > m_sealEngine
BlockLogBlooms logBlooms() const
std::vector< bytes > transactions(h256 const &_blockHash) const
Get all transactions from a block.
Active model of a block within the block chain.
void noteCanonChanged() const
boost::shared_mutex x_lastBlockHash
Hash of the last (valid) block on the longest chain.
std::vector< h256 > LastHashes
Base class for all exceptions.
std::function< void(Exception &)> m_onBad
Called if we have a block that doesn't verify.
std::lock_guard< std::mutex > Guard
SharedMutex x_blocksBlooms
unsigned number(h256 const &_hash) const
Get a number for the given hash (or the most recent mined if none given). Thread-safe.
h256 numberHash(unsigned _i) const
Get the hash for a given block's number.
h256 currentHash() const
Get a given block (RLP format). Thread-safe.
std::vector< byte > bytes
std::pair< h256, unsigned > transactionLocation(h256 const &_transactionHash) const
std::vector< unsigned char > toBytes() const
vector_ref< byte const > bytesConstRef
BlocksBloomsHash m_blocksBlooms
std::function< void(BlockHeader const &)> m_onBlockImport
Called if we have imported a new block into the db.
std::unordered_map< uint64_t, BlockHash > BlockHashHash
boost::shared_lock< boost::shared_mutex > ReadGuard
BlockReceipts receipts() const
TransactionReceipt transactionReceipt(h256 const &_blockHash, unsigned _i) const
Get the transaction by block hash and index;.
bytes m_genesisHeaderBytes
boost::unique_lock< boost::shared_mutex > WriteGuard
BlockHashHash m_blockHashes
void reopen(WithExisting _we=WithExisting::Trust, ProgressCallback const &_pc=ProgressCallback())
Reopen everything.
bytes transaction(h256 const &_transactionHash) const
Get a transaction from its hash. Thread-safe.
std::unordered_map< h256, BlockLogBlooms > BlockLogBloomsHash
SharedMutex x_transactionAddresses
ldb::Slice toSlice(uint64_t _n, unsigned _sub=0)
std::unordered_map< h256, TransactionAddress > TransactionAddressHash
Verified block info, does not hold block data, but a reference instead.
TransactionHashes transactionHashes(h256 const &_hash) const
Get a list of transaction hashes for a given block. Thread-safe.
LastHashes lastHashes(u256 _currentBlockNumber)
BlockReceiptsHash m_receipts
void setOnBad(std::function< void(Exception &)> _t)
Change the function that is called with a bad block.
TransactionReceipt transactionReceipt(h256 const &_transactionHash) const
Get the transaction receipt by transaction hash. Thread-safe.
TransactionAddressHash m_transactionAddresses
boost::shared_mutex SharedMutex
ldb::DB * m_blocksDB
The disk DBs. Thread-safe, so no need for locks.
clock::time_point time_point
bool sha3(bytesConstRef _input, bytesRef o_output)
Calculate SHA3-256 hash of the given input and load it into the given output.
LogBloom blockBloom(unsigned _number) const
std::unordered_map< h256, bytes > BlocksHash
std::unordered_map< h256, BlockReceipts > BlockReceiptsHash
std::ostream & operator<<(std::ostream &_out, BlockChain const &_bc)
h256 genesisHash() const
Get the hash of the genesis block. Thread-safe.
The default logging channels.
std::unordered_set< h256 > h256Hash
TransactionHashes transactionHashes() const
std::vector< h256 > h256s
UncleHashes uncleHashes(h256 const &_hash) const
Get a list of uncle hashes for a given block. Thread-safe.
Class for interpreting Recursive Linear-Prefix Data.
BlockDetails details() const
std::function< void(unsigned, unsigned)> ProgressCallback
unsigned memTotal() const
SealEngineFace * sealEngine() const
BlockDetailsHash m_details
BlockLogBlooms logBlooms(h256 const &_hash) const
Get the transactions' log blooms of a block (or the most recent mined if none given). Thread-safe.