PPCoin: Fix bug in target adjustment since 32e6e50a
[novacoin.git] / src / main.cpp
index e229063..9fd19e3 100644 (file)
@@ -756,7 +756,7 @@ unsigned int static GetNextTargetRequired(const CBlockIndex* pindexLast, bool fP
     // ppcoin: target change every block
     // ppcoin: retarget with exponential moving toward target spacing
     CBigNum bnNew;
-    bnNew.SetCompact(pindexLast->nBits);
+    bnNew.SetCompact(pindexPrev->nBits);
     bnNew *= ((nInterval - 1) * nTargetSpacing + nActualSpacing + nActualSpacing);
     bnNew /= ((nInterval + 1) * nTargetSpacing);
 
@@ -1284,17 +1284,23 @@ bool CBlock::SetBestChain(CTxDB& txdb, CBlockIndex* pindexNew)
 
 // ppcoin: coinstake must meet hash target according to the protocol:
 // at least one input must meet the formula
-//     hash(nBits + txPrev.block.nTime + txPrev.nTime + nTime) < bnTarget * nCoinDay
+//     hash(nBits + txPrev.block.nTime + txPrev.offset + txPrev.nTime + txPrev.vout.n + nTime) < bnTarget * nCoinDay
 // this ensures that the chance of getting a coinstake is proportional to the
 // amount of coin age one owns.
 // The reason this hash is chosen is the following:
-//   nBits: encodes all past block timestamps, prevents computing hash in advance
-//   txPrev.block.nTime: prevent nodes from guessing a good timestamp to generate
-//       transaction for future advantage
-//   txPrev.nTime: prevent nodes from meeting target simultaneously
-//   block/tx hash should not be used here as they can be generated in vast quatities
-//       so as to generate blocks faster, degrading the system back into a proof-of-work
-//       situation.
+//   nBits: encodes all past block timestamps, making computing hash in advance
+//          more difficult
+//   txPrev.block.nTime: prevent nodes from guessing a good timestamp to
+//                       generate transaction for future advantage
+//   txPrev.offset: offset of txPrev inside block, to reduce the chance of 
+//                  nodes generating coinstake at the same time
+//   txPrev.nTime: reduce the chance of nodes generating coinstake at the same
+//                 time
+//   txPrev.vout.n: output number of txPrev, to reduce the chance of nodes
+//                  generating coinstake at the same time
+//   block/tx hash should not be used here as they can be generated in vast
+//   quantities so as to generate blocks faster, degrading the system back into
+//   a proof-of-work situation.
 //
 bool CTransaction::CheckProofOfStake(CTxDB& txdb, unsigned int nBits) const
 {
@@ -1325,7 +1331,7 @@ bool CTransaction::CheckProofOfStake(CTxDB& txdb, unsigned int nBits) const
         CBigNum bnCoinDay = CBigNum(nValueIn) * (nTime-txPrev.nTime) / COIN / (24 * 60 * 60);
         // Calculate hash
         CDataStream ss(SER_GETHASH, VERSION);
-        ss << nBits << block.nTime << txPrev.nTime << nTime;
+        ss << nBits << block.nTime << (txindex.pos.nTxPos - txindex.pos.nBlockPos) << txPrev.nTime << txin.prevout.n << nTime;
         if (CBigNum(Hash(ss.begin(), ss.end())) <= bnCoinDay * bnTargetPerCoinDay)
             return true;
     }
@@ -1860,11 +1866,12 @@ void PrintBlockTree()
         // print item
         CBlock block;
         block.ReadFromDisk(pindex);
-        printf("%d (%u,%u) %s  %s  tx %d",
+        printf("%d (%u,%u) %s  %08lx  %s  tx %d",
             pindex->nHeight,
             pindex->nFile,
             pindex->nBlockPos,
             block.GetHash().ToString().substr(0,20).c_str(),
+            block.nBits,
             DateTimeStrFormat("%x %H:%M:%S", block.GetBlockTime()).c_str(),
             block.vtx.size());
 
@@ -2973,7 +2980,6 @@ public:
 
 CBlock* CreateNewBlock(CWallet* pwallet)
 {
-    CBlockIndex* pindexPrev = pindexBest;
     CReserveKey reservekey(pwallet);
 
     // Create new block
@@ -2993,9 +2999,11 @@ CBlock* CreateNewBlock(CWallet* pwallet)
 
     // ppcoin: if coinstake available add coinstake tx
     static unsigned int nLastCoinStakeCheckTime = GetAdjustedTime() - nMaxClockDrift;  // only initialized at startup
-    pblock->nBits = GetNextTargetRequired(pindexPrev, true);
+    CBlockIndex* pindexPrev = pindexBest;
     while (nLastCoinStakeCheckTime < GetAdjustedTime())
     {
+        pindexPrev = pindexBest;  // get best block again to avoid getting stale
+        pblock->nBits = GetNextTargetRequired(pindexPrev, true);
         static CCriticalSection cs;
         CTransaction txCoinStake;
         CRITICAL_BLOCK(cs)
@@ -3009,8 +3017,8 @@ CBlock* CreateNewBlock(CWallet* pwallet)
             break;
         }
     }
-    if (pblock->IsProofOfWork())
-        pblock->nBits = GetNextTargetRequired(pindexPrev, false);
+
+    pblock->nBits = GetNextTargetRequired(pindexPrev, pblock->IsProofOfStake());
 
     // Collect memory pool transactions into the block
     int64 nFees = 0;
@@ -3279,6 +3287,8 @@ void static BitcoinMiner(CWallet *pwallet)
         if (!pblock.get())
             return;
 
+        IncrementExtraNonce(pblock.get(), pindexPrev, nExtraNonce);
+
         // ppcoin: if proof-of-stake block found then process block
         if (pblock->IsProofOfStake())
         {
@@ -3291,8 +3301,6 @@ void static BitcoinMiner(CWallet *pwallet)
             continue;
         }
 
-        IncrementExtraNonce(pblock.get(), pindexPrev, nExtraNonce);
-
         printf("Running BitcoinMiner with %d transactions in block\n", pblock->vtx.size());