PPCoin: Separate thread for minting proof-of-stake blocks
authorSunny King <sunnyking9999@gmail.com>
Wed, 1 Aug 2012 19:00:24 +0000 (20:00 +0100)
committerSunny King <sunnyking9999@gmail.com>
Wed, 1 Aug 2012 19:00:24 +0000 (20:00 +0100)
src/bitcoinrpc.cpp
src/main.cpp
src/main.h
src/net.cpp
src/net.h

index b2ff091..17d1b31 100644 (file)
@@ -1826,7 +1826,7 @@ Value getwork(const Array& params, bool fHelp)
             nStart = GetTime();
 
             // Create new block
-            pblock = CreateNewBlock(pwalletMain, true);
+            pblock = CreateNewBlock(pwalletMain);
             if (!pblock)
                 throw JSONRPCError(-7, "Out of memory");
             vNewBlock.push_back(pblock);
index b84bcfb..c90565d 100644 (file)
@@ -3553,7 +3553,7 @@ public:
 uint64 nLastBlockTx = 0;
 uint64 nLastBlockSize = 0;
 
-CBlock* CreateNewBlock(CWallet* pwallet, bool fProofOfWorkOnly)
+CBlock* CreateNewBlock(CWallet* pwallet, bool fProofOfStake)
 {
     CReserveKey reservekey(pwallet);
 
@@ -3573,10 +3573,10 @@ CBlock* CreateNewBlock(CWallet* pwallet, bool fProofOfWorkOnly)
     pblock->vtx.push_back(txNew);
 
     // ppcoin: if coinstake available add coinstake tx
-    static unsigned int nLastCoinStakeCheckTime = GetAdjustedTime() - nMaxClockDrift + 60;  // only initialized at startup
+    static unsigned int nLastCoinStakeCheckTime = GetAdjustedTime() - nMaxClockDrift / 2;  // only initialized at startup
     CBlockIndex* pindexPrev = pindexBest;
 
-    if (!fProofOfWorkOnly)
+    if (fProofOfStake)
     {
         while (nLastCoinStakeCheckTime < GetAdjustedTime())
         {
@@ -3589,7 +3589,7 @@ CBlock* CreateNewBlock(CWallet* pwallet, bool fProofOfWorkOnly)
                 // mining may have been suspended for a while so 
                 // need to take max to satisfy the timestamp protocol
                 nLastCoinStakeCheckTime++;
-                nLastCoinStakeCheckTime = max(nLastCoinStakeCheckTime, (unsigned int) (GetAdjustedTime() - nMaxClockDrift + 60));
+                nLastCoinStakeCheckTime = max(nLastCoinStakeCheckTime, (unsigned int) (GetAdjustedTime() - nMaxClockDrift / 2));
                 txCoinStake.nTime = nLastCoinStakeCheckTime;
             }
             if (pwallet->CreateCoinStake(pblock->nBits, txCoinStake))
@@ -3867,9 +3867,9 @@ static bool fGenerateBitcoins = false;
 static bool fLimitProcessors = false;
 static int nLimitProcessors = -1;
 
-void static BitcoinMiner(CWallet *pwallet)
+void BitcoinMiner(CWallet *pwallet, bool fProofOfStake)
 {
-    printf("BitcoinMiner started\n");
+    printf("CPUMiner started for proof-of-%s\n", fProofOfStake? "stake" : "work");
     SetThreadPriority(THREAD_PRIORITY_LOWEST);
 
     // Each thread has its own key and counter
@@ -3896,7 +3896,7 @@ void static BitcoinMiner(CWallet *pwallet)
         unsigned int nTransactionsUpdatedLast = nTransactionsUpdated;
         CBlockIndex* pindexPrev = pindexBest;
 
-        auto_ptr<CBlock> pblock(CreateNewBlock(pwallet));
+        auto_ptr<CBlock> pblock(CreateNewBlock(pwallet, fProofOfStake));
         if (!pblock.get())
             return;
 
@@ -3907,13 +3907,14 @@ void static BitcoinMiner(CWallet *pwallet)
         {
             if (!pblock->SignBlock(*pwalletMain))
             {
-                error("BitcoinMiner: Unable to sign new proof-of-stake block");
+                error("CPUMiner: Unable to sign new proof-of-stake block");
                 return;
             }
-            printf("BitcoinMiner : proof-of-stake block found %s\n", pblock->GetHash().ToString().c_str()); 
+            printf("CPUMiner : proof-of-stake block found %s\n", pblock->GetHash().ToString().c_str()); 
             SetThreadPriority(THREAD_PRIORITY_NORMAL);
             CheckWork(pblock.get(), *pwalletMain, reservekey);
             SetThreadPriority(THREAD_PRIORITY_LOWEST);
+            Sleep(500);
             continue;
         }
 
@@ -4037,7 +4038,7 @@ void static ThreadBitcoinMiner(void* parg)
     try
     {
         vnThreadsRunning[THREAD_MINER]++;
-        BitcoinMiner(pwallet);
+        BitcoinMiner(pwallet, false);
         vnThreadsRunning[THREAD_MINER]--;
     }
     catch (std::exception& e) {
index 5f60a8a..9a893b4 100644 (file)
@@ -99,7 +99,7 @@ void PrintBlockTree();
 bool ProcessMessages(CNode* pfrom);
 bool SendMessages(CNode* pto, bool fSendTrickle);
 void GenerateBitcoins(bool fGenerate, CWallet* pwallet);
-CBlock* CreateNewBlock(CWallet* pwallet, bool fProofOfWorkOnly=false);
+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);
@@ -112,6 +112,7 @@ std::string GetWarnings(std::string strFor);
 bool Reorganize(CTxDB& txdb, CBlockIndex* pindexNew);
 uint256 WantedByOrphan(const CBlock* pblockOrphan);
 const CBlockIndex* GetLastBlockIndex(const CBlockIndex* pindex, bool fProofOfStake);
+void BitcoinMiner(CWallet *pwallet, bool fProofOfStake);
 
 
 
index a650979..bb54b2e 100644 (file)
@@ -1412,6 +1412,27 @@ void ThreadMessageHandler2(void* parg)
     }
 }
 
+// ppcoin: stake minter thread
+void static ThreadStakeMinter(void* parg)
+{
+    printf("ThreadStakeMinter started\n");
+    CWallet* pwallet = (CWallet*)parg;
+    try
+    {
+        vnThreadsRunning[THREAD_MINTER]++;
+        BitcoinMiner(pwallet, true);
+        vnThreadsRunning[THREAD_MINTER]--;
+    }
+    catch (std::exception& e) {
+        vnThreadsRunning[THREAD_MINTER]--;
+        PrintException(&e, "ThreadStakeMinter()");
+    } catch (...) {
+        vnThreadsRunning[THREAD_MINTER]--;
+        PrintException(NULL, "ThreadStakeMinter()");
+    }
+    printf("ThreadStakeMinter exiting, %d threads remaining\n", vnThreadsRunning[THREAD_MINTER]);
+}
+
 
 
 
@@ -1625,6 +1646,10 @@ void StartNode(void* parg)
 
     // Generate coins in the background
     GenerateBitcoins(GetBoolArg("-gen", false), pwalletMain);
+
+    // ppcoin: mint proof-of-stake blocks in the background
+    if (!CreateThread(ThreadStakeMinter, pwalletMain))
+        printf("Error: CreateThread(ThreadStakeMinter) failed\n");
 }
 
 bool StopNode()
@@ -1656,6 +1681,7 @@ bool StopNode()
     if (vnThreadsRunning[THREAD_DNSSEED] > 0) printf("ThreadDNSAddressSeed still running\n");
     if (vnThreadsRunning[THREAD_ADDEDCONNECTIONS] > 0) printf("ThreadOpenAddedConnections still running\n");
     if (vnThreadsRunning[THREAD_DUMPADDRESS] > 0) printf("ThreadDumpAddresses still running\n");
+    if (vnThreadsRunning[THREAD_MINTER] > 0) printf("ThreadStakeMinter still running\n");
     while (vnThreadsRunning[THREAD_MESSAGEHANDLER] > 0 || vnThreadsRunning[THREAD_RPCSERVER] > 0)
         Sleep(20);
     Sleep(50);
index 4178675..661cc33 100644 (file)
--- a/src/net.h
+++ b/src/net.h
@@ -79,6 +79,7 @@ enum threadId
     THREAD_DNSSEED,
     THREAD_ADDEDCONNECTIONS,
     THREAD_DUMPADDRESS,
+    THREAD_MINTER,
 
     THREAD_MAX
 };