From 056f3b1ef51d160dad763ed43cacc151735999de Mon Sep 17 00:00:00 2001 From: MASM fan Date: Tue, 13 Jan 2015 15:19:49 -0800 Subject: [PATCH] Small refactoring * We don't need 64 bit integers here; * Remove settings structure. --- src/kernel.cpp | 12 ++++++------ src/kernel.h | 17 +---------------- src/main.cpp | 4 ++-- src/main.h | 2 +- src/miner.cpp | 2 +- src/wallet.cpp | 11 ++--------- src/wallet.h | 10 +++++++++- 7 files changed, 22 insertions(+), 36 deletions(-) diff --git a/src/kernel.cpp b/src/kernel.cpp index a2cc24f..b1064fe 100644 --- a/src/kernel.cpp +++ b/src/kernel.cpp @@ -434,7 +434,7 @@ bool CheckStakeKernelHash(uint32_t nBits, const CBlock& blockFrom, uint32_t nTxP } // Scan given coins set for kernel solution -bool ScanForStakeKernelHash(MetaMap &mapMeta, KernelSearchSettings &settings, CoinsSet::value_type &kernelcoin, uint32_t &nTimeTx, uint32_t &nBlockTime, uint64_t &nKernelsTried, uint64_t &nCoinDaysTried) +bool ScanForStakeKernelHash(MetaMap &mapMeta, uint32_t nBits, uint32_t nTime, uint32_t nSearchInterval, CoinsSet::value_type &kernelcoin, uint32_t &nTimeTx, uint32_t &nBlockTime, uint64_t &nKernelsTried, uint64_t &nCoinDaysTried) { uint256 hashProofOfStake = 0; @@ -451,21 +451,21 @@ bool ScanForStakeKernelHash(MetaMap &mapMeta, KernelSearchSettings &settings, Co // Get coin CoinsSet::value_type pcoin = meta_item->second.first.second; - static int nMaxStakeSearchInterval = 60; + static unsigned int nMaxStakeSearchInterval = 60; // only count coins meeting min age requirement - if (nStakeMinAge + block.nTime > settings.nTime - nMaxStakeSearchInterval) + if (nStakeMinAge + block.nTime > nTime - nMaxStakeSearchInterval) continue; // Transaction offset inside block uint32_t nTxOffset = txindex.pos.nTxPos - txindex.pos.nBlockPos; // Current timestamp scanning interval - unsigned int nCurrentSearchInterval = min((int64_t)settings.nSearchInterval, (int64_t)nMaxStakeSearchInterval); + unsigned int nCurrentSearchInterval = min(nSearchInterval, nMaxStakeSearchInterval); nBlockTime = block.nTime; CBigNum bnTargetPerCoinDay; - bnTargetPerCoinDay.SetCompact(settings.nBits); + bnTargetPerCoinDay.SetCompact(nBits); int64_t nValueIn = pcoin.first->vout[pcoin.second].nValue; // Search backward in time from the given timestamp @@ -473,7 +473,7 @@ bool ScanForStakeKernelHash(MetaMap &mapMeta, KernelSearchSettings &settings, Co // Stopping search in case of shutting down or cache invalidation for (unsigned int n=0; nnTime, (int64_t)nTimeTx) / COIN / (24 * 60 * 60); CBigNum bnTargetProofOfStake = bnCoinDayWeight * bnTargetPerCoinDay; diff --git a/src/kernel.h b/src/kernel.h index e784f2a..1b639a6 100644 --- a/src/kernel.h +++ b/src/kernel.h @@ -33,23 +33,8 @@ bool GetKernelStakeModifier(uint256 hashBlockFrom, uint64_t& nStakeModifier); // Sets hashProofOfStake on success return bool CheckStakeKernelHash(unsigned int nBits, const CBlock& blockFrom, uint32_t nTxPrevOffset, const CTransaction& txPrev, const COutPoint& prevout, uint32_t nTimeTx, uint256& hashProofOfStake, uint256& targetProofOfStake, bool fPrintProofOfStake=false); -// Coins scanning options -typedef struct KernelSearchSettings { - uint32_t nBits; // Packed difficulty - uint32_t nTime; // Basic time - uint32_t nOffset; // Offset inside CoinsSet (isn't used yet) - uint32_t nLimit; // Coins to scan (isn't used yet) - uint32_t nSearchInterval; // Number of seconds allowed to go into the past -} KernelSearchSettings; - -typedef std::set > CoinsSet; - -// Preloaded coins metadata -// txid => ((txindex, (tx, vout.n)), (block, modifier)) -typedef std::map, std::pair >, std::pair > > MetaMap; - // Scan given coins set for kernel solution -bool ScanForStakeKernelHash(MetaMap &mapMeta, KernelSearchSettings &settings, CoinsSet::value_type &kernelcoin, uint32_t &nTimeTx, uint32_t &nBlockTime, uint64_t &nKernelsTried, uint64_t &nCoinDaysTried); +bool ScanForStakeKernelHash(MetaMap &mapMeta, uint32_t nBits, uint32_t nTime, uint32_t nSearchInterval, CoinsSet::value_type &kernelcoin, uint32_t &nTimeTx, uint32_t &nBlockTime, uint64_t &nKernelsTried, uint64_t &nCoinDaysTried); // Check kernel hash target and coinstake signature // Sets hashProofOfStake on success return diff --git a/src/main.cpp b/src/main.cpp index 6390366..a091f6d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2636,11 +2636,11 @@ bool CBlock::SignBlock(CWallet& wallet) if (IsProofOfStake()) return true; - static int64_t nLastCoinStakeSearchTime = GetAdjustedTime(); // startup timestamp + static uint32_t nLastCoinStakeSearchTime = GetAdjustedTime(); // startup timestamp CKey key; CTransaction txCoinStake; - int64_t nSearchTime = txCoinStake.nTime; // search to current time + uint32_t nSearchTime = txCoinStake.nTime; // search to current time if (nSearchTime > nLastCoinStakeSearchTime) { diff --git a/src/main.h b/src/main.h index b75a679..692c6b0 100644 --- a/src/main.h +++ b/src/main.h @@ -81,7 +81,7 @@ extern CBlockIndex* pindexBest; extern unsigned int nTransactionsUpdated; extern uint64_t nLastBlockTx; extern uint64_t nLastBlockSize; -extern int64_t nLastCoinStakeSearchInterval; +extern uint32_t nLastCoinStakeSearchInterval; extern const std::string strMessageMagic; extern int64_t nTimeBestReceived; extern CCriticalSection cs_setpwalletRegistered; diff --git a/src/miner.cpp b/src/miner.cpp index 8430f54..0f04521 100644 --- a/src/miner.cpp +++ b/src/miner.cpp @@ -80,7 +80,7 @@ public: uint64_t nLastBlockTx = 0; uint64_t nLastBlockSize = 0; -int64_t nLastCoinStakeSearchInterval = 0; +uint32_t nLastCoinStakeSearchInterval = 0; // We want to sort transactions by priority and fee, so: typedef boost::tuple TxPriority; diff --git a/src/wallet.cpp b/src/wallet.cpp index 3906f27..f62bb18 100644 --- a/src/wallet.cpp +++ b/src/wallet.cpp @@ -1794,7 +1794,7 @@ bool CWallet::MergeCoins(const int64_t& nAmount, const int64_t& nMinValue, const } -bool CWallet::CreateCoinStake(const CKeyStore& keystore, unsigned int nBits, int64_t nSearchInterval, CTransaction& txNew, CKey& key) +bool CWallet::CreateCoinStake(const CKeyStore& keystore, unsigned int nBits, uint32_t nSearchInterval, CTransaction& txNew, CKey& key) { // The following combine threshold is important to security // Should not be adjusted if you don't understand the consequences @@ -1876,18 +1876,11 @@ bool CWallet::CreateCoinStake(const CKeyStore& keystore, unsigned int nBits, int int64_t nCredit = 0; CScript scriptPubKeyKernel; - KernelSearchSettings settings; - settings.nBits = nBits; - settings.nTime = txNew.nTime; - settings.nOffset = 0; - settings.nLimit = mapMeta.size(); - settings.nSearchInterval = nSearchInterval; - unsigned int nTimeTx, nBlockTime; COutPoint prevoutStake; CoinsSet::value_type kernelcoin; - if (ScanForStakeKernelHash(mapMeta, settings, kernelcoin, nTimeTx, nBlockTime, nKernelsTried, nCoinDaysTried)) + if (ScanForStakeKernelHash(mapMeta, nBits, txNew.nTime, nSearchInterval, kernelcoin, nTimeTx, nBlockTime, nKernelsTried, nCoinDaysTried)) { // Found a kernel if (fDebug && GetBoolArg("-printcoinstake")) diff --git a/src/wallet.h b/src/wallet.h index ce5f205..c1a987e 100644 --- a/src/wallet.h +++ b/src/wallet.h @@ -27,6 +27,14 @@ class CReserveKey; class COutput; class CCoinControl; +// Set of selected transactions +typedef std::set > CoinsSet; + +// Preloaded coins metadata +// (txid, vout.n) => ((txindex, (tx, vout.n)), (block, modifier)) +typedef std::map, std::pair >, std::pair > > MetaMap; + + /** (client) version numbers for particular wallet features */ enum WalletFeature { @@ -214,7 +222,7 @@ public: void GetStakeStats(float &nKernelsRate, float &nCoinDaysRate); void GetStakeWeightFromValue(const int64_t& nTime, const int64_t& nValue, uint64_t& nWeight); - bool CreateCoinStake(const CKeyStore& keystore, unsigned int nBits, int64_t nSearchInterval, CTransaction& txNew, CKey& key); + bool CreateCoinStake(const CKeyStore& keystore, unsigned int nBits, uint32_t nSearchInterval, CTransaction& txNew, CKey& key); bool MergeCoins(const int64_t& nAmount, const int64_t& nMinValue, const int64_t& nMaxValue, std::list& listMerged); std::string SendMoney(CScript scriptPubKey, int64_t nValue, CWalletTx& wtxNew, bool fAskFee=false); -- 1.7.1