Define nOneDay and nOneHour constants.
authorCryptoManiac <balthazar@yandex.ru>
Sat, 3 Oct 2015 19:31:25 +0000 (12:31 -0700)
committerCryptoManiac <balthazar@yandex.ru>
Sat, 3 Oct 2015 19:31:25 +0000 (12:31 -0700)
src/addrman.cpp
src/init.cpp
src/kernel.cpp
src/main.cpp
src/main.h
src/util.h
src/wallet.cpp

index ba62a00..6676f62 100644 (file)
@@ -322,8 +322,8 @@ bool CAddrMan::Add_(const CAddress &addr, const CNetAddr& source, int64_t nTimeP
     if (pinfo)
     {
         // periodically update nTime
-        bool fCurrentlyOnline = (GetAdjustedTime() - addr.nTime < 24 * 60 * 60);
-        int64_t nUpdateInterval = (fCurrentlyOnline ? 60 * 60 : 24 * 60 * 60);
+        bool fCurrentlyOnline = (GetAdjustedTime() - addr.nTime < nOneDay);
+        int64_t nUpdateInterval = (fCurrentlyOnline ? nOneHour : nOneDay);
         if (addr.nTime && (!pinfo->nTime || pinfo->nTime < addr.nTime - nUpdateInterval - nTimePenalty))
             pinfo->nTime = max((int64_t)0, addr.nTime - nTimePenalty);
 
@@ -512,7 +512,7 @@ void CAddrMan::GetOnlineAddr_(std::vector<CAddrInfo> &vAddr)
     for (std::map<int, CAddrInfo>::const_iterator it = mapInfo.begin(); it != mapInfo.end(); it++)
     {
         CAddrInfo addr = it->second;
-        bool fCurrentlyOnline = (GetAdjustedTime() - addr.nTime < 24 * 60 * 60);
+        bool fCurrentlyOnline = (GetAdjustedTime() - addr.nTime < nOneDay);
         if (fCurrentlyOnline)
             vAddr.push_back(addr);
     }
index 8ba1c61..4b71dec 100644 (file)
@@ -380,8 +380,7 @@ bool AppInit2()
 
     // Ping and address broadcast intervals
     nPingInterval = max<int64_t>(10 * 60, GetArg("-keepalive", 30 * 60));
-
-    nBroadcastInterval = max<int64_t>(6 * 60 * 60, GetArg("-addrsetlifetime", 24 * 60 * 60));
+    nBroadcastInterval = max<int64_t>(6 * 60 * 60, GetArg("-addrsetlifetime", nOneDay));
 
     CheckpointsMode = Checkpoints::STRICT;
     std::string strCpMode = GetArg("-cppolicy", "strict");
index 9891de5..57f2e05 100644 (file)
@@ -377,7 +377,7 @@ bool CheckStakeKernelHash(uint32_t nBits, const CBlock& blockFrom, uint32_t nTxP
 
     uint256 hashBlockFrom = blockFrom.GetHash();
 
-    CBigNum bnCoinDayWeight = CBigNum(nValueIn) * GetWeight((int64_t)txPrev.nTime, (int64_t)nTimeTx) / COIN / (24 * 60 * 60);
+    CBigNum bnCoinDayWeight = CBigNum(nValueIn) * GetWeight((int64_t)txPrev.nTime, (int64_t)nTimeTx) / COIN / nOneDay;
     targetProofOfStake = (bnCoinDayWeight * bnTargetPerCoinDay).getuint256();
 
     // Calculate hash
@@ -429,31 +429,33 @@ public:
     ScanMidstateWorker()
     { }
     ScanMidstateWorker(unsigned char *kernel, uint32_t nBits, uint32_t nInputTxTime, int64_t nValueIn, uint32_t nIntervalBegin, uint32_t nIntervalEnd) 
-        : nBits(nBits), nInputTxTime(nInputTxTime), nValueIn(nValueIn), nIntervalBegin(nIntervalBegin), nIntervalEnd(nIntervalEnd)
+        : nBits(nBits), nInputTxTime(nInputTxTime), bnValueIn(nValueIn), nIntervalBegin(nIntervalBegin), nIntervalEnd(nIntervalEnd)
     {
         // Init new sha256 context and update it
         //   with first 24 bytes of kernel
         SHA256_Init(&workerCtx);
         SHA256_Update(&workerCtx, kernel, 8 + 16);
-        workerSolutions = vector<std::pair<uint256,uint32_t> >();
+        solutions = vector<std::pair<uint256,uint32_t> >();
     }
 
     void Do()
     {
         SetThreadPriority(THREAD_PRIORITY_LOWEST);
+        SHA256_CTX ctx = workerCtx;
+
+        // Sha256 result buffer
+        uint32_t hashProofOfStake[8];
 
+        // Compute maximum possible target to filter out majority of obviously insufficient hashes
         CBigNum bnTargetPerCoinDay;
         bnTargetPerCoinDay.SetCompact(nBits);
 
-        // Get maximum possible target to filter out the majority of obviously insufficient hashes
-        CBigNum bnMaxTargetPerCoinDay = bnTargetPerCoinDay * CBigNum(nValueIn) * nStakeMaxAge / COIN / (24 * 60 * 60);
-        uint256 maxTarget = bnMaxTargetPerCoinDay.getuint256();
-
-        SHA256_CTX ctx = workerCtx;
+        uint256 nMaxTarget = (bnTargetPerCoinDay * bnValueIn * nStakeMaxAge / COIN / nOneDay).getuint256(),
+            *pnHashProofOfStake = (uint256 *)&hashProofOfStake;
 
         // Search forward in time from the given timestamp
         // Stopping search in case of shutting down
-        for (uint32_t nTimeTx=nIntervalBegin; nTimeTx<nIntervalEnd && !fShutdown; nTimeTx++)
+        for (uint32_t nTimeTx=nIntervalBegin, nMaxTarget32 = nMaxTarget.Get32(7); nTimeTx<nIntervalEnd && !fShutdown; nTimeTx++)
         {
             // Complete first hashing iteration
             uint256 hash1;
@@ -464,36 +466,32 @@ public:
             ctx = workerCtx;
 
             // Finally, calculate kernel hash
-            uint256 hashProofOfStake;
             SHA256((unsigned char*)&hash1, sizeof(hashProofOfStake), (unsigned char*)&hashProofOfStake);
 
-
             // Skip if hash doesn't satisfy the maximum target
-            if (hashProofOfStake > maxTarget)
+            if (hashProofOfStake[7] > nMaxTarget32)
                 continue;
 
-            CBigNum bnCoinDayWeight = CBigNum(nValueIn) * GetWeight((int64_t)nInputTxTime, (int64_t)nTimeTx) / COIN / (24 * 60 * 60);
+            CBigNum bnCoinDayWeight = bnValueIn * GetWeight((int64_t)nInputTxTime, (int64_t)nTimeTx) / COIN / nOneDay;
             CBigNum bnTargetProofOfStake = bnCoinDayWeight * bnTargetPerCoinDay;
 
-            if (bnTargetProofOfStake >= CBigNum(hashProofOfStake))
-            {
-                workerSolutions.push_back(std::pair<uint256,uint32_t>(hashProofOfStake, nTimeTx));
-            }
+            if (bnTargetProofOfStake >= CBigNum(*pnHashProofOfStake))
+                solutions.push_back(std::pair<uint256,uint32_t>(*pnHashProofOfStake, nTimeTx));
         }
     }
 
     vector<std::pair<uint256,uint32_t> >& GetSolutions()
     {
-        return workerSolutions;
+        return solutions;
     }
 
 private:
     SHA256_CTX workerCtx;
-    std::vector<std::pair<uint256,uint32_t> > workerSolutions;
+    std::vector<std::pair<uint256,uint32_t> > solutions;
 
     uint32_t nBits;
     uint32_t nInputTxTime;
-    int64_t nValueIn;
+    CBigNum  bnValueIn;
     uint32_t nIntervalBegin;
     uint32_t nIntervalEnd;
 };
@@ -546,7 +544,7 @@ bool ScanContextBackward(SHA256_CTX &ctx, uint32_t nBits, uint32_t nInputTxTime,
     bnTargetPerCoinDay.SetCompact(nBits);
 
     // Get maximum possible target to filter out the majority of obviously insufficient hashes
-    CBigNum bnMaxTargetPerCoinDay = bnTargetPerCoinDay * CBigNum(nValueIn) * nStakeMaxAge / COIN / (24 * 60 * 60);
+    CBigNum bnMaxTargetPerCoinDay = bnTargetPerCoinDay * CBigNum(nValueIn) * nStakeMaxAge / COIN / nOneDay;
     uint256 maxTarget = bnMaxTargetPerCoinDay.getuint256();
 
     SHA256_CTX ctxCopy = ctx;
@@ -571,7 +569,7 @@ bool ScanContextBackward(SHA256_CTX &ctx, uint32_t nBits, uint32_t nInputTxTime,
         if (hashProofOfStake > maxTarget)
             continue;
 
-        CBigNum bnCoinDayWeight = CBigNum(nValueIn) * GetWeight((int64_t)nInputTxTime, (int64_t)nTimeTx) / COIN / (24 * 60 * 60);
+        CBigNum bnCoinDayWeight = CBigNum(nValueIn) * GetWeight((int64_t)nInputTxTime, (int64_t)nTimeTx) / COIN / nOneDay;
         CBigNum bnTargetProofOfStake = bnCoinDayWeight * bnTargetPerCoinDay;
 
         if (bnTargetProofOfStake >= CBigNum(hashProofOfStake))
index aaa8c52..d96af0a 100644 (file)
@@ -81,7 +81,7 @@ int64_t nMinimumInputValue = MIN_TXOUT_AMOUNT;
 
 // Ping and address broadcast intervals
 int64_t nPingInterval = 30 * 60;
-int64_t nBroadcastInterval = 24 * 60 * 60;
+int64_t nBroadcastInterval = nOneDay;
 
 extern enum Checkpoints::CPMode CheckpointsMode;
 
@@ -1124,7 +1124,7 @@ int64_t GetProofOfStakeReward(int64_t nCoinAge, unsigned int nBits, int64_t nTim
     return nSubsidy;
 }
 
-static const int64_t nTargetTimespan = 7 * 24 * 60 * 60;  // one week
+static const int64_t nTargetTimespan = 7 * nOneDay;  // one week
 
 // get proof of work blocks max spacing according to hard-coded conditions
 int64_t inline GetTargetSpacingWorkMax(int nHeight, unsigned int nTime)
@@ -1150,7 +1150,7 @@ unsigned int ComputeMaxBits(CBigNum bnTargetLimit, unsigned int nBase, int64_t n
     {
         // Maximum 200% adjustment per day...
         bnResult *= 2;
-        nTime -= 24 * 60 * 60;
+        nTime -= nOneDay;
     }
     if (bnResult > bnTargetLimit)
         bnResult = bnTargetLimit;
@@ -1250,7 +1250,7 @@ bool IsInitialBlockDownload()
         nLastUpdate = nCurrentTime;
     }
     return (nCurrentTime - nLastUpdate < 10 &&
-            pindexBest->GetBlockTime() < nCurrentTime - 24 * 60 * 60);
+            pindexBest->GetBlockTime() < nCurrentTime - nOneDay);
 }
 
 void static InvalidChainFound(CBlockIndex* pindexNew)
@@ -2097,7 +2097,7 @@ bool CTransaction::GetCoinAge(CTxDB& txdb, uint64_t& nCoinAge) const
             printf("coin age nValueIn=%" PRId64 " nTimeDiff=%d bnCentSecond=%s\n", nValueIn, nTime - txPrev.nTime, bnCentSecond.ToString().c_str());
     }
 
-    CBigNum bnCoinDay = bnCentSecond * CENT / COIN / (24 * 60 * 60);
+    CBigNum bnCoinDay = bnCentSecond * CENT / COIN / nOneDay;
     if (fDebug && GetBoolArg("-printcoinage"))
         printf("coin age bnCoinDay=%s\n", bnCoinDay.ToString().c_str());
     nCoinAge = bnCoinDay.getuint64();
@@ -3281,7 +3281,7 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv)
             if (fShutdown)
                 return true;
             if (addr.nTime <= 100000000 || addr.nTime > nNow + 10 * 60)
