From 39f5534578749ba4d1994575e891bf8fefa0e0ec Mon Sep 17 00:00:00 2001 From: winsvega Date: Tue, 21 Apr 2015 23:10:55 +0300 Subject: [PATCH] BlockWeight: tests refactoring --- test/TestHelper.cpp | 80 ++++++++++++------- test/TestHelper.h | 8 ++ test/fuzzTesting/createRandomVMTest.cpp | 7 +- .../stTransactionTestFiller.json | 4 +- test/libethereum/blockchain.cpp | 52 +++++------- test/libethereum/transaction.cpp | 2 +- test/libevm/vm.cpp | 61 +++++++------- test/libevm/vm.h | 4 +- test/libweb3jsonrpc/jsonrpc.cpp | 2 +- 9 files changed, 121 insertions(+), 99 deletions(-) diff --git a/test/TestHelper.cpp b/test/TestHelper.cpp index 724bcaf61..8ea70ed1c 100644 --- a/test/TestHelper.cpp +++ b/test/TestHelper.cpp @@ -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 : jsonHex(toInt(str)); } 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"] = jsonHex(_output); // export logs m_TestObject["logs"] = exportLog(_statePost.pending().size() ? _statePost.log(0) : LogEntries()); @@ -344,7 +344,7 @@ void ImportTest::exportTest(bytes const& _output, State const& _statePost) // export post state m_TestObject["post"] = fillJsonWithState(_statePost); - m_TestObject["postStateRoot"] = toHex(_statePost.rootHash().asBytes()); + m_TestObject["postStateRoot"] = jsonHex(_statePost.rootHash().asBytes()); // export pre state m_TestObject["pre"] = fillJsonWithState(m_statePre); @@ -352,29 +352,72 @@ void ImportTest::exportTest(bytes const& _output, State const& _statePost) m_TestObject["transaction"] = makeAllFieldsHex(m_TestObject["transaction"].get_obj()); } +std::string jsonHash(h256 const& _value) { return toString(_value); } +std::string jsonHash(Nonce const& _value) { return toString(_value); } +std::string jsonHash(LogBloom const& _value) { return toString(_value); } +std::string jsonHash(Address const& _value) { return toString(_value); } +std::string jsonHex(bytesConstRef _code) { return "0x" + toHex(_code); } +std::string jsonHex(bytes const& _code) { return "0x" + toHex(_code); } +std::string jsonHex(u256 const& _value, bool _nonempty) +{ + return "0x" + toHex(toCompactBigEndian(_value, _nonempty ? 1 : 0)); +} + +json_spirit::mObject fillJsonWithTransaction(Transaction _txn) +{ + json_spirit::mObject txObject; + txObject["nonce"] = jsonHex(_txn.nonce()); + txObject["data"] = jsonHex(_txn.data()); + txObject["gasLimit"] = jsonHex(_txn.gas()); + txObject["gasPrice"] = jsonHex(_txn.gasPrice()); + txObject["r"] = jsonHex(_txn.signature().r); + txObject["s"] = jsonHex(_txn.signature().s); + txObject["v"] = jsonHex(_txn.signature().v + 27); + txObject["to"] = _txn.isCreation() ? "" : jsonHash(_txn.receiveAddress()); + txObject["value"] = jsonHex(_txn.value()); + 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"] = jsonHex(_state.balance(a.first)); + o["nonce"] = jsonHex(_state.transactionsFrom(a.first)); { json_spirit::mObject store; for (auto const& s: _state.storage(a.first)) - store["0x"+toHex(toCompactBigEndian(s.first))] = "0x"+toHex(toCompactBigEndian(s.second)); + store[jsonHex(s.first)] = jsonHex(s.second); o["storage"] = store; } - o["code"] = "0x" + toHex(_state.code(a.first)); + o["code"] = jsonHex(_state.code(a.first)); 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"] = jsonHash(l.address); + json_spirit::mArray topics; + for (auto const& t: l.topics) + topics.push_back(toString(t)); + o["topics"] = topics; + o["data"] = jsonHex(l.data); + o["bloom"] = jsonHash(l.bloom()); + ret.push_back(o); + } + return ret; +} + u256 toInt(json_spirit::mValue const& _v) { switch (_v.type()) @@ -455,25 +498,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; diff --git a/test/TestHelper.h b/test/TestHelper.h index bf4cdc16e..1e09cf69d 100644 --- a/test/TestHelper.h +++ b/test/TestHelper.h @@ -161,6 +161,14 @@ void userDefinedTest(std::string testTypeFlag, std::function void checkAddresses(mapType& _expectedAddrs, mapType& _resultAddrs) diff --git a/test/fuzzTesting/createRandomVMTest.cpp b/test/fuzzTesting/createRandomVMTest.cpp index abb4e184b..7352d9d98 100644 --- a/test/fuzzTesting/createRandomVMTest.cpp +++ b/test/fuzzTesting/createRandomVMTest.cpp @@ -35,7 +35,8 @@ #include #include #include -#include "../libevm/vm.h" +#include +#include using namespace std; using namespace json_spirit; @@ -199,8 +200,8 @@ void doMyTests(json_spirit::mValue& _v) { o["post"] = mValue(fev.exportState()); o["callcreates"] = fev.exportCallCreates(); - o["out"] = "0x" + toHex(output); - fev.push(o, "gas", gas); + o["out"] = dev::test::jsonHex(output); + o["gas"] = dev::test::jsonHex(gas); o["logs"] = test::exportLog(fev.sub.logs); } } diff --git a/test/libethereum/StateTestsFiller/stTransactionTestFiller.json b/test/libethereum/StateTestsFiller/stTransactionTestFiller.json index 9bd73b540..b28f98d08 100644 --- a/test/libethereum/StateTestsFiller/stTransactionTestFiller.json +++ b/test/libethereum/StateTestsFiller/stTransactionTestFiller.json @@ -838,8 +838,8 @@ "previousHash" : "5e20a0453cecd065ea59c37ac63e079ee08998b6045136a8ce6635c7912ec0b6" }, "expect" : { - "balance" : "0", "0000000000000000000000000000000000000000" : { + "balance" : "0", "storage" : { "0x" : "0x0c", "0x01" : "0x0c", @@ -875,7 +875,7 @@ "c94f5374fce5edbc8e2a8697c15331677e6ebf0b" : { "balance" : "10", - "//" : "gas = 19 going OOG, gas = 20 fine", + "code" : "gas = 19 going OOG, gas = 20 fine", "code" : "{(SSTORE 0 0)(SSTORE 1 0)(SSTORE 2 0)(SSTORE 3 0) (CALL 20000 0 1 0 0 0 0) }", "nonce" : "0", "storage" : { diff --git a/test/libethereum/blockchain.cpp b/test/libethereum/blockchain.cpp index 453cfa298..a70b714fd 100644 --- a/test/libethereum/blockchain.cpp +++ b/test/libethereum/blockchain.cpp @@ -37,8 +37,8 @@ BlockInfo constructBlock(mObject& _o); bytes createBlockRLPFromFields(mObject& _tObj); RLPStream createFullBlockFromHeader(BlockInfo const& _bi, bytes const& _txs = RLPEmptyList, bytes const& _uncles = RLPEmptyList); -mObject writeBlockHeaderToJson(mObject& _o, BlockInfo const& _bi); mArray writeTransactionsToJson(Transactions const& txs); +mObject writeBlockHeaderToJson(mObject& _o, BlockInfo const& _bi); void overwriteBlockHeader(BlockInfo& _current_BlockHeader, mObject& _blObj); BlockInfo constructBlock(mObject& _o); void updatePoW(BlockInfo& _bi); @@ -81,7 +81,7 @@ void doBlockchainTests(json_spirit::mValue& _v, bool _fillin) // create new "genesis" block RLPStream rlpGenesisBlock = createFullBlockFromHeader(biGenesisBlock); biGenesisBlock.verifyInternals(&rlpGenesisBlock.out()); - o["genesisRLP"] = "0x" + toHex(rlpGenesisBlock.out()); + o["genesisRLP"] = jsonHex(rlpGenesisBlock.out()); // construct blockchain TransientDirectory td; @@ -149,7 +149,7 @@ void doBlockchainTests(json_spirit::mValue& _v, bool _fillin) return; } - blObj["rlp"] = "0x" + toHex(state.blockData()); + blObj["rlp"] = jsonHex(state.blockData()); //get valid transactions Transactions txList; @@ -188,7 +188,7 @@ void doBlockchainTests(json_spirit::mValue& _v, bool _fillin) RLPStream block2 = createFullBlockFromHeader(current_BlockHeader, txStream.out(), uncleStream.out()); - blObj["rlp"] = "0x" + toHex(block2.out()); + blObj["rlp"] = jsonHex(block2.out()); if (sha3(RLP(state.blockData())[0].data()) != sha3(RLP(block2.out())[0].data())) cnote << "block header mismatch\n"; @@ -620,17 +620,7 @@ mArray writeTransactionsToJson(Transactions const& txs) mArray txArray; for (auto const& txi: txs) { - mObject txObject; - txObject["nonce"] = toString(txi.nonce()); - txObject["data"] = "0x" + toHex(txi.data()); - txObject["gasLimit"] = toString(txi.gas()); - txObject["gasPrice"] = toString(txi.gasPrice()); - txObject["r"] = "0x" + toString(txi.signature().r); - txObject["s"] = "0x" + toString(txi.signature().s); - txObject["v"] = to_string(txi.signature().v + 27); - txObject["to"] = txi.isCreation() ? "" : toString(txi.receiveAddress()); - txObject["value"] = toString(txi.value()); - txObject = ImportTest::makeAllFieldsHex(txObject); + mObject txObject = fillJsonWithTransaction(txi); txArray.push_back(txObject); } return txArray; @@ -638,22 +628,22 @@ mArray writeTransactionsToJson(Transactions const& txs) mObject writeBlockHeaderToJson(mObject& _o, BlockInfo const& _bi) { - _o["parentHash"] = toString(_bi.parentHash); - _o["uncleHash"] = toString(_bi.sha3Uncles); - _o["coinbase"] = toString(_bi.coinbaseAddress); - _o["stateRoot"] = toString(_bi.stateRoot); - _o["transactionsTrie"] = toString(_bi.transactionsRoot); - _o["receiptTrie"] = toString(_bi.receiptsRoot); - _o["bloom"] = toString(_bi.logBloom); - _o["difficulty"] = "0x" + toHex(toCompactBigEndian(_bi.difficulty), 1); - _o["number"] = "0x" + toHex(toCompactBigEndian(_bi.number), 1); - _o["gasLimit"] = "0x" + toHex(toCompactBigEndian(_bi.gasLimit), 1); - _o["gasUsed"] = "0x" + toHex(toCompactBigEndian(_bi.gasUsed), 1); - _o["timestamp"] = "0x" + toHex(toCompactBigEndian(_bi.timestamp), 1); - _o["extraData"] ="0x" + toHex(_bi.extraData); - _o["mixHash"] = toString(_bi.mixHash); - _o["nonce"] = toString(_bi.nonce); - _o["hash"] = toString(_bi.hash()); + _o["parentHash"] = jsonHash(_bi.parentHash); + _o["uncleHash"] = jsonHash(_bi.sha3Uncles); + _o["coinbase"] = jsonHash(_bi.coinbaseAddress); + _o["stateRoot"] = jsonHash(_bi.stateRoot); + _o["transactionsTrie"] = jsonHash(_bi.transactionsRoot); + _o["receiptTrie"] = jsonHash(_bi.receiptsRoot); + _o["bloom"] = jsonHash(_bi.logBloom); + _o["difficulty"] = jsonHex(_bi.difficulty); + _o["number"] = jsonHex(_bi.number); + _o["gasLimit"] = jsonHex(_bi.gasLimit); + _o["gasUsed"] = jsonHex(_bi.gasUsed); + _o["timestamp"] = jsonHex(_bi.timestamp); + _o["extraData"] = jsonHex(_bi.extraData); + _o["mixHash"] = jsonHash(_bi.mixHash); + _o["nonce"] = jsonHash(_bi.nonce); + _o["hash"] = jsonHash(_bi.hash()); return _o; } diff --git a/test/libethereum/transaction.cpp b/test/libethereum/transaction.cpp index 1a4716725..539f7e066 100644 --- a/test/libethereum/transaction.cpp +++ b/test/libethereum/transaction.cpp @@ -43,7 +43,7 @@ void doTransactionTests(json_spirit::mValue& _v, bool _fillin) //Construct Rlp of the given transaction RLPStream rlpStream = createRLPStreamFromTransactionFields(tObj); - o["rlp"] = "0x" + toHex(rlpStream.out()); + o["rlp"] = jsonHex(rlpStream.out()); try { diff --git a/test/libevm/vm.cpp b/test/libevm/vm.cpp index 43dfa733d..82a17b94b 100644 --- a/test/libevm/vm.cpp +++ b/test/libevm/vm.cpp @@ -83,25 +83,25 @@ void FakeExtVM::reset(u256 _myBalance, u256 _myNonce, map const& _st set(myAddress, _myBalance, _myNonce, _storage, get<3>(addresses[myAddress])); } -void FakeExtVM::push(mObject& o, string const& _n, u256 _v) +/*void FakeExtVM::push(mObject& o, string const& _n, u256 _v) { - o[_n] = "0x" + toHex(toCompactBigEndian(_v)); -} + o[_n] = jsonHex(_v); +}*/ -void FakeExtVM::push(mArray& a, u256 _v) +/*void FakeExtVM::push(mArray& a, u256 _v) { a.push_back(toString(_v)); -} +}*/ mObject FakeExtVM::exportEnv() { mObject ret; - ret["previousHash"] = toString(currentBlock.parentHash); - push(ret, "currentDifficulty", currentBlock.difficulty); - push(ret, "currentTimestamp", currentBlock.timestamp); - ret["currentCoinbase"] = toString(currentBlock.coinbaseAddress); - push(ret, "currentNumber", currentBlock.number); - push(ret, "currentGasLimit", currentBlock.gasLimit); + ret["previousHash"] = jsonHash(currentBlock.parentHash); + ret["currentDifficulty"] = jsonHex(currentBlock.difficulty); + ret["currentTimestamp"] = jsonHex(currentBlock.timestamp); + ret["currentCoinbase"] = jsonHash(currentBlock.coinbaseAddress); + ret["currentNumber"] = jsonHex(currentBlock.number); + ret["currentGasLimit"] = jsonHex(currentBlock.gasLimit); return ret; } @@ -130,16 +130,15 @@ mObject FakeExtVM::exportState() for (auto const& a: addresses) { mObject o; - push(o, "balance", get<0>(a.second)); - push(o, "nonce", get<1>(a.second)); - + o["balance"] = jsonHex(get<0>(a.second)); + o["nonce"] = jsonHex(get<1>(a.second)); { mObject store; for (auto const& s: get<2>(a.second)) - store["0x"+toHex(toCompactBigEndian(s.first))] = "0x"+toHex(toCompactBigEndian(s.second)); + store[jsonHex(s.first)] = jsonHex(s.second); o["storage"] = store; } - o["code"] = "0x" + toHex(get<3>(a.second)); + o["code"] = jsonHex(get<3>(a.second)); ret[toString(a.first)] = o; } @@ -170,14 +169,14 @@ void FakeExtVM::importState(mObject& _object) mObject FakeExtVM::exportExec() { mObject ret; - ret["address"] = toString(myAddress); - ret["caller"] = toString(caller); - ret["origin"] = toString(origin); - push(ret, "value", value); - push(ret, "gasPrice", gasPrice); - push(ret, "gas", gas); - ret["data"] = "0x" + toHex(data); - ret["code"] = "0x" + toHex(code); + ret["address"] = jsonHash(myAddress); + ret["caller"] = jsonHash(caller); + ret["origin"] = jsonHash(origin); + ret["value"] = jsonHex(value); + ret["gasPrice"] = jsonHex(gasPrice); + ret["gas"] = jsonHex(gas); + ret["data"] = jsonHex(data); + ret["code"] = jsonHex(code); return ret; } @@ -218,10 +217,10 @@ mArray FakeExtVM::exportCallCreates() for (Transaction const& tx: callcreates) { mObject o; - o["destination"] = tx.isCreation() ? "" : toString(tx.receiveAddress()); - push(o, "gasLimit", tx.gas()); - push(o, "value", tx.value()); - o["data"] = "0x" + toHex(tx.data()); + o["destination"] = tx.isCreation() ? "" : jsonHash(tx.receiveAddress()); + o["gasLimit"] = jsonHex(tx.gas()); + o["value"] = jsonHex(tx.value()); + o["data"] = jsonHex(tx.data()); ret.push_back(o); } return ret; @@ -292,7 +291,7 @@ eth::OnOpFunc FakeExtVM::simpleTrace() o_step.push_back(Pair("storage", storage)); o_step.push_back(Pair("depth", to_string(ext.depth))); o_step.push_back(Pair("gas", (string)vm.gas())); - o_step.push_back(Pair("address", "0x" + toString(ext.myAddress ))); + o_step.push_back(Pair("address", jsonHash(ext.myAddress ))); o_step.push_back(Pair("step", steps )); o_step.push_back(Pair("pc", (int)vm.curPC())); o_step.push_back(Pair("opcode", instructionInfo(inst).name )); @@ -400,8 +399,8 @@ void doVMTests(json_spirit::mValue& v, bool _fillin) } o["callcreates"] = fev.exportCallCreates(); - o["out"] = "0x" + toHex(output); - fev.push(o, "gas", gas); + o["out"] = jsonHex(output); + o["gas"] = jsonHex(gas); o["logs"] = exportLog(fev.sub.logs); } } diff --git a/test/libevm/vm.h b/test/libevm/vm.h index 2bfc68d56..f78a5e0bb 100644 --- a/test/libevm/vm.h +++ b/test/libevm/vm.h @@ -64,8 +64,8 @@ public: void setContract(Address _myAddress, u256 _myBalance, u256 _myNonce, std::map const& _storage, bytes const& _code); void set(Address _a, u256 _myBalance, u256 _myNonce, std::map const& _storage, bytes const& _code); void reset(u256 _myBalance, u256 _myNonce, std::map const& _storage); - void push(json_spirit::mObject& o, std::string const& _n, u256 _v); - void push(json_spirit::mArray& a, u256 _v); + //void push(json_spirit::mObject& o, std::string const& _n, u256 _v); + //void push(json_spirit::mArray& a, u256 _v); u256 doPosts(); json_spirit::mObject exportEnv(); void importEnv(json_spirit::mObject& _o); diff --git a/test/libweb3jsonrpc/jsonrpc.cpp b/test/libweb3jsonrpc/jsonrpc.cpp index 97ba17ccf..e2b7860ab 100644 --- a/test/libweb3jsonrpc/jsonrpc.cpp +++ b/test/libweb3jsonrpc/jsonrpc.cpp @@ -75,7 +75,7 @@ struct Setup string fromAscii(string _s) { bytes b = asBytes(_s); - return "0x" + toHex(b); + return jsonHex(b); } BOOST_FIXTURE_TEST_SUITE(environment, Setup)