Skip to content

Commit

Permalink
prepare for changing block hash
Browse files Browse the repository at this point in the history
  • Loading branch information
recursive-rat4 committed May 6, 2014
1 parent 2f4d0a8 commit 850c9f6
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 23 deletions.
5 changes: 2 additions & 3 deletions src/kernel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,8 @@ static bool SelectBlockFromCandidates(vector<pair<int64_t, uint256> >& vSortedBy
continue;
// compute the selection hash by hashing its proof-hash and the
// previous proof-of-stake modifier
uint256 hashProof = pindex->IsProofOfStake()? pindex->hashProofOfStake : pindex->GetBlockHash();
CDataStream ss(SER_GETHASH, 0);
ss << hashProof << nStakeModifierPrev;
ss << pindex->hashProof << nStakeModifierPrev;
uint256 hashSelection = Hash(ss.begin(), ss.end());
// the selection hash is divided by 2**32 so that proof-of-stake block
// is always favored over proof-of-work block. this is to preserve
Expand Down Expand Up @@ -373,7 +372,7 @@ unsigned int GetStakeModifierChecksum(const CBlockIndex* pindex)
CDataStream ss(SER_GETHASH, 0);
if (pindex->pprev)
ss << pindex->pprev->nStakeModifierChecksum;
ss << pindex->nFlags << pindex->hashProofOfStake << pindex->nStakeModifier;
ss << pindex->nFlags << (pindex->IsProofOfStake() ? pindex->hashProof : 0) << pindex->nStakeModifier;
uint256 hashChecksum = Hash(ss.begin(), ss.end());
hashChecksum >>= (256 - 32);
return hashChecksum.Get64();
Expand Down
22 changes: 14 additions & 8 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1954,7 +1954,7 @@ bool CBlock::GetCoinAge(uint64_t& nCoinAge) const
return true;
}

bool CBlock::AddToBlockIndex(unsigned int nFile, unsigned int nBlockPos, const uint256& hashProofOfStake)
bool CBlock::AddToBlockIndex(unsigned int nFile, unsigned int nBlockPos, const uint256& hashProof)
{
// Check for duplicate
uint256 hash = GetHash();
Expand All @@ -1980,8 +1980,8 @@ bool CBlock::AddToBlockIndex(unsigned int nFile, unsigned int nBlockPos, const u
if (!pindexNew->SetStakeEntropyBit(GetStakeEntropyBit()))
return error("AddToBlockIndex() : SetStakeEntropyBit() failed");

// ppcoin: record proof-of-stake hash value
pindexNew->hashProofOfStake = hashProofOfStake;
// Record proof hash value
pindexNew->hashProof = hashProof;

// ppcoin: compute stake modifier
uint64_t nStakeModifier = 0;
Expand Down Expand Up @@ -2037,7 +2037,7 @@ bool CBlock::CheckBlock(bool fCheckPOW, bool fCheckMerkleRoot, bool fCheckSig) c
return DoS(100, error("CheckBlock() : size limits failed"));

// Check proof of work matches claimed amount
if (fCheckPOW && IsProofOfWork() && !CheckProofOfWork(GetHash(), nBits))
if (fCheckPOW && IsProofOfWork() && !CheckProofOfWork(GetPoWHash(), nBits))
return DoS(50, error("CheckBlock() : proof of work failed"));

// Check timestamp
Expand Down Expand Up @@ -2151,16 +2151,22 @@ bool CBlock::AcceptBlock()
if (!Checkpoints::CheckHardened(nHeight, hash))
return DoS(100, error("AcceptBlock() : rejected by hardened checkpoint lock-in at %d", nHeight));

uint256 hashProof;
// Verify hash target and signature of coinstake tx
uint256 hashProofOfStake = 0, targetProofOfStake = 0;
if (IsProofOfStake())
{
if (!CheckProofOfStake(vtx[1], nBits, hashProofOfStake, targetProofOfStake))
uint256 targetProofOfStake;
if (!CheckProofOfStake(vtx[1], nBits, hashProof, targetProofOfStake))
{
printf("WARNING: AcceptBlock(): check proof-of-stake failed for block %s\n", hash.ToString().c_str());
return false; // do not error here as we expect this during initial block download
}
}
// PoW is checked in CheckBlock()
if (IsProofOfWork())
{
hashProof = GetPoWHash();
}

bool cpSatisfies = Checkpoints::CheckSync(hash, pindexPrev);

Expand All @@ -2184,7 +2190,7 @@ bool CBlock::AcceptBlock()
unsigned int nBlockPos = 0;
if (!WriteToDisk(nFile, nBlockPos))
return error("AcceptBlock() : WriteToDisk failed");
if (!AddToBlockIndex(nFile, nBlockPos, hashProofOfStake))
if (!AddToBlockIndex(nFile, nBlockPos, hashProof))
return error("AcceptBlock() : AddToBlockIndex failed");

// Relay inventory, but don't relay old inventory during initial block download
Expand Down Expand Up @@ -2562,7 +2568,7 @@ bool LoadBlockIndex(bool fAllowNew)
unsigned int nBlockPos;
if (!block.WriteToDisk(nFile, nBlockPos))
return error("LoadBlockIndex() : writing genesis block to disk failed");
if (!block.AddToBlockIndex(nFile, nBlockPos, 0))
if (!block.AddToBlockIndex(nFile, nBlockPos, hashGenesisBlock))
return error("LoadBlockIndex() : genesis block not accepted");

// ppcoin: initialize synchronized checkpoint
Expand Down
23 changes: 14 additions & 9 deletions src/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -904,6 +904,11 @@ class CBlock
}

uint256 GetHash() const
{
return GetPoWHash();
}

uint256 GetPoWHash() const
{
return scrypt_blockhash(CVOIDBEGIN(nVersion));
}
Expand Down Expand Up @@ -1047,7 +1052,7 @@ class CBlock
}

