From ee06872b1927b6f88e2dc8892d94f00fecbe2a7e Mon Sep 17 00:00:00 2001 From: Marek Kotewicz Date: Fri, 12 Jun 2015 17:37:59 +0200 Subject: [PATCH 1/6] ImportRoute class instead of pair --- libethereum/BlockChain.cpp | 14 +++++++------- libethereum/BlockChain.h | 14 +++++++++++++- libethereum/Client.cpp | 13 +++++++------ 3 files changed, 27 insertions(+), 14 deletions(-) diff --git a/libethereum/BlockChain.cpp b/libethereum/BlockChain.cpp index 480c7f977..b7618e2c7 100644 --- a/libethereum/BlockChain.cpp +++ b/libethereum/BlockChain.cpp @@ -326,8 +326,8 @@ tuple BlockChain::sync(BlockQueue& _bq, OverlayDB const& _st ImportRoute r; DEV_TIMED_ABOVE(Block import, 500) r = import(block.verified, _stateDB, ImportRequirements::Default & ~ImportRequirements::ValidNonce & ~ImportRequirements::CheckUncles); - fresh += r.first; - dead += r.second; + fresh += r.liveBlocks(); + dead += r.deadBlocks(); } catch (dev::eth::UnknownParent) { @@ -364,21 +364,21 @@ pair BlockChain::attemptImport(bytes const& _block, O } catch (UnknownParent&) { - return make_pair(ImportResult::UnknownParent, make_pair(h256s(), h256s())); + return make_pair(ImportResult::UnknownParent, ImportRoute()); } catch (AlreadyHaveBlock&) { - return make_pair(ImportResult::AlreadyKnown, make_pair(h256s(), h256s())); + return make_pair(ImportResult::AlreadyKnown, ImportRoute()); } catch (FutureTime&) { - return make_pair(ImportResult::FutureTime, make_pair(h256s(), h256s())); + return make_pair(ImportResult::FutureTime, ImportRoute()); } catch (Exception& ex) { if (m_onBad) m_onBad(ex); - return make_pair(ImportResult::Malformed, make_pair(h256s(), h256s())); + return make_pair(ImportResult::Malformed, ImportRoute()); } } @@ -699,7 +699,7 @@ ImportRoute BlockChain::import(VerifiedBlockRef const& _block, OverlayDB const& dead.push_back(h); else fresh.push_back(h); - return make_pair(fresh, dead); + return ImportRoute(dead, fresh); } void BlockChain::clearBlockBlooms(unsigned _begin, unsigned _end) diff --git a/libethereum/BlockChain.h b/libethereum/BlockChain.h index 5f197cfee..6decf46b6 100644 --- a/libethereum/BlockChain.h +++ b/libethereum/BlockChain.h @@ -80,7 +80,19 @@ ldb::Slice toSlice(h256 const& _h, unsigned _sub = 0); using BlocksHash = std::unordered_map; using TransactionHashes = h256s; using UncleHashes = h256s; -using ImportRoute = std::pair; + +class ImportRoute +{ +public: + ImportRoute() {}; + ImportRoute(h256s const& _deadBlocks, h256s const& _liveBlocks): m_deadBlocks(_deadBlocks), m_liveBlocks(_liveBlocks) {}; + h256s const& deadBlocks() const { return m_deadBlocks; } + h256s const& liveBlocks() const { return m_liveBlocks; } + +private: + h256s m_deadBlocks; + h256s m_liveBlocks; +}; enum { ExtraDetails = 0, diff --git a/libethereum/Client.cpp b/libethereum/Client.cpp index 5581d1071..9d2c9a9e6 100644 --- a/libethereum/Client.cpp +++ b/libethereum/Client.cpp @@ -613,10 +613,11 @@ bool Client::submitWork(ProofOfWork::Solution const& _solution) void Client::syncBlockQueue() { - ImportRoute ir; cwork << "BQ ==> CHAIN ==> STATE"; - tie(ir.first, ir.second, m_syncBlockQueue) = m_bc.sync(m_bq, m_stateDB, rand() % 10 + 5); - if (ir.first.empty()) + pair blocks; + tie(blocks.first, blocks.second, m_syncBlockQueue) = m_bc.sync(m_bq, m_stateDB, rand() % 10 + 5); + ImportRoute ir(blocks.second, blocks.first); + if (ir.liveBlocks().empty()) return; onChainChanged(ir); } @@ -658,7 +659,7 @@ void Client::syncTransactionQueue() void Client::onChainChanged(ImportRoute const& _ir) { // insert transactions that we are declaring the dead part of the chain - for (auto const& h: _ir.second) + for (auto const& h: _ir.deadBlocks()) { clog(ClientNote) << "Dead block:" << h; for (auto const& t: m_bc.transactions(h)) @@ -669,7 +670,7 @@ void Client::onChainChanged(ImportRoute const& _ir) } // remove transactions from m_tq nicely rather than relying on out of date nonce later on. - for (auto const& h: _ir.first) + for (auto const& h: _ir.liveBlocks()) { clog(ClientChat) << "Live block:" << h; for (auto const& th: m_bc.transactionHashes(h)) @@ -683,7 +684,7 @@ void Client::onChainChanged(ImportRoute const& _ir) h->noteNewBlocks(); h256Hash changeds; - for (auto const& h: _ir.first) + for (auto const& h: _ir.liveBlocks()) appendFromNewBlock(h, changeds); // RESTART MINING From e9748677df6463b3bebb304bbad609fee5f1fa01 Mon Sep 17 00:00:00 2001 From: Marek Kotewicz Date: Sun, 14 Jun 2015 06:19:29 +0200 Subject: [PATCH 2/6] ImportRoute struct instead of class --- libethereum/BlockChain.cpp | 8 ++++---- libethereum/BlockChain.h | 14 +++----------- libethereum/Client.cpp | 13 ++++++------- 3 files changed, 13 insertions(+), 22 deletions(-) diff --git a/libethereum/BlockChain.cpp b/libethereum/BlockChain.cpp index b76ec591e..83275045c 100644 --- a/libethereum/BlockChain.cpp +++ b/libethereum/BlockChain.cpp @@ -326,8 +326,8 @@ tuple BlockChain::sync(BlockQueue& _bq, OverlayDB const& _st ImportRoute r; DEV_TIMED_ABOVE(Block import, 500) r = import(block.verified, _stateDB, ImportRequirements::Default & ~ImportRequirements::ValidNonce & ~ImportRequirements::CheckUncles); - fresh += r.liveBlocks(); - dead += r.deadBlocks(); + fresh += r.liveBlocks; + dead += r.deadBlocks; } catch (dev::eth::UnknownParent) { @@ -699,8 +699,8 @@ ImportRoute BlockChain::import(VerifiedBlockRef const& _block, OverlayDB const& dead.push_back(h); else fresh.push_back(h); - return ImportRoute(dead, fresh); -} + return ImportRoute{dead, fresh}; +}; void BlockChain::clearBlockBlooms(unsigned _begin, unsigned _end) { diff --git a/libethereum/BlockChain.h b/libethereum/BlockChain.h index d865fd025..23137c38d 100644 --- a/libethereum/BlockChain.h +++ b/libethereum/BlockChain.h @@ -81,17 +81,9 @@ using BlocksHash = std::unordered_map; using TransactionHashes = h256s; using UncleHashes = h256s; -class ImportRoute -{ -public: - ImportRoute() {}; - ImportRoute(h256s const& _deadBlocks, h256s const& _liveBlocks): m_deadBlocks(_deadBlocks), m_liveBlocks(_liveBlocks) {}; - h256s const& deadBlocks() const { return m_deadBlocks; } - h256s const& liveBlocks() const { return m_liveBlocks; } - -private: - h256s m_deadBlocks; - h256s m_liveBlocks; +struct ImportRoute { + h256s deadBlocks; + h256s liveBlocks; }; enum { diff --git a/libethereum/Client.cpp b/libethereum/Client.cpp index cd8cb6805..0177c6007 100644 --- a/libethereum/Client.cpp +++ b/libethereum/Client.cpp @@ -614,10 +614,9 @@ bool Client::submitWork(ProofOfWork::Solution const& _solution) void Client::syncBlockQueue() { cwork << "BQ ==> CHAIN ==> STATE"; - pair blocks; - tie(blocks.first, blocks.second, m_syncBlockQueue) = m_bc.sync(m_bq, m_stateDB, rand() % 10 + 5); - ImportRoute ir(blocks.second, blocks.first); - if (ir.liveBlocks().empty()) + ImportRoute ir; + tie(ir.liveBlocks, ir.deadBlocks, m_syncBlockQueue) = m_bc.sync(m_bq, m_stateDB, rand() % 10 + 5); + if (ir.liveBlocks.empty()) return; onChainChanged(ir); } @@ -659,7 +658,7 @@ void Client::syncTransactionQueue() void Client::onChainChanged(ImportRoute const& _ir) { // insert transactions that we are declaring the dead part of the chain - for (auto const& h: _ir.deadBlocks()) + for (auto const& h: _ir.deadBlocks) { clog(ClientNote) << "Dead block:" << h; for (auto const& t: m_bc.transactions(h)) @@ -670,7 +669,7 @@ void Client::onChainChanged(ImportRoute const& _ir) } // remove transactions from m_tq nicely rather than relying on out of date nonce later on. - for (auto const& h: _ir.liveBlocks()) + for (auto const& h: _ir.liveBlocks) { clog(ClientChat) << "Live block:" << h; for (auto const& th: m_bc.transactionHashes(h)) @@ -684,7 +683,7 @@ void Client::onChainChanged(ImportRoute const& _ir) h->noteNewBlocks(); h256Hash changeds; - for (auto const& h: _ir.liveBlocks()) + for (auto const& h: _ir.liveBlocks) appendFromNewBlock(h, changeds); // RESTART MINING From 821ba0e002a0af53c6c86c4f712d03bcb2f10a2d Mon Sep 17 00:00:00 2001 From: Marek Kotewicz Date: Sun, 14 Jun 2015 06:32:51 +0200 Subject: [PATCH 3/6] changed Blockchain sync return tuple from to --- libethereum/BlockChain.cpp | 4 ++-- libethereum/BlockChain.h | 2 +- libethereum/Client.cpp | 3 ++- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/libethereum/BlockChain.cpp b/libethereum/BlockChain.cpp index 83275045c..0a3be0f1b 100644 --- a/libethereum/BlockChain.cpp +++ b/libethereum/BlockChain.cpp @@ -305,7 +305,7 @@ LastHashes BlockChain::lastHashes(unsigned _n) const return m_lastLastHashes; } -tuple BlockChain::sync(BlockQueue& _bq, OverlayDB const& _stateDB, unsigned _max) +tuple BlockChain::sync(BlockQueue& _bq, OverlayDB const& _stateDB, unsigned _max) { // _bq.tick(*this); @@ -353,7 +353,7 @@ tuple BlockChain::sync(BlockQueue& _bq, OverlayDB const& _st badBlocks.push_back(block.verified.info.hash()); } } - return make_tuple(fresh, dead, _bq.doneDrain(badBlocks)); + return make_tuple(ImportRoute{dead, fresh}, _bq.doneDrain(badBlocks)); } pair BlockChain::attemptImport(bytes const& _block, OverlayDB const& _stateDB, ImportRequirements::value _ir) noexcept diff --git a/libethereum/BlockChain.h b/libethereum/BlockChain.h index 23137c38d..e3fcf83c1 100644 --- a/libethereum/BlockChain.h +++ b/libethereum/BlockChain.h @@ -116,7 +116,7 @@ public: /// 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. - std::tuple sync(BlockQueue& _bq, OverlayDB const& _stateDB, unsigned _max); + std::tuple sync(BlockQueue& _bq, OverlayDB const& _stateDB, unsigned _max); /// Attempt to import the given block directly into the CanonBlockChain and sync with the state DB. /// @returns the block hashes of any blocks that came into/went out of the canonical block chain. diff --git a/libethereum/Client.cpp b/libethereum/Client.cpp index 0177c6007..70ef1e406 100644 --- a/libethereum/Client.cpp +++ b/libethereum/Client.cpp @@ -614,8 +614,9 @@ bool Client::submitWork(ProofOfWork::Solution const& _solution) void Client::syncBlockQueue() { cwork << "BQ ==> CHAIN ==> STATE"; + ImportRoute ir; - tie(ir.liveBlocks, ir.deadBlocks, m_syncBlockQueue) = m_bc.sync(m_bq, m_stateDB, rand() % 10 + 5); + tie(ir, m_syncBlockQueue) = m_bc.sync(m_bq, m_stateDB, rand() % 10 + 5); if (ir.liveBlocks.empty()) return; onChainChanged(ir); From 63fb8d89bd0b9112d6253cb817ee43c8f7a54d1a Mon Sep 17 00:00:00 2001 From: Marek Kotewicz Date: Sun, 14 Jun 2015 06:38:12 +0200 Subject: [PATCH 4/6] merge fix --- libethereum/Client.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libethereum/Client.cpp b/libethereum/Client.cpp index a3294f3c7..9ce0cb5ac 100644 --- a/libethereum/Client.cpp +++ b/libethereum/Client.cpp @@ -629,7 +629,7 @@ void Client::syncBlockQueue() m_syncAmount = max(c_syncMin, m_syncAmount * 9 / 10); else if (elapsed < c_targetDuration * 0.9 && m_syncAmount < c_syncMax) m_syncAmount = min(c_syncMax, m_syncAmount * 11 / 10 + 1); - if (ir.first.empty()) + if (ir.liveBlocks.empty()) return; onChainChanged(ir); } From c592fde8cf8481d77ffd5d3a97ad851d1c0c4008 Mon Sep 17 00:00:00 2001 From: Marek Kotewicz Date: Mon, 15 Jun 2015 03:22:55 +0200 Subject: [PATCH 5/6] style fix --- libethereum/BlockChain.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/libethereum/BlockChain.h b/libethereum/BlockChain.h index e3fcf83c1..2d3abd922 100644 --- a/libethereum/BlockChain.h +++ b/libethereum/BlockChain.h @@ -81,7 +81,8 @@ using BlocksHash = std::unordered_map; using TransactionHashes = h256s; using UncleHashes = h256s; -struct ImportRoute { +struct ImportRoute +{ h256s deadBlocks; h256s liveBlocks; }; From 6ae2eb8039efc4427c0295f202c2c254a2804d3f Mon Sep 17 00:00:00 2001 From: Marek Kotewicz Date: Mon, 15 Jun 2015 04:39:44 +0200 Subject: [PATCH 6/6] removed extra ; --- libethereum/BlockChain.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libethereum/BlockChain.cpp b/libethereum/BlockChain.cpp index 0a3be0f1b..7386fa776 100644 --- a/libethereum/BlockChain.cpp +++ b/libethereum/BlockChain.cpp @@ -700,7 +700,7 @@ ImportRoute BlockChain::import(VerifiedBlockRef const& _block, OverlayDB const& else fresh.push_back(h); return ImportRoute{dead, fresh}; -}; +} void BlockChain::clearBlockBlooms(unsigned _begin, unsigned _end) {