]>
Commit | Line | Data |
---|---|---|
d247a5d1 | 1 | // Copyright (c) 2009-2010 Satoshi Nakamoto |
f914f1a7 | 2 | // Copyright (c) 2009-2014 The Bitcoin Core developers |
78253fcb | 3 | // Distributed under the MIT software license, see the accompanying |
d247a5d1 JG |
4 | // file COPYING or http://www.opensource.org/licenses/mit-license.php. |
5 | ||
d247a5d1 | 6 | #include "miner.h" |
8e8b6d70 | 7 | #ifdef ENABLE_MINING |
c7aaab7a | 8 | #include "pow/tromp/equi_miner.h" |
2cc0a252 | 9 | #endif |
51ed9ec9 | 10 | |
eda37330 | 11 | #include "amount.h" |
8e8b6d70 | 12 | #include "base58.h" |
bebe7282 | 13 | #include "chainparams.h" |
691161d4 | 14 | #include "consensus/consensus.h" |
be126699 | 15 | #include "consensus/upgrades.h" |
da29ecbc | 16 | #include "consensus/validation.h" |
8e8b6d70 JG |
17 | #ifdef ENABLE_MINING |
18 | #include "crypto/equihash.h" | |
42181656 | 19 | #include "crypto/verus_hash.h" |
8e8b6d70 | 20 | #endif |
85aab2a0 | 21 | #include "hash.h" |
42181656 | 22 | |
d247a5d1 | 23 | #include "main.h" |
a6df7ab5 | 24 | #include "metrics.h" |
51ed9ec9 | 25 | #include "net.h" |
df852d2b | 26 | #include "pow.h" |
bebe7282 | 27 | #include "primitives/transaction.h" |
8e165d57 | 28 | #include "random.h" |
22c4272b | 29 | #include "timedata.h" |
8e8b6d70 | 30 | #include "ui_interface.h" |
ad49c256 WL |
31 | #include "util.h" |
32 | #include "utilmoneystr.h" | |
df840de5 | 33 | #ifdef ENABLE_WALLET |
50c72f23 | 34 | #include "wallet/wallet.h" |
df840de5 | 35 | #endif |
09eb201b | 36 | |
fdda3c50 JG |
37 | #include "sodium.h" |
38 | ||
ad49c256 | 39 | #include <boost/thread.hpp> |
a3c26c2e | 40 | #include <boost/tuple/tuple.hpp> |
8e8b6d70 JG |
41 | #ifdef ENABLE_MINING |
42 | #include <functional> | |
43 | #endif | |
5a360a5c | 44 | #include <mutex> |
ad49c256 | 45 | |
09eb201b | 46 | using namespace std; |
7b4737c8 | 47 | |
d247a5d1 JG |
48 | ////////////////////////////////////////////////////////////////////////////// |
49 | // | |
50 | // BitcoinMiner | |
51 | // | |
52 | ||
c6cb21d1 GA |
53 | // |
54 | // Unconfirmed transactions in the memory pool often depend on other | |
55 | // transactions in the memory pool. When we select transactions from the | |
56 | // pool, we select by highest priority or fee rate, so we might consider | |
57 | // transactions that depend on transactions that aren't yet in the block. | |
58 | // The COrphan class keeps track of these 'temporary orphans' while | |
59 | // CreateBlock is figuring out which transactions to include. | |
60 | // | |
d247a5d1 JG |
61 | class COrphan |
62 | { | |
63 | public: | |
4d707d51 | 64 | const CTransaction* ptx; |
d247a5d1 | 65 | set<uint256> setDependsOn; |
c6cb21d1 | 66 | CFeeRate feeRate; |
02bec4b2 | 67 | double dPriority; |
e9e70b95 | 68 | |
c6cb21d1 | 69 | COrphan(const CTransaction* ptxIn) : ptx(ptxIn), feeRate(0), dPriority(0) |
d247a5d1 | 70 | { |
d247a5d1 | 71 | } |
d247a5d1 JG |
72 | }; |
73 | ||
51ed9ec9 BD |
74 | uint64_t nLastBlockTx = 0; |
75 | uint64_t nLastBlockSize = 0; | |
d247a5d1 | 76 | |
c6cb21d1 GA |
77 | // We want to sort transactions by priority and fee rate, so: |
78 | typedef boost::tuple<double, CFeeRate, const CTransaction*> TxPriority; | |
d247a5d1 JG |
79 | class TxPriorityCompare |
80 | { | |
81 | bool byFee; | |
e9e70b95 | 82 | |
d247a5d1 JG |
83 | public: |
84 | TxPriorityCompare(bool _byFee) : byFee(_byFee) { } | |
e9e70b95 | 85 | |
d247a5d1 JG |
86 | bool operator()(const TxPriority& a, const TxPriority& b) |
87 | { | |
88 | if (byFee) | |
89 | { | |
90 | if (a.get<1>() == b.get<1>()) | |
91 | return a.get<0>() < b.get<0>(); | |
92 | return a.get<1>() < b.get<1>(); | |
93 | } | |
94 | else | |
95 | { | |
96 | if (a.get<0>() == b.get<0>()) | |
97 | return a.get<1>() < b.get<1>(); | |
98 | return a.get<0>() < b.get<0>(); | |
99 | } | |
100 | } | |
101 | }; | |
102 | ||
bebe7282 | 103 | void UpdateTime(CBlockHeader* pblock, const Consensus::Params& consensusParams, const CBlockIndex* pindexPrev) |
22c4272b | 104 | { |
4972b74b | 105 | pblock->nTime = 1 + std::max(pindexPrev->GetMedianTimePast()+1, GetAdjustedTime()); |
22c4272b | 106 | } |
107 | ||
5416af1d | 108 | #include "komodo_defs.h" |
109 | ||
16c7bf6b | 110 | extern int32_t ASSETCHAINS_SEED,IS_KOMODO_NOTARY,USE_EXTERNAL_PUBKEY,KOMODO_CHOSEN_ONE,ASSETCHAIN_INIT,KOMODO_INITDONE,KOMODO_ON_DEMAND,KOMODO_INITDONE,KOMODO_PASSPORT_INITDONE; |
48d800c2 | 111 | extern uint64_t ASSETCHAINS_COMMISSION, ASSETCHAINS_STAKED; |
42181656 | 112 | extern uint64_t ASSETCHAINS_REWARD[ASSETCHAINS_MAX_ERAS], ASSETCHAINS_TIMELOCKGTE, ASSETCHAINS_NONCEMASK[]; |
113 | extern const char *ASSETCHAINS_ALGORITHMS[]; | |
1fae37f6 | 114 | extern int32_t VERUS_MIN_STAKEAGE, ASSETCHAINS_ALGO, ASSETCHAINS_EQUIHASH, ASSETCHAINS_LASTERA, ASSETCHAINS_LWMAPOS, ASSETCHAINS_NONCESHIFT[], ASSETCHAINS_HASHESPERROUND[]; |
7c130297 | 115 | extern char ASSETCHAINS_SYMBOL[KOMODO_ASSETCHAIN_MAXLEN]; |
f24b36ca | 116 | extern std::string NOTARY_PUBKEY; |
94a465a6 | 117 | extern uint8_t NOTARY_PUBKEY33[33],ASSETCHAINS_OVERRIDE_PUBKEY33[33]; |
f24b36ca | 118 | uint32_t Mining_start,Mining_height; |
8683bd8d | 119 | int32_t komodo_chosennotary(int32_t *notaryidp,int32_t height,uint8_t *pubkey33,uint32_t timestamp); |
b4810651 | 120 | int32_t komodo_pax_opreturn(int32_t height,uint8_t *opret,int32_t maxsize); |
265660f7 | 121 | //uint64_t komodo_paxtotal(); |
d63fdb34 | 122 | int32_t komodo_baseid(char *origbase); |
001bc04a | 123 | //int32_t komodo_is_issuer(); |
124 | //int32_t komodo_gateway_deposits(CMutableTransaction *txNew,char *symbol,int32_t tokomodo); | |
265660f7 | 125 | //int32_t komodo_isrealtime(int32_t *kmdheightp); |
3bc88f14 | 126 | int32_t komodo_validate_interest(const CTransaction &tx,int32_t txheight,uint32_t nTime,int32_t dispflag); |
29bd53a1 | 127 | int64_t komodo_block_unlocktime(uint32_t nHeight); |
18443f69 | 128 | uint64_t komodo_commission(const CBlock *block); |
135fa24e | 129 | int32_t komodo_staked(CPubKey &pubkey, CMutableTransaction &txNew,uint32_t nBits,uint32_t *blocktimep,uint32_t *txtimep,uint256 *utxotxidp,int32_t *utxovoutp,uint64_t *utxovaluep,uint8_t *utxosig); |
1f722359 | 130 | int32_t verus_staked(CPubKey &pubkey, CMutableTransaction &txNew, uint32_t &nBits, arith_uint256 &hashResult, uint8_t *utxosig); |
496f1fd2 | 131 | int32_t komodo_notaryvin(CMutableTransaction &txNew,uint8_t *notarypub33); |
7652ed92 | 132 | |
135fa24e | 133 | CBlockTemplate* CreateNewBlock(const CScript& scriptPubKeyIn, bool isStake) |
d247a5d1 | 134 | { |
9339a0cb | 135 | uint64_t deposits; int32_t isrealtime,kmdheight; uint32_t blocktime; const CChainParams& chainparams = Params(); |
d247a5d1 | 136 | // Create new block |
08c58194 | 137 | std::unique_ptr<CBlockTemplate> pblocktemplate(new CBlockTemplate()); |
d247a5d1 | 138 | if(!pblocktemplate.get()) |
1b5b89ba | 139 | { |
140 | fprintf(stderr,"pblocktemplate.get() failure\n"); | |
d247a5d1 | 141 | return NULL; |
1b5b89ba | 142 | } |
d247a5d1 | 143 | CBlock *pblock = &pblocktemplate->block; // pointer for convenience |
265660f7 | 144 | /*if ( ASSETCHAINS_SYMBOL[0] != 0 && komodo_baseid(ASSETCHAINS_SYMBOL) >= 0 && chainActive.Tip()->nHeight >= ASSETCHAINS_MINHEIGHT ) |
a5e36fdd | 145 | { |
48a3cd18 | 146 | isrealtime = komodo_isrealtime(&kmdheight); |
3da07d2e | 147 | deposits = komodo_paxtotal(); |
5ba0ce9d | 148 | while ( KOMODO_ON_DEMAND == 0 && deposits == 0 && (int32_t)mempool.GetTotalTxSize() == 0 ) |
ec7ad5f6 | 149 | { |
79562bf3 | 150 | deposits = komodo_paxtotal(); |
16c7bf6b | 151 | if ( KOMODO_PASSPORT_INITDONE == 0 || KOMODO_INITDONE == 0 || (komodo_baseid(ASSETCHAINS_SYMBOL) >= 0 && (isrealtime= komodo_isrealtime(&kmdheight)) == 0) ) |
79562bf3 | 152 | { |
bbaa688c | 153 | //fprintf(stderr,"INITDONE.%d RT.%d deposits %.8f ht.%d\n",KOMODO_INITDONE,isrealtime,(double)deposits/COIN,kmdheight); |
79562bf3 | 154 | } |
2bec17cb | 155 | else if ( komodo_isrealtime(&kmdheight) != 0 && (deposits != 0 || (int32_t)mempool.GetTotalTxSize() > 0) ) |
4a21ccd2 | 156 | { |
eb928486 | 157 | 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); |
79562bf3 | 158 | break; |
4a21ccd2 | 159 | } |
6fd0ae11 | 160 | sleep(10); |
ec7ad5f6 | 161 | } |
f24b36ca | 162 | KOMODO_ON_DEMAND = 0; |
16cd9f2d | 163 | if ( 0 && deposits != 0 ) |
79562bf3 | 164 | 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()); |
265660f7 | 165 | }*/ |
dbca89b7 GA |
166 | // -regtest only: allow overriding block.nVersion with |
167 | // -blockversion=N to test forking scenarios | |
168 | if (Params().MineBlocksOnDemand()) | |
169 | pblock->nVersion = GetArg("-blockversion", pblock->nVersion); | |
e9e70b95 | 170 | |
4949004d PW |
171 | // Add dummy coinbase tx as first transaction |
172 | pblock->vtx.push_back(CTransaction()); | |
d247a5d1 JG |
173 | pblocktemplate->vTxFees.push_back(-1); // updated at end |
174 | pblocktemplate->vTxSigOps.push_back(-1); // updated at end | |
e9e70b95 | 175 | |
d247a5d1 | 176 | // Largest block you're willing to create: |
ad898b40 | 177 | unsigned int nBlockMaxSize = GetArg("-blockmaxsize", DEFAULT_BLOCK_MAX_SIZE); |
d247a5d1 JG |
178 | // Limit to betweeen 1K and MAX_BLOCK_SIZE-1K for sanity: |
179 | nBlockMaxSize = std::max((unsigned int)1000, std::min((unsigned int)(MAX_BLOCK_SIZE-1000), nBlockMaxSize)); | |
e9e70b95 | 180 | |
d247a5d1 JG |
181 | // How much of the block should be dedicated to high-priority transactions, |
182 | // included regardless of the fees they pay | |
183 | unsigned int nBlockPrioritySize = GetArg("-blockprioritysize", DEFAULT_BLOCK_PRIORITY_SIZE); | |
184 | nBlockPrioritySize = std::min(nBlockMaxSize, nBlockPrioritySize); | |
e9e70b95 | 185 | |
d247a5d1 JG |
186 | // Minimum block size you want to create; block will be filled with free transactions |
187 | // until there are no more or the block reaches this size: | |
037b4f14 | 188 | unsigned int nBlockMinSize = GetArg("-blockminsize", DEFAULT_BLOCK_MIN_SIZE); |
d247a5d1 | 189 | nBlockMinSize = std::min(nBlockMaxSize, nBlockMinSize); |
e9e70b95 | 190 | |
d247a5d1 | 191 | // Collect memory pool transactions into the block |
a372168e | 192 | CAmount nFees = 0; |
e9e70b95 | 193 | |
d247a5d1 JG |
194 | { |
195 | LOCK2(cs_main, mempool.cs); | |
48265f3c | 196 | CBlockIndex* pindexPrev = chainActive.Tip(); |
b867e409 | 197 | const int nHeight = pindexPrev->nHeight + 1; |
be126699 | 198 | uint32_t consensusBranchId = CurrentEpochBranchId(nHeight, chainparams.GetConsensus()); |
a0dd01bc | 199 | |
a1d3c6fb | 200 | const int64_t nMedianTimePast = pindexPrev->GetMedianTimePast(); |
a0dd01bc | 201 | uint32_t proposedTime = GetAdjustedTime(); |
202 | if (proposedTime == nMedianTimePast) | |
203 | { | |
204 | // too fast or stuck, this addresses the too fast issue, while moving | |
205 | // forward as quickly as possible | |
206 | for (int i; i < 100; i++) | |
207 | { | |
208 | proposedTime = GetAdjustedTime(); | |
209 | if (proposedTime == nMedianTimePast) | |
210 | MilliSleep(10); | |
211 | } | |
212 | } | |
213 | pblock->nTime = GetAdjustedTime(); | |
214 | ||
7c70438d | 215 | CCoinsViewCache view(pcoinsTip); |
f9155fec | 216 | uint32_t expired; uint64_t commission; |
6ff77181 | 217 | |
d247a5d1 JG |
218 | // Priority order to process transactions |
219 | list<COrphan> vOrphan; // list memory doesn't move | |
220 | map<uint256, vector<COrphan*> > mapDependers; | |
221 | bool fPrintPriority = GetBoolArg("-printpriority", false); | |
e9e70b95 | 222 | |
d247a5d1 JG |
223 | // This vector will be sorted into a priority queue: |
224 | vector<TxPriority> vecPriority; | |
225 | vecPriority.reserve(mempool.mapTx.size()); | |
e328fa32 | 226 | for (CTxMemPool::indexed_transaction_set::iterator mi = mempool.mapTx.begin(); |
4d707d51 | 227 | mi != mempool.mapTx.end(); ++mi) |
d247a5d1 | 228 | { |
e328fa32 | 229 | const CTransaction& tx = mi->GetTx(); |
e9e70b95 | 230 | |
a1d3c6fb | 231 | int64_t nLockTimeCutoff = (STANDARD_LOCKTIME_VERIFY_FLAGS & LOCKTIME_MEDIAN_TIME_PAST) |
e9e70b95 | 232 | ? nMedianTimePast |
233 | : pblock->GetBlockTime(); | |
234 | ||
9bb37bf0 | 235 | if (tx.IsCoinBase() || !IsFinalTx(tx, nHeight, nLockTimeCutoff) || IsExpiredTx(tx, nHeight)) |
61f8caf2 | 236 | { |
51376f3c | 237 | //fprintf(stderr,"coinbase.%d finaltx.%d expired.%d\n",tx.IsCoinBase(),IsFinalTx(tx, nHeight, nLockTimeCutoff),IsExpiredTx(tx, nHeight)); |
14aa6cc0 | 238 | continue; |
61f8caf2 | 239 | } |
161f617d | 240 | if ( ASSETCHAINS_SYMBOL[0] == 0 && komodo_validate_interest(tx,nHeight,(uint32_t)pblock->nTime,0) < 0 ) |
6ff77181 | 241 | { |
64b45b71 | 242 | //fprintf(stderr,"CreateNewBlock: komodo_validate_interest failure nHeight.%d nTime.%u vs locktime.%u\n",nHeight,(uint32_t)pblock->nTime,(uint32_t)tx.nLockTime); |
d247a5d1 | 243 | continue; |
14aa6cc0 | 244 | } |
d247a5d1 JG |
245 | COrphan* porphan = NULL; |
246 | double dPriority = 0; | |
a372168e | 247 | CAmount nTotalIn = 0; |
d247a5d1 JG |
248 | bool fMissingInputs = false; |
249 | BOOST_FOREACH(const CTxIn& txin, tx.vin) | |
250 | { | |
251 | // Read prev transaction | |
252 | if (!view.HaveCoins(txin.prevout.hash)) | |
253 | { | |
254 | // This should never happen; all transactions in the memory | |
255 | // pool should connect to either transactions in the chain | |
256 | // or other transactions in the memory pool. | |
257 | if (!mempool.mapTx.count(txin.prevout.hash)) | |
258 | { | |
881a85a2 | 259 | LogPrintf("ERROR: mempool transaction missing input\n"); |
d247a5d1 JG |
260 | if (fDebug) assert("mempool transaction missing input" == 0); |
261 | fMissingInputs = true; | |
262 | if (porphan) | |
263 | vOrphan.pop_back(); | |
264 | break; | |
265 | } | |
e9e70b95 | 266 | |
d247a5d1 JG |
267 | // Has to wait for dependencies |
268 | if (!porphan) | |
269 | { | |
270 | // Use list for automatic deletion | |
271 | vOrphan.push_back(COrphan(&tx)); | |
272 | porphan = &vOrphan.back(); | |
273 | } | |
274 | mapDependers[txin.prevout.hash].push_back(porphan); | |
275 | porphan->setDependsOn.insert(txin.prevout.hash); | |
e328fa32 | 276 | nTotalIn += mempool.mapTx.find(txin.prevout.hash)->GetTx().vout[txin.prevout.n].nValue; |
d247a5d1 JG |
277 | continue; |
278 | } | |
629d75fa PW |
279 | const CCoins* coins = view.AccessCoins(txin.prevout.hash); |
280 | assert(coins); | |
e9e70b95 | 281 | |
a372168e | 282 | CAmount nValueIn = coins->vout[txin.prevout.n].nValue; |
d247a5d1 | 283 | nTotalIn += nValueIn; |
e9e70b95 | 284 | |
b867e409 | 285 | int nConf = nHeight - coins->nHeight; |
e9e70b95 | 286 | |
d247a5d1 JG |
287 | dPriority += (double)nValueIn * nConf; |
288 | } | |
2b2bc69e | 289 | nTotalIn += tx.GetJoinSplitValueIn(); |
e9e70b95 | 290 | |
d247a5d1 | 291 | if (fMissingInputs) continue; |
e9e70b95 | 292 | |
d6eb2599 | 293 | // Priority is sum(valuein * age) / modified_txsize |
d247a5d1 | 294 | unsigned int nTxSize = ::GetSerializeSize(tx, SER_NETWORK, PROTOCOL_VERSION); |
4d707d51 | 295 | dPriority = tx.ComputePriority(dPriority, nTxSize); |
e9e70b95 | 296 | |
805344dc | 297 | uint256 hash = tx.GetHash(); |
2a72d459 | 298 | mempool.ApplyDeltas(hash, dPriority, nTotalIn); |
e9e70b95 | 299 | |
c6cb21d1 | 300 | CFeeRate feeRate(nTotalIn-tx.GetValueOut(), nTxSize); |
e9e70b95 | 301 | |
d247a5d1 JG |
302 | if (porphan) |
303 | { | |
304 | porphan->dPriority = dPriority; | |
c6cb21d1 | 305 | porphan->feeRate = feeRate; |
d247a5d1 JG |
306 | } |
307 | else | |
e328fa32 | 308 | vecPriority.push_back(TxPriority(dPriority, feeRate, &(mi->GetTx()))); |
d247a5d1 | 309 | } |
e9e70b95 | 310 | |
d247a5d1 | 311 | // Collect transactions into block |
51ed9ec9 BD |
312 | uint64_t nBlockSize = 1000; |
313 | uint64_t nBlockTx = 0; | |
355ca565 | 314 | int64_t interest; |
d247a5d1 JG |
315 | int nBlockSigOps = 100; |
316 | bool fSortedByFee = (nBlockPrioritySize <= 0); | |
e9e70b95 | 317 | |
d247a5d1 JG |
318 | TxPriorityCompare comparer(fSortedByFee); |
319 | std::make_heap(vecPriority.begin(), vecPriority.end(), comparer); | |
e9e70b95 | 320 | |
d247a5d1 JG |
321 | while (!vecPriority.empty()) |
322 | { | |
323 | // Take highest priority transaction off the priority queue: | |
324 | double dPriority = vecPriority.front().get<0>(); | |
c6cb21d1 | 325 | CFeeRate feeRate = vecPriority.front().get<1>(); |
4d707d51 | 326 | const CTransaction& tx = *(vecPriority.front().get<2>()); |
e9e70b95 | 327 | |
d247a5d1 JG |
328 | std::pop_heap(vecPriority.begin(), vecPriority.end(), comparer); |
329 | vecPriority.pop_back(); | |
e9e70b95 | 330 | |
d247a5d1 JG |
331 | // Size limits |
332 | unsigned int nTxSize = ::GetSerializeSize(tx, SER_NETWORK, PROTOCOL_VERSION); | |
01fda4c1 | 333 | if (nBlockSize + nTxSize >= nBlockMaxSize-512) // room for extra autotx |
61f8caf2 | 334 | { |
51376f3c | 335 | //fprintf(stderr,"nBlockSize %d + %d nTxSize >= %d nBlockMaxSize\n",(int32_t)nBlockSize,(int32_t)nTxSize,(int32_t)nBlockMaxSize); |
d247a5d1 | 336 | continue; |
61f8caf2 | 337 | } |
e9e70b95 | 338 | |
d247a5d1 JG |
339 | // Legacy limits on sigOps: |
340 | unsigned int nTxSigOps = GetLegacySigOpCount(tx); | |
a4a40a38 | 341 | if (nBlockSigOps + nTxSigOps >= MAX_BLOCK_SIGOPS-1) |
61f8caf2 | 342 | { |
51376f3c | 343 | //fprintf(stderr,"A nBlockSigOps %d + %d nTxSigOps >= %d MAX_BLOCK_SIGOPS-1\n",(int32_t)nBlockSigOps,(int32_t)nTxSigOps,(int32_t)MAX_BLOCK_SIGOPS); |
d247a5d1 | 344 | continue; |
61f8caf2 | 345 | } |
d247a5d1 | 346 | // Skip free transactions if we're past the minimum block size: |
805344dc | 347 | const uint256& hash = tx.GetHash(); |
2a72d459 | 348 | double dPriorityDelta = 0; |
a372168e | 349 | CAmount nFeeDelta = 0; |
2a72d459 | 350 | mempool.ApplyDeltas(hash, dPriorityDelta, nFeeDelta); |
13fc83c7 | 351 | if (fSortedByFee && (dPriorityDelta <= 0) && (nFeeDelta <= 0) && (feeRate < ::minRelayTxFee) && (nBlockSize + nTxSize >= nBlockMinSize)) |
61f8caf2 | 352 | { |
51376f3c | 353 | //fprintf(stderr,"fee rate skip\n"); |
d247a5d1 | 354 | continue; |
61f8caf2 | 355 | } |
2a72d459 | 356 | // Prioritise by fee once past the priority size or we run out of high-priority |
d247a5d1 JG |
357 | // transactions: |
358 | if (!fSortedByFee && | |
359 | ((nBlockSize + nTxSize >= nBlockPrioritySize) || !AllowFree(dPriority))) | |
360 | { | |
361 | fSortedByFee = true; | |
362 | comparer = TxPriorityCompare(fSortedByFee); | |
363 | std::make_heap(vecPriority.begin(), vecPriority.end(), comparer); | |
364 | } | |
e9e70b95 | 365 | |
d247a5d1 | 366 | if (!view.HaveInputs(tx)) |
61f8caf2 | 367 | { |
51376f3c | 368 | //fprintf(stderr,"dont have inputs\n"); |
d247a5d1 | 369 | continue; |
61f8caf2 | 370 | } |
1141b678 | 371 | CAmount nTxFees = view.GetValueIn(chainActive.Tip()->nHeight,&interest,tx,chainActive.Tip()->nTime)-tx.GetValueOut(); |
e9e70b95 | 372 | |
d247a5d1 | 373 | nTxSigOps += GetP2SHSigOpCount(tx, view); |
a4a40a38 | 374 | if (nBlockSigOps + nTxSigOps >= MAX_BLOCK_SIGOPS-1) |
61f8caf2 | 375 | { |
51376f3c | 376 | //fprintf(stderr,"B nBlockSigOps %d + %d nTxSigOps >= %d MAX_BLOCK_SIGOPS-1\n",(int32_t)nBlockSigOps,(int32_t)nTxSigOps,(int32_t)MAX_BLOCK_SIGOPS); |
d247a5d1 | 377 | continue; |
61f8caf2 | 378 | } |
68f7d1d7 PT |
379 | // Note that flags: we don't want to set mempool/IsStandard() |
380 | // policy here, but we still have to ensure that the block we | |
381 | // create only contains transactions that are valid in new blocks. | |
d247a5d1 | 382 | CValidationState state; |
6514771a | 383 | PrecomputedTransactionData txdata(tx); |
be126699 | 384 | if (!ContextualCheckInputs(tx, state, view, true, MANDATORY_SCRIPT_VERIFY_FLAGS, true, txdata, Params().GetConsensus(), consensusBranchId)) |
61f8caf2 | 385 | { |
51376f3c | 386 | //fprintf(stderr,"context failure\n"); |
d247a5d1 | 387 | continue; |
61f8caf2 | 388 | } |
8cb98d91 | 389 | UpdateCoins(tx, view, nHeight); |
e9e70b95 | 390 | |
d247a5d1 JG |
391 | // Added |
392 | pblock->vtx.push_back(tx); | |
393 | pblocktemplate->vTxFees.push_back(nTxFees); | |
394 | pblocktemplate->vTxSigOps.push_back(nTxSigOps); | |
395 | nBlockSize += nTxSize; | |
396 | ++nBlockTx; | |
397 | nBlockSigOps += nTxSigOps; | |
398 | nFees += nTxFees; | |
e9e70b95 | 399 | |
d247a5d1 JG |
400 | if (fPrintPriority) |
401 | { | |
3f0813b3 | 402 | LogPrintf("priority %.1f fee %s txid %s\n",dPriority, feeRate.ToString(), tx.GetHash().ToString()); |
d247a5d1 | 403 | } |
e9e70b95 | 404 | |
d247a5d1 JG |
405 | // Add transactions that depend on this one to the priority queue |
406 | if (mapDependers.count(hash)) | |
407 | { | |
408 | BOOST_FOREACH(COrphan* porphan, mapDependers[hash]) | |
409 | { | |
410 | if (!porphan->setDependsOn.empty()) | |
411 | { | |
412 | porphan->setDependsOn.erase(hash); | |
413 | if (porphan->setDependsOn.empty()) | |
414 | { | |
c6cb21d1 | 415 | vecPriority.push_back(TxPriority(porphan->dPriority, porphan->feeRate, porphan->ptx)); |
d247a5d1 JG |
416 | std::push_heap(vecPriority.begin(), vecPriority.end(), comparer); |
417 | } | |
418 | } | |
419 | } | |
420 | } | |
421 | } | |
e9e70b95 | 422 | |
d247a5d1 JG |
423 | nLastBlockTx = nBlockTx; |
424 | nLastBlockSize = nBlockSize; | |
d07308d2 | 425 | blocktime = 1 + std::max(pindexPrev->GetMedianTimePast()+1, GetAdjustedTime()); |
9a0f2798 | 426 | //pblock->nTime = blocktime + 1; |
135fa24e | 427 | pblock->nBits = GetNextWorkRequired(pindexPrev, pblock, Params().GetConsensus()); |
428 | ||
7dbeae5d | 429 | //LogPrintf("CreateNewBlock(): total size %u blocktime.%u nBits.%08x\n", nBlockSize,blocktime,pblock->nBits); |
135fa24e | 430 | if ( ASSETCHAINS_SYMBOL[0] != 0 && isStake ) |
a4a40a38 | 431 | { |
135fa24e | 432 | CPubKey key; |
433 | ||
434 | if ( NOTARY_PUBKEY33[0] != 0 ) | |
435 | { | |
436 | key = CPubKey(ParseHex(NOTARY_PUBKEY)); | |
437 | } | |
438 | else | |
439 | { | |
440 | vector<vector<unsigned char>> vSolutions; | |
441 | txnouttype whichType; | |
442 | ||
443 | if (Solver(scriptPubKeyIn, whichType, vSolutions) && !vSolutions.empty()) | |
444 | { | |
445 | key = CPubKey(vSolutions[0]); | |
446 | } | |
447 | else | |
448 | { | |
449 | fprintf(stderr,"CreateNewBlock() invalid scriptPubKeyIn\n"); | |
450 | return(0); | |
451 | } | |
452 | } | |
453 | ||
1f722359 | 454 | uint64_t txfees,utxovalue; uint32_t txtime; uint256 utxotxid; int32_t i,siglen,numsigs,utxovout; uint8_t utxosig[128],*ptr; |
d231a6a7 | 455 | CMutableTransaction txStaked = CreateNewContextualCMutableTransaction(Params().GetConsensus(), chainActive.Height() + 1); |
1f722359 | 456 | |
cfb55edb | 457 | //if ( blocktime > pindexPrev->GetMedianTimePast()+60 ) |
458 | // blocktime = pindexPrev->GetMedianTimePast() + 60; | |
1f722359 MT |
459 | if (ASSETCHAINS_LWMAPOS != 0) |
460 | { | |
461 | uint32_t nBitsPOS; | |
462 | arith_uint256 posHash; | |
463 | siglen = verus_staked(key, txStaked, nBitsPOS, posHash, utxosig); | |
464 | blocktime = GetAdjustedTime(); | |
1fae37f6 | 465 | pblock->SetVerusPOSTarget(nBitsPOS); |
1f722359 MT |
466 | } |
467 | else | |
468 | { | |
469 | siglen = komodo_staked(key, txStaked, pblock->nBits, &blocktime, &txtime, &utxotxid, &utxovout, &utxovalue, utxosig); | |
470 | } | |
471 | ||
472 | if ( siglen > 0 ) | |
a4a40a38 | 473 | { |
66b4e563 | 474 | CAmount txfees = 0; |
de230f5e | 475 | if ( GetAdjustedTime() < blocktime-13 ) |
ba1587fd | 476 | return(0); |
a4a40a38 | 477 | pblock->vtx.push_back(txStaked); |
a4a40a38 | 478 | pblocktemplate->vTxFees.push_back(txfees); |
d231a6a7 | 479 | pblocktemplate->vTxSigOps.push_back(GetLegacySigOpCount(txStaked)); |
a4a40a38 | 480 | nFees += txfees; |
9339a0cb | 481 | pblock->nTime = blocktime; |
de230f5e | 482 | //printf("PoS ht.%d t%u\n",(int32_t)chainActive.Tip()->nHeight+1,blocktime); |
9464ac21 | 483 | } else return(0); //fprintf(stderr,"no utxos eligible for staking\n"); |
a4a40a38 | 484 | } |
e9e70b95 | 485 | |
f3ffa3d2 | 486 | // Create coinbase tx |
072099d7 | 487 | CMutableTransaction txNew = CreateNewContextualCMutableTransaction(chainparams.GetConsensus(), nHeight); |
f3ffa3d2 SB |
488 | txNew.vin.resize(1); |
489 | txNew.vin[0].prevout.SetNull(); | |
abb90a89 MT |
490 | txNew.vin[0].scriptSig = CScript() << nHeight << OP_0; |
491 | ||
d581f229 | 492 | txNew.vout.resize(1); |
f3ffa3d2 | 493 | txNew.vout[0].scriptPubKey = scriptPubKeyIn; |
abb90a89 | 494 | txNew.vout[0].nValue = GetBlockSubsidy(nHeight,chainparams.GetConsensus()) + nFees; |
9bb37bf0 | 495 | txNew.nExpiryHeight = 0; |
48d800c2 | 496 | txNew.nLockTime = std::max(pindexPrev->GetMedianTimePast()+1, GetAdjustedTime()); |
abb90a89 | 497 | |
29bd53a1 | 498 | // check if coinbase transactions must be time locked at current subsidy and prepend the time lock |
a0dd01bc | 499 | // to transaction if so, cast for GTE operator |
500 | if ((uint64_t)(txNew.vout[0].nValue) >= ASSETCHAINS_TIMELOCKGTE) | |
abb90a89 MT |
501 | { |
502 | int32_t opretlen, p2shlen, scriptlen; | |
29bd53a1 | 503 | CScriptExt opretScript = CScriptExt(); |
abb90a89 MT |
504 | |
505 | txNew.vout.resize(2); | |
506 | ||
29bd53a1 MT |
507 | // prepend time lock to original script unless original script is P2SH, in which case, we will leave the coins |
508 | // protected only by the time lock rather than 100% inaccessible | |
509 | opretScript.AddCheckLockTimeVerify(komodo_block_unlocktime(nHeight)); | |
510 | if (!scriptPubKeyIn.IsPayToScriptHash()) | |
511 | opretScript += scriptPubKeyIn; | |
abb90a89 | 512 | |
29bd53a1 MT |
513 | txNew.vout[0].scriptPubKey = CScriptExt().PayToScriptHash(CScriptID(opretScript)); |
514 | txNew.vout[1].scriptPubKey = CScriptExt().OpReturnScript(opretScript, OPRETTYPE_TIMELOCK); | |
abb90a89 | 515 | txNew.vout[1].nValue = 0; |
48d800c2 | 516 | } // timelocks and commissions are currently incompatible due to validation complexity of the combination |
517 | else if ( ASSETCHAINS_SYMBOL[0] != 0 && ASSETCHAINS_OVERRIDE_PUBKEY33[0] != 0 && ASSETCHAINS_COMMISSION != 0 && (commission= komodo_commission((CBlock*)&pblocktemplate->block)) != 0 ) | |
c9b1071d | 518 | { |
c000c9ca | 519 | int32_t i; uint8_t *ptr; |
9ed1be03 | 520 | txNew.vout.resize(2); |
521 | txNew.vout[1].nValue = commission; | |
522 | txNew.vout[1].scriptPubKey.resize(35); | |
523 | ptr = (uint8_t *)txNew.vout[1].scriptPubKey.data(); | |
c000c9ca | 524 | ptr[0] = 33; |
525 | for (i=0; i<33; i++) | |
526 | ptr[i+1] = ASSETCHAINS_OVERRIDE_PUBKEY33[i]; | |
527 | ptr[34] = OP_CHECKSIG; | |
146d2aa2 | 528 | //printf("autocreate commision vout\n"); |
c9b1071d | 529 | } |
48d800c2 | 530 | |
4949004d | 531 | pblock->vtx[0] = txNew; |
d247a5d1 | 532 | pblocktemplate->vTxFees[0] = -nFees; |
48d800c2 | 533 | |
1fae37f6 MT |
534 | // if not Verus stake, setup nonce, otherwise, leave it alone |
535 | if (!isStake || ASSETCHAINS_LWMAPOS == 0) | |
536 | { | |
537 | // Randomise nonce | |
538 | arith_uint256 nonce = UintToArith256(GetRandHash()); | |
48d800c2 | 539 | |
1fae37f6 MT |
540 | // Clear the top 16 and bottom 16 or 24 bits (for local use as thread flags and counters) |
541 | nonce <<= ASSETCHAINS_NONCESHIFT[ASSETCHAINS_ALGO]; | |
542 | nonce >>= 16; | |
543 | pblock->nNonce = ArithToUint256(nonce); | |
544 | } | |
e9e70b95 | 545 | |
d247a5d1 JG |
546 | // Fill in header |
547 | pblock->hashPrevBlock = pindexPrev->GetBlockHash(); | |
a8d384ae | 548 | pblock->hashReserved = uint256(); |
3da69a31 | 549 | |
9a0f2798 | 550 | if ( ASSETCHAINS_SYMBOL[0] == 0 || ASSETCHAINS_STAKED == 0 || NOTARY_PUBKEY33[0] == 0 ) |
551 | { | |
552 | UpdateTime(pblock, Params().GetConsensus(), pindexPrev); | |
1fae37f6 | 553 | pblock->nBits = GetNextWorkRequired(pindexPrev, pblock, Params().GetConsensus()); |
9a0f2798 | 554 | } |
fdda3c50 | 555 | pblock->nSolution.clear(); |
d247a5d1 | 556 | pblocktemplate->vTxSigOps[0] = GetLegacySigOpCount(pblock->vtx[0]); |
496f1fd2 | 557 | if ( ASSETCHAINS_SYMBOL[0] == 0 && NOTARY_PUBKEY33[0] != 0 ) |
af805d53 | 558 | { |
496f1fd2 | 559 | CMutableTransaction txNotary = CreateNewContextualCMutableTransaction(Params().GetConsensus(), chainActive.Height() + 1); |
f31815fc | 560 | if ( pblock->nTime < pindexPrev->nTime+65 ) |
c670f024 | 561 | pblock->nTime = pindexPrev->nTime + 65; |
a893e994 | 562 | if ( komodo_notaryvin(txNotary,NOTARY_PUBKEY33) > 0 ) |
496f1fd2 | 563 | { |
564 | CAmount txfees = 0; | |
565 | pblock->vtx.push_back(txNotary); | |
566 | pblocktemplate->vTxFees.push_back(txfees); | |
567 | pblocktemplate->vTxSigOps.push_back(GetLegacySigOpCount(txNotary)); | |
568 | nFees += txfees; | |
f31815fc | 569 | //fprintf(stderr,"added notaryvin\n"); |
0857c3d5 | 570 | } |
571 | else | |
572 | { | |
573 | fprintf(stderr,"error adding notaryvin, need to create 0.0001 utxos\n"); | |
574 | return(0); | |
575 | } | |
707b061c | 576 | } |
8fc79ac9 | 577 | else |
af805d53 | 578 | { |
8fc79ac9 | 579 | CValidationState state; |
580 | if ( !TestBlockValidity(state, *pblock, pindexPrev, false, false)) | |
581 | { | |
582 | //static uint32_t counter; | |
583 | //if ( counter++ < 100 && ASSETCHAINS_STAKED == 0 ) | |
584 | // fprintf(stderr,"warning: miner testblockvalidity failed\n"); | |
585 | return(0); | |
586 | } | |
af805d53 | 587 | } |
d247a5d1 | 588 | } |
d247a5d1 JG |
589 | return pblocktemplate.release(); |
590 | } | |
32b915c9 | 591 | |
1a31463b | 592 | /* |
e9e70b95 | 593 | #ifdef ENABLE_WALLET |
594 | boost::optional<CScript> GetMinerScriptPubKey(CReserveKey& reservekey) | |
595 | #else | |
596 | boost::optional<CScript> GetMinerScriptPubKey() | |
597 | #endif | |
598 | { | |
599 | CKeyID keyID; | |
600 | CBitcoinAddress addr; | |
601 | if (addr.SetString(GetArg("-mineraddress", ""))) { | |
602 | addr.GetKeyID(keyID); | |
603 | } else { | |
604 | #ifdef ENABLE_WALLET | |
605 | CPubKey pubkey; | |
606 | if (!reservekey.GetReservedKey(pubkey)) { | |
607 | return boost::optional<CScript>(); | |
608 | } | |
609 | keyID = pubkey.GetID(); | |
610 | #else | |
611 | return boost::optional<CScript>(); | |
612 | #endif | |
613 | } | |
614 | ||
615 | CScript scriptPubKey = CScript() << OP_DUP << OP_HASH160 << ToByteVector(keyID) << OP_EQUALVERIFY << OP_CHECKSIG; | |
616 | return scriptPubKey; | |
617 | } | |
618 | ||
619 | #ifdef ENABLE_WALLET | |
620 | CBlockTemplate* CreateNewBlockWithKey(CReserveKey& reservekey) | |
621 | { | |
622 | boost::optional<CScript> scriptPubKey = GetMinerScriptPubKey(reservekey); | |
623 | #else | |
624 | CBlockTemplate* CreateNewBlockWithKey() | |
625 | { | |
626 | boost::optional<CScript> scriptPubKey = GetMinerScriptPubKey(); | |
627 | #endif | |
628 | ||
629 | if (!scriptPubKey) { | |
630 | return NULL; | |
631 | } | |
632 | return CreateNewBlock(*scriptPubKey); | |
633 | }*/ | |
acfa0333 | 634 | |
c1de826f JG |
635 | ////////////////////////////////////////////////////////////////////////////// |
636 | // | |
637 | // Internal miner | |
638 | // | |
639 | ||
2cc0a252 | 640 | #ifdef ENABLE_MINING |
c1de826f | 641 | |
d247a5d1 JG |
642 | void IncrementExtraNonce(CBlock* pblock, CBlockIndex* pindexPrev, unsigned int& nExtraNonce) |
643 | { | |
644 | // Update nExtraNonce | |
645 | static uint256 hashPrevBlock; | |
646 | if (hashPrevBlock != pblock->hashPrevBlock) | |
647 | { | |
648 | nExtraNonce = 0; | |
649 | hashPrevBlock = pblock->hashPrevBlock; | |
650 | } | |
651 | ++nExtraNonce; | |
652 | unsigned int nHeight = pindexPrev->nHeight+1; // Height first in coinbase required for block.version=2 | |
4949004d PW |
653 | CMutableTransaction txCoinbase(pblock->vtx[0]); |
654 | txCoinbase.vin[0].scriptSig = (CScript() << nHeight << CScriptNum(nExtraNonce)) + COINBASE_FLAGS; | |
655 | assert(txCoinbase.vin[0].scriptSig.size() <= 100); | |
e9e70b95 | 656 | |
4949004d | 657 | pblock->vtx[0] = txCoinbase; |
d247a5d1 JG |
658 | pblock->hashMerkleRoot = pblock->BuildMerkleTree(); |
659 | } | |
660 | ||
4a85e067 | 661 | #ifdef ENABLE_WALLET |
acfa0333 WL |
662 | ////////////////////////////////////////////////////////////////////////////// |
663 | // | |
664 | // Internal miner | |
665 | // | |
acfa0333 | 666 | |
135fa24e | 667 | CBlockTemplate* CreateNewBlockWithKey(CReserveKey& reservekey, bool isStake) |
acfa0333 | 668 | { |
f6c647ed | 669 | CPubKey pubkey; CScript scriptPubKey; uint8_t *script,*ptr; int32_t i; |
7bfc207a | 670 | if ( USE_EXTERNAL_PUBKEY != 0 ) |
998397aa | 671 | { |
7bfc207a | 672 | //fprintf(stderr,"use notary pubkey\n"); |
c95fd5e0 | 673 | scriptPubKey = CScript() << ParseHex(NOTARY_PUBKEY) << OP_CHECKSIG; |
f6c647ed | 674 | } |
675 | else | |
676 | { | |
677 | if (!reservekey.GetReservedKey(pubkey)) | |
1b5b89ba | 678 | { |
f6c647ed | 679 | return NULL; |
1b5b89ba | 680 | } |
f6c647ed | 681 | scriptPubKey.resize(35); |
f66f65ec | 682 | ptr = (uint8_t *)pubkey.begin(); |
f6c647ed | 683 | script = (uint8_t *)scriptPubKey.data(); |
684 | script[0] = 33; | |
685 | for (i=0; i<33; i++) | |
686 | script[i+1] = ptr[i]; | |
687 | script[34] = OP_CHECKSIG; | |
688 | //scriptPubKey = CScript() << ToByteVector(pubkey) << OP_CHECKSIG; | |
689 | } | |
135fa24e | 690 | return CreateNewBlock(scriptPubKey, isStake); |
acfa0333 WL |
691 | } |
692 | ||
395f10cf | 693 | void komodo_broadcast(CBlock *pblock,int32_t limit) |
694 | { | |
695 | int32_t n = 1; | |
696 | //fprintf(stderr,"broadcast new block t.%u\n",(uint32_t)time(NULL)); | |
697 | { | |
698 | LOCK(cs_vNodes); | |
699 | BOOST_FOREACH(CNode* pnode, vNodes) | |
700 | { | |
701 | if ( pnode->hSocket == INVALID_SOCKET ) | |
702 | continue; | |
703 | if ( (rand() % n) == 0 ) | |
704 | { | |
705 | pnode->PushMessage("block", *pblock); | |
706 | if ( n++ > limit ) | |
707 | break; | |
708 | } | |
709 | } | |
710 | } | |
711 | //fprintf(stderr,"finished broadcast new block t.%u\n",(uint32_t)time(NULL)); | |
712 | } | |
945f015d | 713 | |
269d8ba0 | 714 | static bool ProcessBlockFound(CBlock* pblock, CWallet& wallet, CReserveKey& reservekey) |
8e8b6d70 JG |
715 | #else |
716 | static bool ProcessBlockFound(CBlock* pblock) | |
717 | #endif // ENABLE_WALLET | |
d247a5d1 | 718 | { |
81212588 | 719 | LogPrintf("%s\n", pblock->ToString()); |
35915149 | 720 | LogPrintf("generated %s height.%d\n", FormatMoney(pblock->vtx[0].vout[0].nValue),chainActive.Tip()->nHeight+1); |
e9e70b95 | 721 | |
d247a5d1 JG |
722 | // Found a solution |
723 | { | |
724 | LOCK(cs_main); | |
4c6d41b8 | 725 | if (pblock->hashPrevBlock != chainActive.Tip()->GetBlockHash()) |
ba8419c7 | 726 | { |
727 | uint256 hash; int32_t i; | |
728 | hash = pblock->hashPrevBlock; | |
92266e99 | 729 | for (i=31; i>=0; i--) |
ba8419c7 | 730 | fprintf(stderr,"%02x",((uint8_t *)&hash)[i]); |
c0dbb034 | 731 | fprintf(stderr," <- prev (stale)\n"); |
ba8419c7 | 732 | hash = chainActive.Tip()->GetBlockHash(); |
92266e99 | 733 | for (i=31; i>=0; i--) |
ba8419c7 | 734 | fprintf(stderr,"%02x",((uint8_t *)&hash)[i]); |
c0dbb034 | 735 | fprintf(stderr," <- chainTip (stale)\n"); |
e9e70b95 | 736 | |
2e500f50 | 737 | return error("KomodoMiner: generated block is stale"); |
ba8419c7 | 738 | } |
18e72167 | 739 | } |
e9e70b95 | 740 | |
8e8b6d70 | 741 | #ifdef ENABLE_WALLET |
18e72167 | 742 | // Remove key from key pool |
998397aa | 743 | if ( IS_KOMODO_NOTARY == 0 ) |
945f015d | 744 | { |
745 | if (GetArg("-mineraddress", "").empty()) { | |
746 | // Remove key from key pool | |
747 | reservekey.KeepKey(); | |
748 | } | |
8e8b6d70 | 749 | } |
18e72167 | 750 | // Track how many getdata requests this block gets |
438ba9c1 | 751 | //if ( 0 ) |
18e72167 PW |
752 | { |
753 | LOCK(wallet.cs_wallet); | |
754 | wallet.mapRequestCount[pblock->GetHash()] = 0; | |
d247a5d1 | 755 | } |
8e8b6d70 | 756 | #endif |
e9e70b95 | 757 | |
18e72167 PW |
758 | // Process this block the same as if we had received it from another node |
759 | CValidationState state; | |
7bb789bb | 760 | if (!ProcessNewBlock(1,chainActive.Tip()->nHeight+1,state, NULL, pblock, true, NULL)) |
2e500f50 | 761 | return error("KomodoMiner: ProcessNewBlock, block not accepted"); |
e9e70b95 | 762 | |
d793f94b | 763 | TrackMinedBlock(pblock->GetHash()); |
395f10cf | 764 | komodo_broadcast(pblock,16); |
d247a5d1 JG |
765 | return true; |
766 | } | |
767 | ||
078f6af1 | 768 | int32_t komodo_baseid(char *origbase); |
a30dd993 | 769 | int32_t komodo_eligiblenotary(uint8_t pubkeys[66][33],int32_t *mids,uint32_t *blocktimes,int32_t *nonzpkeysp,int32_t height); |
13691369 | 770 | arith_uint256 komodo_PoWtarget(int32_t *percPoSp,arith_uint256 target,int32_t height,int32_t goalperc); |
8ee93080 | 771 | int32_t FOUND_BLOCK,KOMODO_MAYBEMINED; |
9152feb5 | 772 | extern int32_t KOMODO_LASTMINED; |
8b51b9e4 | 773 | int32_t roundrobin_delay; |
18443f69 | 774 | arith_uint256 HASHTarget,HASHTarget_POW; |
078f6af1 | 775 | |
5642c96c | 776 | // wait for peers to connect |
b45d65b4 | 777 | int32_t waitForPeers(const CChainParams &chainparams) |
5642c96c | 778 | { |
779 | if (chainparams.MiningRequiresPeers()) | |
780 | { | |
3da69a31 MT |
781 | bool fvNodesEmpty; |
782 | { | |
783 | LOCK(cs_vNodes); | |
784 | fvNodesEmpty = vNodes.empty(); | |
785 | } | |
786 | if (fvNodesEmpty) | |
787 | { | |
788 | do { | |
789 | MilliSleep(5000 + rand() % 5000); | |
790 | { | |
791 | LOCK(cs_vNodes); | |
792 | fvNodesEmpty = vNodes.empty(); | |
793 | } | |
794 | } while (fvNodesEmpty); | |
795 | MilliSleep(5000 + rand() % 5000); | |
796 | } | |
5642c96c | 797 | } |
798 | } | |
799 | ||
42181656 | 800 | #ifdef ENABLE_WALLET |
135fa24e | 801 | /* |
802 | * A separate thread to stake, while the miner threads mine. | |
803 | */ | |
804 | void static VerusStaker(CWallet *pwallet) | |
805 | { | |
806 | LogPrintf("Verus staker thread started\n"); | |
807 | RenameThread("verus-staker"); | |
808 | ||
809 | const CChainParams& chainparams = Params(); | |
810 | ||
811 | // Each thread has its own key | |
812 | CReserveKey reservekey(pwallet); | |
813 | ||
814 | // Each thread has its own counter | |
815 | unsigned int nExtraNonce = 0; | |
816 | std::vector<unsigned char> solnPlaceholder = std::vector<unsigned char>(); | |
817 | solnPlaceholder.resize(Eh200_9.SolutionWidth); | |
818 | uint8_t *script; uint64_t total,checktoshis; int32_t i,j; | |
819 | ||
820 | while ( (ASSETCHAIN_INIT == 0 || KOMODO_INITDONE == 0) ) //chainActive.Tip()->nHeight != 235300 && | |
821 | { | |
822 | sleep(1); | |
823 | if ( komodo_baseid(ASSETCHAINS_SYMBOL) < 0 ) | |
824 | break; | |
825 | } | |
826 | ||
3da69a31 MT |
827 | SetThreadPriority(THREAD_PRIORITY_LOWEST); |
828 | ||
135fa24e | 829 | // try a nice clean peer connection to start |
830 | waitForPeers(chainparams); | |
3da69a31 MT |
831 | // try a nice clean peer connection to start |
832 | waitForPeers(chainparams); | |
833 | CBlockIndex* pindexPrev; | |
135fa24e | 834 | do { |
3da69a31 MT |
835 | pindexPrev = chainActive.Tip(); |
836 | MilliSleep(5000 + rand() % 5000); | |
837 | } while (pindexPrev != chainActive.Tip()); | |
135fa24e | 838 | |
839 | sleep(5); | |
1fae37f6 MT |
840 | printf("Staking height %d for %s\n", chainActive.Tip()->nHeight + 1, ASSETCHAINS_SYMBOL); |
841 | //fprintf(stderr,"Staking height %d for %s\n", chainActive.Tip()->nHeight + 1, ASSETCHAINS_SYMBOL); | |
135fa24e | 842 | |
843 | miningTimer.start(); | |
844 | ||
845 | try { | |
135fa24e | 846 | while (true) |
847 | { | |
848 | miningTimer.stop(); | |
849 | waitForPeers(chainparams); | |
850 | miningTimer.start(); | |
851 | ||
852 | // Create new block | |
853 | unsigned int nTransactionsUpdatedLast = mempool.GetTransactionsUpdated(); | |
854 | CBlockIndex* pindexPrev = chainActive.Tip(); | |
855 | if ( Mining_height != pindexPrev->nHeight+1 ) | |
856 | { | |
857 | Mining_height = pindexPrev->nHeight+1; | |
858 | Mining_start = (uint32_t)time(NULL); | |
859 | } | |
860 | ||
1fae37f6 MT |
861 | // Check for stop or if block needs to be rebuilt |
862 | boost::this_thread::interruption_point(); | |
863 | ||
135fa24e | 864 | // try to stake a block |
1fae37f6 MT |
865 | CBlockTemplate *ptr = NULL; |
866 | if (Mining_height > VERUS_MIN_STAKEAGE) | |
867 | ptr = CreateNewBlockWithKey(reservekey, true); | |
135fa24e | 868 | |
869 | if ( ptr == 0 ) | |
870 | { | |
1fae37f6 MT |
871 | // wait to try another staking block until after the tip moves again |
872 | while ( chainActive.Tip() == pindexPrev ) | |
873 | sleep(5); | |
135fa24e | 874 | continue; |
875 | } | |
876 | ||
877 | unique_ptr<CBlockTemplate> pblocktemplate(ptr); | |
878 | if (!pblocktemplate.get()) | |
879 | { | |
880 | if (GetArg("-mineraddress", "").empty()) { | |
1fae37f6 | 881 | LogPrintf("Error in %s staker: Keypool ran out, please call keypoolrefill before restarting the mining thread\n", |
135fa24e | 882 | ASSETCHAINS_ALGORITHMS[ASSETCHAINS_ALGO]); |
883 | } else { | |
884 | // Should never reach here, because -mineraddress validity is checked in init.cpp | |
1fae37f6 | 885 | LogPrintf("Error in %s staker: Invalid %s -mineraddress\n", ASSETCHAINS_ALGORITHMS[ASSETCHAINS_ALGO], ASSETCHAINS_SYMBOL); |
135fa24e | 886 | } |
887 | return; | |
888 | } | |
889 | ||
1fae37f6 | 890 | |
135fa24e | 891 | CBlock *pblock = &pblocktemplate->block; |
1fae37f6 | 892 | LogPrintf("Staking with %u transactions in block (%u bytes)\n", pblock->vtx.size(),::GetSerializeSize(*pblock,SER_NETWORK,PROTOCOL_VERSION)); |
135fa24e | 893 | // |
894 | // Search | |
895 | // | |
1fae37f6 MT |
896 | int64_t nStart = GetTime(); |
897 | ||
52670b72 MT |
898 | // take up the necessary space for alignment |
899 | pblock->nSolution = solnPlaceholder; | |
900 | ||
1fae37f6 MT |
901 | // we don't use this, but IncrementExtraNonce is the function that builds the merkle tree |
902 | unsigned int nExtraNonce = 0; | |
903 | IncrementExtraNonce(pblock, pindexPrev, nExtraNonce); | |
135fa24e | 904 | |
1fae37f6 MT |
905 | if (vNodes.empty() && chainparams.MiningRequiresPeers()) |
906 | { | |
907 | if ( Mining_height > ASSETCHAINS_MINHEIGHT ) | |
908 | { | |
909 | fprintf(stderr,"no nodes, attempting reconnect\n"); | |
910 | continue; | |
911 | } | |
912 | } | |
913 | ||
914 | if (mempool.GetTransactionsUpdated() != nTransactionsUpdatedLast && GetTime() - nStart > 60) | |
915 | { | |
916 | fprintf(stderr,"timeout, retrying\n"); | |
917 | continue; | |
918 | } | |
135fa24e | 919 | |
920 | if ( pindexPrev != chainActive.Tip() ) | |
921 | { | |
922 | printf("Block %d added to chain\n", chainActive.Tip()->nHeight); | |
923 | MilliSleep(250); | |
924 | continue; | |
925 | } | |
926 | ||
1fae37f6 | 927 | SetThreadPriority(THREAD_PRIORITY_NORMAL); |
135fa24e | 928 | |
1fae37f6 MT |
929 | int32_t unlockTime = komodo_block_unlocktime(Mining_height); |
930 | int64_t subsidy = (int64_t)(pblock->vtx[0].vout[0].nValue); | |
135fa24e | 931 | |
1fae37f6 | 932 | uint256 hashTarget = ArithToUint256(arith_uint256().SetCompact(pblock->nBits)); |
135fa24e | 933 | |
1fae37f6 MT |
934 | LogPrintf("Using %s algorithm:\n", ASSETCHAINS_ALGORITHMS[ASSETCHAINS_ALGO]); |
935 | LogPrintf("Staked block found \n hash: %s \ntarget: %s\n", pblock->GetHash().GetHex(), hashTarget.GetHex()); | |
936 | printf("Found block %d \n", Mining_height ); | |
937 | printf("staking reward %.8f %s!\n", (double)subsidy / (double)COIN, ASSETCHAINS_SYMBOL); | |
938 | printf(" hash: %s \ntarget: %s\n", pblock->GetHash().GetHex().c_str(), hashTarget.GetHex().c_str()); | |
939 | if (unlockTime > Mining_height && subsidy >= ASSETCHAINS_TIMELOCKGTE) | |
940 | printf("- timelocked until block %i\n", unlockTime); | |
941 | else | |
942 | printf("\n"); | |
135fa24e | 943 | |
1fae37f6 | 944 | pblock->nBits = GetNextWorkRequired(pindexPrev, pblock, chainparams.GetConsensus()); |
135fa24e | 945 | |
1fae37f6 | 946 | UpdateTime(pblock, chainparams.GetConsensus(), pindexPrev); |
135fa24e | 947 | |
1fae37f6 | 948 | ProcessBlockFound(pblock, *pwallet, reservekey); |
135fa24e | 949 | |
1fae37f6 MT |
950 | // Check for stop or if block needs to be rebuilt |
951 | boost::this_thread::interruption_point(); | |
952 | SetThreadPriority(THREAD_PRIORITY_LOWEST); | |
135fa24e | 953 | |
3da69a31 MT |
954 | sleep(5); |
955 | ||
1fae37f6 MT |
956 | // In regression test mode, stop mining after a block is found. |
957 | if (chainparams.MineBlocksOnDemand()) { | |
958 | throw boost::thread_interrupted(); | |
135fa24e | 959 | } |
960 | } | |
961 | } | |
962 | catch (const boost::thread_interrupted&) | |
963 | { | |
964 | miningTimer.stop(); | |
965 | LogPrintf("VerusStaker terminated\n"); | |
966 | throw; | |
967 | } | |
968 | catch (const std::runtime_error &e) | |
969 | { | |
970 | miningTimer.stop(); | |
971 | LogPrintf("VerusStaker runtime error: %s\n", e.what()); | |
972 | return; | |
973 | } | |
974 | miningTimer.stop(); | |
975 | } | |
976 | ||
42181656 | 977 | void static BitcoinMiner_noeq(CWallet *pwallet) |
978 | #else | |
979 | void static BitcoinMiner_noeq() | |
980 | #endif | |
981 | { | |
05f6e633 | 982 | LogPrintf("%s miner started\n", ASSETCHAINS_ALGORITHMS[ASSETCHAINS_ALGO]); |
05f6e633 | 983 | RenameThread("verushash-miner"); |
42181656 | 984 | |
985 | #ifdef ENABLE_WALLET | |
986 | // Each thread has its own key | |
987 | CReserveKey reservekey(pwallet); | |
988 | #endif | |
989 | ||
2910478b | 990 | const CChainParams& chainparams = Params(); |
42181656 | 991 | // Each thread has its own counter |
992 | unsigned int nExtraNonce = 0; | |
993 | std::vector<unsigned char> solnPlaceholder = std::vector<unsigned char>(); | |
994 | solnPlaceholder.resize(Eh200_9.SolutionWidth); | |
42181656 | 995 | uint8_t *script; uint64_t total,checktoshis; int32_t i,j; |
996 | ||
997 | while ( (ASSETCHAIN_INIT == 0 || KOMODO_INITDONE == 0) ) //chainActive.Tip()->nHeight != 235300 && | |
998 | { | |
999 | sleep(1); | |
1000 | if ( komodo_baseid(ASSETCHAINS_SYMBOL) < 0 ) | |
1001 | break; | |
1002 | } | |
9f3e2213 | 1003 | |
3da69a31 MT |
1004 | SetThreadPriority(THREAD_PRIORITY_LOWEST); |
1005 | ||
5642c96c | 1006 | // try a nice clean peer connection to start |
1007 | waitForPeers(chainparams); | |
3da69a31 | 1008 | CBlockIndex* pindexPrev; |
9f3e2213 | 1009 | do { |
3da69a31 MT |
1010 | pindexPrev = chainActive.Tip(); |
1011 | MilliSleep(5000 + rand() % 5000); | |
1012 | } while (pindexPrev != chainActive.Tip()); | |
6176a421 | 1013 | |
a62041d9 | 1014 | printf("Mining height %d\n", chainActive.Tip()->nHeight + 1); |
9f3e2213 | 1015 | |
42181656 | 1016 | miningTimer.start(); |
1017 | ||
1018 | try { | |
05f6e633 | 1019 | fprintf(stderr,"Mining %s with %s\n", ASSETCHAINS_SYMBOL, ASSETCHAINS_ALGORITHMS[ASSETCHAINS_ALGO]); |
42181656 | 1020 | while (true) |
1021 | { | |
68334c8d | 1022 | miningTimer.stop(); |
1023 | waitForPeers(chainparams); | |
dfcf8255 | 1024 | |
3da69a31 MT |
1025 | pindexPrev = chainActive.Tip(); |
1026 | sleep(1); | |
dfcf8255 MT |
1027 | |
1028 | // prevent forking on startup before the diff algorithm kicks in | |
3da69a31 | 1029 | if (pindexPrev->nHeight < 50 || pindexPrev != chainActive.Tip()) |
dfcf8255 MT |
1030 | { |
1031 | do { | |
dfcf8255 | 1032 | pindexPrev = chainActive.Tip(); |
3da69a31 MT |
1033 | MilliSleep(5000 + rand() % 5000); |
1034 | } while (pindexPrev != chainActive.Tip()); | |
dfcf8255 | 1035 | } |
68334c8d | 1036 | miningTimer.start(); |
42181656 | 1037 | |
1038 | // Create new block | |
1039 | unsigned int nTransactionsUpdatedLast = mempool.GetTransactionsUpdated(); | |
42181656 | 1040 | if ( Mining_height != pindexPrev->nHeight+1 ) |
1041 | { | |
1042 | Mining_height = pindexPrev->nHeight+1; | |
1043 | Mining_start = (uint32_t)time(NULL); | |
1044 | } | |
1045 | ||
1046 | //fprintf(stderr,"%s create new block ht.%d\n",ASSETCHAINS_SYMBOL,Mining_height); | |
1047 | ||
1048 | #ifdef ENABLE_WALLET | |
1049 | CBlockTemplate *ptr = CreateNewBlockWithKey(reservekey); | |
1050 | #else | |
1051 | CBlockTemplate *ptr = CreateNewBlockWithKey(); | |
1052 | #endif | |
1053 | if ( ptr == 0 ) | |
1054 | { | |
1055 | static uint32_t counter; | |
1056 | if ( counter++ < 100 ) | |
1057 | fprintf(stderr,"created illegal block, retry\n"); | |
1058 | continue; | |
1059 | } | |
1060 | unique_ptr<CBlockTemplate> pblocktemplate(ptr); | |
1061 | if (!pblocktemplate.get()) | |
1062 | { | |
1063 | if (GetArg("-mineraddress", "").empty()) { | |
05f6e633 | 1064 | LogPrintf("Error in %s miner: Keypool ran out, please call keypoolrefill before restarting the mining thread\n", |
1065 | ASSETCHAINS_ALGORITHMS[ASSETCHAINS_ALGO]); | |
42181656 | 1066 | } else { |
1067 | // Should never reach here, because -mineraddress validity is checked in init.cpp | |
05f6e633 | 1068 | LogPrintf("Error in %s miner: Invalid %s -mineraddress\n", ASSETCHAINS_ALGORITHMS[ASSETCHAINS_ALGO], ASSETCHAINS_SYMBOL); |
42181656 | 1069 | } |
1070 | return; | |
1071 | } | |
1072 | CBlock *pblock = &pblocktemplate->block; | |
1073 | if ( ASSETCHAINS_SYMBOL[0] != 0 ) | |
1074 | { | |
1075 | if ( ASSETCHAINS_REWARD[0] == 0 && !ASSETCHAINS_LASTERA ) | |
1076 | { | |
1077 | if ( pblock->vtx.size() == 1 && pblock->vtx[0].vout.size() == 1 && Mining_height > ASSETCHAINS_MINHEIGHT ) | |
1078 | { | |
1079 | static uint32_t counter; | |
1080 | if ( counter++ < 10 ) | |
1081 | fprintf(stderr,"skip generating %s on-demand block, no tx avail\n",ASSETCHAINS_SYMBOL); | |
1082 | sleep(10); | |
1083 | continue; | |
1084 | } else fprintf(stderr,"%s vouts.%d mining.%d vs %d\n",ASSETCHAINS_SYMBOL,(int32_t)pblock->vtx[0].vout.size(),Mining_height,ASSETCHAINS_MINHEIGHT); | |
1085 | } | |
1086 | } | |
1087 | IncrementExtraNonce(pblock, pindexPrev, nExtraNonce); | |
1088 | LogPrintf("Running %s miner with %u transactions in block (%u bytes)\n",ASSETCHAINS_ALGORITHMS[ASSETCHAINS_ALGO], | |
1089 | pblock->vtx.size(),::GetSerializeSize(*pblock,SER_NETWORK,PROTOCOL_VERSION)); | |
1090 | // | |
1091 | // Search | |
1092 | // | |
1093 | uint32_t savebits; int64_t nStart = GetTime(); | |
1094 | ||
1095 | pblock->nSolution = solnPlaceholder; | |
1096 | savebits = pblock->nBits; | |
1097 | arith_uint256 hashTarget = arith_uint256().SetCompact(pblock->nBits); | |
1098 | arith_uint256 mask(ASSETCHAINS_NONCEMASK[ASSETCHAINS_ALGO]); | |
1099 | ||
1100 | Mining_start = 0; | |
ef70c5b2 | 1101 | |
ef70c5b2 | 1102 | if ( pindexPrev != chainActive.Tip() ) |
05f6e633 | 1103 | { |
6176a421 | 1104 | printf("Block %d added to chain\n", chainActive.Tip()->nHeight); |
1105 | MilliSleep(250); | |
05f6e633 | 1106 | continue; |
1107 | } | |
ef70c5b2 | 1108 | |
135fa24e | 1109 | if ( ASSETCHAINS_STAKED != 0 ) |
1110 | { | |
1111 | int32_t percPoS,z; | |
1112 | hashTarget = komodo_PoWtarget(&percPoS,hashTarget,Mining_height,ASSETCHAINS_STAKED); | |
1113 | for (z=31; z>=0; z--) | |
1114 | fprintf(stderr,"%02x",((uint8_t *)&hashTarget)[z]); | |
1115 | fprintf(stderr," PoW for staked coin PoS %d%% vs target %d%%\n",percPoS,(int32_t)ASSETCHAINS_STAKED); | |
1116 | } | |
1117 | ||
42181656 | 1118 | while (true) |
1119 | { | |
1120 | // for speed check multiples at a time | |
1121 | for (int i = 0; i < ASSETCHAINS_HASHESPERROUND[ASSETCHAINS_ALGO]; i++) | |
1122 | { | |
1123 | solutionTargetChecks.increment(); | |
1124 | ||
1125 | // Update nNonce and nTime | |
1126 | pblock->nNonce = ArithToUint256(UintToArith256(pblock->nNonce) + 1); | |
1127 | ||
1128 | if ( UintToArith256(pblock->GetHash()) <= hashTarget ) | |
1129 | { | |
1130 | SetThreadPriority(THREAD_PRIORITY_NORMAL); | |
1131 | ||
ef70c5b2 | 1132 | int32_t unlockTime = komodo_block_unlocktime(Mining_height); |
1133 | int64_t subsidy = (int64_t)(pblock->vtx[0].vout[0].nValue); | |
1134 | ||
135fa24e | 1135 | LogPrintf("Using %s algorithm:\n", ASSETCHAINS_ALGORITHMS[ASSETCHAINS_ALGO]); |
42181656 | 1136 | LogPrintf("proof-of-work found \n hash: %s \ntarget: %s\n", pblock->GetHash().GetHex(), hashTarget.GetHex()); |
14d104e9 | 1137 | printf("Found block %d \n", Mining_height ); |
a62041d9 | 1138 | printf("mining reward %.8f %s!\n", (double)subsidy / (double)COIN, ASSETCHAINS_SYMBOL); |
ef70c5b2 | 1139 | printf(" hash: %s \ntarget: %s\n", pblock->GetHash().GetHex().c_str(), hashTarget.GetHex().c_str()); |
1140 | if (unlockTime > Mining_height && subsidy >= ASSETCHAINS_TIMELOCKGTE) | |
1141 | printf("- timelocked until block %i\n", unlockTime); | |
1142 | else | |
1143 | printf("\n"); | |
1144 | ||
42181656 | 1145 | #ifdef ENABLE_WALLET |
1146 | ProcessBlockFound(pblock, *pwallet, reservekey); | |
1147 | #else | |
1148 | ProcessBlockFound(pblock)); | |
1149 | #endif | |
1150 | SetThreadPriority(THREAD_PRIORITY_LOWEST); | |
1151 | ||
1152 | // In regression test mode, stop mining after a block is found. | |
1153 | if (chainparams.MineBlocksOnDemand()) { | |
1154 | throw boost::thread_interrupted(); | |
1155 | } | |
1156 | break; | |
1157 | } | |
ef70c5b2 | 1158 | else |
42181656 | 1159 | { |
ef70c5b2 | 1160 | if ((UintToArith256(pblock->nNonce) & mask) == mask) |
1161 | { | |
1162 | break; | |
1163 | } | |
42181656 | 1164 | } |
1165 | } | |
1166 | ||
1167 | // Check for stop or if block needs to be rebuilt | |
1168 | boost::this_thread::interruption_point(); | |
1169 | ||
1170 | if (vNodes.empty() && chainparams.MiningRequiresPeers()) | |
1171 | { | |
1172 | if ( Mining_height > ASSETCHAINS_MINHEIGHT ) | |
1173 | { | |
ef70c5b2 | 1174 | fprintf(stderr,"no nodes, attempting reconnect\n"); |
42181656 | 1175 | break; |
1176 | } | |
1177 | } | |
1178 | ||
1179 | if ((UintToArith256(pblock->nNonce) & mask) == mask) | |
1180 | { | |
5642c96c | 1181 | fprintf(stderr,"%lu mega hashes complete - working\n", (ASSETCHAINS_NONCEMASK[ASSETCHAINS_ALGO] + 1) / 1048576); |
42181656 | 1182 | break; |
1183 | } | |
1184 | ||
1185 | if (mempool.GetTransactionsUpdated() != nTransactionsUpdatedLast && GetTime() - nStart > 60) | |
1186 | { | |
ef70c5b2 | 1187 | fprintf(stderr,"timeout, retrying\n"); |
42181656 | 1188 | break; |
1189 | } | |
1190 | ||
1191 | if ( pindexPrev != chainActive.Tip() ) | |
1192 | { | |
6176a421 | 1193 | printf("Block %d added to chain\n", chainActive.Tip()->nHeight); |
42181656 | 1194 | break; |
1195 | } | |
1196 | ||
1197 | pblock->nBits = savebits; | |
1198 | UpdateTime(pblock, chainparams.GetConsensus(), pindexPrev); | |
1199 | if (chainparams.GetConsensus().fPowAllowMinDifficultyBlocks) | |
1200 | { | |
1201 | // Changing pblock->nTime can change work required on testnet: | |
1202 | hashTarget.SetCompact(pblock->nBits); | |
1203 | } | |
1204 | } | |
1205 | } | |
1206 | } | |
1207 | catch (const boost::thread_interrupted&) | |
1208 | { | |
1209 | miningTimer.stop(); | |
1210 | LogPrintf("KomodoMiner terminated\n"); | |
1211 | throw; | |
1212 | } | |
1213 | catch (const std::runtime_error &e) | |
1214 | { | |
1215 | miningTimer.stop(); | |
1216 | LogPrintf("KomodoMiner runtime error: %s\n", e.what()); | |
1217 | return; | |
1218 | } | |
1219 | miningTimer.stop(); | |
1220 | } | |
1221 | ||
8e8b6d70 | 1222 | #ifdef ENABLE_WALLET |
d247a5d1 | 1223 | void static BitcoinMiner(CWallet *pwallet) |
8e8b6d70 JG |
1224 | #else |
1225 | void static BitcoinMiner() | |
1226 | #endif | |
d247a5d1 | 1227 | { |
2e500f50 | 1228 | LogPrintf("KomodoMiner started\n"); |
d247a5d1 | 1229 | SetThreadPriority(THREAD_PRIORITY_LOWEST); |
2e500f50 | 1230 | RenameThread("komodo-miner"); |
bebe7282 | 1231 | const CChainParams& chainparams = Params(); |
e9e70b95 | 1232 | |
8e8b6d70 JG |
1233 | #ifdef ENABLE_WALLET |
1234 | // Each thread has its own key | |
d247a5d1 | 1235 | CReserveKey reservekey(pwallet); |
8e8b6d70 | 1236 | #endif |
e9e70b95 | 1237 | |
8e8b6d70 | 1238 | // Each thread has its own counter |
d247a5d1 | 1239 | unsigned int nExtraNonce = 0; |
e9e70b95 | 1240 | |
e9574728 JG |
1241 | unsigned int n = chainparams.EquihashN(); |
1242 | unsigned int k = chainparams.EquihashK(); | |
8683bd8d | 1243 | uint8_t *script; uint64_t total,checktoshis; int32_t i,j,notaryid = -1; |
b7ba6413 | 1244 | while ( (ASSETCHAIN_INIT == 0 || KOMODO_INITDONE == 0) ) //chainActive.Tip()->nHeight != 235300 && |
755ead98 | 1245 | { |
1246 | sleep(1); | |
4e624c04 | 1247 | if ( komodo_baseid(ASSETCHAINS_SYMBOL) < 0 ) |
1248 | break; | |
755ead98 | 1249 | } |
8683bd8d | 1250 | komodo_chosennotary(¬aryid,chainActive.Tip()->nHeight,NOTARY_PUBKEY33,(uint32_t)chainActive.Tip()->GetBlockTime()); |
e9e70b95 | 1251 | |
755ead98 | 1252 | std::string solver; |
e1e65cef | 1253 | //if ( notaryid >= 0 || ASSETCHAINS_SYMBOL[0] != 0 ) |
e9e70b95 | 1254 | solver = "tromp"; |
e1e65cef | 1255 | //else solver = "default"; |
5f0009b2 | 1256 | assert(solver == "tromp" || solver == "default"); |
c7aaab7a | 1257 | LogPrint("pow", "Using Equihash solver \"%s\" with n = %u, k = %u\n", solver, n, k); |
9ee43671 | 1258 | if ( ASSETCHAINS_SYMBOL[0] != 0 ) |
25f7ef8c | 1259 | fprintf(stderr,"notaryid.%d Mining.%s with %s\n",notaryid,ASSETCHAINS_SYMBOL,solver.c_str()); |
5a360a5c JG |
1260 | std::mutex m_cs; |
1261 | bool cancelSolver = false; | |
1262 | boost::signals2::connection c = uiInterface.NotifyBlockTip.connect( | |
e9e70b95 | 1263 | [&m_cs, &cancelSolver](const uint256& hashNewTip) mutable { |
1264 | std::lock_guard<std::mutex> lock{m_cs}; | |
1265 | cancelSolver = true; | |
1266 | } | |
1267 | ); | |
07be8f7e | 1268 | miningTimer.start(); |
e9e70b95 | 1269 | |
0655fac0 | 1270 | try { |
ad84148d | 1271 | if ( ASSETCHAINS_SYMBOL[0] != 0 ) |
c96df8ec | 1272 | fprintf(stderr,"try %s Mining with %s\n",ASSETCHAINS_SYMBOL,solver.c_str()); |
e725f1cb | 1273 | while (true) |
1274 | { | |
2c70b583 | 1275 | if (chainparams.MiningRequiresPeers()) //chainActive.Tip()->nHeight != 235300 && |
e725f1cb | 1276 | { |
a96fd7b5 | 1277 | //if ( ASSETCHAINS_SEED != 0 && chainActive.Tip()->nHeight < 100 ) |
1278 | // break; | |
0655fac0 PK |
1279 | // Busy-wait for the network to come online so we don't waste time mining |
1280 | // on an obsolete chain. In regtest mode we expect to fly solo. | |
07be8f7e | 1281 | miningTimer.stop(); |
bba7c249 GM |
1282 | do { |
1283 | bool fvNodesEmpty; | |
1284 | { | |
373668be | 1285 | //LOCK(cs_vNodes); |
bba7c249 GM |
1286 | fvNodesEmpty = vNodes.empty(); |
1287 | } | |
269fe243 | 1288 | if (!fvNodesEmpty )//&& !IsInitialBlockDownload()) |
bba7c249 | 1289 | break; |
f8e367eb | 1290 | MilliSleep(5000); |
ad84148d | 1291 | //fprintf(stderr,"fvNodesEmpty %d IsInitialBlockDownload(%s) %d\n",(int32_t)fvNodesEmpty,ASSETCHAINS_SYMBOL,(int32_t)IsInitialBlockDownload()); |
e9e70b95 | 1292 | |
bba7c249 | 1293 | } while (true); |
ad84148d | 1294 | //fprintf(stderr,"%s Found peers\n",ASSETCHAINS_SYMBOL); |
07be8f7e | 1295 | miningTimer.start(); |
0655fac0 | 1296 | } |
f70ced6a | 1297 | /*while ( ASSETCHAINS_SYMBOL[0] != 0 && chainActive.Tip()->nHeight < ASSETCHAINS_MINHEIGHT ) |
e9e70b95 | 1298 | { |
1299 | fprintf(stderr,"%s waiting for block 100, ht.%d\n",ASSETCHAINS_SYMBOL,chainActive.Tip()->nHeight); | |
1300 | sleep(3); | |
1301 | }*/ | |
0655fac0 PK |
1302 | // |
1303 | // Create new block | |
1304 | // | |
1305 | unsigned int nTransactionsUpdatedLast = mempool.GetTransactionsUpdated(); | |
48265f3c | 1306 | CBlockIndex* pindexPrev = chainActive.Tip(); |
4940066c | 1307 | if ( Mining_height != pindexPrev->nHeight+1 ) |
1308 | { | |
1309 | Mining_height = pindexPrev->nHeight+1; | |
1310 | Mining_start = (uint32_t)time(NULL); | |
1311 | } | |
8e9ef91c | 1312 | if ( ASSETCHAINS_SYMBOL[0] != 0 && ASSETCHAINS_STAKED == 0 ) |
2825c0b5 | 1313 | { |
40304479 | 1314 | //fprintf(stderr,"%s create new block ht.%d\n",ASSETCHAINS_SYMBOL,Mining_height); |
2825c0b5 | 1315 | sleep(3); |
1316 | } | |
135fa24e | 1317 | |
8e8b6d70 | 1318 | #ifdef ENABLE_WALLET |
135fa24e | 1319 | // notaries always default to staking |
1320 | CBlockTemplate *ptr = CreateNewBlockWithKey(reservekey, (ASSETCHAINS_STAKED != 0 && NOTARY_PUBKEY33[0] != 0)); | |
8e8b6d70 | 1321 | #else |
945f015d | 1322 | CBlockTemplate *ptr = CreateNewBlockWithKey(); |
8e8b6d70 | 1323 | #endif |
08d0b73c | 1324 | if ( ptr == 0 ) |
1325 | { | |
d0f7ead0 | 1326 | static uint32_t counter; |
5bb3d0fe | 1327 | if ( counter++ < 100 && ASSETCHAINS_STAKED == 0 ) |
1b5b89ba | 1328 | fprintf(stderr,"created illegal block, retry\n"); |
8fc79ac9 | 1329 | sleep(1); |
d0f7ead0 | 1330 | continue; |
08d0b73c | 1331 | } |
1332 | unique_ptr<CBlockTemplate> pblocktemplate(ptr); | |
0655fac0 | 1333 | if (!pblocktemplate.get()) |
6c37f7fd | 1334 | { |
8e8b6d70 | 1335 | if (GetArg("-mineraddress", "").empty()) { |
945f015d | 1336 | LogPrintf("Error in KomodoMiner: Keypool ran out, please call keypoolrefill before restarting the mining thread\n"); |
8e8b6d70 JG |
1337 | } else { |
1338 | // Should never reach here, because -mineraddress validity is checked in init.cpp | |
945f015d | 1339 | LogPrintf("Error in KomodoMiner: Invalid -mineraddress\n"); |
8e8b6d70 | 1340 | } |
0655fac0 | 1341 | return; |
6c37f7fd | 1342 | } |
0655fac0 | 1343 | CBlock *pblock = &pblocktemplate->block; |
16c7bf6b | 1344 | if ( ASSETCHAINS_SYMBOL[0] != 0 ) |
1345 | { | |
42181656 | 1346 | if ( ASSETCHAINS_REWARD[0] == 0 && !ASSETCHAINS_LASTERA ) |
16c7bf6b | 1347 | { |
8683bd8d | 1348 | if ( pblock->vtx.size() == 1 && pblock->vtx[0].vout.size() == 1 && Mining_height > ASSETCHAINS_MINHEIGHT ) |
1349 | { | |
1350 | static uint32_t counter; | |
1351 | if ( counter++ < 10 ) | |
1352 | fprintf(stderr,"skip generating %s on-demand block, no tx avail\n",ASSETCHAINS_SYMBOL); | |
1353 | sleep(10); | |
1354 | continue; | |
1355 | } else fprintf(stderr,"%s vouts.%d mining.%d vs %d\n",ASSETCHAINS_SYMBOL,(int32_t)pblock->vtx[0].vout.size(),Mining_height,ASSETCHAINS_MINHEIGHT); | |
1356 | } | |
16c7bf6b | 1357 | } |
0655fac0 | 1358 | IncrementExtraNonce(pblock, pindexPrev, nExtraNonce); |
2e500f50 | 1359 | LogPrintf("Running KomodoMiner.%s with %u transactions in block (%u bytes)\n",solver.c_str(),pblock->vtx.size(),::GetSerializeSize(*pblock,SER_NETWORK,PROTOCOL_VERSION)); |
0655fac0 PK |
1360 | // |
1361 | // Search | |
1362 | // | |
a30dd993 | 1363 | uint8_t pubkeys[66][33]; uint32_t blocktimes[66]; int mids[256],gpucount,nonzpkeys,i,j,externalflag; uint32_t savebits; int64_t nStart = GetTime(); |
404391b5 | 1364 | savebits = pblock->nBits; |
88287857 | 1365 | HASHTarget = arith_uint256().SetCompact(pblock->nBits); |
f0100e72 | 1366 | roundrobin_delay = ROUNDROBIN_DELAY; |
3e7e3109 | 1367 | if ( ASSETCHAINS_SYMBOL[0] == 0 && notaryid >= 0 ) |
5203fc4b | 1368 | { |
fda5f849 | 1369 | j = 65; |
5a471ba9 | 1370 | if ( (Mining_height >= 235300 && Mining_height < 236000) || (Mining_height % KOMODO_ELECTION_GAP) > 64 || (Mining_height % KOMODO_ELECTION_GAP) == 0 ) |
fb6c7505 | 1371 | { |
4fff8a63 | 1372 | int32_t dispflag = 0; |
ef70c5b2 | 1373 | if ( notaryid <= 3 || notaryid == 32 || (notaryid >= 43 && notaryid <= 45) &¬aryid == 51 || notaryid == 52 || notaryid == 56 || notaryid == 57 ) |
4fff8a63 | 1374 | dispflag = 1; |
a30dd993 | 1375 | komodo_eligiblenotary(pubkeys,mids,blocktimes,&nonzpkeys,pindexPrev->nHeight); |
29e60e48 | 1376 | if ( nonzpkeys > 0 ) |
1377 | { | |
ccb71a6e | 1378 | for (i=0; i<33; i++) |
1379 | if( pubkeys[0][i] != 0 ) | |
1380 | break; | |
1381 | if ( i == 33 ) | |
1382 | externalflag = 1; | |
1383 | else externalflag = 0; | |
e4a383e3 | 1384 | if ( NOTARY_PUBKEY33[0] != 0 ) |
b176c125 | 1385 | { |
345e545e | 1386 | for (i=1; i<66; i++) |
1387 | if ( memcmp(pubkeys[i],pubkeys[0],33) == 0 ) | |
1388 | break; | |
6494f040 | 1389 | if ( externalflag == 0 && i != 66 && mids[i] >= 0 ) |
1390 | printf("VIOLATION at %d, notaryid.%d\n",i,mids[i]); | |
2c7ad758 | 1391 | for (j=gpucount=0; j<65; j++) |
1392 | { | |
4fff8a63 | 1393 | if ( dispflag != 0 ) |
e4a383e3 | 1394 | { |
1395 | if ( mids[j] >= 0 ) | |
1396 | fprintf(stderr,"%d ",mids[j]); | |
1397 | else fprintf(stderr,"GPU "); | |
1398 | } | |
2c7ad758 | 1399 | if ( mids[j] == -1 ) |
1400 | gpucount++; | |
1401 | } | |
b000fa04 | 1402 | if ( gpucount > j/2 ) |
f0100e72 | 1403 | { |
dd98c02d | 1404 | double delta; |
d2d3c766 | 1405 | if ( notaryid < 0 ) |
1406 | i = (rand() % 64); | |
1407 | else i = ((Mining_height + notaryid) % 64); | |
b000fa04 | 1408 | delta = sqrt((double)gpucount - j/2) / 2.; |
dd98c02d | 1409 | roundrobin_delay += ((delta * i) / 64) - delta; |
67478d4a | 1410 | //fprintf(stderr,"delta.%f %f %f\n",delta,(double)(gpucount - j/3) / 2,(delta * i) / 64); |
f0100e72 | 1411 | } |
4fff8a63 | 1412 | if ( dispflag != 0 ) |
2c7ba74d | 1413 | fprintf(stderr," <- prev minerids from ht.%d notary.%d gpucount.%d %.2f%% t.%u %d\n",pindexPrev->nHeight,notaryid,gpucount,100.*(double)gpucount/j,(uint32_t)time(NULL),roundrobin_delay); |
b176c125 | 1414 | } |
29e60e48 | 1415 | for (j=0; j<65; j++) |
1416 | if ( mids[j] == notaryid ) | |
1417 | break; | |
49b49585 | 1418 | if ( j == 65 ) |
1419 | KOMODO_LASTMINED = 0; | |
965f0f7e | 1420 | } else fprintf(stderr,"no nonz pubkeys\n"); |
49b49585 | 1421 | if ( (Mining_height >= 235300 && Mining_height < 236000) || (j == 65 && Mining_height > KOMODO_MAYBEMINED+1 && Mining_height > KOMODO_LASTMINED+64) ) |
fda5f849 | 1422 | { |
88287857 | 1423 | HASHTarget = arith_uint256().SetCompact(KOMODO_MINDIFF_NBITS); |
fda5f849 | 1424 | fprintf(stderr,"I am the chosen one for %s ht.%d\n",ASSETCHAINS_SYMBOL,pindexPrev->nHeight+1); |
1425 | } //else fprintf(stderr,"duplicate at j.%d\n",j); | |
fb6c7505 | 1426 | } else Mining_start = 0; |
d7d27bb3 | 1427 | } else Mining_start = 0; |
deba7f20 | 1428 | if ( ASSETCHAINS_STAKED != 0 && NOTARY_PUBKEY33[0] == 0 ) |
e725f1cb | 1429 | { |
deba7f20 | 1430 | int32_t percPoS,z; |
d9935f2b | 1431 | /*if ( Mining_height <= 100 ) |
16cd9f2d | 1432 | { |
c966741f | 1433 | sleep(60); |
1434 | continue; | |
16c7bf6b | 1435 | }*/ |
18443f69 | 1436 | HASHTarget_POW = komodo_PoWtarget(&percPoS,HASHTarget,Mining_height,ASSETCHAINS_STAKED); |
deba7f20 | 1437 | for (z=31; z>=0; z--) |
8fbee929 | 1438 | fprintf(stderr,"%02x",((uint8_t *)&HASHTarget_POW)[z]); |
b509fdae | 1439 | fprintf(stderr," PoW for staked coin PoS %d%% vs target %d%%\n",percPoS,(int32_t)ASSETCHAINS_STAKED); |
deba7f20 | 1440 | } |
e725f1cb | 1441 | while (true) |
1442 | { | |
16c7bf6b | 1443 | /*if ( 0 && ASSETCHAINS_SYMBOL[0] != 0 && pblock->vtx[0].vout.size() == 1 && Mining_height > ASSETCHAINS_MINHEIGHT ) // skips when it shouldnt |
e9e70b95 | 1444 | { |
1445 | fprintf(stderr,"skip generating %s on-demand block, no tx avail\n",ASSETCHAINS_SYMBOL); | |
1446 | sleep(10); | |
1447 | break; | |
1448 | }*/ | |
7213c0b1 | 1449 | // Hash state |
8c22eb46 | 1450 | KOMODO_CHOSEN_ONE = 0; |
42181656 | 1451 | |
7213c0b1 | 1452 | crypto_generichash_blake2b_state state; |
e9574728 | 1453 | EhInitialiseState(n, k, state); |
7213c0b1 JG |
1454 | // I = the block header minus nonce and solution. |
1455 | CEquihashInput I{*pblock}; | |
1456 | CDataStream ss(SER_NETWORK, PROTOCOL_VERSION); | |
1457 | ss << I; | |
7213c0b1 JG |
1458 | // H(I||... |
1459 | crypto_generichash_blake2b_update(&state, (unsigned char*)&ss[0], ss.size()); | |
8e165d57 JG |
1460 | // H(I||V||... |
1461 | crypto_generichash_blake2b_state curr_state; | |
1462 | curr_state = state; | |
7a4c01c9 | 1463 | crypto_generichash_blake2b_update(&curr_state,pblock->nNonce.begin(),pblock->nNonce.size()); |
8e165d57 | 1464 | // (x_1, x_2, ...) = A(I, V, n, k) |
7a4c01c9 | 1465 | LogPrint("pow", "Running Equihash solver \"%s\" with nNonce = %s\n",solver, pblock->nNonce.ToString()); |
18443f69 | 1466 | arith_uint256 hashTarget; |
1f722359 | 1467 | if ( NOTARY_PUBKEY33[0] == 0 && ASSETCHAINS_STAKED > 0 && ASSETCHAINS_STAKED <= 100 ) |
18443f69 | 1468 | hashTarget = HASHTarget_POW; |
1469 | else hashTarget = HASHTarget; | |
5be6abbf | 1470 | std::function<bool(std::vector<unsigned char>)> validBlock = |
8e8b6d70 | 1471 | #ifdef ENABLE_WALLET |
e9e70b95 | 1472 | [&pblock, &hashTarget, &pwallet, &reservekey, &m_cs, &cancelSolver, &chainparams] |
8e8b6d70 | 1473 | #else |
e9e70b95 | 1474 | [&pblock, &hashTarget, &m_cs, &cancelSolver, &chainparams] |
8e8b6d70 | 1475 | #endif |
e9e70b95 | 1476 | (std::vector<unsigned char> soln) { |
c21c6306 | 1477 | int32_t z; arith_uint256 h; CBlock B; |
51eb5273 JG |
1478 | // Write the solution to the hash and compute the result. |
1479 | LogPrint("pow", "- Checking solution against target\n"); | |
8e165d57 | 1480 | pblock->nSolution = soln; |
e7d59bbc | 1481 | solutionTargetChecks.increment(); |
eff2c3a3 | 1482 | B = *pblock; |
1483 | h = UintToArith256(B.GetHash()); | |
c21c6306 | 1484 | if ( h > hashTarget ) |
51eb5273 | 1485 | return false; |
eff2c3a3 | 1486 | /*for (z=31; z>=16; z--) |
02c30aac | 1487 | fprintf(stderr,"%02x",((uint8_t *)&h)[z]); |
aea2d1aa | 1488 | fprintf(stderr," mined "); |
1489 | for (z=31; z>=16; z--) | |
18443f69 | 1490 | fprintf(stderr,"%02x",((uint8_t *)&HASHTarget)[z]); |
aea2d1aa | 1491 | fprintf(stderr," hashTarget "); |
1492 | for (z=31; z>=16; z--) | |
18443f69 | 1493 | fprintf(stderr,"%02x",((uint8_t *)&HASHTarget_POW)[z]); |
eff2c3a3 | 1494 | fprintf(stderr," POW\n");*/ |
6181f7d5 | 1495 | if ( NOTARY_PUBKEY33[0] != 0 && B.nTime > GetAdjustedTime() ) |
d7d27bb3 | 1496 | { |
97e9d76e | 1497 | fprintf(stderr,"need to wait %d seconds to submit block\n",(int32_t)(B.nTime - GetAdjustedTime())); |
596b05ba | 1498 | while ( GetAdjustedTime() < B.nTime-2 ) |
8e9ef91c | 1499 | { |
eb1ba5a0 | 1500 | sleep(1); |
4cc387ec | 1501 | if ( chainActive.Tip()->nHeight >= Mining_height ) |
1502 | { | |
1503 | fprintf(stderr,"new block arrived\n"); | |
1504 | return(false); | |
1505 | } | |
8e9ef91c | 1506 | } |
eb1ba5a0 | 1507 | } |
8e9ef91c | 1508 | if ( ASSETCHAINS_STAKED == 0 ) |
d7d27bb3 | 1509 | { |
9703f8a0 | 1510 | if ( NOTARY_PUBKEY33[0] != 0 ) |
8e9ef91c | 1511 | { |
26810a26 | 1512 | int32_t r; |
9703f8a0 | 1513 | if ( (r= ((Mining_height + NOTARY_PUBKEY33[16]) % 64) / 8) > 0 ) |
596b05ba | 1514 | MilliSleep((rand() % (r * 1000)) + 1000); |
ef70c5b2 | 1515 | } |
e5430f52 | 1516 | } |
8e9ef91c | 1517 | else |
d7d27bb3 | 1518 | { |
deba7f20 | 1519 | if ( NOTARY_PUBKEY33[0] != 0 ) |
1520 | { | |
ef70c5b2 | 1521 | printf("need to wait %d seconds to submit staked block\n",(int32_t)(B.nTime - GetAdjustedTime())); |
eff2c3a3 | 1522 | while ( GetAdjustedTime() < B.nTime ) |
deba7f20 | 1523 | sleep(1); |
1524 | } | |
68d0354d | 1525 | else |
1526 | { | |
286d95b1 | 1527 | uint256 tmp = B.GetHash(); |
b7aaca4b | 1528 | int32_t z; for (z=31; z>=0; z--) |
1529 | fprintf(stderr,"%02x",((uint8_t *)&tmp)[z]); | |
1530 | fprintf(stderr," mined block!\n"); | |
68d0354d | 1531 | } |
d7d27bb3 | 1532 | } |
8fc79ac9 | 1533 | CValidationState state; |
1534 | if ( !TestBlockValidity(state,B, chainActive.Tip(), true, false)) | |
d2d3c766 | 1535 | { |
8fc79ac9 | 1536 | h = UintToArith256(B.GetHash()); |
1537 | for (z=31; z>=0; z--) | |
1538 | fprintf(stderr,"%02x",((uint8_t *)&h)[z]); | |
1539 | fprintf(stderr," Invalid block mined, try again\n"); | |
1540 | return(false); | |
d2d3c766 | 1541 | } |
b3183e3e | 1542 | KOMODO_CHOSEN_ONE = 1; |
8e165d57 JG |
1543 | // Found a solution |
1544 | SetThreadPriority(THREAD_PRIORITY_NORMAL); | |
2e500f50 | 1545 | LogPrintf("KomodoMiner:\n"); |
eff2c3a3 | 1546 | LogPrintf("proof-of-work found \n hash: %s \ntarget: %s\n", B.GetHash().GetHex(), HASHTarget.GetHex()); |
8e8b6d70 | 1547 | #ifdef ENABLE_WALLET |
eff2c3a3 | 1548 | if (ProcessBlockFound(&B, *pwallet, reservekey)) { |
8e8b6d70 | 1549 | #else |
eff2c3a3 | 1550 | if (ProcessBlockFound(&B)) { |
8e8b6d70 | 1551 | #endif |
e9e70b95 | 1552 | // Ignore chain updates caused by us |
1553 | std::lock_guard<std::mutex> lock{m_cs}; | |
1554 | cancelSolver = false; | |
1555 | } | |
1556 | KOMODO_CHOSEN_ONE = 0; | |
1557 | SetThreadPriority(THREAD_PRIORITY_LOWEST); | |
1558 | // In regression test mode, stop mining after a block is found. | |
1559 | if (chainparams.MineBlocksOnDemand()) { | |
1560 | // Increment here because throwing skips the call below | |
1561 | ehSolverRuns.increment(); | |
1562 | throw boost::thread_interrupted(); | |
1563 | } | |
1564 | //if ( ASSETCHAINS_SYMBOL[0] == 0 && NOTARY_PUBKEY33[0] != 0 ) | |
1565 | // sleep(1800); | |
1566 | return true; | |
1567 | }; | |
1568 | std::function<bool(EhSolverCancelCheck)> cancelled = [&m_cs, &cancelSolver](EhSolverCancelCheck pos) { | |
a6a0d913 | 1569 | std::lock_guard<std::mutex> lock{m_cs}; |
e9e70b95 | 1570 | return cancelSolver; |
1571 | }; | |
1572 | ||
1573 | // TODO: factor this out into a function with the same API for each solver. | |
1574 | if (solver == "tromp" ) { //&& notaryid >= 0 ) { | |
1575 | // Create solver and initialize it. | |
1576 | equi eq(1); | |
1577 | eq.setstate(&curr_state); | |
1578 | ||
1579 | // Initialization done, start algo driver. | |
1580 | eq.digit0(0); | |
c7aaab7a | 1581 | eq.xfull = eq.bfull = eq.hfull = 0; |
e9e70b95 | 1582 | eq.showbsizes(0); |
1583 | for (u32 r = 1; r < WK; r++) { | |
1584 | (r&1) ? eq.digitodd(r, 0) : eq.digiteven(r, 0); | |
1585 | eq.xfull = eq.bfull = eq.hfull = 0; | |
1586 | eq.showbsizes(r); | |
c7aaab7a | 1587 | } |
e9e70b95 | 1588 | eq.digitK(0); |
1589 | ehSolverRuns.increment(); | |
1590 | ||
1591 | // Convert solution indices to byte array (decompress) and pass it to validBlock method. | |
1592 | for (size_t s = 0; s < eq.nsols; s++) { | |
1593 | LogPrint("pow", "Checking solution %d\n", s+1); | |
1594 | std::vector<eh_index> index_vector(PROOFSIZE); | |
1595 | for (size_t i = 0; i < PROOFSIZE; i++) { | |
1596 | index_vector[i] = eq.sols[s][i]; | |
1597 | } | |
1598 | std::vector<unsigned char> sol_char = GetMinimalFromIndices(index_vector, DIGITBITS); | |
1599 | ||
1600 | if (validBlock(sol_char)) { | |
1601 | // If we find a POW solution, do not try other solutions | |
1602 | // because they become invalid as we created a new block in blockchain. | |
1603 | break; | |
1604 | } | |
1605 | } | |
1606 | } else { | |
1607 | try { | |
1608 | // If we find a valid block, we rebuild | |
1609 | bool found = EhOptimisedSolve(n, k, curr_state, validBlock, cancelled); | |
1610 | ehSolverRuns.increment(); | |
1611 | if (found) { | |
997ddd92 | 1612 | int32_t i; uint256 hash = pblock->GetHash(); |
e9e70b95 | 1613 | for (i=0; i<32; i++) |
1614 | fprintf(stderr,"%02x",((uint8_t *)&hash)[i]); | |
1615 | fprintf(stderr," <- %s Block found %d\n",ASSETCHAINS_SYMBOL,Mining_height); | |
1616 | FOUND_BLOCK = 1; | |
1617 | KOMODO_MAYBEMINED = Mining_height; | |
1618 | break; | |
1619 | } | |
1620 | } catch (EhSolverCancelledException&) { | |
1621 | LogPrint("pow", "Equihash solver cancelled\n"); | |
1622 | std::lock_guard<std::mutex> lock{m_cs}; | |
1623 | cancelSolver = false; | |
c7aaab7a DH |
1624 | } |
1625 | } | |
e9e70b95 | 1626 | |
1627 | // Check for stop or if block needs to be rebuilt | |
1628 | boost::this_thread::interruption_point(); | |
1629 | // Regtest mode doesn't require peers | |
1630 | if ( FOUND_BLOCK != 0 ) | |
1631 | { | |
1632 | FOUND_BLOCK = 0; | |
1633 | fprintf(stderr,"FOUND_BLOCK!\n"); | |
1634 | //sleep(2000); | |
1635 | } | |
1636 | if (vNodes.empty() && chainparams.MiningRequiresPeers()) | |
1637 | { | |
1638 | if ( ASSETCHAINS_SYMBOL[0] == 0 || Mining_height > ASSETCHAINS_MINHEIGHT ) | |
1639 | { | |
1640 | fprintf(stderr,"no nodes, break\n"); | |
c7aaab7a | 1641 | break; |
a6df7ab5 | 1642 | } |
c7aaab7a | 1643 | } |
997ddd92 | 1644 | if ((UintToArith256(pblock->nNonce) & 0xffff) == 0xffff) |
10694486 | 1645 | { |
e9e70b95 | 1646 | //if ( 0 && ASSETCHAINS_SYMBOL[0] != 0 ) |
1647 | fprintf(stderr,"0xffff, break\n"); | |
d90cef0b | 1648 | break; |
10694486 | 1649 | } |
e9e70b95 | 1650 | if (mempool.GetTransactionsUpdated() != nTransactionsUpdatedLast && GetTime() - nStart > 60) |
1651 | { | |
1652 | if ( 0 && ASSETCHAINS_SYMBOL[0] != 0 ) | |
1653 | fprintf(stderr,"timeout, break\n"); | |
1654 | break; | |
1655 | } | |
1656 | if ( pindexPrev != chainActive.Tip() ) | |
1657 | { | |
1658 | if ( 0 && ASSETCHAINS_SYMBOL[0] != 0 ) | |
1659 | fprintf(stderr,"Tip advanced, break\n"); | |
1660 | break; | |
1661 | } | |
1662 | // Update nNonce and nTime | |
1663 | pblock->nNonce = ArithToUint256(UintToArith256(pblock->nNonce) + 1); | |
1664 | pblock->nBits = savebits; | |
18dd6a3b | 1665 | /*if ( NOTARY_PUBKEY33[0] == 0 ) |
e9e70b95 | 1666 | { |
f8f740a9 | 1667 | int32_t percPoS; |
23fc88bb | 1668 | UpdateTime(pblock, chainparams.GetConsensus(), pindexPrev); |
1669 | if (chainparams.GetConsensus().fPowAllowMinDifficultyBlocks) | |
1670 | { | |
1671 | // Changing pblock->nTime can change work required on testnet: | |
1672 | HASHTarget.SetCompact(pblock->nBits); | |
18443f69 | 1673 | HASHTarget_POW = komodo_PoWtarget(&percPoS,HASHTarget,Mining_height,ASSETCHAINS_STAKED); |
23fc88bb | 1674 | } |
18dd6a3b | 1675 | }*/ |
48265f3c | 1676 | } |
d247a5d1 JG |
1677 | } |
1678 | } | |
e9e70b95 | 1679 | catch (const boost::thread_interrupted&) |
1680 | { | |
1681 | miningTimer.stop(); | |
1682 | c.disconnect(); | |
1683 | LogPrintf("KomodoMiner terminated\n"); | |
1684 | throw; | |
1685 | } | |
1686 | catch (const std::runtime_error &e) | |
1687 | { | |
1688 | miningTimer.stop(); | |
1689 | c.disconnect(); | |
1690 | LogPrintf("KomodoMiner runtime error: %s\n", e.what()); | |
1691 | return; | |
1692 | } | |
07be8f7e | 1693 | miningTimer.stop(); |
5e9b555f | 1694 | c.disconnect(); |
bba7c249 | 1695 | } |
e9e70b95 | 1696 | |
8e8b6d70 | 1697 | #ifdef ENABLE_WALLET |
e9e70b95 | 1698 | void GenerateBitcoins(bool fGenerate, CWallet* pwallet, int nThreads) |
8e8b6d70 | 1699 | #else |
e9e70b95 | 1700 | void GenerateBitcoins(bool fGenerate, int nThreads) |
8e8b6d70 | 1701 | #endif |
d247a5d1 | 1702 | { |
e9e70b95 | 1703 | static boost::thread_group* minerThreads = NULL; |
1704 | ||
1705 | if (nThreads < 0) | |
1706 | nThreads = GetNumCores(); | |
1707 | ||
1708 | if (minerThreads != NULL) | |
1709 | { | |
1710 | minerThreads->interrupt_all(); | |
1711 | delete minerThreads; | |
1712 | minerThreads = NULL; | |
1713 | } | |
135fa24e | 1714 | |
1f722359 | 1715 | if ((nThreads == 0 && ASSETCHAINS_LWMAPOS == 0) || !fGenerate) |
e9e70b95 | 1716 | return; |
135fa24e | 1717 | |
e9e70b95 | 1718 | minerThreads = new boost::thread_group(); |
135fa24e | 1719 | |
1720 | #ifdef ENABLE_WALLET | |
1f722359 | 1721 | if (ASSETCHAINS_LWMAPOS != 0) |
135fa24e | 1722 | { |
1723 | minerThreads->create_thread(boost::bind(&VerusStaker, pwallet)); | |
1724 | } | |
1725 | #endif | |
1726 | ||
e9e70b95 | 1727 | for (int i = 0; i < nThreads; i++) { |
135fa24e | 1728 | |
8e8b6d70 | 1729 | #ifdef ENABLE_WALLET |
135fa24e | 1730 | if (ASSETCHAINS_ALGO == ASSETCHAINS_EQUIHASH) |
1731 | minerThreads->create_thread(boost::bind(&BitcoinMiner, pwallet)); | |
1732 | else | |
1733 | minerThreads->create_thread(boost::bind(&BitcoinMiner_noeq, pwallet)); | |
8e8b6d70 | 1734 | #else |
135fa24e | 1735 | if (ASSETCHAINS_ALGO == ASSETCHAINS_EQUIHASH) |
1736 | minerThreads->create_thread(&BitcoinMiner); | |
1737 | else | |
1738 | minerThreads->create_thread(&BitcoinMiner_noeq); | |
8e8b6d70 | 1739 | #endif |
e9e70b95 | 1740 | } |
8e8b6d70 | 1741 | } |
e9e70b95 | 1742 | |
2cc0a252 | 1743 | #endif // ENABLE_MINING |