diff --git a/libethereum/BlockChain.cpp b/libethereum/BlockChain.cpp index 5344f5147..21fa9f256 100644 --- a/libethereum/BlockChain.cpp +++ b/libethereum/BlockChain.cpp @@ -40,7 +40,7 @@ namespace eth std::ostream& operator<<(std::ostream& _out, BlockChain const& _bc) { - string cmp = toBigEndianString(_bc.m_lastBlockHash); + string cmp = toBigEndianString(_bc.currentHash()); auto it = _bc.m_extrasDB->NewIterator(_bc.m_readOptions); for (it->SeekToFirst(); it->Valid(); it->Next()) if (it->key().ToString() != "best") @@ -87,6 +87,21 @@ std::map const& eth::genesisState() } BlockInfo* BlockChain::s_genesis = nullptr; +boost::shared_mutex BlockChain::x_genesis; + +ldb::Slice eth::toSlice(h256 _h, unsigned _sub) +{ +#if ALL_COMPILERS_ARE_CPP11_COMPLIANT + static thread_local h256 h = _h ^ h256(u256(_sub)); + return ldb::Slice((char const*)&h, 32); +#else + static boost::thread_specific_ptr t_h; + if (!t_h.get()) + t_h.reset(new h256); + *t_h = _h ^ h256(u256(_sub)); + return ldb::Slice((char const*)t_h.get(), 32); +#endif +} bytes BlockChain::createGenesisBlock() { @@ -144,9 +159,10 @@ BlockChain::BlockChain(std::string _path, bool _killExisting) // TODO: Implement ability to rebuild details map from DB. std::string l; m_extrasDB->Get(m_readOptions, ldb::Slice("best"), &l); + m_lastBlockHash = l.empty() ? m_genesisHash : *(h256*)l.data(); - cnote << "Opened blockchain DB. Latest: " << m_lastBlockHash; + cnote << "Opened blockchain DB. Latest: " << currentHash(); } BlockChain::~BlockChain() @@ -181,20 +197,6 @@ h256s BlockChain::attemptImport(bytes const& _block, OverlayDB const& _stateDB) #endif } -inline ldb::Slice toSlice(h256 _h, unsigned _sub = 0) -{ -#if ALL_COMPILERS_ARE_CPP11_COMPLIANT - static thread_local h256 h = _h ^ h256(u256(_sub)); - return ldb::Slice((char const*)&h, 32); -#else - static boost::thread_specific_ptr t_h; - if (!t_h.get()) - t_h.reset(new h256); - *t_h = _h ^ h256(u256(_sub)); - return ldb::Slice((char const*)t_h.get(), 32); -#endif -} - h256s BlockChain::import(bytes const& _block, OverlayDB const& _db) { // VERIFY: populates from the block and checks the block is internally coherent. @@ -267,10 +269,16 @@ h256s BlockChain::import(bytes const& _block, OverlayDB const& _db) #endif // All ok - insert into DB { - lock_guard l(m_lock); + WriteGuard l(x_details); m_details[newHash] = BlockDetails((uint)pd.number + 1, td, bi.parentHash, {}, b); m_details[bi.parentHash].children.push_back(newHash); + } + { + WriteGuard l(x_blooms); m_blooms[newHash] = bb; + } + { + WriteGuard l(x_traces); m_traces[newHash] = bt; } @@ -296,10 +304,14 @@ h256s BlockChain::import(bytes const& _block, OverlayDB const& _db) h256s ret; // This might be the new best block... - if (td > details(m_lastBlockHash).totalDifficulty) + h256 last = currentHash(); + if (td > details(last).totalDifficulty) { - ret = treeRoute(m_lastBlockHash, newHash); - m_lastBlockHash = newHash; + ret = treeRoute(last, newHash); + { + WriteGuard l(x_lastBlockHash); + m_lastBlockHash = newHash; + } m_extrasDB->Put(m_writeOptions, ldb::Slice("best"), ldb::Slice((char const*)&newHash, 32)); clog(BlockChainNote) << " Imported and best. Has" << (details(bi.parentHash).children.size() - 1) << "siblings. Route:"; for (auto r: ret) @@ -307,7 +319,7 @@ h256s BlockChain::import(bytes const& _block, OverlayDB const& _db) } else { - clog(BlockChainNote) << " Imported but not best (oTD:" << details(m_lastBlockHash).totalDifficulty << ", TD:" << td << ")"; + clog(BlockChainNote) << " Imported but not best (oTD:" << details(last).totalDifficulty << ", TD:" << td << ")"; } return ret; } @@ -347,7 +359,6 @@ h256s BlockChain::treeRoute(h256 _from, h256 _to, h256* o_common) const void BlockChain::checkConsistency() { - lock_guard l(m_lock); m_details.clear(); ldb::Iterator* it = m_db->NewIterator(m_readOptions); for (it->SeekToFirst(); it->Valid(); it->Next()) @@ -371,15 +382,17 @@ bytes BlockChain::block(h256 _hash) const if (_hash == m_genesisHash) return m_genesisBlock; - lock_guard l(m_lock); - - auto it = m_cache.find(_hash); - if (it != m_cache.end()) - return it->second; + { + ReadGuard l(x_cache); + auto it = m_cache.find(_hash); + if (it != m_cache.end()) + return it->second; + } string d; m_db->Get(m_readOptions, ldb::Slice((char const*)&_hash, 32), &d); + WriteGuard l(x_cache); m_cache[_hash].resize(d.size()); memcpy(m_cache[_hash].data(), d.data(), d.size()); @@ -389,11 +402,6 @@ bytes BlockChain::block(h256 _hash) const return m_cache[_hash]; } -eth::uint BlockChain::number(h256 _hash) const -{ - return details(_hash).number; -} - h256 BlockChain::numberHash(unsigned _n) const { if (!_n) @@ -402,69 +410,3 @@ h256 BlockChain::numberHash(unsigned _n) const for (; _n < details().number; ++_n, ret = details(ret).parent) {} return ret; } - -BlockDetails BlockChain::details(h256 _h) const -{ - lock_guard l(m_lock); - - BlockDetailsHash::const_iterator it = m_details.find(_h); - if (it != m_details.end()) - return it->second; - - std::string s; - m_extrasDB->Get(m_readOptions, toSlice(_h), &s); - if (s.empty()) - { -// cout << "Not found in DB: " << _h << endl; - return NullBlockDetails; - } - { - bool ok; - tie(it, ok) = m_details.insert(std::make_pair(_h, BlockDetails(RLP(s)))); - } - return it->second; -} - -BlockBlooms BlockChain::blooms(h256 _h) const -{ - lock_guard l(m_lock); - - BlockBloomsHash::const_iterator it = m_blooms.find(_h); - if (it != m_blooms.end()) - return it->second; - - std::string s; - m_extrasDB->Get(m_readOptions, toSlice(_h, 1), &s); - if (s.empty()) - { -// cout << "Not found in DB: " << _h << endl; - return NullBlockBlooms; - } - { - bool ok; - tie(it, ok) = m_blooms.insert(std::make_pair(_h, BlockBlooms(RLP(s)))); - } - return it->second; -} - -BlockTraces BlockChain::traces(h256 _h) const -{ - lock_guard l(m_lock); - - BlockTracesHash::const_iterator it = m_traces.find(_h); - if (it != m_traces.end()) - return it->second; - - std::string s; - m_extrasDB->Get(m_readOptions, toSlice(_h, 2), &s); - if (s.empty()) - { -// cout << "Not found in DB: " << _h << endl; - return NullBlockTraces; - } - { - bool ok; - tie(it, ok) = m_traces.insert(std::make_pair(_h, BlockTraces(RLP(s)))); - } - return it->second; -} diff --git a/libethereum/BlockChain.h b/libethereum/BlockChain.h index 749dd87f9..1a05d631c 100644 --- a/libethereum/BlockChain.h +++ b/libethereum/BlockChain.h @@ -22,6 +22,7 @@ #pragma once #include +#include #include #include #include @@ -94,6 +95,13 @@ struct BlockChainNote: public LogChannel { static const char* name() { return "= // TODO: Move all this Genesis stuff into Genesis.h/.cpp std::map const& genesisState(); +using ReadGuard = boost::shared_lock; +using UpgradableGuard = boost::upgrade_lock; +using UpgradeGuard = boost::upgrade_to_unique_lock; +using WriteGuard = boost::unique_lock; + +ldb::Slice toSlice(h256 _h, unsigned _sub = 0); + /** * @brief Implements the blockchain database. All data this gives is disk-backed. * @todo Make thread-safe. @@ -118,35 +126,36 @@ public: h256s import(bytes const& _block, OverlayDB const& _stateDB); /// Get the familial details concerning a block (or the most recent mined if none given). Thread-safe. - BlockDetails details(h256 _hash) const; + BlockDetails details(h256 _hash) const { return queryExtras(_hash, m_details, x_details, NullBlockDetails); } BlockDetails details() const { return details(currentHash()); } /// Get the transactions' bloom filters of a block (or the most recent mined if none given). Thread-safe. - BlockBlooms blooms(h256 _hash) const; + BlockBlooms blooms(h256 _hash) const { return queryExtras(_hash, m_blooms, x_blooms, NullBlockBlooms); } BlockBlooms blooms() const { return blooms(currentHash()); } /// Get the transactions' trace manifests of a block (or the most recent mined if none given). Thread-safe. - BlockTraces traces(h256 _hash) const; + BlockTraces traces(h256 _hash) const { return queryExtras(_hash, m_traces, x_traces, NullBlockTraces); } BlockTraces traces() const { return traces(currentHash()); } - /// Get a given block (RLP format). Thread-safe. + /// Get a block (RLP format) for the given hash (or the most recent mined if none given). Thread-safe. bytes block(h256 _hash) const; bytes block() const { return block(currentHash()); } - uint number(h256 _hash) const; + /// Get a number for the given hash (or the most recent mined if none given). Thread-safe. + uint number(h256 _hash) const { return details(_hash).number; } uint number() const { return number(currentHash()); } /// Get a given block (RLP format). Thread-safe. - h256 currentHash() const { return m_lastBlockHash; } + h256 currentHash() const { ReadGuard l(x_lastBlockHash); return m_lastBlockHash; } - /// Get the hash of the genesis block. + /// Get the hash of the genesis block. Thread-safe. h256 genesisHash() const { return m_genesisHash; } - /// Get the hash of a block of a given number. + /// Get the hash of a block of a given number. Slow; try not to use it too much. h256 numberHash(unsigned _n) const; /// @returns the genesis block header. - static BlockInfo const& genesis() { if (!s_genesis) { auto gb = createGenesisBlock(); (s_genesis = new BlockInfo)->populate(&gb); } return *s_genesis; } + static BlockInfo const& genesis() { UpgradableGuard l(x_genesis); if (!s_genesis) { auto gb = createGenesisBlock(); UpgradeGuard ul(l); (s_genesis = new BlockInfo)->populate(&gb); } return *s_genesis; } /// @returns the genesis block as its RLP-encoded byte array. /// @note This is slow as it's constructed anew each call. Consider genesis() instead. @@ -169,21 +178,49 @@ public: h256s treeRoute(h256 _from, h256 _to, h256* o_common = nullptr) const; private: + template T queryExtras(h256 _h, std::map& _m, boost::shared_mutex& _x, T const& _n) const + { + { + ReadGuard l(_x); + auto it = _m.find(_h); + if (it != _m.end()) + return it->second; + } + + std::string s; + m_extrasDB->Get(m_readOptions, toSlice(_h, N), &s); + if (s.empty()) + { + // cout << "Not found in DB: " << _h << endl; + return _n; + } + + WriteGuard l(_x); + auto ret = _m.insert(std::make_pair(_h, T(RLP(s)))); + return ret.first->second; + } + void checkConsistency(); - /// Get fully populated from disk DB. + /// The caches of the disk DB and their locks. + mutable boost::shared_mutex x_details; mutable BlockDetailsHash m_details; + mutable boost::shared_mutex x_blooms; mutable BlockBloomsHash m_blooms; + mutable boost::shared_mutex x_traces; mutable BlockTracesHash m_traces; - + mutable boost::shared_mutex x_cache; mutable std::map m_cache; - mutable std::recursive_mutex m_lock; + /// The disk DBs. Thread-safe, so no need for locks. ldb::DB* m_db; ldb::DB* m_extrasDB; /// Hash of the last (valid) block on the longest chain. + mutable boost::shared_mutex x_lastBlockHash; h256 m_lastBlockHash; + + /// Genesis block info. h256 m_genesisHash; bytes m_genesisBlock; @@ -192,6 +229,8 @@ private: friend std::ostream& operator<<(std::ostream& _out, BlockChain const& _bc); + /// Static genesis info and its lock. + static boost::shared_mutex x_genesis; static BlockInfo* s_genesis; };