Browse Source

create block from transaction with genesis block as parent

cl-refactor
CJentzsch 10 years ago
parent
commit
7d03f7be2e
  1. 2
      libethereum/State.h
  2. 27
      test/TestHelper.cpp
  3. 1
      test/TestHelper.h
  4. 15
      test/blFirstTestFiller.json
  5. 197
      test/block.cpp
  6. 30
      test/transaction.cpp

2
libethereum/State.h

@ -128,7 +128,7 @@ public:
* commitToMine(_blockChain); // will call uncommitToMine if a repeat.
* // unlock
* MineInfo info;
* for (info.complete = false; !info.complete; info = mine()) {}
* for (info.completed = false; !info.completed; info = mine()) {}
* }
* // lock
* completeMine();

27
test/TestHelper.cpp

@ -487,6 +487,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()
{

1
test/TestHelper.h

@ -79,6 +79,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);

15
test/blFirstTestFiller.json

@ -0,0 +1,15 @@
{
"firstBlockTest" : {
"transactions": [{
"nonce": "0",
"gasPrice": "0x09184e72a000",
"gasLimit": "0x0f3e6f",
"to": "",
"value": "",
"data": "0x60056013565b6101918061001d6000396000f35b3360008190555056006001600060e060020a6000350480630a874df61461003a57806341c0e1b514610058578063a02b161e14610066578063dbbdf0831461007757005b610045600435610149565b80600160a060020a031660005260206000f35b610060610161565b60006000f35b6100716004356100d4565b60006000f35b61008560043560243561008b565b60006000f35b600054600160a060020a031632600160a060020a031614156100ac576100b1565b6100d0565b8060018360005260205260406000208190555081600060005260206000a15b5050565b600054600160a060020a031633600160a060020a031614158015610118575033600160a060020a0316600182600052602052604060002054600160a060020a031614155b61012157610126565b610146565b600060018260005260205260406000208190555080600060005260206000a15b50565b60006001826000526020526040600020549050919050565b600054600160a060020a031633600160a060020a0316146101815761018f565b600054600160a060020a0316ff5b56",
"v": "0x1b",
"r": "0xd4287e915ebac7a8af390560fa53c8f0b7f13802ba0393d7afa5823c2560ca89",
"s": "0xae75db31a34f7e386ad459646de98ec3a1c88cc91b11620b4ffd86871f579942"
}]
}
}

197
test/block.cpp

@ -20,6 +20,7 @@
* block test functions.
*/
#include <libethereum/CanonBlockChain.h>
#include "TestHelper.h"
using namespace std;
@ -116,40 +117,42 @@ void doBlockTests(json_spirit::mValue& _v, bool _fillin)
}
else
{
BOOST_REQUIRE(o.count("block") > 0);
// construct Rlp of the given block
bytes blockRLP = createBlockRLPFromFields(o["block"].get_obj());
RLP myRLP(blockRLP);
o["rlp"] = toHex(blockRLP);
try
{
BlockInfo blockFromFields;
blockFromFields.populateFromHeader(myRLP, false);
(void)blockFromFields;
//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"));
}
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"));
}
catch(...)
{
cnote << "block construction did throw an unknow exception\n";
o.erase(o.find("block"));
}
// BOOST_REQUIRE(o.count("block") > 0);
// // construct Rlp of the given block
// bytes blockRLP = createBlockRLPFromFields(o["block"].get_obj());
// RLP myRLP(blockRLP);
// o["rlp"] = toHex(blockRLP);
// try
// {
// BlockInfo blockFromFields;
// blockFromFields.populateFromHeader(myRLP, false);
// (void)blockFromFields;
// //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"));
// }
// 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"));
// }
// catch(...)
// {
// cnote << "block construction did throw an unknow exception\n";
// o.erase(o.find("block"));
// }
BOOST_REQUIRE(o.count("transactions") > 0);
TransactionQueue txs;
for (auto const& txObj: o["transactions"].get_array())
{
mObject tx = txObj.get_obj();
@ -162,12 +165,60 @@ 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";
Transaction txFromFields = createTransactionFromFields(tx);
//BOOST_REQUIRE(o.count("env") > 0);
//BOOST_REQUIRE(o.count("pre") > 0);
// ImportTest importer;
// importer.importEnv(o["env"].get_obj());
// importer.importState(o["pre"].get_obj(), m_statePre);
// State theState = importer.m_statePre;
// bytes output;
cout << "construct bc\n";
CanonBlockChain bc(true);
cout << "construct state\n";
State theState;
try
{
cout << "sync bc and txs in state\n";
theState.sync(bc,txs);
// lock
cout << "commit to mine\n";
theState.commitToMine(bc); // will call uncommitToMine if a repeat.
// unlock
MineInfo info;
cout << "mine...\n";
for (info.completed = false; !info.completed; info = theState.mine()) {}
cout << "done mining, completeMine\n";
// lock
theState.completeMine();
// unlock
cout << "new block: " << theState.blockData() << endl << theState.info() << endl;
}
catch (Exception const& _e)
{
cnote << "state sync did throw an exception: " << diagnostic_information(_e);
}
catch (std::exception const& _e)
{
cnote << "state sync did throw an exception: " << _e.what();
}
// write block and rlp to json
//TODO if block rlp is invalid, delete transactions, and block
}
}
}
}
@ -177,48 +228,48 @@ void doBlockTests(json_spirit::mValue& _v, bool _fillin)
BOOST_AUTO_TEST_SUITE(BlockTests)
BOOST_AUTO_TEST_CASE(blValidBlocksTest)
{
dev::test::executeTests("blValidBlockTest", "/BlockTests", dev::test::doBlockTests);
}
BOOST_AUTO_TEST_CASE(ttCreateTest)
BOOST_AUTO_TEST_CASE(blFirstTest)
{
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());
}
}
}
dev::test::executeTests("blFirstTest", "/BlockTests", dev::test::doBlockTests);
}
BOOST_AUTO_TEST_CASE(userDefinedFileTT)
{
dev::test::userDefinedTest("--bltest", 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_SUITE_END()

30
test/transaction.cpp

@ -29,33 +29,6 @@ using namespace dev::eth;
namespace dev { namespace test {
Transaction createTransactionFromFields(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 Transaction(rlpStream.out(), CheckSignature::Sender);
}
void doTransactionTests(json_spirit::mValue& _v, bool _fillin)
{
for (auto& i: _v.get_obj())
@ -84,7 +57,8 @@ void doTransactionTests(json_spirit::mValue& _v, bool _fillin)
BOOST_REQUIRE(o.count("transaction") > 0);
mObject tObj = o["transaction"].get_obj();
Transaction txFromFields = createTransactionFromFields(tObj);
bytes txRLP = createTransactionFromFields(tObj);
Transaction txFromFields(txRLP, CheckSignature::Sender);
//Check the fields restored from RLP to original fields
BOOST_CHECK_MESSAGE(txFromFields.data() == txFromRlp.data(), "Data in given RLP not matching the Transaction data!");

Loading…
Cancel
Save