-                addr.nTime = nNow - 5 * 24 * 60 * 60;
+                addr.nTime = nNow - 5 * nOneDay;
             pfrom->AddAddressKnown(addr);
             bool fReachable = IsReachable(addr);
             if (addr.nTime > nSince && !pfrom->fGetAddr && vAddr.size() <= 10 && addr.IsRoutable())
@@ -3622,7 +3622,7 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv)
     else if (strCommand == "getaddr")
     {
         // Don't return addresses older than nCutOff timestamp
-        int64_t nCutOff = GetTime() - (nNodeLifespan * 24 * 60 * 60);
+        int64_t nCutOff = GetTime() - (nNodeLifespan * nOneDay);
         pfrom->vAddrToSend.clear();
         vector<CAddress> vAddr = addrman.GetAddr();
         BOOST_FOREACH(const CAddress &addr, vAddr)
index c659275..abcc1aa 100644 (file)
@@ -47,6 +47,7 @@ static const int64_t MAX_MINT_PROOF_OF_WORK = 100 * COIN;
 static const int64_t MAX_MINT_PROOF_OF_STAKE = 1 * COIN;
 static const int64_t MIN_TXOUT_AMOUNT = CENT/100;
 
+
 inline bool MoneyRange(int64_t nValue) { return (nValue >= 0 && nValue <= MAX_MONEY); }
 // Threshold for nLockTime: below this value it is interpreted as block number, otherwise as UNIX timestamp.
 static const unsigned int LOCKTIME_THRESHOLD = 500000000; // Tue Nov  5 00:53:20 1985 UTC
@@ -62,8 +63,8 @@ static const int fHaveUPnP = false;
 static const uint256 hashGenesisBlock("0x00000a060336cbb72fe969666d337b87198b1add2abaa59cca226820b32933a4");
 static const uint256 hashGenesisBlockTestNet("0x000c763e402f2436da9ed36c7286f62c3f6e5dbafce9ff289bd43d7459327eb");
 
-inline int64_t PastDrift(int64_t nTime)   { return nTime - 2 * 60 * 60; } // up to 2 hours from the past
-inline int64_t FutureDrift(int64_t nTime) { return nTime + 2 * 60 * 60; } // up to 2 hours from the future
+inline int64_t PastDrift(int64_t nTime)   { return nTime - 2 * nOneHour; } // up to 2 hours from the past
+inline int64_t FutureDrift(int64_t nTime) { return nTime + 2 * nOneHour; } // up to 2 hours from the future
 
 extern CScript COINBASE_FLAGS;
 extern CCriticalSection cs_main;
index 1b119d6..e7f5977 100644 (file)
@@ -35,6 +35,8 @@
 
 #include "netbase.h" // for AddTimeData
 
+static const int32_t nOneHour = 60 * 60;
+static const int32_t nOneDay = 24 * 60 * 60;
 
 static const int64_t COIN = 1000000;
 static const int64_t CENT = 10000;
index b1cfaa5..e02f30b 100644 (file)
@@ -1697,7 +1697,7 @@ void CWallet::GetStakeWeightFromValue(const int64_t& nTime, const int64_t& nValu
         return;
     }
 
-    CBigNum bnCoinDayWeight = CBigNum(nValue) * nTimeWeight / COIN / (24 * 60 * 60);
+    CBigNum bnCoinDayWeight = CBigNum(nValue) * nTimeWeight / COIN / nOneDay;
     nWeight = bnCoinDayWeight.getuint64();
 }