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