Skip to content

Commit

Permalink
Merge pull request bitcoin#14 from qtumproject/minsk-team/QTUMCORE-44
Browse files Browse the repository at this point in the history
Add hashStateRoot to CBlockHeader (Qtum Core / QTUMCORE-44)
  • Loading branch information
designsters committed Apr 3, 2017
2 parents a1f4b4c + a50b0a2 commit 20f7c8e
Show file tree
Hide file tree
Showing 13 changed files with 169 additions and 76 deletions.
6 changes: 6 additions & 0 deletions src/chain.h
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ class CBlockIndex
unsigned int nTime;
unsigned int nBits;
unsigned int nNonce;
uint256 hashStateRoot; // qtum

//! (memory only) Sequential id assigned to distinguish order in which blocks are received.
int32_t nSequenceId;
Expand Down Expand Up @@ -226,6 +227,7 @@ class CBlockIndex
nTime = 0;
nBits = 0;
nNonce = 0;
hashStateRoot = uint256(); // qtum
}

CBlockIndex()
Expand All @@ -242,6 +244,7 @@ class CBlockIndex
nTime = block.nTime;
nBits = block.nBits;
nNonce = block.nNonce;
hashStateRoot = block.hashStateRoot; // qtum
}

CDiskBlockPos GetBlockPos() const {
Expand Down Expand Up @@ -272,6 +275,7 @@ class CBlockIndex
block.nTime = nTime;
block.nBits = nBits;
block.nNonce = nNonce;
block.hashStateRoot = hashStateRoot; // qtum
return block;
}

Expand Down Expand Up @@ -388,6 +392,7 @@ class CDiskBlockIndex : public CBlockIndex
READWRITE(nTime);
READWRITE(nBits);
READWRITE(nNonce);
READWRITE(hashStateRoot); // qtum
}

uint256 GetBlockHash() const
Expand All @@ -399,6 +404,7 @@ class CDiskBlockIndex : public CBlockIndex
block.nTime = nTime;
block.nBits = nBits;
block.nNonce = nNonce;
block.hashStateRoot = hashStateRoot; // qtum
return block.GetHash();
}

Expand Down
20 changes: 13 additions & 7 deletions src/chainparams.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@

#include "chainparamsseeds.h"

///////////////////////////////////////////// // qtum
#include <libdevcore/SHA3.h>
#include <libdevcore/RLP.h>
#include "arith_uint256.h"
/////////////////////////////////////////////

