]> Git Repo - VerusCoin.git/blame - src/miner.cpp
ID registration and sendcurrency fixes
[VerusCoin.git] / src / miner.cpp
CommitLineData
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
bc909a7a 4// file COPYING or https://www.opensource.org/licenses/mit-license.php .
d247a5d1 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"
bebe7282 12#include "chainparams.h"
ca4a5f26 13#include "cc/StakeGuard.h"
20c3ac51 14#include "importcoin.h"
691161d4 15#include "consensus/consensus.h"
be126699 16#include "consensus/upgrades.h"
da29ecbc 17#include "consensus/validation.h"
8e8b6d70
JG
18#ifdef ENABLE_MINING
19#include "crypto/equihash.h"
42181656 20#include "crypto/verus_hash.h"
8e8b6d70 21#endif
85aab2a0 22#include "hash.h"
93bd00a0 23#include "key_io.h"
d247a5d1 24#include "main.h"
a6df7ab5 25#include "metrics.h"
51ed9ec9 26#include "net.h"
df852d2b 27#include "pow.h"
bebe7282 28#include "primitives/transaction.h"
8e165d57 29#include "random.h"
22c4272b 30#include "timedata.h"
8e8b6d70 31#include "ui_interface.h"
ad49c256
WL
32#include "util.h"
33#include "utilmoneystr.h"
b2993bc5 34#include "validationinterface.h"
09eb201b 35
df756d24
MT
36#include "zcash/Address.hpp"
37#include "transaction_builder.h"
38
fdda3c50
JG
39#include "sodium.h"
40
ad49c256 41#include <boost/thread.hpp>
a3c26c2e 42#include <boost/tuple/tuple.hpp>
8e8b6d70
JG
43#ifdef ENABLE_MINING
44#include <functional>
45#endif
5a360a5c 46#include <mutex>
ad49c256 47
2299bd95
MT
48#include "pbaas/pbaas.h"
49#include "pbaas/notarization.h"
b7c685b8 50#include "pbaas/identity.h"
e7e14f44 51#include "rpc/pbaasrpc.h"
13ed2980 52#include "transaction_builder.h"
2299bd95 53
09eb201b 54using namespace std;
7b4737c8 55
d247a5d1
JG
56//////////////////////////////////////////////////////////////////////////////
57//
58// BitcoinMiner
59//
60
c6cb21d1
GA
61//
62// Unconfirmed transactions in the memory pool often depend on other
63// transactions in the memory pool. When we select transactions from the
64// pool, we select by highest priority or fee rate, so we might consider
65// transactions that depend on transactions that aren't yet in the block.
66// The COrphan class keeps track of these 'temporary orphans' while
67// CreateBlock is figuring out which transactions to include.
68//
d247a5d1
JG
69class COrphan
70{
71public:
4d707d51 72 const CTransaction* ptx;
d247a5d1 73 set<uint256> setDependsOn;
c6cb21d1 74 CFeeRate feeRate;
02bec4b2 75 double dPriority;
e9e70b95 76
c6cb21d1 77 COrphan(const CTransaction* ptxIn) : ptx(ptxIn), feeRate(0), dPriority(0)
d247a5d1 78 {
d247a5d1 79 }
d247a5d1
JG
80};
81
51ed9ec9
BD
82uint64_t nLastBlockTx = 0;
83uint64_t nLastBlockSize = 0;
d247a5d1 84
c6cb21d1
GA
85// We want to sort transactions by priority and fee rate, so:
86typedef boost::tuple<double, CFeeRate, const CTransaction*> TxPriority;
d247a5d1
JG
87class TxPriorityCompare
88{
89 bool byFee;
e9e70b95 90
d247a5d1
JG
91public:
92 TxPriorityCompare(bool _byFee) : byFee(_byFee) { }
e9e70b95 93
d247a5d1
JG
94 bool operator()(const TxPriority& a, const TxPriority& b)
95 {
96 if (byFee)
97 {
98 if (a.get<1>() == b.get<1>())
99 return a.get<0>() < b.get<0>();
100 return a.get<1>() < b.get<1>();
101 }
102 else
103 {
104 if (a.get<0>() == b.get<0>())
105 return a.get<1>() < b.get<1>();
106 return a.get<0>() < b.get<0>();
107 }
108 }
109};
110
bebe7282 111void UpdateTime(CBlockHeader* pblock, const Consensus::Params& consensusParams, const CBlockIndex* pindexPrev)
22c4272b 112{
113 pblock->nTime = std::max(pindexPrev->GetMedianTimePast()+1, GetAdjustedTime());
5ead4b17
JG
114
115 // Updating time can change work required on testnet:
4c902704 116 if (consensusParams.nPowAllowMinDifficultyBlocksAfterHeight != boost::none) {
5ead4b17 117 pblock->nBits = GetNextWorkRequired(pindexPrev, pblock, consensusParams);
b86dc980 118 }
22c4272b 119}
120
5416af1d 121#include "komodo_defs.h"
122
69767347 123extern CCriticalSection cs_metrics;
6e78d3df 124extern int32_t KOMODO_MININGTHREADS,KOMODO_LONGESTCHAIN,ASSETCHAINS_SEED,IS_KOMODO_NOTARY,USE_EXTERNAL_PUBKEY,KOMODO_CHOSEN_ONE,ASSETCHAIN_INIT,KOMODO_INITDONE,KOMODO_ON_DEMAND,KOMODO_INITDONE,KOMODO_PASSPORT_INITDONE;
48d800c2 125extern uint64_t ASSETCHAINS_COMMISSION, ASSETCHAINS_STAKED;
5f63373e 126extern bool VERUS_MINTBLOCKS;
42181656 127extern uint64_t ASSETCHAINS_REWARD[ASSETCHAINS_MAX_ERAS], ASSETCHAINS_TIMELOCKGTE, ASSETCHAINS_NONCEMASK[];
128extern const char *ASSETCHAINS_ALGORITHMS[];
56fe75cb 129extern int32_t VERUS_MIN_STAKEAGE, ASSETCHAINS_EQUIHASH, ASSETCHAINS_VERUSHASH, ASSETCHAINS_LASTERA, ASSETCHAINS_LWMAPOS, ASSETCHAINS_NONCESHIFT[], ASSETCHAINS_HASHESPERROUND[];
130extern uint32_t ASSETCHAINS_ALGO;
7c130297 131extern char ASSETCHAINS_SYMBOL[KOMODO_ASSETCHAIN_MAXLEN];
b2a98c42
MT
132extern uint160 ASSETCHAINS_CHAINID;
133extern uint160 VERUS_CHAINID;
f2d873d0 134extern std::string VERUS_CHAINNAME;
68b309c0 135extern int32_t PBAAS_STARTBLOCK, PBAAS_ENDBLOCK;
7af5cf39 136extern string PBAAS_HOST, PBAAS_USERPASS, ASSETCHAINS_RPCHOST, ASSETCHAINS_RPCCREDENTIALS;;
f8f61a6d 137extern int32_t PBAAS_PORT;
7af5cf39 138extern uint16_t ASSETCHAINS_RPCPORT;
d9f176ac 139extern std::string NOTARY_PUBKEY,ASSETCHAINS_OVERRIDE_PUBKEY;
292809f7 140void vcalc_sha256(char deprecated[(256 >> 3) * 2 + 1],uint8_t hash[256 >> 3],uint8_t *src,int32_t len);
d9f176ac 141
94a465a6 142extern uint8_t NOTARY_PUBKEY33[33],ASSETCHAINS_OVERRIDE_PUBKEY33[33];
31609f35 143uint32_t Mining_start, Mining_height;
28a62b60 144int32_t My_notaryid = -1;
8683bd8d 145int32_t komodo_chosennotary(int32_t *notaryidp,int32_t height,uint8_t *pubkey33,uint32_t timestamp);
b4810651 146int32_t komodo_pax_opreturn(int32_t height,uint8_t *opret,int32_t maxsize);
d63fdb34 147int32_t komodo_baseid(char *origbase);
3bc88f14 148int32_t komodo_validate_interest(const CTransaction &tx,int32_t txheight,uint32_t nTime,int32_t dispflag);
29bd53a1 149int64_t komodo_block_unlocktime(uint32_t nHeight);
18443f69 150uint64_t komodo_commission(const CBlock *block);
d231a6a7 151int32_t komodo_staked(CMutableTransaction &txNew,uint32_t nBits,uint32_t *blocktimep,uint32_t *txtimep,uint256 *utxotxidp,int32_t *utxovoutp,uint64_t *utxovaluep,uint8_t *utxosig);
855714b0 152int32_t verus_staked(CBlock *pBlock, CMutableTransaction &txNew, uint32_t &nBits, arith_uint256 &hashResult, std::vector<unsigned char> &utxosig, CTxDestination &rewardDest);
496f1fd2 153int32_t komodo_notaryvin(CMutableTransaction &txNew,uint8_t *notarypub33);
7652ed92 154
1685bba0
MT
155void IncrementExtraNonce(CBlock* pblock, CBlockIndex* pindexPrev, unsigned int &nExtraNonce, bool buildMerkle, uint32_t *pSaveBits)
156{
157 // Update nExtraNonce
158 static uint256 hashPrevBlock;
159 if (hashPrevBlock != pblock->hashPrevBlock)
160 {
161 nExtraNonce = 0;
162 hashPrevBlock = pblock->hashPrevBlock;
163 }
164 ++nExtraNonce;
165
166 if (pSaveBits)
167 {
168 *pSaveBits = pblock->nBits;
169 }
170
171 int32_t nHeight = pindexPrev->GetHeight() + 1;
172
d3dfc922 173 int solutionVersion = CConstVerusSolutionVector::activationHeight.ActiveVersion(nHeight);
174
175 if (solutionVersion >= CConstVerusSolutionVector::activationHeight.ACTIVATE_PBAAS_HEADER)
1685bba0
MT
176 {
177 // coinbase should already be finalized in the new version
178 if (buildMerkle)
179 {
180 pblock->hashMerkleRoot = pblock->BuildMerkleTree();
af521e42 181 pblock->SetPrevMMRRoot(ChainMerkleMountainView(chainActive.GetMMR(), pindexPrev->GetHeight()).GetRoot());
2d88a342 182 BlockMMRange mmRange(pblock->BuildBlockMMRTree());
56fe75cb 183 BlockMMView mmView(mmRange);
184 pblock->SetBlockMMRRoot(mmView.GetRoot());
185 pblock->AddUpdatePBaaSHeader();
1685bba0
MT
186 }
187
188 UpdateTime(pblock, Params().GetConsensus(), pindexPrev);
189
1685bba0
MT
190 // POS blocks have already had their solution space filled, and there is no actual extra nonce, extradata is used
191 // for POS proof, so don't modify it
d3dfc922 192 if (solutionVersion >= CConstVerusSolutionVector::activationHeight.ACTIVATE_PBAAS && !pblock->IsVerusPOSBlock())
1685bba0 193 {
d3dfc922 194 pblock->AddUpdatePBaaSHeader();
195
1685bba0
MT
196 uint8_t dummy;
197 // clear extra data to allow adding more PBaaS headers
198 pblock->SetExtraData(&dummy, 0);
199
200 // combine blocks and set compact difficulty if necessary
201 uint32_t savebits;
202 if ((savebits = ConnectedChains.CombineBlocks(*pblock)) && pSaveBits)
203 {
204 arith_uint256 ours, merged;
205 ours.SetCompact(pblock->nBits);
206 merged.SetCompact(savebits);
207 if (merged > ours)
208 {
209 *pSaveBits = savebits;
210 }
211 }
212
213 // extra nonce is kept in the header, not in the coinbase any longer
214 // this allows instant spend transactions to use coinbase funds for
215 // inputs by ensuring that once final, the coinbase transaction hash
216 // will not continue to change
217 CDataStream s(SER_NETWORK, PROTOCOL_VERSION);
218 s << nExtraNonce;
219 std::vector<unsigned char> vENonce(s.begin(), s.end());
220
221 assert(pblock->ExtraDataLen() >= vENonce.size());
222 pblock->SetExtraData(vENonce.data(), vENonce.size());
223 }
224 }
225 else
226 {
227 // finalize input of coinbase
228 CMutableTransaction txcb(pblock->vtx[0]);
229 txcb.vin[0].scriptSig = (CScript() << nHeight << CScriptNum(nExtraNonce)) + COINBASE_FLAGS;
230 assert(txcb.vin[0].scriptSig.size() <= 100);
231 pblock->vtx[0] = txcb;
232 if (buildMerkle)
233 {
234 pblock->hashMerkleRoot = pblock->BuildMerkleTree();
235 }
236
237 UpdateTime(pblock, Params().GetConsensus(), pindexPrev);
238 }
239}
240
a4f9bc97 241extern CWallet *pwalletMain;
242
243CPubKey GetSolutionPubKey(const std::vector<std::vector<unsigned char>> &vSolutions, txnouttype txType)
244{
245 CPubKey pk;
246
247 if (txType == TX_PUBKEY)
248 {
249 pk = CPubKey(vSolutions[0]);
250 }
251 else if(txType == TX_PUBKEYHASH)
252 {
253 // we need to have this in our wallet to get the public key
254 LOCK(pwalletMain->cs_wallet);
255 pwalletMain->GetPubKey(CKeyID(uint160(vSolutions[0])), pk);
256 }
257 else if (txType == TX_CRYPTOCONDITION)
258 {
259 if (vSolutions[0].size() == 33)
260 {
261 pk = CPubKey(vSolutions[0]);
262 }
263 else if (vSolutions[0].size() == 34 && vSolutions[0][0] == COptCCParams::ADDRTYPE_PK)
264 {
265 pk = CPubKey(std::vector<unsigned char>(vSolutions[0].begin() + 1, vSolutions[0].end()));
266 }
267 else if (vSolutions[0].size() == 20)
268 {
269 LOCK(pwalletMain->cs_wallet);
270 pwalletMain->GetPubKey(CKeyID(uint160(vSolutions[0])), pk);
271 }
272 else if (vSolutions[0].size() == 21 && vSolutions[0][0] == COptCCParams::ADDRTYPE_ID)
273 {
274 // destination is an identity, see if we can get its first public key
275 std::pair<CIdentityMapKey, CIdentityMapValue> identity;
276
277 if (pwalletMain->GetIdentity(CIdentityID(uint160(std::vector<unsigned char>(vSolutions[0].begin() + 1, vSolutions[0].end()))), identity) &&
278 identity.second.IsValidUnrevoked() &&
279 identity.second.primaryAddresses.size())
280 {
281 CPubKey pkTmp = boost::apply_visitor<GetPubKeyForPubKey>(GetPubKeyForPubKey(), identity.second.primaryAddresses[0]);
282 if (pkTmp.IsValid())
283 {
284 pk = pkTmp;
285 }
286 else
287 {
288 LOCK(pwalletMain->cs_wallet);
289 pwalletMain->GetPubKey(CKeyID(GetDestinationID(identity.second.primaryAddresses[0])), pk);
290 }
291 }
292 }
293 }
294 return pk;
295}
296
297CPubKey GetScriptPublicKey(const CScript &scriptPubKey)
298{
299 txnouttype typeRet;
300 std::vector<std::vector<unsigned char>> vSolutions;
301 if (Solver(scriptPubKey, typeRet, vSolutions))
302 {
303 return GetSolutionPubKey(vSolutions, typeRet);
304 }
305 return CPubKey();
306}
307
c8c684e9 308// call a chain that we consider a notary chain, meaning we call its daemon, not the other way around,
42bbe8d7 309// retrieve new exports that we have not imported, and process them. Also, send any exports that are now
310// provable with the available notarization on the specified chain.
c8c684e9 311void ProcessNewImports(const uint160 &sourceChainID, CPBaaSNotarization &lastConfirmed, CUTXORef &lastConfirmedUTXO, uint32_t nHeight)
a041b57e 312{
2f416b17 313 if (CConstVerusSolutionVector::GetVersionByHeight(nHeight) < CActivationHeight::ACTIVATE_PBAAS ||
314 CConstVerusSolutionVector::activationHeight.IsActivationHeight(CActivationHeight::ACTIVATE_PBAAS, nHeight))
315 {
316 return;
317 }
c8c684e9 318
a041b57e 319 uint32_t consensusBranchId = CurrentEpochBranchId(nHeight, Params().GetConsensus());
320
321 // get any pending imports from the source chain. if the source chain is this chain, we don't need notarization
322 CCurrencyDefinition thisChain = ConnectedChains.ThisChain();
918dc9e0 323 uint160 thisChainID = thisChain.GetID();
c8c684e9 324 CCurrencyDefinition sourceChain = ConnectedChains.GetCachedCurrency(sourceChainID);
325 if (!sourceChain.IsValid())
326 {
327 printf("Unrecognized source chain %s\n", EncodeDestination(CIdentityID(sourceChainID)).c_str());
328 return;
329 }
330
e57a7325 331 // printf("%s: processing imports for %s\n", __func__, sourceChain.name.c_str());
cdff02e5 332
c8c684e9 333 bool isSameChain = thisChain.GetID() == sourceChainID;
334
335 CChainNotarizationData cnd;
c3292082 336 if (!GetNotarizationData(sourceChainID, cnd) && cnd.IsConfirmed())
c8c684e9 337 {
338 printf("Cannot get notarization data for currency %s\n", sourceChain.name.c_str());
339 return;
340 }
341
342 lastConfirmedUTXO = cnd.vtx[cnd.lastConfirmed].first;
343 lastConfirmed = cnd.vtx[cnd.lastConfirmed].second;
a041b57e 344
345 CTransaction lastImportTx;
346
347 // we need to find the last unspent import transaction
348 std::vector<CAddressUnspentDbEntry> unspentOutputs;
349
350 bool found = false;
c8c684e9 351 CAddressUnspentDbEntry foundEntry;
352 CCrossChainImport lastCCI;
353 std::vector<std::pair<std::pair<CInputDescriptor, CPartialTransactionProof>, std::vector<CReserveTransfer>>> exports;
a041b57e 354
a041b57e 355 {
c8c684e9 356 LOCK(cs_main);
357
358 if (!isSameChain &&
5d75d90d 359 lastConfirmed.proofRoots.count(sourceChainID) &&
360 GetAddressUnspent(CKeyID(CCrossChainRPCData::GetConditionID(sourceChainID, CCrossChainImport::CurrencySystemImportKey())), CScript::P2IDX, unspentOutputs))
a041b57e 361 {
c8c684e9 362 // if one spends the prior one, get the one that is not spent
363 for (auto &txidx : unspentOutputs)
364 {
365 COptCCParams p;
366 if (txidx.second.script.IsPayToCryptoCondition(p) &&
367 p.IsValid() &&
368 p.evalCode == EVAL_CROSSCHAIN_IMPORT &&
369 p.vData.size() &&
370 (lastCCI = CCrossChainImport(p.vData[0])).IsValid())
371 {
372 found = true;
373 foundEntry = txidx;
374 break;
375 }
376 }
377
378 if (found &&
c8c684e9 379 lastCCI.sourceSystemHeight < lastConfirmed.notarizationHeight)
a041b57e 380 {
c8c684e9 381 UniValue params(UniValue::VARR);
382
918dc9e0 383 params.push_back(EncodeDestination(CIdentityID(thisChainID)));
c8c684e9 384 params.push_back((int64_t)lastCCI.sourceSystemHeight);
4c26b35c 385 params.push_back((int64_t)lastConfirmed.proofRoots[sourceChainID].rootHeight);
c8c684e9 386
387 UniValue result = NullUniValue;
388 try
389 {
390 if (sourceChainID == thisChain.GetID())
391 {
392 UniValue getexports(const UniValue& params, bool fHelp);
393 result = getexports(params, false);
394 }
395 else
396 {
397 result = find_value(RPCCallRoot("getexports", params), "result");
398 }
399 } catch (exception e)
400 {
401 printf("Could not get latest export from external chain %s\n", uni_get_str(params[0]).c_str());
402 return;
403 }
404
405 // now, we should have a list of exports to import in order
406 if (!result.isArray() || !result.size())
407 {
408 return;
409 }
179b6f82 410 bool foundCurrent = false;
c8c684e9 411 for (int i = 0; i < result.size(); i++)
412 {
179b6f82 413 uint256 exportTxId = uint256S(uni_get_str(find_value(result[i], "txid")));
414 if (!foundCurrent && !lastCCI.exportTxId.IsNull())
415 {
416 // when we find our export, take the next
417 if (exportTxId == lastCCI.exportTxId)
418 {
419 foundCurrent = true;
420 }
421 continue;
422 }
423
c8c684e9 424 // create one import at a time
425 uint32_t notarizationHeight = uni_get_int64(find_value(result[i], "height"));
c8c684e9 426 int32_t exportTxOutNum = uni_get_int(find_value(result[i], "txoutnum"));
427 CPartialTransactionProof txProof = CPartialTransactionProof(find_value(result[i], "partialtransactionproof"));
428 UniValue transferArrUni = find_value(result[i], "transfers");
429 if (!notarizationHeight ||
430 exportTxId.IsNull() ||
431 exportTxOutNum == -1 ||
432 !transferArrUni.isArray())
433 {
434 printf("Invalid export from %s\n", uni_get_str(params[0]).c_str());
435 return;
436 }
437
438 CTransaction exportTx;
439 uint256 blkHash;
440 auto proofRootIt = lastConfirmed.proofRoots.find(sourceChainID);
441 if (!isSameChain &&
442 !(txProof.IsValid() &&
51a9c83c 443 !txProof.GetPartialTransaction(exportTx).IsNull() &&
444 txProof.TransactionHash() == exportTxId &&
445 proofRootIt != lastConfirmed.proofRoots.end() &&
446 proofRootIt->second.stateRoot == txProof.CheckPartialTransaction(exportTx) &&
447 exportTx.vout.size() > exportTxOutNum))
c8c684e9 448 {
e7c64795 449 /* printf("%s: proofRoot: %s, checkPartialRoot: %s, proofheight: %u, ischainproof: %s, blockhash: %s\n",
4c26b35c 450 __func__,
451 proofRootIt->second.ToUniValue().write(1,2).c_str(),
452 txProof.CheckPartialTransaction(exportTx).GetHex().c_str(),
5d75d90d 453 txProof.GetProofHeight(),
4c26b35c 454 txProof.IsChainProof() ? "true" : "false",
e7c64795 455 txProof.GetBlockHash().GetHex().c_str()); */
5d75d90d 456 printf("Invalid export for %s\n", uni_get_str(params[0]).c_str());
c8c684e9 457 return;
458 }
459 else if (isSameChain &&
460 !(myGetTransaction(exportTxId, exportTx, blkHash) &&
461 exportTx.vout.size() > exportTxOutNum))
462 {
463 printf("Invalid export msg2 from %s\n", uni_get_str(params[0]).c_str());
464 return;
465 }
179b6f82 466 if (!foundCurrent)
467 {
468 CCrossChainExport ccx(exportTx.vout[exportTxOutNum].scriptPubKey);
469 if (!ccx.IsValid())
470 {
471 printf("Invalid export msg3 from %s\n", uni_get_str(params[0]).c_str());
472 return;
473 }
474 if (ccx.IsChainDefinition())
475 {
476 continue;
477 }
478 }
c8c684e9 479 std::pair<std::pair<CInputDescriptor, CPartialTransactionProof>, std::vector<CReserveTransfer>> oneExport =
480 std::make_pair(std::make_pair(CInputDescriptor(exportTx.vout[exportTxOutNum].scriptPubKey,
481 exportTx.vout[exportTxOutNum].nValue,
482 CTxIn(exportTxId, exportTxOutNum)),
483 txProof),
484 std::vector<CReserveTransfer>());
485 for (int j = 0; j < transferArrUni.size(); j++)
486 {
5d75d90d 487 //printf("%s: onetransfer: %s\n", __func__, transferArrUni[j].write(1,2).c_str());
488 oneExport.second.push_back(CReserveTransfer(transferArrUni[j]));
c8c684e9 489 if (!oneExport.second.back().IsValid())
490 {
5d75d90d 491 printf("Invalid reserve transfers in export from %s\n", sourceChain.name.c_str());
c8c684e9 492 return;
493 }
494 }
cdff02e5 495 exports.push_back(oneExport);
c8c684e9 496 }
a041b57e 497 }
c8c684e9 498 std::map<uint160, std::vector<std::pair<int, CTransaction>>> newImports;
499 ConnectedChains.CreateLatestImports(sourceChain, lastConfirmedUTXO, exports, newImports);
500 }
501 else if (isSameChain)
502 {
503 ConnectedChains.ProcessLocalImports();
504 return;
505 }
506 else
507 {
508 printf("Could not get prior import for currency %s\n", sourceChain.name.c_str());
509 return;
a041b57e 510 }
511 }
c8c684e9 512}
a041b57e 513
c8c684e9 514bool CheckNotaryConnection(const CRPCChainData &notarySystem)
515{
516 // ensure we have connection parameters, or we fail
517 if (notarySystem.rpcHost == "" || notarySystem.rpcUserPass == "" || !notarySystem.rpcPort)
518 {
519 return false;
520 }
521 return true;
522}
523
524bool CallNotary(const CRPCChainData &notarySystem, std::string command, const UniValue &params, UniValue &result, UniValue &error)
525{
526 // ensure we have connection parameters, or we fail
527 if (!CheckNotaryConnection(notarySystem))
528 {
529 return false;
530 }
531
532 try
533 {
534 UniValue rpcResult = RPCCall(command, params, notarySystem.rpcUserPass, notarySystem.rpcPort, notarySystem.rpcHost);
535 result = find_value(rpcResult, "result");
536 error = find_value(rpcResult, "error");
537 } catch (std::exception e)
a041b57e 538 {
c8c684e9 539 error = strprintf("Failed to connect to %s chain, error: %s\n", notarySystem.chainDefinition.name.c_str(), e.what());
540 }
541 return error.isNull();
542}
543
544// get initial currency state from the notary system specified
203d1021 545bool GetBlockOneLaunchNotarization(const CRPCChainData &notarySystem,
546 const uint160 &currencyID,
547 CCurrencyDefinition &curDef,
548 CPBaaSNotarization &launchNotarization,
e7c64795 549 CPBaaSNotarization &notaryNotarization,
0b1a0e7f 550 std::pair<CUTXORef, CPartialTransactionProof> &notarizationOutputProof,
a36e0d92 551 std::pair<CUTXORef, CPartialTransactionProof> &exportOutputProof,
552 std::vector<CReserveTransfer> &exportTransfers)
c8c684e9 553{
554 UniValue result, error;
203d1021 555 bool retVal = false;
a041b57e 556
c8c684e9 557 UniValue params(UniValue::VARR);
558 params.push_back(EncodeDestination(CIdentityID(currencyID)));
559
560 // VRSC and VRSCTEST do not start with a notary chain
40dc0bda 561 if (!IsVerusActive() && ConnectedChains.IsNotaryAvailable())
c8c684e9 562 {
563 // we are starting a PBaaS chain. We only assume that our chain definition and the first notary chain, if there is one, are setup
564 // in ConnectedChains. All other currencies and identities necessary to start have not been populated and must be in block 1 by
565 // getting the information from the notary chain.
203d1021 566 if (CallNotary(notarySystem, "getlaunchinfo", params, result, error))
a041b57e 567 {
203d1021 568 CCurrencyDefinition currency(find_value(result, "currencydefinition"));
e7c64795 569 CPBaaSNotarization notarization(find_value(result, "launchnotarization"));
570 notaryNotarization = CPBaaSNotarization(find_value(result, "notarynotarization"));
0b1a0e7f 571 CPartialTransactionProof notarizationProof(find_value(result, "notarizationproof"));
572 CUTXORef notarizationUtxo(uint256S(uni_get_str(find_value(result, "notarizationtxid"))), uni_get_int(find_value(result, "notarizationvoutnum")));
573
574 CPartialTransactionProof exportProof(find_value(result, "exportproof"));
a36e0d92 575 UniValue exportTransfersUni = find_value(result, "exportransfers");
576
577 bool reject = false;
578 if (exportTransfersUni.isArray() && exportTransfersUni.size())
579 {
580 for (int i = 0; i < exportTransfersUni.size(); i++)
581 {
582 CReserveTransfer oneTransfer(exportTransfersUni[i]);
583 if (!oneTransfer.IsValid())
584 {
585 reject = true;
586 }
587 else
588 {
589 exportTransfers.push_back(oneTransfer);
590 }
591 }
592 }
0b1a0e7f 593 CUTXORef exportUtxo(uint256S(uni_get_str(find_value(result, "exporttxid"))), uni_get_int(find_value(result, "exportvoutnum")));
594
a36e0d92 595 if (reject ||
596 !currency.IsValid() ||
203d1021 597 !notarization.IsValid() ||
5d75d90d 598 !notarizationProof.IsValid() ||
e7c64795 599 !notaryNotarization.IsValid())
c8c684e9 600 {
203d1021 601 LogPrintf("%s: invalid launch notarization for currency %s\n", __func__, EncodeDestination(CIdentityID(currencyID)).c_str());
b4ba7e7c 602 printf("%s: invalid launch notarization for currency %s\ncurrencydefinition: %s\nnotarization: %s\ntransactionproof: %s\n",
603 __func__,
604 EncodeDestination(CIdentityID(currencyID)).c_str(),
605 currency.ToUniValue().write(1,2).c_str(),
606 notarization.ToUniValue().write(1,2).c_str(),
0b1a0e7f 607 notarizationProof.ToUniValue().write(1,2).c_str());
203d1021 608 }
609 else
610 {
5d75d90d 611 //printf("%s: proofroot: %s\n", __func__, latestProofRoot.ToUniValue().write(1,2).c_str());
203d1021 612 curDef = currency;
613 launchNotarization = notarization;
e7c64795 614 launchNotarization.proofRoots = notaryNotarization.proofRoots;
615 notaryNotarization.proofRoots[ASSETCHAINS_CHAINID] = CProofRoot::GetProofRoot(0);
616 notaryNotarization.currencyStates[ASSETCHAINS_CHAINID] = launchNotarization.currencyState;
0b1a0e7f 617 notarizationOutputProof = std::make_pair(notarizationUtxo, notarizationProof);
618 exportOutputProof = std::make_pair(exportUtxo, exportProof);
203d1021 619 retVal = true;
a041b57e 620 }
621 }
203d1021 622 else
623 {
624 LogPrintf("%s: error calling notary chain %s\n", __func__, error.write(1,2).c_str());
625 printf("%s: error calling notary chain %s\n", __func__, error.write(1,2).c_str());
626 }
c8c684e9 627 }
203d1021 628 return retVal;
c8c684e9 629}
630
6f40dc90 631bool DecodeOneExport(const UniValue obj, CCrossChainExport &ccx,
632 std::pair<std::pair<CInputDescriptor, CPartialTransactionProof>, std::vector<CReserveTransfer>> &oneExport)
633{
634 uint32_t exportHeight = uni_get_int64(find_value(obj, "height"));
635 uint256 txId = uint256S(uni_get_str(find_value(obj, "txid")));
636 uint32_t outNum = uni_get_int(find_value(obj, "txoutnum"));
637 ccx = CCrossChainExport(find_value(obj, "exportinfo"));
638 if (!ccx.IsValid())
639 {
640 LogPrintf("%s: invalid launch export from notary chain\n", __func__);
641 printf("%s: invalid launch export from notary chain\n", __func__);
642 return false;
643 }
644 CPartialTransactionProof partialTxProof(find_value(obj, "partialtransactionproof"));
645 CTransaction exportTx;
646 COptCCParams p;
647 CScript outputScript;
648 CAmount outputValue;
649
0b1a0e7f 650 // TODO: HARDENING - check the proof against the actual notarization here
6f40dc90 651 if (!partialTxProof.IsValid() ||
51a9c83c 652 partialTxProof.GetPartialTransaction(exportTx).IsNull() ||
653 partialTxProof.TransactionHash() != txId ||
6f40dc90 654 exportTx.vout.size() <= outNum ||
655 !exportTx.vout[outNum].scriptPubKey.IsPayToCryptoCondition(p) ||
656 !p.IsValid() ||
657 !p.evalCode == EVAL_CROSSCHAIN_EXPORT ||
658 (outputValue = exportTx.vout[outNum].nValue) == -1)
659 {
0b1a0e7f 660 //UniValue jsonTxOut(UniValue::VOBJ);
661 //TxToUniv(exportTx, uint256(), jsonTxOut);
51a9c83c 662 //printf("%s: proofTxRoot:%s\npartialTx: %s\n", __func__,
663 // partialTxProof.GetPartialTransaction(exportTx).GetHex().c_str(),
664 // jsonTxOut.write(1,2).c_str());
6f40dc90 665 LogPrintf("%s: invalid partial transaction proof from notary chain\n", __func__);
666 printf("%s: invalid partial transaction proof from notary chain\n", __func__);
667 return false;
668 }
669
670 UniValue transfers = find_value(obj, "transfers");
671 std::vector<CReserveTransfer> reserveTransfers;
672 if (transfers.isArray() && transfers.size())
673 {
674 for (int i = 0; i < transfers.size(); i++)
675 {
676 CReserveTransfer rt(transfers[i]);
677 if (rt.IsValid())
678 {
679 reserveTransfers.push_back(rt);
680 }
681 }
682 }
683
684 oneExport.first.first = CInputDescriptor(outputScript, outputValue, CTxIn(txId, outNum));
685 oneExport.first.second = partialTxProof;
686 oneExport.second = reserveTransfers;
687 return true;
688}
689
690// get initial currency state from the notary system specified
5d75d90d 691bool GetBlockOneImports(const CRPCChainData &notarySystem, const CPBaaSNotarization &launchNotarization, std::map<uint160, std::vector<std::pair<std::pair<CInputDescriptor, CPartialTransactionProof>, std::vector<CReserveTransfer>>>> &exports)
6f40dc90 692{
693 UniValue result, error;
694
695 UniValue params(UniValue::VARR);
696 params.push_back(EncodeDestination(CIdentityID(ASSETCHAINS_CHAINID)));
5d75d90d 697 params.push_back((int)0);
698 if (launchNotarization.proofRoots.count(ConnectedChains.ThisChain().launchSystemID))
699 {
700 params.push_back((int64_t)launchNotarization.proofRoots.find(ConnectedChains.ThisChain().launchSystemID)->second.rootHeight);
701 }
6f40dc90 702
703 // VRSC and VRSCTEST do not start with a notary chain
704 if (!IsVerusActive() && ConnectedChains.IsNotaryAvailable())
705 {
706 // we are starting a PBaaS chain. We only assume that our chain definition and the first notary chain, if there is one, are setup
707 // in ConnectedChains. All other currencies and identities necessary to start have not been populated and must be in block 1 by
708 // getting the information from the notary chain.
709 if (CallNotary(notarySystem, "getexports", params, result, error) &&
710 result.isArray() &&
711 result.size())
712 {
713 // we now have an array of exports that we should import into this system
714 // load up to the last launch export on each currency
715 for (int i = 0; i < result.size(); i++)
716 {
717 CCrossChainExport ccx;
718 std::pair<std::pair<CInputDescriptor, CPartialTransactionProof>, std::vector<CReserveTransfer>> oneExport;
237ab583 719 if (DecodeOneExport(result[i], ccx, oneExport))
6f40dc90 720 {
721 exports[ccx.destCurrencyID].push_back(oneExport);
722 }
723 else
724 {
725 return false;
726 }
727 }
728 return true;
729 }
730 return false;
731 }
732 return false;
733}
734
c8c684e9 735// This is called with either the initial currency, or the gateway converter currency
736// to setup an import/export thread, transfer the initial issuance of native currency
737// into the converter, and notarize the state of each currency.
738// All outputs to do those things are added to the outputs vector.
37434dab 739bool AddOneCurrencyImport(const CCurrencyDefinition &newCurrency,
6f40dc90 740 const CPBaaSNotarization &lastNotarization,
203d1021 741 const std::pair<CUTXORef, CPartialTransactionProof> *pLaunchProof,
0b1a0e7f 742 const std::pair<CUTXORef, CPartialTransactionProof> *pFirstExport,
a36e0d92 743 const std::vector<CReserveTransfer> &_exportTransfers,
e6c5e0b8 744 CCurrencyValueMap &gatewayDeposits,
24d12992 745 std::vector<CTxOut> &outputs,
746 CCurrencyValueMap &additionalFees)
c8c684e9 747{
748 uint160 newCurID = newCurrency.GetID();
37434dab 749 CPBaaSNotarization newNotarization = lastNotarization;
750 newNotarization.prevNotarization = CUTXORef();
923bb2c0 751 newNotarization.SetBlockOneNotarization();
a041b57e 752
c8c684e9 753 // each currency will get:
754 // * one currency definition output
755 // * notarization of latest currency state
756
757 CCcontract_info CC;
758 CCcontract_info *cp;
759
37434dab 760 // make a currency definition
c8c684e9 761 cp = CCinit(&CC, EVAL_CURRENCY_DEFINITION);
762 std::vector<CTxDestination> dests({CPubKey(ParseHex(CC.CChexstr))});
763 outputs.push_back(CTxOut(0, MakeMofNCCScript(CConditionObj<CCurrencyDefinition>(EVAL_CURRENCY_DEFINITION, dests, 1, &newCurrency))));
764
765 // import / export capable currencies include the main currency, fractional currencies on any system,
c3292082 766 // gateway currencies. the launch system, and non-token currencies. they also get an import / export thread
767 if (ConnectedChains.ThisChain().launchSystemID == newCurID ||
768 (newCurrency.systemID == ASSETCHAINS_CHAINID &&
a9c8cca3 769 (newCurrency.IsFractional() ||
e7c64795 770 newCurrency.systemID == newCurID ||
c3292082 771 (newCurrency.IsGateway() && newCurrency.GetID() == newCurrency.gatewayID))))
c8c684e9 772 {
37434dab 773 uint160 firstNotaryID = ConnectedChains.FirstNotaryChain().chainDefinition.GetID();
774
203d1021 775 // first, put evidence of the notarization pre-import
0b1a0e7f 776 int notarizationIdx = -1;
203d1021 777 if (pLaunchProof)
778 {
779 // add notarization before other outputs
780 cp = CCinit(&CC, EVAL_NOTARY_EVIDENCE);
781 dests = std::vector<CTxDestination>({CPubKey(ParseHex(CC.CChexstr))});
782 // now, we need to put the launch notarization evidence, followed by the import outputs
783 CNotaryEvidence evidence = CNotaryEvidence(ConnectedChains.FirstNotaryChain().chainDefinition.GetID(),
784 pLaunchProof->first,
785 true,
786 std::map<CIdentityID, CIdentitySignature>(),
787 std::vector<CPartialTransactionProof>({pLaunchProof->second}),
788 CNotaryEvidence::TYPE_PARTIAL_TXPROOF);
0b1a0e7f 789 notarizationIdx = outputs.size();
203d1021 790 outputs.push_back(CTxOut(0, MakeMofNCCScript(CConditionObj<CNotaryEvidence>(EVAL_NOTARY_EVIDENCE, dests, 1, &evidence))));
791 }
792
a36e0d92 793 // create the import thread output
794 cp = CCinit(&CC, EVAL_CROSSCHAIN_IMPORT);
795 if (newCurrency.proofProtocol == newCurrency.PROOF_CHAINID)
796 {
797 dests = std::vector<CTxDestination>({CIdentityID(newCurID)});
798 }
799 else
800 {
801 dests = std::vector<CTxDestination>({CPubKey(ParseHex(CC.CChexstr))});
802 }
803
c3292082 804 if ((newCurrency.systemID == ASSETCHAINS_CHAINID) && firstNotaryID == newCurrency.launchSystemID)
37434dab 805 {
37434dab 806 uint256 transferHash;
807 std::vector<CTxOut> importOutputs;
808 CCurrencyValueMap importedCurrency, gatewayDepositsUsed, spentCurrencyOut;
809
40dc0bda 810 CPBaaSNotarization tempLastNotarization = lastNotarization;
db616a05 811 tempLastNotarization.currencyState.SetLaunchCompleteMarker(false);
40dc0bda 812
a36e0d92 813 std::vector<CReserveTransfer> exportTransfers(_exportTransfers);
40dc0bda 814 if (!tempLastNotarization.NextNotarizationInfo(ConnectedChains.FirstNotaryChain().chainDefinition,
815 newCurrency,
816 0,
db616a05 817 1,
40dc0bda 818 exportTransfers,
819 transferHash,
820 newNotarization,
821 importOutputs,
822 importedCurrency,
823 gatewayDepositsUsed,
824 spentCurrencyOut))
37434dab 825 {
826 LogPrintf("%s: invalid import for currency %s on system %s\n", __func__,
827 newCurrency.name.c_str(),
828 EncodeDestination(CIdentityID(ASSETCHAINS_CHAINID)).c_str());
829 return false;
830 }
40dc0bda 831
a36e0d92 832 // if fees are not converted, we will pay out original fees,
833 // less liquidity fees, which go into the currency reserves
834 bool feesConverted;
835 CCurrencyValueMap liquidityFees;
836 CCurrencyValueMap originalFees =
837 newNotarization.currencyState.CalculateConvertedFees(
838 newNotarization.currencyState.viaConversionPrice,
839 newNotarization.currencyState.viaConversionPrice,
840 ASSETCHAINS_CHAINID,
841 feesConverted,
842 liquidityFees,
843 additionalFees);
844
845 if (!feesConverted)
24d12992 846 {
a36e0d92 847 additionalFees += (originalFees - liquidityFees);
24d12992 848 }
849
923bb2c0 850 newNotarization.SetBlockOneNotarization();
851
b4ba7e7c 852 // display import outputs
a36e0d92 853 /*
a3c9294b 854 CMutableTransaction debugTxOut;
24d12992 855 debugTxOut.vout = outputs;
856 debugTxOut.vout.insert(debugTxOut.vout.end(), importOutputs.begin(), importOutputs.end());
40dc0bda 857 UniValue jsonTxOut(UniValue::VOBJ);
858 TxToUniv(debugTxOut, uint256(), jsonTxOut);
859 printf("%s: launch outputs: %s\nlast notarization: %s\nnew notarization: %s\n", __func__,
860 jsonTxOut.write(1,2).c_str(),
861 lastNotarization.ToUniValue().write(1,2).c_str(),
862 newNotarization.ToUniValue().write(1,2).c_str());
a36e0d92 863 */
a3c9294b 864
37434dab 865 newNotarization.prevNotarization = CUTXORef();
866 newNotarization.prevHeight = 0;
c8c684e9 867
6f40dc90 868 // get the first export for launch from the notary chain
0b1a0e7f 869 CTransaction firstExportTx;
dc51c829 870 if (!pFirstExport || !(pFirstExport->second.IsValid() && !pFirstExport->second.GetPartialTransaction(firstExportTx).IsNull()))
6f40dc90 871 {
0b1a0e7f 872 LogPrintf("%s: invalid first export for PBaaS or converter launch\n");
6f40dc90 873 return false;
874 }
875
876 // get the export for this import
0b1a0e7f 877 CCrossChainExport ccx(firstExportTx.vout[pFirstExport->first.n].scriptPubKey);
878 if (!ccx.IsValid())
879 {
880 LogPrintf("%s: invalid export output for PBaaS or converter launch\n");
881 return false;
882 }
6f40dc90 883
faffe06c 884 // create an import based on launch conditions that covers all pre-allocations and uses the initial notarization.
6f40dc90 885 // generate outputs, then fill in numOutputs
c8c684e9 886 CCrossChainImport cci = CCrossChainImport(newCurrency.launchSystemID,
37434dab 887 newNotarization.notarizationHeight,
c8c684e9 888 newCurID,
6f40dc90 889 ccx.totalAmounts,
890 CCurrencyValueMap(),
891 0,
892 ccx.hashReserveTransfers,
dc51c829 893 pFirstExport->first.hash,
0b1a0e7f 894 pFirstExport->first.n);
6f40dc90 895 cci.SetSameChain(newCurrency.launchSystemID == ASSETCHAINS_CHAINID);
cdff02e5 896 cci.SetPostLaunch();
40dc0bda 897 cci.SetInitialLaunchImport();
c8c684e9 898
e6c5e0b8 899 // anything we had before plus anything imported and minus all spent currency out should
900 // be all reserve deposits remaining under control of this currency
901
a36e0d92 902 /* printf("%s: ccx.totalAmounts: %s\ngatewayDepositsUsed: %s\nadditionalFees: %s\n",
903 __func__,
904 ccx.totalAmounts.ToUniValue().write(1,2).c_str(),
905 gatewayDepositsUsed.ToUniValue().write(1,2).c_str(),
906 additionalFees.ToUniValue().write(1,2).c_str()); */
e6c5e0b8 907
908 // to determine left over reserves for deposit, consider imported and emitted as the same
a36e0d92 909 if (newCurrency.IsFractional())
910 {
911 gatewayDeposits = CCurrencyValueMap(lastNotarization.currencyState.currencies, lastNotarization.currencyState.reserveIn);
912 }
913 else
e6c5e0b8 914 {
a36e0d92 915 gatewayDeposits = importedCurrency;
e6c5e0b8 916 }
a36e0d92 917 gatewayDeposits.valueMap[newCurID] += newNotarization.currencyState.primaryCurrencyOut;
918
919 /* printf("importedcurrency %s\nspentcurrencyout %s\nnewgatewaydeposits %s\n",
920 importedCurrency.ToUniValue().write(1,2).c_str(),
921 spentCurrencyOut.ToUniValue().write(1,2).c_str(),
922 gatewayDeposits.ToUniValue().write(1,2).c_str()); */
923
924 gatewayDeposits = (gatewayDeposits - spentCurrencyOut).CanonicalMap();
e6c5e0b8 925
a36e0d92 926 /* printf("importedcurrency %s\nspentcurrencyout %s\nnewgatewaydeposits %s\n",
e6c5e0b8 927 importedCurrency.ToUniValue().write(1,2).c_str(),
928 spentCurrencyOut.ToUniValue().write(1,2).c_str(),
a36e0d92 929 gatewayDeposits.ToUniValue().write(1,2).c_str()); */
e6c5e0b8 930
c31944d3 931 // add the reserve deposit output with all deposits for this currency for the new chain
932 if (gatewayDeposits.valueMap.size())
933 {
934 CCcontract_info *depositCp;
935 CCcontract_info depositCC;
936
937 // create the import thread output
938 depositCp = CCinit(&depositCC, EVAL_RESERVE_DEPOSIT);
faffe06c 939 std::vector<CTxDestination> depositDests({CPubKey(ParseHex(depositCC.CChexstr))});
c31944d3 940 CReserveDeposit rd(newCurID, gatewayDeposits);
941 CAmount nativeOut = gatewayDeposits.valueMap.count(ASSETCHAINS_CHAINID) ? gatewayDeposits.valueMap[ASSETCHAINS_CHAINID] : 0;
942 outputs.push_back(CTxOut(nativeOut, MakeMofNCCScript(CConditionObj<CReserveDeposit>(EVAL_RESERVE_DEPOSIT, depositDests, 1, &rd))));
943 }
944
d5af62fe 945 if (newCurrency.notaries.size())
946 {
947 // notaries all get an even share of 10% of the launch fee in the launch currency to use for notarizing
948 // they may also get pre-allocations
949 uint160 notaryNativeID = ConnectedChains.FirstNotaryChain().chainDefinition.GetID();
950 CAmount notaryFeeShare = ConnectedChains.FirstNotaryChain().chainDefinition.currencyRegistrationFee / 10;
951 additionalFees -= CCurrencyValueMap(std::vector<uint160>({notaryNativeID}), std::vector<int64_t>({notaryFeeShare}));
952 CAmount oneNotaryShare = notaryFeeShare / newCurrency.notaries.size();
953 CAmount notaryModExtra = notaryFeeShare % newCurrency.notaries.size();
954 for (auto &oneNotary : newCurrency.notaries)
955 {
956 CTokenOutput to(notaryNativeID, oneNotaryShare);
957 if (notaryModExtra)
958 {
959 to.reserveValues.valueMap[notaryNativeID]++;
960 notaryModExtra--;
961 }
962 outputs.push_back(CTxOut(0, MakeMofNCCScript(CConditionObj<CTokenOutput>(EVAL_RESERVE_OUTPUT,
963 std::vector<CTxDestination>({CIdentityID(oneNotary)}),
964 1,
965 &to))));
966 }
967 }
968
6f40dc90 969 cci.numOutputs = importOutputs.size();
970
971 // now add the import itself
972 outputs.push_back(CTxOut(0, MakeMofNCCScript(CConditionObj<CCrossChainImport>(EVAL_CROSSCHAIN_IMPORT, dests, 1, &cci))));
973
974 // add notarization before other outputs
975 cp = CCinit(&CC, EVAL_EARNEDNOTARIZATION);
976 if (newCurID == ASSETCHAINS_CHAINID &&
977 newCurrency.notarizationProtocol == newCurrency.NOTARIZATION_NOTARY_CHAINID)
978 {
979 dests = std::vector<CTxDestination>({CIdentityID(newCurID)});
980 }
981 else
982 {
983 dests = std::vector<CTxDestination>({CPubKey(ParseHex(CC.CChexstr))});
984 }
985 outputs.push_back(CTxOut(0, MakeMofNCCScript(CConditionObj<CPBaaSNotarization>(EVAL_EARNEDNOTARIZATION, dests, 1, &newNotarization))));
986
0b1a0e7f 987 // add export before other outputs
6f40dc90 988 cp = CCinit(&CC, EVAL_NOTARY_EVIDENCE);
989 dests = std::vector<CTxDestination>({CPubKey(ParseHex(CC.CChexstr))});
990 // now, we need to put the export evidence, followed by the import outputs
991 CNotaryEvidence evidence = CNotaryEvidence(cci.sourceSystemID,
0b1a0e7f 992 CUTXORef(uint256(), notarizationIdx),
6f40dc90 993 true,
994 std::map<CIdentityID, CIdentitySignature>(),
0b1a0e7f 995 std::vector<CPartialTransactionProof>({pFirstExport->second}),
6f40dc90 996 CNotaryEvidence::TYPE_PARTIAL_TXPROOF);
997 outputs.push_back(CTxOut(0, MakeMofNCCScript(CConditionObj<CNotaryEvidence>(EVAL_NOTARY_EVIDENCE, dests, 1, &evidence))));
998
999 outputs.insert(outputs.end(), importOutputs.begin(), importOutputs.end());
c8c684e9 1000 }
1001 else
1002 {
1003 // begin with an empty import for this currency
1004 // create an import based on launch conditions that covers all pre-allocations and uses the initial notarization
2f1f5cda 1005
1006 // if the currency is new and owned by this chain, its registration requires the fee, paid in its launch chain currency
1007 // otherwise, it is being imported from another chain and requires an import fee
1008 CCurrencyValueMap registrationFees;
1009 CAmount registrationAmount = 0;
1010 if (newCurrency.systemID == ASSETCHAINS_CHAINID)
1011 {
1012 if (newCurrency.launchSystemID != ASSETCHAINS_CHAINID)
1013 {
1014 registrationFees = CCurrencyValueMap(std::vector<uint160>({newCurrency.launchSystemID}),
1015 std::vector<int64_t>({ConnectedChains.FirstNotaryChain().chainDefinition.currencyRegistrationFee}));
1016 }
1017 else
1018 {
1019 registrationAmount = ConnectedChains.ThisChain().currencyRegistrationFee;
1020 }
1021 }
1022 else
1023 {
179b6f82 1024 registrationAmount = 0;
2f1f5cda 1025 }
a420f7d8 1026
179b6f82 1027 CCrossChainImport cci = CCrossChainImport(ConnectedChains.ThisChain().launchSystemID,
c8c684e9 1028 1,
1029 newCurID,
1030 CCurrencyValueMap());
203d1021 1031 cci.SetDefinitionImport(true);
c8c684e9 1032 outputs.push_back(CTxOut(0, MakeMofNCCScript(CConditionObj<CCrossChainImport>(EVAL_CROSSCHAIN_IMPORT, dests, 1, &cci))));
2f1f5cda 1033
2f1f5cda 1034 // add notarization before other outputs
1035 cp = CCinit(&CC, EVAL_EARNEDNOTARIZATION);
1036 if (newCurID == ASSETCHAINS_CHAINID &&
1037 newCurrency.notarizationProtocol == newCurrency.NOTARIZATION_NOTARY_CHAINID)
1038 {
1039 dests = std::vector<CTxDestination>({CIdentityID(newCurID)});
1040 }
1041 else
1042 {
1043 dests = std::vector<CTxDestination>({CPubKey(ParseHex(CC.CChexstr))});
1044 }
37434dab 1045 outputs.push_back(CTxOut(0, MakeMofNCCScript(CConditionObj<CPBaaSNotarization>(EVAL_EARNEDNOTARIZATION, dests, 1, &newNotarization))));
2f1f5cda 1046
1047 CReserveTransactionDescriptor rtxd;
37434dab 1048 CCoinbaseCurrencyState importState = newNotarization.currencyState;
2f1f5cda 1049 importState.RevertReservesAndSupply();
1050 CCurrencyValueMap importedCurrency;
1051 CCurrencyValueMap gatewayDepositsIn;
1052 CCurrencyValueMap spentCurrencyOut;
1053 CCoinbaseCurrencyState newCurrencyState;
1054 if (!rtxd.AddReserveTransferImportOutputs(ConnectedChains.FirstNotaryChain().chainDefinition,
1055 ConnectedChains.ThisChain(),
1056 newCurrency,
1057 importState,
1058 std::vector<CReserveTransfer>(),
1059 outputs,
1060 importedCurrency,
1061 gatewayDepositsIn,
1062 spentCurrencyOut,
1063 &newCurrencyState))
1064 {
1065 LogPrintf("Invalid starting currency import for %s\n", ConnectedChains.ThisChain().name.c_str());
1066 printf("Invalid starting currency import for %s\n", ConnectedChains.ThisChain().name.c_str());
1067 return false;
1068 }
c8c684e9 1069 }
2f1f5cda 1070 // export thread
1071 cp = CCinit(&CC, EVAL_CROSSCHAIN_EXPORT);
1072 dests = std::vector<CTxDestination>({CPubKey(ParseHex(CC.CChexstr))});
1073 CCrossChainExport ccx;
1074
1075 ccx = CCrossChainExport(ASSETCHAINS_CHAINID, 1, 1, newCurrency.systemID, newCurID, 0, CCurrencyValueMap(), CCurrencyValueMap(), uint256());
1076 outputs.push_back(CTxOut(0, MakeMofNCCScript(CConditionObj<CCrossChainExport>(EVAL_CROSSCHAIN_EXPORT, dests, 1, &ccx))));
c8c684e9 1077 }
1078 else
1079 {
1080 cp = CCinit(&CC, EVAL_EARNEDNOTARIZATION);
1081 if (newCurID == ASSETCHAINS_CHAINID &&
1082 newCurrency.notarizationProtocol == newCurrency.NOTARIZATION_NOTARY_CHAINID)
1083 {
1084 dests = std::vector<CTxDestination>({CIdentityID(newCurID)});
1085 }
1086 else
1087 {
1088 dests = std::vector<CTxDestination>({CPubKey(ParseHex(CC.CChexstr))});
1089 }
1090 // we notarize our notary chain here, not ourselves
37434dab 1091 if (newNotarization.currencyID == ASSETCHAINS_CHAINID)
c8c684e9 1092 {
37434dab 1093 if (!newNotarization.SetMirror())
a041b57e 1094 {
c8c684e9 1095 LogPrintf("Cannot mirror our notarization from notary chain\n");
1096 printf("Cannot mirror our notarization from notary chain\n");
1097 return false;
a041b57e 1098 }
1099 }
923bb2c0 1100 newNotarization.SetBlockOneNotarization();
37434dab 1101 outputs.push_back(CTxOut(0, MakeMofNCCScript(CConditionObj<CPBaaSNotarization>(EVAL_EARNEDNOTARIZATION, dests, 1, &newNotarization))));
c8c684e9 1102 }
c8c684e9 1103 return true;
1104}
1105
1106// create all special PBaaS outputs for block 1
1107bool MakeBlockOneCoinbaseOutputs(std::vector<CTxOut> &outputs,
1108 CPBaaSNotarization &launchNotarization,
24d12992 1109 CCurrencyValueMap &additionalFees,
c8c684e9 1110 const Consensus::Params &consensusParams)
1111{
c8c684e9 1112 uint160 thisChainID = ConnectedChains.ThisChain().GetID();
203d1021 1113 CCurrencyDefinition &thisChain = ConnectedChains.ThisChain();
37434dab 1114 uint160 firstNotaryID = ConnectedChains.FirstNotaryChain().GetID();
c8c684e9 1115 CCoinbaseCurrencyState currencyState;
9baf4edc 1116 std::map<uint160, std::vector<std::pair<std::pair<CInputDescriptor, CPartialTransactionProof>, std::vector<CReserveTransfer>>>> blockOneExportImports;
a041b57e 1117
203d1021 1118 std::pair<CUTXORef, CPartialTransactionProof> launchNotarizationProof;
0b1a0e7f 1119 std::pair<CUTXORef, CPartialTransactionProof> launchExportProof;
a36e0d92 1120 std::vector<CReserveTransfer> launchExportTransfers;
e7c64795 1121 CPBaaSNotarization notaryNotarization;
203d1021 1122
1123 if (!GetBlockOneLaunchNotarization(ConnectedChains.FirstNotaryChain(),
1124 thisChainID,
1125 thisChain,
1126 launchNotarization,
e7c64795 1127 notaryNotarization,
0b1a0e7f 1128 launchNotarizationProof,
a36e0d92 1129 launchExportProof,
1130 launchExportTransfers))
c8c684e9 1131 {
1132 // cannot make block 1 unless we can get the initial currency state from the first notary system
1133 LogPrintf("Cannot find chain on notary system\n");
1134 printf("Cannot find chain on notary system\n");
1135 return false;
1136 }
1137
1138 // we need to have a launch decision to be able to mine any blocks, prior to launch being clear,
1139 // it is not an error. we are just not ready.
1140 if (!launchNotarization.IsLaunchCleared())
1141 {
1142 return false;
1143 }
1144
2f1f5cda 1145 if (!launchNotarization.IsLaunchConfirmed())
c8c684e9 1146 {
1147 // we must reach minimums in all currencies to launch
1148 LogPrintf("This chain did not receive the minimum currency contributions and cannot launch. Pre-launch contributions to this chain can be refunded.\n");
1149 printf("This chain did not receive the minimum currency contributions and cannot launch. Pre-launch contributions to this chain can be refunded.\n");
1150 return false;
1151 }
1152
9baf4edc 1153 // get initial imports
5d75d90d 1154 if (!GetBlockOneImports(ConnectedChains.FirstNotaryChain(), launchNotarization, blockOneExportImports))
9baf4edc 1155 {
1156 // we must reach minimums in all currencies to launch
1157 LogPrintf("Cannot retrieve initial export imports from notary system\n");
1158 printf("Cannot retrieve initial export imports from notary system\n");
1159 return false;
1160 }
1161
c8c684e9 1162 // get all currencies/IDs that we will need to retrieve from our notary chain
5d75d90d 1163 std::set<uint160> blockOneCurrencies;
e6c5e0b8 1164 std::set<uint160> blockOneIDs = {ASSETCHAINS_CHAINID};
c8c684e9 1165 std::set<uint160> convertersToCreate;
1166
1167 CPBaaSNotarization converterNotarization;
203d1021 1168 std::pair<CUTXORef, CPartialTransactionProof> converterNotarizationProof;
0b1a0e7f 1169 std::pair<CUTXORef, CPartialTransactionProof> converterExportProof;
a36e0d92 1170 std::vector<CReserveTransfer> converterExportTransfers;
441087ab 1171 uint160 converterCurrencyID = thisChain.GatewayConverterID();
203d1021 1172 CCurrencyDefinition converterCurDef;
c8c684e9 1173
1174 // if we have a converter currency, ensure that it also meets requirements for currency launch
1175 if (!thisChain.gatewayConverterName.empty())
1176 {
1177 if (!GetBlockOneLaunchNotarization(ConnectedChains.FirstNotaryChain(),
203d1021 1178 converterCurrencyID,
1179 converterCurDef,
1180 converterNotarization,
e7c64795 1181 notaryNotarization,
0b1a0e7f 1182 converterNotarizationProof,
a36e0d92 1183 converterExportProof,
1184 converterExportTransfers))
a041b57e 1185 {
c8c684e9 1186 LogPrintf("Unable to get gateway converter initial state\n");
1187 printf("Unable to get gateway converter initial state\n");
1188 return false;
1189 }
a041b57e 1190
54f68f98 1191 notaryNotarization.currencyStates[converterCurrencyID] = converterNotarization.currencyState;
1192
c8c684e9 1193 // both currency and primary gateway must have their pre-launch phase complete before we can make a decision
1194 // about launching
1195 if (!converterNotarization.IsLaunchCleared())
1196 {
1197 return false;
1198 }
a041b57e 1199
c8c684e9 1200 // we need to have a cleared launch to be able to launch
1201 if (!converterNotarization.IsLaunchConfirmed())
1202 {
1203 LogPrintf("Primary currency met requirements for launch, but gateway currency converter did not\n");
1204 printf("Primary currency met requirements for launch, but gateway currency converter did not\n");
1205 return false;
1206 }
a041b57e 1207
c8c684e9 1208 convertersToCreate.insert(converterCurrencyID);
c8c684e9 1209 }
a041b57e 1210
2f1f5cda 1211 // Now, add block 1 imports, which provide a foundation of all IDs and currencies needed to launch the
c8c684e9 1212 // new system, including ID and currency outputs for notary chain, all currencies we accept for pre-conversion,
1213 // native currency of system launching the chain.
c8c684e9 1214 for (auto &oneNotary : ConnectedChains.notarySystems)
1215 {
bea765bd 1216 // first, we need to have the native notary currency itself and its notaries, if it has them
c8c684e9 1217 blockOneCurrencies.insert(oneNotary.first);
e4e650fa 1218 blockOneIDs.insert(oneNotary.second.notaryChain.chainDefinition.notaries.begin(), oneNotary.second.notaryChain.chainDefinition.notaries.end());
c8c684e9 1219 }
a041b57e 1220
8fab3a0d 1221 for (auto &onePrealloc : thisChain.preAllocation)
1222 {
1223 blockOneIDs.insert(onePrealloc.first);
1224 }
1225
bea765bd 1226 // get this chain's notaries
c8c684e9 1227 auto &notaryIDs = ConnectedChains.ThisChain().notaries;
1228 blockOneIDs.insert(notaryIDs.begin(), notaryIDs.end());
1229
1230 // now retrieve IDs and currencies
1231 std::map<uint160, std::pair<CCurrencyDefinition,CPBaaSNotarization>> currencyImports;
1232 std::map<uint160, CIdentity> identityImports;
1233 if (!ConnectedChains.GetNotaryCurrencies(ConnectedChains.FirstNotaryChain(), blockOneCurrencies, currencyImports) ||
1234 !ConnectedChains.GetNotaryIDs(ConnectedChains.FirstNotaryChain(), blockOneIDs, identityImports))
1235 {
1236 // we must reach minimums in all currencies to launch
1237 LogPrintf("Cannot retrieve identity and currency definitions needed to create block 1\n");
1238 printf("Cannot retrieve identity and currency definitions needed to create block 1\n");
1239 return false;
1240 }
1241
e7c64795 1242 if (currencyImports.count(notaryNotarization.currencyID))
1243 {
1244 currencyImports[notaryNotarization.currencyID].second = notaryNotarization;
1245 }
1246
c8c684e9 1247 // add all imported currency and identity outputs, identity revocaton and recovery IDs must be explicitly imported if needed
1248 for (auto &oneIdentity : identityImports)
1249 {
1250 outputs.push_back(CTxOut(0, oneIdentity.second.IdentityUpdateOutputScript(1)));
1251 }
1252
e6c5e0b8 1253 // calculate all issued currency on this chain for both the native and converter currencies,
1254 // which is the only currency that can be considered a gateway deposit at launch. this can
1255 // be used for native currency fee conversions
1256 CCurrencyValueMap gatewayDeposits;
1257
203d1021 1258 bool success = AddOneCurrencyImport(thisChain,
1259 launchNotarization,
1260 &launchNotarizationProof,
0b1a0e7f 1261 &launchExportProof,
a36e0d92 1262 launchExportTransfers,
e6c5e0b8 1263 gatewayDeposits,
24d12992 1264 outputs,
1265 additionalFees);
203d1021 1266
1267 // now, the converter
1268 if (success && converterCurDef.IsValid())
c8c684e9 1269 {
e6c5e0b8 1270 // TODO: add a new ID for the converter currency, controlled by the same primary addresses as the
1271 // ID for this chain
1272 CCurrencyValueMap converterDeposits;
1273
b4ba7e7c 1274 success = AddOneCurrencyImport(converterCurDef,
203d1021 1275 converterNotarization,
1276 &converterNotarizationProof,
0b1a0e7f 1277 &converterExportProof,
a36e0d92 1278 converterExportTransfers,
e6c5e0b8 1279 converterDeposits,
24d12992 1280 outputs,
1281 additionalFees);
203d1021 1282 }
1283
1284 if (success)
1285 {
1286 currencyImports.erase(ASSETCHAINS_CHAINID);
1287 currencyImports.erase(converterCurrencyID);
1288 // now, add the rest of necessary currencies
1289 for (auto &oneCurrency : currencyImports)
c8c684e9 1290 {
203d1021 1291 success = AddOneCurrencyImport(oneCurrency.second.first,
1292 oneCurrency.second.second,
1293 nullptr,
0b1a0e7f 1294 nullptr,
a36e0d92 1295 std::vector<CReserveTransfer>(),
e6c5e0b8 1296 gatewayDeposits,
24d12992 1297 outputs,
1298 additionalFees);
203d1021 1299 if (!success)
1300 {
1301 break;
1302 }
a041b57e 1303 }
1304 }
c8c684e9 1305 return success;
a041b57e 1306}
1307
855714b0 1308CBlockTemplate* CreateNewBlock(const CChainParams& chainparams, const std::vector<CTxOut> &minerOutputs, bool isStake)
d247a5d1 1309{
41f170fd
MT
1310 // instead of one scriptPubKeyIn, we take a vector of them along with relative weight. each is assigned a percentage of the block subsidy and
1311 // mining reward based on its weight relative to the total
855714b0 1312 if (!(minerOutputs.size() && ConnectedChains.SetLatestMiningOutputs(minerOutputs) || isStake))
06f41160 1313 {
a4f9bc97 1314 fprintf(stderr,"%s: Must have valid miner outputs, including script with valid PK, PKH, or Verus ID destination.\n", __func__);
88bc6df5 1315 return NULL;
06f41160 1316 }
1317
855714b0 1318 CTxDestination firstDestination;
a4f9bc97 1319
bb6c3482 1320 if (minerOutputs.size())
06f41160 1321 {
bb6c3482 1322 int64_t shareCheck = 0;
855714b0 1323 CTxDestination checkDest;
1324 for (auto &output : minerOutputs)
c3250dcd 1325 {
855714b0 1326 shareCheck += output.nValue;
1327 if (shareCheck < 0 ||
1328 shareCheck > INT_MAX ||
1329 !ExtractDestination(output.scriptPubKey, checkDest) ||
1330 (checkDest.which() == COptCCParams::ADDRTYPE_INVALID))
bb6c3482 1331 {
1332 fprintf(stderr,"Invalid miner outputs share specifications\n");
1333 return NULL;
1334 }
c3250dcd 1335 }
855714b0 1336 ExtractDestination(minerOutputs[0].scriptPubKey, firstDestination);
06f41160 1337 }
1338
855714b0 1339 uint32_t blocktime;
2a6a442a 1340 //fprintf(stderr,"create new block\n");
df756d24 1341 // Create new block
08c58194 1342 std::unique_ptr<CBlockTemplate> pblocktemplate(new CBlockTemplate());
d247a5d1 1343 if(!pblocktemplate.get())
1b5b89ba 1344 {
1345 fprintf(stderr,"pblocktemplate.get() failure\n");
d247a5d1 1346 return NULL;
1b5b89ba 1347 }
d247a5d1 1348 CBlock *pblock = &pblocktemplate->block; // pointer for convenience
12217420 1349
855714b0 1350 pblock->nSolution.resize(Eh200_9.SolutionWidth);
1351
12217420 1352 pblock->SetVersionByHeight(chainActive.LastTip()->GetHeight() + 1);
1353
1354 // -regtest only: allow overriding block.nVersion with
dbca89b7 1355 // -blockversion=N to test forking scenarios
793a72e3 1356 if (chainparams.MineBlocksOnDemand())
dbca89b7 1357 pblock->nVersion = GetArg("-blockversion", pblock->nVersion);
e9e70b95 1358
41f170fd 1359 // Add dummy coinbase tx placeholder as first transaction
4949004d 1360 pblock->vtx.push_back(CTransaction());
41f170fd 1361
d247a5d1
JG
1362 pblocktemplate->vTxFees.push_back(-1); // updated at end
1363 pblocktemplate->vTxSigOps.push_back(-1); // updated at end
e9e70b95 1364
d247a5d1 1365 // Largest block you're willing to create:
ad898b40 1366 unsigned int nBlockMaxSize = GetArg("-blockmaxsize", DEFAULT_BLOCK_MAX_SIZE);
855714b0 1367
d247a5d1
JG
1368 // Limit to betweeen 1K and MAX_BLOCK_SIZE-1K for sanity:
1369 nBlockMaxSize = std::max((unsigned int)1000, std::min((unsigned int)(MAX_BLOCK_SIZE-1000), nBlockMaxSize));
f711aed7 1370
1371 unsigned int nMaxIDSize = nBlockMaxSize / 2;
1372 unsigned int nCurrentIDSize = 0;
e9e70b95 1373
d247a5d1
JG
1374 // How much of the block should be dedicated to high-priority transactions,
1375 // included regardless of the fees they pay
1376 unsigned int nBlockPrioritySize = GetArg("-blockprioritysize", DEFAULT_BLOCK_PRIORITY_SIZE);
1377 nBlockPrioritySize = std::min(nBlockMaxSize, nBlockPrioritySize);
e9e70b95 1378
d247a5d1
JG
1379 // Minimum block size you want to create; block will be filled with free transactions
1380 // until there are no more or the block reaches this size:
037b4f14 1381 unsigned int nBlockMinSize = GetArg("-blockminsize", DEFAULT_BLOCK_MIN_SIZE);
d247a5d1 1382 nBlockMinSize = std::min(nBlockMaxSize, nBlockMinSize);
e9e70b95 1383
d247a5d1 1384 // Collect memory pool transactions into the block
a372168e 1385 CAmount nFees = 0;
855714b0 1386 CAmount takenFees = 0;
df756d24 1387
41f170fd 1388 bool isVerusActive = IsVerusActive();
56fe75cb 1389 CCurrencyDefinition &thisChain = ConnectedChains.ThisChain();
56fe75cb 1390
1391 std::vector<CAmount> exchangeRate(thisChain.currencies.size());
41f170fd 1392
df756d24
MT
1393 // we will attempt to spend any cheats we see
1394 CTransaction cheatTx;
1395 boost::optional<CTransaction> cheatSpend;
1396 uint256 cbHash;
1397
562852ab 1398 CBlockIndex* pindexPrev = 0;
fdf05416 1399 bool loop = true;
1400 while (loop)
d247a5d1 1401 {
fdf05416 1402 loop = false;
1403 int nHeight;
df756d24 1404 const Consensus::Params &consensusParams = chainparams.GetConsensus();
fdf05416 1405 uint32_t consensusBranchId;
fdf05416 1406 int64_t nMedianTimePast = 0;
1407 uint32_t proposedTime = 0;
a0dd01bc 1408
a0dd01bc 1409 {
fdf05416 1410 while (proposedTime == nMedianTimePast)
a0dd01bc 1411 {
fdf05416 1412 if (proposedTime)
1413 {
1414 MilliSleep(20);
1415 }
1416 LOCK(cs_main);
1417 pindexPrev = chainActive.LastTip();
1418 nHeight = pindexPrev->GetHeight() + 1;
1419 consensusBranchId = CurrentEpochBranchId(nHeight, consensusParams);
fdf05416 1420 nMedianTimePast = pindexPrev->GetMedianTimePast();
a0dd01bc 1421 proposedTime = GetAdjustedTime();
fdf05416 1422
a0dd01bc 1423 if (proposedTime == nMedianTimePast)
fdf05416 1424 {
1425 boost::this_thread::interruption_point();
1426 }
a0dd01bc 1427 }
1428 }
fdf05416 1429
a420f7d8 1430 CCoinbaseCurrencyState currencyState;
7c70438d 1431 CCoinsViewCache view(pcoinsTip);
f9155fec 1432 uint32_t expired; uint64_t commission;
6ff77181 1433
4fc309f0 1434 SaplingMerkleTree sapling_tree;
a420f7d8 1435
1436 {
1437 LOCK2(cs_main, mempool.cs);
1438 if (pindexPrev != chainActive.LastTip())
1439 {
1440 // try again
1441 loop = true;
1442 continue;
1443 }
1444 pblock->nTime = GetAdjustedTime();
1445
1446 currencyState = ConnectedChains.GetCurrencyState(nHeight);
1447
1448 if (!(view.GetSaplingAnchorAt(view.GetBestAnchor(SAPLING), sapling_tree)))
1449 {
1450 LogPrintf("%s: failed to get Sapling anchor\n", __func__);
1451 assert(false);
1452 }
1453 }
31a04d28 1454
d247a5d1
JG
1455 // Priority order to process transactions
1456 list<COrphan> vOrphan; // list memory doesn't move
1457 map<uint256, vector<COrphan*> > mapDependers;
1458 bool fPrintPriority = GetBoolArg("-printpriority", false);
e9e70b95 1459
d247a5d1
JG
1460 // This vector will be sorted into a priority queue:
1461 vector<TxPriority> vecPriority;
df756d24
MT
1462 vecPriority.reserve(mempool.mapTx.size() + 1);
1463
df756d24 1464 {
a420f7d8 1465 LOCK(cs_main);
1466
1467 // check if we should add cheat transaction
1468 CBlockIndex *ppast;
1469 CTransaction cb;
1470 int cheatHeight = nHeight - COINBASE_MATURITY < 1 ? 1 : nHeight - COINBASE_MATURITY;
1471 if (defaultSaplingDest &&
1472 chainActive.Height() > 100 &&
1473 (ppast = chainActive[cheatHeight]) &&
1474 ppast->IsVerusPOSBlock() &&
1475 cheatList.IsHeightOrGreaterInList(cheatHeight))
df756d24 1476 {
a420f7d8 1477 // get the block and see if there is a cheat candidate for the stake tx
1478 CBlock b;
1479 if (!(fHavePruned && !(ppast->nStatus & BLOCK_HAVE_DATA) && ppast->nTx > 0) && ReadBlockFromDisk(b, ppast, chainparams.GetConsensus(), 1))
df756d24 1480 {
a420f7d8 1481 CTransaction &stakeTx = b.vtx[b.vtx.size() - 1];
df756d24 1482
a420f7d8 1483 if (cheatList.IsCheatInList(stakeTx, &cheatTx))
73a4cd20 1484 {
a420f7d8 1485 // make and sign the cheat transaction to spend the coinbase to our address
1486 CMutableTransaction mtx = CreateNewContextualCMutableTransaction(consensusParams, nHeight);
df756d24 1487
a420f7d8 1488 uint32_t voutNum;
1489 // get the first vout with value
1490 for (voutNum = 0; voutNum < b.vtx[0].vout.size(); voutNum++)
df756d24 1491 {
a420f7d8 1492 if (b.vtx[0].vout[voutNum].nValue > 0)
1493 break;
df756d24
MT
1494 }
1495
a420f7d8 1496 // send to the same pub key as the destination of this block reward
1497 if (MakeCheatEvidence(mtx, b.vtx[0], voutNum, cheatTx))
df756d24 1498 {
a420f7d8 1499 LOCK(pwalletMain->cs_wallet);
1500 TransactionBuilder tb = TransactionBuilder(consensusParams, nHeight);
1501 cb = b.vtx[0];
1502 cbHash = cb.GetHash();
1503
1504 bool hasInput = false;
1505 for (uint32_t i = 0; i < cb.vout.size(); i++)
1506 {
1507 // add the spends with the cheat
1508 if (cb.vout[i].nValue > 0)
88d014d0 1509 {
a420f7d8 1510 tb.AddTransparentInput(COutPoint(cbHash,i), cb.vout[0].scriptPubKey, cb.vout[0].nValue);
1511 hasInput = true;
88d014d0 1512 }
a420f7d8 1513 }
1514
1515 if (hasInput)
1516 {
1517 // this is a send from a t-address to a sapling address, which we don't have an ovk for.
1518 // Instead, generate a common one from the HD seed. This ensures the data is
1519 // recoverable, at least for us, while keeping it logically separate from the ZIP 32
1520 // Sapling key hierarchy, which the user might not be using.
1521 uint256 ovk;
1522 HDSeed seed;
1523 if (pwalletMain->GetHDSeed(seed)) {
1524 ovk = ovkForShieldingFromTaddr(seed);
1525
1526 // send everything to Sapling address
1527 tb.SendChangeTo(defaultSaplingDest.value(), ovk);
1528
1529 tb.AddOpRet(mtx.vout[mtx.vout.size() - 1].scriptPubKey);
1530
1531 TransactionBuilderResult buildResult(tb.Build());
1532 if (!buildResult.IsError() && buildResult.IsTx())
1533 {
1534 cheatSpend = buildResult.GetTxOrThrow();
1535 }
1536 else
1537 {
1538 LogPrintf("Error building cheat catcher transaction: %s\n", buildResult.GetError().c_str());
1539 }
88d014d0 1540 }
df756d24
MT
1541 }
1542 }
1543 }
1544 }
1545 }
df756d24 1546
a420f7d8 1547 if (cheatSpend)
45bb4681 1548 {
a420f7d8 1549 LOCK(mempool.cs);
1550
1551 cheatTx = cheatSpend.value();
1552 std::list<CTransaction> removed;
1553 mempool.removeConflicts(cheatTx, removed);
1554 printf("Found cheating stake! Adding cheat spend for %.8f at block #%d, coinbase tx\n%s\n",
1555 (double)cb.GetValueOut() / (double)COIN, nHeight, cheatSpend.value().vin[0].prevout.hash.GetHex().c_str());
1556
1557 // add to mem pool and relay
1558 if (myAddtomempool(cheatTx))
1559 {
1560 RelayTransaction(cheatTx);
1561 }
45bb4681 1562 }
271326fa 1563 }
1564
41f170fd
MT
1565 //
1566 // Now start solving the block
1567 //
1568
1569 uint64_t nBlockSize = 1000; // initial size
1570 uint64_t nBlockTx = 1; // number of transactions - always have a coinbase
1571 uint32_t autoTxSize = 0; // extra transaction overhead that we will add while creating the block
1572 int nBlockSigOps = 100;
1573
1574 // VerusPoP staking transaction data
1575 CMutableTransaction txStaked; // if this is a stake operation, the staking transaction that goes at the end
1576 uint32_t nStakeTxSize = 0; // serialized size of the stake transaction
1577
855714b0 1578 // if this is not for mining, first determine if we have a right to make a block
41f170fd
MT
1579 if (isStake)
1580 {
b2a59a12 1581 uint64_t txfees, utxovalue;
1582 uint32_t txtime;
1583 uint256 utxotxid;
1584 int32_t i, siglen, numsigs, utxovout;
1585 std::vector<unsigned char> utxosig;
1586
41f170fd
MT
1587 txStaked = CreateNewContextualCMutableTransaction(Params().GetConsensus(), nHeight);
1588
41f170fd
MT
1589 if (ASSETCHAINS_LWMAPOS != 0)
1590 {
1591 uint32_t nBitsPOS;
1592 arith_uint256 posHash;
1593
855714b0 1594 siglen = verus_staked(pblock, txStaked, nBitsPOS, posHash, utxosig, firstDestination);
41f170fd 1595 blocktime = GetAdjustedTime();
41f170fd 1596 }
41f170fd
MT
1597
1598 if (siglen <= 0)
1599 {
1600 return NULL;
1601 }
1602
1603 pblock->nTime = blocktime;
1604 nStakeTxSize = GetSerializeSize(txStaked, SER_NETWORK, PROTOCOL_VERSION);
1605 nBlockSize += nStakeTxSize;
1606 }
1607
bb6c3482 1608 ConnectedChains.AggregateChainTransfers(firstDestination, nHeight);
1609
41f170fd
MT
1610 // Now the coinbase -
1611 // A PBaaS coinbase must have some additional outputs to enable certain chain state and functions to be properly
1612 // validated. All but currency state and the first chain definition are either optional or not valid on non-fractional reserve PBaaS blockchains
1613 // All of these are instant spend outputs that have no maturity wait time and may be spent in the same block.
1614 //
1615 // 1. (required) currency state - current state of currency supply and optionally reserve, premine, etc. This is primarily a data output to provide
1616 // cross check for coin minting and burning operations, making it efficient to determine up-to-date supply, reserves, and conversions. To provide
1617 // an extra level of supply cross-checking and fast data retrieval, this is part of all PBaaS chains' protocol, not just reserves.
1618 // This output also includes reserve and native amounts for total conversions, less fees, of any conversions between Verus reserve and the
1619 // native currency.
1620 //
1621 // 2. (block 1 required) chain definition - in order to confirm the amount of coins converted and issued within the possible range, before chain start,
1622 // new PBaaS chains have a zero-amount, unspendable chain definition output.
1623 //
1624 // 3. (block 1 optional) initial import utxo - for any chain with conversion or pre-conversion, the first coinbase must include an initial import utxo.
1625 // Pre-conversions are handled on the launch chain before the PBaaS chain starts, so they are an additional output, which begins
1626 // as a fixed amount and is spent with as many outputs as necessary to the recipients of the pre-conversion transactions when those pre-conversions
1627 // are imported. All pre-converted outputs get their source currency from a thread that starts with this output in block 1.
1628 //
1629 // 4. (block 1 optional) initial export utxo - reserve chains, or any chain that will use exports to another chain must have an initial export utxo, any chain
1630 // may have one, but currently, they can only be spent with valid exports, which only occur on reserve chains
1631 //
1632 // 5. (optional) notarization output - in order to ensure that notarization can occur independent of the availability of fungible
1633 // coins on the network, and also that the notarization can provide a spendable finalization output and possible reward
1634 //
1635 // In addition, each PBaaS block can be mined with optional, fee-generating transactions. Inporting transactions from the reserve chain or sending
1636 // exported transactions to the reserve chain are optional fee-generating steps that would be easy to do when running multiple daemons.
1637 // The types of transactions miners/stakers may facilitate or create for fees are as follows:
1638 //
1639 // 1. Earned notarization of Verus chain - spends the notarization instant out. must be present and spend the notarization output if there is a notarization output
1640 //
1641 // 2. Imported transactions from the export thread for this PBaaS chain on the Verus blockchain - imported transactions must spend the import utxo
1642 // thread, represent the export from the alternate chain which spends the export output from the prior import transaction, carry a notary proof, and
1643 // include outputs that map to each of its inputs on the source chain. Outputs can include unconverted reserve outputs only on fractional
1644 // reserve chains, pre-converted outputs for any chain with launch conversion, and post launch outputs to be converted on fractional reserve
1645 // chains. Each are handled in the following way:
1646 // a. Unconverted outputs are left as outputs to the intended destination of Verus reserve token and do not pass through the coinbase
1647 // b. Pre-converted outputs require that the import transaction spend the last pre-conversion output starting at block 1 as the source for
1648 // pre-converted currency.
1649 //
1650 // 3. Zero or more aggregated exports that combine individual cross-chain transactions and reserve transfer outputs for export to the Verus chain.
1651 //
1652 // 4. Conversion distribution transactions for all native and reserve currency conversions, including reserve transfer outputs without conversion as
1653 // a second step for reserve transfers that have conversion included. Any remaining pre-converted reserve must always remain in a change output
1654 // until it is exhausted
24d12992 1655 CTxOut premineOut;
41f170fd
MT
1656
1657 // size of conversion tx
0574c740 1658 std::vector<CInputDescriptor> conversionInputs;
41f170fd 1659
4fa3b13d 1660 // if we are a PBaaS chain, first make sure we don't start prematurely, and if
68b309c0 1661 // we should make an earned notarization, make it and set index to non-zero value
41f170fd
MT
1662 int32_t notarizationTxIndex = 0; // index of notarization if it is added
1663 int32_t conversionTxIndex = 0; // index of conversion transaction if it is added
1664
1665 // export transactions can be created here by aggregating all pending transfer requests and either getting 10 or more together, or
1666 // waiting n (10) blocks since the last one. each export must spend the output of the one before it
1667 std::vector<CMutableTransaction> exportTransactions;
1668
1669 // all transaction outputs requesting conversion to another currency (PBaaS fractional reserve only)
1670 // these will be used to calculate conversion price, fees, and generate coinbase conversion output as well as the
1671 // conversion output transaction
1672 std::vector<CTxOut> reserveConversionTo;
1673 std::vector<CTxOut> reserveConversionFrom;
1674
68b309c0 1675 int64_t pbaasTransparentIn = 0;
eb0a6550 1676 int64_t pbaasTransparentOut = 0;
31609f35 1677 //extern int64_t ASSETCHAINS_SUPPLY;
1678 //printf("%lu premine\n", ASSETCHAINS_SUPPLY);
1fa4454d 1679 int64_t blockSubsidy = GetBlockSubsidy(nHeight, consensusParams);
ebee7b5b 1680
56fe75cb 1681 uint160 thisChainID = ConnectedChains.ThisChain().GetID();
41f170fd 1682
8577896f 1683 uint256 mmrRoot;
13ed2980 1684 vector<CInputDescriptor> notarizationInputs;
1fa4454d 1685
41f170fd
MT
1686 // used as scratch for making CCs, should be reinitialized each time
1687 CCcontract_info CC;
1688 CCcontract_info *cp;
c8c684e9 1689 std::vector<CTxDestination> dests;
c3250dcd 1690 CPubKey pkCC;
41f170fd
MT
1691
1692 // Create coinbase tx and set up the null input with height
1693 CMutableTransaction coinbaseTx = CreateNewContextualCMutableTransaction(consensusParams, nHeight);
bb6c3482 1694 coinbaseTx.vin.push_back(CTxIn(uint256(), (uint32_t)-1, CScript() << nHeight << OP_0));
41f170fd 1695
41f170fd
MT
1696 // we will update amounts and fees later, but convert the guarded output now for validity checking and size estimate
1697 if (isStake)
1698 {
1699 // if there is a specific destination, use it
1700 CTransaction stakeTx(txStaked);
1701 CStakeParams p;
1702 if (ValidateStakeTransaction(stakeTx, p, false))
1703 {
56fe75cb 1704 if (p.Version() < p.VERSION_EXTENDED_STAKE && !p.pk.IsValid())
41f170fd
MT
1705 {
1706 LogPrintf("CreateNewBlock: invalid public key\n");
1707 fprintf(stderr,"CreateNewBlock: invalid public key\n");
1708 return NULL;
1709 }
c52bd43c 1710 CTxDestination guardedOutputDest = (p.Version() < p.VERSION_EXTENDED_STAKE) ? p.pk : p.delegate;
56fe75cb 1711 coinbaseTx.vout.push_back(CTxOut(1, CScript()));
c52bd43c 1712 if (!MakeGuardedOutput(1, guardedOutputDest, stakeTx, coinbaseTx.vout.back()))
41f170fd 1713 {
56fe75cb 1714 LogPrintf("CreateNewBlock: failed to make GuardedOutput on staking coinbase\n");
1715 fprintf(stderr,"CreateNewBlock: failed to make GuardedOutput on staking coinbase\n");
1716 return NULL;
41f170fd 1717 }
5d6bdede 1718 COptCCParams optP;
5c7c7edc 1719 if (!coinbaseTx.vout.back().scriptPubKey.IsPayToCryptoCondition(optP) || !optP.IsValid())
5d6bdede 1720 {
c52bd43c 1721 MakeGuardedOutput(1, guardedOutputDest, stakeTx, coinbaseTx.vout.back());
5d6bdede 1722 LogPrintf("%s: created invalid staking coinbase\n", __func__);
1723 fprintf(stderr,"%s: created invalid staking coinbase\n", __func__);
1724 return NULL;
1725 }
41f170fd
MT
1726 }
1727 else
1728 {
1729 LogPrintf("CreateNewBlock: invalid stake transaction\n");
1730 fprintf(stderr,"CreateNewBlock: invalid stake transaction\n");
1731 return NULL;
1732 }
1733 }
56fe75cb 1734 else
1735 {
1736 // default outputs for mining and before stake guard or fee calculation
1737 // store the relative weight in the amount output to convert later to a relative portion
1738 // of the reward + fees
855714b0 1739 coinbaseTx.vout.insert(coinbaseTx.vout.end(), minerOutputs.begin(), minerOutputs.end());
56fe75cb 1740 }
41f170fd 1741
34d1aa13 1742 CAmount totalEmission = blockSubsidy;
24d12992 1743 CCurrencyValueMap additionalFees;
34d1aa13 1744
c8c684e9 1745 // if we don't have a connected root PBaaS chain, we can't properly check
1746 // and notarize the start block, so we have to pass the notarization and cross chain steps
67dceeec 1747 bool notaryConnected = ConnectedChains.IsNotaryAvailable();
5cd8b21a 1748
c8c684e9 1749 // at block 1 for a PBaaS chain, we validate launch conditions
1750 if (!isVerusActive && nHeight == 1)
1751 {
1752 CPBaaSNotarization launchNotarization;
e54d0c10 1753 if (!ConnectedChains.readyToStart &&
1754 !ConnectedChains.CheckVerusPBaaSAvailable() &&
1755 !ConnectedChains.readyToStart)
76f69285 1756 {
1757 return NULL;
1758 }
24d12992 1759 if (!MakeBlockOneCoinbaseOutputs(coinbaseTx.vout, launchNotarization, additionalFees, Params().GetConsensus()))
4fa3b13d 1760 {
c8c684e9 1761 // can't mine block 1 if we are not connected to a notary
1762 printf("%s: cannot create block one coinbase outputs\n", __func__);
1763 LogPrintf("%s: cannot create block one coinbase outputs\n", __func__);
c8c684e9 1764 return NULL;
989b1de1 1765 }
c8c684e9 1766 currencyState = launchNotarization.currencyState;
1767 }
989b1de1 1768
9abe77b7 1769 // if we are a notary, notarize
1770 if (!VERUS_NOTARYID.IsNull())
1771 {
1772 CValidationState state;
d5af62fe 1773 TransactionBuilder notarizationBuilder = TransactionBuilder(consensusParams, nHeight, pwalletMain);
9abe77b7 1774 bool finalized;
a420f7d8 1775 CTransaction notarizationTx;
9abe77b7 1776 if (CPBaaSNotarization::ConfirmOrRejectNotarizations(pwalletMain, ConnectedChains.FirstNotaryChain(), state, notarizationBuilder, finalized))
1777 {
d5af62fe 1778 if (!notarizationBuilder.mtx.vin.size())
48ccc7f5 1779 {
544b1140 1780 bool success = false;
1781 CCurrencyValueMap reserveValueOut;
1782 CAmount nativeValueOut;
d5af62fe 1783 // get a native currency input capable of paying a fee, and make our notary ID the change address
1784 std::set<std::pair<const CWalletTx *, unsigned int>> setCoinsRet;
1785 {
1786 LOCK(pwalletMain->cs_wallet);
1787 std::vector<COutput> vCoins;
d5af62fe 1788 if (IsVerusActive())
1789 {
1790 pwalletMain->AvailableCoins(vCoins,
1791 false,
1792 nullptr,
1793 false,
1794 true,
1795 true,
1796 true,
1797 false);
544b1140 1798 success = pwalletMain->SelectCoinsMinConf(CPBaaSNotarization::DEFAULT_NOTARIZATION_FEE, 0, 0, vCoins, setCoinsRet, nativeValueOut);
d5af62fe 1799 notarizationBuilder.SetFee(CPBaaSNotarization::DEFAULT_NOTARIZATION_FEE);
1800 }
1801 else
1802 {
1803 CCurrencyValueMap totalTxFees;
1804 totalTxFees.valueMap[ConnectedChains.FirstNotaryChain().chainDefinition.GetID()] = CPBaaSNotarization::DEFAULT_NOTARIZATION_FEE;
1805 notarizationBuilder.SetReserveFee(totalTxFees);
1806 notarizationBuilder.SetFee(0);
1807 pwalletMain->AvailableReserveCoins(vCoins,
1808 false,
1809 nullptr,
1810 true,
1811 true,
1812 nullptr,
1813 &totalTxFees,
1814 false);
544b1140 1815
1816 success = pwalletMain->SelectReserveCoinsMinConf(totalTxFees,
1817 0,
1818 0,
1819 1,
1820 vCoins,
1821 setCoinsRet,
1822 reserveValueOut,
1823 nativeValueOut);
d5af62fe 1824 }
1825 }
1826 for (auto &oneInput : setCoinsRet)
1827 {
1828 notarizationBuilder.AddTransparentInput(COutPoint(oneInput.first->GetHash(), oneInput.second),
1829 oneInput.first->vout[oneInput.second].scriptPubKey,
1830 oneInput.first->vout[oneInput.second].nValue);
1831 }
1832 notarizationBuilder.SendChangeTo(CTxDestination(VERUS_NOTARYID));
48ccc7f5 1833 }
d5af62fe 1834 else
48ccc7f5 1835 {
d5af62fe 1836 notarizationBuilder.SetFee(0);
48ccc7f5 1837 }
48ccc7f5 1838
5e34790a 1839 if (notarizationBuilder.mtx.vin.size())
a420f7d8 1840 {
5e34790a 1841 LOCK2(cs_main, mempool.cs);
1842 TransactionBuilderResult buildResult = notarizationBuilder.Build();
1843 if (buildResult.IsTx())
1844 {
1845 notarizationTx = buildResult.GetTxOrThrow();
6c93e6f8 1846
5e34790a 1847 UniValue jsonNotaryConfirmations(UniValue::VOBJ);
1848 TxToUniv(notarizationTx, uint256(), jsonNotaryConfirmations);
1849 //printf("%s: (PII) Submitting notarization confirmations:\n%s\n", __func__, jsonNotaryConfirmations.write(1,2).c_str());
1850 LogPrintf("%s: (PII) Submitting notarization confirmations:\n%s\n", __func__, jsonNotaryConfirmations.write(1,2).c_str());
9abe77b7 1851
5e34790a 1852 // add to mem pool and relay
1853 if (myAddtomempool(notarizationTx))
1854 {
1855 RelayTransaction(notarizationTx);
1856 }
1857 }
1858 else
a420f7d8 1859 {
5e34790a 1860 printf("%s: (PII) error adding notary evidence: %s\n", __func__, buildResult.GetError().c_str());
1861 LogPrintf("%s: (PII) error adding notary evidence: %s\n", __func__, buildResult.GetError().c_str());
a420f7d8 1862 }
1863 }
9abe77b7 1864 }
1865 }
1866
c8c684e9 1867 if (notaryConnected)
1868 {
1869 // if we should make an earned notarization, do so
ac07d7a9 1870 if (nHeight != 1 && !(VERUS_NOTARYID.IsNull() && VERUS_DEFAULTID.IsNull() && VERUS_NODEID.IsNull()))
4fa3b13d 1871 {
ac07d7a9 1872 CIdentityID proposer = VERUS_NOTARYID.IsNull() ? (VERUS_DEFAULTID.IsNull() ? VERUS_NODEID : VERUS_DEFAULTID) : VERUS_NOTARYID;
c8c684e9 1873
41f170fd 1874 // if we have access to our notary daemon
56fe75cb 1875 // create a notarization if we would qualify to do so. add it to the mempool and next block
4fa3b13d 1876 ChainMerkleMountainView mmv = chainActive.GetMMV();
8577896f 1877 mmrRoot = mmv.GetRoot();
1fa4454d
MT
1878 int32_t confirmedInput = -1;
1879 CTxDestination confirmedDest;
c8c684e9 1880 CValidationState state;
1881 CPBaaSNotarization earnedNotarization;
1882
1883 if (CPBaaSNotarization::CreateEarnedNotarization(ConnectedChains.FirstNotaryChain(),
1884 DestinationToTransferDestination(proposer),
1885 state,
1886 coinbaseTx.vout,
1887 earnedNotarization))
4fa3b13d 1888 {
833b3007 1889 }
c8c684e9 1890 CPBaaSNotarization lastImportNotarization;
1891 CUTXORef lastImportNotarizationUTXO;
9abe77b7 1892
1893 CPBaaSNotarization::SubmitFinalizedNotarizations(ConnectedChains.FirstNotaryChain(), state);
c8c684e9 1894 ProcessNewImports(ConnectedChains.FirstNotaryChain().chainDefinition.GetID(), lastImportNotarization, lastImportNotarizationUTXO, nHeight);
4fa3b13d 1895 }
2299bd95 1896 }
855714b0 1897
a420f7d8 1898 // done calling out, take locks for the rest
1899 LOCK2(cs_main, mempool.cs);
1900
c8c684e9 1901 totalEmission = GetBlockSubsidy(nHeight, consensusParams);
1902 blockSubsidy = totalEmission;
24d12992 1903
1904 // PBaaS chain's block 1 currency state is done by the time we get here,
1905 // including pre-allocations, etc.
1906 if (isVerusActive || nHeight != 1)
1907 {
1908 currencyState.UpdateWithEmission(totalEmission);
1909 }
c8c684e9 1910
1911 // process any imports from the current chain to itself
1912 ConnectedChains.ProcessLocalImports();
855714b0 1913
1914 CFeePool feePool;
c8c684e9 1915 if (!CFeePool::GetCoinbaseFeePool(feePool, nHeight - 1) ||
855714b0 1916 (!feePool.IsValid() && CConstVerusSolutionVector::GetVersionByHeight(nHeight - 1) >= CActivationHeight::ACTIVATE_PBAAS))
1917 {
1918 // we should be able to get a valid currency state, if not, fail
1919 LogPrintf("Failure to get fee pool information for blockchain height #%d\n", nHeight - 1);
1920 printf("Failure to get fee pool information for blockchain height #%d\n", nHeight - 1);
1921 return NULL;
34d1aa13 1922 }
e7e14f44 1923
855714b0 1924 uint32_t solutionVersion = CConstVerusSolutionVector::GetVersionByHeight(nHeight);
1925 if (solutionVersion >= CActivationHeight::ACTIVATE_PBAAS && !feePool.IsValid())
1926 {
1927 // first block with a fee pool, so make it valid and empty
1928 feePool = CFeePool();
1929 }
a041b57e 1930
c8c684e9 1931 // coinbase should have all necessary outputs
41f170fd
MT
1932 uint32_t nCoinbaseSize = GetSerializeSize(coinbaseTx, SER_NETWORK, PROTOCOL_VERSION);
1933 nBlockSize += nCoinbaseSize;
e7c700b5 1934
41f170fd
MT
1935 // now create the priority array, including market order reserve transactions, since they can always execute, leave limits for later
1936 bool haveReserveTransactions = false;
1937 uint32_t reserveExchangeLimitSize = 0;
56fe75cb 1938 std::vector<CReserveTransactionDescriptor> limitOrders;
2299bd95 1939
41f170fd 1940 // now add transactions from the mem pool to the priority heap
e328fa32 1941 for (CTxMemPool::indexed_transaction_set::iterator mi = mempool.mapTx.begin();
4d707d51 1942 mi != mempool.mapTx.end(); ++mi)
d247a5d1 1943 {
e328fa32 1944 const CTransaction& tx = mi->GetTx();
41f170fd 1945 uint256 hash = tx.GetHash();
e9e70b95 1946
a1d3c6fb 1947 int64_t nLockTimeCutoff = (STANDARD_LOCKTIME_VERIFY_FLAGS & LOCKTIME_MEDIAN_TIME_PAST)
e9e70b95 1948 ? nMedianTimePast
1949 : pblock->GetBlockTime();
9c034267 1950
9bb37bf0 1951 if (tx.IsCoinBase() || !IsFinalTx(tx, nHeight, nLockTimeCutoff) || IsExpiredTx(tx, nHeight))
61f8caf2 1952 {
51376f3c 1953 //fprintf(stderr,"coinbase.%d finaltx.%d expired.%d\n",tx.IsCoinBase(),IsFinalTx(tx, nHeight, nLockTimeCutoff),IsExpiredTx(tx, nHeight));
14aa6cc0 1954 continue;
61f8caf2 1955 }
9c034267 1956
161f617d 1957 if ( ASSETCHAINS_SYMBOL[0] == 0 && komodo_validate_interest(tx,nHeight,(uint32_t)pblock->nTime,0) < 0 )
6ff77181 1958 {
64b45b71 1959 //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 1960 continue;
14aa6cc0 1961 }
df756d24 1962
d247a5d1
JG
1963 COrphan* porphan = NULL;
1964 double dPriority = 0;
a372168e 1965 CAmount nTotalIn = 0;
56fe75cb 1966 CCurrencyValueMap totalReserveIn;
d247a5d1 1967 bool fMissingInputs = false;
41f170fd
MT
1968 CReserveTransactionDescriptor rtxd;
1969 bool isReserve = mempool.IsKnownReserveTransaction(hash, rtxd);
e7e14f44 1970
0cb91a8d 1971 if (tx.IsCoinImport())
d247a5d1 1972 {
0cb91a8d
SS
1973 CAmount nValueIn = GetCoinImportValue(tx);
1974 nTotalIn += nValueIn;
1975 dPriority += (double)nValueIn * 1000; // flat multiplier
1976 } else {
41f170fd
MT
1977 if (isReserve)
1978 {
1979 nTotalIn += rtxd.nativeIn;
24d12992 1980 totalReserveIn = rtxd.ReserveInputMap();
1981 assert(!totalReserveIn.valueMap.count(ASSETCHAINS_CHAINID));
f711aed7 1982 if (rtxd.IsIdentity() && CNameReservation(tx).IsValid())
1983 {
1984 nCurrentIDSize += GetSerializeSize(tx, SER_NETWORK, PROTOCOL_VERSION);
1985 if (nCurrentIDSize > nMaxIDSize)
1986 {
1987 continue;
1988 }
1989 }
41f170fd 1990 }
0cb91a8d 1991 BOOST_FOREACH(const CTxIn& txin, tx.vin)
d247a5d1 1992 {
56fe75cb 1993 CAmount nValueIn = 0;
1994 CCurrencyValueMap reserveValueIn;
41f170fd 1995
0cb91a8d
SS
1996 // Read prev transaction
1997 if (!view.HaveCoins(txin.prevout.hash))
d247a5d1 1998 {
0cb91a8d
SS
1999 // This should never happen; all transactions in the memory
2000 // pool should connect to either transactions in the chain
2001 // or other transactions in the memory pool.
2002 if (!mempool.mapTx.count(txin.prevout.hash))
2003 {
2004 LogPrintf("ERROR: mempool transaction missing input\n");
2005 if (fDebug) assert("mempool transaction missing input" == 0);
2006 fMissingInputs = true;
2007 if (porphan)
2008 vOrphan.pop_back();
2009 break;
2010 }
2011
2012 // Has to wait for dependencies
2013 if (!porphan)
2014 {
2015 // Use list for automatic deletion
2016 vOrphan.push_back(COrphan(&tx));
2017 porphan = &vOrphan.back();
2018 }
2019 mapDependers[txin.prevout.hash].push_back(porphan);
2020 porphan->setDependsOn.insert(txin.prevout.hash);
e7e14f44
MT
2021
2022 const CTransaction &otx = mempool.mapTx.find(txin.prevout.hash)->GetTx();
e7e14f44 2023 // consider reserve outputs and set priority according to their value here as well
56fe75cb 2024 if (isReserve)
e7e14f44 2025 {
56fe75cb 2026 totalReserveIn += otx.vout[txin.prevout.n].ReserveOutValue();
e7e14f44 2027 }
56fe75cb 2028 nTotalIn += otx.vout[txin.prevout.n].nValue;
0cb91a8d 2029 continue;
d247a5d1 2030 }
0cb91a8d
SS
2031 const CCoins* coins = view.AccessCoins(txin.prevout.hash);
2032 assert(coins);
2033
41f170fd 2034 if (isReserve)
e7e14f44 2035 {
56fe75cb 2036 reserveValueIn = coins->vout[txin.prevout.n].ReserveOutValue();
e7e14f44 2037 }
0cb91a8d 2038
41f170fd 2039 nValueIn = coins->vout[txin.prevout.n].nValue;
0cb91a8d
SS
2040 int nConf = nHeight - coins->nHeight;
2041
56fe75cb 2042 dPriority += ((double)((reserveValueIn.valueMap.size() ? currencyState.ReserveToNative(reserveValueIn) : 0) + nValueIn)) * nConf;
71a3314d 2043
71a3314d 2044 if (!isReserve)
2045 {
2046 nTotalIn += nValueIn;
56fe75cb 2047 totalReserveIn += reserveValueIn;
71a3314d 2048 }
d247a5d1 2049 }
9feb4b9e 2050 nTotalIn += tx.GetShieldedValueIn();
d247a5d1 2051 }
0cb91a8d 2052
d247a5d1 2053 if (fMissingInputs) continue;
e9e70b95 2054
d6eb2599 2055 // Priority is sum(valuein * age) / modified_txsize
d247a5d1 2056 unsigned int nTxSize = ::GetSerializeSize(tx, SER_NETWORK, PROTOCOL_VERSION);
4d707d51 2057 dPriority = tx.ComputePriority(dPriority, nTxSize);
e9e70b95 2058
56fe75cb 2059 CAmount nDeltaValueIn = nTotalIn + (totalReserveIn.valueMap.size() ? currencyState.ReserveToNative(totalReserveIn) : 0);
41f170fd
MT
2060 CAmount nFeeValueIn = nDeltaValueIn;
2061 mempool.ApplyDeltas(hash, dPriority, nDeltaValueIn);
e7e14f44 2062
71a3314d 2063 CAmount nativeEquivalentOut = 0;
e7e14f44
MT
2064
2065 // if there is reserve in, or this is a reserveexchange transaction, calculate fee properly
56fe75cb 2066 if (isReserve && rtxd.ReserveOutputMap().valueMap.size())
e7e14f44
MT
2067 {
2068 // if this has reserve currency out, convert it to native currency for fee calculation
56fe75cb 2069 nativeEquivalentOut = currencyState.ReserveToNative(rtxd.ReserveOutputMap());
e7e14f44
MT
2070 }
2071
56fe75cb 2072 CFeeRate feeRate(isReserve ? rtxd.AllFeesAsNative(currencyState) + currencyState.ReserveToNative(rtxd.ReserveConversionFeesMap()) + rtxd.nativeConversionFees :
2073 nFeeValueIn - (tx.GetValueOut() + nativeEquivalentOut), nTxSize);
e7e14f44 2074
d247a5d1
JG
2075 if (porphan)
2076 {
2077 porphan->dPriority = dPriority;
c6cb21d1 2078 porphan->feeRate = feeRate;
d247a5d1
JG
2079 }
2080 else
e328fa32 2081 vecPriority.push_back(TxPriority(dPriority, feeRate, &(mi->GetTx())));
d247a5d1 2082 }
df756d24 2083
41f170fd
MT
2084 //
2085 // NOW -- REALLY START TO FILL THE BLOCK
bb6c3482 2086 //
41f170fd 2087 // estimate number of conversions, staking transaction size, and additional coinbase outputs that will be required
e7c700b5 2088
41f170fd 2089 int32_t maxPreLimitOrderBlockSize = nBlockMaxSize - std::min(nBlockMaxSize >> 2, reserveExchangeLimitSize);
e7e14f44 2090
355ca565 2091 int64_t interest;
d247a5d1 2092 bool fSortedByFee = (nBlockPrioritySize <= 0);
41f170fd 2093
d247a5d1
JG
2094 TxPriorityCompare comparer(fSortedByFee);
2095 std::make_heap(vecPriority.begin(), vecPriority.end(), comparer);
41f170fd
MT
2096
2097 std::vector<int> reservePositions;
2098
2099 // now loop and fill the block, leaving space for reserve exchange limit transactions
d247a5d1
JG
2100 while (!vecPriority.empty())
2101 {
2102 // Take highest priority transaction off the priority queue:
2103 double dPriority = vecPriority.front().get<0>();
c6cb21d1 2104 CFeeRate feeRate = vecPriority.front().get<1>();
4d707d51 2105 const CTransaction& tx = *(vecPriority.front().get<2>());
e9e70b95 2106
d247a5d1
JG
2107 std::pop_heap(vecPriority.begin(), vecPriority.end(), comparer);
2108 vecPriority.pop_back();
e9e70b95 2109
d247a5d1
JG
2110 // Size limits
2111 unsigned int nTxSize = ::GetSerializeSize(tx, SER_NETWORK, PROTOCOL_VERSION);
41f170fd 2112 if (nBlockSize + nTxSize >= maxPreLimitOrderBlockSize - autoTxSize) // room for extra autotx
61f8caf2 2113 {
41f170fd 2114 //fprintf(stderr,"nBlockSize %d + %d nTxSize >= %d maxPreLimitOrderBlockSize\n",(int32_t)nBlockSize,(int32_t)nTxSize,(int32_t)maxPreLimitOrderBlockSize);
d247a5d1 2115 continue;
61f8caf2 2116 }
e9e70b95 2117
d247a5d1
JG
2118 // Legacy limits on sigOps:
2119 unsigned int nTxSigOps = GetLegacySigOpCount(tx);
a4a40a38 2120 if (nBlockSigOps + nTxSigOps >= MAX_BLOCK_SIGOPS-1)
61f8caf2 2121 {
51376f3c 2122 //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 2123 continue;
61f8caf2 2124 }
d247a5d1 2125 // Skip free transactions if we're past the minimum block size:
805344dc 2126 const uint256& hash = tx.GetHash();
2a72d459 2127 double dPriorityDelta = 0;
a372168e 2128 CAmount nFeeDelta = 0;
2a72d459 2129 mempool.ApplyDeltas(hash, dPriorityDelta, nFeeDelta);
13fc83c7 2130 if (fSortedByFee && (dPriorityDelta <= 0) && (nFeeDelta <= 0) && (feeRate < ::minRelayTxFee) && (nBlockSize + nTxSize >= nBlockMinSize))
61f8caf2 2131 {
51376f3c 2132 //fprintf(stderr,"fee rate skip\n");
d247a5d1 2133 continue;
61f8caf2 2134 }
41f170fd 2135
2a72d459 2136 // Prioritise by fee once past the priority size or we run out of high-priority
d247a5d1
JG
2137 // transactions:
2138 if (!fSortedByFee &&
2139 ((nBlockSize + nTxSize >= nBlockPrioritySize) || !AllowFree(dPriority)))
2140 {
2141 fSortedByFee = true;
2142 comparer = TxPriorityCompare(fSortedByFee);
2143 std::make_heap(vecPriority.begin(), vecPriority.end(), comparer);
2144 }
e9e70b95 2145
d247a5d1 2146 if (!view.HaveInputs(tx))
61f8caf2 2147 {
51376f3c 2148 //fprintf(stderr,"dont have inputs\n");
d247a5d1 2149 continue;
61f8caf2 2150 }
41f170fd
MT
2151 CAmount nTxFees;
2152 CReserveTransactionDescriptor txDesc;
2153 bool isReserve = mempool.IsKnownReserveTransaction(hash, txDesc);
2154
a8419988 2155 nTxFees = view.GetValueIn(chainActive.LastTip()->GetHeight(),&interest,tx,chainActive.LastTip()->nTime) - tx.GetValueOut();
e9e70b95 2156
d247a5d1 2157 nTxSigOps += GetP2SHSigOpCount(tx, view);
a4a40a38 2158 if (nBlockSigOps + nTxSigOps >= MAX_BLOCK_SIGOPS-1)
61f8caf2 2159 {
51376f3c 2160 //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 2161 continue;
61f8caf2 2162 }
41f170fd 2163
68f7d1d7
PT
2164 // Note that flags: we don't want to set mempool/IsStandard()
2165 // policy here, but we still have to ensure that the block we
2166 // create only contains transactions that are valid in new blocks.
d247a5d1 2167 CValidationState state;
6514771a 2168 PrecomputedTransactionData txdata(tx);
b76f691a 2169 if (!ContextualCheckInputs(tx, state, view, nHeight, true, MANDATORY_SCRIPT_VERIFY_FLAGS, true, txdata, Params().GetConsensus(), consensusBranchId))
61f8caf2 2170 {
51376f3c 2171 //fprintf(stderr,"context failure\n");
d247a5d1 2172 continue;
61f8caf2 2173 }
e7e14f44 2174
8cb98d91 2175 UpdateCoins(tx, view, nHeight);
d247a5d1 2176
41f170fd
MT
2177 if (isReserve)
2178 {
2179 reservePositions.push_back(nBlockTx);
2180 haveReserveTransactions = true;
efcc01c5 2181 additionalFees += txDesc.ReserveFees();
41f170fd
MT
2182 }
2183
31a04d28
SB
2184 BOOST_FOREACH(const OutputDescription &outDescription, tx.vShieldedOutput) {
2185 sapling_tree.append(outDescription.cm);
2186 }
2187
d247a5d1
JG
2188 // Added
2189 pblock->vtx.push_back(tx);
2190 pblocktemplate->vTxFees.push_back(nTxFees);
2191 pblocktemplate->vTxSigOps.push_back(nTxSigOps);
2192 nBlockSize += nTxSize;
2193 ++nBlockTx;
2194 nBlockSigOps += nTxSigOps;
2195 nFees += nTxFees;
d247a5d1
JG
2196 if (fPrintPriority)
2197 {
3f0813b3 2198 LogPrintf("priority %.1f fee %s txid %s\n",dPriority, feeRate.ToString(), tx.GetHash().ToString());
d247a5d1 2199 }
e9e70b95 2200
d247a5d1
JG
2201 // Add transactions that depend on this one to the priority queue
2202 if (mapDependers.count(hash))
2203 {
2204 BOOST_FOREACH(COrphan* porphan, mapDependers[hash])
2205 {
2206 if (!porphan->setDependsOn.empty())
2207 {
2208 porphan->setDependsOn.erase(hash);
2209 if (porphan->setDependsOn.empty())
2210 {
c6cb21d1 2211 vecPriority.push_back(TxPriority(porphan->dPriority, porphan->feeRate, porphan->ptx));
d247a5d1
JG
2212 std::push_heap(vecPriority.begin(), vecPriority.end(), comparer);
2213 }
2214 }
2215 }
2216 }
2217 }
135fa24e 2218
05ece4c3 2219 // first calculate and distribute block rewards, including fees in the minerOutputs vector
88bc6df5 2220 CAmount rewardTotalShareAmount = 0;
855714b0 2221 CAmount rewardFees = nFees;
24d12992 2222 if (additionalFees.valueMap.count(thisChainID))
2223 {
2224 rewardFees += additionalFees.valueMap[thisChainID];
2225 additionalFees.valueMap.erase(thisChainID);
2226 }
2227
2228 CAmount verusFees = 0;
efcc01c5 2229 if (VERUS_CHAINID != ASSETCHAINS_CHAINID && additionalFees.valueMap.count(VERUS_CHAINID))
24d12992 2230 {
2231 verusFees += additionalFees.valueMap[VERUS_CHAINID];
2232 additionalFees.valueMap.erase(VERUS_CHAINID);
2233 }
2234
2235 if (additionalFees.valueMap.size())
2236 {
2237 printf("%s: burning reserve currency: %s\n", __func__, additionalFees.ToUniValue().write(1,2).c_str());
2238 }
2239
855714b0 2240 if (feePool.IsValid())
2241 {
c31944d3 2242 // we support only the current native currency or VRSC on PBaaS chains in the fee pool for now
24d12992 2243 feePool.reserveValues.valueMap[thisChainID] += rewardFees;
2244 if (verusFees)
2245 {
2246 feePool.reserveValues.valueMap[VERUS_CHAINID] += verusFees;
2247 }
2248 CFeePool oneFeeShare = feePool.OneFeeShare();
2249 rewardFees = oneFeeShare.reserveValues.valueMap[thisChainID];
855714b0 2250 feePool.reserveValues.valueMap[thisChainID] -= rewardFees;
2251
efcc01c5 2252 if (VERUS_CHAINID != ASSETCHAINS_CHAINID && oneFeeShare.reserveValues.valueMap.count(VERUS_CHAINID))
24d12992 2253 {
2254 verusFees = oneFeeShare.reserveValues.valueMap[VERUS_CHAINID];
2255 feePool.reserveValues.valueMap[VERUS_CHAINID] -= verusFees;
2256 }
2257
855714b0 2258 cp = CCinit(&CC, EVAL_FEE_POOL);
2259 pkCC = CPubKey(ParseHex(CC.CChexstr));
2260 coinbaseTx.vout.push_back(CTxOut(0,MakeMofNCCScript(CConditionObj<CFeePool>(EVAL_FEE_POOL,{pkCC.GetID()},1,&feePool))));
2261 }
efcc01c5 2262
2263 // printf("%s: rewardfees: %ld, verusfees: %ld\n", __func__, rewardFees, verusFees);
2264
c8c684e9 2265 CAmount rewardTotal = blockSubsidy + rewardFees;
41f170fd 2266
56fe75cb 2267 // now that we have the total reward, update the coinbase outputs
2268 if (isStake)
06f41160 2269 {
6e54f125 2270 // TODO: need to add reserve output to stake coinbase to prevent burning of VRSC
24d12992 2271 coinbaseTx.vout[0].nValue = rewardTotal;
88bc6df5 2272 }
56fe75cb 2273 else
88bc6df5 2274 {
56fe75cb 2275 for (auto &outputShare : minerOutputs)
06f41160 2276 {
855714b0 2277 rewardTotalShareAmount += outputShare.nValue;
88bc6df5 2278 }
41f170fd 2279
56fe75cb 2280 int cbOutIdx;
24d12992 2281 CAmount rewardLeft = rewardTotal;
2282 CAmount verusFeeLeft = verusFees;
56fe75cb 2283 for (cbOutIdx = 0; cbOutIdx < minerOutputs.size(); cbOutIdx++)
2284 {
855714b0 2285 CAmount amount = (arith_uint256(rewardTotal) * arith_uint256(minerOutputs[cbOutIdx].nValue) / arith_uint256(rewardTotalShareAmount)).GetLow64();
56fe75cb 2286 if (rewardLeft <= amount || (cbOutIdx + 1) == minerOutputs.size())
2287 {
2288 amount = rewardLeft;
2289 }
2290 rewardLeft -= amount;
24d12992 2291
2292 // now make outputs for non-native, VRSC fees
2293 if (verusFeeLeft)
2294 {
6e54f125 2295 CAmount verusFee = (arith_uint256(verusFees) * arith_uint256(minerOutputs[cbOutIdx].nValue) / arith_uint256(rewardTotalShareAmount)).GetLow64();
2296 if (verusFeeLeft <= verusFee || (cbOutIdx + 1) == minerOutputs.size())
24d12992 2297 {
6e54f125 2298 verusFee = verusFeeLeft;
24d12992 2299 }
2300 CTxDestination minerDestination;
2301 if (verusFee >= CFeePool::MIN_SHARE_SIZE && ExtractDestination(coinbaseTx.vout[cbOutIdx].scriptPubKey, minerDestination))
2302 {
2303 CTokenOutput to = CTokenOutput(VERUS_CHAINID, verusFee);
2304 coinbaseTx.vout[cbOutIdx].scriptPubKey = MakeMofNCCScript(CConditionObj<CTokenOutput>(EVAL_RESERVE_OUTPUT,
2305 std::vector<CTxDestination>({minerDestination}),
2306 1,
2307 &to));
2308 }
2309 verusFeeLeft -= verusFee;
2310 }
2311
2312 // we had to wait to update this here to ensure it represented a correct distribution ratio
56fe75cb 2313 coinbaseTx.vout[cbOutIdx].nValue = amount;
56fe75cb 2314 }
88bc6df5
MT
2315 }
2316
88bc6df5
MT
2317 nLastBlockTx = nBlockTx;
2318 nLastBlockSize = nBlockSize;
2319
2320 blocktime = std::max(pindexPrev->GetMedianTimePast(), GetAdjustedTime());
2321
2322 pblock->nBits = GetNextWorkRequired(pindexPrev, pblock, Params().GetConsensus());
2323
41f170fd 2324 coinbaseTx.nExpiryHeight = 0;
88bc6df5 2325 coinbaseTx.nLockTime = blocktime;
abb90a89 2326
ebee7b5b 2327 // finalize input of coinbase
41f170fd
MT
2328 coinbaseTx.vin[0].scriptSig = (CScript() << nHeight << CScriptNum(0)) + COINBASE_FLAGS;
2329 assert(coinbaseTx.vin[0].scriptSig.size() <= 100);
ebee7b5b 2330
88bc6df5
MT
2331 // coinbase is done
2332 pblock->vtx[0] = coinbaseTx;
2333 uint256 cbHash = coinbaseTx.GetHash();
ebee7b5b 2334
a9c8cca3 2335 // display it at block 1 for PBaaS debugging
2d9b4d0e 2336 /* if (nHeight == 1)
a9c8cca3 2337 {
2338 UniValue jsonTxOut(UniValue::VOBJ);
2339 TxToUniv(coinbaseTx, uint256(), jsonTxOut);
2340 printf("%s: new coinbase transaction: %s\n", __func__, jsonTxOut.write(1,2).c_str());
2d9b4d0e 2341 } */
a9c8cca3 2342
88bc6df5
MT
2343 // if there is a stake transaction, add it to the very end
2344 if (isStake)
2345 {
2346 UpdateCoins(txStaked, view, nHeight);
2347 pblock->vtx.push_back(txStaked);
2348 pblocktemplate->vTxFees.push_back(0);
2349 int txSigOps = GetLegacySigOpCount(txStaked);
2350 pblocktemplate->vTxSigOps.push_back(txSigOps);
2351 // already added to the block size above
2352 ++nBlockTx;
2353 nBlockSigOps += txSigOps;
2354 }
68b309c0 2355
88bc6df5 2356 extern CWallet *pwalletMain;
8577896f 2357
41f170fd 2358 pblock->vtx[0] = coinbaseTx;
d247a5d1 2359 pblocktemplate->vTxFees[0] = -nFees;
88bc6df5 2360 pblocktemplate->vTxSigOps[0] = GetLegacySigOpCount(pblock->vtx[0]);
48d800c2 2361
1fae37f6
MT
2362 // if not Verus stake, setup nonce, otherwise, leave it alone
2363 if (!isStake || ASSETCHAINS_LWMAPOS == 0)
2364 {
eb0a6550 2365 // Randomize nonce
1fae37f6 2366 arith_uint256 nonce = UintToArith256(GetRandHash());
48d800c2 2367
1fae37f6
MT
2368 // Clear the top 16 and bottom 16 or 24 bits (for local use as thread flags and counters)
2369 nonce <<= ASSETCHAINS_NONCESHIFT[ASSETCHAINS_ALGO];
2370 nonce >>= 16;
2371 pblock->nNonce = ArithToUint256(nonce);
2372 }
e9e70b95 2373
d247a5d1
JG
2374 // Fill in header
2375 pblock->hashPrevBlock = pindexPrev->GetBlockHash();
31a04d28 2376 pblock->hashFinalSaplingRoot = sapling_tree.root();
0c8fa56a
MT
2377
2378 // all Verus PoS chains need this data in the block at all times
2379 if ( ASSETCHAINS_LWMAPOS || ASSETCHAINS_SYMBOL[0] == 0 || ASSETCHAINS_STAKED == 0 || KOMODO_MININGTHREADS > 0 )
9a0f2798 2380 {
2381 UpdateTime(pblock, Params().GetConsensus(), pindexPrev);
1fae37f6 2382 pblock->nBits = GetNextWorkRequired(pindexPrev, pblock, Params().GetConsensus());
9a0f2798 2383 }
12217420 2384
4d068367 2385 if ( ASSETCHAINS_SYMBOL[0] == 0 && IS_KOMODO_NOTARY != 0 && My_notaryid >= 0 )
af805d53 2386 {
28a62b60 2387 uint32_t r;
496f1fd2 2388 CMutableTransaction txNotary = CreateNewContextualCMutableTransaction(Params().GetConsensus(), chainActive.Height() + 1);
fa04bcf3 2389 if ( pblock->nTime < pindexPrev->nTime+60 )
2390 pblock->nTime = pindexPrev->nTime + 60;
855714b0 2391
a893e994 2392 if ( komodo_notaryvin(txNotary,NOTARY_PUBKEY33) > 0 )
496f1fd2 2393 {
2d79309f 2394 CAmount txfees = 5000;
496f1fd2 2395 pblock->vtx.push_back(txNotary);
2396 pblocktemplate->vTxFees.push_back(txfees);
2397 pblocktemplate->vTxSigOps.push_back(GetLegacySigOpCount(txNotary));
2398 nFees += txfees;
2d79309f 2399 pblocktemplate->vTxFees[0] = -nFees;
c881e52b 2400 //*(uint64_t *)(&pblock->vtx[0].vout[0].nValue) += txfees;
f31815fc 2401 //fprintf(stderr,"added notaryvin\n");
0857c3d5 2402 }
2403 else
2404 {
2405 fprintf(stderr,"error adding notaryvin, need to create 0.0001 utxos\n");
2406 return(0);
2407 }
707b061c 2408 }
809f2e25 2409 else if ( ASSETCHAINS_CC == 0 && pindexPrev != 0 && ASSETCHAINS_STAKED == 0 && (ASSETCHAINS_SYMBOL[0] != 0 || IS_KOMODO_NOTARY == 0 || My_notaryid < 0) )
af805d53 2410 {
8fc79ac9 2411 CValidationState state;
809f2e25 2412 //fprintf(stderr,"check validity\n");
e6c5e0b8 2413 if (!TestBlockValidity(state, chainparams, *pblock, pindexPrev, false, false)) // invokes CC checks
8fc79ac9 2414 {
9feb4b9e 2415 throw std::runtime_error("CreateNewBlock(): TestBlockValidity failed");
8fc79ac9 2416 }
809f2e25 2417 //fprintf(stderr,"valid\n");
af805d53 2418 }
d247a5d1 2419 }
2a6a442a 2420 //fprintf(stderr,"done new block\n");
1685bba0
MT
2421
2422 // setup the header and buid the Merkle tree
2423 unsigned int extraNonce;
88d014d0 2424 IncrementExtraNonce(pblock, pindexPrev, extraNonce, true);
1685bba0 2425
d247a5d1
JG
2426 return pblocktemplate.release();
2427}
1db7bdb7 2428
855714b0 2429CBlockTemplate* CreateNewBlock(const CChainParams& chainparams, const CScript& _scriptPubKeyIn, bool isStake)
2430{
2431 std::vector<CTxOut> minerOutputs = _scriptPubKeyIn.size() ? std::vector<CTxOut>({CTxOut(1, _scriptPubKeyIn)}) : std::vector<CTxOut>();
2432 return CreateNewBlock(chainparams, minerOutputs, isStake);
2433}
acfa0333 2434
c1de826f
JG
2435//////////////////////////////////////////////////////////////////////////////
2436//
2437// Internal miner
2438//
2439
2cc0a252 2440#ifdef ENABLE_MINING
c1de826f 2441
88d014d0 2442class MinerAddressScript : public CReserveScript
2443{
2444 // CReserveScript requires implementing this function, so that if an
2445 // internal (not-visible) wallet address is used, the wallet can mark it as
2446 // important when a block is mined (so it then appears to the user).
2447 // If -mineraddress is set, the user already knows about and is managing the
2448 // address, so we don't need to do anything here.
2449 void KeepScript() {}
2450};
2451
2452void GetScriptForMinerAddress(boost::shared_ptr<CReserveScript> &script)
2453{
2454 CTxDestination addr = DecodeDestination(GetArg("-mineraddress", ""));
2455 if (!IsValidDestination(addr)) {
2456 return;
2457 }
2458
2459 boost::shared_ptr<MinerAddressScript> mAddr(new MinerAddressScript());
88d014d0 2460 script = mAddr;
fa6c618c 2461 script->reserveScript = GetScriptForDestination(addr);
88d014d0 2462}
2463
4a85e067 2464#ifdef ENABLE_WALLET
acfa0333
WL
2465//////////////////////////////////////////////////////////////////////////////
2466//
2467// Internal miner
2468//
acfa0333 2469
855714b0 2470CBlockTemplate* CreateNewBlockWithKey(CReserveKey& reservekey, int32_t nHeight, bool isStake)
acfa0333 2471{
fa6c618c 2472 CPubKey pubkey;
2473 CScript scriptPubKey;
2474 uint8_t *ptr;
2475 int32_t i;
2476 boost::shared_ptr<CReserveScript> coinbaseScript;
2477
d9f176ac 2478 if ( nHeight == 1 && ASSETCHAINS_OVERRIDE_PUBKEY33[0] != 0 )
2479 {
2480 scriptPubKey = CScript() << ParseHex(ASSETCHAINS_OVERRIDE_PUBKEY) << OP_CHECKSIG;
2481 }
2482 else if ( USE_EXTERNAL_PUBKEY != 0 )
998397aa 2483 {
7bfc207a 2484 //fprintf(stderr,"use notary pubkey\n");
c95fd5e0 2485 scriptPubKey = CScript() << ParseHex(NOTARY_PUBKEY) << OP_CHECKSIG;
f6c647ed 2486 }
fa6c618c 2487 else if (GetArg("-mineraddress", "").empty() || !(GetScriptForMinerAddress(coinbaseScript), (scriptPubKey = coinbaseScript->reserveScript).size()))
f6c647ed 2488 {
f1f6dfbb 2489 if (!isStake)
1b5b89ba 2490 {
f1f6dfbb 2491 if (!reservekey.GetReservedKey(pubkey))
2492 {
2493 return NULL;
2494 }
855714b0 2495 scriptPubKey = GetScriptForDestination(pubkey);
1b5b89ba 2496 }
f6c647ed 2497 }
855714b0 2498 return CreateNewBlock(Params(), scriptPubKey, isStake);
acfa0333
WL
2499}
2500
88d014d0 2501void komodo_broadcast(const CBlock *pblock,int32_t limit)
395f10cf 2502{
2503 int32_t n = 1;
2504 //fprintf(stderr,"broadcast new block t.%u\n",(uint32_t)time(NULL));
2505 {
2506 LOCK(cs_vNodes);
2507 BOOST_FOREACH(CNode* pnode, vNodes)
2508 {
2509 if ( pnode->hSocket == INVALID_SOCKET )
2510 continue;
2511 if ( (rand() % n) == 0 )
2512 {
2513 pnode->PushMessage("block", *pblock);
2514 if ( n++ > limit )
2515 break;
2516 }
2517 }
2518 }
2519 //fprintf(stderr,"finished broadcast new block t.%u\n",(uint32_t)time(NULL));
2520}
945f015d 2521
269d8ba0 2522static bool ProcessBlockFound(CBlock* pblock, CWallet& wallet, CReserveKey& reservekey)
8e8b6d70
JG
2523#else
2524static bool ProcessBlockFound(CBlock* pblock)
2525#endif // ENABLE_WALLET
d247a5d1 2526{
572c763f 2527 int32_t height = chainActive.LastTip()->GetHeight()+1;
0ab273d2 2528 //LogPrintf("%s\n", pblock->ToString());
572c763f 2529 LogPrintf("generated %s height.%d\n", FormatMoney(pblock->vtx[0].vout[0].nValue), height);
af521e42 2530
d247a5d1
JG
2531 // Found a solution
2532 {
86131275 2533 if (pblock->hashPrevBlock != chainActive.LastTip()->GetBlockHash())
ba8419c7 2534 {
2535 uint256 hash; int32_t i;
2536 hash = pblock->hashPrevBlock;
92266e99 2537 for (i=31; i>=0; i--)
ba8419c7 2538 fprintf(stderr,"%02x",((uint8_t *)&hash)[i]);
c0dbb034 2539 fprintf(stderr," <- prev (stale)\n");
86131275 2540 hash = chainActive.LastTip()->GetBlockHash();
92266e99 2541 for (i=31; i>=0; i--)
ba8419c7 2542 fprintf(stderr,"%02x",((uint8_t *)&hash)[i]);
c0dbb034 2543 fprintf(stderr," <- chainTip (stale)\n");
e9e70b95 2544
ffde1589 2545 return error("VerusMiner: generated block is stale");
ba8419c7 2546 }
18e72167 2547 }
e9e70b95 2548
8e8b6d70 2549#ifdef ENABLE_WALLET
18e72167 2550 // Remove key from key pool
998397aa 2551 if ( IS_KOMODO_NOTARY == 0 )
945f015d 2552 {
2553 if (GetArg("-mineraddress", "").empty()) {
2554 // Remove key from key pool
2555 reservekey.KeepKey();
2556 }
8e8b6d70 2557 }
18e72167 2558 // Track how many getdata requests this block gets
438ba9c1 2559 //if ( 0 )
18e72167 2560 {
d1bc3a75 2561 //fprintf(stderr,"lock cs_wallet\n");
18e72167
PW
2562 LOCK(wallet.cs_wallet);
2563 wallet.mapRequestCount[pblock->GetHash()] = 0;
d247a5d1 2564 }
8e8b6d70 2565#endif
d1bc3a75 2566 //fprintf(stderr,"process new block\n");
194ad5b8 2567
c871d47f 2568 // Process this block (almost) the same as if we had received it from another node
18e72167 2569 CValidationState state;
88d014d0 2570 if (!ProcessNewBlock(1, chainActive.LastTip()->GetHeight()+1, state, Params(), NULL, pblock, true, NULL))
ffde1589 2571 return error("VerusMiner: ProcessNewBlock, block not accepted");
e9e70b95 2572
d793f94b 2573 TrackMinedBlock(pblock->GetHash());
395f10cf 2574 komodo_broadcast(pblock,16);
d247a5d1
JG
2575 return true;
2576}
2577
078f6af1 2578int32_t komodo_baseid(char *origbase);
a30dd993 2579int32_t komodo_eligiblenotary(uint8_t pubkeys[66][33],int32_t *mids,uint32_t *blocktimes,int32_t *nonzpkeysp,int32_t height);
13691369 2580arith_uint256 komodo_PoWtarget(int32_t *percPoSp,arith_uint256 target,int32_t height,int32_t goalperc);
8ee93080 2581int32_t FOUND_BLOCK,KOMODO_MAYBEMINED;
99ba67a0 2582extern int32_t KOMODO_LASTMINED,KOMODO_INSYNC;
8b51b9e4 2583int32_t roundrobin_delay;
18443f69 2584arith_uint256 HASHTarget,HASHTarget_POW;
3363d1c0 2585int32_t komodo_longestchain();
078f6af1 2586
5642c96c 2587// wait for peers to connect
12217420 2588void waitForPeers(const CChainParams &chainparams)
5642c96c 2589{
2590 if (chainparams.MiningRequiresPeers())
2591 {
3da69a31
MT
2592 bool fvNodesEmpty;
2593 {
00a7120e 2594 boost::this_thread::interruption_point();
3da69a31
MT
2595 LOCK(cs_vNodes);
2596 fvNodesEmpty = vNodes.empty();
2597 }
3363d1c0 2598 int longestchain = komodo_longestchain();
2599 int lastlongest = 0;
2600 if (fvNodesEmpty || IsNotInSync() || (longestchain != 0 && longestchain > chainActive.LastTip()->GetHeight()))
3da69a31 2601 {
af2e212d 2602 int loops = 0, blockDiff = 0, newDiff = 0;
2603
3da69a31 2604 do {
64d6048f 2605 if (fvNodesEmpty)
3da69a31 2606 {
69fa3d0e 2607 MilliSleep(1000 + rand() % 4000);
00a7120e 2608 boost::this_thread::interruption_point();
3da69a31
MT
2609 LOCK(cs_vNodes);
2610 fvNodesEmpty = vNodes.empty();
af2e212d 2611 loops = 0;
2612 blockDiff = 0;
3363d1c0 2613 lastlongest = 0;
af2e212d 2614 }
3363d1c0 2615 else if ((newDiff = IsNotInSync()) > 0)
af2e212d 2616 {
2617 if (blockDiff != newDiff)
2618 {
2619 blockDiff = newDiff;
2620 }
2621 else
2622 {
3363d1c0 2623 if (++loops <= 5)
af2e212d 2624 {
2625 MilliSleep(1000);
2626 }
2627 else break;
2628 }
3363d1c0 2629 lastlongest = 0;
2630 }
2631 else if (!fvNodesEmpty && !IsNotInSync() && longestchain > chainActive.LastTip()->GetHeight())
2632 {
2633 // the only thing may be that we are seeing a long chain that we'll never get
2634 // don't wait forever
2635 if (lastlongest == 0)
2636 {
2637 MilliSleep(3000);
2638 lastlongest = longestchain;
2639 }
3da69a31 2640 }
af2e212d 2641 } while (fvNodesEmpty || IsNotInSync());
2603640e 2642 MilliSleep(500 + rand() % 1000);
3da69a31 2643 }
5642c96c 2644 }
2645}
2646
42181656 2647#ifdef ENABLE_WALLET
d7e6718d
MT
2648CBlockIndex *get_chainactive(int32_t height)
2649{
3c40a9a6 2650 if ( chainActive.LastTip() != 0 )
d7e6718d 2651 {
4b729ec5 2652 if ( height <= chainActive.LastTip()->GetHeight() )
3c40a9a6
MT
2653 {
2654 LOCK(cs_main);
d7e6718d 2655 return(chainActive[height]);
3c40a9a6 2656 }
4b729ec5 2657 // else fprintf(stderr,"get_chainactive height %d > active.%d\n",height,chainActive.Tip()->GetHeight());
d7e6718d
MT
2658 }
2659 //fprintf(stderr,"get_chainactive null chainActive.Tip() height %d\n",height);
2660 return(0);
2661}
2662
135fa24e 2663/*
2664 * A separate thread to stake, while the miner threads mine.
2665 */
2666void static VerusStaker(CWallet *pwallet)
2667{
2668 LogPrintf("Verus staker thread started\n");
2669 RenameThread("verus-staker");
2670
2671 const CChainParams& chainparams = Params();
2d02c19e 2672 auto consensusParams = chainparams.GetConsensus();
76f69285 2673 bool isNotaryConnected = ConnectedChains.CheckVerusPBaaSAvailable();
135fa24e 2674
2675 // Each thread has its own key
2676 CReserveKey reservekey(pwallet);
2677
2678 // Each thread has its own counter
2679 unsigned int nExtraNonce = 0;
12217420 2680
135fa24e 2681 uint8_t *script; uint64_t total,checktoshis; int32_t i,j;
2682
4b729ec5 2683 while ( (ASSETCHAIN_INIT == 0 || KOMODO_INITDONE == 0) ) //chainActive.Tip()->GetHeight() != 235300 &&
135fa24e 2684 {
2685 sleep(1);
2686 if ( komodo_baseid(ASSETCHAINS_SYMBOL) < 0 )
2687 break;
2688 }
2689
2690 // try a nice clean peer connection to start
bf9c36f4
MT
2691 CBlockIndex *pindexPrev, *pindexCur;
2692 do {
2693 pindexPrev = chainActive.LastTip();
2694 MilliSleep(5000 + rand() % 5000);
2695 waitForPeers(chainparams);
2696 pindexCur = chainActive.LastTip();
2697 } while (pindexPrev != pindexCur);
c132b91a 2698
135fa24e 2699 try {
0fc0dc56 2700 static int32_t lastStakingHeight = 0;
2701
135fa24e 2702 while (true)
2703 {
135fa24e 2704 waitForPeers(chainparams);
4ca6678c 2705 CBlockIndex* pindexPrev = chainActive.LastTip();
135fa24e 2706
2707 // Create new block
2708 unsigned int nTransactionsUpdatedLast = mempool.GetTransactionsUpdated();
0fc0dc56 2709
4b729ec5 2710 if ( Mining_height != pindexPrev->GetHeight()+1 )
135fa24e 2711 {
4b729ec5 2712 Mining_height = pindexPrev->GetHeight()+1;
135fa24e 2713 Mining_start = (uint32_t)time(NULL);
2714 }
2715
1fae37f6
MT
2716 // Check for stop or if block needs to be rebuilt
2717 boost::this_thread::interruption_point();
2718
135fa24e 2719 // try to stake a block
1fae37f6 2720 CBlockTemplate *ptr = NULL;
31609f35 2721
2722 // get height locally for consistent reporting
2723 int32_t newHeight = Mining_height;
2724
2725 if (newHeight > VERUS_MIN_STAKEAGE)
855714b0 2726 ptr = CreateNewBlockWithKey(reservekey, newHeight, true);
135fa24e 2727
a73ab4b4 2728 // TODO - putting this output here tends to help mitigate announcing a staking height earlier than
2729 // announcing the last block win when we start staking before a block's acceptance has been
2730 // acknowledged by the mining thread - a better solution may be to put the output on the submission
2731 // thread.
31609f35 2732 if ( ptr == 0 && newHeight != lastStakingHeight )
a73ab4b4 2733 {
31609f35 2734 printf("Staking height %d for %s\n", newHeight, ASSETCHAINS_SYMBOL);
a73ab4b4 2735 }
31609f35 2736 lastStakingHeight = newHeight;
a73ab4b4 2737
135fa24e 2738 if ( ptr == 0 )
2739 {
67dceeec 2740 if (newHeight == 1 && (isNotaryConnected = ConnectedChains.IsNotaryAvailable()))
76f69285 2741 {
2742 static int outputCounter;
2743 if (outputCounter++ % 60 == 0)
2744 {
695db088 2745 printf("%s: waiting for confirmation of launch at or after block %u on %s before mining block 1\n", __func__,
2746 (uint32_t)ConnectedChains.ThisChain().startBlock,
76f69285 2747 ConnectedChains.FirstNotaryChain().chainDefinition.name.c_str());
2748 sleep(1);
2749 }
2750 }
1fae37f6 2751 // wait to try another staking block until after the tip moves again
37ad6886 2752 while ( chainActive.LastTip() == pindexPrev )
bab13dd2 2753 MilliSleep(250);
76f69285 2754 if (newHeight == 1)
2755 {
2756 sleep(10);
2757 }
135fa24e 2758 continue;
2759 }
2760
2761 unique_ptr<CBlockTemplate> pblocktemplate(ptr);
2762 if (!pblocktemplate.get())
2763 {
2764 if (GetArg("-mineraddress", "").empty()) {
1fae37f6 2765 LogPrintf("Error in %s staker: Keypool ran out, please call keypoolrefill before restarting the mining thread\n",
135fa24e 2766 ASSETCHAINS_ALGORITHMS[ASSETCHAINS_ALGO]);
2767 } else {
2768 // Should never reach here, because -mineraddress validity is checked in init.cpp
1fae37f6 2769 LogPrintf("Error in %s staker: Invalid %s -mineraddress\n", ASSETCHAINS_ALGORITHMS[ASSETCHAINS_ALGO], ASSETCHAINS_SYMBOL);
135fa24e 2770 }
2771 return;
2772 }
2773
2774 CBlock *pblock = &pblocktemplate->block;
1fae37f6 2775 LogPrintf("Staking with %u transactions in block (%u bytes)\n", pblock->vtx.size(),::GetSerializeSize(*pblock,SER_NETWORK,PROTOCOL_VERSION));
135fa24e 2776 //
2777 // Search
2778 //
1fae37f6
MT
2779 int64_t nStart = GetTime();
2780
1fae37f6
MT
2781 if (vNodes.empty() && chainparams.MiningRequiresPeers())
2782 {
2783 if ( Mining_height > ASSETCHAINS_MINHEIGHT )
2784 {
2785 fprintf(stderr,"no nodes, attempting reconnect\n");
2786 continue;
2787 }
2788 }
2789
2790 if (mempool.GetTransactionsUpdated() != nTransactionsUpdatedLast && GetTime() - nStart > 60)
2791 {
2792 fprintf(stderr,"timeout, retrying\n");
2793 continue;
2794 }
135fa24e 2795
37ad6886 2796 if ( pindexPrev != chainActive.LastTip() )
135fa24e 2797 {
4b729ec5 2798 printf("Block %d added to chain\n", chainActive.LastTip()->GetHeight());
135fa24e 2799 MilliSleep(250);
2800 continue;
2801 }
2802
1fae37f6
MT
2803 int32_t unlockTime = komodo_block_unlocktime(Mining_height);
2804 int64_t subsidy = (int64_t)(pblock->vtx[0].vout[0].nValue);
135fa24e 2805
1fae37f6 2806 uint256 hashTarget = ArithToUint256(arith_uint256().SetCompact(pblock->nBits));
135fa24e 2807
df756d24 2808 pblock->nBits = GetNextWorkRequired(pindexPrev, pblock, consensusParams);
b9956efc 2809
df756d24 2810 UpdateTime(pblock, consensusParams, pindexPrev);
b9956efc 2811
ed47e5ec
MT
2812 if (ProcessBlockFound(pblock, *pwallet, reservekey))
2813 {
2814 LogPrintf("Using %s algorithm:\n", ASSETCHAINS_ALGORITHMS[ASSETCHAINS_ALGO]);
2815 LogPrintf("Staked block found \n hash: %s \ntarget: %s\n", pblock->GetHash().GetHex(), hashTarget.GetHex());
31609f35 2816 printf("Found block %d \n", newHeight);
ed47e5ec
MT
2817 printf("staking reward %.8f %s!\n", (double)subsidy / (double)COIN, ASSETCHAINS_SYMBOL);
2818 arith_uint256 post;
2819 post.SetCompact(pblock->GetVerusPOSTarget());
31609f35 2820
ed47e5ec
MT
2821 CTransaction &sTx = pblock->vtx[pblock->vtx.size()-1];
2822 printf("POS hash: %s \ntarget: %s\n",
31609f35 2823 CTransaction::_GetVerusPOSHash(&(pblock->nNonce),
2824 sTx.vin[0].prevout.hash,
2825 sTx.vin[0].prevout.n,
2826 newHeight,
8d2b052e 2827 chainActive.GetVerusEntropyHash(newHeight),
31609f35 2828 sTx.vout[0].nValue).GetHex().c_str(),
2829 ArithToUint256(post).GetHex().c_str());
2830 if (unlockTime > newHeight && subsidy >= ASSETCHAINS_TIMELOCKGTE)
ed47e5ec
MT
2831 printf("- timelocked until block %i\n", unlockTime);
2832 else
2833 printf("\n");
2834 }
1fae37f6 2835 else
ed47e5ec
MT
2836 {
2837 LogPrintf("Found block rejected at staking height: %d\n", Mining_height);
2838 printf("Found block rejected at staking height: %d\n", Mining_height);
2839 }
135fa24e 2840
1fae37f6
MT
2841 // Check for stop or if block needs to be rebuilt
2842 boost::this_thread::interruption_point();
135fa24e 2843
bf9c36f4 2844 sleep(3);
3da69a31 2845
1fae37f6
MT
2846 // In regression test mode, stop mining after a block is found.
2847 if (chainparams.MineBlocksOnDemand()) {
2848 throw boost::thread_interrupted();
135fa24e 2849 }
2850 }
2851 }
2852 catch (const boost::thread_interrupted&)
2853 {
135fa24e 2854 LogPrintf("VerusStaker terminated\n");
2855 throw;
2856 }
2857 catch (const std::runtime_error &e)
2858 {
135fa24e 2859 LogPrintf("VerusStaker runtime error: %s\n", e.what());
2860 return;
2861 }
135fa24e 2862}
2863
c17dbdee 2864typedef bool (*minefunction)(CBlockHeader &bh, CVerusHashV2bWriter &vhw, uint256 &finalHash, uint256 &target, uint64_t start, uint64_t *count);
2865bool mine_verus_v2(CBlockHeader &bh, CVerusHashV2bWriter &vhw, uint256 &finalHash, uint256 &target, uint64_t start, uint64_t *count);
2866bool mine_verus_v2_port(CBlockHeader &bh, CVerusHashV2bWriter &vhw, uint256 &finalHash, uint256 &target, uint64_t start, uint64_t *count);
fa7fdbc6 2867
42181656 2868void static BitcoinMiner_noeq(CWallet *pwallet)
2869#else
2870void static BitcoinMiner_noeq()
2871#endif
2872{
05f6e633 2873 LogPrintf("%s miner started\n", ASSETCHAINS_ALGORITHMS[ASSETCHAINS_ALGO]);
05f6e633 2874 RenameThread("verushash-miner");
42181656 2875
2876#ifdef ENABLE_WALLET
2877 // Each thread has its own key
2878 CReserveKey reservekey(pwallet);
2879#endif
2880
248084b7 2881 miningTimer.clear();
2882
2910478b 2883 const CChainParams& chainparams = Params();
42181656 2884 // Each thread has its own counter
2885 unsigned int nExtraNonce = 0;
12217420 2886
42181656 2887 uint8_t *script; uint64_t total,checktoshis; int32_t i,j;
2888
4b729ec5 2889 while ( (ASSETCHAIN_INIT == 0 || KOMODO_INITDONE == 0) ) //chainActive.Tip()->GetHeight() != 235300 &&
42181656 2890 {
2891 sleep(1);
2892 if ( komodo_baseid(ASSETCHAINS_SYMBOL) < 0 )
2893 break;
2894 }
9f3e2213 2895
3da69a31
MT
2896 SetThreadPriority(THREAD_PRIORITY_LOWEST);
2897
5642c96c 2898 // try a nice clean peer connection to start
c132b91a 2899 CBlockIndex *pindexPrev, *pindexCur;
9f3e2213 2900 do {
37ad6886 2901 pindexPrev = chainActive.LastTip();
3da69a31 2902 MilliSleep(5000 + rand() % 5000);
bf9c36f4 2903 waitForPeers(chainparams);
37ad6886 2904 pindexCur = chainActive.LastTip();
c132b91a 2905 } while (pindexPrev != pindexCur);
6176a421 2906
a9f18272 2907 // make sure that we have checked for PBaaS availability
2908 ConnectedChains.CheckVerusPBaaSAvailable();
2909
dbe656fe
MT
2910 // this will not stop printing more than once in all cases, but it will allow us to print in all cases
2911 // and print duplicates rarely without having to synchronize
2912 static CBlockIndex *lastChainTipPrinted;
90198f71 2913 static int32_t lastMiningHeight = 0;
9f3e2213 2914
42181656 2915 miningTimer.start();
2916
2917 try {
dbe656fe 2918 printf("Mining %s with %s\n", ASSETCHAINS_SYMBOL, ASSETCHAINS_ALGORITHMS[ASSETCHAINS_ALGO]);
08d46b7f 2919
42181656 2920 while (true)
2921 {
68334c8d 2922 miningTimer.stop();
2923 waitForPeers(chainparams);
dfcf8255 2924
37ad6886 2925 pindexPrev = chainActive.LastTip();
dfcf8255 2926
f8f61a6d 2927 // prevent forking on startup before the diff algorithm kicks in,
2928 // but only for a startup Verus test chain. PBaaS chains have the difficulty inherited from
2929 // their parent
57055854 2930 if (chainparams.MiningRequiresPeers() && ((IsVerusActive() && pindexPrev->GetHeight() < 50) || pindexPrev != chainActive.LastTip()))
dfcf8255
MT
2931 {
2932 do {
37ad6886 2933 pindexPrev = chainActive.LastTip();
2830db29 2934 MilliSleep(2000 + rand() % 2000);
37ad6886 2935 } while (pindexPrev != chainActive.LastTip());
dfcf8255 2936 }
42181656 2937
2938 // Create new block
2939 unsigned int nTransactionsUpdatedLast = mempool.GetTransactionsUpdated();
4b729ec5 2940 if ( Mining_height != pindexPrev->GetHeight()+1 )
42181656 2941 {
4b729ec5 2942 Mining_height = pindexPrev->GetHeight()+1;
90198f71 2943 if (lastMiningHeight != Mining_height)
2944 {
2945 lastMiningHeight = Mining_height;
dc74c06d 2946 printf("Mining %s at height %d\n", ASSETCHAINS_SYMBOL, Mining_height);
90198f71 2947 }
42181656 2948 Mining_start = (uint32_t)time(NULL);
2949 }
2950
dbe656fe 2951 miningTimer.start();
42181656 2952
2953#ifdef ENABLE_WALLET
855714b0 2954 CBlockTemplate *ptr = CreateNewBlockWithKey(reservekey, Mining_height);
42181656 2955#else
2956 CBlockTemplate *ptr = CreateNewBlockWithKey();
2957#endif
2958 if ( ptr == 0 )
2959 {
2960 static uint32_t counter;
f6084562
MT
2961 if ( counter++ % 40 == 0 )
2962 {
2963 if (!IsVerusActive() &&
67dceeec 2964 ConnectedChains.IsNotaryAvailable() &&
76f69285 2965 !ConnectedChains.readyToStart)
f6084562 2966 {
695db088 2967 fprintf(stderr,"waiting for confirmation of launch at or after block %u on %s chain to start\n", (uint32_t)ConnectedChains.ThisChain().startBlock,
c8c684e9 2968 ConnectedChains.FirstNotaryChain().chainDefinition.name.c_str());
f6084562
MT
2969 }
2970 else
2971 {
2972 fprintf(stderr,"Unable to create valid block... will continue to try\n");
2973 }
2974 }
2830db29 2975 MilliSleep(2000);
42181656 2976 continue;
2977 }
dbe656fe 2978
42181656 2979 unique_ptr<CBlockTemplate> pblocktemplate(ptr);
2980 if (!pblocktemplate.get())
2981 {
2982 if (GetArg("-mineraddress", "").empty()) {
05f6e633 2983 LogPrintf("Error in %s miner: Keypool ran out, please call keypoolrefill before restarting the mining thread\n",
2984 ASSETCHAINS_ALGORITHMS[ASSETCHAINS_ALGO]);
42181656 2985 } else {
2986 // Should never reach here, because -mineraddress validity is checked in init.cpp
05f6e633 2987 LogPrintf("Error in %s miner: Invalid %s -mineraddress\n", ASSETCHAINS_ALGORITHMS[ASSETCHAINS_ALGO], ASSETCHAINS_SYMBOL);
42181656 2988 }
02dbe9a3 2989 miningTimer.stop();
248084b7 2990 miningTimer.clear();
42181656 2991 return;
2992 }
2993 CBlock *pblock = &pblocktemplate->block;
f8f61a6d 2994
2995 uint32_t savebits;
2996 bool mergeMining = false;
2997 savebits = pblock->nBits;
2998
1e435b54 2999 uint32_t solutionVersion = CConstVerusSolutionVector::Version(pblock->nSolution);
f37d48ca 3000 if (pblock->nVersion != CBlockHeader::VERUS_V2)
3001 {
3002 // must not be in sync
3003 printf("Mining on incorrect block version.\n");
3004 sleep(2);
3005 continue;
3006 }
d3dfc922 3007 bool verusSolutionPBaaS = solutionVersion >= CActivationHeight::ACTIVATE_PBAAS;
f8f61a6d 3008
c17dbdee 3009 // v2 hash writer with adjustments for the current height
3010 CVerusHashV2bWriter ss2 = CVerusHashV2bWriter(SER_GETHASH, PROTOCOL_VERSION, solutionVersion);
3011
42181656 3012 if ( ASSETCHAINS_SYMBOL[0] != 0 )
3013 {
3014 if ( ASSETCHAINS_REWARD[0] == 0 && !ASSETCHAINS_LASTERA )
3015 {
3016 if ( pblock->vtx.size() == 1 && pblock->vtx[0].vout.size() == 1 && Mining_height > ASSETCHAINS_MINHEIGHT )
3017 {
3018 static uint32_t counter;
3019 if ( counter++ < 10 )
3020 fprintf(stderr,"skip generating %s on-demand block, no tx avail\n",ASSETCHAINS_SYMBOL);
3021 sleep(10);
3022 continue;
3023 } else fprintf(stderr,"%s vouts.%d mining.%d vs %d\n",ASSETCHAINS_SYMBOL,(int32_t)pblock->vtx[0].vout.size(),Mining_height,ASSETCHAINS_MINHEIGHT);
3024 }
3025 }
b2a98c42 3026
c871d47f 3027 // set our easiest target, if V3+, no need to rebuild the merkle tree
f37d48ca 3028 IncrementExtraNonce(pblock, pindexPrev, nExtraNonce, verusSolutionPBaaS ? false : true, &savebits);
b2a98c42
MT
3029
3030 // update PBaaS header
f37d48ca 3031 if (verusSolutionPBaaS)
b2a98c42 3032 {
2fd1f0fb 3033 if (!IsVerusActive() && ConnectedChains.IsVerusPBaaSAvailable())
f8f61a6d 3034 {
b2a98c42 3035
2fd1f0fb 3036 UniValue params(UniValue::VARR);
3037 UniValue error(UniValue::VARR);
3038 params.push_back(EncodeHexBlk(*pblock));
3039 params.push_back(ASSETCHAINS_SYMBOL);
3040 params.push_back(ASSETCHAINS_RPCHOST);
3041 params.push_back(ASSETCHAINS_RPCPORT);
3042 params.push_back(ASSETCHAINS_RPCCREDENTIALS);
3043 try
b2a98c42 3044 {
be17c611 3045 ConnectedChains.lastSubmissionFailed = false;
2fd1f0fb 3046 params = RPCCallRoot("addmergedblock", params);
3047 params = find_value(params, "result");
3048 error = find_value(params, "error");
3049 } catch (std::exception e)
3050 {
c8c684e9 3051 printf("Failed to connect to %s chain\n", ConnectedChains.FirstNotaryChain().chainDefinition.name.c_str());
2fd1f0fb 3052 params = UniValue(e.what());
b2a98c42 3053 }
2fd1f0fb 3054 if (mergeMining = (params.isNull() && error.isNull()))
f8f61a6d 3055 {
c8c684e9 3056 printf("Merge mining %s with %s as the hashing chain\n", ASSETCHAINS_SYMBOL, ConnectedChains.FirstNotaryChain().chainDefinition.name.c_str());
3057 LogPrintf("Merge mining with %s as the hashing chain\n", ConnectedChains.FirstNotaryChain().chainDefinition.name.c_str());
f8f61a6d 3058 }
b2a98c42
MT
3059 }
3060 }
3061
42181656 3062 LogPrintf("Running %s miner with %u transactions in block (%u bytes)\n",ASSETCHAINS_ALGORITHMS[ASSETCHAINS_ALGO],
3063 pblock->vtx.size(),::GetSerializeSize(*pblock,SER_NETWORK,PROTOCOL_VERSION));
3064 //
3065 // Search
3066 //
f8f61a6d 3067 int64_t nStart = GetTime();
42181656 3068
f8f61a6d 3069 arith_uint256 hashTarget = arith_uint256().SetCompact(savebits);
fa7fdbc6 3070 uint256 uintTarget = ArithToUint256(hashTarget);
f8f61a6d 3071 arith_uint256 ourTarget;
3072 ourTarget.SetCompact(pblock->nBits);
3073
42181656 3074 Mining_start = 0;
ef70c5b2 3075
37ad6886 3076 if ( pindexPrev != chainActive.LastTip() )
05f6e633 3077 {
37ad6886 3078 if (lastChainTipPrinted != chainActive.LastTip())
dbe656fe 3079 {
37ad6886 3080 lastChainTipPrinted = chainActive.LastTip();
4b729ec5 3081 printf("Block %d added to chain\n", lastChainTipPrinted->GetHeight());
dbe656fe 3082 }
f8f61a6d 3083 MilliSleep(100);
05f6e633 3084 continue;
3085 }
ef70c5b2 3086
2830db29 3087 uint64_t count;
3088 uint64_t hashesToGo = 0;
3089 uint64_t totalDone = 0;
3090
e29b5dd5 3091 int64_t subsidy = (int64_t)(pblock->vtx[0].vout[0].nValue);
fa7fdbc6 3092 count = ((ASSETCHAINS_NONCEMASK[ASSETCHAINS_ALGO] >> 3) + 1) / ASSETCHAINS_HASHESPERROUND[ASSETCHAINS_ALGO];
db027133 3093 CVerusHashV2 *vh2 = &ss2.GetState();
3b500530 3094 u128 *hashKey;
3095 verusclhasher &vclh = vh2->vclh;
fa7fdbc6 3096 minefunction mine_verus;
3097 mine_verus = IsCPUVerusOptimized() ? &mine_verus_v2 : &mine_verus_v2_port;
f21fad6a 3098
42181656 3099 while (true)
3100 {
4dcb64c0 3101 uint256 hashResult = uint256();
458bfcab 3102
e5fb645e 3103 unsigned char *curBuf;
3104
f8f61a6d 3105 if (mergeMining)
42181656 3106 {
c89d86ee 3107 // loop for a few minutes before refreshing the block
e771a884 3108 while (true)
12217420 3109 {
93ff475b 3110 uint256 ourMerkle = pblock->hashMerkleRoot;
a1d91f89 3111 if ( pindexPrev != chainActive.LastTip() )
3112 {
3113 if (lastChainTipPrinted != chainActive.LastTip())
3114 {
3115 lastChainTipPrinted = chainActive.LastTip();
3116 printf("Block %d added to chain\n\n", lastChainTipPrinted->GetHeight());
3117 arith_uint256 target;
3118 target.SetCompact(lastChainTipPrinted->nBits);
93ff475b
MT
3119 if (ourMerkle == lastChainTipPrinted->hashMerkleRoot)
3120 {
3121 LogPrintf("proof-of-work found \n hash: %s \ntarget: %s\n", lastChainTipPrinted->GetBlockHash().GetHex().c_str(), ArithToUint256(ourTarget).GetHex().c_str());
607402ba 3122 printf("Found block %d \n", lastChainTipPrinted->GetHeight());
93ff475b
MT
3123 printf("mining reward %.8f %s!\n", (double)subsidy / (double)COIN, ASSETCHAINS_SYMBOL);
3124 printf(" hash: %s\ntarget: %s\n", lastChainTipPrinted->GetBlockHash().GetHex().c_str(), ArithToUint256(ourTarget).GetHex().c_str());
3125 }
a1d91f89 3126 }
3127 break;
3128 }
3129
e771a884 3130 // if PBaaS is no longer available, we can't count on merge mining
3131 if (!ConnectedChains.IsVerusPBaaSAvailable())
3132 {
3133 break;
3134 }
f8f61a6d 3135
3136 if (vNodes.empty() && chainparams.MiningRequiresPeers())
458bfcab 3137 {
f8f61a6d 3138 if ( Mining_height > ASSETCHAINS_MINHEIGHT )
fa7fdbc6 3139 {
f8f61a6d 3140 fprintf(stderr,"no nodes, attempting reconnect\n");
3141 break;
fa7fdbc6 3142 }
f8f61a6d 3143 }
3144
a82942e4 3145 // update every few minutes, regardless
3146 int64_t elapsed = GetTime() - nStart;
f8f61a6d 3147
a9663647 3148 if ((mempool.GetTransactionsUpdated() != nTransactionsUpdatedLast && elapsed > 60) || elapsed > 60 || ConnectedChains.lastSubmissionFailed)
458bfcab 3149 {
f8f61a6d 3150 break;
458bfcab 3151 }
a1d91f89 3152
dc74c06d 3153 boost::this_thread::interruption_point();
a1d91f89 3154 MilliSleep(500);
458bfcab 3155 }
ffde1589 3156 break;
f8f61a6d 3157 }
3158 else
3159 {
3160 // check NONCEMASK at a time
3161 for (uint64_t i = 0; i < count; i++)
42181656 3162 {
2fd1f0fb 3163 // this is the actual mining loop, which enables us to drop out and queue a header anytime we earn a block that is good enough for a
f8f61a6d 3164 // merge mined block, but not our own
f8f61a6d 3165 bool blockFound;
3166 arith_uint256 arithHash;
2830db29 3167 totalDone = 0;
f8f61a6d 3168 do
3169 {
2fd1f0fb 3170 // pickup/remove any new/deleted headers
71f97948 3171 if (ConnectedChains.dirty || (pblock->NumPBaaSHeaders() < ConnectedChains.mergeMinedChains.size() + 1))
2fd1f0fb 3172 {
f37d48ca 3173 IncrementExtraNonce(pblock, pindexPrev, nExtraNonce, verusSolutionPBaaS ? false : true, &savebits);
1fa4454d 3174
2fd1f0fb 3175 hashTarget.SetCompact(savebits);
3176 uintTarget = ArithToUint256(hashTarget);
3177 }
3178
f8f61a6d 3179 // hashesToGo gets updated with actual number run for metrics
3180 hashesToGo = ASSETCHAINS_HASHESPERROUND[ASSETCHAINS_ALGO];
2830db29 3181 uint64_t start = i * hashesToGo + totalDone;
f8f61a6d 3182 hashesToGo -= totalDone;
3183
f37d48ca 3184 if (verusSolutionPBaaS)
f8f61a6d 3185 {
3186 // mine on canonical header for merge mining
3187 CPBaaSPreHeader savedHeader(*pblock);
da97aa5c 3188
f8f61a6d 3189 pblock->ClearNonCanonicalData();
c17dbdee 3190 blockFound = (*mine_verus)(*pblock, ss2, hashResult, uintTarget, start, &hashesToGo);
f8f61a6d 3191 savedHeader.SetBlockData(*pblock);
3192 }
3193 else
3194 {
c17dbdee 3195 blockFound = (*mine_verus)(*pblock, ss2, hashResult, uintTarget, start, &hashesToGo);
f8f61a6d 3196 }
3197
3198 arithHash = UintToArith256(hashResult);
249e20e4 3199 totalDone += hashesToGo + 1;
f8f61a6d 3200 if (blockFound && IsVerusActive())
3201 {
3202 ConnectedChains.QueueNewBlockHeader(*pblock);
3203 if (arithHash > ourTarget)
3204 {
3205 // all blocks qualified with this hash will be submitted
3206 // until we redo the block, we might as well not try again with anything over this hash
3207 hashTarget = arithHash;
3208 uintTarget = ArithToUint256(hashTarget);
3209 }
3210 }
2fd1f0fb 3211 } while (blockFound && arithHash > ourTarget);
c98efb5a 3212
f8f61a6d 3213 if (!blockFound || arithHash > ourTarget)
4dcb64c0 3214 {
f8f61a6d 3215 // Check for stop or if block needs to be rebuilt
3216 boost::this_thread::interruption_point();
ce40cf2e 3217 if ( pindexPrev != chainActive.LastTip() )
f8f61a6d 3218 {
3219 if (lastChainTipPrinted != chainActive.LastTip())
3220 {
3221 lastChainTipPrinted = chainActive.LastTip();
3222 printf("Block %d added to chain\n", lastChainTipPrinted->GetHeight());
3223 }
3224 break;
3225 }
a1d91f89 3226 else if ((i + 1) < count)
f8f61a6d 3227 {
a1d91f89 3228 // if we'll not drop through, update hashcount
f8f61a6d 3229 {
02dbe9a3 3230 miningTimer += totalDone;
2830db29 3231 totalDone = 0;
f8f61a6d 3232 }
f8f61a6d 3233 }
4dcb64c0 3234 }
f8f61a6d 3235 else
3236 {
3237 // Check for stop or if block needs to be rebuilt
3238 boost::this_thread::interruption_point();
4dcb64c0 3239
f8f61a6d 3240 if (pblock->nSolution.size() != 1344)
3241 {
3242 LogPrintf("ERROR: Block solution is not 1344 bytes as it should be");
3243 break;
3244 }
42181656 3245
f8f61a6d 3246 SetThreadPriority(THREAD_PRIORITY_NORMAL);
3247
3248 int32_t unlockTime = komodo_block_unlocktime(Mining_height);
ef70c5b2 3249
3363d1c0 3250#ifdef VERUSHASHDEBUG
f8f61a6d 3251 std::string validateStr = hashResult.GetHex();
3252 std::string hashStr = pblock->GetHash().GetHex();
3253 uint256 *bhalf1 = (uint256 *)vh2->CurBuffer();
3254 uint256 *bhalf2 = bhalf1 + 1;
3363d1c0 3255#else
f8f61a6d 3256 std::string hashStr = hashResult.GetHex();
3363d1c0 3257#endif
3af22e67 3258
f8f61a6d 3259 LogPrintf("Using %s algorithm:\n", ASSETCHAINS_ALGORITHMS[ASSETCHAINS_ALGO]);
3260 LogPrintf("proof-of-work found \n hash: %s \ntarget: %s\n", hashStr, ArithToUint256(ourTarget).GetHex());
3261 printf("Found block %d \n", Mining_height );
3262 printf("mining reward %.8f %s!\n", (double)subsidy / (double)COIN, ASSETCHAINS_SYMBOL);
3363d1c0 3263#ifdef VERUSHASHDEBUG
f8f61a6d 3264 printf(" hash: %s\n val: %s \ntarget: %s\n\n", hashStr.c_str(), validateStr.c_str(), ArithToUint256(ourTarget).GetHex().c_str());
3265 printf("intermediate %lx\n", intermediate);
3266 printf("Curbuf: %s%s\n", bhalf1->GetHex().c_str(), bhalf2->GetHex().c_str());
3267 bhalf1 = (uint256 *)verusclhasher_key.get();
3268 bhalf2 = bhalf1 + ((vh2->vclh.keyMask + 1) >> 5);
3269 printf(" Key: %s%s\n", bhalf1->GetHex().c_str(), bhalf2->GetHex().c_str());
3363d1c0 3270#else
f8f61a6d 3271 printf(" hash: %s\ntarget: %s", hashStr.c_str(), ArithToUint256(ourTarget).GetHex().c_str());
3363d1c0 3272#endif
f8f61a6d 3273 if (unlockTime > Mining_height && subsidy >= ASSETCHAINS_TIMELOCKGTE)
3274 printf(" - timelocked until block %i\n", unlockTime);
3275 else
3276 printf("\n");
42181656 3277#ifdef ENABLE_WALLET
f8f61a6d 3278 ProcessBlockFound(pblock, *pwallet, reservekey);
42181656 3279#else
f8f61a6d 3280 ProcessBlockFound(pblock);
42181656 3281#endif
f8f61a6d 3282 SetThreadPriority(THREAD_PRIORITY_LOWEST);
3283 break;
3284 }
42181656 3285 }
42181656 3286
f8f61a6d 3287 {
02dbe9a3 3288 miningTimer += totalDone;
f8f61a6d 3289 }
69767347 3290 }
f8f61a6d 3291
69767347 3292
42181656 3293 // Check for stop or if block needs to be rebuilt
3294 boost::this_thread::interruption_point();
3295
3296 if (vNodes.empty() && chainparams.MiningRequiresPeers())
3297 {
3298 if ( Mining_height > ASSETCHAINS_MINHEIGHT )
3299 {
ef70c5b2 3300 fprintf(stderr,"no nodes, attempting reconnect\n");
42181656 3301 break;
3302 }
3303 }
3304
dbe656fe 3305 if (mempool.GetTransactionsUpdated() != nTransactionsUpdatedLast && GetTime() - nStart > 60)
42181656 3306 {
dbe656fe 3307 fprintf(stderr,"timeout, retrying\n");
42181656 3308 break;
3309 }
3310
37ad6886 3311 if ( pindexPrev != chainActive.LastTip() )
42181656 3312 {
37ad6886 3313 if (lastChainTipPrinted != chainActive.LastTip())
dbe656fe 3314 {
37ad6886 3315 lastChainTipPrinted = chainActive.LastTip();
90198f71 3316 printf("Block %d added to chain\n\n", lastChainTipPrinted->GetHeight());
dbe656fe 3317 }
42181656 3318 break;
3319 }
3320
2830db29 3321 // totalDone now has the number of hashes actually done since starting on one nonce mask worth
ce40cf2e 3322 uint64_t hashesPerNonceMask = ASSETCHAINS_NONCEMASK[ASSETCHAINS_ALGO] >> 3;
2830db29 3323 if (!(totalDone < hashesPerNonceMask))
ce40cf2e 3324 {
52cf66e1 3325#ifdef _WIN32
ce40cf2e 3326 printf("%llu mega hashes complete - working\n", (hashesPerNonceMask + 1) / 1048576);
52cf66e1 3327#else
ce40cf2e 3328 printf("%lu mega hashes complete - working\n", (hashesPerNonceMask + 1) / 1048576);
52cf66e1 3329#endif
ce40cf2e 3330 }
4dcb64c0 3331 break;
8682e17a 3332
42181656 3333 }
3334 }
3335 }
3336 catch (const boost::thread_interrupted&)
3337 {
3338 miningTimer.stop();
248084b7 3339 miningTimer.clear();
5034d1c1 3340 LogPrintf("%s miner terminated\n", ASSETCHAINS_ALGORITHMS[ASSETCHAINS_ALGO]);
42181656 3341 throw;
3342 }
3343 catch (const std::runtime_error &e)
3344 {
3345 miningTimer.stop();
248084b7 3346 miningTimer.clear();
5034d1c1 3347 LogPrintf("%s miner runtime error: %s\n", ASSETCHAINS_ALGORITHMS[ASSETCHAINS_ALGO], e.what());
42181656 3348 return;
3349 }
3350 miningTimer.stop();
248084b7 3351 miningTimer.clear();
42181656 3352}
3353
8e8b6d70 3354#ifdef ENABLE_WALLET
e9e70b95 3355 void GenerateBitcoins(bool fGenerate, CWallet* pwallet, int nThreads)
8e8b6d70 3356#else
e9e70b95 3357 void GenerateBitcoins(bool fGenerate, int nThreads)
8e8b6d70 3358#endif
d247a5d1 3359 {
9ae83faa 3360 static CCriticalSection cs_startmining;
3361
3362 LOCK(cs_startmining);
f8f61a6d 3363 if (!AreParamsInitialized())
3364 {
3365 return;
3366 }
3367
b20c38cc 3368 VERUS_MINTBLOCKS = (VERUS_MINTBLOCKS && ASSETCHAINS_LWMAPOS != 0);
bd6639fd 3369
89cd7b59 3370 if (fGenerate == true || VERUS_MINTBLOCKS)
10214558 3371 {
89cd7b59
MT
3372 mapArgs["-gen"] = "1";
3373
6e62f837 3374 if (VERUS_DEFAULT_ZADDR.size() > 0)
99c94fc3 3375 {
6e62f837 3376 if (defaultSaplingDest == boost::none)
89cd7b59 3377 {
6e62f837 3378 LogPrintf("ERROR: -defaultzaddr parameter is invalid Sapling payment address\n");
3379 fprintf(stderr, "-defaultzaddr parameter is invalid Sapling payment address\n");
89cd7b59
MT
3380 }
3381 else
3382 {
6e62f837 3383 LogPrintf("StakeGuard searching for double stakes on %s\n", VERUS_DEFAULT_ZADDR.c_str());
3384 fprintf(stderr, "StakeGuard searching for double stakes on %s\n", VERUS_DEFAULT_ZADDR.c_str());
89cd7b59 3385 }
99c94fc3 3386 }
3387 }
10214558 3388
e9e70b95 3389 static boost::thread_group* minerThreads = NULL;
28424e9f 3390
e9e70b95 3391 if (nThreads < 0)
3392 nThreads = GetNumCores();
9ae83faa 3393
e9e70b95 3394 if (minerThreads != NULL)
3395 {
3396 minerThreads->interrupt_all();
88d014d0 3397 minerThreads->join_all();
e9e70b95 3398 delete minerThreads;
3399 minerThreads = NULL;
3400 }
135fa24e 3401
afaeb54b 3402 //fprintf(stderr,"nThreads.%d fGenerate.%d\n",(int32_t)nThreads,fGenerate);
5034d1c1 3403 if ( nThreads == 0 && ASSETCHAINS_STAKED )
3a446d9f 3404 nThreads = 1;
5034d1c1 3405
28424e9f 3406 if (!fGenerate)
e9e70b95 3407 return;
135fa24e 3408
e9e70b95 3409 minerThreads = new boost::thread_group();
135fa24e 3410
85c51d62 3411 // add the PBaaS thread when mining or staking
3412 minerThreads->create_thread(boost::bind(&CConnectedChains::SubmissionThreadStub));
3413
135fa24e 3414#ifdef ENABLE_WALLET
b20c38cc 3415 if (VERUS_MINTBLOCKS && pwallet != NULL)
135fa24e 3416 {
3417 minerThreads->create_thread(boost::bind(&VerusStaker, pwallet));
3418 }
3419#endif
3420
e9e70b95 3421 for (int i = 0; i < nThreads; i++) {
8e8b6d70 3422#ifdef ENABLE_WALLET
855714b0 3423 minerThreads->create_thread(boost::bind(&BitcoinMiner_noeq, pwallet));
8e8b6d70 3424#else
855714b0 3425 minerThreads->create_thread(&BitcoinMiner_noeq);
8e8b6d70 3426#endif
e9e70b95 3427 }
8e8b6d70 3428 }
e9e70b95 3429
2cc0a252 3430#endif // ENABLE_MINING
This page took 1.185103 seconds and 4 git commands to generate.