Check State

Refactoring
This commit is contained in:
winsvega 2015-03-26 03:12:25 +03:00
parent 7ed2db70a7
commit b0c8babf0e
4 changed files with 29 additions and 78 deletions

View File

@ -177,59 +177,35 @@ void ImportTest::importTransaction(json_spirit::mObject& _o)
} }
} }
bool ImportTest::compareStates(State const& _stateExpect, State const& _statePost) void ImportTest::checkExpectedState(State const& _stateExpect, State const& _statePost, WhenError _throw)
{ {
//TestNet Addresses #define CHECK(a,b) \
static Addresses testNetAddressList = boost::assign::list_of if (_throw == WhenError::Throw) \
(Address("0000000000000000000000000000000000000001")) BOOST_CHECK_MESSAGE(a,b); \
(Address("0000000000000000000000000000000000000002")) else \
(Address("0000000000000000000000000000000000000003")) BOOST_WARN_MESSAGE(a,b);
(Address("0000000000000000000000000000000000000004"))
(Address("1a26338f0d905e295fccb71fa9ea849ffa12aaf4"))
(Address("2ef47100e0787b915105fd5e3f4ff6752079d5cb"))
(Address("6c386a4b26f73c802f34673f7248bb118f97424a"))
(Address("b9c015918bdaba24b4ff057a92a3873d6eb201be"))
(Address("cd2a3d9f938e13cd947ec05abc7fe734df8dd826"))
(Address("dbdbdb2cbd23b783741e8d7fcf51e459b497e4a6"))
(Address("e4157b34ea9615cfbde6b4fda419828124b70c78"))
(Address("e6716f9544a56c530d868e4bfbacb172315bdead"));
for (auto const& a: _stateExpect.addresses()) for (auto const& a: _stateExpect.addresses())
{ {
bool bFound = false; //do not count default addresses as it's not in addressInUse CHECK(_statePost.addressInUse(a.first), "Filling Test: " << a.first << " missing expected address!");
for (auto const& it: testNetAddressList)
{
if (it == a.first)
{
bFound = true;
break;
}
}
if (bFound)
continue;
BOOST_CHECK_MESSAGE(_statePost.addressInUse(a.first), "Filling Test Error: " << a.first << " expected address not in use!");
if (_statePost.addressInUse(a.first)) if (_statePost.addressInUse(a.first))
{ {
BOOST_CHECK_MESSAGE(_stateExpect.balance(a.first) == _statePost.balance(a.first), CHECK(_stateExpect.balance(a.first) == _statePost.balance(a.first),
"Filling Test Error: " << a.first << ": incorrect balance " << _statePost.balance(a.first) << ", expected " << _stateExpect.balance(a.first)); "Check State: " << a.first << ": incorrect balance " << _statePost.balance(a.first) << ", expected " << _stateExpect.balance(a.first));
BOOST_CHECK_MESSAGE(_stateExpect.transactionsFrom(a.first) == _statePost.transactionsFrom(a.first), CHECK(_stateExpect.transactionsFrom(a.first) == _statePost.transactionsFrom(a.first),
"Filling Test Error: " << a.first << ": incorrect nonce " << _statePost.transactionsFrom(a.first) << ", expected " << _stateExpect.transactionsFrom(a.first)); "Check State: " << a.first << ": incorrect nonce " << _statePost.transactionsFrom(a.first) << ", expected " << _stateExpect.transactionsFrom(a.first));
map<u256, u256> stateStorage = _statePost.storage(a.first); map<u256, u256> stateStorage = _statePost.storage(a.first);
for (auto const& s: _stateExpect.storage(a.first)) for (auto const& s: _stateExpect.storage(a.first))
{ {
BOOST_CHECK_MESSAGE(stateStorage[s.first] == s.second, CHECK(stateStorage[s.first] == s.second,
"Filling Test Error: " << a.first << ": incorrect storage [" << s.first << "] = " << toHex(stateStorage[s.first]) << ", expected [" << s.first << "] = " << toHex(s.second)); "Check State: " << a.first << ": incorrect storage [" << s.first << "] = " << toHex(stateStorage[s.first]) << ", expected [" << s.first << "] = " << toHex(s.second));
} }
BOOST_CHECK_MESSAGE(_stateExpect.code(a.first) == _statePost.code(a.first), CHECK(_stateExpect.code(a.first) == _statePost.code(a.first),
"Filling Test Error: " << a.first << ": incorrect code '" << toHex(_statePost.code(a.first)) << "', expected '" << toHex(_stateExpect.code(a.first)) << "'"); "Check State: " << a.first << ": incorrect code '" << toHex(_statePost.code(a.first)) << "', expected '" << toHex(_stateExpect.code(a.first)) << "'");
} }
} }
return true;
} }
void ImportTest::exportTest(bytes const& _output, State const& _statePost) void ImportTest::exportTest(bytes const& _output, State const& _statePost)
@ -243,9 +219,9 @@ void ImportTest::exportTest(bytes const& _output, State const& _statePost)
// compare expected state with post state // compare expected state with post state
if (m_TestObject.count("expect") > 0) if (m_TestObject.count("expect") > 0)
{ {
State expectState; State expectState(Address(), OverlayDB(), eth::BaseState::Empty);
importState(m_TestObject["expect"].get_obj(), expectState); importState(m_TestObject["expect"].get_obj(), expectState);
compareStates(expectState, _statePost); checkExpectedState(expectState, _statePost, Options::get().checkState ? WhenError::Throw : WhenError::DontThrow);
m_TestObject.erase(m_TestObject.find("expect")); m_TestObject.erase(m_TestObject.find("expect"));
} }
@ -623,6 +599,8 @@ Options::Options()
inputLimits = true; inputLimits = true;
else if (arg == "--bigdata") else if (arg == "--bigdata")
bigData = true; bigData = true;
else if (arg == "--checkstate")
checkState = true;
else if (arg == "--all") else if (arg == "--all")
{ {
performance = true; performance = true;
@ -630,6 +608,7 @@ Options::Options()
memory = true; memory = true;
inputLimits = true; inputLimits = true;
bigData = true; bigData = true;
checkState = true;
} }
} }
} }

