Merge pull request #1655 from winsvega/blockweight

BlockWeight: blockchain test refactoring
This commit is contained in:
Gav Wood 2015-04-27 23:08:48 +02:00
commit 5e0eb4bae1
2 changed files with 41 additions and 28 deletions

View File

@ -137,7 +137,7 @@ json_spirit::mObject& ImportTest::makeAllFieldsHex(json_spirit::mObject& _o)
str = value.get_str();
else continue;
_o[key] = (str.substr(0, 2) == "0x") ? str : "0x" + toHex(toCompactBigEndian(toInt(str), 1));
_o[key] = (str.substr(0, 2) == "0x") ? str : toCompactHex(toInt(str), HexPrefix::Add);
}
return _o;
}
@ -327,7 +327,7 @@ void ImportTest::checkExpectedState(State const& _stateExpect, State const& _sta
void ImportTest::exportTest(bytes const& _output, State const& _statePost)
{
// export output
m_TestObject["out"] = "0x" + toHex(_output);
m_TestObject["out"] = toHex(_output, 2, HexPrefix::Add);
// export logs
m_TestObject["logs"] = exportLog(_statePost.pending().size() ? _statePost.log(0) : LogEntries());
@ -352,29 +352,60 @@ void ImportTest::exportTest(bytes const& _output, State const& _statePost)
m_TestObject["transaction"] = makeAllFieldsHex(m_TestObject["transaction"].get_obj());
}
json_spirit::mObject fillJsonWithTransaction(Transaction _txn)
{
json_spirit::mObject txObject;
txObject["nonce"] = toCompactHex(_txn.nonce(), HexPrefix::Add);
txObject["data"] = toHex(_txn.data(), 2, HexPrefix::Add);
txObject["gasLimit"] = toCompactHex(_txn.gas(), HexPrefix::Add);
txObject["gasPrice"] = toCompactHex(_txn.gasPrice(), HexPrefix::Add);
txObject["r"] = toCompactHex(_txn.signature().r, HexPrefix::Add);
txObject["s"] = toCompactHex(_txn.signature().s, HexPrefix::Add);
txObject["v"] = toCompactHex(_txn.signature().v + 27, HexPrefix::Add);
txObject["to"] = _txn.isCreation() ? "" : toString(_txn.receiveAddress());
txObject["value"] = toCompactHex(_txn.value(), HexPrefix::Add);
return txObject;
}
json_spirit::mObject fillJsonWithState(State _state)
{
// export pre state
json_spirit::mObject oState;
for (auto const& a: _state.addresses())
{
json_spirit::mObject o;
o["balance"] = "0x" + toHex(toCompactBigEndian(_state.balance(a.first), 1));
o["nonce"] = "0x" + toHex(toCompactBigEndian(_state.transactionsFrom(a.first), 1));
o["balance"] = toCompactHex(_state.balance(a.first), HexPrefix::Add);
o["nonce"] = toCompactHex(_state.transactionsFrom(a.first), HexPrefix::Add);
{
json_spirit::mObject store;
for (auto const& s: _state.storage(a.first))
store["0x"+toHex(toCompactBigEndian(s.first))] = "0x"+toHex(toCompactBigEndian(s.second));
store[toCompactHex(s.first, HexPrefix::Add)] = toCompactHex(s.second, HexPrefix::Add);
o["storage"] = store;
}
o["code"] = "0x" + toHex(_state.code(a.first));
o["code"] = toHex(_state.code(a.first), 2, HexPrefix::Add);
oState[toString(a.first)] = o;
}
return oState;
}
json_spirit::mArray exportLog(eth::LogEntries _logs)
{
json_spirit::mArray ret;
if (_logs.size() == 0) return ret;
for (LogEntry const& l: _logs)
{
json_spirit::mObject o;
o["address"] = toString(l.address);
json_spirit::mArray topics;
for (auto const& t: l.topics)
topics.push_back(toString(t));
o["topics"] = topics;
o["data"] = toHex(l.data, 2, HexPrefix::Add);
o["bloom"] = toString(l.bloom());
ret.push_back(o);
}
return ret;
}
u256 toInt(json_spirit::mValue const& _v)
{
switch (_v.type())
@ -455,25 +486,6 @@ LogEntries importLog(json_spirit::mArray& _a)
return logEntries;
}
json_spirit::mArray exportLog(eth::LogEntries _logs)
{
json_spirit::mArray ret;
if (_logs.size() == 0) return ret;
for (LogEntry const& l: _logs)
{
json_spirit::mObject o;
o["address"] = toString(l.address);
json_spirit::mArray topics;
for (auto const& t: l.topics)
topics.push_back(toString(t));
o["topics"] = topics;
o["data"] = "0x" + toHex(l.data);
o["bloom"] = toString(l.bloom());
ret.push_back(o);
}
return ret;
}
void checkOutput(bytes const& _output, json_spirit::mObject& _o)
{
int j = 0;

View File

@ -161,6 +161,7 @@ void userDefinedTest(std::string testTypeFlag, std::function<void(json_spirit::m
RLPStream createRLPStreamFromTransactionFields(json_spirit::mObject& _tObj);
eth::LastHashes lastHashes(u256 _currentBlockNumber);
json_spirit::mObject fillJsonWithState(eth::State _state);
json_spirit::mObject fillJsonWithTransaction(eth::Transaction _txn);
template<typename mapType>
void checkAddresses(mapType& _expectedAddrs, mapType& _resultAddrs)