// Check the header
if (fReadTransactions && IsProofOfWork() && !CheckProofOfWork(GetHash(), nBits))
if (fReadTransactions && IsProofOfWork() && !CheckProofOfWork(GetPoWHash(), nBits))
return error("CBlock::ReadFromDisk() : errors in block header");

return true;
Expand Down Expand Up @@ -1081,7 +1086,7 @@ class CBlock
bool ConnectBlock(CTxDB& txdb, CBlockIndex* pindex, bool fJustCheck=false);
bool ReadFromDisk(const CBlockIndex* pindex, bool fReadTransactions=true);
bool SetBestChain(CTxDB& txdb, CBlockIndex* pindexNew);
bool AddToBlockIndex(unsigned int nFile, unsigned int nBlockPos, const uint256& hashProofOfStake);
bool AddToBlockIndex(unsigned int nFile, unsigned int nBlockPos, const uint256& hashProof);
bool CheckBlock(bool fCheckPOW=true, bool fCheckMerkleRoot=true, bool fCheckSig=true) const;
bool AcceptBlock();
bool GetCoinAge(uint64_t& nCoinAge) const; // ppcoin: calculate total coin age spent in block
Expand Down Expand Up @@ -1132,7 +1137,8 @@ class CBlockIndex
// proof-of-stake specific fields
COutPoint prevoutStake;
unsigned int nStakeTime;
uint256 hashProofOfStake;

uint256 hashProof;

// block header
int nVersion;
Expand All @@ -1155,7 +1161,7 @@ class CBlockIndex
nFlags = 0;
nStakeModifier = 0;
nStakeModifierChecksum = 0;
hashProofOfStake = 0;
hashProof = 0;
prevoutStake.SetNull();
nStakeTime = 0;

