From 2b42a3a22ebdb8cc20a368deb37ddb47d8752802 Mon Sep 17 00:00:00 2001 From: Tranz5 Date: Thu, 22 May 2014 22:17:19 -0400 Subject: [PATCH] Add CheckStake Function --- src/main.cpp | 37 +++++++++++++++++++++++++++++++++++++ src/main.h | 2 ++ 2 files changed, 39 insertions(+) diff --git a/src/main.cpp b/src/main.cpp index f0400ee..9492783 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4374,6 +4374,43 @@ bool CheckWork(CBlock* pblock, CWallet& wallet, CReserveKey& reservekey) return true; } +bool CheckStake(CBlock* pblock, CWallet& wallet) +{ + uint256 proofHash = 0, hashTarget = 0; + uint256 hashBlock = pblock->GetHash(); + + if(!pblock->IsProofOfStake()) + return error("CheckStake() : %s is not a proof-of-stake block", hashBlock.GetHex().c_str()); + + // verify hash target and signature of coinstake tx + if (!CheckProofOfStake(pblock->vtx[1], pblock->nBits, proofHash, hashTarget)) + return error("CheckStake() : proof-of-stake checking failed"); + + //// debug print + printf("CheckStake() : new proof-of-stake block found \n hash: %s \nproofhash: %s \ntarget: %s\n", hashBlock.GetHex().c_str(), proofHash.GetHex().c_str(), hashTarget.GetHex().c_str()); + pblock->print(); + printf("out %s\n", FormatMoney(pblock->vtx[1].GetValueOut()).c_str()); + + // Found a solution + { + LOCK(cs_main); + if (pblock->hashPrevBlock != hashBestChain) + return error("CheckStake() : generated block is stale"); + + // Track how many getdata requests this block gets + { + LOCK(wallet.cs_wallet); + wallet.mapRequestCount[hashBlock] = 0; + } + + // Process this block the same as if we had received it from another node + if (!ProcessBlock(NULL, pblock)) + return error("CheckStake() : ProcessBlock, block not accepted"); + } + + return true; +} + void static ThreadBitcoinMiner(void* parg); static bool fGenerateBitcoins = false; diff --git a/src/main.h b/src/main.h index d984e56..1d2ea59 100644 --- a/src/main.h +++ b/src/main.h @@ -118,6 +118,8 @@ CBlock* CreateNewBlock(CWallet* pwallet, bool fProofOfStake=false); void IncrementExtraNonce(CBlock* pblock, CBlockIndex* pindexPrev, unsigned int& nExtraNonce); void FormatHashBuffers(CBlock* pblock, char* pmidstate, char* pdata, char* phash1); bool CheckWork(CBlock* pblock, CWallet& wallet, CReserveKey& reservekey); +/** Check mined proof-of-stake block */ +bool CheckStake(CBlock* pblock, CWallet& wallet); bool CheckProofOfWork(uint256 hash, unsigned int nBits); int64 GetProofOfWorkReward(unsigned int nBits); int64 GetProofOfStakeReward(int64 nCoinAge, unsigned int nBits, unsigned int nTime, bool bCoinYearOnly=false);