From: alex Date: Fri, 23 Aug 2013 17:59:27 +0000 (+0400) Subject: Minor optimization in PoS kernel: call scrypt_blockhash once time instead of multiple... X-Git-Tag: v0.4.4.3~4 X-Git-Url: https://git.novaco.in/?p=novacoin.git;a=commitdiff_plain;h=0c0eb55c15b7cc09e8f422f1e2e54fe569f06c2e Minor optimization in PoS kernel: call scrypt_blockhash once time instead of multiple times --- diff --git a/src/kernel.cpp b/src/kernel.cpp index ef08a86..71a14e3 100644 --- a/src/kernel.cpp +++ b/src/kernel.cpp @@ -280,6 +280,7 @@ bool CheckStakeKernelHash(unsigned int nBits, const CBlock& blockFrom, unsigned int64 nValueIn = txPrev.vout[prevout.n].nValue; int64 nTimeWeight; + uint256 hashBlockFrom = blockFrom.GetHash(); // Kernel hash weight starts from 0 at the 30-day min age // this change increases active coins participating the hash and helps @@ -305,7 +306,7 @@ bool CheckStakeKernelHash(unsigned int nBits, const CBlock& blockFrom, unsigned int nStakeModifierHeight = 0; int64 nStakeModifierTime = 0; - if (!GetKernelStakeModifier(blockFrom.GetHash(), nStakeModifier, nStakeModifierHeight, nStakeModifierTime, fPrintProofOfStake)) + if (!GetKernelStakeModifier(hashBlockFrom, nStakeModifier, nStakeModifierHeight, nStakeModifierTime, fPrintProofOfStake)) return false; ss << nStakeModifier; @@ -316,7 +317,7 @@ bool CheckStakeKernelHash(unsigned int nBits, const CBlock& blockFrom, unsigned printf("CheckStakeKernelHash() : using modifier 0x%016"PRI64x" at height=%d timestamp=%s for block from height=%d timestamp=%s\n", nStakeModifier, nStakeModifierHeight, DateTimeStrFormat(nStakeModifierTime).c_str(), - mapBlockIndex[blockFrom.GetHash()]->nHeight, + mapBlockIndex[hashBlockFrom]->nHeight, DateTimeStrFormat(blockFrom.GetBlockTime()).c_str()); printf("CheckStakeKernelHash() : check protocol=%s modifier=0x%016"PRI64x" nTimeBlockFrom=%u nTxPrevOffset=%u nTimeTxPrev=%u nPrevout=%u nTimeTx=%u hashProof=%s\n", "0.3", @@ -333,7 +334,7 @@ bool CheckStakeKernelHash(unsigned int nBits, const CBlock& blockFrom, unsigned printf("CheckStakeKernelHash() : using modifier 0x%016"PRI64x" at height=%d timestamp=%s for block from height=%d timestamp=%s\n", nStakeModifier, nStakeModifierHeight, DateTimeStrFormat(nStakeModifierTime).c_str(), - mapBlockIndex[blockFrom.GetHash()]->nHeight, + mapBlockIndex[hashBlockFrom]->nHeight, DateTimeStrFormat(blockFrom.GetBlockTime()).c_str()); printf("CheckStakeKernelHash() : pass protocol=%s modifier=0x%016"PRI64x" nTimeBlockFrom=%u nTxPrevOffset=%u nTimeTxPrev=%u nPrevout=%u nTimeTx=%u hashProof=%s\n", "0.3", diff --git a/src/main.cpp b/src/main.cpp index 894bad6..4b0caf8 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3567,11 +3567,12 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv) { CBlock block; vRecv >> block; + uint256 hashBlock = block.GetHash(); - printf("received block %s\n", block.GetHash().ToString().substr(0,20).c_str()); + printf("received block %s\n", hashBlock.ToString().substr(0,20).c_str()); // block.print(); - CInv inv(MSG_BLOCK, block.GetHash()); + CInv inv(MSG_BLOCK, hashBlock); pfrom->AddInventoryKnown(inv); if (ProcessBlock(pfrom, &block)) @@ -4465,17 +4466,17 @@ void FormatHashBuffers(CBlock* pblock, char* pmidstate, char* pdata, char* phash bool CheckWork(CBlock* pblock, CWallet& wallet, CReserveKey& reservekey) { - uint256 hash = pblock->GetHash(); + uint256 hashBlock = pblock->GetHash(); uint256 hashTarget = CBigNum().SetCompact(pblock->nBits).getuint256(); if(!pblock->IsProofOfWork()) - return error("CheckWork() : %s is not a proof-of-work block", hash.GetHex().c_str()); + return error("CheckWork() : %s is not a proof-of-work block", hashBlock.GetHex().c_str()); - if (hash > hashTarget) + if (hashBlock > hashTarget) return error("CheckWork() : proof-of-work not meeting target"); //// debug print - printf("CheckWork() : new proof-of-stake block found \n hash: %s \ntarget: %s\n", hash.GetHex().c_str(), hashTarget.GetHex().c_str()); + printf("CheckWork() : new proof-of-stake block found \n hash: %s \ntarget: %s\n", hashBlock.GetHex().c_str(), hashTarget.GetHex().c_str()); pblock->print(); printf("generated %s\n", FormatMoney(pblock->vtx[0].vout[0].nValue).c_str()); @@ -4491,7 +4492,7 @@ bool CheckWork(CBlock* pblock, CWallet& wallet, CReserveKey& reservekey) // Track how many getdata requests this block gets { LOCK(wallet.cs_wallet); - wallet.mapRequestCount[pblock->GetHash()] = 0; + wallet.mapRequestCount[hashBlock] = 0; } // Process this block the same as if we had received it from another node @@ -4505,17 +4506,17 @@ bool CheckWork(CBlock* pblock, CWallet& wallet, CReserveKey& reservekey) bool CheckStake(CBlock* pblock, CWallet& wallet) { uint256 proofHash = 0, hashTarget = 0; - uint256 hash = pblock->GetHash(); + uint256 hashBlock = pblock->GetHash(); if(!pblock->IsProofOfStake()) - return error("CheckStake() : %s is not a proof-of-stake block", hash.GetHex().c_str()); + 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", hash.GetHex().c_str(), proofHash.GetHex().c_str(), hashTarget.GetHex().c_str()); + 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()); @@ -4528,7 +4529,7 @@ bool CheckStake(CBlock* pblock, CWallet& wallet) // Track how many getdata requests this block gets { LOCK(wallet.cs_wallet); - wallet.mapRequestCount[pblock->GetHash()] = 0; + wallet.mapRequestCount[hashBlock] = 0; } // Process this block the same as if we had received it from another node diff --git a/src/main.h b/src/main.h index 3711793..0ed1777 100644 --- a/src/main.h +++ b/src/main.h @@ -856,6 +856,7 @@ public: // memory only mutable std::vector vMerkleTree; + uint256 hashBlock; // Denial-of-service detection: mutable int nDoS; @@ -901,6 +902,7 @@ public: vchBlockSig.clear(); vMerkleTree.clear(); nDoS = 0; + hashBlock = 0; } bool IsNull() const @@ -908,7 +910,7 @@ public: return (nBits == 0); } - uint256 GetHash() const + uint256 GetHash(bool fRehash=false) const { return scrypt_blockhash(CVOIDBEGIN(nVersion)); } diff --git a/src/scrypt_mine.cpp b/src/scrypt_mine.cpp index cd556fa..6636097 100644 --- a/src/scrypt_mine.cpp +++ b/src/scrypt_mine.cpp @@ -49,11 +49,11 @@ extern "C" void scrypt_core(uint32_t *X, uint32_t *V); r = 1, p = 1, N = 1024 */ -static uint256 scrypt(const void* input, size_t inputlen, void *scratchpad) +uint256 scrypt(const void* input, size_t inputlen, void *scratchpad) { uint32_t *V; uint32_t X[32]; - uint256 result; + uint256 result = 0; V = (uint32_t *)(((uintptr_t)(scratchpad) + 63) & ~ (uintptr_t)(63)); PBKDF2_SHA256((const uint8_t*)input, inputlen, (const uint8_t*)input, inputlen, 1, (uint8_t *)X, 128);