]> Git Repo - VerusCoin.git/commitdiff
Token refund and launch fixes
authormiketout <[email protected]>
Wed, 6 May 2020 03:07:35 +0000 (20:07 -0700)
committermiketout <[email protected]>
Wed, 6 May 2020 03:07:35 +0000 (20:07 -0700)
src/pbaas/pbaas.cpp
src/pbaas/pbaas.h
src/rpc/pbaasrpc.cpp
src/script/interpreter.cpp

index 553c0523a155f03c47f20f79e467e4c4e0b371a3..9f655e566c26e0518cf6325e56477e844cb93f07 100644 (file)
@@ -1470,8 +1470,8 @@ void CConnectedChains::SignAndCommitImportTransactions(const CTransaction &lastI
 
             if (!signSuccess)
             {
-                fprintf(stderr,"%s: failure to sign refund transaction\n", __func__);
-                LogPrintf("%s: failure to sign refund transaction\n", __func__);
+                fprintf(stderr,"%s: failure to sign transaction\n", __func__);
+                LogPrintf("%s: failure to sign transaction\n", __func__);
                 break;
             } else {
                 UpdateTransaction(newTx, i, sigdata);
@@ -1506,16 +1506,41 @@ void CConnectedChains::SignAndCommitImportTransactions(const CTransaction &lastI
                 }
                 break;
             }
-            else
-            {
-                RelayTransaction(signedTx);
-            }
         }
     }
 }
 
 bool RefundFailedLaunch(uint160 currencyID, CTransaction &lastImportTx, std::vector<CTransaction> &newRefunds, std::string &errorReason);
 
+CCurrencyValueMap CalculatePreconversions(const CCurrencyDefinition &chainDef, int32_t definitionHeight)
+{
+    // if we are getting information on the current chain, we assume that preconverted amounts have been
+    // pre-calculated. otherwise, we will calculate them.
+    CCurrencyValueMap retVal;
+    if (chainDef.GetID() != ConnectedChains.ThisChain().GetID())
+    {
+        std::multimap<uint160, pair<CInputDescriptor, CReserveTransfer>> transferInputs;
+        CCurrencyValueMap preconvertedAmounts;
+        CCurrencyValueMap fees;
+        bool isReserve = chainDef.ChainOptions() & CCurrencyDefinition::OPTION_FRACTIONAL;
+
+        if (GetChainTransfers(transferInputs, chainDef.GetID(), definitionHeight, chainDef.startBlock, CReserveTransfer::PRECONVERT | CReserveTransfer::VALID))
+        {
+            for (auto transfer : transferInputs)
+            {
+                preconvertedAmounts.valueMap[transfer.second.second.currencyID] += transfer.second.second.nValue;
+                fees.valueMap[transfer.second.second.currencyID] += transfer.second.second.nFees;
+            }
+        }
+    }
+    else
+    {
+        retVal = CCurrencyValueMap(chainDef.currencies, chainDef.preconverted);
+    }
+    
+    return retVal;
+}
+
 // process token related, local imports and exports
 void CConnectedChains::ProcessLocalImports()
 {
@@ -1567,10 +1592,11 @@ void CConnectedChains::ProcessLocalImports()
                     minPreMap = CCurrencyValueMap(exportDef.currencies, exportDef.minPreconvert).CanonicalMap();
                 }
 
-                if (cnd.vtx.size() == 1)
+                if (cnd.vtx.back().second.prevHeight == 0)
                 {
                     // check if the chain is qualified for a refund
                     CCurrencyValueMap minPreMap, preConvertedMap;
+                    exportDef.preconverted = CalculatePreconversions(exportDef, defHeight).AsCurrencyVector(exportDef.currencies);
                     CCoinbaseCurrencyState initialCur = GetInitialCurrencyState(exportDef);
                     if (minPreMap > preConvertedMap &&
                         (preConvertedMap = CCurrencyValueMap(exportDef.currencies, initialCur.reserveIn)) < minPreMap)
@@ -1594,7 +1620,7 @@ void CConnectedChains::ProcessLocalImports()
                                                                 chainActive.GetMMRNode(exportDef.startBlock).hash,
                                                                 ArithToUint256(GetCompactPower(pindex->nNonce, pindex->nBits, pindex->nVersion)),
                                                                 initialCur,
-                                                                notarizations[0].second,
+                                                                notarizations[0].first.GetHash(),
                                                                 defHeight,
                                                                 uint256(), 0, COpRetProof(), std::vector<CNodeData>());
 
@@ -1612,43 +1638,92 @@ void CConnectedChains::ProcessLocalImports()
                     // make the accepted notarization output
                     cp = CCinit(&CC, EVAL_ACCEPTEDNOTARIZATION);
 
-                    if (exportDef.notarizationProtocol != CCurrencyDefinition::NOTARIZATION_AUTO)
+                    if (exportDef.notarizationProtocol == exportDef.NOTARIZATION_NOTARY_CHAINID)
                     {
-                        // we can't do anything with an unknown notarization protocol
-                        continue;
+                        dests = std::vector<CTxDestination>({CIdentityID(exportThread.first)});
                     }
