diff options
-rw-r--r-- | TestHelper.cpp | 27 | ||||
-rw-r--r-- | TestHelper.h | 4 | ||||
-rw-r--r-- | blFirstTestFiller.json | 32 | ||||
-rw-r--r-- | blValidBlockTestFiller.json | 44 | ||||
-rw-r--r-- | block.cpp | 363 | ||||
-rw-r--r-- | transaction.cpp | 2 |
6 files changed, 471 insertions, 1 deletions
diff --git a/TestHelper.cpp b/TestHelper.cpp index 8a00a546..43e6106d 100644 --- a/TestHelper.cpp +++ b/TestHelper.cpp @@ -504,6 +504,33 @@ void executeTests(const string& _name, const string& _testPathAppendix, std::fun } } +bytes createTransactionFromFields(json_spirit::mObject& _tObj) +{ + BOOST_REQUIRE(_tObj.count("data") > 0); + BOOST_REQUIRE(_tObj.count("value") > 0); + BOOST_REQUIRE(_tObj.count("gasPrice") > 0); + BOOST_REQUIRE(_tObj.count("gasLimit") > 0); + BOOST_REQUIRE(_tObj.count("nonce")> 0); + BOOST_REQUIRE(_tObj.count("to") > 0); + + BOOST_REQUIRE(_tObj.count("v") > 0); + BOOST_REQUIRE(_tObj.count("r") > 0); + BOOST_REQUIRE(_tObj.count("s") > 0); + + //Construct Rlp of the given transaction + RLPStream rlpStream; + rlpStream.appendList(9); + rlpStream << bigint(_tObj["nonce"].get_str()) << bigint(_tObj["gasPrice"].get_str()) << bigint(_tObj["gasLimit"].get_str()); + if (_tObj["to"].get_str().empty()) + rlpStream << ""; + else + rlpStream << Address(_tObj["to"].get_str()); + rlpStream << bigint(_tObj["value"].get_str()) << importData(_tObj); + rlpStream << bigint(_tObj["v"].get_str()) << bigint(_tObj["r"].get_str()) << bigint(_tObj["s"].get_str()); + + return rlpStream.out(); +} + void processCommandLineOptions() { diff --git a/TestHelper.h b/TestHelper.h index ae6ea20c..9ac1f0d4 100644 --- a/TestHelper.h +++ b/TestHelper.h @@ -45,7 +45,7 @@ namespace test class ImportTest { public: - ImportTest() = default; + ImportTest(json_spirit::mObject& _o) : m_TestObject(_o) {} ImportTest(json_spirit::mObject& _o, bool isFiller); // imports @@ -53,6 +53,7 @@ public: void importState(json_spirit::mObject& _o, eth::State& _state); void importTransaction(json_spirit::mObject& _o); void exportTest(bytes _output, eth::State& _statePost); + std::map<Address, eth::Account> getStateMap(eth::State& _state){return _state.m_cache;} eth::State m_statePre; eth::State m_statePost; @@ -79,6 +80,7 @@ void checkCallCreates(eth::Transactions _resultCallCreates, eth::Transactions _e void executeTests(const std::string& _name, const std::string& _testPathAppendix, std::function<void(json_spirit::mValue&, bool)> doTests); std::string getTestPath(); void userDefinedTest(std::string testTypeFlag, std::function<void(json_spirit::mValue&, bool)> doTests); +bytes createTransactionFromFields(json_spirit::mObject& _tObj); void processCommandLineOptions(); eth::LastHashes lastHashes(u256 _currentBlockNumber); diff --git a/blFirstTestFiller.json b/blFirstTestFiller.json new file mode 100644 index 00000000..d4e6d109 --- /dev/null +++ b/blFirstTestFiller.json @@ -0,0 +1,32 @@ +{ + "firstBlockTest" : { + "block" : { + "parentHash": "0xefb4db878627027c81b3bb1c7dd3a18dae3914a49cdd24a3e40ab3bbfbb240c5", + "uncleHash": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347", + "coinbase": "0x8888f1f195afa192cfee860698584c030f4c9db1", + "stateRoot": "0xf99eb1626cfa6db435c0836235942d7ccaa935f1ae247d3f1c21e495685f903a", + "transactionsTrie": "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421", + "receiptTrie": "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421", + "bloom": "00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", + "difficulty": "023101", + "number": "62", + "gasLimit": "0x0dddb6", + "gasUsed": "100", + "timestamp": "0x54c98c81", + "extraData": "42", + "nonce": "0x498e88f5c14b0b60d6e14ce9c6cc958cbe16a1df8dd90210e50d2d77562a348d" + }, + "pre" : {}, + "transactions": [{ + "nonce": "0", + "gasPrice": "0x09184e72a000", + "gasLimit": "0x0f3e6f", + "to": "", + "value": "", + "data": "0x60056013565b6101918061001d6000396000f35b3360008190555056006001600060e060020a6000350480630a874df61461003a57806341c0e1b514610058578063a02b161e14610066578063dbbdf0831461007757005b610045600435610149565b80600160a060020a031660005260206000f35b610060610161565b60006000f35b6100716004356100d4565b60006000f35b61008560043560243561008b565b60006000f35b600054600160a060020a031632600160a060020a031614156100ac576100b1565b6100d0565b8060018360005260205260406000208190555081600060005260206000a15b5050565b600054600160a060020a031633600160a060020a031614158015610118575033600160a060020a0316600182600052602052604060002054600160a060020a031614155b61012157610126565b610146565b600060018260005260205260406000208190555080600060005260206000a15b50565b60006001826000526020526040600020549050919050565b600054600160a060020a031633600160a060020a0316146101815761018f565b600054600160a060020a0316ff5b56", + "v": "0x1b", + "r": "0xd4287e915ebac7a8af390560fa53c8f0b7f13802ba0393d7afa5823c2560ca89", + "s": "0xae75db31a34f7e386ad459646de98ec3a1c88cc91b11620b4ffd86871f579942" + }] + } +} diff --git a/blValidBlockTestFiller.json b/blValidBlockTestFiller.json new file mode 100644 index 00000000..75ad8452 --- /dev/null +++ b/blValidBlockTestFiller.json @@ -0,0 +1,44 @@ +{ + "lowGasLimitBoundary" : { + "genesisBlockHeader" : { + "bloom" : "00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", + "coinbase" : "0x8888f1f195afa192cfee860698584c030f4c9db1", + "difficulty" : "10000", + "extraData" : "42", + "gasLimit" : "100000", + "gasUsed" : "100", + "nonce" : "0x498e88f5c14b0b60d6e14ce9c6cc958cbe16a1df8dd90210e50d2d77562a348d", + "number" : "0", + "parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000", + "receiptTrie" : "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421", + "stateRoot" : "0xf99eb1626cfa6db435c0836235942d7ccaa935f1ae247d3f1c21e495685f903a", + "timestamp" : "0x54c98c81", + "transactionsTrie" : "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421", + "uncleHash" : "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" + }, + "pre" : { + "a94f5374fce5edbc8e2a8697c15331677e6ebf0b" : { + "balance" : "10000000000", + "nonce" : 0, + "code" : "", + "storage": {} + } + }, + "transactions" : [ + { + "data" : "", + "gasLimit" : "850", + "gasPrice" : "1", + "nonce" : "0", + "r" : "0x48b55bfa915ac795c431978d8a6a992b628d557da5ff759b307d495a36649353", + "s" : "0xefffd310ac743f371de3b9f7f9cb56c0b28ad43601b4ab949f53faa07bd2c804", + "to" : "095e7baea6a6c7c4c2dfeb977efac326af552d87", + "v" : "27", + "value" : "10" + } + ], + "uncleHeaders" : [ + ] + } +} + diff --git a/block.cpp b/block.cpp new file mode 100644 index 00000000..f72e7001 --- /dev/null +++ b/block.cpp @@ -0,0 +1,363 @@ +/* + This file is part of cpp-ethereum. + + cpp-ethereum is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + cpp-ethereum is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>. +*/ +/** @file block.cpp + * @author Christoph Jentzsch <cj@ethdev.com> + * @date 2015 + * block test functions. + */ + +#include <libethereum/CanonBlockChain.h> +#include "TestHelper.h" + +using namespace std; +using namespace json_spirit; +using namespace dev; +using namespace dev::eth; + +namespace dev { namespace test { + +bytes createBlockRLPFromFields(mObject& _tObj) +{ + BOOST_REQUIRE(_tObj.count("parentHash") > 0); + BOOST_REQUIRE(_tObj.count("uncleHash") > 0); + BOOST_REQUIRE(_tObj.count("coinbase") > 0); + BOOST_REQUIRE(_tObj.count("stateRoot") > 0); + BOOST_REQUIRE(_tObj.count("transactionsTrie")> 0); + BOOST_REQUIRE(_tObj.count("receiptTrie") > 0); + BOOST_REQUIRE(_tObj.count("bloom") > 0); + BOOST_REQUIRE(_tObj.count("difficulty") > 0); + BOOST_REQUIRE(_tObj.count("number") > 0); + BOOST_REQUIRE(_tObj.count("gasLimit")> 0); + BOOST_REQUIRE(_tObj.count("gasUsed") > 0); + BOOST_REQUIRE(_tObj.count("timestamp") > 0); + BOOST_REQUIRE(_tObj.count("extraData") > 0); + BOOST_REQUIRE(_tObj.count("nonce") > 0); + + // construct RLP of the given block + cout << "done with require\n"; + RLPStream rlpStream; + rlpStream.appendList(14); + cout << "increate aha1\n"; + rlpStream << h256(_tObj["parentHash"].get_str()) << h256(_tObj["uncleHash"].get_str()) << Address(_tObj["coinbase"].get_str()); + rlpStream << h256(_tObj["stateRoot"].get_str()) << h256(_tObj["transactionsTrie"].get_str()) << Address(_tObj["receiptTrie"].get_str()); + rlpStream << LogBloom(_tObj["bloom"].get_str()) << u256(_tObj["difficulty"].get_str()) << u256(_tObj["number"].get_str()); + rlpStream << u256(_tObj["gasLimit"].get_str()) << u256(_tObj["gasUsed"].get_str()) << u256(_tObj["timestamp"].get_str()); + rlpStream << importByteArray(_tObj["extraData"].get_str()) << h256(_tObj["nonce"].get_str()); + + return rlpStream.out(); +} + +void doBlockTests(json_spirit::mValue& _v, bool _fillin) +{ + for (auto& i: _v.get_obj()) + { + cerr << i.first << endl; + mObject& o = i.second.get_obj(); + + if (_fillin == false) + { + BOOST_REQUIRE(o.count("genesisBlockHeader") > 0); + + // construct RLP of the genesis block + bytes blockRLP = createBlockRLPFromFields(o["genesisBlockHeader"].get_obj()); + RLP myRLP(blockRLP); + BlockInfo blockFromFields; + + try + { + blockFromFields.populateFromHeader(myRLP, false); + } + catch (Exception const& _e) + { + cnote << "block construction did throw an exception: " << diagnostic_information(_e); + BOOST_ERROR("Failed block construction Test with Exception: " << _e.what()); + return; + } + catch (std::exception const& _e) + { + cnote << "block construction did throw an exception: " << _e.what(); + BOOST_ERROR("Failed block construction Test with Exception: " << _e.what()); + return; + } + catch(...) + { + cnote << "block construction did throw an unknown exception\n"; + return; + } + + BOOST_REQUIRE(o.count("pre") > 0); + + ImportTest importer(o["pre"].get_obj()); + State theState(Address(), OverlayDB(), BaseState::Empty); + importer.importState(o["pre"].get_obj(), theState); + + // commit changes to DB + theState.commit(); + + BOOST_CHECK_MESSAGE(blockFromFields.stateRoot == theState.rootHash(), "root hash do not match"); + cout << "root hash - no fill in : " << theState.rootHash() << endl; + cout << "root hash - no fill in - from block: " << blockFromFields.stateRoot << endl; + + // create new "genesis" block + RLPStream rlpStream; + blockFromFields.streamRLP(rlpStream, WithNonce); + + RLPStream block(3); + block.appendRaw(rlpStream.out()); + block.appendRaw(RLPEmptyList); + block.appendRaw(RLPEmptyList); + + blockFromFields.verifyInternals(&block.out()); + + // construc blockchain + BlockChain bc(block.out(), string(), true); + + try + { + theState.sync(bc); + bytes blockRLP = importByteArray(o["rlp"].get_str()); + cout << "import block rlp\n"; + bc.import(blockRLP, theState.db()); + cout << "sync with the state\n"; + theState.sync(bc); + } + // if exception is thrown, RLP is invalid and not blockHeader, Transaction list, and Uncle list should be given + catch (Exception const& _e) + { + cnote << "state sync or block import did throw an exception: " << diagnostic_information(_e); + BOOST_CHECK(o.count("blockHeader") == 0); + BOOST_CHECK(o.count("transactions") == 0); + BOOST_CHECK(o.count("uncleHeaders") == 0); + } + catch (std::exception const& _e) + { + cnote << "state sync or block import did throw an exception: " << _e.what(); + BOOST_CHECK(o.count("blockHeader") == 0); + BOOST_CHECK(o.count("transactions") == 0); + BOOST_CHECK(o.count("uncleHeaders") == 0); + } + catch(...) + { + cnote << "state sync or block import did throw an exception\n"; + BOOST_CHECK(o.count("blockHeader") == 0); + BOOST_CHECK(o.count("transactions") == 0); + BOOST_CHECK(o.count("uncleHeaders") == 0); + } + + + // if yes, check parameters in blockHeader + // check transaction list + // check uncle list + + BOOST_REQUIRE(o.count("blockHeader") > 0); + + mObject tObj = o["blockHeader"].get_obj(); + BlockInfo blockHeaderFromFields; + const bytes rlpBytesBlockHeader = createBlockRLPFromFields(tObj); + RLP blockHeaderRLP(rlpBytesBlockHeader); + blockHeaderFromFields.populateFromHeader(blockHeaderRLP, false); + + BlockInfo blockFromRlp = bc.info(); + + cout << "root hash - no fill in - from state : " << theState.rootHash() << endl; + cout << " hash - no fill in - from rlp : " << blockFromRlp.hash << endl; + cout << " hash - no fill in - from block: " << blockHeaderFromFields.hash << endl; + + //Check the fields restored from RLP to original fields + BOOST_CHECK_MESSAGE(blockHeaderFromFields.headerHash() == blockFromRlp.headerHash(), "hash in given RLP not matching the block hash!"); + BOOST_CHECK_MESSAGE(blockHeaderFromFields.parentHash == blockFromRlp.parentHash, "parentHash in given RLP not matching the block parentHash!"); + BOOST_CHECK_MESSAGE(blockHeaderFromFields.sha3Uncles == blockFromRlp.sha3Uncles, "sha3Uncles in given RLP not matching the block sha3Uncles!"); + BOOST_CHECK_MESSAGE(blockHeaderFromFields.coinbaseAddress == blockFromRlp.coinbaseAddress,"coinbaseAddress in given RLP not matching the block coinbaseAddress!"); + BOOST_CHECK_MESSAGE(blockHeaderFromFields.stateRoot == blockFromRlp.stateRoot, "stateRoot in given RLP not matching the block stateRoot!"); + BOOST_CHECK_MESSAGE(blockHeaderFromFields.transactionsRoot == blockFromRlp.transactionsRoot, "transactionsRoot in given RLP not matching the block transactionsRoot!"); + BOOST_CHECK_MESSAGE(blockHeaderFromFields.logBloom == blockFromRlp.logBloom, "logBloom in given RLP not matching the block logBloom!"); + BOOST_CHECK_MESSAGE(blockHeaderFromFields.difficulty == blockFromRlp.difficulty, "difficulty in given RLP not matching the block difficulty!"); + BOOST_CHECK_MESSAGE(blockHeaderFromFields.number == blockFromRlp.number, "number in given RLP not matching the block number!"); + BOOST_CHECK_MESSAGE(blockHeaderFromFields.gasLimit == blockFromRlp.gasLimit,"gasLimit in given RLP not matching the block gasLimit!"); + BOOST_CHECK_MESSAGE(blockHeaderFromFields.gasUsed == blockFromRlp.gasUsed, "gasUsed in given RLP not matching the block gasUsed!"); + BOOST_CHECK_MESSAGE(blockHeaderFromFields.timestamp == blockFromRlp.timestamp, "timestamp in given RLP not matching the block timestamp!"); + BOOST_CHECK_MESSAGE(blockHeaderFromFields.extraData == blockFromRlp.extraData, "extraData in given RLP not matching the block extraData!"); + BOOST_CHECK_MESSAGE(blockHeaderFromFields.nonce == blockFromRlp.nonce, "nonce in given RLP not matching the block nonce!"); + + BOOST_CHECK_MESSAGE(blockHeaderFromFields == blockFromRlp, "However, blockHeaderFromFields != blockFromRlp!"); + + } + else + { + BOOST_REQUIRE(o.count("genesisBlockHeader") > 0); + + // construct RLP of the genesis block + bytes blockRLP = createBlockRLPFromFields(o["genesisBlockHeader"].get_obj()); + RLP myRLP(blockRLP); + BlockInfo blockFromFields; + + try + { + blockFromFields.populateFromHeader(myRLP, false); + } + catch (Exception const& _e) + { + cnote << "block construction did throw an exception: " << diagnostic_information(_e); + BOOST_ERROR("Failed block construction Test with Exception: " << _e.what()); + return; + } + catch (std::exception const& _e) + { + cnote << "block construction did throw an exception: " << _e.what(); + BOOST_ERROR("Failed block construction Test with Exception: " << _e.what()); + return; + } + catch(...) + { + cnote << "block construction did throw an unknown exception\n"; + return; + } + + BOOST_REQUIRE(o.count("pre") > 0); + + ImportTest importer(o["pre"].get_obj()); + State theState(Address(), OverlayDB(), BaseState::Empty); + importer.importState(o["pre"].get_obj(), theState); + + // commit changes to DB + theState.commit(); + + + // fillin specific --- start + + BOOST_REQUIRE(o.count("transactions") > 0); + + TransactionQueue txs; + + for (auto const& txObj: o["transactions"].get_array()) + { + mObject tx = txObj.get_obj(); + BOOST_REQUIRE(tx.count("nonce") > 0); + BOOST_REQUIRE(tx.count("gasPrice") > 0); + BOOST_REQUIRE(tx.count("gasLimit") > 0); + BOOST_REQUIRE(tx.count("to") > 0); + BOOST_REQUIRE(tx.count("value") > 0); + BOOST_REQUIRE(tx.count("v") > 0); + BOOST_REQUIRE(tx.count("r") > 0); + BOOST_REQUIRE(tx.count("s") > 0); + BOOST_REQUIRE(tx.count("data") > 0); + + if (!txs.attemptImport(createTransactionFromFields(tx))) + cnote << "failed importing transaction\n"; + } + + // update stateRootHash + blockFromFields.stateRoot = theState.rootHash(); + cout << "root hash1: " << theState.rootHash() << endl; + + // find new valid nonce + ProofOfWork pow; + MineInfo ret; + tie(ret, blockFromFields.nonce) = pow.mine(blockFromFields.headerHash(WithoutNonce), blockFromFields.difficulty, 1000, true, false); + //---stop + + //update genesis block in json file + o["genesisBlockHeader"].get_obj()["stateRoot"] = toString(blockFromFields.stateRoot); + o["genesisBlockHeader"].get_obj()["nonce"] = toString(blockFromFields.nonce); + + + // create new "genesis" block + RLPStream rlpStream; + blockFromFields.streamRLP(rlpStream, WithNonce); + + RLPStream block(3); + block.appendRaw(rlpStream.out()); + block.appendRaw(RLPEmptyList); + block.appendRaw(RLPEmptyList); + + blockFromFields.verifyInternals(&block.out()); + + // construct blockchain + BlockChain bc(block.out(), string(), true); + + + + try + { + theState.sync(bc); + cout << "root hash2: " << theState.rootHash() << endl; + theState.sync(bc,txs); + cout << "root hash3: " << theState.rootHash() << endl; + theState.commitToMine(bc); + cout << "root hash4: " << theState.rootHash() << endl; + MineInfo info; + for (info.completed = false; !info.completed; info = theState.mine()) {} + cout << "root hash5: " << theState.rootHash() << endl; + theState.completeMine(); + cout << "root hash6: " << theState.rootHash() << endl; + } + catch (Exception const& _e) + { + cnote << "state sync or mining did throw an exception: " << diagnostic_information(_e); + } + catch (std::exception const& _e) + { + cnote << "state sync or mining did throw an exception: " << _e.what(); + } + + o["rlp"] = "0x" + toHex(theState.blockData()); + + // write block header + + mObject oBlockHeader; + BlockInfo current_BlockHeader = theState.info(); + oBlockHeader["parentHash"] = toString(current_BlockHeader.parentHash); + oBlockHeader["uncleHash"] = toString(current_BlockHeader.sha3Uncles); + oBlockHeader["coinbase"] = toString(current_BlockHeader.coinbaseAddress); + oBlockHeader["stateRoot"] = toString(current_BlockHeader.stateRoot); + oBlockHeader["transactionsTrie"] = toString(current_BlockHeader.transactionsRoot); + oBlockHeader["receiptTrie"] = toString(current_BlockHeader.receiptsRoot); + oBlockHeader["bloom"] = toString(current_BlockHeader.logBloom); + oBlockHeader["difficulty"] = toString(current_BlockHeader.difficulty); + oBlockHeader["number"] = toString(current_BlockHeader.number); + oBlockHeader["gasLimit"] = toString(current_BlockHeader.gasLimit); + oBlockHeader["gasUsed"] = toString(current_BlockHeader.gasUsed); + oBlockHeader["timestamp"] = toString(current_BlockHeader.timestamp); + oBlockHeader["extraData"] = toHex(current_BlockHeader.extraData); + oBlockHeader["nonce"] = toString(current_BlockHeader.nonce); + + o["blockHeader"] = oBlockHeader; + + // write uncle list + + mArray aUncleList; // as of now, our parent is always the genesis block, so we can not have uncles. + o["uncleHeaders"] = aUncleList; + } + } +} + +} }// Namespace Close + + +BOOST_AUTO_TEST_SUITE(BlockTests) + +BOOST_AUTO_TEST_CASE(blValidBlockTest) +{ + dev::test::executeTests("blValidBlockTest", "/BlockTests", dev::test::doBlockTests); +} + +BOOST_AUTO_TEST_CASE(userDefinedFileBl) +{ + dev::test::userDefinedTest("--bltest", dev::test::doBlockTests); +} + +BOOST_AUTO_TEST_SUITE_END() diff --git a/transaction.cpp b/transaction.cpp index b51494d8..6a1aa0d8 100644 --- a/transaction.cpp +++ b/transaction.cpp @@ -21,6 +21,7 @@ */ #include "TestHelper.h" + using namespace std; using namespace json_spirit; using namespace dev; @@ -140,6 +141,7 @@ void doTransactionTests(json_spirit::mValue& _v, bool _fillin) } }//for }//doTransactionTests + } }// Namespace Close |