1 /********************************************************************
2 * (C) 2019 Michael Toutonghi
4 * Distributed under the MIT software license, see the accompanying
5 * file COPYING or http://www.opensource.org/licenses/mit-license.php.
7 * This provides support for PBaaS initialization, notarization, and cross-chain token
8 * transactions and enabling liquid or non-liquid tokens across the
15 #include "rpc/pbaasrpc.h"
17 #include "transaction_builder.h"
19 CConnectedChains ConnectedChains;
23 return (strcmp(ASSETCHAINS_SYMBOL, "VRSC") == 0 || strcmp(ASSETCHAINS_SYMBOL, "VRSCTEST") == 0);
26 bool IsVerusMainnetActive()
28 return (strcmp(ASSETCHAINS_SYMBOL, "VRSC") == 0);
31 // this adds an opret to a mutable transaction and returns the voutnum if it could be added
32 int32_t AddOpRetOutput(CMutableTransaction &mtx, const CScript &opRetScript)
34 if (opRetScript.IsOpReturn() && opRetScript.size() <= MAX_OP_RETURN_RELAY)
36 CTxOut vOut = CTxOut();
37 vOut.scriptPubKey = opRetScript;
39 mtx.vout.push_back(vOut);
40 return mtx.vout.size() - 1;
48 // returns a pointer to a base chain object, which can be cast to the
49 // object type indicated in its objType member
50 uint256 GetChainObjectHash(const CBaseChainObject &bo)
53 const CBaseChainObject *retPtr;
54 const CChainObject<CBlockHeaderAndProof> *pNewHeader;
55 const CChainObject<CPartialTransactionProof> *pNewTx;
56 const CChainObject<CBlockHeaderProof> *pNewHeaderRef;
57 const CChainObject<CPriorBlocksCommitment> *pPriors;
58 const CChainObject<uint256> *pNewProofRoot;
59 const CChainObject<CReserveTransfer> *pExport;
60 const CChainObject<CCrossChainProof> *pCrossChainProof;
61 const CChainObject<CCompositeChainObject> *pCompositeChainObject;
69 return pNewHeader->GetHash();
71 case CHAINOBJ_TRANSACTION_PROOF:
72 return pNewTx->GetHash();
74 case CHAINOBJ_HEADER_REF:
75 return pNewHeaderRef->GetHash();
77 case CHAINOBJ_PRIORBLOCKS:
78 return pPriors->GetHash();
80 case CHAINOBJ_PROOF_ROOT:
81 return pNewProofRoot->object;
83 case CHAINOBJ_RESERVETRANSFER:
84 return pExport->GetHash();
86 case CHAINOBJ_CROSSCHAINPROOF:
87 return pCrossChainProof->GetHash();
89 case CHAINOBJ_COMPOSITEOBJECT:
90 return pCrossChainProof->GetHash();
96 // used to export coins from one chain to another, if they are not native, they are represented on the other
98 bool ValidateCrossChainExport(struct CCcontract_info *cp, Eval* eval, const CTransaction &tx, uint32_t nIn, bool fulfilled)
103 bool IsCrossChainExportInput(const CScript &scriptSig)
108 // used to validate import of coins from one chain to another. if they are not native and are supported,
109 // they are represented o the chain as tokens
110 bool ValidateCrossChainImport(struct CCcontract_info *cp, Eval* eval, const CTransaction &tx, uint32_t nIn, bool fulfilled)
114 bool IsCrossChainImportInput(const CScript &scriptSig)
119 // used to validate a specific service reward based on the spending transaction
120 bool ValidateServiceReward(struct CCcontract_info *cp, Eval* eval, const CTransaction &tx, uint32_t nIn, bool fulfilled)
122 // for each type of service reward, we need to check and see if the spender is
123 // correctly formatted to be a valid spend of the service reward. for notarization
124 // we ensure that the notarization and its outputs are valid and that the spend
125 // applies to the correct billing period
128 bool IsServiceRewardInput(const CScript &scriptSig)
133 // used as a proxy token output for a reserve currency on its fractional reserve chain
134 bool ValidateReserveOutput(struct CCcontract_info *cp, Eval* eval, const CTransaction &tx, uint32_t nIn, bool fulfilled)
138 bool IsReserveOutputInput(const CScript &scriptSig)
143 bool ValidateReserveTransfer(struct CCcontract_info *cp, Eval* eval, const CTransaction &tx, uint32_t nIn, bool fulfilled)
147 bool IsReserveTransferInput(const CScript &scriptSig)
152 bool ValidateReserveDeposit(struct CCcontract_info *cp, Eval* eval, const CTransaction &tx, uint32_t nIn, bool fulfilled)
156 bool IsReserveDepositInput(const CScript &scriptSig)
161 bool ValidateCurrencyState(struct CCcontract_info *cp, Eval* eval, const CTransaction &tx, uint32_t nIn, bool fulfilled)
165 bool IsCurrencyStateInput(const CScript &scriptSig)
170 // used to convert a fractional reserve currency into its reserve and back
171 bool ValidateReserveExchange(struct CCcontract_info *cp, Eval* eval, const CTransaction &tx, uint32_t nIn, bool fulfilled)
175 bool IsReserveExchangeInput(const CScript &scriptSig)
182 * Verifies that the input objects match the hashes and returns the transaction.
184 * If the opRetTx has the op ret, this calculates based on the actual transaction and
185 * validates the hashes. If the opRetTx does not have the opRet itself, this validates
186 * by ensuring that all objects are present on this chain, composing the opRet, and
187 * ensuring that the transaction then hashes to the correct txid.
190 bool ValidateOpretProof(CScript &opRet, COpRetProof &orProof)
192 // enumerate through the objects and validate that they are objects of the expected type that hash
193 // to the value expected. return true if so
197 int8_t ObjTypeCode(const CBlockHeaderProof &obj)
199 return CHAINOBJ_HEADER;
202 int8_t ObjTypeCode(const uint256 &obj)
204 return CHAINOBJ_PROOF_ROOT;
207 int8_t ObjTypeCode(const CPartialTransactionProof &obj)
209 return CHAINOBJ_TRANSACTION_PROOF;
212 int8_t ObjTypeCode(const CBlockHeaderAndProof &obj)
214 return CHAINOBJ_HEADER_REF;
217 int8_t ObjTypeCode(const CPriorBlocksCommitment &obj)
219 return CHAINOBJ_PRIORBLOCKS;
222 int8_t ObjTypeCode(const CReserveTransfer &obj)
224 return CHAINOBJ_RESERVETRANSFER;
227 int8_t ObjTypeCode(const CCrossChainProof &obj)
229 return CHAINOBJ_CROSSCHAINPROOF;
232 int8_t ObjTypeCode(const CCompositeChainObject &obj)
234 return CHAINOBJ_COMPOSITEOBJECT;
237 // this adds an opret to a mutable transaction that provides the necessary evidence of a signed, cheating stake transaction
238 CScript StoreOpRetArray(const std::vector<CBaseChainObject *> &objPtrs)
241 CDataStream s = CDataStream(SER_NETWORK, PROTOCOL_VERSION);
242 s << (int32_t)OPRETTYPE_OBJECTARR;
245 for (auto pobj : objPtrs)
249 if (!DehydrateChainObject(s, pobj))
255 catch(const std::exception& e)
257 std::cerr << e.what() << '\n';
263 //std::vector<unsigned char> schars(s.begin(), s.begin() + 200);
264 //printf("stream vector chars: %s\n", HexBytes(&schars[0], schars.size()).c_str());
266 std::vector<unsigned char> vch(s.begin(), s.end());
267 return error ? CScript() : CScript() << OP_RETURN << vch;
270 void DeleteOpRetObjects(std::vector<CBaseChainObject *> &ora)
272 for (auto pobj : ora)
274 switch(pobj->objectType)
276 case CHAINOBJ_HEADER:
278 delete (CChainObject<CBlockHeaderAndProof> *)pobj;
282 case CHAINOBJ_TRANSACTION_PROOF:
284 delete (CChainObject<CPartialTransactionProof> *)pobj;
288 case CHAINOBJ_PROOF_ROOT:
290 delete (CChainObject<uint256> *)pobj;
294 case CHAINOBJ_HEADER_REF:
296 delete (CChainObject<CBlockHeaderProof> *)pobj;
300 case CHAINOBJ_PRIORBLOCKS:
302 delete (CChainObject<CPriorBlocksCommitment> *)pobj;
306 case CHAINOBJ_RESERVETRANSFER:
308 delete (CChainObject<CReserveTransfer> *)pobj;
312 case CHAINOBJ_CROSSCHAINPROOF:
314 delete (CChainObject<CCrossChainProof> *)pobj;
318 case CHAINOBJ_COMPOSITEOBJECT:
320 delete (CChainObject<CCompositeChainObject> *)pobj;
326 printf("ERROR: invalid object type (%u), likely corrupt pointer %p\n", pobj->objectType, pobj);
327 printf("generate code that won't be optimized away %s\n", CCurrencyValueMap(std::vector<uint160>({ASSETCHAINS_CHAINID}), std::vector<CAmount>({200000000})).ToUniValue().write(1,2).c_str());
328 printf("This is here to generate enough code for a good break point system chain name: %s\n", ConnectedChains.ThisChain().name.c_str());
337 std::vector<CBaseChainObject *> RetrieveOpRetArray(const CScript &opRetScript)
339 std::vector<unsigned char> vch;
340 std::vector<CBaseChainObject *> vRet;
341 if (opRetScript.IsOpReturn() && GetOpReturnData(opRetScript, vch) && vch.size() > 0)
343 CDataStream s = CDataStream(vch, SER_NETWORK, PROTOCOL_VERSION);
350 if (opRetType == OPRETTYPE_OBJECTARR)
352 CBaseChainObject *pobj;
353 while (!s.empty() && (pobj = RehydrateChainObject(s)))
355 vRet.push_back(pobj);
359 printf("failed to load all objects in opret");
360 DeleteOpRetObjects(vRet);
365 catch(const std::exception& e)
367 std::cerr << e.what() << '\n';
368 DeleteOpRetObjects(vRet);
375 CServiceReward::CServiceReward(const CTransaction &tx)
377 nVersion = PBAAS_VERSION_INVALID;
378 for (auto out : tx.vout)
381 if (IsPayToCryptoCondition(out.scriptPubKey, p))
383 // always take the first for now
384 if (p.evalCode == EVAL_SERVICEREWARD)
386 FromVector(p.vData[0], *this);
393 CCrossChainExport::CCrossChainExport(const CTransaction &tx, int32_t *pCCXOutputNum)
395 int32_t _ccxOutputNum = 0;
396 int32_t &ccxOutputNum = pCCXOutputNum ? *pCCXOutputNum : _ccxOutputNum;
398 for (int i = 0; i < tx.vout.size(); i++)
401 if (tx.vout[i].scriptPubKey.IsPayToCryptoCondition(p) &&
403 p.evalCode == EVAL_CROSSCHAIN_EXPORT)
405 FromVector(p.vData[0], *this);
412 CCurrencyDefinition::CCurrencyDefinition(const CScript &scriptPubKey)
414 nVersion = PBAAS_VERSION_INVALID;
416 if (scriptPubKey.IsPayToCryptoCondition(p) && p.IsValid())
418 if (p.evalCode == EVAL_CURRENCY_DEFINITION)
420 FromVector(p.vData[0], *this);
425 std::vector<CCurrencyDefinition> CCurrencyDefinition::GetCurrencyDefinitions(const CTransaction &tx)
427 std::vector<CCurrencyDefinition> retVal;
428 for (auto &out : tx.vout)
430 CCurrencyDefinition oneCur = CCurrencyDefinition(out.scriptPubKey);
431 if (oneCur.IsValid())
433 retVal.push_back(oneCur);
439 #define _ASSETCHAINS_TIMELOCKOFF 0xffffffffffffffff
440 extern uint64_t ASSETCHAINS_TIMELOCKGTE, ASSETCHAINS_TIMEUNLOCKFROM, ASSETCHAINS_TIMEUNLOCKTO;
441 extern int64_t ASSETCHAINS_SUPPLY, ASSETCHAINS_REWARD[3], ASSETCHAINS_DECAY[3], ASSETCHAINS_HALVING[3], ASSETCHAINS_ENDSUBSIDY[3], ASSETCHAINS_ERAOPTIONS[3];
442 extern int32_t PBAAS_STARTBLOCK, PBAAS_ENDBLOCK, ASSETCHAINS_LWMAPOS;
443 extern uint32_t ASSETCHAINS_ALGO, ASSETCHAINS_VERUSHASH, ASSETCHAINS_LASTERA;
444 extern std::string VERUS_CHAINNAME;
445 extern uint160 VERUS_CHAINID;
447 // ensures that the chain definition is valid and that there are no other definitions of the same name
448 // that have been confirmed.
449 bool ValidateChainDefinition(struct CCcontract_info *cp, Eval* eval, const CTransaction &tx, uint32_t nIn, bool fulfilled)
451 // the chain definition output can be spent when the chain is at the end of its life and only then
456 // ensures that the chain definition is valid and that there are no other definitions of the same name
457 // that have been confirmed.
458 bool CheckChainDefinitionOutputs(struct CCcontract_info *cp, Eval* eval, const CTransaction &tx, uint32_t nIn)
460 // checked before a chain definition output script is accepted as a valid transaction
462 // basics - we need a chain definition transaction to kick off a PBaaS chain. it must have:
463 // 1) valid chain definition output with parameters in proper ranges and no duplicate name
464 // 2) notarization output with conformant values
465 // 3) finalization output
466 // 3) notarization funding
469 // get the source transaction
472 if (!GetTransaction(tx.vin[nIn].prevout.hash, thisTx, blkHash))
474 LogPrintf("failed to retrieve transaction %s\n", tx.vin[nIn].prevout.hash.GetHex().c_str());
478 std::vector<CCurrencyDefinition> chainDefs = CCurrencyDefinition::GetCurrencyDefinitions(thisTx);
479 CPBaaSNotarization notarization(thisTx);
480 CTransactionFinalization finalization(thisTx);
481 bool isVerusActive = IsVerusActive();
483 if (!notarization.IsValid() || !finalization.IsValid())
485 LogPrintf("transaction specified, %s, must have valid notarization, and finaization outputs\n", tx.vin[nIn].prevout.hash.GetHex().c_str());
489 std::set<uint160> allCurrencyIDs;
490 for (auto &curPair : ConnectedChains.ReserveCurrencies())
492 allCurrencyIDs.insert(curPair.first);
494 allCurrencyIDs.insert(ConnectedChains.ThisChain().GetID());
497 allCurrencyIDs.insert(ConnectedChains.notaryChain.GetID());
500 bool isCoinbase = thisTx.IsCoinBase();
501 bool isVerified = false;
502 CIdentity activatedID(thisTx);
504 // currency definitions can be valid as follows:
505 // 1. original definition in a transaction that simultaneously sets the active currency bit on the identity of the same
507 // 2. outputs of a coinbase transaction in block 1 that defines the parent currency, new currency, and any reserve currencies
508 // 3. currency import from the defining chain of the currency, which has not been implemented as of this comment
509 if (activatedID.IsValid() &&
510 activatedID.HasActiveCurrency() &&
511 chainDefs.size() == 1 &&
512 activatedID.parent == ASSETCHAINS_CHAINID &&
513 activatedID.GetID() == chainDefs[0].GetID())
517 else if (isCoinbase && chainDefs.size() >= 1 && !isVerusActive)
520 CScript expect = CScript() << height1;
521 opcodetype opcode = (opcodetype)*expect.begin();
523 if (opcode >= OP_1 && opcode <= OP_16)
525 isVerified = (thisTx.vin[0].scriptSig.size() >= 1 && CScript::DecodeOP_N(opcode) == height1) ||
526 (thisTx.vin[0].scriptSig.size() >= 2 && thisTx.vin[0].scriptSig[0] == OP_PUSHDATA1 && (int)thisTx.vin[0].scriptSig[1] == height1);
530 isVerified = thisTx.vin[0].scriptSig.size() >= expect.size() && std::equal(expect.begin(), expect.end(), thisTx.vin[0].scriptSig.begin());
533 for (auto &chainDef : chainDefs)
535 uint160 chainID = chainDef.GetID();
536 if (!chainDef.IsValid())
538 LogPrintf("transaction specified, %s, must not contain invalid chain definitions\n", tx.vin[nIn].prevout.hash.GetHex().c_str());
541 if (!allCurrencyIDs.count(chainID))
543 LogPrintf("transaction specified, %s, must not contain invalid chain definitions\n", tx.vin[nIn].prevout.hash.GetHex().c_str());
546 allCurrencyIDs.erase(chainID);
548 if (allCurrencyIDs.size())
550 LogPrintf("transaction specified, %s, does not contain all required chain definitions\n", tx.vin[nIn].prevout.hash.GetHex().c_str());
558 CCurrencyValueMap CCrossChainExport::CalculateExportFee(const CCurrencyValueMap &fees, int numIn)
560 CCurrencyValueMap retVal;
562 if (numIn > MAX_EXPORT_INPUTS)
566 static const arith_uint256 satoshis(100000000);
568 arith_uint256 ratio(50000000 + ((25000000 / MAX_EXPORT_INPUTS) * (numIn - 1)));
570 for (auto &feePair : fees.valueMap)
572 retVal.valueMap[feePair.first] = (((arith_uint256(feePair.second) * ratio)) / satoshis).GetLow64();
574 return retVal.CanonicalMap();
577 CAmount CCrossChainExport::CalculateExportFeeRaw(CAmount fee, int numIn)
579 if (numIn > MAX_EXPORT_INPUTS)
581 numIn = MAX_EXPORT_INPUTS;
583 static const arith_uint256 satoshis(100000000);
585 arith_uint256 ratio(50000000 + ((25000000 / MAX_EXPORT_INPUTS) * (numIn - 1)));
587 return (((arith_uint256(fee) * ratio)) / satoshis).GetLow64();
590 CAmount CCrossChainExport::ExportReward(int64_t exportFee)
592 int64_t individualExportFee = ((arith_uint256(exportFee) * 10000000) / SATOSHIDEN).GetLow64();
593 if (individualExportFee < MIN_FEES_BEFORE_FEEPOOL)
595 individualExportFee = exportFee > MIN_FEES_BEFORE_FEEPOOL ? MIN_FEES_BEFORE_FEEPOOL : exportFee;
597 return individualExportFee;
600 CCurrencyValueMap CCrossChainExport::CalculateExportFee() const
602 return CalculateExportFee(totalFees, numInputs);
605 CCurrencyValueMap CCrossChainExport::CalculateImportFee() const
607 CCurrencyValueMap retVal;
609 for (auto &feePair : CalculateExportFee().valueMap)
611 CAmount feeAmount = feePair.second;
612 auto it = totalFees.valueMap.find(feePair.first);
613 retVal.valueMap[feePair.first] = (it != totalFees.valueMap.end() ? it->second : 0) - feeAmount;
618 bool CConnectedChains::RemoveMergedBlock(uint160 chainID)
621 LOCK(cs_mergemining);
623 //printf("RemoveMergedBlock ID: %s\n", chainID.GetHex().c_str());
625 auto chainIt = mergeMinedChains.find(chainID);
626 if (chainIt != mergeMinedChains.end())
628 arith_uint256 target;
629 target.SetCompact(chainIt->second.block.nBits);
630 for (auto removeRange = mergeMinedTargets.equal_range(target); removeRange.first != removeRange.second; removeRange.first++)
632 // make sure we don't just match by target
633 if (removeRange.first->second->GetID() == chainID)
635 mergeMinedTargets.erase(removeRange.first);
639 mergeMinedChains.erase(chainID);
640 dirty = retval = true;
642 // if we get to 0, give the thread a kick to stop waiting for mining
643 //if (!mergeMinedChains.size())
645 // sem_submitthread.post();
651 // remove merge mined chains added and not updated since a specific time
652 void CConnectedChains::PruneOldChains(uint32_t pruneBefore)
654 vector<uint160> toRemove;
656 LOCK(cs_mergemining);
657 for (auto blkData : mergeMinedChains)
659 if (blkData.second.block.nTime < pruneBefore)
661 toRemove.push_back(blkData.first);
665 for (auto id : toRemove)
667 //printf("Pruning chainID: %s\n", id.GetHex().c_str());
668 RemoveMergedBlock(id);
672 // adds or updates merge mined blocks
673 // returns false if failed to add
674 bool CConnectedChains::AddMergedBlock(CPBaaSMergeMinedChainData &blkData)
676 // determine if we should replace one or add to the merge mine vector
678 LOCK(cs_mergemining);
680 arith_uint256 target;
681 uint160 cID = blkData.GetID();
682 auto it = mergeMinedChains.find(cID);
683 if (it != mergeMinedChains.end())
685 RemoveMergedBlock(cID); // remove it if already there
687 target.SetCompact(blkData.block.nBits);
689 //printf("AddMergedBlock name: %s, ID: %s\n", blkData.chainDefinition.name.c_str(), cID.GetHex().c_str());
691 mergeMinedTargets.insert(make_pair(target, &(mergeMinedChains.insert(make_pair(cID, blkData)).first->second)));
697 bool CConnectedChains::GetChainInfo(uint160 chainID, CRPCChainData &rpcChainData)
700 LOCK(cs_mergemining);
701 auto chainIt = mergeMinedChains.find(chainID);
702 if (chainIt != mergeMinedChains.end())
704 rpcChainData = (CRPCChainData)chainIt->second;
711 // this returns a pointer to the data without copy and assumes the lock is held
712 CPBaaSMergeMinedChainData *CConnectedChains::GetChainInfo(uint160 chainID)
715 auto chainIt = mergeMinedChains.find(chainID);
716 if (chainIt != mergeMinedChains.end())
718 return &chainIt->second;
724 void CConnectedChains::QueueNewBlockHeader(CBlockHeader &bh)
726 //printf("QueueNewBlockHeader %s\n", bh.GetHash().GetHex().c_str());
728 LOCK(cs_mergemining);
730 qualifiedHeaders[UintToArith256(bh.GetHash())] = bh;
732 sem_submitthread.post();
735 void CConnectedChains::CheckImports()
737 sem_submitthread.post();
740 // get the latest block header and submit one block at a time, returning after there are no more
741 // matching blocks to be found
742 vector<pair<string, UniValue>> CConnectedChains::SubmitQualifiedBlocks()
744 std::set<uint160> inHeader;
745 bool submissionFound;
746 CPBaaSMergeMinedChainData chainData;
747 vector<pair<string, UniValue>> results;
750 arith_uint256 lastHash;
751 CPBaaSBlockHeader pbh;
755 submissionFound = false;
757 LOCK(cs_mergemining);
758 // attempt to submit with the lowest hash answers first to increase the likelihood of submitting
759 // common, merge mined headers for notarization, drop out on any submission
760 for (auto headerIt = qualifiedHeaders.begin(); !submissionFound && headerIt != qualifiedHeaders.end(); headerIt = qualifiedHeaders.begin())
762 // add the PBaaS chain ids from this header to a set for search
763 for (uint32_t i = 0; headerIt->second.GetPBaaSHeader(pbh, i); i++)
765 inHeader.insert(pbh.chainID);
769 // now look through all targets that are equal to or above the hash of this header
770 for (auto chainIt = mergeMinedTargets.lower_bound(headerIt->first); !submissionFound && chainIt != mergeMinedTargets.end(); chainIt++)
772 chainID = chainIt->second->GetID();
773 if (inHeader.count(chainID))
775 // first, check that the winning header matches the block that is there
776 CPBaaSPreHeader preHeader(chainIt->second->block);
777 preHeader.SetBlockData(headerIt->second);
779 // check if the block header matches the block's specific data, only then can we create a submission from this block
780 if (headerIt->second.CheckNonCanonicalData(chainID))
782 // save block as is, remove the block from merged headers, replace header, and submit
783 chainData = *chainIt->second;
785 *(CBlockHeader *)&chainData.block = headerIt->second;
787 submissionFound = true;
789 //else // not an error condition. code is here for debugging
791 // printf("Mismatch in non-canonical data for chain %s\n", chainIt->second->chainDefinition.name.c_str());
794 //else // not an error condition. code is here for debugging
796 // printf("Not found in header %s\n", chainIt->second->chainDefinition.name.c_str());
800 // if this header matched no block, discard and move to the next, otherwise, we'll drop through
803 // once it is going to be submitted, remove block from this chain until a new one is added again
804 RemoveMergedBlock(chainID);
809 qualifiedHeaders.erase(headerIt);
815 // submit one block and loop again. this approach allows multiple threads
816 // to collectively empty the submission queue, mitigating the impact of
817 // any one stalled daemon
818 UniValue submitParams(UniValue::VARR);
819 submitParams.push_back(EncodeHexBlk(chainData.block));
820 UniValue result, error;
823 result = RPCCall("submitblock", submitParams, chainData.rpcUserPass, chainData.rpcPort, chainData.rpcHost);
824 result = find_value(result, "result");
825 error = find_value(result, "error");
829 result = UniValue(e.what());
831 results.push_back(make_pair(chainData.chainDefinition.name, result));
832 if (result.isStr() || !error.isNull())
834 printf("Error submitting block to %s chain: %s\n", chainData.chainDefinition.name.c_str(), result.isStr() ? result.get_str().c_str() : error.get_str().c_str());
838 printf("Successfully submitted block to %s chain\n", chainData.chainDefinition.name.c_str());
841 } while (submissionFound);
845 // add all merge mined chain PBaaS headers into the blockheader and return the easiest nBits target in the header
846 uint32_t CConnectedChains::CombineBlocks(CBlockHeader &bh)
848 vector<uint160> inHeader;
849 vector<UniValue> toCombine;
850 arith_uint256 blkHash = UintToArith256(bh.GetHash());
851 arith_uint256 target(0);
853 CPBaaSBlockHeader pbh;
856 LOCK(cs_mergemining);
858 CPBaaSSolutionDescriptor descr = CVerusSolutionVector::solutionTools.GetDescriptor(bh.nSolution);
860 for (uint32_t i = 0; i < descr.numPBaaSHeaders; i++)
862 if (bh.GetPBaaSHeader(pbh, i))
864 inHeader.push_back(pbh.chainID);
868 // loop through the existing PBaaS chain ids in the header
869 // remove any that are not either this Chain ID or in our local collection and then add all that are present
870 for (uint32_t i = 0; i < inHeader.size(); i++)
872 auto it = mergeMinedChains.find(inHeader[i]);
873 if (inHeader[i] != ASSETCHAINS_CHAINID && (it == mergeMinedChains.end()))
875 bh.DeletePBaaSHeader(i);
879 for (auto chain : mergeMinedChains)
881 // get the native PBaaS header for each chain and put it into the
882 // header we are given
883 // it must have itself in as a PBaaS header
884 uint160 cid = chain.second.GetID();
885 if (chain.second.block.GetPBaaSHeader(pbh, cid) != -1)
887 if (!bh.AddUpdatePBaaSHeader(pbh))
889 LogPrintf("Failure to add PBaaS block header for %s chain\n", chain.second.chainDefinition.name.c_str());
895 t.SetCompact(chain.second.block.nBits);
904 LogPrintf("Merge mined block for %s does not contain PBaaS information\n", chain.second.chainDefinition.name.c_str());
910 return target.GetCompact();
913 bool CConnectedChains::IsVerusPBaaSAvailable()
915 return notaryChainVersion >= "0.8.0";
918 extern string PBAAS_HOST, PBAAS_USERPASS;
919 extern int32_t PBAAS_PORT;
920 bool CConnectedChains::CheckVerusPBaaSAvailable(UniValue &chainInfoUni, UniValue &chainDefUni)
922 if (chainInfoUni.isObject() && chainDefUni.isObject())
924 UniValue uniVer = find_value(chainInfoUni, "VRSCversion");
927 LOCK(cs_mergemining);
928 notaryChainVersion = uni_get_str(uniVer);
929 notaryChainHeight = uni_get_int(find_value(chainInfoUni, "blocks"));
930 CCurrencyDefinition chainDef(chainDefUni);
931 notaryChain = CRPCChainData(chainDef, PBAAS_HOST, PBAAS_PORT, PBAAS_USERPASS);
934 return IsVerusPBaaSAvailable();
937 uint32_t CConnectedChains::NotaryChainHeight()
939 LOCK(cs_mergemining);
940 return notaryChainHeight;
943 bool CConnectedChains::CheckVerusPBaaSAvailable()
947 notaryChainVersion = "";
951 // if this is a PBaaS chain, poll for presence of Verus / root chain and current Verus block and version number
952 // tolerate only 15 second timeout
953 UniValue chainInfo, chainDef;
956 UniValue params(UniValue::VARR);
957 chainInfo = find_value(RPCCallRoot("getinfo", params), "result");
958 if (!chainInfo.isNull())
960 params.push_back(VERUS_CHAINNAME);
961 chainDef = find_value(RPCCallRoot("getcurrency", params), "result");
963 if (!chainDef.isNull() && CheckVerusPBaaSAvailable(chainInfo, chainDef))
965 // if we have not past block 1 yet, store the best known update of our current state
966 if ((!chainActive.LastTip() || !chainActive.LastTip()->GetHeight()))
968 bool success = false;
970 params.push_back(EncodeDestination(CIdentityID(thisChain.GetID())));
971 chainDef = find_value(RPCCallRoot("getcurrency", params), "result");
972 if (!chainDef.isNull())
974 CCurrencyDefinition currencyDef(chainDef);
975 if (currencyDef.IsValid())
977 thisChain = currencyDef;
978 if (NotaryChainHeight() >= thisChain.startBlock)
980 readyToStart = true; // this only gates mining of block one, to be sure we have the latest definition
990 } catch (exception e)
992 LogPrintf("%s: Error communicating with %s chain\n", __func__, VERUS_CHAINNAME);
995 notaryChainVersion = "";
999 int CConnectedChains::GetThisChainPort() const
1003 for (auto node : defaultPeerNodes)
1005 SplitHostPort(node.networkAddress, port, host);
1014 CCoinbaseCurrencyState CConnectedChains::AddPrelaunchConversions(CCurrencyDefinition &curDef,
1015 const CCoinbaseCurrencyState &_currencyState,
1018 int32_t curDefHeight)
1020 if (curDef.name == "PA-PD-PC-Tri")
1022 printf("%s: currency PA-PD-PC-Tri starting\n", __func__);
1025 CCoinbaseCurrencyState currencyState = _currencyState;
1026 bool firstUpdate = fromHeight <= curDefHeight;
1029 if (curDef.IsFractional())
1031 currencyState.supply = curDef.initialFractionalSupply;
1032 currencyState.reserves = std::vector<int64_t>(currencyState.reserves.size(), 0);
1033 currencyState.weights = curDef.weights;
1037 // supply is determined by purchases * current conversion rate
1038 currencyState.supply = currencyState.initialSupply;
1042 // get chain transfers that should apply before the start block
1043 // until there is a post-start block notarization, we always consider the
1044 // currency state to be up to just before the start block
1045 std::multimap<uint160, std::pair<CInputDescriptor, CReserveTransfer>> unspentTransfers;
1046 std::map<uint160, int32_t> currencyIndexes = currencyState.GetReserveMap();
1047 int32_t nativeIdx = currencyIndexes.count(curDef.systemID) ? currencyIndexes[curDef.systemID] : -1;
1049 if (GetChainTransfers(unspentTransfers, curDef.GetID(), fromHeight, height < curDef.startBlock ? height : curDef.startBlock - 1))
1051 currencyState.ClearForNextBlock();
1053 for (auto &transfer : unspentTransfers)
1055 if (transfer.second.second.IsPreConversion())
1057 CAmount conversionFee = CReserveTransactionDescriptor::CalculateConversionFee(transfer.second.second.nValue);
1058 CAmount valueIn = transfer.second.second.nValue - conversionFee;
1059 int32_t curIdx = currencyIndexes[transfer.second.second.currencyID];
1061 currencyState.reserveIn[curIdx] += valueIn;
1062 curDef.preconverted[curIdx] += valueIn;
1063 if (curDef.IsFractional())
1065 currencyState.reserves[curIdx] += valueIn;
1069 currencyState.supply += CCurrencyState::ReserveToNativeRaw(valueIn, currencyState.PriceInReserve(curIdx));
1072 if (transfer.second.second.currencyID == curDef.systemID)
1074 currencyState.nativeConversionFees += conversionFee;
1075 currencyState.nativeFees += conversionFee;
1077 currencyState.fees[curIdx] += conversionFee;
1078 currencyState.nativeFees += transfer.second.second.CalculateTransferFee(transfer.second.second.destination);
1079 currencyState.fees[nativeIdx] += transfer.second.second.CalculateTransferFee(transfer.second.second.destination);
1080 currencyState.conversionFees[curIdx] += conversionFee;
1085 if (curDef.IsFractional())
1087 // convert all non-native fees to native and update the price as a result
1088 bool isFeeConversion = false;
1089 int numCurrencies = curDef.currencies.size();
1090 std::vector<int64_t> reservesToConvert(numCurrencies, 0);
1091 std::vector<int64_t> fractionalToConvert(numCurrencies, 0);
1092 CAmount newSupply = currencyState.supply;
1094 for (int i = 0; i < numCurrencies; i++)
1096 curDef.conversions[i] = currencyState.conversionPrice[i] = currencyState.PriceInReserve(i, true);
1098 // all currencies except the native currency of the system will be converted to the native currency
1099 if (currencyState.fees[i] && curDef.currencies[i] != curDef.systemID)
1101 fractionalToConvert[nativeIdx] += currencyState.ReserveToNativeRaw(currencyState.fees[i], currencyState.conversionPrice[i]);
1102 newSupply += fractionalToConvert[i];
1103 isFeeConversion = true;
1106 currencyState.supply = newSupply;
1108 // convert all non-native fee currencies to native and adjust prices
1109 if (isFeeConversion)
1111 CCurrencyState converterState = static_cast<CCurrencyState>(currencyState);
1112 currencyState.viaConversionPrice =
1113 converterState.ConvertAmounts(reservesToConvert, fractionalToConvert, currencyState);
1117 // set initial supply from actual conversions if this is first update
1118 if (firstUpdate && curDef.IsFractional())
1120 CAmount calculatedSupply = 0;
1121 for (auto &transfer : unspentTransfers)
1123 if (transfer.second.second.IsPreConversion())
1125 CAmount toConvert = transfer.second.second.nValue - CReserveTransactionDescriptor::CalculateConversionFee(transfer.second.second.nValue);
1126 calculatedSupply += CCurrencyState::ReserveToNativeRaw(toConvert, currencyState.conversionPrice[currencyIndexes[transfer.second.second.currencyID]]);
1130 if (calculatedSupply > curDef.initialFractionalSupply)
1132 // get a ratio and reduce all prices by that ratio
1133 static arith_uint256 bigSatoshi(SATOSHIDEN);
1134 arith_uint256 bigMultipliedSupply(calculatedSupply * bigSatoshi);
1135 arith_uint256 newRatio(bigMultipliedSupply / curDef.initialFractionalSupply);
1136 // truncate up, not down, to prevent any overflow at all
1137 if (newRatio * curDef.initialFractionalSupply < bigMultipliedSupply)
1141 for (auto &rate : currencyState.conversionPrice)
1143 arith_uint256 numerator = rate * newRatio;
1144 rate = (numerator / bigSatoshi).GetLow64();
1145 if ((numerator - (bigSatoshi * rate)) > 0)
1152 calculatedSupply = 0;
1153 for (auto &transfer : unspentTransfers)
1155 if (transfer.second.second.IsPreConversion())
1157 CAmount toConvert = transfer.second.second.nValue - CReserveTransactionDescriptor::CalculateConversionFee(transfer.second.second.nValue);
1158 calculatedSupply += CCurrencyState::ReserveToNativeRaw(toConvert, currencyState.conversionPrice[currencyIndexes[transfer.second.second.currencyID]]);
1161 printf("Calculated supply %s\n", ValueFromAmount(calculatedSupply).write().c_str());
1163 // now, remove carveout percentage from each weight & reserve
1164 // for currency state
1165 int32_t preLaunchCarveOutTotal = 0;
1166 for (auto &carveout : curDef.preLaunchCarveOuts)
1168 preLaunchCarveOutTotal += carveout.second;
1171 static arith_uint256 bigSatoshi(SATOSHIDEN);
1172 for (auto &oneReserve : currencyState.reserves)
1174 oneReserve = ((arith_uint256(oneReserve) * arith_uint256(SATOSHIDEN - preLaunchCarveOutTotal)) / bigSatoshi).GetLow64();
1176 for (auto &oneWeight : currencyState.weights)
1178 oneWeight = ((arith_uint256(oneWeight) * arith_uint256(CCurrencyDefinition::CalculateRatioOfValue((SATOSHIDEN - preLaunchCarveOutTotal), SATOSHIDEN - curDef.preLaunchDiscount))) / bigSatoshi).GetLow64();
1182 currencyState.UpdateWithEmission(curDef.GetTotalPreallocation());
1184 return currencyState;
1187 CCoinbaseCurrencyState CConnectedChains::GetCurrencyState(CCurrencyDefinition &curDef, int32_t height, int32_t curDefHeight)
1189 uint160 chainID = curDef.GetID();
1190 CCoinbaseCurrencyState currencyState;
1191 std::vector<CAddressIndexDbEntry> notarizationIndex;
1193 if (chainID == ASSETCHAINS_CHAINID)
1196 if (IsVerusActive() ||
1197 CConstVerusSolutionVector::activationHeight.ActiveVersion(height) < CActivationHeight::ACTIVATE_PBAAS ||
1199 height > chainActive.Height() ||
1200 !chainActive[height] ||
1201 !ReadBlockFromDisk(block, chainActive[height], Params().GetConsensus()) ||
1202 !(currencyState = CCoinbaseCurrencyState(block.vtx[0])).IsValid())
1204 currencyState = GetInitialCurrencyState(thisChain);
1207 // if this is a token on this chain, it will be simply notarized
1208 else if (curDef.systemID == ASSETCHAINS_CHAINID)
1210 // get the last unspent notarization for this currency, which is valid by definition for a token
1211 CPBaaSNotarization notarization;
1212 if ((notarization.GetLastNotarization(chainID, EVAL_ACCEPTEDNOTARIZATION, curDefHeight, height) &&
1213 (currencyState = notarization.currencyState).IsValid()) ||
1214 (currencyState = GetInitialCurrencyState(curDef)).IsValid())
1216 if (!(notarization.IsValid() && notarization.notarizationHeight >= curDef.startBlock))
1219 currencyState.SetPrelaunch(true);
1220 currencyState = AddPrelaunchConversions(curDef,
1222 notarization.IsValid() ? notarization.notarizationHeight : curDefHeight,
1228 currencyState.SetPrelaunch(false);
1234 CChainNotarizationData cnd;
1235 uint32_t ecode = IsVerusActive() ?
1236 EVAL_ACCEPTEDNOTARIZATION :
1237 (chainID == notaryChain.GetID() ? EVAL_EARNEDNOTARIZATION : EVAL_ACCEPTEDNOTARIZATION);
1238 if (GetNotarizationData(chainID, ecode, cnd))
1240 int32_t transfersFrom = curDefHeight;
1241 if (cnd.lastConfirmed != -1)
1243 transfersFrom = cnd.vtx[cnd.lastConfirmed].second.notarizationHeight;
1245 int32_t transfersUntil = cnd.lastConfirmed == -1 ? curDef.startBlock - 1 :
1246 (cnd.vtx[cnd.lastConfirmed].second.notarizationHeight < curDef.startBlock ?
1247 (height < curDef.startBlock ? height : curDef.startBlock - 1) :
1248 cnd.vtx[cnd.lastConfirmed].second.notarizationHeight);
1249 if (transfersUntil < curDef.startBlock)
1251 // get chain transfers that should apply before the start block
1252 // until there is a post-start block notarization, we always consider the
1253 // currency state to be up to just before the start block
1254 std::multimap<uint160, std::pair<CInputDescriptor, CReserveTransfer>> unspentTransfers;
1255 if (GetChainTransfers(unspentTransfers, chainID, transfersFrom, transfersUntil))
1257 // at this point, all pre-allocation, minted, and pre-converted currency are included
1258 // in the currency state before final notarization
1259 std::map<uint160, int32_t> currencyIndexes = currencyState.GetReserveMap();
1260 if (curDef.IsFractional())
1262 currencyState.supply = curDef.initialFractionalSupply;
1266 // supply is determined by purchases * current conversion rate
1267 currencyState.supply = currencyState.initialSupply;
1270 for (auto &transfer : unspentTransfers)
1272 if (transfer.second.second.IsPreConversion())
1274 CAmount conversionFee = CReserveTransactionDescriptor::CalculateConversionFee(transfer.second.second.nValue);
1276 currencyState.reserveIn[currencyIndexes[transfer.second.second.currencyID]] += transfer.second.second.nValue;
1277 curDef.preconverted[currencyIndexes[transfer.second.second.currencyID]] += transfer.second.second.nValue;
1278 if (curDef.IsFractional())
1280 currencyState.reserves[currencyIndexes[transfer.second.second.currencyID]] += transfer.second.second.nValue - conversionFee;
1284 currencyState.supply += CCurrencyState::ReserveToNativeRaw(transfer.second.second.nValue - conversionFee, currencyState.PriceInReserve(currencyIndexes[transfer.second.second.currencyID]));
1287 if (transfer.second.second.currencyID == curDef.systemID)
1289 currencyState.nativeConversionFees += conversionFee;
1290 currencyState.nativeFees += conversionFee + transfer.second.second.CalculateTransferFee(transfer.second.second.destination);
1294 currencyState.fees[currencyIndexes[transfer.second.second.currencyID]] +=
1295 conversionFee + transfer.second.second.CalculateTransferFee(transfer.second.second.destination);
1296 currencyState.conversionFees[currencyIndexes[transfer.second.second.currencyID]] += conversionFee;
1299 else if (transfer.second.second.flags & CReserveTransfer::PREALLOCATE)
1301 currencyState.emitted += transfer.second.second.nValue;
1304 currencyState.supply += currencyState.emitted;
1305 if (curDef.conversions.size() != curDef.currencies.size())
1307 curDef.conversions = std::vector<int64_t>(curDef.currencies.size());
1309 for (int i = 0; i < curDef.conversions.size(); i++)
1311 currencyState.conversionPrice[i] = curDef.conversions[i] = currencyState.PriceInReserve(i);
1317 std::pair<uint256, CPBaaSNotarization> notPair = cnd.lastConfirmed != -1 ? cnd.vtx[cnd.lastConfirmed] : cnd.vtx[cnd.forks[cnd.bestChain][0]];
1318 currencyState = notPair.second.currencyState;
1322 return currencyState;
1325 CCoinbaseCurrencyState CConnectedChains::GetCurrencyState(const uint160 ¤cyID, int32_t height)
1327 int32_t curDefHeight;
1328 CCurrencyDefinition curDef;
1329 if (GetCurrencyDefinition(currencyID, curDef, &curDefHeight, true))
1331 return GetCurrencyState(curDef, height, curDefHeight);
1335 LogPrintf("%s: currency %s:%s not found\n", __func__, currencyID.GetHex().c_str(), EncodeDestination(CIdentityID(currencyID)).c_str());
1336 printf("%s: currency %s:%s not found\n", __func__, currencyID.GetHex().c_str(), EncodeDestination(CIdentityID(currencyID)).c_str());
1338 return CCoinbaseCurrencyState();
1341 CCoinbaseCurrencyState CConnectedChains::GetCurrencyState(int32_t height)
1343 return GetCurrencyState(thisChain.GetID(), height);
1346 bool CConnectedChains::SetLatestMiningOutputs(const std::vector<CTxOut> &minerOutputs)
1348 LOCK(cs_mergemining);
1349 latestMiningOutputs = minerOutputs;
1353 CCurrencyDefinition CConnectedChains::GetCachedCurrency(const uint160 ¤cyID)
1355 CCurrencyDefinition currencyDef;
1357 auto it = currencyDefCache.find(currencyID);
1358 if ((it != currencyDefCache.end() && !(currencyDef = it->second).IsValid()) ||
1359 (it == currencyDefCache.end() && !GetCurrencyDefinition(currencyID, currencyDef, &defHeight, true)))
1361 printf("%s: definition for transfer currency ID %s not found\n\n", __func__, EncodeDestination(CIdentityID(currencyID)).c_str());
1362 LogPrintf("%s: definition for transfer currency ID %s not found\n\n", __func__, EncodeDestination(CIdentityID(currencyID)).c_str());
1365 if (it == currencyDefCache.end())
1367 currencyDefCache[currencyID] = currencyDef;
1369 return currencyDefCache[currencyID];
1372 CCurrencyDefinition CConnectedChains::UpdateCachedCurrency(const uint160 ¤cyID, uint32_t height)
1374 // due to the main lock being taken on the thread that waits for transaction checks,
1375 // low level functions like this must be called either from a thread that holds LOCK(cs_main),
1376 // or script validation, where it is held either by this thread or one waiting for it.
1377 // in the long run, the daemon synchonrization model should be improved
1378 CCurrencyDefinition currencyDef = GetCachedCurrency(currencyID);
1379 CCoinbaseCurrencyState curState = GetCurrencyState(currencyDef, height);
1380 currencyDefCache[currencyID] = currencyDef;
1384 void CConnectedChains::AggregateChainTransfers(const CTxDestination &feeOutput, uint32_t nHeight)
1386 // all chains aggregate reserve transfer transactions, so aggregate and add all necessary export transactions to the mem pool
1393 std::multimap<uint160, std::pair<CInputDescriptor, CReserveTransfer>> transferOutputs;
1397 uint160 thisChainID = ConnectedChains.ThisChain().GetID();
1399 // get all available transfer outputs to aggregate into export transactions
1400 if (GetUnspentChainTransfers(transferOutputs))
1402 if (!transferOutputs.size())
1407 std::vector<pair<CInputDescriptor, CReserveTransfer>> txInputs;
1408 uint160 lastChain = transferOutputs.begin()->first;
1412 CCoinsViewCache view(&dummy);
1416 CCoinsViewMemPool viewMemPool(pcoinsTip, mempool);
1417 view.SetBackend(viewMemPool);
1419 auto outputIt = transferOutputs.begin();
1420 for (int outputsDone = 0; outputsDone <= transferOutputs.size(); outputIt++, outputsDone++)
1422 if (outputIt != transferOutputs.end())
1424 auto &output = *outputIt;
1425 if (output.first == lastChain)
1427 txInputs.push_back(output.second);
1432 CCurrencyDefinition destDef, systemDef;
1434 destDef = GetCachedCurrency(lastChain);
1435 systemDef = GetCachedCurrency(destDef.systemID);
1437 if (!destDef.IsValid())
1439 printf("%s: cannot find destination currency %s\n", __func__, EncodeDestination(CIdentityID(lastChain)).c_str());
1440 LogPrintf("%s: cannot find destination currency %s\n", __func__, EncodeDestination(CIdentityID(lastChain)).c_str());
1443 if (!systemDef.IsValid())
1445 printf("%s: cannot find destination system definition %s\n", __func__, EncodeDestination(CIdentityID(destDef.systemID)).c_str());
1446 LogPrintf("%s: cannot find destination system definition %s\n", __func__, EncodeDestination(CIdentityID(destDef.systemID)).c_str());
1450 // if destination is a token on the current chain, consider it its own system
1451 if (destDef.systemID == thisChainID)
1453 systemDef = destDef;
1456 // when we get here, we have a consecutive number of transfer outputs to consume in txInputs
1457 // we need an unspent export output to export, or use the last one of it is an export to the same
1459 std::multimap<uint160, pair<int, CInputDescriptor>> exportOutputs;
1460 CCurrencyDefinition lastChainDef = UpdateCachedCurrency(lastChain, nHeight);
1462 if (GetUnspentChainExports(lastChain, exportOutputs) && exportOutputs.size())
1464 std::pair<uint160, std::pair<int, CInputDescriptor>> lastExport = *exportOutputs.begin();
1466 bool oneFullSize = txInputs.size() >= CCrossChainExport::MIN_INPUTS;
1468 if (((nHeight - lastExport.second.first) >= CCrossChainExport::MIN_BLOCKS) || oneFullSize)
1470 boost::optional<CTransaction> oneExport;
1472 // make one or more transactions that spends the last export and all possible cross chain transfers
1473 while (txInputs.size())
1475 TransactionBuilder tb(Params().GetConsensus(), nHeight);
1477 int inputsLeft = txInputs.size();
1478 int numInputs = inputsLeft > CCrossChainExport::MAX_EXPORT_INPUTS ? CCrossChainExport::MAX_EXPORT_INPUTS : inputsLeft;
1480 if (numInputs > CCrossChainExport::MAX_EXPORT_INPUTS)
1482 numInputs = CCrossChainExport::MAX_EXPORT_INPUTS;
1484 inputsLeft = txInputs.size() - numInputs;
1486 // if we have already made one and don't have enough to make another
1487 // without going under the input minimum, wait until next time for the others
1488 if (numInputs > 0 && numInputs < CCrossChainExport::MIN_INPUTS && oneFullSize)
1493 // each time through, we make one export transaction with the remainder or a subset of the
1494 // reserve transfer inputs. inputs can be:
1495 // 1. transfers of reserve for fractional reserve chains
1496 // 2. pre-conversions for pre-launch participation in the premine
1497 // 3. reserve market conversions to send between Verus and a fractional reserve chain and always output the native coin
1499 // If we are on the Verus chain, all inputs will include native coins. On a PBaaS chain, inputs can either be native
1500 // or reserve token inputs.
1502 // On the Verus chain, total native amount, minus the fee, must be sent to the reserve address of the specific chain
1503 // as reserve deposit with native coin equivalent. Pre-conversions and conversions will be realized on the PBaaS chain
1504 // as part of the import process
1506 // If we are on the PBaaS chain, conversions must happen before coins are sent this way back to the reserve chain.
1507 // Verus reserve outputs can be directly aggregated and transferred, with fees paid through conversion and the
1508 // remaining Verus reserve coin will be burned on the PBaaS chain as spending it is allowed, once notarized, on the
1511 CCurrencyValueMap totalTxFees;
1512 CCurrencyValueMap totalAmounts;
1513 CAmount exportOutVal = 0;
1514 std::vector<CBaseChainObject *> chainObjects;
1516 // first, we must add the export output from the current export thread to this chain
1517 if (oneExport.is_initialized())
1519 // spend the last export transaction output
1520 CTransaction &tx = oneExport.get();
1523 for (j = 0; j < tx.vout.size(); j++)
1525 if (::IsPayToCryptoCondition(tx.vout[j].scriptPubKey, p) &&
1527 p.evalCode == EVAL_CROSSCHAIN_EXPORT)
1533 // had to be found and valid if we made the tx
1534 assert(j < tx.vout.size() && p.IsValid());
1536 tb.AddTransparentInput(COutPoint(tx.GetHash(), j), tx.vout[j].scriptPubKey, tx.vout[j].nValue);
1537 exportOutVal = tx.vout[j].nValue;
1538 lastExport.second.first = nHeight;
1539 lastExport.second.second = CInputDescriptor(tx.vout[j].scriptPubKey, tx.vout[j].nValue, CTxIn(tx.GetHash(), j));
1543 // spend the recentExportIt output
1544 tb.AddTransparentInput(lastExport.second.second.txIn.prevout, lastExport.second.second.scriptPubKey, lastExport.second.second.nValue);
1545 exportOutVal = lastExport.second.second.nValue;
1547 CTransaction lastExportTx;
1548 // we must find the export, or it doesn't exist
1549 if (!myGetTransaction(lastExport.second.second.txIn.prevout.hash, lastExportTx, blkHash))
1553 oneExport = lastExportTx;
1556 // combine any reserve deposits from the last export
1557 // into the reserve deposit outputs for this export and spend them
1558 // TODO: allow reserve deposits to hold multiple reserve token types
1559 // to make this more efficient for multi-reserves
1560 CCurrencyValueMap currentReserveDeposits;
1561 if (!view.GetCoins(lastExport.second.second.txIn.prevout.hash, coins))
1565 CTransaction &lastExportTx = oneExport.get();
1567 for (int i = 0; i < coins.vout.size(); i++)
1569 // import on same chain spends reserve deposits as well, so only spend them if they are not
1571 auto &oneOut = lastExportTx.vout[i];
1574 if (::IsPayToCryptoCondition(oneOut.scriptPubKey, p) &&
1576 p.evalCode == EVAL_RESERVE_DEPOSIT &&
1577 coins.IsAvailable(i))
1579 // only reserve deposits for the export currency/system will be present on an export transaction
1580 CCurrencyValueMap reserveOut = oneOut.scriptPubKey.ReserveOutValue().CanonicalMap();
1581 currentReserveDeposits += reserveOut;
1582 if (oneOut.nValue || reserveOut.valueMap.size())
1584 tb.AddTransparentInput(COutPoint(lastExport.second.second.txIn.prevout.hash, i),
1585 oneOut.scriptPubKey, oneOut.nValue);
1586 currentReserveDeposits.valueMap[ASSETCHAINS_CHAINID] += oneOut.nValue;
1592 std::vector<int> toRemove;
1594 for (int j = 0; j < numInputs; j++)
1596 tb.AddTransparentInput(txInputs[j].first.txIn.prevout, txInputs[j].first.scriptPubKey, txInputs[j].first.nValue, txInputs[j].first.txIn.nSequence);
1597 CCurrencyValueMap newTransferInput = txInputs[j].first.scriptPubKey.ReserveOutValue();
1598 newTransferInput.valueMap[ASSETCHAINS_CHAINID] = txInputs[j].first.nValue;
1600 totalTxFees += txInputs[j].second.CalculateFee(txInputs[j].second.flags, txInputs[j].second.nValue, lastChainDef.systemID);
1601 totalAmounts += newTransferInput;
1602 chainObjects.push_back(new CChainObject<CReserveTransfer>(ObjTypeCode(txInputs[j].second), txInputs[j].second));
1605 // remove in reverse order so one removal does not affect the position of the next
1606 for (int j = toRemove.size() - 1; j >= 0; j--)
1608 txInputs.erase(txInputs.begin() + toRemove[j]);
1612 // this logic may cause us to create a tx that will get rejected, but we will never wait too long
1613 if (!numInputs || (oneFullSize && (nHeight - lastExport.second.first) < CCrossChainExport::MIN_BLOCKS && numInputs < CCrossChainExport::MIN_INPUTS))
1618 //printf("%s: total export amounts:\n%s\n", __func__, totalAmounts.ToUniValue().write().c_str());
1619 CCrossChainExport ccx(lastChain, numInputs, totalAmounts.CanonicalMap(), totalTxFees.CanonicalMap());
1621 // make a fee output
1622 CReserveTransfer feeOut(CReserveTransfer::VALID + CReserveTransfer::FEE_OUTPUT,
1623 lastChainDef.systemID, 0, 0, lastChainDef.systemID, DestinationToTransferDestination(feeOutput));
1624 chainObjects.push_back(new CChainObject<CReserveTransfer>(ObjTypeCode(feeOut), feeOut));
1626 // do a preliminary check
1627 CReserveTransactionDescriptor rtxd;
1628 std::vector<CTxOut> vOutputs;
1629 CChainNotarizationData cnd;
1630 CCoinbaseCurrencyState currencyState;
1631 if (!GetNotarizationData(lastChain,
1632 !lastChainDef.IsToken() && lastChainDef.systemID == lastChain ? EVAL_EARNEDNOTARIZATION : EVAL_ACCEPTEDNOTARIZATION,
1635 printf("%s: failed to create valid exports\n", __func__);
1636 LogPrintf("%s: failed to create valid exports\n", __func__);
1640 currencyState = cnd.vtx[cnd.lastConfirmed].second.currencyState;
1642 if (!currencyState.IsValid() ||
1643 !rtxd.AddReserveTransferImportOutputs(ConnectedChains.ThisChain().GetID(), lastChainDef, currencyState, chainObjects, vOutputs))
1645 vOutputs = std::vector<CTxOut>();
1646 rtxd.AddReserveTransferImportOutputs(ConnectedChains.ThisChain().GetID(), lastChainDef, currencyState, chainObjects, vOutputs);
1647 DeleteOpRetObjects(chainObjects);
1649 printf("%s: failed to create valid exports\n", __func__);
1650 LogPrintf("%s: failed to create valid exports\n", __func__);
1653 printf("%s: failed to export outputs:\n", __func__);
1654 for (auto oneout : vOutputs)
1657 ScriptPubKeyToJSON(oneout.scriptPubKey, uniOut, false);
1658 printf("%s\n", uniOut.write(true, 2).c_str());
1664 CCcontract_info *cp;
1668 printf("%s: exported outputs:\n", __func__);
1669 for (auto &oneout : chainObjects)
1671 if (oneout->objectType == CHAINOBJ_RESERVETRANSFER)
1673 CReserveTransfer &rt = ((CChainObject<CReserveTransfer> *)(oneout))->object;
1674 printf("%s\n", rt.ToUniValue().write(true, 2).c_str());
1679 CScript opRet = StoreOpRetArray(chainObjects);
1680 DeleteOpRetObjects(chainObjects);
1682 // now send transferred currencies to a reserve deposit
1683 cp = CCinit(&CC, EVAL_RESERVE_DEPOSIT);
1685 currentReserveDeposits += ccx.totalAmounts;
1686 CCurrencyValueMap reserveDepositOut;
1687 CAmount nativeOut = 0;
1689 for (auto &oneCurrencyOut : currentReserveDeposits.valueMap)
1691 CCurrencyDefinition oneDef = currencyDefCache[oneCurrencyOut.first];
1693 // if the destination is this chain, or
1694 // the destination is another blockchain that does not control source currency, store in reserve
1695 if ((oneDef.systemID == ASSETCHAINS_CHAINID || oneDef.systemID != lastChainDef.systemID) &&
1696 oneCurrencyOut.second)
1698 if (oneCurrencyOut.first == ASSETCHAINS_CHAINID)
1700 nativeOut = oneCurrencyOut.second;
1704 reserveDepositOut.valueMap[oneCurrencyOut.first] = oneCurrencyOut.second;
1709 if (reserveDepositOut.valueMap.size() || nativeOut)
1711 // send the entire amount to a reserve deposit output of the specific chain
1712 // we receive our fee on the other chain, when it comes back, or if a token,
1713 // when it gets imported back to the chain
1714 std::vector<CTxDestination> dests({CPubKey(ParseHex(CC.CChexstr))});
1715 CReserveDeposit rd = CReserveDeposit(lastChain, reserveDepositOut);
1716 tb.AddTransparentOutput(MakeMofNCCScript(CConditionObj<CReserveDeposit>(EVAL_RESERVE_DEPOSIT, dests, 1, &rd)),
1720 cp = CCinit(&CC, EVAL_CROSSCHAIN_EXPORT);
1722 // send native amount of zero to a cross chain export output of the specific chain
1723 std::vector<CTxDestination> dests = std::vector<CTxDestination>({CPubKey(ParseHex(CC.CChexstr)).GetID()});
1725 tb.AddTransparentOutput(MakeMofNCCScript(CConditionObj<CCrossChainExport>(EVAL_CROSSCHAIN_EXPORT, dests, 1, &ccx)),
1728 // when exports are confirmed as having been imported, they are finalized
1729 // until then, a finalization UTXO enables an index search to only find transactions
1730 // that have work to complete on this chain, or have not had their cross-chain import
1732 cp = CCinit(&CC, EVAL_FINALIZE_EXPORT);
1733 CTransactionFinalization finalization(CTransactionFinalization::FINALIZE_EXPORT, lastChain, 0);
1735 //printf("%s: Finalizing export with index dest %s\n", __func__, EncodeDestination(CKeyID(CCrossChainRPCData::GetConditionID(lastChainDef.systemID, EVAL_FINALIZE_EXPORT))).c_str());
1737 dests = std::vector<CTxDestination>({CPubKey(ParseHex(CC.CChexstr)).GetID()});
1738 tb.AddTransparentOutput(MakeMofNCCScript(CConditionObj<CTransactionFinalization>(EVAL_FINALIZE_EXPORT, dests, 1, &finalization)), 0);
1743 /* UniValue uni(UniValue::VOBJ);
1744 TxToUniv(tb.mtx, uint256(), uni);
1745 printf("%s: about to send reserve deposits with tx:\n%s\n", __func__, uni.write(1,2).c_str()); */
1747 TransactionBuilderResult buildResult(tb.Build());
1749 if (!buildResult.IsError() && buildResult.IsTx())
1751 // replace the last one only if we have a valid new one
1752 CTransaction tx = buildResult.GetTxOrThrow();
1754 /* uni = UniValue(UniValue::VOBJ);
1755 TxToUniv(tx, uint256(), uni);
1756 printf("%s: successfully built tx:\n%s\n", __func__, uni.write(1,2).c_str()); */
1758 LOCK2(cs_main, mempool.cs);
1759 static int lastHeight = 0;
1760 // remove conflicts, so that we get in
1761 std::list<CTransaction> removed;
1762 mempool.removeConflicts(tx, removed);
1764 // add to mem pool, prioritize according to the fee we will get, and relay
1765 //printf("Created and signed export transaction %s\n", tx.GetHash().GetHex().c_str());
1766 //LogPrintf("Created and signed export transaction %s\n", tx.GetHash().GetHex().c_str());
1767 if (myAddtomempool(tx))
1770 uint256 hash = tx.GetHash();
1771 CAmount nativeExportFees = ccx.totalFees.valueMap[ASSETCHAINS_CHAINID];
1772 mempool.PrioritiseTransaction(hash, hash.GetHex(), (double)(nativeExportFees << 1), nativeExportFees);
1776 UniValue uni(UniValue::VOBJ);
1777 TxToUniv(tx, uint256(), uni);
1778 //printf("%s: created invalid transaction:\n%s\n", __func__, uni.write(1,2).c_str());
1779 LogPrintf("%s: created invalid transaction:\n%s\n", __func__, uni.write(1,2).c_str());
1785 // we can't do any more useful work for this chain if we failed here
1786 printf("Failed to create export transaction: %s\n", buildResult.GetError().c_str());
1787 LogPrintf("Failed to create export transaction: %s\n", buildResult.GetError().c_str());
1792 // erase the inputs we've attempted to spend
1793 txInputs.erase(txInputs.begin(), txInputs.begin() + numInputs);
1798 if (outputIt != transferOutputs.end())
1800 lastChain = outputIt->first;
1801 txInputs.push_back(outputIt->second);
1809 void CConnectedChains::SignAndCommitImportTransactions(const CTransaction &lastImportTx, const std::vector<CTransaction> &transactions)
1811 int nHeight = chainActive.LastTip()->GetHeight();
1812 uint32_t consensusBranchId = CurrentEpochBranchId(nHeight, Params().GetConsensus());
1813 LOCK2(cs_main, mempool.cs);
1815 uint256 lastHash, lastSignedHash;
1816 CCoinsViewCache view(pcoinsTip);
1818 // sign and commit the transactions
1819 for (auto &_tx : transactions)
1821 CMutableTransaction newTx(_tx);
1823 if (!lastHash.IsNull())
1825 //printf("last hash before signing: %s\n", lastHash.GetHex().c_str());
1826 for (auto &oneIn : newTx.vin)
1828 //printf("checking input with hash: %s\n", oneIn.prevout.hash.GetHex().c_str());
1829 if (oneIn.prevout.hash == lastHash)
1831 oneIn.prevout.hash = lastSignedHash;
1832 //printf("updated hash before signing: %s\n", lastSignedHash.GetHex().c_str());
1836 lastHash = _tx.GetHash();
1837 CTransaction tx = newTx;
1839 // sign the transaction and submit
1840 bool signSuccess = false;
1841 for (int i = 0; i < tx.vin.size(); i++)
1843 SignatureData sigdata;
1845 CScript outputScript;
1847 if (tx.vin[i].prevout.hash == lastImportTx.GetHash())
1849 value = lastImportTx.vout[tx.vin[i].prevout.n].nValue;
1850 outputScript = lastImportTx.vout[tx.vin[i].prevout.n].scriptPubKey;
1855 if (!view.GetCoins(tx.vin[i].prevout.hash, coins))
1857 fprintf(stderr,"%s: cannot get input coins from tx: %s, output: %d\n", __func__, tx.vin[i].prevout.hash.GetHex().c_str(), tx.vin[i].prevout.n);
1858 LogPrintf("%s: cannot get input coins from tx: %s, output: %d\n", __func__, tx.vin[i].prevout.hash.GetHex().c_str(), tx.vin[i].prevout.n);
1861 value = coins.vout[tx.vin[i].prevout.n].nValue;
1862 outputScript = coins.vout[tx.vin[i].prevout.n].scriptPubKey;
1865 signSuccess = ProduceSignature(TransactionSignatureCreator(nullptr, &tx, i, value, SIGHASH_ALL), outputScript, sigdata, consensusBranchId);
1869 fprintf(stderr,"%s: failure to sign transaction\n", __func__);
1870 LogPrintf("%s: failure to sign transaction\n", __func__);
1873 UpdateTransaction(newTx, i, sigdata);
1879 // push to local node and sync with wallets
1880 CValidationState state;
1881 bool fMissingInputs;
1882 CTransaction signedTx(newTx);
1885 //TxToJSON(tx, uint256(), jsonTX);
1886 //printf("signed transaction:\n%s\n", jsonTX.write(1, 2).c_str());
1888 if (!AcceptToMemoryPool(mempool, state, signedTx, false, &fMissingInputs)) {
1889 if (state.IsInvalid()) {
1890 //UniValue txUni(UniValue::VOBJ);
1891 //TxToUniv(signedTx, uint256(), txUni);
1892 //fprintf(stderr,"%s: rejected by memory pool for %s\n%s\n", __func__, state.GetRejectReason().c_str(), txUni.write(1,2).c_str());
1893 LogPrintf("%s: rejected by memory pool for %s\n", __func__, state.GetRejectReason().c_str());
1895 if (fMissingInputs) {
1896 fprintf(stderr,"%s: missing inputs\n", __func__);
1897 LogPrintf("%s: missing inputs\n", __func__);
1901 fprintf(stderr,"%s: rejected by memory pool for %s\n", __func__, state.GetRejectReason().c_str());
1902 LogPrintf("%s: rejected by memory pool for %s\n", __func__, state.GetRejectReason().c_str());
1909 UpdateCoins(signedTx, view, nHeight);
1910 lastSignedHash = signedTx.GetHash();
1920 CCurrencyValueMap CalculatePreconversions(const CCurrencyDefinition &chainDef, int32_t definitionHeight, CCurrencyValueMap &fees)
1922 // if we are getting information on the current chain, we assume that preconverted amounts have been
1923 // pre-calculated. otherwise, we will calculate them.
1924 CCurrencyValueMap retVal;
1925 if (chainDef.GetID() != ConnectedChains.ThisChain().GetID())
1927 std::multimap<uint160, pair<CInputDescriptor, CReserveTransfer>> transferInputs;
1928 CCurrencyValueMap preconvertedAmounts;
1930 if (GetChainTransfers(transferInputs, chainDef.GetID(), definitionHeight, chainDef.startBlock - 1, CReserveTransfer::PRECONVERT | CReserveTransfer::VALID))
1932 auto curMap = chainDef.GetCurrenciesMap();
1933 for (auto &transfer : transferInputs)
1935 if (!(transfer.second.second.flags & CReserveTransfer::PREALLOCATE) && curMap.count(transfer.second.second.currencyID))
1937 CAmount conversionFee = CReserveTransactionDescriptor::CalculateConversionFee(transfer.second.second.nValue);
1938 preconvertedAmounts.valueMap[transfer.second.second.currencyID] += (transfer.second.second.nValue - conversionFee);
1939 fees.valueMap[transfer.second.second.currencyID] += transfer.second.second.nFees + conversionFee;
1942 retVal = preconvertedAmounts;
1943 if (!chainDef.IsToken() && !(chainDef.ChainOptions() & chainDef.OPTION_FEESASRESERVE))
1951 retVal = CCurrencyValueMap(chainDef.currencies, chainDef.preconverted);
1956 // This creates a new token notarization input and output and attaches them to a new import transaction,
1957 // given the next export transaction about to be imported and its height
1958 bool CConnectedChains::NewImportNotarization(const CCurrencyDefinition &_curDef,
1960 const CTransaction &lastImportTx,
1961 uint32_t exportHeight,
1962 const CTransaction &exportTx,
1963 CMutableTransaction &mnewTx,
1964 CCoinbaseCurrencyState &oldCurState,
1965 CCoinbaseCurrencyState &newCurState)
1967 if (!_curDef.IsValid() || !_curDef.IsToken())
1969 LogPrintf("%s: cannot create import notarization for invalid or non-token currencies\n", __func__);
1973 uint160 currencyID = _curDef.GetID();
1975 CCurrencyDefinition curDef;
1976 int32_t curDefHeight;
1977 if (!GetCurrencyDefinition(currencyID, curDef, &curDefHeight))
1979 LogPrintf("%s: cannot create import notarization - currency not found\n", __func__);
1983 CPBaaSNotarization lastNotarization(lastImportTx);
1984 if (curDef.systemID == ASSETCHAINS_CHAINID && !lastNotarization.IsValid())
1986 LogPrintf("%s: error getting notarization transaction %s\n", __func__, lastImportTx.GetHash().GetHex().c_str());
1990 CCoinbaseCurrencyState initialCurrencyState = lastNotarization.currencyState;
1992 bool isDefinition = false;
1994 // if our last notarization is prior to the start block, then we need to get our
1995 // initial currency state from a new start
1996 if (lastNotarization.notarizationHeight < curDef.startBlock)
1998 initialCurrencyState = ConnectedChains.GetCurrencyState(curDef, curDef.startBlock - 1, curDefHeight);
1999 isDefinition = true;
2000 if (height >= curDef.startBlock)
2002 initialCurrencyState.SetPrelaunch(false);
2006 CCrossChainExport ccx(exportTx);
2009 LogPrintf("%s: invalid export transaction %s\n", __func__, lastImportTx.GetHash().GetHex().c_str());
2013 if (!lastNotarization.IsValid())
2015 CChainNotarizationData cnd;
2016 // TODO: right now, there is no notarization made until after the launch
2017 // we should roll up periodic notarizations and pay miners to do it, making
2018 // long pre-launch periods possible
2019 if (GetNotarizationData(curDef.GetID(), EVAL_ACCEPTEDNOTARIZATION, cnd) && cnd.vtx.size() && cnd.lastConfirmed >= 0)
2021 lastNotarization = cnd.vtx[cnd.lastConfirmed].second;
2022 initialCurrencyState = lastNotarization.currencyState;
2026 LogPrintf("%s: cannot get last notarization for %s\n", __func__, curDef.name.c_str());
2031 oldCurState = initialCurrencyState;
2033 CBlockIndex *pindex;
2034 CTxDestination notarizationID = VERUS_DEFAULTID.IsNull() ? CTxDestination(CIdentityID(currencyID)) : CTxDestination(VERUS_DEFAULTID);
2036 // if this is the first notarization after start, make the notarization and determine if we should
2038 if (isDefinition || height == curDef.startBlock -1)
2040 bool refunding = false;
2042 pindex = chainActive[curDef.startBlock];
2044 // check if the chain is qualified for a refund
2045 CCurrencyValueMap minPreMap, fees;
2046 CCurrencyValueMap preConvertedMap = CCurrencyValueMap(curDef.currencies, curDef.preconverted).CanonicalMap();
2047 newCurState = initialCurrencyState;
2049 if (curDef.minPreconvert.size() && curDef.minPreconvert.size() == curDef.currencies.size())
2051 minPreMap = CCurrencyValueMap(curDef.currencies, curDef.minPreconvert).CanonicalMap();
2054 if (minPreMap.valueMap.size() && preConvertedMap < minPreMap)
2056 // we force the supply to zero
2057 // in any case where there was less than minimum participation,
2058 // the result of the supply cannot be zero, enabling us to easily determine that this
2059 // represents a failed launch
2060 newCurState.supply = 0;
2061 newCurState.SetRefunding(true);
2064 else if (curDef.IsFractional() &&
2065 exportTx.vout.size() &&
2066 exportTx.vout.back().scriptPubKey.IsOpReturn())
2068 // we are not refunding, and it is possible that we also have
2069 // normal conversions in addition to pre-conversions. add any conversions that may
2070 // be present into the new currency state
2071 CReserveTransactionDescriptor rtxd;
2072 std::vector<CBaseChainObject *> exportObjects;
2073 std::vector<CTxOut> vOutputs;
2075 exportObjects = RetrieveOpRetArray(exportTx.vout.back().scriptPubKey);
2077 bool isValidExport = rtxd.AddReserveTransferImportOutputs(currencyID, curDef, initialCurrencyState, exportObjects, vOutputs, &newCurState);
2080 // on definition, initial state is correct
2081 newCurState = initialCurrencyState;
2083 DeleteOpRetObjects(exportObjects);
2086 LogPrintf("%s: invalid export opreturn for transaction %s\n", __func__, exportTx.GetHash().GetHex().c_str());
2093 if (chainActive.Height() > height)
2095 pindex = chainActive[height];
2099 pindex = chainActive.LastTip();
2104 LogPrintf("%s: invalid active chain\n", __func__);
2108 // this is not the first notarization, so the last notarization will let us know if this is a refund or not
2109 CCurrencyValueMap minPreMap;
2111 newCurState = initialCurrencyState;
2113 if (curDef.minPreconvert.size() && curDef.minPreconvert.size() == curDef.currencies.size())
2115 minPreMap = CCurrencyValueMap(curDef.currencies, curDef.minPreconvert).CanonicalMap();
2118 // we won't change currency state in notarizations after failure to launch, if success, recalculate as needed
2119 if (!(lastNotarization.currencyState.IsRefunding()))
2121 // calculate new currency state from this import
2122 // we are not refunding, and it is possible that we also have
2123 // normal conversions in addition to pre-conversions. add any conversions that may
2124 // be present into the new currency state
2125 CReserveTransactionDescriptor rtxd;
2126 std::vector<CBaseChainObject *> exportObjects;
2127 std::vector<CTxOut> vOutputs;
2129 exportObjects = RetrieveOpRetArray(exportTx.vout.back().scriptPubKey);
2131 bool isValidExport = rtxd.AddReserveTransferImportOutputs(currencyID, curDef, initialCurrencyState, exportObjects, vOutputs, &newCurState);
2132 if (isValidExport && curDef.IsFractional())
2134 // we want the new price and the old state as a starting point to ensure no rounding error impact
2136 CCoinbaseCurrencyState tempCurState = initialCurrencyState;
2137 tempCurState.conversionPrice = newCurState.conversionPrice;
2139 rtxd = CReserveTransactionDescriptor();
2140 isValidExport = rtxd.AddReserveTransferImportOutputs(currencyID, curDef, tempCurState, exportObjects, vOutputs, &newCurState);
2142 else if (!curDef.IsFractional())
2144 newCurState = initialCurrencyState;
2146 DeleteOpRetObjects(exportObjects);
2149 LogPrintf("%s: invalid export opreturn for transaction %s\n", __func__, exportTx.GetHash().GetHex().c_str());
2155 uint256 lastImportTxHash = lastImportTx.GetHash();
2157 // now, add the initial notarization to the import tx
2158 // we will begin refund or import after notarization is accepted and returned by GetNotarizationData
2159 CPBaaSNotarization pbn = CPBaaSNotarization(curDef.notarizationProtocol,
2163 chainActive.GetMMV().GetRoot(),
2164 chainActive.GetMMRNode(pindex->GetHeight()).hash,
2165 ArithToUint256(GetCompactPower(pindex->nNonce, pindex->nBits, pindex->nVersion)),
2168 lastNotarization.notarizationHeight,
2169 uint256(), 0, COpRetProof(), std::vector<CNodeData>());
2171 // create notarization output
2173 CCcontract_info *cp;
2175 std::vector<CTxDestination> dests;
2177 // make the accepted notarization output
2178 cp = CCinit(&CC, EVAL_ACCEPTEDNOTARIZATION);
2180 if (curDef.notarizationProtocol == curDef.NOTARIZATION_NOTARY_CHAINID)
2182 dests = std::vector<CTxDestination>({CIdentityID(currencyID)});
2184 else if (curDef.notarizationProtocol == curDef.NOTARIZATION_AUTO)
2186 dests = std::vector<CTxDestination>({CPubKey(ParseHex(CC.CChexstr))});
2192 mnewTx.vout.push_back(CTxOut(0, MakeMofNCCScript(CConditionObj<CPBaaSNotarization>(EVAL_ACCEPTEDNOTARIZATION, dests, 1, &pbn))));
2194 // make the finalization output
2195 cp = CCinit(&CC, EVAL_FINALIZE_NOTARIZATION);
2197 if (curDef.notarizationProtocol == curDef.NOTARIZATION_AUTO)
2199 dests = std::vector<CTxDestination>({CPubKey(ParseHex(CC.CChexstr))});
2202 // finish transaction by adding the prior input and finalization, sign, then put it in the mempool
2203 // all output for notarizing will be paid as mining fees, so there's no need to relay
2204 uint32_t confirmedOut, finalizeOut;
2205 if (!GetNotarizationAndFinalization(EVAL_ACCEPTEDNOTARIZATION, lastImportTx, pbn, &confirmedOut, &finalizeOut))
2207 printf("ERROR: could not find expected initial notarization for currency %s\n", curDef.name.c_str());
2211 mnewTx.vin.push_back(CTxIn(COutPoint(lastImportTxHash, confirmedOut)));
2212 mnewTx.vin.push_back(CTxIn(COutPoint(lastImportTxHash, finalizeOut)));
2214 // we need to store the input that we confirmed if we spent finalization outputs
2215 CTransactionFinalization nf(CTransactionFinalization::FINALIZE_NOTARIZATION, pbn.currencyID, mnewTx.vin.size() - 1);
2217 // update crypto condition with final notarization output data
2218 mnewTx.vout.push_back(CTxOut(0,
2219 MakeMofNCCScript(CConditionObj<CTransactionFinalization>(EVAL_FINALIZE_NOTARIZATION, dests, 1, &nf))));
2224 // process token related, local imports and exports
2225 void CConnectedChains::ProcessLocalImports()
2227 // first determine all export threads on the current chain that are valid to import
2228 std::multimap<uint160, std::pair<int, CInputDescriptor>> exportOutputs;
2229 std::multimap<uint160, CTransaction> importThreads;
2230 uint160 thisChainID = thisChain.GetID();
2232 LOCK2(cs_main, mempool.cs);
2233 uint32_t nHeight = chainActive.Height();
2235 // get all pending, local exports and put them into a map
2236 std::vector<std::pair<CAddressUnspentKey, CAddressUnspentValue>> unspentOutputs;
2237 std::map<uint160, std::pair<uint32_t, CTransaction>> currenciesToImport; // height of earliest tx
2238 CCurrencyDefinition oneCurrency;
2240 //printf("%s: Searching for %s\n", __func__, EncodeDestination(CKeyID(ConnectedChains.ThisChain().GetConditionID(EVAL_FINALIZE_EXPORT))).c_str());
2241 if (GetAddressUnspent(ConnectedChains.ThisChain().GetConditionID(EVAL_FINALIZE_EXPORT), CScript::P2IDX, unspentOutputs))
2243 CCrossChainExport ccx, ccxDummy;
2244 CTransaction txOut, txImport;
2245 CPartialTransactionProof lastExport;
2246 CCrossChainImport cci;
2248 for (auto &oneOut : unspentOutputs)
2251 if (oneOut.second.blockHeight <= nHeight &&
2252 oneOut.second.script.IsPayToCryptoCondition(p) &&
2254 p.evalCode == EVAL_FINALIZE_EXPORT &&
2256 CTransactionFinalization(p.vData[0]).IsValid() &&
2257 myGetTransaction(oneOut.first.txhash, txOut, blkHash) &&
2258 (ccx = CCrossChainExport(txOut)).IsValid() &&
2259 !currenciesToImport.count(ccx.systemID) &&
2260 (oneCurrency = GetCachedCurrency(ccx.systemID)).IsValid() &&
2261 oneCurrency.startBlock <= nHeight &&
2262 GetLastImport(ccx.systemID, txImport, lastExport, cci, ccxDummy))
2264 auto blockIt = mapBlockIndex.find(blkHash);
2265 if (blockIt != mapBlockIndex.end() && chainActive.Contains(blockIt->second))
2267 currenciesToImport.insert(make_pair(ccx.systemID, make_pair(blockIt->second->GetHeight(), txImport)));
2273 CMutableTransaction txTemplate = CreateNewContextualCMutableTransaction(Params().GetConsensus(), nHeight);
2274 for (auto &oneIT : currenciesToImport)
2276 std::vector<CTransaction> importTxes;
2277 int32_t importOutNum = 0;
2279 CCrossChainImport oneImportInput(oneIT.second.second, &importOutNum);
2280 if (oneImportInput.IsValid())
2282 std::vector<CAddressUnspentDbEntry> reserveDeposits;
2283 GetAddressUnspent(ConnectedChains.GetCachedCurrency(oneIT.first).GetConditionID(EVAL_RESERVE_DEPOSIT), CScript::P2IDX, reserveDeposits);
2284 CCurrencyValueMap tokenImportAvailable;
2285 CAmount nativeImportAvailable = 0;
2286 for (auto &oneOut : reserveDeposits)
2288 nativeImportAvailable += oneOut.second.satoshis;
2289 tokenImportAvailable += oneOut.second.script.ReserveOutValue();
2290 //printf("nativeImportAvailable:%ld, tokenImportAvailable:%s\n", nativeImportAvailable, tokenImportAvailable.ToUniValue().write().c_str());
2292 nativeImportAvailable += oneIT.second.second.vout[importOutNum].nValue;
2293 tokenImportAvailable += oneIT.second.second.vout[importOutNum].ReserveOutValue();
2294 //printf("nativeImportAvailable:%ld, tokenImportAvailable:%s\n", nativeImportAvailable, tokenImportAvailable.ToUniValue().write().c_str());
2295 if (CreateLatestImports(currencyDefCache[oneIT.first], oneIT.second.second, txTemplate, CTransaction(), tokenImportAvailable, nativeImportAvailable, importTxes))
2297 // fund the first import transaction with all reserveDeposits
2298 // change amounts are passed through on the import thread
2300 // TODO: manage when this becomes to large by splitting reserve deposits over the
2302 if (importTxes.size())
2304 CMutableTransaction oneImport = importTxes[0];
2306 CCrossChainImport cci(importTxes[0], &outNum);
2309 // add the reserve deposit inputs to the first transaction
2310 // the outputs should have been automatically propagated through
2311 // fixup inputs if necessary
2312 if (reserveDeposits.size())
2314 UniValue jsonTX(UniValue::VOBJ);
2315 std::vector<uint256> prevHashes;
2317 for (int i = 0; i < importTxes.size() - 1; i++)
2319 prevHashes.push_back(importTxes[i].GetHash());
2322 // TODO - get reserve deposits from exports, not all at once, as in refunds
2323 for (auto &oneOut : reserveDeposits)
2325 oneImport.vin.push_back(CTxIn(oneOut.first.txhash, oneOut.first.index));
2328 importTxes[0] = oneImport;
2330 for (int i = 0; i < importTxes.size() - 1; i++)
2332 oneImport = importTxes[i + 1];
2333 for (auto &oneIn : oneImport.vin)
2335 if (oneIn.prevout.hash == prevHashes[i])
2337 //printf("updating hash before signing to new value\nold: %s\nnew: %s\n", oneIn.prevout.hash.GetHex().c_str(), importTxes[i].GetHash().GetHex().c_str());
2338 oneIn.prevout.hash = importTxes[i].GetHash();
2341 importTxes[i + 1] = oneImport;
2345 SignAndCommitImportTransactions(oneIT.second.second, importTxes);
2353 void CConnectedChains::SubmissionThread()
2357 arith_uint256 lastHash;
2358 int64_t lastImportTime = 0;
2359 uint32_t lastHeight = 0;
2361 // wait for something to check on, then submit blocks that should be submitted
2364 boost::this_thread::interruption_point();
2366 if (IsVerusActive())
2368 // blocks get discarded after no refresh for 5 minutes by default, probably should be more often
2369 //printf("SubmissionThread: pruning\n");
2370 PruneOldChains(GetAdjustedTime() - 300);
2371 bool submit = false;
2373 LOCK(cs_mergemining);
2374 if (mergeMinedChains.size() == 0 && qualifiedHeaders.size() != 0)
2376 qualifiedHeaders.clear();
2378 submit = qualifiedHeaders.size() != 0 && mergeMinedChains.size() != 0;
2380 //printf("SubmissionThread: qualifiedHeaders.size(): %lu, mergeMinedChains.size(): %lu\n", qualifiedHeaders.size(), mergeMinedChains.size());
2384 //printf("SubmissionThread: calling submit qualified blocks\n");
2385 SubmitQualifiedBlocks();
2388 ProcessLocalImports();
2392 sem_submitthread.wait();
2397 // if this is a PBaaS chain, poll for presence of Verus / root chain and current Verus block and version number
2398 if (CheckVerusPBaaSAvailable())
2400 // check to see if we have recently earned a block with an earned notarization that qualifies for
2401 // submitting an accepted notarization
2402 if (earnedNotarizationHeight)
2405 int32_t txIndex = -1, height;
2407 LOCK(cs_mergemining);
2408 if (earnedNotarizationHeight && earnedNotarizationHeight <= chainActive.Height() && earnedNotarizationBlock.GetHash() == chainActive[earnedNotarizationHeight]->GetBlockHash())
2410 blk = earnedNotarizationBlock;
2411 earnedNotarizationBlock = CBlock();
2412 txIndex = earnedNotarizationIndex;
2413 height = earnedNotarizationHeight;
2414 earnedNotarizationHeight = 0;
2420 //printf("SubmissionThread: testing notarization\n");
2421 CTransaction lastConfirmed;
2422 uint256 txId = CreateAcceptedNotarization(blk, txIndex, height);
2426 printf("Submitted notarization for acceptance: %s\n", txId.GetHex().c_str());
2427 LogPrintf("Submitted notarization for acceptance: %s\n", txId.GetHex().c_str());
2432 // every "n" seconds, look for imports to include in our blocks from the Verus chain
2433 if ((GetAdjustedTime() - lastImportTime) >= 30 || lastHeight < (chainActive.LastTip() ? 0 : chainActive.LastTip()->GetHeight()))
2435 lastImportTime = GetAdjustedTime();
2436 lastHeight = (chainActive.LastTip() ? 0 : chainActive.LastTip()->GetHeight());
2438 // see if our notary has a confirmed notarization for us
2439 UniValue params(UniValue::VARR);
2442 params.push_back(thisChain.name);
2446 result = find_value(RPCCallRoot("getlastimportin", params), "result");
2447 } catch (exception e)
2449 result = NullUniValue;
2452 if (!result.isNull())
2454 auto txUniStr = find_value(result, "lastimporttransaction");
2455 auto txLastConfirmedStr = find_value(result, "lastconfirmednotarization");
2456 auto txTemplateStr = find_value(result, "importtxtemplate");
2457 CAmount nativeImportAvailable = uni_get_int64(find_value(result, "nativeimportavailable"));
2458 CCurrencyValueMap tokenImportAvailable(find_value(params[0], "tokenimportavailable"));
2460 CTransaction lastImportTx, lastConfirmedTx, templateTx;
2462 if (txUniStr.isStr() && txTemplateStr.isStr() &&
2463 DecodeHexTx(lastImportTx, txUniStr.get_str()) &&
2464 DecodeHexTx(lastConfirmedTx, txLastConfirmedStr.get_str()) &&
2465 DecodeHexTx(templateTx, txTemplateStr.get_str()))
2467 std::vector<CTransaction> importTxes;
2468 if (CreateLatestImports(notaryChain.chainDefinition, lastImportTx, templateTx, lastConfirmedTx, tokenImportAvailable, nativeImportAvailable, importTxes))
2470 for (auto importTx : importTxes)
2474 params.push_back(EncodeHexTx(importTx));
2478 txResult = find_value(RPCCallRoot("signrawtransaction", params), "result");
2479 if (txResult.isObject() && !(txResult = find_value(txResult, "hex")).isNull() && txResult.isStr() && txResult.get_str().size())
2482 params.push_back(txResult);
2483 txResult = find_value(RPCCallRoot("sendrawtransaction", params), "result");
2487 txResult = NullUniValue;
2490 } catch (exception e)
2492 txResult = NullUniValue;
2495 if (txResult.isStr())
2497 testId.SetHex(txResult.get_str());
2499 if (testId.IsNull())
2511 boost::this_thread::interruption_point();
2514 catch (const boost::thread_interrupted&)
2516 LogPrintf("Verus merge mining thread terminated\n");
2520 void CConnectedChains::SubmissionThreadStub()
2522 ConnectedChains.SubmissionThread();
2525 void CConnectedChains::QueueEarnedNotarization(CBlock &blk, int32_t txIndex, int32_t height)
2527 // called after winning a block that contains an earned notarization
2528 // the earned notarization and its height are queued for processing by the submission thread
2529 // when a new notarization is added, older notarizations are removed, but all notarizations in the current height are
2531 LOCK(cs_mergemining);
2533 // we only care about the last
2534 earnedNotarizationHeight = height;
2535 earnedNotarizationBlock = blk;
2536 earnedNotarizationIndex = txIndex;
2539 bool IsChainDefinitionInput(const CScript &scriptSig)
2542 return scriptSig.IsPayToCryptoCondition(&ecode) && ecode == EVAL_CURRENCY_DEFINITION;