-                    else
+                    else if (exportDef.notarizationProtocol == exportDef.NOTARIZATION_AUTO)
                     {
                         dests = std::vector<CTxDestination>({CPubKey(ParseHex(CC.CChexstr))});
                     }
-
+                    else
+                    {
+                        continue;
+                    }
                     indexDests = std::vector<CTxDestination>({CKeyID(exportDef.GetConditionID(EVAL_ACCEPTEDNOTARIZATION))});
-                    mnewTx.vout.push_back(CTxOut(0, MakeMofNCCScript(CConditionObj<CPBaaSNotarization>(EVAL_EARNEDNOTARIZATION, dests, 1, &pbn), &indexDests)));
+                    mnewTx.vout.push_back(CTxOut(0, MakeMofNCCScript(CConditionObj<CPBaaSNotarization>(EVAL_ACCEPTEDNOTARIZATION, dests, 1, &pbn), &indexDests)));
 
-                    // finish transaction by adding the prior input and put it in the mempool
-                    // all output for notarizing will be paid as mining fees, so there's no need to relay
-                    int i;
-                    for (i = 0; i < notarizations[0].first.vout.size(); i++)
+                    // make the finalization output
+                    cp = CCinit(&CC, EVAL_FINALIZENOTARIZATION);
+
+                    if (exportDef.notarizationProtocol == exportDef.NOTARIZATION_AUTO)
                     {
-                        COptCCParams p;
-                        if (notarizations[0].first.vout[i].scriptPubKey.IsPayToCryptoCondition(p) && p.evalCode == EVAL_ACCEPTEDNOTARIZATION)
-                        {
-                            break;
-                        }
+                        dests = std::vector<CTxDestination>({CPubKey(ParseHex(CC.CChexstr))});
+                    }
+                    else
+                    {
+                        continue;
                     }
-                    if (i == notarizations[0].first.vout.size())
+
+                    // finish transaction by adding the prior input and finalization, sign, then put it in the mempool
+                    // all output for notarizing will be paid as mining fees, so there's no need to relay
+                    uint32_t confirmedOut, finalizeOut;
+                    if (!GetNotarizationAndFinalization(EVAL_ACCEPTEDNOTARIZATION, notarizations[0].first, pbn, &confirmedOut, &finalizeOut))
                     {
                         printf("ERROR: could not find expected initial notarization for currency %s\n", exportDef.name.c_str());
                         continue;
                     }
-                    mnewTx.vin.push_back(CTxIn(COutPoint(notarizations[0].second, i)));
-                    CTransaction notarizationTx(mnewTx);
+
+                    mnewTx.vin.push_back(CTxIn(COutPoint(notarizations[0].first.GetHash(), confirmedOut)));
+                    mnewTx.vin.push_back(CTxIn(COutPoint(notarizations[0].first.GetHash(), finalizeOut)));
+
+                    // we need to store the input that we confirmed if we spent finalization outputs
+                    CNotarizationFinalization nf(0);
+
+                    indexDests = std::vector<CTxDestination>({CKeyID(exportDef.GetConditionID(EVAL_FINALIZENOTARIZATION))});
+
+                    // update crypto condition with final notarization output data
+                    mnewTx.vout.push_back(CTxOut(PBAAS_MINNOTARIZATIONOUTPUT, 
+                            MakeMofNCCScript(CConditionObj<CNotarizationFinalization>(EVAL_FINALIZENOTARIZATION, dests, 1, &nf), &indexDests)));
+
+                    CTransaction ntx(mnewTx);
+
+                    uint32_t consensusBranchId = CurrentEpochBranchId(chainActive.LastTip()->GetHeight(), Params().GetConsensus());
+
+                    // sign the transaction and submit
+                    for (int i = 0; i < ntx.vin.size(); i++)
+                    {
+                        bool signSuccess;
+                        SignatureData sigdata;
+                        CAmount value;
+                        const CScript *pScriptPubKey;
+
+                        pScriptPubKey = &(notarizations[0].first.vout[ntx.vin[i].prevout.n].scriptPubKey);
+                        value = notarizations[0].first.vout[ntx.vin[i].prevout.n].nValue;
+
+                        extern CWallet *pwalletMain;
+                        signSuccess = ProduceSignature(TransactionSignatureCreator(pwalletMain, &ntx, i, value, SIGHASH_ALL), *pScriptPubKey, sigdata, consensusBranchId);
+
+                        if (!signSuccess)
+                        {
+                            printf("%s: failure to sign notarization\n", __func__);
+                            continue;
+                        } else {
+                            UpdateTransaction(mnewTx, i, sigdata);
+                        }
+                    }
+
+                    // add to mempool and submit transaction
+                    ntx = mnewTx;
+
                     CValidationState state;
