2015-03-04 11:04:15 +00:00
/*
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 .
*/
2015-03-29 20:23:59 +00:00
# include <boost/filesystem.hpp>
2015-03-04 11:04:15 +00:00
# include <libdevcrypto/FileSystem.h>
2015-03-29 20:37:32 +00:00
# include <libtestutils/TransientDirectory.h>
2015-03-04 11:04:15 +00:00
# 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 {
2015-03-04 20:32:26 +00:00
bytes createBlockRLPFromFields ( mObject & _tObj ) ;
2015-03-05 23:52:32 +00:00
void overwriteBlockHeader ( BlockInfo & _current_BlockHeader , mObject & _blObj ) ;
2015-03-05 23:12:51 +00:00
BlockInfo constructBlock ( mObject & _o ) ;
void updatePoW ( BlockInfo & _bi ) ;
2015-03-29 20:23:59 +00:00
void writeBlockHeaderToJson ( mObject & _o , BlockInfo const & _bi ) ;
RLPStream createFullBlockFromHeader ( BlockInfo const & _bi , bytes const & _txs = RLPEmptyList , bytes const & _uncles = RLPEmptyList ) ;
2015-03-04 11:04:15 +00:00
2015-03-03 23:24:06 +00:00
void doBlockchainTests ( json_spirit : : mValue & _v , bool _fillin )
2015-03-04 11:04:15 +00:00
{
for ( auto & i : _v . get_obj ( ) )
{
cerr < < i . first < < endl ;
mObject & o = i . second . get_obj ( ) ;
BOOST_REQUIRE ( o . count ( " genesisBlockHeader " ) ) ;
2015-03-05 23:12:51 +00:00
BlockInfo biGenesisBlock = constructBlock ( o [ " genesisBlockHeader " ] . get_obj ( ) ) ;
2015-03-04 11:04:15 +00:00
BOOST_REQUIRE ( o . count ( " pre " ) ) ;
ImportTest importer ( o [ " pre " ] . get_obj ( ) ) ;
2015-04-05 19:08:22 +00:00
State state ( OverlayDB ( ) , BaseState : : Empty , biGenesisBlock . coinbaseAddress ) ;
2015-03-04 11:04:15 +00:00
importer . importState ( o [ " pre " ] . get_obj ( ) , state ) ;
2015-03-09 09:28:48 +00:00
o [ " pre " ] = fillJsonWithState ( state ) ;
2015-03-04 11:04:15 +00:00
state . commit ( ) ;
if ( _fillin )
2015-03-05 23:12:51 +00:00
biGenesisBlock . stateRoot = state . rootHash ( ) ;
2015-03-04 11:04:15 +00:00
else
2015-03-05 23:12:51 +00:00
BOOST_CHECK_MESSAGE ( biGenesisBlock . stateRoot = = state . rootHash ( ) , " root hash does not match " ) ;
2015-03-04 11:04:15 +00:00
if ( _fillin )
{
// find new valid nonce
2015-03-05 23:12:51 +00:00
updatePoW ( biGenesisBlock ) ;
2015-03-04 11:04:15 +00:00
//update genesis block in json file
2015-03-05 23:12:51 +00:00
writeBlockHeaderToJson ( o [ " genesisBlockHeader " ] . get_obj ( ) , biGenesisBlock ) ;
2015-03-04 11:04:15 +00:00
}
// create new "genesis" block
2015-03-05 23:12:51 +00:00
RLPStream rlpGenesisBlock = createFullBlockFromHeader ( biGenesisBlock ) ;
biGenesisBlock . verifyInternals ( & rlpGenesisBlock . out ( ) ) ;
2015-03-23 12:08:14 +00:00
o [ " genesisRLP " ] = " 0x " + toHex ( rlpGenesisBlock . out ( ) ) ;
2015-03-04 11:04:15 +00:00
// construct blockchain
2015-03-29 20:37:32 +00:00
TransientDirectory td ;
2015-04-03 19:10:15 +00:00
BlockChain bc ( rlpGenesisBlock . out ( ) , td . path ( ) , WithExisting : : Kill ) ;
2015-03-04 11:04:15 +00:00
if ( _fillin )
{
BOOST_REQUIRE ( o . count ( " blocks " ) ) ;
mArray blArray ;
2015-03-05 23:12:51 +00:00
vector < BlockInfo > vBiBlocks ;
2015-03-06 08:14:45 +00:00
vBiBlocks . push_back ( biGenesisBlock ) ;
2015-03-04 11:04:15 +00:00
for ( auto const & bl : o [ " blocks " ] . get_array ( ) )
{
mObject blObj = bl . get_obj ( ) ;
2015-03-05 23:12:51 +00:00
// get txs
2015-03-04 11:04:15 +00:00
TransactionQueue txs ;
2015-03-09 09:28:48 +00:00
ZeroGasPricer gp ;
2015-03-05 23:12:51 +00:00
BOOST_REQUIRE ( blObj . count ( " transactions " ) ) ;
2015-03-04 11:04:15 +00:00
for ( auto const & txObj : blObj [ " transactions " ] . get_array ( ) )
{
mObject tx = txObj . get_obj ( ) ;
importer . importTransaction ( tx ) ;
2015-04-05 14:33:51 +00:00
if ( txs . import ( importer . m_transaction . rlp ( ) ) ! = ImportResult : : Success )
2015-03-04 11:04:15 +00:00
cnote < < " failed importing transaction \n " ;
}
2015-03-05 23:12:51 +00:00
// write uncle list
BlockQueue uncleBlockQueue ;
mArray aUncleList ;
vector < BlockInfo > vBiUncles ;
2015-03-09 09:28:48 +00:00
mObject uncleHeaderObj_pre ;
2015-03-05 23:12:51 +00:00
for ( auto const & uHObj : blObj [ " uncleHeaders " ] . get_array ( ) )
{
mObject uncleHeaderObj = uHObj . get_obj ( ) ;
2015-03-09 13:50:09 +00:00
if ( uncleHeaderObj . count ( " sameAsPreviousSibling " ) )
2015-03-09 09:28:48 +00:00
{
writeBlockHeaderToJson ( uncleHeaderObj_pre , vBiUncles [ vBiUncles . size ( ) - 1 ] ) ;
aUncleList . push_back ( uncleHeaderObj_pre ) ;
vBiUncles . push_back ( vBiUncles [ vBiUncles . size ( ) - 1 ] ) ;
continue ;
}
2015-03-23 12:08:14 +00:00
if ( uncleHeaderObj . count ( " sameAsBlock " ) )
{
writeBlockHeaderToJson ( uncleHeaderObj_pre , vBiBlocks [ ( size_t ) toInt ( uncleHeaderObj [ " sameAsBlock " ] ) ] ) ;
aUncleList . push_back ( uncleHeaderObj_pre ) ;
vBiUncles . push_back ( vBiBlocks [ ( size_t ) toInt ( uncleHeaderObj [ " sameAsBlock " ] ) ] ) ;
continue ;
}
2015-04-06 06:52:20 +00:00
string overwrite = " false " ;
if ( uncleHeaderObj . count ( " overwriteAndRedoPoW " ) )
{
overwrite = uncleHeaderObj [ " overwriteAndRedoPoW " ] . get_str ( ) ;
uncleHeaderObj . erase ( " overwriteAndRedoPoW " ) ;
}
2015-03-23 12:08:14 +00:00
2015-03-05 23:12:51 +00:00
BlockInfo uncleBlockFromFields = constructBlock ( uncleHeaderObj ) ;
// make uncle header valid
uncleBlockFromFields . timestamp = ( u256 ) time ( 0 ) ;
2015-03-06 08:14:45 +00:00
if ( vBiBlocks . size ( ) > 2 )
2015-03-14 09:03:30 +00:00
{
if ( uncleBlockFromFields . number - 1 < vBiBlocks . size ( ) )
uncleBlockFromFields . populateFromParent ( vBiBlocks [ ( size_t ) uncleBlockFromFields . number - 1 ] ) ;
else
uncleBlockFromFields . populateFromParent ( vBiBlocks [ vBiBlocks . size ( ) - 2 ] ) ;
}
2015-03-06 08:14:45 +00:00
else
continue ;
2015-03-05 23:12:51 +00:00
2015-04-06 06:52:20 +00:00
if ( overwrite ! = " false " )
{
uncleBlockFromFields . difficulty = overwrite = = " difficulty " ? toInt ( uncleHeaderObj [ " difficulty " ] ) : uncleBlockFromFields . difficulty ;
uncleBlockFromFields . gasLimit = overwrite = = " gasLimit " ? toInt ( uncleHeaderObj [ " gasLimit " ] ) : uncleBlockFromFields . gasLimit ;
uncleBlockFromFields . gasUsed = overwrite = = " gasUsed " ? toInt ( uncleHeaderObj [ " gasUsed " ] ) : uncleBlockFromFields . gasUsed ;
uncleBlockFromFields . parentHash = overwrite = = " parentHash " ? h256 ( uncleHeaderObj [ " parentHash " ] . get_str ( ) ) : uncleBlockFromFields . parentHash ;
uncleBlockFromFields . stateRoot = overwrite = = " stateRoot " ? h256 ( uncleHeaderObj [ " stateRoot " ] . get_str ( ) ) : uncleBlockFromFields . stateRoot ;
2015-04-06 22:03:03 +00:00
if ( overwrite = = " timestamp " )
{
uncleBlockFromFields . timestamp = toInt ( uncleHeaderObj [ " timestamp " ] ) ;
uncleBlockFromFields . difficulty = uncleBlockFromFields . calculateDifficulty ( vBiBlocks [ ( size_t ) uncleBlockFromFields . number - 1 ] ) ;
}
2015-04-06 06:52:20 +00:00
}
2015-03-05 23:12:51 +00:00
updatePoW ( uncleBlockFromFields ) ;
writeBlockHeaderToJson ( uncleHeaderObj , uncleBlockFromFields ) ;
aUncleList . push_back ( uncleHeaderObj ) ;
vBiUncles . push_back ( uncleBlockFromFields ) ;
cnote < < " import uncle in blockQueue " ;
2015-04-06 06:52:20 +00:00
2015-03-05 23:12:51 +00:00
RLPStream uncle = createFullBlockFromHeader ( uncleBlockFromFields ) ;
2015-04-06 06:52:20 +00:00
try
{
uncleBlockQueue . import ( & uncle . out ( ) , bc ) ;
}
catch ( . . . )
{
2015-04-06 22:03:03 +00:00
cnote < < " error in importing uncle! This produces an invalid block (May be by purpose for testing). " ;
2015-04-06 06:52:20 +00:00
}
2015-03-09 09:28:48 +00:00
uncleHeaderObj_pre = uncleHeaderObj ;
2015-03-05 23:12:51 +00:00
}
blObj [ " uncleHeaders " ] = aUncleList ;
bc . sync ( uncleBlockQueue , state . db ( ) , 4 ) ;
state . commitToMine ( bc ) ;
2015-03-04 11:04:15 +00:00
try
{
state . sync ( bc ) ;
2015-03-06 21:53:49 +00:00
state . sync ( bc , txs , gp ) ;
2015-03-04 11:04:15 +00:00
state . commitToMine ( bc ) ;
MineInfo info ;
for ( info . completed = false ; ! info . completed ; info = state . mine ( ) ) { }
state . completeMine ( ) ;
}
catch ( Exception const & _e )
{
cnote < < " state sync or mining did throw an exception: " < < diagnostic_information ( _e ) ;
return ;
}
catch ( std : : exception const & _e )
{
cnote < < " state sync or mining did throw an exception: " < < _e . what ( ) ;
return ;
}
2015-03-05 23:12:51 +00:00
blObj [ " rlp " ] = " 0x " + toHex ( state . blockData ( ) ) ;
2015-03-04 11:04:15 +00:00
// write valid txs
mArray txArray ;
Transactions txList ;
for ( auto const & txi : txs . transactions ( ) )
{
2015-03-29 17:13:39 +00:00
txList . push_back ( txi . second ) ;
2015-03-04 11:04:15 +00:00
mObject txObject ;
2015-03-29 17:13:39 +00:00
txObject [ " nonce " ] = toString ( txi . second . nonce ( ) ) ;
txObject [ " data " ] = " 0x " + toHex ( txi . second . data ( ) ) ;
txObject [ " gasLimit " ] = toString ( txi . second . gas ( ) ) ;
txObject [ " gasPrice " ] = toString ( txi . second . gasPrice ( ) ) ;
txObject [ " r " ] = " 0x " + toString ( txi . second . signature ( ) . r ) ;
txObject [ " s " ] = " 0x " + toString ( txi . second . signature ( ) . s ) ;
txObject [ " v " ] = to_string ( txi . second . signature ( ) . v + 27 ) ;
txObject [ " to " ] = txi . second . isCreation ( ) ? " " : toString ( txi . second . receiveAddress ( ) ) ;
txObject [ " value " ] = toString ( txi . second . value ( ) ) ;
2015-03-04 11:04:15 +00:00
txArray . push_back ( txObject ) ;
}
blObj [ " transactions " ] = txArray ;
BlockInfo current_BlockHeader = state . info ( ) ;
if ( blObj . count ( " blockHeader " ) )
2015-03-04 20:32:26 +00:00
overwriteBlockHeader ( current_BlockHeader , blObj ) ;
2015-03-04 11:04:15 +00:00
// write block header
mObject oBlockHeader ;
2015-03-05 23:12:51 +00:00
writeBlockHeaderToJson ( oBlockHeader , current_BlockHeader ) ;
2015-03-04 11:04:15 +00:00
blObj [ " blockHeader " ] = oBlockHeader ;
2015-03-05 23:12:51 +00:00
vBiBlocks . push_back ( current_BlockHeader ) ;
2015-03-04 11:04:15 +00:00
2015-03-05 23:12:51 +00:00
// compare blocks from state and from rlp
2015-03-04 11:04:15 +00:00
RLPStream txStream ;
txStream . appendList ( txList . size ( ) ) ;
for ( unsigned i = 0 ; i < txList . size ( ) ; + + i )
{
RLPStream txrlp ;
txList [ i ] . streamRLP ( txrlp ) ;
txStream . appendRaw ( txrlp . out ( ) ) ;
}
2015-03-05 23:12:51 +00:00
RLPStream uncleStream ;
uncleStream . appendList ( vBiUncles . size ( ) ) ;
for ( unsigned i = 0 ; i < vBiUncles . size ( ) ; + + i )
{
RLPStream uncleRlp ;
vBiUncles [ i ] . streamRLP ( uncleRlp , WithNonce ) ;
uncleStream . appendRaw ( uncleRlp . out ( ) ) ;
}
2015-03-04 11:04:15 +00:00
2015-03-05 23:12:51 +00:00
RLPStream block2 = createFullBlockFromHeader ( current_BlockHeader , txStream . out ( ) , uncleStream . out ( ) ) ;
2015-03-04 11:04:15 +00:00
blObj [ " rlp " ] = " 0x " + toHex ( block2 . out ( ) ) ;
if ( sha3 ( RLP ( state . blockData ( ) ) [ 0 ] . data ( ) ) ! = sha3 ( RLP ( block2 . out ( ) ) [ 0 ] . data ( ) ) )
cnote < < " block header mismatch \n " ;
if ( sha3 ( RLP ( state . blockData ( ) ) [ 1 ] . data ( ) ) ! = sha3 ( RLP ( block2 . out ( ) ) [ 1 ] . data ( ) ) )
cnote < < " txs mismatch \n " ;
if ( sha3 ( RLP ( state . blockData ( ) ) [ 2 ] . data ( ) ) ! = sha3 ( RLP ( block2 . out ( ) ) [ 2 ] . data ( ) ) )
2015-03-05 23:12:51 +00:00
cnote < < " uncle list mismatch \n " < < RLP ( state . blockData ( ) ) [ 2 ] . data ( ) < < " \n " < < RLP ( block2 . out ( ) ) [ 2 ] . data ( ) ;
2015-03-29 20:23:59 +00:00
2015-03-04 11:04:15 +00:00
try
{
2015-03-03 23:24:06 +00:00
state . sync ( bc ) ;
bc . import ( block2 . out ( ) , state . db ( ) ) ;
state . sync ( bc ) ;
2015-03-05 23:12:51 +00:00
state . commit ( ) ;
2015-03-04 11:04:15 +00:00
}
// if exception is thrown, RLP is invalid and no blockHeader, Transaction list, or Uncle list should be given
catch ( . . . )
{
cnote < < " block is invalid! \n " ;
blObj . erase ( blObj . find ( " blockHeader " ) ) ;
blObj . erase ( blObj . find ( " uncleHeaders " ) ) ;
blObj . erase ( blObj . find ( " transactions " ) ) ;
}
blArray . push_back ( blObj ) ;
}
o [ " blocks " ] = blArray ;
2015-03-16 16:07:48 +00:00
o [ " postState " ] = fillJsonWithState ( state ) ;
2015-03-04 11:04:15 +00:00
}
else
{
for ( auto const & bl : o [ " blocks " ] . get_array ( ) )
{
mObject blObj = bl . get_obj ( ) ;
bytes blockRLP ;
try
{
state . sync ( bc ) ;
blockRLP = importByteArray ( blObj [ " rlp " ] . get_str ( ) ) ;
bc . import ( blockRLP , state . db ( ) ) ;
state . sync ( bc ) ;
}
// if exception is thrown, RLP is invalid and no blockHeader, Transaction list, or Uncle list should be given
catch ( Exception const & _e )
{
cnote < < " state sync or block import did throw an exception: " < < diagnostic_information ( _e ) ;
BOOST_CHECK ( blObj . count ( " blockHeader " ) = = 0 ) ;
BOOST_CHECK ( blObj . count ( " transactions " ) = = 0 ) ;
BOOST_CHECK ( blObj . count ( " uncleHeaders " ) = = 0 ) ;
continue ;
}
catch ( std : : exception const & _e )
{
cnote < < " state sync or block import did throw an exception: " < < _e . what ( ) ;
BOOST_CHECK ( blObj . count ( " blockHeader " ) = = 0 ) ;
BOOST_CHECK ( blObj . count ( " transactions " ) = = 0 ) ;
BOOST_CHECK ( blObj . count ( " uncleHeaders " ) = = 0 ) ;
continue ;
}
2015-03-29 20:23:59 +00:00
catch ( . . . )
2015-03-04 11:04:15 +00:00
{
cnote < < " state sync or block import did throw an exception \n " ;
BOOST_CHECK ( blObj . count ( " blockHeader " ) = = 0 ) ;
BOOST_CHECK ( blObj . count ( " transactions " ) = = 0 ) ;
BOOST_CHECK ( blObj . count ( " uncleHeaders " ) = = 0 ) ;
continue ;
}
BOOST_REQUIRE ( blObj . count ( " blockHeader " ) ) ;
mObject tObj = blObj [ " blockHeader " ] . get_obj ( ) ;
BlockInfo blockHeaderFromFields ;
const bytes c_rlpBytesBlockHeader = createBlockRLPFromFields ( tObj ) ;
const RLP c_blockHeaderRLP ( c_rlpBytesBlockHeader ) ;
2015-03-06 21:53:49 +00:00
blockHeaderFromFields . populateFromHeader ( c_blockHeaderRLP , IgnoreNonce ) ;
2015-03-04 11:04:15 +00:00
BlockInfo blockFromRlp = bc . info ( ) ;
//Check the fields restored from RLP to original fields
BOOST_CHECK_MESSAGE ( blockHeaderFromFields . headerHash ( WithNonce ) = = blockFromRlp . headerHash ( WithNonce ) , " 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 . receiptsRoot = = blockFromRlp . receiptsRoot , " receiptsRoot in given RLP not matching the block receiptsRoot! " ) ;
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! " ) ;
2015-03-04 16:35:23 +00:00
BOOST_CHECK_MESSAGE ( blockHeaderFromFields . mixHash = = blockFromRlp . mixHash , " mixHash in given RLP not matching the block mixHash! " ) ;
2015-03-04 11:04:15 +00:00
BOOST_CHECK_MESSAGE ( blockHeaderFromFields . nonce = = blockFromRlp . nonce , " nonce in given RLP not matching the block nonce! " ) ;
BOOST_CHECK_MESSAGE ( blockHeaderFromFields = = blockFromRlp , " However, blockHeaderFromFields != blockFromRlp! " ) ;
//Check transaction list
Transactions txsFromField ;
for ( auto const & txObj : blObj [ " transactions " ] . get_array ( ) )
{
mObject tx = txObj . get_obj ( ) ;
BOOST_REQUIRE ( tx . count ( " nonce " ) ) ;
BOOST_REQUIRE ( tx . count ( " gasPrice " ) ) ;
BOOST_REQUIRE ( tx . count ( " gasLimit " ) ) ;
BOOST_REQUIRE ( tx . count ( " to " ) ) ;
BOOST_REQUIRE ( tx . count ( " value " ) ) ;
BOOST_REQUIRE ( tx . count ( " v " ) ) ;
BOOST_REQUIRE ( tx . count ( " r " ) ) ;
BOOST_REQUIRE ( tx . count ( " s " ) ) ;
BOOST_REQUIRE ( tx . count ( " data " ) ) ;
try
{
Transaction t ( createRLPStreamFromTransactionFields ( tx ) . out ( ) , CheckSignature : : Sender ) ;
txsFromField . push_back ( t ) ;
}
catch ( Exception const & _e )
{
BOOST_ERROR ( " Failed transaction constructor with Exception: " < < diagnostic_information ( _e ) ) ;
}
catch ( exception const & _e )
{
cnote < < _e . what ( ) ;
}
}
Transactions txsFromRlp ;
RLP root ( blockRLP ) ;
for ( auto const & tr : root [ 1 ] )
{
Transaction tx ( tr . data ( ) , CheckSignature : : Sender ) ;
txsFromRlp . push_back ( tx ) ;
}
BOOST_CHECK_MESSAGE ( txsFromRlp . size ( ) = = txsFromField . size ( ) , " transaction list size does not match " ) ;
for ( size_t i = 0 ; i < txsFromField . size ( ) ; + + i )
{
BOOST_CHECK_MESSAGE ( txsFromField [ i ] . data ( ) = = txsFromRlp [ i ] . data ( ) , " transaction data in rlp and in field do not match " ) ;
BOOST_CHECK_MESSAGE ( txsFromField [ i ] . gas ( ) = = txsFromRlp [ i ] . gas ( ) , " transaction gasLimit in rlp and in field do not match " ) ;
BOOST_CHECK_MESSAGE ( txsFromField [ i ] . gasPrice ( ) = = txsFromRlp [ i ] . gasPrice ( ) , " transaction gasPrice in rlp and in field do not match " ) ;
BOOST_CHECK_MESSAGE ( txsFromField [ i ] . nonce ( ) = = txsFromRlp [ i ] . nonce ( ) , " transaction nonce in rlp and in field do not match " ) ;
BOOST_CHECK_MESSAGE ( txsFromField [ i ] . signature ( ) . r = = txsFromRlp [ i ] . signature ( ) . r , " transaction r in rlp and in field do not match " ) ;
BOOST_CHECK_MESSAGE ( txsFromField [ i ] . signature ( ) . s = = txsFromRlp [ i ] . signature ( ) . s , " transaction s in rlp and in field do not match " ) ;
BOOST_CHECK_MESSAGE ( txsFromField [ i ] . signature ( ) . v = = txsFromRlp [ i ] . signature ( ) . v , " transaction v in rlp and in field do not match " ) ;
BOOST_CHECK_MESSAGE ( txsFromField [ i ] . receiveAddress ( ) = = txsFromRlp [ i ] . receiveAddress ( ) , " transaction receiveAddress in rlp and in field do not match " ) ;
BOOST_CHECK_MESSAGE ( txsFromField [ i ] . value ( ) = = txsFromRlp [ i ] . value ( ) , " transaction receiveAddress in rlp and in field do not match " ) ;
2015-03-04 20:32:26 +00:00
BOOST_CHECK_MESSAGE ( txsFromField [ i ] = = txsFromRlp [ i ] , " transactions from rlp and transaction from field do not match " ) ;
BOOST_CHECK_MESSAGE ( txsFromField [ i ] . rlp ( ) = = txsFromRlp [ i ] . rlp ( ) , " transactions rlp do not match " ) ;
2015-03-04 11:04:15 +00:00
}
// check uncle list
2015-03-05 23:12:51 +00:00
// uncles from uncle list field
vector < BlockInfo > uBlHsFromField ;
if ( blObj [ " uncleHeaders " ] . type ( ) ! = json_spirit : : null_type )
for ( auto const & uBlHeaderObj : blObj [ " uncleHeaders " ] . get_array ( ) )
{
mObject uBlH = uBlHeaderObj . get_obj ( ) ;
2015-03-13 13:58:47 +00:00
BOOST_REQUIRE ( uBlH . size ( ) = = 16 ) ;
2015-03-05 23:12:51 +00:00
bytes uncleRLP = createBlockRLPFromFields ( uBlH ) ;
const RLP c_uRLP ( uncleRLP ) ;
BlockInfo uncleBlockHeader ;
try
{
2015-03-06 21:53:49 +00:00
uncleBlockHeader . populateFromHeader ( c_uRLP ) ;
2015-03-05 23:12:51 +00:00
}
catch ( . . . )
{
BOOST_ERROR ( " invalid uncle header " ) ;
}
uBlHsFromField . push_back ( uncleBlockHeader ) ;
}
// uncles from block RLP
vector < BlockInfo > uBlHsFromRlp ;
for ( auto const & uRLP : root [ 2 ] )
{
BlockInfo uBl ;
2015-03-06 21:53:49 +00:00
uBl . populateFromHeader ( uRLP ) ;
2015-03-05 23:12:51 +00:00
uBlHsFromRlp . push_back ( uBl ) ;
}
BOOST_REQUIRE_EQUAL ( uBlHsFromField . size ( ) , uBlHsFromRlp . size ( ) ) ;
for ( size_t i = 0 ; i < uBlHsFromField . size ( ) ; + + i )
BOOST_CHECK_MESSAGE ( uBlHsFromField [ i ] = = uBlHsFromRlp [ i ] , " block header in rlp and in field do not match " ) ;
2015-03-04 20:32:26 +00:00
}
}
}
2015-03-04 11:04:15 +00:00
}
2015-03-05 23:12:51 +00:00
// helping functions
2015-03-04 20:32:26 +00:00
bytes createBlockRLPFromFields ( mObject & _tObj )
{
RLPStream rlpStream ;
rlpStream . appendList ( _tObj . count ( " hash " ) > 0 ? ( _tObj . size ( ) - 1 ) : _tObj . size ( ) ) ;
if ( _tObj . count ( " parentHash " ) )
rlpStream < < importByteArray ( _tObj [ " parentHash " ] . get_str ( ) ) ;
if ( _tObj . count ( " uncleHash " ) )
rlpStream < < importByteArray ( _tObj [ " uncleHash " ] . get_str ( ) ) ;
if ( _tObj . count ( " coinbase " ) )
rlpStream < < importByteArray ( _tObj [ " coinbase " ] . get_str ( ) ) ;
if ( _tObj . count ( " stateRoot " ) )
rlpStream < < importByteArray ( _tObj [ " stateRoot " ] . get_str ( ) ) ;
if ( _tObj . count ( " transactionsTrie " ) )
rlpStream < < importByteArray ( _tObj [ " transactionsTrie " ] . get_str ( ) ) ;
if ( _tObj . count ( " receiptTrie " ) )
rlpStream < < importByteArray ( _tObj [ " receiptTrie " ] . get_str ( ) ) ;
if ( _tObj . count ( " bloom " ) )
rlpStream < < importByteArray ( _tObj [ " bloom " ] . get_str ( ) ) ;
if ( _tObj . count ( " difficulty " ) )
rlpStream < < bigint ( _tObj [ " difficulty " ] . get_str ( ) ) ;
if ( _tObj . count ( " number " ) )
rlpStream < < bigint ( _tObj [ " number " ] . get_str ( ) ) ;
if ( _tObj . count ( " gasLimit " ) )
rlpStream < < bigint ( _tObj [ " gasLimit " ] . get_str ( ) ) ;
if ( _tObj . count ( " gasUsed " ) )
rlpStream < < bigint ( _tObj [ " gasUsed " ] . get_str ( ) ) ;
if ( _tObj . count ( " timestamp " ) )
rlpStream < < bigint ( _tObj [ " timestamp " ] . get_str ( ) ) ;
if ( _tObj . count ( " extraData " ) )
rlpStream < < fromHex ( _tObj [ " extraData " ] . get_str ( ) ) ;
if ( _tObj . count ( " mixHash " ) )
rlpStream < < importByteArray ( _tObj [ " mixHash " ] . get_str ( ) ) ;
if ( _tObj . count ( " nonce " ) )
rlpStream < < importByteArray ( _tObj [ " nonce " ] . get_str ( ) ) ;
return rlpStream . out ( ) ;
}
2015-03-29 20:23:59 +00:00
void overwriteBlockHeader ( BlockInfo & _currentBlockHeader , mObject & _blObj )
2015-03-04 20:32:26 +00:00
{
2015-03-05 23:52:32 +00:00
if ( _blObj [ " blockHeader " ] . get_obj ( ) . size ( ) ! = 14 )
2015-03-04 20:32:26 +00:00
{
2015-03-29 20:23:59 +00:00
BlockInfo tmp = _currentBlockHeader ;
2015-03-04 20:32:26 +00:00
2015-03-05 23:52:32 +00:00
if ( _blObj [ " blockHeader " ] . get_obj ( ) . count ( " parentHash " ) )
tmp . parentHash = h256 ( _blObj [ " blockHeader " ] . get_obj ( ) [ " parentHash " ] . get_str ( ) ) ;
2015-03-04 20:32:26 +00:00
2015-03-05 23:52:32 +00:00
if ( _blObj [ " blockHeader " ] . get_obj ( ) . count ( " uncleHash " ) )
tmp . sha3Uncles = h256 ( _blObj [ " blockHeader " ] . get_obj ( ) [ " uncleHash " ] . get_str ( ) ) ;
2015-03-04 20:32:26 +00:00
2015-03-05 23:52:32 +00:00
if ( _blObj [ " blockHeader " ] . get_obj ( ) . count ( " coinbase " ) )
tmp . coinbaseAddress = Address ( _blObj [ " blockHeader " ] . get_obj ( ) [ " coinbase " ] . get_str ( ) ) ;
2015-03-04 20:32:26 +00:00
2015-03-05 23:52:32 +00:00
if ( _blObj [ " blockHeader " ] . get_obj ( ) . count ( " stateRoot " ) )
tmp . stateRoot = h256 ( _blObj [ " blockHeader " ] . get_obj ( ) [ " stateRoot " ] . get_str ( ) ) ;
2015-03-04 20:32:26 +00:00
2015-03-05 23:52:32 +00:00
if ( _blObj [ " blockHeader " ] . get_obj ( ) . count ( " transactionsTrie " ) )
tmp . transactionsRoot = h256 ( _blObj [ " blockHeader " ] . get_obj ( ) [ " transactionsTrie " ] . get_str ( ) ) ;
2015-03-04 20:32:26 +00:00
2015-03-05 23:52:32 +00:00
if ( _blObj [ " blockHeader " ] . get_obj ( ) . count ( " receiptTrie " ) )
tmp . receiptsRoot = h256 ( _blObj [ " blockHeader " ] . get_obj ( ) [ " receiptTrie " ] . get_str ( ) ) ;
2015-03-04 20:32:26 +00:00
2015-03-05 23:52:32 +00:00
if ( _blObj [ " blockHeader " ] . get_obj ( ) . count ( " bloom " ) )
tmp . logBloom = LogBloom ( _blObj [ " blockHeader " ] . get_obj ( ) [ " bloom " ] . get_str ( ) ) ;
2015-03-04 20:32:26 +00:00
2015-03-05 23:52:32 +00:00
if ( _blObj [ " blockHeader " ] . get_obj ( ) . count ( " difficulty " ) )
tmp . difficulty = toInt ( _blObj [ " blockHeader " ] . get_obj ( ) [ " difficulty " ] ) ;
2015-03-04 20:32:26 +00:00
2015-03-05 23:52:32 +00:00
if ( _blObj [ " blockHeader " ] . get_obj ( ) . count ( " number " ) )
tmp . number = toInt ( _blObj [ " blockHeader " ] . get_obj ( ) [ " number " ] ) ;
2015-03-04 20:32:26 +00:00
2015-03-05 23:52:32 +00:00
if ( _blObj [ " blockHeader " ] . get_obj ( ) . count ( " gasLimit " ) )
tmp . gasLimit = toInt ( _blObj [ " blockHeader " ] . get_obj ( ) [ " gasLimit " ] ) ;
2015-03-04 20:32:26 +00:00
2015-03-05 23:52:32 +00:00
if ( _blObj [ " blockHeader " ] . get_obj ( ) . count ( " gasUsed " ) )
tmp . gasUsed = toInt ( _blObj [ " blockHeader " ] . get_obj ( ) [ " gasUsed " ] ) ;
2015-03-04 20:32:26 +00:00
2015-03-05 23:52:32 +00:00
if ( _blObj [ " blockHeader " ] . get_obj ( ) . count ( " timestamp " ) )
tmp . timestamp = toInt ( _blObj [ " blockHeader " ] . get_obj ( ) [ " timestamp " ] ) ;
2015-03-04 20:32:26 +00:00
2015-03-05 23:52:32 +00:00
if ( _blObj [ " blockHeader " ] . get_obj ( ) . count ( " extraData " ) )
tmp . extraData = importByteArray ( _blObj [ " blockHeader " ] . get_obj ( ) [ " extraData " ] . get_str ( ) ) ;
2015-03-04 20:32:26 +00:00
2015-03-05 23:52:32 +00:00
if ( _blObj [ " blockHeader " ] . get_obj ( ) . count ( " mixHash " ) )
tmp . mixHash = h256 ( _blObj [ " blockHeader " ] . get_obj ( ) [ " mixHash " ] . get_str ( ) ) ;
2015-03-04 20:32:26 +00:00
// find new valid nonce
2015-03-29 20:23:59 +00:00
if ( tmp ! = _currentBlockHeader )
2015-03-04 20:32:26 +00:00
{
2015-03-29 20:23:59 +00:00
_currentBlockHeader = tmp ;
2015-03-04 20:32:26 +00:00
ProofOfWork pow ;
std : : pair < MineInfo , Ethash : : Proof > ret ;
2015-03-29 20:23:59 +00:00
while ( ! ProofOfWork : : verify ( _currentBlockHeader ) )
2015-03-04 20:32:26 +00:00
{
2015-03-29 20:23:59 +00:00
ret = pow . mine ( _currentBlockHeader , 1000 , true , true ) ;
Ethash : : assignResult ( ret . second , _currentBlockHeader ) ;
2015-03-04 20:32:26 +00:00
}
}
}
else
{
// take the blockheader as is
2015-03-05 23:52:32 +00:00
const bytes c_blockRLP = createBlockRLPFromFields ( _blObj [ " blockHeader " ] . get_obj ( ) ) ;
2015-03-04 20:32:26 +00:00
const RLP c_bRLP ( c_blockRLP ) ;
2015-03-29 20:23:59 +00:00
_currentBlockHeader . populateFromHeader ( c_bRLP , IgnoreNonce ) ;
2015-03-04 20:32:26 +00:00
}
}
2015-03-04 11:04:15 +00:00
2015-03-05 23:12:51 +00:00
BlockInfo constructBlock ( mObject & _o )
{
BlockInfo ret ;
try
{
// construct genesis block
const bytes c_blockRLP = createBlockRLPFromFields ( _o ) ;
const RLP c_bRLP ( c_blockRLP ) ;
2015-03-06 21:53:49 +00:00
ret . populateFromHeader ( c_bRLP , IgnoreNonce ) ;
2015-03-05 23:12:51 +00:00
}
catch ( Exception const & _e )
{
cnote < < " block population did throw an exception: " < < diagnostic_information ( _e ) ;
BOOST_ERROR ( " Failed block population with Exception: " < < _e . what ( ) ) ;
}
catch ( std : : exception const & _e )
{
BOOST_ERROR ( " Failed block population with Exception: " < < _e . what ( ) ) ;
}
catch ( . . . )
{
BOOST_ERROR ( " block population did throw an unknown exception \n " ) ;
}
return ret ;
}
2015-03-05 23:12:51 +00:00
2015-03-05 23:12:51 +00:00
void updatePoW ( BlockInfo & _bi )
2015-03-05 23:52:32 +00:00
{
2015-03-05 23:12:51 +00:00
ProofOfWork pow ;
std : : pair < MineInfo , Ethash : : Proof > ret ;
while ( ! ProofOfWork : : verify ( _bi ) )
{
2015-03-13 14:35:32 +00:00
ret = pow . mine ( _bi , 10000 , true , true ) ;
2015-03-05 23:12:51 +00:00
Ethash : : assignResult ( ret . second , _bi ) ;
}
2015-04-05 19:08:22 +00:00
_bi . noteDirty ( ) ;
2015-03-05 23:52:32 +00:00
}
2015-03-05 23:12:51 +00:00
2015-03-29 20:23:59 +00:00
void writeBlockHeaderToJson ( mObject & _o , BlockInfo const & _bi )
2015-03-03 23:24:06 +00:00
{
2015-03-05 23:12:51 +00:00
_o [ " parentHash " ] = toString ( _bi . parentHash ) ;
_o [ " uncleHash " ] = toString ( _bi . sha3Uncles ) ;
_o [ " coinbase " ] = toString ( _bi . coinbaseAddress ) ;
_o [ " stateRoot " ] = toString ( _bi . stateRoot ) ;
_o [ " transactionsTrie " ] = toString ( _bi . transactionsRoot ) ;
_o [ " receiptTrie " ] = toString ( _bi . receiptsRoot ) ;
_o [ " bloom " ] = toString ( _bi . logBloom ) ;
_o [ " difficulty " ] = toString ( _bi . difficulty ) ;
_o [ " number " ] = toString ( _bi . number ) ;
_o [ " gasLimit " ] = toString ( _bi . gasLimit ) ;
_o [ " gasUsed " ] = toString ( _bi . gasUsed ) ;
_o [ " timestamp " ] = toString ( _bi . timestamp ) ;
_o [ " extraData " ] = " 0x " + toHex ( _bi . extraData ) ;
_o [ " mixHash " ] = toString ( _bi . mixHash ) ;
_o [ " nonce " ] = toString ( _bi . nonce ) ;
2015-04-05 19:08:22 +00:00
_o [ " hash " ] = toString ( _bi . hash ( ) ) ;
2015-03-03 23:24:06 +00:00
}
2015-03-04 11:04:15 +00:00
2015-03-29 20:23:59 +00:00
RLPStream createFullBlockFromHeader ( BlockInfo const & _bi , bytes const & _txs , bytes const & _uncles )
2015-03-03 23:24:06 +00:00
{
2015-03-05 23:12:51 +00:00
RLPStream rlpStream ;
_bi . streamRLP ( rlpStream , WithNonce ) ;
RLPStream ret ( 3 ) ;
ret . appendRaw ( rlpStream . out ( ) ) ;
ret . appendRaw ( _txs ) ;
ret . appendRaw ( _uncles ) ;
return ret ;
2015-03-03 23:24:06 +00:00
}
2015-03-04 11:04:15 +00:00
2015-03-29 20:23:59 +00:00
} } // Namespace Close
2015-03-05 23:12:51 +00:00
BOOST_AUTO_TEST_SUITE ( BlockChainTests )
2015-03-18 15:50:21 +00:00
BOOST_AUTO_TEST_CASE ( bcInvalidRLPTest )
{
dev : : test : : executeTests ( " bcInvalidRLPTest " , " /BlockTests " , dev : : test : : doBlockchainTests ) ;
}
2015-03-16 16:07:48 +00:00
BOOST_AUTO_TEST_CASE ( bcJS_API_Test )
2015-03-05 23:52:32 +00:00
{
2015-03-16 16:07:48 +00:00
dev : : test : : executeTests ( " bcJS_API_Test " , " /BlockTests " , dev : : test : : doBlockchainTests ) ;
2015-03-05 23:52:32 +00:00
}
2015-03-05 23:12:51 +00:00
2015-03-05 23:52:32 +00:00
BOOST_AUTO_TEST_CASE ( bcValidBlockTest )
{
dev : : test : : executeTests ( " bcValidBlockTest " , " /BlockTests " , dev : : test : : doBlockchainTests ) ;
}
2015-03-05 23:12:51 +00:00
2015-03-05 23:52:32 +00:00
BOOST_AUTO_TEST_CASE ( bcInvalidHeaderTest )
{
dev : : test : : executeTests ( " bcInvalidHeaderTest " , " /BlockTests " , dev : : test : : doBlockchainTests ) ;
}
2015-03-05 23:12:51 +00:00
2015-03-05 23:12:51 +00:00
BOOST_AUTO_TEST_CASE ( bcUncleTest )
{
dev : : test : : executeTests ( " bcUncleTest " , " /BlockTests " , dev : : test : : doBlockchainTests ) ;
}
2015-04-06 06:52:20 +00:00
BOOST_AUTO_TEST_CASE ( bcUncleHeaderValiditiy )
{
dev : : test : : executeTests ( " bcUncleHeaderValiditiy " , " /BlockTests " , dev : : test : : doBlockchainTests ) ;
}
2015-03-19 20:17:27 +00:00
BOOST_AUTO_TEST_CASE ( userDefinedFile )
2015-03-04 11:04:15 +00:00
{
2015-03-19 20:17:27 +00:00
dev : : test : : userDefinedTest ( " --singletest " , dev : : test : : doBlockchainTests ) ;
2015-03-04 11:04:15 +00:00
}
BOOST_AUTO_TEST_SUITE_END ( )