From 619958c2a40844cda9807f6c41a0528ba4bef6cc Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 18 Jan 2019 00:59:14 -1100 Subject: [PATCH] Merge earlier to expire locked funds --- src/cc/marmara.cpp | 43 +++++++++++++++++++++++++++++++++++++------ 1 file changed, 37 insertions(+), 6 deletions(-) diff --git a/src/cc/marmara.cpp b/src/cc/marmara.cpp index 5260df2ae..ec41cc3e8 100644 --- a/src/cc/marmara.cpp +++ b/src/cc/marmara.cpp @@ -398,20 +398,51 @@ int64_t AddMarmarainputs(CMutableTransaction &mtx,std::vector &pubkeys, UniValue MarmaraLock(uint64_t txfee,int64_t amount,int32_t height) { CMutableTransaction mtx = CreateNewContextualCMutableTransaction(Params().GetConsensus(), komodo_nextheight()); - UniValue result(UniValue::VOBJ); struct CCcontract_info *cp,C; CPubKey Marmarapk,mypk; int32_t unlockht; int64_t inputsum,change = 0; std::string rawtx,errorstr; + UniValue result(UniValue::VOBJ); struct CCcontract_info *cp,C; CPubKey Marmarapk,mypk,pk; int32_t unlockht,refunlockht,ht,numvouts; int64_t nValue,inputsum,threshold,remains,change = 0; std::string rawtx,errorstr; char coinaddr[64]; uint256 txid,hashBlock; CTransaction tx; uint8_t funcid; if ( txfee == 0 ) txfee = 10000; cp = CCinit(&C,EVAL_MARMARA); mypk = pubkey2pk(Mypubkey()); Marmarapk = GetUnspendable(cp,0); inputsum = AddNormalinputs(mtx,mypk,amount + txfee,1); + mtx.vout.push_back(MakeCC1of2vout(EVAL_MARMARA,amount,Marmarapk,mypk)); if ( inputsum < amount+txfee ) { - unlockht = MarmaraUnlockht(height); + remains = (amount + txfee) - inputsum; + std::vector > unspentOutputs; + GetCCaddress1of2(cp,coinaddr,Marmarapk,mypk); + SetCCunspents(unspentOutputs,coinaddr); + threshold = remains / 16; + refunlockht = MarmaraUnlockht(height); + for (std::vector >::const_iterator it=unspentOutputs.begin(); it!=unspentOutputs.end(); it++) + { + txid = it->first.txhash; + vout = (int32_t)it->first.index; + if ( (nValue= it->second.satoshis) < threshold ) + continue; + if ( GetTransaction(txid,tx,hashBlock,false) != 0 && (numvouts= tx.vout.size()) > 0 && vout < numvouts && tx.vout[vout].scriptPubKey.IsPayToCryptoCondition() != 0 && myIsutxo_spentinmempool(txid,vout) == 0 ) + { + if ( (funcid= DecodeMaramaraCoinbaseOpRet(tx.vout[numvouts-1].scriptPubKey,pk,ht,unlockht)) == 'C' || funcid == 'P' || funcid == 'L' ) + { + if ( unlockht < refunlockht ) + { + mtx.vin.push_back(CTxIn(txid,vout,CScript())); + fprintf(stderr,"merge CC vout %s/v%d %.8f unlockht.%d < ref.%d\n",txid.GetHex().c_str(),vout,(double)nValue/COIN,unlockht,refunlockht); + inputsum += nValue; + remains -= nValue; + if ( inputsum >= amount + txfee ) + { + fprintf(stderr,"inputsum %.8f >= amount %.8f, update amount\n",(double)inputsum/COIN,(double)amount/COIN); + amount = inputsum; + break; + } + } + } + } + } } - if ( inputsum > 0 ) + if ( inputsum >= amount+txfee ) { - mtx.vout.push_back(MakeCC1of2vout(EVAL_MARMARA,amount,Marmarapk,mypk)); if ( inputsum > amount+txfee ) { change = (inputsum - amount); @@ -426,13 +457,13 @@ UniValue MarmaraLock(uint64_t txfee,int64_t amount,int32_t height) result.push_back(Pair("rawtx",rawtx)); return(result); } - } + } else errorstr = (char *)"insufficient funds"; result.push_back(Pair("result",(char *)"error")); result.push_back(Pair("error",errorstr)); return(result); } -// decide on what unlockht settlement change should have +// jl777: decide on what unlockht settlement change should have UniValue MarmaraSettlement(uint64_t txfee,uint256 refbatontxid) {