]> Git Repo - VerusCoin.git/blobdiff - src/miner.cpp
test
[VerusCoin.git] / src / miner.cpp
index 6cfc35c55a53f517a3353fd37eb400013b7edd44..04c31633d8a9225435a2ff212012e8facb22968e 100644 (file)
@@ -99,37 +99,47 @@ void UpdateTime(CBlockHeader* pblock, const Consensus::Params& consensusParams,
 }
 
 #define ASSETCHAINS_MINHEIGHT 100
+#define ROUNDROBIN_DELAY 45
+extern int32_t ASSETCHAINS_SEED,IS_KOMODO_NOTARY,USE_EXTERNAL_PUBKEY,KOMODO_CHOSEN_ONE,ASSETCHAIN_INIT,KOMODO_INITDONE,KOMODO_ON_DEMAND,KOMODO_INITDONE;
+extern char ASSETCHAINS_SYMBOL[16];
+extern std::string NOTARY_PUBKEY;
+extern uint8_t NOTARY_PUBKEY33[33];
+uint32_t Mining_start,Mining_height;
+int32_t komodo_chosennotary(int32_t *notaryidp,int32_t height,uint8_t *pubkey33);
+int32_t komodo_is_special(int32_t height,uint8_t pubkey33[33]);
 int32_t komodo_pax_opreturn(uint8_t *opret,int32_t maxsize);
 uint64_t komodo_paxtotal();
 int32_t komodo_is_issuer();
