diff options
author | CJentzsch <jentzsch.software@gmail.com> | 2015-02-11 15:37:54 +0800 |
---|---|---|
committer | CJentzsch <jentzsch.software@gmail.com> | 2015-02-11 15:37:54 +0800 |
commit | 4578732556684b3c7f24793d0459e2e8b40fe458 (patch) | |
tree | 30ab2c0cbe2219bf9d86a6182545efc90639fdb0 | |
parent | 32a8f7faebb60b165576f5f0f7e1fa8635cd72b6 (diff) | |
download | dexon-solidity-4578732556684b3c7f24793d0459e2e8b40fe458.tar.gz dexon-solidity-4578732556684b3c7f24793d0459e2e8b40fe458.tar.zst dexon-solidity-4578732556684b3c7f24793d0459e2e8b40fe458.zip |
validate block (the not fill tests path)
-rw-r--r-- | blValidBlockTestFiller.json | 27 | ||||
-rw-r--r-- | block.cpp | 315 |
2 files changed, 179 insertions, 163 deletions
diff --git a/blValidBlockTestFiller.json b/blValidBlockTestFiller.json index 8b951b17..75ad8452 100644 --- a/blValidBlockTestFiller.json +++ b/blValidBlockTestFiller.json @@ -1,37 +1,21 @@ { - "validBlock" : { - "block" : { + "lowGasLimitBoundary" : { + "genesisBlockHeader" : { "bloom" : "00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", "coinbase" : "0x8888f1f195afa192cfee860698584c030f4c9db1", - "difficulty" : "1024", + "difficulty" : "10000", "extraData" : "42", - "gasLimit" : "0x0dddb6", + "gasLimit" : "100000", "gasUsed" : "100", "nonce" : "0x498e88f5c14b0b60d6e14ce9c6cc958cbe16a1df8dd90210e50d2d77562a348d", "number" : "0", - "parentHash" : "0xefb4db878627027c81b3bb1c7dd3a18dae3914a49cdd24a3e40ab3bbfbb240c5", + "parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000", "receiptTrie" : "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421", "stateRoot" : "0xf99eb1626cfa6db435c0836235942d7ccaa935f1ae247d3f1c21e495685f903a", "timestamp" : "0x54c98c81", "transactionsTrie" : "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421", "uncleHash" : "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" }, - "blockHeader" : { - "bloom" : "00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", - "coinbase" : "0000000000000000000000000000000000000000", - "difficulty" : "130944", - "extraData" : "", - "gasLimit" : "999023", - "gasUsed" : "4803", - "nonce" : "62778f62183098b7cc8d221fc199654e613ca47b85696f940de3bd2bb6a2a54e", - "number" : "1", - "parentHash" : "c9cb614fddd89b3bc6e2f0ed1f8e58e8a0d826612a607a6151be6f39c991a941", - "receiptTrie" : "8fd35225dd530f30dc39719f9791583309b5889ad79bff0d31e9b1eb12f55000", - "stateRoot" : "b3ef9fe736086bdf1b3cd235f68097aab5f4c6e40d57f417d39c2cc6fb67f6c7", - "timestamp" : "1423490430", - "transactionsTrie" : "2cb4068eb8ccc9124426a7ed5c445b1353c997fc419031254ff7e2768c4dcd7f", - "uncleHash" : "1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" - }, "pre" : { "a94f5374fce5edbc8e2a8697c15331677e6ebf0b" : { "balance" : "10000000000", @@ -40,7 +24,6 @@ "storage": {} } }, - "rlp" : "0xf9033bf9012fa0c9cb614fddd89b3bc6e2f0ed1f8e58e8a0d826612a607a6151be6f39c991a941a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347940000000000000000000000000000000000000000a0b3ef9fe736086bdf1b3cd235f68097aab5f4c6e40d57f417d39c2cc6fb67f6c7a02cb4068eb8ccc9124426a7ed5c445b1353c997fc419031254ff7e2768c4dcd7fa08fd35225dd530f30dc39719f9791583309b5889ad79bff0d31e9b1eb12f55000b840000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008301ff8001830f3e6f8212c38454d8bd7e80a062778f62183098b7cc8d221fc199654e613ca47b85696f940de3bd2bb6a2a54ef90205f90202808609184e72a000830f3e6f8080b901ae60056013565b6101918061001d6000396000f35b3360008190555056006001600060e060020a6000350480630a874df61461003a57806341c0e1b514610058578063a02b161e14610066578063dbbdf0831461007757005b610045600435610149565b80600160a060020a031660005260206000f35b610060610161565b60006000f35b6100716004356100d4565b60006000f35b61008560043560243561008b565b60006000f35b600054600160a060020a031632600160a060020a031614156100ac576100b1565b6100d0565b8060018360005260205260406000208190555081600060005260206000a15b5050565b600054600160a060020a031633600160a060020a031614158015610118575033600160a060020a0316600182600052602052604060002054600160a060020a031614155b61012157610126565b610146565b600060018260005260205260406000208190555080600060005260206000a15b50565b60006001826000526020526040600020549050919050565b600054600160a060020a031633600160a060020a0316146101815761018f565b600054600160a060020a0316ff5b561ba0d4287e915ebac7a8af390560fa53c8f0b7f13802ba0393d7afa5823c2560ca89a0ae75db31a34f7e386ad459646de98ec3a1c88cc91b11620b4ffd86871f579942c0", "transactions" : [ { "data" : "", @@ -70,86 +70,174 @@ void doBlockTests(json_spirit::mValue& _v, bool _fillin) if (_fillin == false) { - // TODO - - // BOOST_REQUIRE(o.count("rlp") > 0); - // const bytes rlpReaded = importByteArray(o["rlp"].get_str()); - // RLP myRLP(rlpReaded); - // BlockInfo blockFromRlp; - - // try - // { - // blockFromRlp.populateFromHeader(myRLP, false); - // //blockFromRlp.verifyInternals(rlpReaded); - // } - // catch(Exception const& _e) - // { - // cnote << "block construction did throw an exception: " << diagnostic_information(_e); - // BOOST_ERROR("Failed block construction Test with Exception: " << _e.what()); - // BOOST_CHECK_MESSAGE(o.count("block") == 0, "A block object should not be defined because the block RLP is invalid!"); - // return; - // } - - // BOOST_REQUIRE(o.count("block") > 0); - - // mObject tObj = o["block"].get_obj(); - // BlockInfo blockFromFields; - // const bytes rlpreade2 = createBlockRLPFromFields(tObj); - // RLP mysecondRLP(rlpreade2); - // blockFromFields.populateFromHeader(mysecondRLP, false); - - // //Check the fields restored from RLP to original fields - // BOOST_CHECK_MESSAGE(blockFromFields.hash == blockFromRlp.hash, "hash in given RLP not matching the block hash!"); - // BOOST_CHECK_MESSAGE(blockFromFields.parentHash == blockFromRlp.parentHash, "parentHash in given RLP not matching the block parentHash!"); - // BOOST_CHECK_MESSAGE(blockFromFields.sha3Uncles == blockFromRlp.sha3Uncles, "sha3Uncles in given RLP not matching the block sha3Uncles!"); - // BOOST_CHECK_MESSAGE(blockFromFields.coinbaseAddress == blockFromRlp.coinbaseAddress,"coinbaseAddress in given RLP not matching the block coinbaseAddress!"); - // BOOST_CHECK_MESSAGE(blockFromFields.stateRoot == blockFromRlp.stateRoot, "stateRoot in given RLP not matching the block stateRoot!"); - // BOOST_CHECK_MESSAGE(blockFromFields.transactionsRoot == blockFromRlp.transactionsRoot, "transactionsRoot in given RLP not matching the block transactionsRoot!"); - // BOOST_CHECK_MESSAGE(blockFromFields.logBloom == blockFromRlp.logBloom, "logBloom in given RLP not matching the block logBloom!"); - // BOOST_CHECK_MESSAGE(blockFromFields.difficulty == blockFromRlp.difficulty, "difficulty in given RLP not matching the block difficulty!"); - // BOOST_CHECK_MESSAGE(blockFromFields.number == blockFromRlp.number, "number in given RLP not matching the block number!"); - // BOOST_CHECK_MESSAGE(blockFromFields.gasLimit == blockFromRlp.gasLimit,"gasLimit in given RLP not matching the block gasLimit!"); - // BOOST_CHECK_MESSAGE(blockFromFields.gasUsed == blockFromRlp.gasUsed, "gasUsed in given RLP not matching the block gasUsed!"); - // BOOST_CHECK_MESSAGE(blockFromFields.timestamp == blockFromRlp.timestamp, "timestamp in given RLP not matching the block timestamp!"); - // BOOST_CHECK_MESSAGE(blockFromFields.extraData == blockFromRlp.extraData, "extraData in given RLP not matching the block extraData!"); - // BOOST_CHECK_MESSAGE(blockFromFields.nonce == blockFromRlp.nonce, "nonce in given RLP not matching the block nonce!"); - - // BOOST_CHECK_MESSAGE(blockFromFields == blockFromRlp, "However, blockFromFields != blockFromRlp!"); + 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("block") > 0); + BOOST_REQUIRE(o.count("genesisBlockHeader") > 0); - // construct Rlp of the given block - bytes blockRLP = createBlockRLPFromFields(o["block"].get_obj()); + // construct RLP of the genesis block + bytes blockRLP = createBlockRLPFromFields(o["genesisBlockHeader"].get_obj()); RLP myRLP(blockRLP); BlockInfo blockFromFields; - cout << "blockFromFields diff:" << blockFromFields.difficulty << endl; try { - blockFromFields.populateFromHeader(myRLP, false); - //blockFromFields.verifyInternals(blockRLP); } catch (Exception const& _e) { cnote << "block construction did throw an exception: " << diagnostic_information(_e); BOOST_ERROR("Failed block construction Test with Exception: " << _e.what()); - o.erase(o.find("block")); + 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()); - o.erase(o.find("block")); + return; } catch(...) { cnote << "block construction did throw an unknown exception\n"; - o.erase(o.find("block")); - } + 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); @@ -167,32 +255,27 @@ void doBlockTests(json_spirit::mValue& _v, bool _fillin) BOOST_REQUIRE(tx.count("r") > 0); BOOST_REQUIRE(tx.count("s") > 0); BOOST_REQUIRE(tx.count("data") > 0); - cout << "attempt to import transaction\n"; - txs.attemptImport(createTransactionFromFields(tx)); - } - cout << "done importing txs\n"; - - BOOST_REQUIRE(o.count("pre") > 0); - // create state - ImportTest importer(o["pre"].get_obj()); - State theState(Address(), OverlayDB(), BaseState::Empty); - importer.importState(o["pre"].get_obj(), theState); + if (!txs.attemptImport(createTransactionFromFields(tx))) + cnote << "failed importing transaction\n"; + } - cout << "current state diff 1 : " << theState.info().difficulty << endl; - cout << "balance of a94f5374fce5edbc8e2a8697c15331677e6ebf0b: " << theState.balance(Address("0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b")) << endl; - theState.commit(); - cout << "balance of a94f5374fce5edbc8e2a8697c15331677e6ebf0b: " << theState.balance(Address("0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b")) << endl; + // update stateRootHash blockFromFields.stateRoot = theState.rootHash(); - cout << "stateRoot: " << blockFromFields.stateRoot << endl; + cout << "root hash1: " << theState.rootHash() << endl; - // find new nonce + // find new valid nonce ProofOfWork pow; MineInfo ret; tie(ret, blockFromFields.nonce) = pow.mine(blockFromFields.headerHash(WithoutNonce), blockFromFields.difficulty, 1000, true, false); + //---stop - // create new "genesis" block" + //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); @@ -200,46 +283,35 @@ void doBlockTests(json_spirit::mValue& _v, bool _fillin) block.appendRaw(rlpStream.out()); block.appendRaw(RLPEmptyList); block.appendRaw(RLPEmptyList); - //return block.out(); - cout << "my genesis hash: " << sha3(RLP(block.out())[0].data()) << endl; - cout << "construct bc\n"; - BlockChain bc(block.out(), std::string(), true); - cout << "pre difficulty: " << blockFromFields.difficulty << endl; + blockFromFields.verifyInternals(&block.out()); + + // construct blockchain + BlockChain bc(block.out(), string(), true); + + try { - //cout << "sync state with pre block" << endl; theState.sync(bc); - cout << "sync bc and txs in state\n"; - cout << "balance of a94f5374fce5edbc8e2a8697c15331677e6ebf0b: " << theState.balance(Address("0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b")) << endl; - cout << "current state diff: " << theState.info().difficulty << endl; + cout << "root hash2: " << theState.rootHash() << endl; theState.sync(bc,txs); - cout << "current state diff: " << theState.info().difficulty << endl; - // lock - cout << "commit to mine\n"; - theState.commitToMine(bc); // will call uncommitToMine if a repeat. - cout << "current state diff: " << theState.info().difficulty << endl; - // unlock + cout << "root hash3: " << theState.rootHash() << endl; + theState.commitToMine(bc); + cout << "root hash4: " << theState.rootHash() << endl; MineInfo info; - cout << "mine...\n"; for (info.completed = false; !info.completed; info = theState.mine()) {} - cout << "done mining, completeMine\n"; - // lock + cout << "root hash5: " << theState.rootHash() << endl; theState.completeMine(); - cout << "current state diff: " << theState.info().difficulty << endl; - // unlock - - cout << "new block: " << theState.blockData() << endl << theState.info() << endl; - cout << "current diff: " << theState.info().difficulty << endl; + cout << "root hash6: " << theState.rootHash() << endl; } catch (Exception const& _e) { - cnote << "state sync did throw an exception: " << diagnostic_information(_e); + cnote << "state sync or mining did throw an exception: " << diagnostic_information(_e); } catch (std::exception const& _e) { - cnote << "state sync did throw an exception: " << _e.what(); + cnote << "state sync or mining did throw an exception: " << _e.what(); } o["rlp"] = "0x" + toHex(theState.blockData()); @@ -267,7 +339,7 @@ void doBlockTests(json_spirit::mValue& _v, bool _fillin) // write uncle list - mArray aUncleList; // as of now, our parent is always the genesis block, so we can not have uncles. That will change. + mArray aUncleList; // as of now, our parent is always the genesis block, so we can not have uncles. o["uncleHeaders"] = aUncleList; } } @@ -278,53 +350,14 @@ void doBlockTests(json_spirit::mValue& _v, bool _fillin) BOOST_AUTO_TEST_SUITE(BlockTests) -//BOOST_AUTO_TEST_CASE(blFirstTest) -//{ -// dev::test::executeTests("blFirstTest", "/BlockTests", dev::test::doBlockTests); -//} - BOOST_AUTO_TEST_CASE(blValidBlockTest) { dev::test::executeTests("blValidBlockTest", "/BlockTests", dev::test::doBlockTests); } -//BOOST_AUTO_TEST_CASE(ttCreateTest) -//{ -// for (int i = 1; i < boost::unit_test::framework::master_test_suite().argc; ++i) -// { -// string arg = boost::unit_test::framework::master_test_suite().argv[i]; -// if (arg == "--createtest") -// { -// if (boost::unit_test::framework::master_test_suite().argc <= i + 2) -// { -// cnote << "usage: ./testeth --createtest <PathToConstructor> <PathToDestiny>\n"; -// return; -// } -// try -// { -// cnote << "Populating tests..."; -// json_spirit::mValue v; -// string s = asString(dev::contents(boost::unit_test::framework::master_test_suite().argv[i + 1])); -// BOOST_REQUIRE_MESSAGE(s.length() > 0, "Content of " + (string)boost::unit_test::framework::master_test_suite().argv[i + 1] + " is empty."); -// json_spirit::read_string(s, v); -// dev::test::doBlockTests(v, true); -// writeFile(boost::unit_test::framework::master_test_suite().argv[i + 2], asBytes(json_spirit::write_string(v, true))); -// } -// catch (Exception const& _e) -// { -// BOOST_ERROR("Failed block test with Exception: " << diagnostic_information(_e)); -// } -// catch (std::exception const& _e) -// { -// BOOST_ERROR("Failed block test with Exception: " << _e.what()); -// } -// } -// } -//} - -//BOOST_AUTO_TEST_CASE(userDefinedFileTT) -//{ -// dev::test::userDefinedTest("--bltest", dev::test::doBlockTests); -//} +BOOST_AUTO_TEST_CASE(userDefinedFileBl) +{ + dev::test::userDefinedTest("--bltest", dev::test::doBlockTests); +} BOOST_AUTO_TEST_SUITE_END() |