From 6d29d724e42581ae71d5565383fb20d329e1ddab Mon Sep 17 00:00:00 2001 From: arkpar Date: Thu, 13 Aug 2015 18:50:43 +0200 Subject: [PATCH] immediate invariants check --- libdevcore/Common.cpp | 8 ++++---- libdevcore/Common.h | 11 ++++++----- libethereum/BlockChainSync.cpp | 16 +++++++++------- 3 files changed, 19 insertions(+), 16 deletions(-) diff --git a/libdevcore/Common.cpp b/libdevcore/Common.cpp index 6fd97a781..e38d0136d 100644 --- a/libdevcore/Common.cpp +++ b/libdevcore/Common.cpp @@ -33,12 +33,12 @@ char const* Version = ETH_PROJECT_VERSION; const u256 UndefinedU256 = ~(u256)0; -void InvariantChecker::checkInvariants() const +void InvariantChecker::checkInvariants(HasInvariants const* _this, char const* _fn, char const* _file, int _line, bool _pre) { - if (!m_this->invariants()) + if (!_this->invariants()) { - cwarn << "Invariant failed in" << m_function << "at" << m_file << ":" << m_line; - ::boost::exception_detail::throw_exception_(FailedInvariant(), m_function, m_file, m_line); + cwarn << (_pre ? "Pre" : "Post") << "invariant failed in" << _fn << "at" << _file << ":" << _line; + ::boost::exception_detail::throw_exception_(FailedInvariant(), _fn, _file, _line); } } diff --git a/libdevcore/Common.h b/libdevcore/Common.h index ce637a2a6..158cea255 100644 --- a/libdevcore/Common.h +++ b/libdevcore/Common.h @@ -216,13 +216,12 @@ public: class InvariantChecker { public: - InvariantChecker(HasInvariants* _this, char const* _fn, char const* _file, int _line): m_this(_this), m_function(_fn), m_file(_file), m_line(_line) { checkInvariants(); } - ~InvariantChecker() { checkInvariants(); } - -private: + InvariantChecker(HasInvariants* _this, char const* _fn, char const* _file, int _line): m_this(_this), m_function(_fn), m_file(_file), m_line(_line) { checkInvariants(_this, _fn , _file, _line, true); } + ~InvariantChecker() { checkInvariants(m_this, m_function, m_file, m_line, false); } /// Check invariants are met, throw if not. - void checkInvariants() const; + static void checkInvariants(HasInvariants const* _this, char const* _fn, char const* _file, int line, bool _pre); +private: HasInvariants const* m_this; char const* m_function; char const* m_file; @@ -232,8 +231,10 @@ private: /// Scope guard for invariant check in a class derived from HasInvariants. #if ETH_DEBUG #define DEV_INVARIANT_CHECK ::dev::InvariantChecker __dev_invariantCheck(this, BOOST_CURRENT_FUNCTION, __FILE__, __LINE__) +#define DEV_INVARIANT_CHECK_HERE ::dev::InvariantChecker::checkInvariants(this, BOOST_CURRENT_FUNCTION, __FILE__, __LINE__, true) #else #define DEV_INVARIANT_CHECK (void)0; +#define DEV_INVARIANT_CHECK_HERE (void)0; #endif /// Simple scope-based timer helper. diff --git a/libethereum/BlockChainSync.cpp b/libethereum/BlockChainSync.cpp index d51c25048..7692cb91c 100644 --- a/libethereum/BlockChainSync.cpp +++ b/libethereum/BlockChainSync.cpp @@ -260,13 +260,13 @@ void BlockChainSync::onPeerBlocks(std::shared_ptr _peer, RLP const else requestBlocks(_peer); // Some of the blocks might have been downloaded by helping peers, proceed anyway } - DEV_INVARIANT_CHECK; + DEV_INVARIANT_CHECK_HERE; } void BlockChainSync::onPeerNewBlock(std::shared_ptr _peer, RLP const& _r) { - DEV_INVARIANT_CHECK; RecursiveGuard l(x_sync); + DEV_INVARIANT_CHECK; auto h = BlockInfo::headerHashFromBlock(_r[0].data()); if (_r.itemCount() != 2) @@ -441,7 +441,7 @@ void PV60Sync::transition(std::shared_ptr _peer, SyncState _s, boo // Looks like it's the best yet for total difficulty. Set to download. setState(_peer, SyncState::Blocks, isSyncing(_peer), _needHelp); // will kick off other peers to help if available. requestBlocks(_peer); - DEV_INVARIANT_CHECK; + DEV_INVARIANT_CHECK_HERE; return; } } @@ -453,7 +453,7 @@ void PV60Sync::transition(std::shared_ptr _peer, SyncState _s, boo { setState(_peer, SyncState::NewBlocks, true, _needHelp); requestBlocks(_peer); - DEV_INVARIANT_CHECK; + DEV_INVARIANT_CHECK_HERE; return; } } @@ -487,7 +487,7 @@ void PV60Sync::transition(std::shared_ptr _peer, SyncState _s, boo setState(_peer, SyncState::Idle, false); } // Otherwise it's fine. We don't care if it's Nothing->Nothing. - DEV_INVARIANT_CHECK; + DEV_INVARIANT_CHECK_HERE; return; } @@ -827,6 +827,7 @@ void PV60Sync::abortSync() // Just set to idle. Hashchain is keept, Sync will be continued if there are more peers to sync with setState(std::shared_ptr(), SyncState::Idle, false, true); } + DEV_INVARIANT_CHECK_HERE; } void PV60Sync::onPeerAborting() @@ -839,6 +840,7 @@ void PV60Sync::onPeerAborting() m_syncer.reset(); abortSync(); } + DEV_INVARIANT_CHECK_HERE; } bool PV60Sync::invariants() const @@ -1131,7 +1133,7 @@ void PV61Sync::onPeerHashes(std::shared_ptr _peer, h256s const& _h } requestSubchain(_peer); } - DEV_INVARIANT_CHECK; + DEV_INVARIANT_CHECK_HERE; } void PV61Sync::onPeerAborting() @@ -1173,7 +1175,7 @@ void PV61Sync::onPeerAborting() } else if (isPV61Syncing() && m_state == SyncState::Hashes) requestSubchains(); - DEV_INVARIANT_CHECK; + DEV_INVARIANT_CHECK_HERE; } SyncStatus PV61Sync::status() const