-void komodo_gateway_deposits(CMutableTransaction *txNew,int32_t shortflag,char *symbol,int32_t tokomodo);
-extern int32_t KOMODO_INITDONE,ASSETCHAINS_SHORTFLAG,KOMODO_REALTIME;
-extern char ASSETCHAINS_SYMBOL[16];
+int32_t komodo_gateway_deposits(CMutableTransaction *txNew,int32_t shortflag,char *symbol,int32_t tokomodo);
+int32_t komodo_isrealtime(int32_t *kmdheightp,char *target);
 
 CBlockTemplate* CreateNewBlock(const CScript& scriptPubKeyIn)
 {
-    uint64_t deposits; const CChainParams& chainparams = Params();
+    uint64_t deposits; int32_t isrealtime,kmdheight; const CChainParams& chainparams = Params();
     // Create new block
     unique_ptr<CBlockTemplate> pblocktemplate(new CBlockTemplate());
     if(!pblocktemplate.get())
         return NULL;
     CBlock *pblock = &pblocktemplate->block; // pointer for convenience
-    if ( ASSETCHAINS_SYMBOL[0] != 0 )
-        fprintf(stderr,"start CreateNewBlock %s initdone.%d deposit %.8f mempool.%d  RT.%u\n",ASSETCHAINS_SYMBOL,KOMODO_INITDONE,(double)komodo_paxtotal()/COIN,(int32_t)mempool.GetTotalTxSize(),KOMODO_REALTIME);
-    while ( mempool.GetTotalTxSize() <= 0 )
+    if ( ASSETCHAINS_SYMBOL[0] != 0 && chainActive.Tip()->nHeight >= 100 )
     {
-        deposits = komodo_paxtotal();
-        if ( KOMODO_INITDONE == 0 || KOMODO_REALTIME == 0 )
+        isrealtime = komodo_isrealtime(&kmdheight,ASSETCHAINS_SYMBOL);
+        fprintf(stderr,"start CreateNewBlock %s initdone.%d deposit %.8f mempool.%d RT.%u KOMODO_ON_DEMAND.%d\n",ASSETCHAINS_SYMBOL,KOMODO_INITDONE,(double)komodo_paxtotal()/COIN,(int32_t)mempool.GetTotalTxSize(),isrealtime,KOMODO_ON_DEMAND);
+        while ( KOMODO_ON_DEMAND == 0 )
         {
-            fprintf(stderr,"INITDONE.%d RT.%d deposits %.8f\n",KOMODO_INITDONE,KOMODO_REALTIME,(double)deposits/COIN);
+            deposits = komodo_paxtotal();
+            if ( KOMODO_INITDONE == 0 || komodo_isrealtime(&kmdheight,ASSETCHAINS_SYMBOL) == 0 )
+            {
+                //fprintf(stderr,"INITDONE.%d RT.%d deposits %.8f\n",KOMODO_INITDONE,isrealtime,(double)deposits/COIN);
+            }
+            else if ( deposits != 0 )
+                break;
+            sleep(10);
         }
-        else if ( deposits != 0 )
-            break;
-        sleep(10);
+        KOMODO_ON_DEMAND = 0;
+        if ( 0 && deposits != 0 )
+            printf("miner KOMODO_DEPOSIT %llu pblock->nHeight %d mempool.GetTotalTxSize(%d)\n",(long long)komodo_paxtotal(),(int32_t)chainActive.Tip()->nHeight,(int32_t)mempool.GetTotalTxSize());
     }
-    if ( 0 && deposits != 0 )
-        printf("miner KOMODO_DEPOSIT %llu pblock->nHeight %d mempool.GetTotalTxSize(%d)\n",(long long)komodo_paxtotal(),(int32_t)chainActive.Tip()->nHeight,(int32_t)mempool.GetTotalTxSize());
-
     // -regtest only: allow overriding block.nVersion with
     // -blockversion=N to test forking scenarios
     if (Params().MineBlocksOnDemand())
@@ -366,17 +376,19 @@ CBlockTemplate* CreateNewBlock(const CScript& scriptPubKeyIn)
         if ( ASSETCHAINS_SYMBOL[0] == 0 )
         {
             int32_t i,opretlen; uint8_t opret[256],*ptr;
-            if ( (opretlen= komodo_pax_opreturn(opret,sizeof(opret))) > 0 )
+            if ( komodo_gateway_deposits(&txNew,0,(char *)"EUR",1) == 0 )
             {
-                txNew.vout.resize(2);
-                txNew.vout[1].scriptPubKey.resize(opretlen);
-                ptr = (uint8_t *)txNew.vout[1].scriptPubKey.data();
-                for (i=0; i<opretlen; i++)
-                    ptr[i] = opret[i];
-                txNew.vout[1].nValue = 0;
-                //fprintf(stderr,"opretlen.%d\n",opretlen);
+                if ( (opretlen= komodo_pax_opreturn(opret,sizeof(opret))) > 0 )
+                {
+                    txNew.vout.resize(2);
+                    txNew.vout[1].scriptPubKey.resize(opretlen);
+                    ptr = (uint8_t *)txNew.vout[1].scriptPubKey.data();
+                    for (i=0; i<opretlen; i++)
+                        ptr[i] = opret[i];
+                    txNew.vout[1].nValue = 0;
+                    //fprintf(stderr,"opretlen.%d\n",opretlen);
+                }
             }
-            komodo_gateway_deposits(&txNew,0,(char *)"EUR",1);
         }
         else if ( komodo_is_issuer() != 0 )
         {
@@ -435,12 +447,6 @@ void IncrementExtraNonce(CBlock* pblock, CBlockIndex* pindexPrev, unsigned int&
 //
 // Internal miner
 //
-#define ROUNDROBIN_DELAY 10
-extern int32_t IS_KOMODO_NOTARY,USE_EXTERNAL_PUBKEY,KOMODO_CHOSEN_ONE;
-extern std::string NOTARY_PUBKEY;
-extern uint8_t NOTARY_PUBKEY33[33];
-uint32_t Mining_start,Mining_height;
-int32_t komodo_chosennotary(int32_t *notaryidp,int32_t height,uint8_t *pubkey33);
 
 CBlockTemplate* CreateNewBlockWithKey(CReserveKey& reservekey)
 {
@@ -475,7 +481,7 @@ static bool ProcessBlockFound(CBlock* pblock, CWallet& wallet, CReserveKey& rese
     {
         LOCK(cs_main);
         if (pblock->hashPrevBlock != chainActive.Tip()->GetBlockHash())
-            return error("ZcashMiner: generated block is stale");
+            return error("KomodoMiner: generated block is stale");
     }
 
     // Remove key from key pool
@@ -491,18 +497,20 @@ static bool ProcessBlockFound(CBlock* pblock, CWallet& wallet, CReserveKey& rese
     // Process this block the same as if we had received it from another node
     CValidationState state;
     if (!ProcessNewBlock(chainActive.Tip()->nHeight+1,state, NULL, pblock, true, NULL))
-        return error("ZcashMiner: ProcessNewBlock, block not accepted");
+        return error("KomodoMiner: ProcessNewBlock, block not accepted");
 
     minedBlocks.increment();
 
     return true;
 }
 
+int32_t komodo_baseid(char *origbase);
+
 void static BitcoinMiner(CWallet *pwallet)
 {
-    LogPrintf("ZcashMiner started\n");
+    LogPrintf("KomodoMiner started\n");
     SetThreadPriority(THREAD_PRIORITY_LOWEST);
-    RenameThread("zcash-miner");
+    RenameThread("komodo-miner");
     const CChainParams& chainparams = Params();
 
     // Each thread has its own key and counter
@@ -511,12 +519,12 @@ void static BitcoinMiner(CWallet *pwallet)
 
     unsigned int n = chainparams.EquihashN();
     unsigned int k = chainparams.EquihashK();
-    extern int32_t ASSETCHAIN_INIT,KOMODO_INITDONE; extern uint8_t NOTARY_PUBKEY33[33];
-    int32_t komodo_chosennotary(int32_t *notaryidp,int32_t height,uint8_t *pubkey33);
     int32_t notaryid = -1;
     while ( ASSETCHAIN_INIT == 0 || KOMODO_INITDONE == 0 )
     {
         sleep(1);
+        if ( komodo_baseid(ASSETCHAINS_SYMBOL) < 0 )
+            break;
     }
     komodo_chosennotary(&notaryid,chainActive.Tip()->nHeight,NOTARY_PUBKEY33);
 
@@ -542,6 +550,8 @@ void static BitcoinMiner(CWallet *pwallet)
         {
             if (chainparams.MiningRequiresPeers())
             {
+                if ( ASSETCHAINS_SEED != 0 && chainActive.Tip()->nHeight < 100 )
+                    break;
                 // Busy-wait for the network to come online so we don't waste time mining
                 // on an obsolete chain. In regtest mode we expect to fly solo.
                 //fprintf(stderr,"Wait for peers...\n");
@@ -562,33 +572,35 @@ void static BitcoinMiner(CWallet *pwallet)
             //
             // Create new block
             //
-            Mining_start = (uint32_t)time(NULL);
             unsigned int nTransactionsUpdatedLast = mempool.GetTransactionsUpdated();
             CBlockIndex* pindexPrev = chainActive.Tip();
-            Mining_height = pindexPrev->nHeight+1;
-            //if ( ASSETCHAINS_SYMBOL[0] != 0 )
-            //fprintf(stderr,"%s create new block ht.%d\n",ASSETCHAINS_SYMBOL,Mining_height);
+            if ( Mining_height != pindexPrev->nHeight+1 )
+            {
+                Mining_height = pindexPrev->nHeight+1;
+                Mining_start = (uint32_t)time(NULL);
+            }
+            if ( ASSETCHAINS_SYMBOL[0] != 0 )
+                fprintf(stderr,"%s create new block ht.%d\n",ASSETCHAINS_SYMBOL,Mining_height);
 
             unique_ptr<CBlockTemplate> pblocktemplate(CreateNewBlockWithKey(reservekey));
             if (!pblocktemplate.get())
             {
-                LogPrintf("Error in ZcashMiner: Keypool ran out, please call keypoolrefill before restarting the mining thread\n");
+                LogPrintf("Error in KomodoMiner: Keypool ran out, please call keypoolrefill before restarting the mining thread\n");
                 return;
             }
             CBlock *pblock = &pblocktemplate->block;
             IncrementExtraNonce(pblock, pindexPrev, nExtraNonce);
-            LogPrintf("Running ZcashMiner.%s with %u transactions in block (%u bytes)\n",solver.c_str(),pblock->vtx.size(),::GetSerializeSize(*pblock,SER_NETWORK,PROTOCOL_VERSION));
+            LogPrintf("Running KomodoMiner.%s with %u transactions in block (%u bytes)\n",solver.c_str(),pblock->vtx.size(),::GetSerializeSize(*pblock,SER_NETWORK,PROTOCOL_VERSION));
             //
             // Search
             //
-            int32_t notaryid; uint32_t savebits; int64_t nStart = GetTime();
+            uint32_t savebits; int64_t nStart = GetTime();
             savebits = pblock->nBits;
             arith_uint256 hashTarget = arith_uint256().SetCompact(pblock->nBits);
-            if ( ASSETCHAINS_SYMBOL[0] == 0 && komodo_chosennotary(&notaryid,pindexPrev->nHeight+1,NOTARY_PUBKEY33) > 0 )
+            if ( ASSETCHAINS_SYMBOL[0] == 0 && komodo_is_special(pindexPrev->nHeight+1,NOTARY_PUBKEY33) > 0 )
             {
                 hashTarget = arith_uint256().SetCompact(KOMODO_MINDIFF_NBITS);
-                Mining_start = (uint32_t)time(NULL);
-                fprintf(stderr,"I am the chosen one for %s ht.%d\n",ASSETCHAINS_SYMBOL,pindexPrev->nHeight+1);
+                //fprintf(stderr,"I am the chosen one for %s ht.%d\n",ASSETCHAINS_SYMBOL,pindexPrev->nHeight+1);
             } else Mining_start = 0;
             while (true)
             {
@@ -631,7 +643,7 @@ void static BitcoinMiner(CWallet *pwallet)
                     }
                     // Found a solution
                     SetThreadPriority(THREAD_PRIORITY_NORMAL);
-                    LogPrintf("ZcashMiner:\n");
+                    LogPrintf("KomodoMiner:\n");
                     LogPrintf("proof-of-work found  \n  hash: %s  \ntarget: %s\n", pblock->GetHash().GetHex(), hashTarget.GetHex());
                     if (ProcessBlockFound(pblock, *pwallet, reservekey)) {
                         // Ignore chain updates caused by us
@@ -710,9 +722,8 @@ void static BitcoinMiner(CWallet *pwallet)
                 // Regtest mode doesn't require peers
                 if (vNodes.empty() && chainparams.MiningRequiresPeers())
                 {
-                    if ( ASSETCHAINS_SYMBOL[0] != 0 )
-                        printf("no nodes, break\n");
-                    break;
+                    if ( ASSETCHAINS_SYMBOL[0] == 0 || Mining_height >= 100 )
+                        break;
                 }
                 if ((UintToArith256(pblock->nNonce) & 0xffff) == 0xffff)
                 {
@@ -722,14 +733,14 @@ void static BitcoinMiner(CWallet *pwallet)
                 }
                 if (mempool.GetTransactionsUpdated() != nTransactionsUpdatedLast && GetTime() - nStart > 60)
                 {
-                    if ( ASSETCHAINS_SYMBOL[0] != 0 )
-                        printf("timeout, break\n");
+                    //if ( ASSETCHAINS_SYMBOL[0] != 0 )
+                    //    printf("timeout, break\n");
                     break;
                 }
                 if ( pindexPrev != chainActive.Tip() )
                 {
-                    if ( ASSETCHAINS_SYMBOL[0] != 0 )
-                        printf("Tip advanced, break\n");
+                    //if ( ASSETCHAINS_SYMBOL[0] != 0 )
+                    //    printf("Tip advanced, break\n");
                     break;
                 }
                 // Update nNonce and nTime
@@ -746,12 +757,12 @@ void static BitcoinMiner(CWallet *pwallet)
     }
     catch (const boost::thread_interrupted&)
     {
-        LogPrintf("ZcashMiner terminated\n");
+        LogPrintf("KomodoMiner terminated\n");
         throw;
     }
     catch (const std::runtime_error &e)
     {
-        LogPrintf("ZcashMiner runtime error: %s\n", e.what());
+        LogPrintf("KomodoMiner runtime error: %s\n", e.what());
         return;
     }
     c.disconnect();
This page took 0.033279 seconds and 4 git commands to generate.