Skip to content

Commit

Permalink
Merge pull request ethereum#4359 from ethereum/fillBC_separation
Browse files Browse the repository at this point in the history
separate input and output of fillBCTest() function
  • Loading branch information
pirapira authored Aug 21, 2017
2 parents a114270 + 8e71ca3 commit 723f60a
Showing 1 changed file with 56 additions and 39 deletions.
95 changes: 56 additions & 39 deletions test/tools/jsontests/BlockChainTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ void checkJsonSectionForInvalidBlock(mObject& _blObj);
void checkExpectedException(mObject& _blObj, Exception const& _e);
void checkBlocks(TestBlock const& _blockFromFields, TestBlock const& _blockFromRlp, string const& _testname);
bigint calculateMiningReward(u256 const& _blNumber, u256 const& _unNumber1 = 0, u256 const& _unNumber2 = 0);
void fillBCTest(json_spirit::mObject& _o);
json_spirit::mObject fillBCTest(json_spirit::mObject const& _input);
void testBCTest(json_spirit::mObject& _o);

//percent output for many tests in one file
Expand Down Expand Up @@ -137,7 +137,7 @@ json_spirit::mValue doTransitionTest(json_spirit::mValue const& _input, bool _fi
}

if (_fillin)
fillBCTest(o);
o = fillBCTest(o);
else
testBCTest(o);
}
Expand Down Expand Up @@ -207,7 +207,7 @@ json_spirit::mValue doBlockchainTestNoLog(json_spirit::mValue const& _input, boo
jObj.erase(jObj.find("expect"));
}
TestOutputHelper::setCurrentTestName(newtestname);
fillBCTest(jObj);
jObj = fillBCTest(jObj);
jObj["network"] = test::netIdToString(network);
tests[newtestname] = jObj;
}
Expand Down Expand Up @@ -244,46 +244,65 @@ json_spirit::mValue doBlockchainTestNoLog(json_spirit::mValue const& _input, boo
return v;
}