static CBlock CreateGenesisBlock(const char* pszTimestamp, const CScript& genesisOutputScript, uint32_t nTime, uint32_t nNonce, uint32_t nBits, int32_t nVersion, const CAmount& genesisReward)
{
CMutableTransaction txNew;
Expand All @@ -34,6 +40,7 @@ static CBlock CreateGenesisBlock(const char* pszTimestamp, const CScript& genesi
genesis.vtx.push_back(MakeTransactionRef(std::move(txNew)));
genesis.hashPrevBlock.SetNull();
genesis.hashMerkleRoot = BlockMerkleRoot(genesis);
genesis.hashStateRoot = uint256(h256Touint(dev::sha3(dev::rlp("")))); // qtum
return genesis;
}

Expand Down Expand Up @@ -116,11 +123,10 @@ class CMainParams : public CChainParams {
nDefaultPort = 3888;
nPruneAfterHeight = 100000;


genesis = CreateGenesisBlock(1490247077, 6848315, 0x1f00ffff, 1, 50 * COIN);
genesis = CreateGenesisBlock(1490247077, 6863426, 0x1f00ffff, 1, 50 * COIN);
consensus.hashGenesisBlock = genesis.GetHash();

assert(consensus.hashGenesisBlock == uint256S("0x00008891bb0890952941a813a14bf116ba550067b4f7b4cbb0136a43a812c2eb"));
assert(consensus.hashGenesisBlock == uint256S("0x00004eac3ac0388288dde0bd56eca180c85a9386bb64d320c8171d1e2c797c7d"));
assert(genesis.hashMerkleRoot == uint256S("0x66c9b04253902df4aca489f3226b95830eecf780ea90b6bbb0432ccc35b5d2a6"));

// Note that of those with the service bits flag, most only support a subset of possible options
Expand Down Expand Up @@ -205,10 +211,10 @@ class CTestNetParams : public CChainParams {
nDefaultPort = 13888;
nPruneAfterHeight = 1000;

genesis = CreateGenesisBlock(1490247078, 6914006, 0x1f00ffff, 1, 50 * COIN);
genesis = CreateGenesisBlock(1490247078, 6971564, 0x1f00ffff, 1, 50 * COIN);
consensus.hashGenesisBlock = genesis.GetHash();

assert(consensus.hashGenesisBlock == uint256S("0x000072c36e9e98359d18e08eb3a07ec802071a195aa003fd722315caa1667119"));
assert(consensus.hashGenesisBlock == uint256S("0x0000c2b6935b02247c66ea84d17d674f573df0d50e6313bbfa0c65fb99dedce0"));
assert(genesis.hashMerkleRoot == uint256S("0x66c9b04253902df4aca489f3226b95830eecf780ea90b6bbb0432ccc35b5d2a6"));

vFixedSeeds.clear();
Expand Down Expand Up @@ -291,10 +297,10 @@ class CRegTestParams : public CChainParams {
nDefaultPort = 23888;
nPruneAfterHeight = 1000;

genesis = CreateGenesisBlock(1490247077, 6, 0x207fffff, 1, 50 * COIN);
genesis = CreateGenesisBlock(1490247077, 8, 0x207fffff, 1, 50 * COIN);
consensus.hashGenesisBlock = genesis.GetHash();

assert(consensus.hashGenesisBlock == uint256S("0x72303d3080cdd1161da936dd388ae0a58a9246dc4fa61a658cdac17b55d93cb9"));
assert(consensus.hashGenesisBlock == uint256S("0x32ace58914ca833c3985b9f29be8771ee8016222d9e57d802b755a6b1f43c8cf"));
assert(genesis.hashMerkleRoot == uint256S("0x66c9b04253902df4aca489f3226b95830eecf780ea90b6bbb0432ccc35b5d2a6"));

vFixedSeeds.clear(); //!< Regtest mode doesn't have any fixed seeds.
Expand Down
7 changes: 6 additions & 1 deletion src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1459,7 +1459,12 @@ bool AppInitMain(boost::thread_group& threadGroup, CScheduler& scheduler)
const dev::h256 hashDB(dev::sha3(dev::rlp("")));
dev::eth::BaseState existsQtumstate = fStatus ? dev::eth::BaseState::PreExisting : dev::eth::BaseState::Empty;
globalState = std::unique_ptr<QtumState>(new QtumState(dev::u256(0), QtumState::openDB(dirQtum, hashDB, dev::WithExisting::Trust), existsQtumstate));
globalState->setRoot(hashDB); // qtum temp

if(chainActive.Tip() != NULL)
globalState->setRoot(uintToh256(chainActive.Tip()->hashStateRoot));
else
globalState->setRoot(uintToh256(chainparams.GenesisBlock().hashStateRoot));
globalState->db().commit();
///////////////////////////////////////////////////////////

// Initialize the block index (no-op if non-empty database was already loaded)
Expand Down
21 changes: 16 additions & 5 deletions src/miner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,13 @@ std::unique_ptr<CBlockTemplate> BlockAssembler::CreateNewBlock(const CScript& sc
// transaction (which in most cases can be a no-op).
fIncludeWitness = IsWitnessEnabled(pindexPrev, chainparams.GetConsensus());

//////////////////////////////////////////////////////// qtum
dev::h256 oldHashStateRoot(globalState->rootHash());
addPriorityTxs();
addPackageTxs();
pblock->hashStateRoot = uint256(h256Touint(dev::h256(globalState->rootHash())));
globalState->setRoot(oldHashStateRoot);
////////////////////////////////////////////////////////

nLastBlockTx = nBlockTx;
nLastBlockSize = nBlockSize;
Expand Down Expand Up @@ -196,15 +201,11 @@ std::unique_ptr<CBlockTemplate> BlockAssembler::CreateNewBlock(const CScript& sc
pblock->nNonce = 0;
pblocktemplate->vTxSigOpsCost[0] = WITNESS_SCALE_FACTOR * GetLegacySigOpCount(*pblock->vtx[0]);

dev::h256 oldHashState(globalState->rootHash()); // qtum temp

CValidationState state;
if (!TestBlockValidity(state, chainparams, *pblock, pindexPrev, false, false)) {
throw std::runtime_error(strprintf("%s: TestBlockValidity failed: %s", __func__, FormatStateMessage(state)));
}

globalState->setRoot(oldHashState); // qtum temp


return std::move(pblocktemplate);
}

Expand Down Expand Up @@ -320,6 +321,16 @@ bool BlockAssembler::TestForBlock(CTxMemPool::txiter iter)

void BlockAssembler::AddToBlock(CTxMemPool::txiter iter)
{

////////////////////////////////////////////////////////////// // qtum
const CTransaction& tx = iter->GetTx();
if(tx.HasCreateOrCall()){
ByteCodeExec exec;
QtumTxConverter convert(tx, NULL);
exec.performByteCode(convert.extractionQtumTransactions());
}
//////////////////////////////////////////////////////////////

pblock->vtx.emplace_back(iter->GetSharedTx());
pblocktemplate->vTxFees.push_back(iter->GetFee());
pblocktemplate->vTxSigOpsCost.push_back(iter->GetSigOpCost());
Expand Down
3 changes: 2 additions & 1 deletion src/primitives/block.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@ uint256 CBlockHeader::GetHash() const
std::string CBlock::ToString() const
{
std::stringstream s;
s << strprintf("CBlock(hash=%s, ver=0x%08x, hashPrevBlock=%s, hashMerkleRoot=%s, nTime=%u, nBits=%08x, nNonce=%u, vtx=%u)\n",
s << strprintf("CBlock(hash=%s, ver=0x%08x, hashPrevBlock=%s, hashMerkleRoot=%s, nTime=%u, nBits=%08x, nNonce=%u, hashStateRoot=%s, vtx=%u)\n",
GetHash().ToString(),
nVersion,
hashPrevBlock.ToString(),
hashMerkleRoot.ToString(),
nTime, nBits, nNonce,
hashStateRoot.ToString(), // qtum
vtx.size());
for (unsigned int i = 0; i < vtx.size(); i++)
{
Expand Down
4 changes: 4 additions & 0 deletions src/primitives/block.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class CBlockHeader
uint32_t nTime;
uint32_t nBits;
uint32_t nNonce;
uint256 hashStateRoot; // qtum

CBlockHeader()
{
Expand All @@ -43,6 +44,7 @@ class CBlockHeader
READWRITE(nTime);
READWRITE(nBits);
READWRITE(nNonce);
READWRITE(hashStateRoot); // qtum
}

void SetNull()
Expand All @@ -53,6 +55,7 @@ class CBlockHeader
nTime = 0;
nBits = 0;
nNonce = 0;
hashStateRoot.SetNull(); // qtum
}

bool IsNull() const
Expand Down Expand Up @@ -113,6 +116,7 @@ class CBlock : public CBlockHeader
block.nTime = nTime;
block.nBits = nBits;
block.nNonce = nNonce;
block.hashStateRoot = hashStateRoot; // qtum
return block;
}

Expand Down
2 changes: 2 additions & 0 deletions src/rpc/blockchain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ UniValue blockheaderToJSON(const CBlockIndex* blockindex)
result.push_back(Pair("bits", strprintf("%08x", blockindex->nBits)));
result.push_back(Pair("difficulty", GetDifficulty(blockindex)));
result.push_back(Pair("chainwork", blockindex->nChainWork.GetHex()));
result.push_back(Pair("hashStateRoot", blockindex->hashStateRoot.GetHex()));

if (blockindex->pprev)
result.push_back(Pair("previousblockhash", blockindex->pprev->GetBlockHash().GetHex()));
Expand All @@ -118,6 +119,7 @@ UniValue blockToJSON(const CBlock& block, const CBlockIndex* blockindex, bool tx
result.push_back(Pair("version", block.nVersion));
result.push_back(Pair("versionHex", strprintf("%08x", block.nVersion)));
result.push_back(Pair("merkleroot", block.hashMerkleRoot.GetHex()));
result.push_back(Pair("hashStateRoot", block.hashStateRoot.GetHex())); // qtum
UniValue txs(UniValue::VARR);
for(const auto& tx : block.vtx)
{
Expand Down
Loading

0 comments on commit 20f7c8e

Please sign in to comment.