aboutsummaryrefslogtreecommitdiffstats
path: root/TestHelper.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'TestHelper.cpp')
-rw-r--r--TestHelper.cpp86
1 files changed, 54 insertions, 32 deletions
diff --git a/TestHelper.cpp b/TestHelper.cpp
index e2c51a66..39977975 100644
--- a/TestHelper.cpp
+++ b/TestHelper.cpp
@@ -25,6 +25,7 @@
#include <chrono>
#include <libethcore/EthashAux.h>
#include <libethereum/Client.h>
+#include <libevm/ExtVMFace.h>
#include <liblll/Compiler.h>
#include <libevm/VMFactory.h>
#include "Stats.h"
@@ -61,7 +62,7 @@ void connectClients(Client& c1, Client& c2)
#endif
}
-void mine(State& s, BlockChain const& _bc)
+void mine(Block& s, BlockChain const& _bc)
{
std::unique_ptr<SealEngineFace> sealer(Ethash::createSealEngine());
s.commitToSeal(_bc);
@@ -94,23 +95,43 @@ struct MissingFields : virtual Exception {};
bigint const c_max256plus1 = bigint(1) << 256;
-ImportTest::ImportTest(json_spirit::mObject& _o, bool isFiller):
- m_statePre(OverlayDB(), eth::BaseState::Empty, Address(_o["env"].get_obj()["currentCoinbase"].get_str())),
- m_statePost(OverlayDB(), eth::BaseState::Empty, Address(_o["env"].get_obj()["currentCoinbase"].get_str())),
- m_environment(m_envInfo),
+ImportTest::ImportTest(json_spirit::mObject& _o, bool isFiller, testType testTemplate):
+ m_statePre(OverlayDB(), eth::BaseState::Empty),
+ m_statePost(OverlayDB(), eth::BaseState::Empty),
m_testObject(_o)
{
- importEnv(_o["env"].get_obj());
- importState(_o["pre"].get_obj(), m_statePre);
- importTransaction(_o["transaction"].get_obj());
-
- if (!isFiller)
+ if (testTemplate == testType::StateTests)
{
- importState(_o["post"].get_obj(), m_statePost);
- m_environment.sub.logs = importLog(_o["logs"].get_array());
+ importEnv(_o["env"].get_obj());
+ importTransaction(_o["transaction"].get_obj());
+ importState(_o["pre"].get_obj(), m_statePre);
+ if (!isFiller)
+ {
+ if (_o.count("post"))
+ importState(_o["post"].get_obj(), m_statePost);
+ else
+ importState(_o["postState"].get_obj(), m_statePost);
+ m_logsExpected = importLog(_o["logs"].get_array());
+ }
}
}
+//executes an imported transacton on preState
+bytes ImportTest::executeTest()
+{
+ ExecutionResult res;
+ eth::State tmpState = m_statePre;
+
+ std::pair<ExecutionResult, TransactionReceipt> execOut = m_statePre.execute(m_envInfo, m_transaction);
+ res = execOut.first;
+ m_logs = execOut.second.log();
+ m_statePre.commit();
+ m_statePost = m_statePre;
+ m_statePre = tmpState;
+
+ return res.output;
+}
+
json_spirit::mObject& ImportTest::makeAllFieldsHex(json_spirit::mObject& _o)
{
static const set<string> hashes {"bloom" , "coinbase", "hash", "mixHash", "parentHash", "receiptTrie",
@@ -139,24 +160,25 @@ json_spirit::mObject& ImportTest::makeAllFieldsHex(json_spirit::mObject& _o)
void ImportTest::importEnv(json_spirit::mObject& _o)
{
- assert(_o.count("previousHash") > 0);
assert(_o.count("currentGasLimit") > 0);
- assert(_o.count("currentDifficulty") > 0);
+ assert(_o.count("currentDifficulty") > 0);
+ assert(_o.count("currentNumber") > 0);
assert(_o.count("currentTimestamp") > 0);
assert(_o.count("currentCoinbase") > 0);
- assert(_o.count("currentNumber") > 0);
- m_envInfo.setBeneficiary(Address(_o["currentCoinbase"].get_str()));
+ m_envInfo.setGasLimit(toInt(_o["currentGasLimit"]));
m_envInfo.setDifficulty(toInt(_o["currentDifficulty"]));
m_envInfo.setNumber(toInt(_o["currentNumber"]));
- m_envInfo.setGasLimit(toInt(_o["currentGasLimit"]));
m_envInfo.setTimestamp(toInt(_o["currentTimestamp"]));
+ m_envInfo.setBeneficiary(Address(_o["currentCoinbase"].get_str()));
+ m_envInfo.setLastHashes( lastHashes( m_envInfo.number() ) );
}
// import state from not fully declared json_spirit::mObject, writing to _stateOptionsMap which fields were defined in json
void ImportTest::importState(json_spirit::mObject& _o, State& _state, AccountMaskMap& o_mask)
-{
- _state.populateFrom(jsonToAccountMap(json_spirit::write_string(_o, false), &o_mask));
+{
+ std::string jsondata = json_spirit::write_string((json_spirit::mValue)_o, false);
+ _state.populateFrom(jsonToAccountMap(jsondata, &o_mask));
}
void ImportTest::importState(json_spirit::mObject& _o, State& _state)
@@ -215,7 +237,7 @@ void ImportTest::importTransaction(json_spirit::mObject& _o)
}
}
-void ImportTest::checkExpectedState(State const& _stateExpect, State const& _statePost, stateOptionsMap const _expectedStateOptions, WhenError _throw)
+void ImportTest::compareStates(State const& _stateExpect, State const& _statePost, AccountMaskMap const _expectedStateOptions, WhenError _throw)
{
#define CHECK(a,b) \
{ \
@@ -230,7 +252,7 @@ void ImportTest::checkExpectedState(State const& _stateExpect, State const& _sta
CHECK(_statePost.addressInUse(a.first), "Filling Test: " << a.first << " missing expected address!");
if (_statePost.addressInUse(a.first))
{
- ImportStateOptions addressOptions(true);
+ AccountMask addressOptions(true);
if(_expectedStateOptions.size())
{
try
@@ -244,15 +266,15 @@ void ImportTest::checkExpectedState(State const& _stateExpect, State const& _sta
}
}
- if (addressOptions.m_bHasBalance)
+ if (addressOptions.hasBalance())
CHECK((_stateExpect.balance(a.first) == _statePost.balance(a.first)),
"Check State: " << a.first << ": incorrect balance " << _statePost.balance(a.first) << ", expected " << _stateExpect.balance(a.first));
- if (addressOptions.m_bHasNonce)
+ if (addressOptions.hasNonce())
CHECK((_stateExpect.transactionsFrom(a.first) == _statePost.transactionsFrom(a.first)),
"Check State: " << a.first << ": incorrect nonce " << _statePost.transactionsFrom(a.first) << ", expected " << _stateExpect.transactionsFrom(a.first));
- if (addressOptions.m_bHasStorage)
+ if (addressOptions.hasStorage())
{
unordered_map<u256, u256> stateStorage = _statePost.storage(a.first);
for (auto const& s: _stateExpect.storage(a.first))
@@ -266,14 +288,14 @@ void ImportTest::checkExpectedState(State const& _stateExpect, State const& _sta
"Check State: " << a.first << ": incorrect storage [" << s.first << "] = " << toHex(s.second) << ", expected [" << s.first << "] = " << toHex(stateStorage[s.first]));
}
- if (addressOptions.m_bHasCode)
+ if (addressOptions.hasCode())
CHECK((_stateExpect.code(a.first) == _statePost.code(a.first)),
"Check State: " << a.first << ": incorrect code '" << toHex(_statePost.code(a.first)) << "', expected '" << toHex(_stateExpect.code(a.first)) << "'");
}
}
}
-void ImportTest::exportTest(bytes const& _output, State const& _statePost)
+void ImportTest::exportTest(bytes const& _output)
{
// export output
@@ -291,22 +313,22 @@ void ImportTest::exportTest(bytes const& _output, State const& _statePost)
m_testObject.erase(m_testObject.find("expectOut"));
}
- // export logs
- m_testObject["logs"] = exportLog(_statePost.pending().size() ? _statePost.log(0) : LogEntries());
+ // export logs
+ m_testObject["logs"] = exportLog(m_logs);
// compare expected state with post state
if (m_testObject.count("expect") > 0)
{
- stateOptionsMap stateMap;
+ eth::AccountMaskMap stateMap;
State expectState(OverlayDB(), eth::BaseState::Empty);
importState(m_testObject["expect"].get_obj(), expectState, stateMap);
- checkExpectedState(expectState, _statePost, stateMap, Options::get().checkState ? WhenError::Throw : WhenError::DontThrow);
+ compareStates(expectState, m_statePost, stateMap, Options::get().checkState ? WhenError::Throw : WhenError::DontThrow);
m_testObject.erase(m_testObject.find("expect"));
}
// export post state
- m_testObject["post"] = fillJsonWithState(_statePost);
- m_testObject["postStateRoot"] = toHex(_statePost.rootHash().asBytes());
+ m_testObject["post"] = fillJsonWithState(m_statePost);
+ m_testObject["postStateRoot"] = toHex(m_statePost.rootHash().asBytes());
// export pre state
m_testObject["pre"] = fillJsonWithState(m_statePre);