-                    if (!myAddtomempool(notarizationTx, &state))
+                    if (!myAddtomempool(ntx, &state))
                     {
                         if (state.GetRejectCode() != REJECT_DUPLICATE)
                         {
-                            printf("ERROR: could not create launch notarization for currency %s: %s\n", exportDef.name.c_str(), state.GetRejectReason().c_str());
+                            UniValue uniTx(UniValue::VOBJ);
+                            TxToUniv(ntx, uint256(), uniTx);
+                            printf("ERROR: could not create launch notarization for currency %s: %s\ntx: %s\n", exportDef.name.c_str(), state.GetRejectReason().c_str(), uniTx.write(1,2).c_str());
                         }
                     }
                     // we will import after the launch notarization is mined/staked
@@ -1656,7 +1731,10 @@ void CConnectedChains::ProcessLocalImports()
                 }
                 else if (cnd.vtx.size() > 1)
                 {
-                    refunding = true;
+                    if (minPreMap.valueMap.size() && !cnd.vtx[cnd.bestChain].second.currencyState.supply)
+                    {
+                        refunding = true;
+                    }
                 }
                 else
                 {
@@ -1789,9 +1867,11 @@ void CConnectedChains::SubmissionThread()
                     //printf("SubmissionThread: calling submit qualified blocks\n");
                     SubmitQualifiedBlocks();
                 }
-                else
+
+                ProcessLocalImports();
+
+                if (!submit)
                 {
-                    ProcessLocalImports();
                     sem_submitthread.wait();
                 }
             }
index 714e92596c1a9cfccae39c4be561056085c3ebbf..cf230f08e24785dc95e0d00f039c04455483e2dd 100644 (file)
@@ -1096,5 +1096,6 @@ const uint256 &CurrencyDefHash(UniValue &chainDefinition);
 extern CConnectedChains ConnectedChains;
 extern uint160 ASSETCHAINS_CHAINID;
 CCoinbaseCurrencyState GetInitialCurrencyState(CCurrencyDefinition &chainDef);
+CCurrencyValueMap CalculatePreconversions(const CCurrencyDefinition &chainDef, int32_t definitionHeight);
 
 #endif
index 0298c92e8ffea8c594c3456d6bbffb4827c70624..eca397c0163fc3a40827b360ef9eff00d8d6c3b9 100644 (file)
@@ -2268,7 +2268,6 @@ UniValue submitacceptednotarization(const UniValue& params, bool fHelp)
                 CAmount value;
                 const CScript *pScriptPubKey;
 
-                // if this is our coinbase input, we won't find it elsewhere
                 if (i < notarizationInputs.size())
                 {
                     pScriptPubKey = &notarizationInputs[i].scriptPubKey;
@@ -2784,35 +2783,6 @@ UniValue listreservetransactions(const UniValue& params, bool fHelp)
     return NullUniValue;
 }
 
-CCurrencyValueMap CalculatePreconversions(const CCurrencyDefinition &chainDef, int32_t definitionHeight)
-{
-    // if we are getting information on the current chain, we assume that preconverted amounts have been
-    // pre-calculated. otherwise, we will calculate them.
-    CCurrencyValueMap retVal;
-    if (chainDef.GetID() != ConnectedChains.ThisChain().GetID())
-    {
-        std::multimap<uint160, pair<CInputDescriptor, CReserveTransfer>> transferInputs;
-        CCurrencyValueMap preconvertedAmounts;
-        CCurrencyValueMap fees;
-        bool isReserve = chainDef.ChainOptions() & CCurrencyDefinition::OPTION_FRACTIONAL;
-
-        if (GetChainTransfers(transferInputs, chainDef.GetID(), definitionHeight, chainDef.startBlock, CReserveTransfer::PRECONVERT | CReserveTransfer::VALID))
-        {
-            for (auto transfer : transferInputs)
-            {
-                preconvertedAmounts.valueMap[transfer.second.second.currencyID] += transfer.second.second.nValue;
-                fees.valueMap[transfer.second.second.currencyID] += transfer.second.second.nFees;
-            }
-        }
-    }
-    else
-    {
-        retVal = CCurrencyValueMap(chainDef.currencies, chainDef.preconverted);
-    }
-    
-    return retVal;
-}
-
 // this must be called after all initial contributions are updated in the currency definition.
 CCoinbaseCurrencyState GetInitialCurrencyState(CCurrencyDefinition &chainDef)
 {
index 3bb7c8075368d4d472d01d48435ab44949469609..e87d4d2f807deb8eaabc8ecf57e7dd49296eb6d5 100644 (file)
@@ -1663,7 +1663,6 @@ int TransactionSignatureChecker::CheckCryptoCondition(
                 nHashType = signatures.sigHashType;
                 for (auto &sig : signatures.signatures)
                 {
-
                     if (sig.second.sigType != sig.second.SIGTYPE_SECP256K1 || !sig.second.signature.size() || !cc_ApplySecp256k1Signature(outputCC, sig.second.pubKeyData.data(), sig.first.begin(), sig.second.signature.data()))
                     {
                         success = false;
This page took 0.040347 seconds and 4 git commands to generate.