Expand All @@ -1180,7 +1186,7 @@ class CBlockIndex
nFlags = 0;
nStakeModifier = 0;
nStakeModifierChecksum = 0;
hashProofOfStake = 0;
hashProof = 0;
if (block.IsProofOfStake())
{
SetProofOfStake();
Expand Down Expand Up @@ -1318,12 +1324,12 @@ class CBlockIndex

std::string ToString() const
{
return strprintf("CBlockIndex(nprev=%p, pnext=%p, nFile=%u, nBlockPos=%-6d nHeight=%d, nMint=%s, nMoneySupply=%s, nFlags=(%s)(%d)(%s), nStakeModifier=%016"PRIx64", nStakeModifierChecksum=%08x, hashProofOfStake=%s, prevoutStake=(%s), nStakeTime=%d merkle=%s, hashBlock=%s)",
return strprintf("CBlockIndex(nprev=%p, pnext=%p, nFile=%u, nBlockPos=%-6d nHeight=%d, nMint=%s, nMoneySupply=%s, nFlags=(%s)(%d)(%s), nStakeModifier=%016"PRIx64", nStakeModifierChecksum=%08x, hashProof=%s, prevoutStake=(%s), nStakeTime=%d merkle=%s, hashBlock=%s)",
pprev, pnext, nFile, nBlockPos, nHeight,
FormatMoney(nMint).c_str(), FormatMoney(nMoneySupply).c_str(),
GeneratedStakeModifier() ? "MOD" : "-", GetStakeEntropyBit(), IsProofOfStake()? "PoS" : "PoW",
nStakeModifier, nStakeModifierChecksum,
hashProofOfStake.ToString().c_str(),
hashProof.ToString().c_str(),
prevoutStake.ToString().c_str(), nStakeTime,
hashMerkleRoot.ToString().c_str(),
GetBlockHash().ToString().c_str());
Expand Down Expand Up @@ -1377,14 +1383,13 @@ class CDiskBlockIndex : public CBlockIndex
{
READWRITE(prevoutStake);
READWRITE(nStakeTime);
READWRITE(hashProofOfStake);
}
else if (fRead)
{
const_cast<CDiskBlockIndex*>(this)->prevoutStake.SetNull();
const_cast<CDiskBlockIndex*>(this)->nStakeTime = 0;
const_cast<CDiskBlockIndex*>(this)->hashProofOfStake = 0;
}
READWRITE(hashProof);

// block header
READWRITE(this->nVersion);
Expand Down
2 changes: 1 addition & 1 deletion src/rpcblockchain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ Object blockToJSON(const CBlock& block, const CBlockIndex* blockindex, bool fPri
result.push_back(Pair("nextblockhash", blockindex->pnext->GetBlockHash().GetHex()));

result.push_back(Pair("flags", strprintf("%s%s", blockindex->IsProofOfStake()? "proof-of-stake" : "proof-of-work", blockindex->GeneratedStakeModifier()? " stake-modifier": "")));
result.push_back(Pair("proofhash", blockindex->IsProofOfStake()? blockindex->hashProofOfStake.GetHex() : blockindex->GetBlockHash().GetHex()));
result.push_back(Pair("proofhash", blockindex->hashProof.GetHex()));
result.push_back(Pair("entropybit", (int)blockindex->GetStakeEntropyBit()));
result.push_back(Pair("modifier", strprintf("%016"PRIx64, blockindex->nStakeModifier)));
result.push_back(Pair("modifierchecksum", strprintf("%08x", blockindex->nStakeModifierChecksum)));
Expand Down
2 changes: 1 addition & 1 deletion src/txdb-leveldb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ bool CTxDB::LoadBlockIndex()
pindexNew->nStakeModifier = diskindex.nStakeModifier;
pindexNew->prevoutStake = diskindex.prevoutStake;
pindexNew->nStakeTime = diskindex.nStakeTime;
pindexNew->hashProofOfStake = diskindex.hashProofOfStake;
pindexNew->hashProof = diskindex.hashProof;
pindexNew->nVersion = diskindex.nVersion;
pindexNew->hashMerkleRoot = diskindex.hashMerkleRoot;
pindexNew->nTime = diskindex.nTime;
Expand Down
2 changes: 1 addition & 1 deletion src/version.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ extern const std::string CLIENT_DATE;
//
// database format versioning
//
static const int DATABASE_VERSION = 70508;
static const int DATABASE_VERSION = 70509;

//
// network protocol versioning
Expand Down

0 comments on commit 850c9f6

Please sign in to comment.