Skip to content

Commit

Permalink
Fixed need to bootstrap and restart during sync
Browse files Browse the repository at this point in the history
  • Loading branch information
StealthSend committed Nov 25, 2015
1 parent e18175f commit 540f745
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 34 deletions.
11 changes: 2 additions & 9 deletions src/kernel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -397,19 +397,12 @@ bool CheckStakeKernelHash(unsigned int nBits, const CBlock& blockFrom, unsigned
}

// Check kernel hash target and coinstake signature
bool CheckProofOfStake(const CTransaction& tx, unsigned int nBits, uint256& hashProofOfStake,
bool fIsInitialDownload)
bool CheckProofOfStake(const CTransaction& tx, unsigned int nBits, uint256& hashProofOfStake)
{
if (!tx.IsCoinStake())
return error("CheckProofOfStake() : called on non-coinstake %s", tx.GetHash().ToString().c_str());

/*
// give a pass to blocks already in the blockchain, duh (?).
// this prevents many restarts just to load a chain, which has the same effect
if (fIsInitialDownload) {
return true;
}
*/


// Kernel (input 0) must match the stake hash target per coin age (nBits)
const CTxIn& txin = tx.vin[0];
Expand Down
3 changes: 1 addition & 2 deletions src/kernel.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ bool CheckStakeKernelHash(unsigned int nBits, const CBlock& blockFrom, unsigned

// Check kernel hash target and coinstake signature
// Sets hashProofOfStake on success return
bool CheckProofOfStake(const CTransaction& tx, unsigned int nBits, uint256& hashProofOfStake,
bool fIsInitialDowload);
bool CheckProofOfStake(const CTransaction& tx, unsigned int nBits, uint256& hashProofOfStake);

// Check whether the coinstake timestamp meets protocol
bool CheckCoinStakeTimestamp(int64 nTimeBlock, int64 nTimeTx);
Expand Down
45 changes: 23 additions & 22 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2007,7 +2007,7 @@ bool CBlock::GetCoinAge(uint64& nCoinAge) const
return true;
}

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

// ppcoin: record proof-of-stake hash value
// Record proof hash value
if (pindexNew->IsProofOfStake())
{
if (!mapProofOfStake.count(hash))
return error("AddToBlockIndex() : hashProofOfStake not found in map");
pindexNew->hashProofOfStake = mapProofOfStake[hash];
pindexNew->hashProofOfStake = hashProof;
}

// ppcoin: compute stake modifier
uint64 nStakeModifier = 0;
bool fGeneratedStakeModifier = false;
Expand Down Expand Up @@ -2214,6 +2211,24 @@ 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
if (IsProofOfStake())
{
uint256 targetProofOfStake;
if (!CheckProofOfStake(vtx[1], nBits, hashProof))
{
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 = GetHash();
}


// ppcoin: check that the block satisfies synchronized checkpoint
if (!Checkpoints::CheckSync(hash, pindexPrev))
{
Expand Down Expand Up @@ -2243,7 +2258,7 @@ bool CBlock::AcceptBlock()
unsigned int nBlockPos = 0;
if (!WriteToDisk(nFile, nBlockPos))
return error("AcceptBlock() : WriteToDisk failed");
if (!AddToBlockIndex(nFile, nBlockPos))
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 @@ -2316,20 +2331,6 @@ bool ProcessBlock(CNode* pfrom, CBlock* pblock, bool fIsBootstrap)
// Preliminary checks
if (!pblock->CheckBlock())
return error("ProcessBlock() : CheckBlock FAILED");

// ppcoin: verify hash target and signature of coinstake tx
if (pblock->IsProofOfStake())
{
uint256 hashProofOfStake = 0;
if (!CheckProofOfStake(pblock->vtx[1], pblock->nBits, hashProofOfStake, IsInitialBlockDownload()))
{
printf("WARNING: ProcessBlock(): 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
}
if (!mapProofOfStake.count(hash)) // add to mapProofOfStake
mapProofOfStake.insert(make_pair(hash, hashProofOfStake));
}

CBlockLocator locator;
int nHeight = locator.GetBlockIndex()->nHeight;

Expand Down Expand Up @@ -2715,7 +2716,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))
if (!block.AddToBlockIndex(nFile, nBlockPos, hashGenesisBlock))
return error("LoadBlockIndex() : genesis block not accepted");

// ppcoin: initialize synchronized checkpoint
Expand Down
2 changes: 1 addition & 1 deletion src/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -1139,7 +1139,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);
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();
Expand Down

0 comments on commit 540f745

Please sign in to comment.