View File

@ -108,7 +108,7 @@ public:
static void importState(json_spirit::mObject& _o, eth::State& _state); static void importState(json_spirit::mObject& _o, eth::State& _state);
void importTransaction(json_spirit::mObject& _o); void importTransaction(json_spirit::mObject& _o);
void exportTest(bytes const& _output, eth::State const& _statePost); void exportTest(bytes const& _output, eth::State const& _statePost);
static bool compareStates(eth::State const& _stateExpect, eth::State const& _statePost); static void checkExpectedState(eth::State const& _stateExpect, eth::State const& _statePost, WhenError _throw = WhenError::Throw);
eth::State m_statePre; eth::State m_statePre;
eth::State m_statePost; eth::State m_statePost;
@ -166,6 +166,7 @@ public:
bool fillTests = false; ///< Create JSON test files from execution results bool fillTests = false; ///< Create JSON test files from execution results
bool stats = false; ///< Execution time stats bool stats = false; ///< Execution time stats
std::string statsOutFile; ///< Stats output file. "out" for standard output std::string statsOutFile; ///< Stats output file. "out" for standard output
bool checkState = false;///< Throw error when checking test states
/// Test selection /// Test selection
/// @{ /// @{

View File

@ -91,23 +91,9 @@ void doStateTests(json_spirit::mValue& v, bool _fillin)
// check addresses // check addresses
#if ETH_FATDB #if ETH_FATDB
ImportTest::checkExpectedState(importer.m_statePost, theState);
auto expectedAddrs = importer.m_statePost.addresses(); auto expectedAddrs = importer.m_statePost.addresses();
auto resultAddrs = theState.addresses(); auto resultAddrs = theState.addresses();
for (auto& expectedPair : expectedAddrs)
{
auto& expectedAddr = expectedPair.first;
auto resultAddrIt = resultAddrs.find(expectedAddr);
if (resultAddrIt == resultAddrs.end())
BOOST_ERROR("Missing expected address " << expectedAddr);
else
{
BOOST_CHECK_MESSAGE(importer.m_statePost.balance(expectedAddr) == theState.balance(expectedAddr), expectedAddr << ": incorrect balance " << theState.balance(expectedAddr) << ", expected " << importer.m_statePost.balance(expectedAddr));
BOOST_CHECK_MESSAGE(importer.m_statePost.transactionsFrom(expectedAddr) == theState.transactionsFrom(expectedAddr), expectedAddr << ": incorrect txCount " << theState.transactionsFrom(expectedAddr) << ", expected " << importer.m_statePost.transactionsFrom(expectedAddr));
BOOST_CHECK_MESSAGE(importer.m_statePost.code(expectedAddr) == theState.code(expectedAddr), expectedAddr << ": incorrect code");
checkStorage(importer.m_statePost.storage(expectedAddr), theState.storage(expectedAddr), expectedAddr);
}
}
checkAddresses<map<Address, u256> >(expectedAddrs, resultAddrs); checkAddresses<map<Address, u256> >(expectedAddrs, resultAddrs);
#endif #endif
BOOST_CHECK_MESSAGE(theState.rootHash() == h256(o["postStateRoot"].get_str()), "wrong post state root"); BOOST_CHECK_MESSAGE(theState.rootHash() == h256(o["postStateRoot"].get_str()), "wrong post state root");

