avoid code doubling

This commit is contained in:
CJentzsch 2015-02-11 18:17:01 +01:00
parent f0b2d0f30b
commit 9e63ab10ca

248
block.cpp
View File

@ -69,8 +69,6 @@ void doBlockTests(json_spirit::mValue& _v, bool _fillin)
cerr << i.first << endl; cerr << i.first << endl;
mObject& o = i.second.get_obj(); mObject& o = i.second.get_obj();
if (_fillin == false)
{
BOOST_REQUIRE(o.count("genesisBlockHeader") > 0); BOOST_REQUIRE(o.count("genesisBlockHeader") > 0);
// construct RLP of the genesis block // construct RLP of the genesis block
@ -109,8 +107,23 @@ void doBlockTests(json_spirit::mValue& _v, bool _fillin)
// commit changes to DB // commit changes to DB
theState.commit(); theState.commit();
if (_fillin)
blockFromFields.stateRoot = theState.rootHash();
else
BOOST_CHECK_MESSAGE(blockFromFields.stateRoot == theState.rootHash(), "root hash do not match"); BOOST_CHECK_MESSAGE(blockFromFields.stateRoot == theState.rootHash(), "root hash do not match");
if (_fillin)
{
// find new valid nonce
ProofOfWork pow;
MineInfo ret;
tie(ret, blockFromFields.nonce) = pow.mine(blockFromFields.headerHash(WithoutNonce), blockFromFields.difficulty, 1000, true, false);
//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 // create new "genesis" block
RLPStream rlpStream; RLPStream rlpStream;
blockFromFields.streamRLP(rlpStream, WithNonce); blockFromFields.streamRLP(rlpStream, WithNonce);
@ -125,6 +138,80 @@ void doBlockTests(json_spirit::mValue& _v, bool _fillin)
// construct blockchain // construct blockchain
BlockChain bc(block.out(), string(), true); BlockChain bc(block.out(), string(), true);
if (_fillin)
{
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);
//Transaction txFromFields(createRLPStreamFromTransactionFields(tx).out(), CheckSignature::Sender);
if (!txs.attemptImport(&createRLPStreamFromTransactionFields(tx).out()))
cnote << "failed importing transaction\n";
}
try
{
theState.sync(bc);
theState.sync(bc,txs);
theState.commitToMine(bc);
MineInfo info;
for (info.completed = false; !info.completed; info = theState.mine()) {}
theState.completeMine();
}
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;
}
else
{
try try
{ {
theState.sync(bc); theState.sync(bc);
@ -240,162 +327,7 @@ void doBlockTests(json_spirit::mValue& _v, bool _fillin)
// check uncle list // check uncle list
BOOST_CHECK_MESSAGE(o["uncleList"].get_array().size() == 0, "Uncle list is not empty, but the genesis block can not have uncles"); BOOST_CHECK_MESSAGE((o["uncleList"].type() == json_spirit::null_type ? 0 : o["uncleList"].get_array().size()) == 0, "Uncle list is not empty, but the genesis block can not have uncles");
}
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);
//Transaction txFromFields(createRLPStreamFromTransactionFields(tx).out(), CheckSignature::Sender);
if (!txs.attemptImport(&createRLPStreamFromTransactionFields(tx).out()))
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;
} }
} }
} }