void fillBCTest(json_spirit::mObject& _o)
json_spirit::mObject fillBCTest(json_spirit::mObject const& _input)
{
json_spirit::mObject output;
string const& testName = TestOutputHelper::testName();
TestBlock genesisBlock(_o["genesisBlockHeader"].get_obj(), _o["pre"].get_obj());
TestBlock genesisBlock(_input.at("genesisBlockHeader").get_obj(), _input.at("pre").get_obj());
genesisBlock.setBlockHeader(genesisBlock.blockHeader());

TestBlockChain testChain(genesisBlock);
assert(testChain.interface().isKnown(genesisBlock.blockHeader().hash(WithSeal)));

_o["genesisBlockHeader"] = writeBlockHeaderToJson(genesisBlock.blockHeader());
_o["genesisRLP"] = toHexPrefixed(genesisBlock.bytes());
BOOST_REQUIRE(_o.count("blocks"));
output["genesisBlockHeader"] = writeBlockHeaderToJson(genesisBlock.blockHeader());
output["genesisRLP"] = toHexPrefixed(genesisBlock.bytes());
BOOST_REQUIRE(_input.count("blocks"));

mArray blArray;
size_t importBlockNumber = 0;
string chainname = "default";
string chainnetwork = "default";
std::map<string, ChainBranch*> chainMap = { {chainname , new ChainBranch(genesisBlock)}};

for (auto const& bl: _o["blocks"].get_array())
if (_input.count("noBlockChainHistory") > 0)
output["noBlockChainHistory"] = _input.at("noBlockChainHistory");

for (auto const& bl: _input.at("blocks").get_array())
{
mObject blObj = bl.get_obj();
if (blObj.count("blocknumber") > 0)
importBlockNumber = max((int)toInt(blObj["blocknumber"]), 1);
mObject const& blObjInput = bl.get_obj();
mObject blObj;
if (blObjInput.count("blocknumber") > 0)
{
importBlockNumber = max((int)toInt(blObjInput.at("blocknumber")), 1);
blObj["blocknumber"] = blObjInput.at("blocknumber");
}
else
importBlockNumber++;

if (blObj.count("chainname") > 0)
chainname = blObj["chainname"].get_str();
if (blObjInput.count("chainname") > 0)
{
chainname = blObjInput.at("chainname").get_str();
blObj["chainname"] = blObjInput.at("chainname");
}
else
chainname = "default";

if (blObj.count("chainnetwork") > 0)
chainnetwork = blObj["chainnetwork"].get_str();
if (blObjInput.count("chainnetwork") > 0)
{
chainnetwork = blObjInput.at("chainnetwork").get_str();
blObj["chainnetwork"] = blObjInput.at("chainnetwork");
}
else
chainnetwork = "default";

// Copy expectException* fields
for (auto const& field: blObjInput)
if (field.first.substr(0, 15) == "expectException")
blObj[field.first] = field.second;

if (chainMap.count(chainname) > 0)
{
if (_o.count("noBlockChainHistory") == 0)
if (_input.count("noBlockChainHistory") == 0)
{
ChainBranch::forceBlockchain(chainnetwork);
chainMap[chainname]->reset();
Expand All @@ -303,15 +322,15 @@ void fillBCTest(json_spirit::mObject& _o)
vector<TestBlock>& importedBlocks = chainMap[chainname]->importedBlocks;

//Import Transactions
BOOST_REQUIRE(blObj.count("transactions"));
for (auto const& txObj: blObj["transactions"].get_array())
BOOST_REQUIRE(blObjInput.count("transactions"));
for (auto const& txObj: blObjInput.at("transactions").get_array())
{
TestTransaction transaction(txObj.get_obj());
block.addTransaction(transaction);
}

//Import Uncles
for (auto const& uHObj: blObj.at("uncleHeaders").get_array())
for (auto const& uHObj: blObjInput.at("uncleHeaders").get_array())
{
cnote << "Generating uncle block at test " << testName;
TestBlock uncle;
Expand All @@ -327,11 +346,8 @@ void fillBCTest(json_spirit::mObject& _o)
vector<TestBlock> validUncles = blockchain.syncUncles(block.uncles());
block.setUncles(validUncles);

if (blObj.count("blockHeaderPremine"))
{
overwriteBlockHeaderForTest(blObj.at("blockHeaderPremine").get_obj(), block, *chainMap[chainname]);
blObj.erase("blockHeaderPremine");
}
if (blObjInput.count("blockHeaderPremine"))
overwriteBlockHeaderForTest(blObjInput.at("blockHeaderPremine").get_obj(), block, *chainMap[chainname]);

cnote << "Mining block" << importBlockNumber << "for chain" << chainname << "at test " << testName;
block.mine(blockchain);
Expand All @@ -342,8 +358,8 @@ void fillBCTest(json_spirit::mObject& _o)
TestBlock alterBlock(block);
checkBlocks(block, alterBlock, testName);

if (blObj.count("blockHeader"))
overwriteBlockHeaderForTest(blObj.at("blockHeader").get_obj(), alterBlock, *chainMap[chainname]);
if (blObjInput.count("blockHeader"))
overwriteBlockHeaderForTest(blObjInput.at("blockHeader").get_obj(), alterBlock, *chainMap[chainname]);

blObj["rlp"] = toHexPrefixed(alterBlock.bytes());
blObj["blockHeader"] = writeBlockHeaderToJson(alterBlock.blockHeader());
Expand All @@ -360,18 +376,18 @@ void fillBCTest(json_spirit::mObject& _o)
compareBlocks(block, alterBlock);
try
{
if (blObj.count("expectException"))
if (blObjInput.count("expectException"))
BOOST_ERROR("Deprecated expectException field! " + testName);

blockchain.addBlock(alterBlock);
if (testChain.addBlock(alterBlock))
cnote << "The most recent best Block now is " << importBlockNumber << "in chain" << chainname << "at test " << testName;

bool isException = (blObj.count("expectException"+test::netIdToString(test::TestBlockChain::s_sealEngineNetwork))
|| blObj.count("expectExceptionALL"));
bool isException = (blObjInput.count("expectException"+test::netIdToString(test::TestBlockChain::s_sealEngineNetwork))
|| blObjInput.count("expectExceptionALL"));
BOOST_REQUIRE_MESSAGE(!isException, "block import expected exception, but no exception was thrown!");

if (_o.count("noBlockChainHistory") == 0)
if (_input.count("noBlockChainHistory") == 0)
{
importedBlocks.push_back(alterBlock);
importedBlocks.back().clearState(); //close the state as it wont be needed. too many open states would lead to exception.
Expand Down Expand Up @@ -399,27 +415,28 @@ void fillBCTest(json_spirit::mObject& _o)
this_thread::sleep_for(chrono::seconds(1));
}//each blocks

if (_o.count("expect") > 0)
if (_input.count("expect") > 0)
{
AccountMaskMap expectStateMap;
State stateExpect(State::Null);
ImportTest::importState(_o["expect"].get_obj(), stateExpect, expectStateMap);
ImportTest::importState(_input.at("expect").get_obj(), stateExpect, expectStateMap);
if (ImportTest::compareStates(stateExpect, testChain.topBlock().state(), expectStateMap, WhenError::Throw))
cerr << testName << "\n";
_o.erase(_o.find("expect"));
}

_o["blocks"] = blArray;
_o["postState"] = fillJsonWithState(testChain.topBlock().state());
_o["lastblockhash"] = toHexPrefixed(testChain.topBlock().blockHeader().hash(WithSeal));
output["blocks"] = blArray;
output["postState"] = fillJsonWithState(testChain.topBlock().state());
output["lastblockhash"] = toHexPrefixed(testChain.topBlock().blockHeader().hash(WithSeal));

//make all values hex in pre section
State prestate(State::Null);
ImportTest::importState(_o["pre"].get_obj(), prestate);
_o["pre"] = fillJsonWithState(prestate);
ImportTest::importState(_input.at("pre").get_obj(), prestate);
output["pre"] = fillJsonWithState(prestate);

for (auto iterator = chainMap.begin(); iterator != chainMap.end(); iterator++)
delete iterator->second;

return output;
}

void testBCTest(json_spirit::mObject& _o)
Expand Down

0 comments on commit 723f60a

Please sign in to comment.