27
vm.cpp
View File

@ -380,7 +380,6 @@ void doVMTests(json_spirit::mValue& v, bool _fillin)
} }
} }
if (_fillin) if (_fillin)
{ {
o["env"] = mValue(fev.exportEnv()); o["env"] = mValue(fev.exportEnv());
@ -394,7 +393,7 @@ void doVMTests(json_spirit::mValue& v, bool _fillin)
State postState, expectState; State postState, expectState;
ImportTest::importState(o["post"].get_obj(), postState); ImportTest::importState(o["post"].get_obj(), postState);
ImportTest::importState(o["expect"].get_obj(), expectState); ImportTest::importState(o["expect"].get_obj(), expectState);
ImportTest::compareStates(expectState, postState); ImportTest::checkExpectedState(expectState, postState, Options::get().checkState ? WhenError::Throw : WhenError::DontThrow);
o.erase(o.find("expect")); o.erase(o.find("expect"));
} }
@ -425,25 +424,11 @@ void doVMTests(json_spirit::mValue& v, bool _fillin)
BOOST_CHECK_EQUAL(toInt(o["gas"]), gas); BOOST_CHECK_EQUAL(toInt(o["gas"]), gas);
auto& expectedAddrs = test.addresses; State postState, expectState;
auto& resultAddrs = fev.addresses; mObject mPostState = fev.exportState();
for (auto&& expectedPair : expectedAddrs) ImportTest::importState(mPostState, postState);
{ ImportTest::importState(o["post"].get_obj(), expectState);
auto& expectedAddr = expectedPair.first; ImportTest::checkExpectedState(expectState, postState);
auto resultAddrIt = resultAddrs.find(expectedAddr);
if (resultAddrIt == resultAddrs.end())
BOOST_ERROR("Missing expected address " << expectedAddr);
else
{
auto& expectedState = expectedPair.second;
auto& resultState = resultAddrIt->second;
BOOST_CHECK_MESSAGE(std::get<0>(expectedState) == std::get<0>(resultState), expectedAddr << ": incorrect balance " << std::get<0>(resultState) << ", expected " << std::get<0>(expectedState));
BOOST_CHECK_MESSAGE(std::get<1>(expectedState) == std::get<1>(resultState), expectedAddr << ": incorrect txCount " << std::get<1>(resultState) << ", expected " << std::get<1>(expectedState));
BOOST_CHECK_MESSAGE(std::get<3>(expectedState) == std::get<3>(resultState), expectedAddr << ": incorrect code");
checkStorage(std::get<2>(expectedState), std::get<2>(resultState), expectedAddr);
}
}
checkAddresses<std::map<Address, std::tuple<u256, u256, std::map<u256, u256>, bytes> > >(test.addresses, fev.addresses); checkAddresses<std::map<Address, std::tuple<u256, u256, std::map<u256, u256>, bytes> > >(test.addresses, fev.addresses);