Browse Source

immediate invariants check

cl-refactor
arkpar 10 years ago
parent
commit
6d29d724e4
  1. 8
      libdevcore/Common.cpp
  2. 11
      libdevcore/Common.h
  3. 16
      libethereum/BlockChainSync.cpp

8
libdevcore/Common.cpp

@ -33,12 +33,12 @@ char const* Version = ETH_PROJECT_VERSION;
const u256 UndefinedU256 = ~(u256)0; 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; cwarn << (_pre ? "Pre" : "Post") << "invariant failed in" << _fn << "at" << _file << ":" << _line;
::boost::exception_detail::throw_exception_(FailedInvariant(), m_function, m_file, m_line); ::boost::exception_detail::throw_exception_(FailedInvariant(), _fn, _file, _line);
} }
} }

11
libdevcore/Common.h

@ -216,13 +216,12 @@ public:
class InvariantChecker class InvariantChecker
{ {
public: 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(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(); } ~InvariantChecker() { checkInvariants(m_this, m_function, m_file, m_line, false); }
private:
/// Check invariants are met, throw if not. /// 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; HasInvariants const* m_this;
char const* m_function; char const* m_function;
char const* m_file; char const* m_file;
@ -232,8 +231,10 @@ private:
/// Scope guard for invariant check in a class derived from HasInvariants. /// Scope guard for invariant check in a class derived from HasInvariants.
#if ETH_DEBUG #if ETH_DEBUG
#define DEV_INVARIANT_CHECK ::dev::InvariantChecker __dev_invariantCheck(this, BOOST_CURRENT_FUNCTION, __FILE__, __LINE__) #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 #else
#define DEV_INVARIANT_CHECK (void)0; #define DEV_INVARIANT_CHECK (void)0;
#define DEV_INVARIANT_CHECK_HERE (void)0;
#endif #endif
/// Simple scope-based timer helper. /// Simple scope-based timer helper.

16
libethereum/BlockChainSync.cpp

@ -260,13 +260,13 @@ void BlockChainSync::onPeerBlocks(std::shared_ptr<EthereumPeer> _peer, RLP const
else else
requestBlocks(_peer); // Some of the blocks might have been downloaded by helping peers, proceed anyway 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<EthereumPeer> _peer, RLP const& _r) void BlockChainSync::onPeerNewBlock(std::shared_ptr<EthereumPeer> _peer, RLP const& _r)
{ {
DEV_INVARIANT_CHECK;
RecursiveGuard l(x_sync); RecursiveGuard l(x_sync);
DEV_INVARIANT_CHECK;
auto h = BlockInfo::headerHashFromBlock(_r[0].data()); auto h = BlockInfo::headerHashFromBlock(_r[0].data());
if (_r.itemCount() != 2) if (_r.itemCount() != 2)
@ -441,7 +441,7 @@ void PV60Sync::transition(std::shared_ptr<EthereumPeer> _peer, SyncState _s, boo
// Looks like it's the best yet for total difficulty. Set to download. // 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. setState(_peer, SyncState::Blocks, isSyncing(_peer), _needHelp); // will kick off other peers to help if available.
requestBlocks(_peer); requestBlocks(_peer);
DEV_INVARIANT_CHECK; DEV_INVARIANT_CHECK_HERE;
return; return;
} }
} }
@ -453,7 +453,7 @@ void PV60Sync::transition(std::shared_ptr<EthereumPeer> _peer, SyncState _s, boo
{ {
setState(_peer, SyncState::NewBlocks, true, _needHelp); setState(_peer, SyncState::NewBlocks, true, _needHelp);
requestBlocks(_peer); requestBlocks(_peer);
DEV_INVARIANT_CHECK; DEV_INVARIANT_CHECK_HERE;
return; return;
} }
} }
@ -487,7 +487,7 @@ void PV60Sync::transition(std::shared_ptr<EthereumPeer> _peer, SyncState _s, boo
setState(_peer, SyncState::Idle, false); setState(_peer, SyncState::Idle, false);
} }
// Otherwise it's fine. We don't care if it's Nothing->Nothing. // Otherwise it's fine. We don't care if it's Nothing->Nothing.
DEV_INVARIANT_CHECK; DEV_INVARIANT_CHECK_HERE;
return; 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 // Just set to idle. Hashchain is keept, Sync will be continued if there are more peers to sync with
setState(std::shared_ptr<EthereumPeer>(), SyncState::Idle, false, true); setState(std::shared_ptr<EthereumPeer>(), SyncState::Idle, false, true);
} }
DEV_INVARIANT_CHECK_HERE;
} }
void PV60Sync::onPeerAborting() void PV60Sync::onPeerAborting()
@ -839,6 +840,7 @@ void PV60Sync::onPeerAborting()
m_syncer.reset(); m_syncer.reset();
abortSync(); abortSync();
} }
DEV_INVARIANT_CHECK_HERE;
} }
bool PV60Sync::invariants() const bool PV60Sync::invariants() const
@ -1131,7 +1133,7 @@ void PV61Sync::onPeerHashes(std::shared_ptr<EthereumPeer> _peer, h256s const& _h
} }
requestSubchain(_peer); requestSubchain(_peer);
} }
DEV_INVARIANT_CHECK; DEV_INVARIANT_CHECK_HERE;
} }
void PV61Sync::onPeerAborting() void PV61Sync::onPeerAborting()
@ -1173,7 +1175,7 @@ void PV61Sync::onPeerAborting()
} }
else if (isPV61Syncing() && m_state == SyncState::Hashes) else if (isPV61Syncing() && m_state == SyncState::Hashes)
requestSubchains(); requestSubchains();
DEV_INVARIANT_CHECK; DEV_INVARIANT_CHECK_HERE;
} }
SyncStatus PV61Sync::status() const SyncStatus PV61Sync::status() const

Loading…
Cancel
Save