diff --git a/src/cc/dapps/oraclefeed.c b/src/cc/dapps/oraclefeed.c index d6cf753f964..d97df1cd90e 100644 --- a/src/cc/dapps/oraclefeed.c +++ b/src/cc/dapps/oraclefeed.c @@ -236,7 +236,7 @@ void *loadfile(char *fname,uint8_t **bufp,long *lenp,long *allocsizep) { fclose(fp); *lenp = 0; - printf("loadfile null size.(%s)\n",fname); + //printf("loadfile null size.(%s)\n",fname); return(0); } if ( filesize > buflen ) @@ -340,10 +340,12 @@ cJSON *get_komodocli(char *refcoin,char **retstrp,char *acname,char *method,char #ifdef TESTMODE fprintf(stderr,"jsonstr.(%s)\n",jsonstr); #endif // TESTMODE - if ( (jsonstr[0] != '{' && jsonstr[0] != '[') || (retjson= cJSON_Parse(jsonstr)) == 0 ) + if ( (jsonstr[0] != '{' && jsonstr[0] != '[') || (retjson= cJSON_Parse(jsonstr)) == 0) *retstrp = jsonstr; else free(jsonstr); } + else if ( (jsonstr= filestr(&fsize,"/tmp/oraclefeed.error")) != 0 ) + *retstrp = jsonstr; return(retjson); } @@ -355,7 +357,7 @@ bits256 komodobroadcast(char *refcoin,char *acname,cJSON *hexjson) { if ( (retjson= get_komodocli(refcoin,&retstr,acname,"sendrawtransaction",hexstr,"","","")) != 0 ) { - //fprintf(stderr,"broadcast.(%s)\n",jprint(retjson,0)); + if (strcmp("error",jstr(retjson,"result"))==0) printf("%s\n",jstr(retjson,"error")); free_json(retjson); } else if ( retstr != 0 ) @@ -533,7 +535,7 @@ cJSON *get_addressutxos(char *refcoin,char *acname,char *coinaddr) } else if ( retstr != 0 ) { - fprintf(stderr,"get_addressutxos.(%s) error.(%s)\n",acname,retstr); + //fprintf(stderr,"get_addressutxos.(%s) error.(%s)\n",acname,retstr); free(retstr); } return(0); @@ -692,14 +694,14 @@ char *createrawtx(char *refcoin,char *acname,char *depositaddr,char *withdrawadd return(txstr); } -cJSON *addsignature(char *refcoin,char *acname,char *rawtx) +cJSON *addsignature(char *refcoin,char *acname,char *rawtx, int M) { char *retstr,*hexstr; cJSON *retjson; if ( (retjson= get_komodocli(refcoin,&retstr,acname,"signrawtransaction",rawtx,"","","")) != 0 ) { if ( is_cJSON_True(jobj(retjson,"complete")) != 0 ) return(retjson); - else if ((hexstr=jstr(retjson,"hex"))!= 0 && strlen(hexstr) > strlen(rawtx) + 2 ) + else if ((hexstr=jstr(retjson,"hex"))!= 0 && strlen(hexstr) > strlen(rawtx) + (M*2) + 1) { jaddnum(retjson,"partialtx",1); return(retjson); @@ -719,7 +721,8 @@ bits256 gatewayspartialsign(char *refcoin,char *acname,bits256 txid,char *coin,c char str[65],*retstr; cJSON *retjson; if ( (retjson= get_komodocli(refcoin,&retstr,acname,"gatewayspartialsign",bits256_str(str,txid),coin,hex,"")) != 0 ) { - txid=komodobroadcast(refcoin,acname,retjson); + if (strcmp("error",jstr(retjson,"result"))!=0) txid=komodobroadcast(refcoin,acname,retjson); + else printf("%s\n",jstr(retjson,"error")); free(retjson); return (txid); } @@ -733,10 +736,11 @@ bits256 gatewayspartialsign(char *refcoin,char *acname,bits256 txid,char *coin,c bits256 gatewayscompletesigning(char *refcoin,char *acname,bits256 withtxid,char *coin,char *hex) { - char str[65],str2[65],*retstr; cJSON *retjson; bits256 txid; + char str[65],*retstr; cJSON *retjson; bits256 txid; if ( (retjson= get_komodocli(refcoin,&retstr,acname,"gatewayscompletesigning",bits256_str(str,withtxid),coin,hex,"")) != 0 ) { - txid=komodobroadcast(refcoin,acname,retjson); + if (strcmp("error",jstr(retjson,"result"))!=0) txid=komodobroadcast(refcoin,acname,retjson); + else printf("%s\n",jstr(retjson,"error")); free(retjson); return (txid); } @@ -753,7 +757,8 @@ bits256 gatewaysmarkdone(char *refcoin,char *acname,bits256 withtxid,char *coin) char str[65],str2[65],*retstr; cJSON *retjson; bits256 txid; if ( (retjson= get_komodocli(refcoin,&retstr,acname,"gatewaysmarkdone",bits256_str(str,withtxid),coin,"","")) != 0 ) { - txid=komodobroadcast(refcoin,acname,retjson); + if (strcmp("error",jstr(retjson,"result"))!=0) txid=komodobroadcast(refcoin,acname,retjson); + else printf("%s\n",jstr(retjson,"error")); free(retjson); return (txid); } @@ -905,7 +910,7 @@ void update_gatewayspending(char *refcoin,char *acname,char *bindtxidstr,int32_t rawtx = createrawtx(refcoin,"",depositaddr,withdrawaddr,txidaddr,satoshis); if ( rawtx != 0 ) { - if ( (clijson=addsignature(refcoin,"",rawtx)) != 0 && is_cJSON_True(jobj(clijson,"complete")) != 0) + if ( (clijson=addsignature(refcoin,"",rawtx,M)) != 0 && is_cJSON_True(jobj(clijson,"complete")) != 0) { txid=gatewayscompletesigning("KMD",acname,withdrawtxid,refcoin,jstr(clijson,"hex")); if (txid.txid!=zeroid.txid) fprintf(stderr,"#WITHDRAW (%s) complete signing tx sent - %s\n",bits256_str(str,withdrawtxid),bits256_str(str1,txid)); @@ -925,26 +930,26 @@ void update_gatewayspending(char *refcoin,char *acname,char *bindtxidstr,int32_t } else rawtx=jstr(item,"hex"); K=jint(item,"number_of_signs"); - if ( rawtx!=0 && (clijson= addsignature(refcoin,"",rawtx)) != 0 ) + if ( rawtx!=0 && (clijson=addsignature(refcoin,"",rawtx,M)) != 0 ) { if ( is_cJSON_True(jobj(clijson,"complete")) != 0 ) { txid=gatewayscompletesigning("KMD",acname,lasttxid,refcoin,jstr(clijson,"hex")); - if (txid.txid!=zeroid.txid) fprintf(stderr,"#WITHDRAW (%s) complete signing tx (%dof%d) sent - %s\n",bits256_str(str,withdrawtxid),M,N,bits256_str(str1,txid)); - else fprintf(stderr,"error broadcasting tx on %s",acname); + if (txid.txid!=zeroid.txid) fprintf(stderr,"### SIGNING withdraw %s %dof%d\n",bits256_str(str,withdrawtxid),K+1,N); + else fprintf(stderr,"### SIGNING error broadcasting tx on %s\n",acname); } else if ( jint(clijson,"partialtx") != 0 ) { txid=gatewayspartialsign("KMD",acname,lasttxid,refcoin,jstr(clijson,"hex")); - if (txid.txid!=zeroid.txid) fprintf(stderr,"#WITHDRAW (%s) partial tx (%d/%dof%d) sent - %s\n",bits256_str(str,withdrawtxid),K+1,M,N,bits256_str(str1,txid)); - else fprintf(stderr,"error broadcasting tx on %s",acname); + if (txid.txid!=zeroid.txid) fprintf(stderr,"### SIGNING withdraw %s %d/%dof%d\n",bits256_str(str,withdrawtxid),K+1,M,N); + else fprintf(stderr,"### SIGNING error broadcasting tx on %s\n",acname); } free_json(clijson); processed++; - } - free(rawtx); - } - } + if ( lasttxid.txid==withdrawtxid.txid) free(rawtx); + } + } + } } } } @@ -966,20 +971,22 @@ void update_gatewayspending(char *refcoin,char *acname,char *bindtxidstr,int32_t double amount = jdouble(item,"amount"); if (validateaddress(refcoin,"",txidaddr,"iswatchonly")==0 && validateaddress(refcoin,"",txidaddr,"ismine")==0) importaddress(refcoin,"",txidaddr,jstr(item,"withdrawtxid"),0); - if (txidaddr != 0 && markerexists(refcoin,"",txidaddr)==0) + if (is_cJSON_True(jobj(item,"confirmed_or_notarized")) && txidaddr != 0 && markerexists(refcoin,"",txidaddr)==0) { cointxid = komodobroadcast(refcoin,"",item); if ( bits256_nonz(cointxid) != 0 ) { - withdrawaddr = jstr(item,"withdrawaddr"); + withdrawaddr = jstr(item,"withdrawaddr"); + fprintf(stderr,"### WITHDRAW %.8f %s sent to %s\n",amount,refcoin,withdrawaddr); txid=gatewaysmarkdone("KMD",acname,completetxid,refcoin); - if (txid.txid!=zeroid.txid) fprintf(stderr,"#WITHDRAW (%s) markdone tx sent %s - %.8f %s to %s broadcasted on %s (%s)\n",bits256_str(str,withdrawtxid),bits256_str(str1,txid),amount,refcoin,withdrawaddr,refcoin,bits256_str(str2,cointxid)); + if (txid.txid!=zeroid.txid) fprintf(stderr,"### MARKDONE withdraw %s\n",bits256_str(str,withdrawtxid)); } - else fprintf(stderr,"error broadcasting tx on %s",refcoin); + else fprintf(stderr,"### WITHDRAW error broadcasting tx on %s\n",refcoin); } } } } + free_json(retjson); } } diff --git a/src/cc/gateways.cpp b/src/cc/gateways.cpp index 4e2f64184aa..5c8de254df8 100644 --- a/src/cc/gateways.cpp +++ b/src/cc/gateways.cpp @@ -178,15 +178,15 @@ uint8_t DecodeGatewaysBindOpRet(char *depositaddr,const CScript &scriptPubKey,ui if ( N > 1 ) { strcpy(depositaddr,CBitcoinAddress(CScriptID(GetScriptForMultisig(M,gatewaypubkeys))).ToString().c_str()); - //fprintf(stderr,"f.%c M.%d of N.%d size.%d -> %s\n",f,M,N,(int32_t)gatewaypubkeys.size(),depositaddr); + //LogPrint("gatewayscc","f.%c M.%d of N.%d size.%d -> %s\n",f,M,N,(int32_t)gatewaypubkeys.size(),depositaddr); } else Getscriptaddress(depositaddr,CScript() << ParseHex(HexStr(gatewaypubkeys[0])) << OP_CHECKSIG); } else { - fprintf(stderr,"need to generate non-KMD addresses prefix.%d\n",prefix); + LogPrint("gatewayscc","need to generate non-KMD addresses prefix.%d\n",prefix); } return(f); - } else fprintf(stderr,"error decoding bind opret\n"); + } else LogPrint("gatewayscc","error decoding bind opret\n"); return(0); } @@ -363,15 +363,15 @@ bool GatewaysExactAmounts(struct CCcontract_info *cp,Eval* eval,const CTransacti numvouts = tx.vout.size(); for (i=0; iismyvin)(tx.vin[i].scriptSig) != 0 ) { - //fprintf(stderr,"vini.%d check mempool\n",i); + //LogPrint("gatewayscc","vini.%d check mempool\n",i); if ( eval->GetTxUnconfirmed(tx.vin[i].prevout.hash,vinTx,hashBlock) == 0 ) return eval->Invalid("cant find vinTx"); else { - //fprintf(stderr,"vini.%d check hash and vout\n",i); + //LogPrint("gatewayscc","vini.%d check hash and vout\n",i); if ( hashBlock == zerohash ) return eval->Invalid("cant Gateways from mempool"); if ( (assetoshis= IsGatewaysvout(cp,vinTx,tx.vin[i].prevout.n)) != 0 ) @@ -381,13 +381,13 @@ bool GatewaysExactAmounts(struct CCcontract_info *cp,Eval* eval,const CTransacti } for (i=0; iInvalid("mismatched inputs != outputs + txfee"); } else return(true); @@ -407,7 +407,7 @@ static int32_t myIs_coinaddr_inmempoolvout(char *coinaddr) Getscriptaddress(destaddr,tx.vout[i].scriptPubKey); if ( strcmp(destaddr,coinaddr) == 0 ) { - fprintf(stderr,"found (%s) vout in mempool\n",coinaddr); + LogPrint("gatewayscc","found (%s) vout in mempool\n",coinaddr); return(1); } } @@ -421,35 +421,35 @@ uint256 GatewaysReverseScan(uint256 &txid,int32_t height,uint256 reforacletxid,u CTransaction tx; uint256 hash,mhash,bhash,hashBlock,oracletxid; int32_t len,len2,numvouts; int64_t val,merkleht; CPubKey pk; std::vectordata; txid = zeroid; char str[65]; - //fprintf(stderr,"start reverse scan %s\n",uint256_str(str,batontxid)); + //LogPrint("gatewayscc","start reverse scan %s\n",uint256_str(str,batontxid)); while ( myGetTransaction(batontxid,tx,hashBlock) != 0 && (numvouts= tx.vout.size()) > 0 ) { - //fprintf(stderr,"check %s\n",uint256_str(str,batontxid)); + //LogPrint("gatewayscc","check %s\n",uint256_str(str,batontxid)); if ( DecodeOraclesData(tx.vout[numvouts-1].scriptPubKey,oracletxid,bhash,pk,data) == 'D' && oracletxid == reforacletxid ) { - //fprintf(stderr,"decoded %s\n",uint256_str(str,batontxid)); + //LogPrint("gatewayscc","decoded %s\n",uint256_str(str,batontxid)); if ( oracle_format(&hash,&merkleht,0,'I',(uint8_t *)data.data(),0,(int32_t)data.size()) == sizeof(int32_t) && merkleht == height ) { len = oracle_format(&hash,&val,0,'h',(uint8_t *)data.data(),sizeof(int32_t),(int32_t)data.size()); len2 = oracle_format(&mhash,&val,0,'h',(uint8_t *)data.data(),(int32_t)(sizeof(int32_t)+sizeof(uint256)),(int32_t)data.size()); - char str2[65]; fprintf(stderr,"found merkleht.%d len.%d len2.%d %s %s\n",(int32_t)merkleht,len,len2,uint256_str(str,hash),uint256_str(str2,mhash)); + char str2[65]; LogPrint("gatewayscc","found merkleht.%d len.%d len2.%d %s %s\n",(int32_t)merkleht,len,len2,uint256_str(str,hash),uint256_str(str2,mhash)); if ( len == sizeof(hash)+sizeof(int32_t) && len2 == 2*sizeof(mhash)+sizeof(int32_t) && mhash != zeroid ) { txid = batontxid; - //fprintf(stderr,"set txid\n"); + //LogPrint("gatewayscc","set txid\n"); return(mhash); } else { - //fprintf(stderr,"missing hash\n"); + //LogPrint("gatewayscc","missing hash\n"); return(zeroid); } - } //else fprintf(stderr,"height.%d vs search ht.%d\n",(int32_t)merkleht,(int32_t)height); + } //else LogPrint("gatewayscc","height.%d vs search ht.%d\n",(int32_t)merkleht,(int32_t)height); batontxid = bhash; - //fprintf(stderr,"new hash %s\n",uint256_str(str,batontxid)); + //LogPrint("gatewayscc","new hash %s\n",uint256_str(str,batontxid)); } else break; } - fprintf(stderr,"end of loop\n"); + LogPrint("gatewayscc","end of loop\n"); return(zeroid); } @@ -486,20 +486,25 @@ int64_t GatewaysVerify(char *refdepositaddr,uint256 oracletxid,int32_t claimvout if ( myGetTransaction(oracletxid,tx,hashBlock) == 0 || (numvouts= tx.vout.size()) <= 0 ) { - fprintf(stderr,"GatewaysVerify cant find oracletxid %s\n",uint256_str(str,oracletxid)); + LogPrint("gatewayscc","GatewaysVerify cant find oracletxid %s\n",uint256_str(str,oracletxid)); return(0); } if ( DecodeOraclesCreateOpRet(tx.vout[numvouts-1].scriptPubKey,name,description,format) != 'C' || name != refcoin ) { - fprintf(stderr,"GatewaysVerify mismatched oracle name %s != %s\n",name.c_str(),refcoin.c_str()); + LogPrint("gatewayscc","GatewaysVerify mismatched oracle name %s != %s\n",name.c_str(),refcoin.c_str()); return(0); } proofroot = BitcoinGetProofMerkleRoot(proof,txids); if ( proofroot != merkleroot ) { - fprintf(stderr,"GatewaysVerify mismatched merkleroot %s != %s\n",uint256_str(str,proofroot),uint256_str(str2,merkleroot)); + LogPrint("gatewayscc","GatewaysVerify mismatched merkleroot %s != %s\n",uint256_str(str,proofroot),uint256_str(str2,merkleroot)); return(0); } + if (std::find(txids.begin(), txids.end(), cointxid) == txids.end()) + { + LogPrint("gatewayscc", "GatewaysVerify invalid proof for this cointxid\n"); + return 0; + } if ( DecodeHexTx(tx,deposithex) != 0 ) { Getscriptaddress(claimaddr,tx.vout[claimvout].scriptPubKey); @@ -516,13 +521,13 @@ int64_t GatewaysVerify(char *refdepositaddr,uint256 oracletxid,int32_t claimvout break; } } - } else fprintf(stderr,"claimaddr.(%s) != destpubaddr.(%s)\n",claimaddr,destpubaddr); + } else LogPrint("gatewayscc","claimaddr.(%s) != destpubaddr.(%s)\n",claimaddr,destpubaddr); } if ( txid == cointxid ) { - fprintf(stderr,"verified proof for cointxid in merkleroot\n"); + LogPrint("gatewayscc","verified proof for cointxid in merkleroot\n"); return(nValue); - } else fprintf(stderr,"(%s) != (%s) or txid %s mismatch.%d or script mismatch\n",refdepositaddr,destaddr,uint256_str(str,txid),txid != cointxid); + } else LogPrint("gatewayscc","(%s) != (%s) or txid %s mismatch.%d or script mismatch\n",refdepositaddr,destaddr,uint256_str(str,txid),txid != cointxid); return(0); } @@ -534,7 +539,7 @@ int64_t GatewaysDepositval(CTransaction tx,CPubKey mypk) if ( DecodeGatewaysDepositOpRet(tx.vout[numvouts-1].scriptPubKey,bindtxid,coin,publishers,txids,height,cointxid,claimvout,deposithex,proof,claimpubkey,amount) == 'D' && claimpubkey == mypk ) { // coin, bindtxid, publishers - fprintf(stderr,"need to validate deposittxid more\n"); + //LogPrint("gatewayscc","need to validate deposittxid more\n"); return(amount); } } @@ -557,7 +562,7 @@ int32_t GatewaysBindExists(struct CCcontract_info *cp,CPubKey gatewayspk,uint256 { if ( tokenid == reftokenid ) { - fprintf(stderr,"trying to bind an existing tokenid\n"); + LogPrint("gatewayscc","trying to bind an existing tokenid\n"); return(1); } } @@ -585,8 +590,6 @@ bool GatewaysValidate(struct CCcontract_info *cp,Eval *eval,const CTransaction & uint256 hashblock,txid,bindtxid,deposittxid,withdrawtxid,completetxid,tokenid,tmptokenid,oracletxid,bindtokenid,cointxid,tmptxid,merkleroot,mhash; CTransaction bindtx,tmptx; std::string refcoin,tmprefcoin,hex,name,description,format; CPubKey pubkey,tmppubkey,gatewayspk; - // fprintf(stderr,"return true without gateways validation\n"); - // return(true); numvins = tx.vin.size(); numvouts = tx.vout.size(); preventCCvins = preventCCvouts = -1; @@ -594,25 +597,15 @@ bool GatewaysValidate(struct CCcontract_info *cp,Eval *eval,const CTransaction & return eval->Invalid("no vouts"); else { - // for (i=0; iInvalid("illegal normal vini"); - // } - // } - //fprintf(stderr,"check amounts\n"); + //LogPrint("gatewayscc","check amounts\n"); // if ( GatewaysExactAmounts(cp,eval,tx,1,10000) == false ) // { - // fprintf(stderr,"Gatewaysget invalid amount\n"); + // LogPrint("gatewayscc","Gatewaysget invalid amount\n"); // return false; // } // else // { - // txid = tx.GetHash(); - // memcpy(hash,&txid,sizeof(hash)); - gatewayspk = GetUnspendable(cp,0); - + gatewayspk = GetUnspendable(cp,0); if ( (funcid = DecodeGatewaysOpRet(tx.vout[numvouts-1].scriptPubKey)) != 0) { switch ( funcid ) @@ -710,7 +703,6 @@ bool GatewaysValidate(struct CCcontract_info *cp,Eval *eval,const CTransaction & merkleroot = zeroid; for (i=m=0; iInvalid("tokenid does not match tokenid from gatewaysbind"); else if (komodo_txnotarizedconfirmed(bindtxid) == false) return eval->Invalid("gatewaysbind tx is not yet confirmed(notarised)!"); - else if (K!=M) + else if (KInvalid("invalid number of signs!"); break; case 'M': @@ -832,34 +824,24 @@ bool GatewaysValidate(struct CCcontract_info *cp,Eval *eval,const CTransaction & //vout.0: opreturn - 'M' withdrawtxid refcoin completetxid if ((numvouts=tx.vout.size()) > 0 && DecodeGatewaysMarkDoneOpRet(tx.vout[numvouts-1].scriptPubKey,withdrawtxid,refcoin,completetxid)!='M') return eval->Invalid("invalid gatewaysmarkdone OP_RETURN data!"); - else if ( IsCCInput(tx.vin[0].scriptSig) == 0 ) - return eval->Invalid("vin.0 is CC for gatewaysmarkdone!"); + else if ( IsCCInput(tx.vin[0].scriptSig) != 0 ) + return eval->Invalid("vin.0 is normal for gatewaysmarkdone!"); + else if ( IsCCInput(tx.vin[1].scriptSig) == 0 ) + return eval->Invalid("vin.1 is CC for gatewaysmarkdone!"); + else if ( tx.vout[0].scriptPubKey.IsPayToCryptoCondition() != 0 ) + return eval->Invalid("vout.0 is normal for gatewaysmarkdone!"); else if (myGetTransaction(completetxid,tmptx,hashblock) == 0) - return eval->Invalid("invalid gatewaygatewayscompletesigning txid!"); + return eval->Invalid("invalid gatewayscompletesigning txid!"); else if ((numvouts=tmptx.vout.size()) > 0 && DecodeGatewaysCompleteSigningOpRet(tmptx.vout[numvouts-1].scriptPubKey,withdrawtxid,tmprefcoin,K,hex)!='S') return eval->Invalid("invalid gatewayscompletesigning OP_RETURN data!"); - else if ( IsCCInput(tmptx.vin[0].scriptSig) != 0 ) - return eval->Invalid("vin.0 is normal for gatewayscompletesigning!"); - else if ( IsCCInput(tmptx.vin[1].scriptSig) == 0 ) - return eval->Invalid("vin.1 is CC for gatewayscompletesigning!"); - else if ( tmptx.vout[0].scriptPubKey.IsPayToCryptoCondition() == 0 ) - return eval->Invalid("vout.0 is CC for gatewayscompletesigning!"); + else if (komodo_txnotarizedconfirmed(completetxid) == false) + return eval->Invalid("gatewayscompletesigning tx is not yet confirmed(notarised)!"); else if (myGetTransaction(withdrawtxid,tmptx,hashblock) == 0) return eval->Invalid("invalid withdraw txid!"); else if ((numvouts=tmptx.vout.size()) > 0 && DecodeGatewaysWithdrawOpRet(tmptx.vout[numvouts-1].scriptPubKey,tmptokenid,bindtxid,tmprefcoin,pubkey,amount)!='W') return eval->Invalid("invalid gatewayswithdraw OP_RETURN data!"); else if (tmprefcoin!=refcoin) - return eval->Invalid("refcoin different than in bind tx"); - else if ( IsCCInput(tmptx.vin[0].scriptSig) != 0 ) - return eval->Invalid("vin.0 is normal for gatewaysWithdraw!"); - else if ( IsCCInput(tmptx.vin[1].scriptSig) == 0 ) - return eval->Invalid("vin.1 is CC for gatewaysWithdraw!"); - else if ( tmptx.vout[0].scriptPubKey.IsPayToCryptoCondition() == 0 ) - return eval->Invalid("vout.0 is CC for gatewaysWithdraw!"); - else if ( tmptx.vout[1].scriptPubKey.IsPayToCryptoCondition() == 0 ) - return eval->Invalid("vout.1 is CC for gatewaysWithdraw!"); - else if ( tmptx.vout[1].nValue!=amount) - return eval->Invalid("amount in opret not matching tx tokens amount!"); + return eval->Invalid("refcoin different than in bind tx"); else if (komodo_txnotarizedconfirmed(withdrawtxid) == false) return eval->Invalid("gatewayswithdraw tx is not yet confirmed(notarised)!"); else if (myGetTransaction(bindtxid,tmptx,hashblock) == 0) @@ -872,15 +854,15 @@ bool GatewaysValidate(struct CCcontract_info *cp,Eval *eval,const CTransaction & return eval->Invalid("tokenid does not match tokenid from gatewaysbind"); else if (komodo_txnotarizedconfirmed(bindtxid) == false) return eval->Invalid("gatewaysbind tx is not yet confirmed(notarised)!"); - else if (K!=M) + else if (KInvalid("invalid number of signs!"); break; } } retval = PreventCC(eval,tx,preventCCvins,numvins,preventCCvouts,numvouts); if ( retval != 0 ) - fprintf(stderr,"Gatewaysget validated\n"); - else fprintf(stderr,"Gatewaysget invalid\n"); + fprintf(stderr,"Gateways tx validated\n"); + else fprintf(stderr,"Gateways tx invalid\n"); return(retval); // } } @@ -903,7 +885,7 @@ int64_t AddGatewaysInputs(struct CCcontract_info *cp,CMutableTransaction &mtx,CP GetTokensCCaddress(cp,coinaddr,pk); SetCCunspents(unspentOutputs,coinaddr); threshold = total/(maxinputs+1); - fprintf(stderr,"check %s for gateway inputs\n",coinaddr); + LogPrint("gatewayscc","check %s for gateway inputs\n",coinaddr); for (std::vector >::const_iterator it=unspentOutputs.begin(); it!=unspentOutputs.end(); it++) { txid = it->first.txhash; @@ -945,9 +927,9 @@ int64_t AddGatewaysInputs(struct CCcontract_info *cp,CMutableTransaction &mtx,CP } return(totalinputs); } - else fprintf(stderr,"invalid GatewaysBind\n"); + else LogPrint("gatewayscc","invalid GatewaysBind\n"); } - else fprintf(stderr,"can't find GatewaysBind txid\n"); + else LogPrint("gatewayscc","can't find GatewaysBind txid\n"); return(0); } @@ -968,19 +950,21 @@ std::string GatewaysBind(uint64_t txfee,std::string coin,uint256 tokenid,int64_t } else { - fprintf(stderr,"set taddr, prefix, prefix2 for %s\n",coin.c_str()); + LogPrint("gatewayscc","set taddr, prefix, prefix2 for %s\n",coin.c_str()); taddr = 0; prefix = 60; prefix2 = 85; } if ( N == 0 || N > 15 || M > N ) { - fprintf(stderr,"illegal M.%d or N.%d\n",M,N); + CCerror = strprintf("illegal M.%d or N.%d",M,N); + LogPrint("gatewayscc","%s\n", CCerror.c_str() ); return(""); } if ( pubkeys.size() != N ) { - fprintf(stderr,"M.%d N.%d but pubkeys[%d]\n",M,N,(int32_t)pubkeys.size()); + CCerror = strprintf("M.%d N.%d but pubkeys[%d]",M,N,(int32_t)pubkeys.size()); + LogPrint("gatewayscc","%s\n", CCerror.c_str() ); return(""); } for (i=0; iunspendableCCaddr,(double)totalsupply/COIN,(double)fullsupply/COIN); + CCerror = strprintf("Gateway bind.%s (%s) globaladdr.%s totalsupply %.8f != fullsupply %.8f",coin.c_str(),uint256_str(str,tokenid),cp->unspendableCCaddr,(double)totalsupply/COIN,(double)fullsupply/COIN); + LogPrint("gatewayscc","%s\n", CCerror.c_str() ); return(""); } if ( CCtoken_balance(myTokenCCaddr,tokenid) != totalsupply ) { - fprintf(stderr,"token balance on %s %.8f != %.8f\n",myTokenCCaddr,(double)CCtoken_balance(myTokenCCaddr,tokenid)/COIN,(double)totalsupply/COIN); + CCerror = strprintf("token balance on %s %.8f != %.8f",myTokenCCaddr,(double)CCtoken_balance(myTokenCCaddr,tokenid)/COIN,(double)totalsupply/COIN); + LogPrint("gatewayscc","%s\n", CCerror.c_str() ); return(""); } if ( GetTransaction(oracletxid,oracletx,hashBlock,false) == 0 || (numvouts= oracletx.vout.size()) <= 0 ) { - fprintf(stderr,"cant find oracletxid %s\n",uint256_str(str,oracletxid)); + CCerror = strprintf("cant find oracletxid %s",uint256_str(str,oracletxid)); + LogPrint("gatewayscc","%s\n", CCerror.c_str() ); return(""); } if ( DecodeOraclesCreateOpRet(oracletx.vout[numvouts-1].scriptPubKey,name,description,format) != 'C' ) { - fprintf(stderr,"mismatched oracle name %s != %s\n",name.c_str(),coin.c_str()); + CCerror = strprintf("mismatched oracle name %s != %s",name.c_str(),coin.c_str()); + LogPrint("gatewayscc","%s\n", CCerror.c_str() ); return(""); } if ( (fstr=(char *)format.c_str()) == 0 || strncmp(fstr,"Ihh",3) != 0 ) { - fprintf(stderr,"illegal format (%s) != (%s)\n",fstr,(char *)"Ihh"); + CCerror = strprintf("illegal format (%s) != (%s)",fstr,(char *)"Ihh"); + LogPrint("gatewayscc","%s\n", CCerror.c_str() ); return(""); } if ( GatewaysBindExists(cp,gatewayspk,tokenid) != 0 ) { - fprintf(stderr,"Gateway bind.%s (%s) already exists\n",coin.c_str(),uint256_str(str,tokenid)); + CCerror = strprintf("Gateway bind.%s (%s) already exists",coin.c_str(),uint256_str(str,tokenid)); + LogPrint("gatewayscc","%s\n", CCerror.c_str() ); return(""); } if ( AddNormalinputs(mtx,mypk,2*txfee,3) > 0 ) @@ -1042,7 +1034,7 @@ std::string GatewaysBind(uint64_t txfee,std::string coin,uint256 tokenid,int64_t } } CCerror = strprintf("cant find enough inputs"); - fprintf(stderr,"%s\n", CCerror.c_str() ); + LogPrint("gatewayscc","%s\n", CCerror.c_str() ); return(""); } @@ -1057,22 +1049,30 @@ std::string GatewaysDeposit(uint64_t txfee,uint256 bindtxid,int32_t height,std:: if ( txfee == 0 ) txfee = 10000; mypk = pubkey2pk(Mypubkey()); - //fprintf(stderr,"GatewaysDeposit ht.%d %s %.8f numpks.%d\n",height,refcoin.c_str(),(double)amount/COIN,(int32_t)pubkeys.size()); + //LogPrint("gatewayscc","GatewaysDeposit ht.%d %s %.8f numpks.%d\n",height,refcoin.c_str(),(double)amount/COIN,(int32_t)pubkeys.size()); if ( GetTransaction(bindtxid,bindtx,hashBlock,false) == 0 || (numvouts= bindtx.vout.size()) <= 0 ) { - fprintf(stderr,"cant find bindtxid %s\n",uint256_str(str,bindtxid)); + CCerror = strprintf("cant find bindtxid %s",uint256_str(str,bindtxid)); + LogPrint("gatewayscc","%s\n", CCerror.c_str() ); return(""); } if ( DecodeGatewaysBindOpRet(depositaddr,bindtx.vout[numvouts-1].scriptPubKey,tokenid,coin,totalsupply,oracletxid,M,N,pubkeys,taddr,prefix,prefix2) != 'B' || refcoin != coin ) { - fprintf(stderr,"invalid coin - bindtxid %s coin.%s\n",uint256_str(str,bindtxid),coin.c_str()); + CCerror = strprintf("invalid coin - bindtxid %s coin.%n",uint256_str(str,bindtxid),coin.c_str()); + LogPrint("gatewayscc","%s\n", CCerror.c_str() ); + return(""); + } + if (komodo_txnotarizedconfirmed(bindtxid)==false) + { + CCerror = strprintf("gatewaysbind tx not yet confirmed/notarized"); + LogPrint("gatewayscc","%s\n", CCerror.c_str() ); return(""); } n = (int32_t)pubkeys.size(); merkleroot = zeroid; for (i=m=0; i 0 ) @@ -1108,7 +1111,8 @@ std::string GatewaysDeposit(uint64_t txfee,uint256 bindtxid,int32_t height,std:: mtx.vout.push_back(CTxOut(txfee,CScript() << ParseHex(HexStr(CCtxidaddr(txidaddr,cointxid))) << OP_CHECKSIG)); return(FinalizeCCTx(0,cp,mtx,mypk,txfee,EncodeGatewaysDepositOpRet('D',bindtxid,coin,publishers,txids,height,cointxid,claimvout,deposithex,proof,destpub,amount))); } - fprintf(stderr,"cant find enough inputs\n"); + CCerror = strprintf("cant find enough inputs"); + LogPrint("gatewayscc","%s\n", CCerror.c_str() ); return(""); } @@ -1127,32 +1131,50 @@ std::string GatewaysClaim(uint64_t txfee,uint256 bindtxid,std::string refcoin,ui gatewayspk = GetUnspendable(cp,0); if ( GetTransaction(bindtxid,tx,hashBlock,false) == 0 || (numvouts= tx.vout.size()) <= 0 ) { - fprintf(stderr,"cant find bindtxid %s\n",uint256_str(str,bindtxid)); + CCerror = strprintf("cant find bindtxid %s",uint256_str(str,bindtxid)); + LogPrint("gatewayscc","%s\n", CCerror.c_str() ); return(""); } if ( DecodeGatewaysBindOpRet(depositaddr,tx.vout[numvouts-1].scriptPubKey,tokenid,coin,totalsupply,oracletxid,M,N,msigpubkeys,taddr,prefix,prefix2) != 'B' || coin != refcoin ) { - fprintf(stderr,"invalid coin - bindtxid %s coin.%s\n",uint256_str(str,bindtxid),coin.c_str()); + CCerror = strprintf("invalid coin - bindtxid %s coin.%s",uint256_str(str,bindtxid),coin.c_str()); + LogPrint("gatewayscc","%s\n", CCerror.c_str() ); + return(""); + } + if (komodo_txnotarizedconfirmed(bindtxid)==false) + { + CCerror = strprintf("gatewaysbind tx not yet confirmed/notarized"); + LogPrint("gatewayscc","%s\n", CCerror.c_str() ); return(""); } if ( GetTransaction(deposittxid,tx,hashBlock,false) == 0 || (numvouts= tx.vout.size()) <= 0 ) { - fprintf(stderr,"cant find deposittxid %s\n",uint256_str(str,bindtxid)); + CCerror = strprintf("cant find deposittxid %s",uint256_str(str,bindtxid)); + LogPrint("gatewayscc","%s\n", CCerror.c_str() ); return(""); } if (DecodeGatewaysDepositOpRet(tx.vout[numvouts-1].scriptPubKey,tmptxid,coin,publishers,txids,height,cointxid,claimvout,deposithex,proof,tmpdestpub,tmpamount) != 'D' || coin != refcoin) { - fprintf(stderr,"invalid coin - deposittxid %s coin.%s\n",uint256_str(str,bindtxid),coin.c_str()); + CCerror = strprintf("invalid coin - deposittxid %s coin.%s",uint256_str(str,bindtxid),coin.c_str()); + LogPrint("gatewayscc","%s\n", CCerror.c_str() ); + return(""); + } + if (komodo_txnotarizedconfirmed(deposittxid)==false) + { + CCerror = strprintf("gatewaysdeposit tx not yet confirmed/notarized"); + LogPrint("gatewayscc","%s\n", CCerror.c_str() ); return(""); } if (tmpdestpub!=destpub) { - fprintf(stderr,"different destination pubkey from desdeposittxid\n"); + CCerror = strprintf("different destination pubkey from desdeposit tx"); + LogPrint("gatewayscc","%s\n", CCerror.c_str() ); return(""); } if ( (depositamount=GatewaysDepositval(tx,mypk)) != amount ) { - fprintf(stderr,"invalid Gateways deposittxid %s %.8f != %.8f\n",uint256_str(str,deposittxid),(double)depositamount/COIN,(double)amount/COIN); + CCerror = strprintf("invalid Gateways deposittxid %s %.8f != %.8f",uint256_str(str,deposittxid),(double)depositamount/COIN,(double)amount/COIN); + LogPrint("gatewayscc","%s\n", CCerror.c_str() ); return(""); } if ( AddNormalinputs(mtx,mypk,txfee,3) > 0 ) @@ -1167,7 +1189,7 @@ std::string GatewaysClaim(uint64_t txfee,uint256 bindtxid,std::string refcoin,ui } } CCerror = strprintf("cant find enough inputs or mismatched total"); - fprintf(stderr,"%s\n", CCerror.c_str() ); + LogPrint("gatewayscc","%s\n", CCerror.c_str() ); return(""); } @@ -1175,7 +1197,7 @@ std::string GatewaysWithdraw(uint64_t txfee,uint256 bindtxid,std::string refcoin { CMutableTransaction mtx = CreateNewContextualCMutableTransaction(Params().GetConsensus(), komodo_nextheight()); CTransaction tx; CPubKey mypk,gatewayspk,signerpk; uint256 txid,tokenid,hashBlock,oracletxid,tmptokenid,tmpbindtxid,withdrawtxid; int32_t vout,numvouts; - int64_t nValue,totalsupply,inputs,CCchange=0; uint8_t funcid,K,M,N,taddr,prefix,prefix2; std::string coin,hex; + int64_t nValue,totalsupply,inputs,CCchange=0,tmpamount; uint8_t funcid,K,M,N,taddr,prefix,prefix2; std::string coin,hex; std::vector msigpubkeys; char depositaddr[64],str[65],coinaddr[64]; struct CCcontract_info *cp,C,*cpTokens,CTokens; std::vector > unspentOutputs; @@ -1188,12 +1210,20 @@ std::string GatewaysWithdraw(uint64_t txfee,uint256 bindtxid,std::string refcoin if( GetTransaction(bindtxid,tx,hashBlock,false) == 0 || (numvouts= tx.vout.size()) <= 0 ) { - fprintf(stderr,"cant find bindtxid %s\n",uint256_str(str,bindtxid)); + CCerror = strprintf("cant find bindtxid %s",uint256_str(str,bindtxid)); + LogPrint("gatewayscc","%s\n", CCerror.c_str() ); return(""); } if( DecodeGatewaysBindOpRet(depositaddr,tx.vout[numvouts-1].scriptPubKey,tokenid,coin,totalsupply,oracletxid,M,N,msigpubkeys,taddr,prefix,prefix2) != 'B' || coin != refcoin ) { - fprintf(stderr,"invalid bindtxid %s coin.%s\n",uint256_str(str,bindtxid),coin.c_str()); + CCerror = strprintf("invalid bindtxid %s coin.%s",uint256_str(str,bindtxid),coin.c_str()); + LogPrint("gatewayscc","%s\n", CCerror.c_str() ); + return(""); + } + if (komodo_txnotarizedconfirmed(bindtxid)==false) + { + CCerror = strprintf("gatewaysbind tx not yet confirmed/notarized"); + LogPrint("gatewayscc","%s\n", CCerror.c_str() ); return(""); } _GetCCaddress(coinaddr,EVAL_GATEWAYS,gatewayspk); @@ -1207,20 +1237,20 @@ std::string GatewaysWithdraw(uint64_t txfee,uint256 bindtxid,std::string refcoin if ( vout == 0 && nValue == 10000 && GetTransaction(txid,tx,hashBlock,false) != 0 && (numvouts= tx.vout.size())>0 && (funcid=DecodeGatewaysOpRet(tx.vout[numvouts-1].scriptPubKey))!=0 && (funcid=='W' || funcid=='P')) { - if (funcid=='W' && DecodeGatewaysWithdrawOpRet(tx.vout[numvouts-1].scriptPubKey,tmptokenid,tmpbindtxid,coin,withdrawpub,amount)=='W' + if (funcid=='W' && DecodeGatewaysWithdrawOpRet(tx.vout[numvouts-1].scriptPubKey,tmptokenid,tmpbindtxid,coin,withdrawpub,tmpamount)=='W' && refcoin==coin && tmptokenid==tokenid && tmpbindtxid==bindtxid) { - CCerror = strprintf("unable to create withdraw, another withdraw pending\n"); - fprintf(stderr,"%s\n", CCerror.c_str() ); + CCerror = strprintf("unable to create withdraw, another withdraw pending"); + LogPrint("gatewayscc","%s\n", CCerror.c_str() ); return(""); } else if (funcid=='P' && DecodeGatewaysPartialOpRet(tx.vout[numvouts-1].scriptPubKey,withdrawtxid,coin,K,signerpk,hex)=='P' && - GetTransaction(withdrawtxid,tx,hashBlock,false)!=0 && (numvouts=tx.vout.size())>0 && DecodeGatewaysWithdrawOpRet(tx.vout[numvouts-1].scriptPubKey,tmptokenid,tmpbindtxid,coin,withdrawpub,amount)=='W' + GetTransaction(withdrawtxid,tx,hashBlock,false)!=0 && (numvouts=tx.vout.size())>0 && DecodeGatewaysWithdrawOpRet(tx.vout[numvouts-1].scriptPubKey,tmptokenid,tmpbindtxid,coin,withdrawpub,tmpamount)=='W' && refcoin==coin && tmptokenid==tokenid && tmpbindtxid==bindtxid) { - CCerror = strprintf("unable to create withdraw, another withdraw pending\n"); - fprintf(stderr,"%s\n", CCerror.c_str() ); + CCerror = strprintf("unable to create withdraw, another withdraw pending"); + LogPrint("gatewayscc","%s\n", CCerror.c_str() ); return(""); } } @@ -1238,12 +1268,12 @@ std::string GatewaysWithdraw(uint64_t txfee,uint256 bindtxid,std::string refcoin else { CCerror = strprintf("not enough balance of tokens for withdraw"); - fprintf(stderr,"%s\n", CCerror.c_str() ); + LogPrint("gatewayscc","%s\n", CCerror.c_str() ); return(""); } } CCerror = strprintf("cant find enough normal inputs"); - fprintf(stderr,"%s\n", CCerror.c_str() ); + LogPrint("gatewayscc","%s\n", CCerror.c_str() ); return(""); } @@ -1263,8 +1293,8 @@ std::string GatewaysPartialSign(uint64_t txfee,uint256 lasttxid,std::string refc if (GetTransaction(lasttxid,tx,hashBlock,false)==0 || (numvouts= tx.vout.size())<=0 || (funcid=DecodeGatewaysOpRet(tx.vout[numvouts-1].scriptPubKey))==0 || (funcid!='W' && funcid!='P')) { - CCerror = strprintf("can't find last tx %s\n",uint256_str(str,lasttxid)); - fprintf(stderr,"%s\n", CCerror.c_str() ); + CCerror = strprintf("can't find last tx %s",uint256_str(str,lasttxid)); + LogPrint("gatewayscc","%s\n", CCerror.c_str() ); return(""); } if (funcid=='W') @@ -1272,21 +1302,27 @@ std::string GatewaysPartialSign(uint64_t txfee,uint256 lasttxid,std::string refc withdrawtxid=lasttxid; if (DecodeGatewaysWithdrawOpRet(tx.vout[numvouts-1].scriptPubKey,tmptokenid,bindtxid,coin,withdrawpub,amount)!='W' || refcoin!=coin) { - CCerror = strprintf("invalid withdraw tx %s\n",uint256_str(str,lasttxid)); - fprintf(stderr,"%s\n", CCerror.c_str() ); + CCerror = strprintf("invalid withdraw tx %s",uint256_str(str,lasttxid)); + LogPrint("gatewayscc","%s\n", CCerror.c_str() ); + return(""); + } + else if (komodo_txnotarizedconfirmed(withdrawtxid)==false) + { + CCerror = strprintf("gatewayswithdraw tx not yet confirmed/notarized"); + LogPrint("gatewayscc","%s\n", CCerror.c_str() ); return(""); } else if (GetTransaction(bindtxid,tmptx,hashBlock,false)==0 || (numvouts=tmptx.vout.size())<=0) { - CCerror = strprintf("can't find bind tx %s\n",uint256_str(str,bindtxid)); - fprintf(stderr,"%s\n", CCerror.c_str() ); + CCerror = strprintf("can't find bind tx %s",uint256_str(str,bindtxid)); + LogPrint("gatewayscc","%s\n", CCerror.c_str() ); return(""); } else if (DecodeGatewaysBindOpRet(depositaddr,tmptx.vout[numvouts-1].scriptPubKey,tokenid,coin,totalsupply,oracletxid,M,N,pubkeys,taddr,prefix,prefix2) != 'B' || refcoin!=coin || tokenid!=tmptokenid) { - CCerror = strprintf("invalid bind tx %s\n",uint256_str(str,bindtxid)); - fprintf(stderr,"%s\n", CCerror.c_str() ); + CCerror = strprintf("invalid bind tx %s",uint256_str(str,bindtxid)); + LogPrint("gatewayscc","%s\n", CCerror.c_str() ); return(""); } } @@ -1294,41 +1330,52 @@ std::string GatewaysPartialSign(uint64_t txfee,uint256 lasttxid,std::string refc { if (DecodeGatewaysPartialOpRet(tx.vout[numvouts-1].scriptPubKey,withdrawtxid,coin,K,signerpk,tmphex)!='P' || refcoin!=coin) { - CCerror = strprintf("cannot decode partialsign tx opret %s\n",uint256_str(str,lasttxid)); - fprintf(stderr,"%s\n", CCerror.c_str() ); + CCerror = strprintf("cannot decode partialsign tx opret %s",uint256_str(str,lasttxid)); + LogPrint("gatewayscc","%s\n", CCerror.c_str() ); return(""); } else if (GetTransaction(withdrawtxid,tmptx,hashBlock,false)==0 || (numvouts= tmptx.vout.size())<=0) { - CCerror = strprintf("can't find withdraw tx %s\n",uint256_str(str,withdrawtxid)); - fprintf(stderr,"%s\n", CCerror.c_str() ); + CCerror = strprintf("can't find withdraw tx %s",uint256_str(str,withdrawtxid)); + LogPrint("gatewayscc","%s\n", CCerror.c_str() ); return(""); } else if (DecodeGatewaysWithdrawOpRet(tmptx.vout[numvouts-1].scriptPubKey,tmptokenid,bindtxid,coin,withdrawpub,amount)!='W' || refcoin!=coin) { - CCerror = strprintf("invalid withdraw tx %s\n",uint256_str(str,lasttxid)); - fprintf(stderr,"%s\n", CCerror.c_str() ); + CCerror = strprintf("invalid withdraw tx %s",uint256_str(str,lasttxid)); + LogPrint("gatewayscc","%s\n", CCerror.c_str() ); + return(""); + } + else if (komodo_txnotarizedconfirmed(withdrawtxid)==false) + { + CCerror = strprintf("gatewayswithdraw tx not yet confirmed/notarized"); + LogPrint("gatewayscc","%s\n", CCerror.c_str() ); return(""); } else if (GetTransaction(bindtxid,tmptx,hashBlock,false)==0 || (numvouts=tmptx.vout.size())<=0) { - CCerror = strprintf("can't find bind tx %s\n",uint256_str(str,bindtxid)); - fprintf(stderr,"%s\n", CCerror.c_str() ); + CCerror = strprintf("can't find bind tx %s",uint256_str(str,bindtxid)); + LogPrint("gatewayscc","%s\n", CCerror.c_str() ); return(""); } else if (DecodeGatewaysBindOpRet(depositaddr,tmptx.vout[numvouts-1].scriptPubKey,tokenid,coin,totalsupply,oracletxid,M,N,pubkeys,taddr,prefix,prefix2) != 'B' || refcoin!=coin || tokenid!=tmptokenid) { - CCerror = strprintf("invalid bind tx %s\n",uint256_str(str,bindtxid)); - fprintf(stderr,"%s\n", CCerror.c_str() ); + CCerror = strprintf("invalid bind tx %s",uint256_str(str,bindtxid)); + LogPrint("gatewayscc","%s\n", CCerror.c_str() ); return(""); } } - if (AddNormalinputs(mtx,mypk,txfee,3)==0) fprintf(stderr,"error adding funds for partialsign\n"); - mtx.vin.push_back(CTxIn(tx.GetHash(),0,CScript())); - mtx.vout.push_back(MakeCC1vout(EVAL_GATEWAYS,txfee,gatewayspk)); - return(FinalizeCCTx(0,cp,mtx,mypk,txfee,EncodeGatewaysPartialOpRet('P',withdrawtxid,refcoin,K+1,mypk,hex))); + if (AddNormalinputs(mtx,mypk,txfee,3)!=0) + { + mtx.vin.push_back(CTxIn(tx.GetHash(),0,CScript())); + mtx.vout.push_back(MakeCC1vout(EVAL_GATEWAYS,txfee,gatewayspk)); + return(FinalizeCCTx(0,cp,mtx,mypk,txfee,EncodeGatewaysPartialOpRet('P',withdrawtxid,refcoin,K+1,mypk,hex))); + } + CCerror = strprintf("error adding funds for partialsign"); + LogPrint("gatewayscc","%s\n", CCerror.c_str() ); + return(""); } std::string GatewaysCompleteSigning(uint64_t txfee,uint256 lasttxid,std::string refcoin,std::string hex) @@ -1346,8 +1393,8 @@ std::string GatewaysCompleteSigning(uint64_t txfee,uint256 lasttxid,std::string if (GetTransaction(lasttxid,tx,hashBlock,false)==0 || (numvouts= tx.vout.size())<=0 || (funcid=DecodeGatewaysOpRet(tx.vout[numvouts-1].scriptPubKey))==0 || (funcid!='W' && funcid!='P')) { - CCerror = strprintf("invalid last txid %s\n",uint256_str(str,lasttxid)); - fprintf(stderr,"%s\n", CCerror.c_str() ); + CCerror = strprintf("invalid last txid %s",uint256_str(str,lasttxid)); + LogPrint("gatewayscc","%s\n", CCerror.c_str() ); return(""); } if (funcid=='W') @@ -1355,21 +1402,27 @@ std::string GatewaysCompleteSigning(uint64_t txfee,uint256 lasttxid,std::string withdrawtxid=lasttxid; if (DecodeGatewaysWithdrawOpRet(tx.vout[numvouts-1].scriptPubKey,tmptokenid,bindtxid,coin,withdrawpub,amount)!='W' || refcoin!=coin) { - CCerror = strprintf("cannot decode withdraw tx opret %s\n",uint256_str(str,lasttxid)); - fprintf(stderr,"%s\n", CCerror.c_str() ); + CCerror = strprintf("cannot decode withdraw tx opret %s",uint256_str(str,lasttxid)); + LogPrint("gatewayscc","%s\n", CCerror.c_str() ); return(""); } else if (GetTransaction(bindtxid,tmptx,hashBlock,false)==0 || (numvouts=tmptx.vout.size())<=0) { - CCerror = strprintf("can't find bind tx %s\n",uint256_str(str,bindtxid)); - fprintf(stderr,"%s\n", CCerror.c_str() ); + CCerror = strprintf("can't find bind tx %s",uint256_str(str,bindtxid)); + LogPrint("gatewayscc","%s\n", CCerror.c_str() ); + return(""); + } + else if (komodo_txnotarizedconfirmed(withdrawtxid)==false) + { + CCerror = strprintf("gatewayswithdraw tx not yet confirmed/notarized"); + LogPrint("gatewayscc","%s\n", CCerror.c_str() ); return(""); } else if (DecodeGatewaysBindOpRet(depositaddr,tmptx.vout[numvouts-1].scriptPubKey,tokenid,coin,totalsupply,oracletxid,M,N,pubkeys,taddr,prefix,prefix2) != 'B' || refcoin!=coin || tokenid!=tmptokenid) { - CCerror = strprintf("invalid bind tx %s\n",uint256_str(str,bindtxid)); - fprintf(stderr,"%s\n", CCerror.c_str() ); + CCerror = strprintf("invalid bind tx %s",uint256_str(str,bindtxid)); + LogPrint("gatewayscc","%s\n", CCerror.c_str() ); return(""); } } @@ -1377,41 +1430,51 @@ std::string GatewaysCompleteSigning(uint64_t txfee,uint256 lasttxid,std::string { if (DecodeGatewaysPartialOpRet(tx.vout[numvouts-1].scriptPubKey,withdrawtxid,coin,K,signerpk,tmphex)!='P' || refcoin!=coin) { - CCerror = strprintf("cannot decode partialsign tx opret %s\n",uint256_str(str,lasttxid)); - fprintf(stderr,"%s\n", CCerror.c_str() ); + CCerror = strprintf("cannot decode partialsign tx opret %s",uint256_str(str,lasttxid)); + LogPrint("gatewayscc","%s\n", CCerror.c_str() ); return(""); } else if (GetTransaction(withdrawtxid,tmptx,hashBlock,false)==0 || (numvouts=tmptx.vout.size())==0) { - CCerror = strprintf("invalid withdraw txid %s\n",uint256_str(str,withdrawtxid)); - fprintf(stderr,"%s\n", CCerror.c_str() ); + CCerror = strprintf("invalid withdraw txid %s",uint256_str(str,withdrawtxid)); + LogPrint("gatewayscc","%s\n", CCerror.c_str() ); return(""); } else if (DecodeGatewaysWithdrawOpRet(tmptx.vout[numvouts-1].scriptPubKey,tmptokenid,bindtxid,coin,withdrawpub,amount)!='W' || refcoin!=coin) { - printf("aaaaaaaaaaa\n"); - CCerror = strprintf("cannot decode withdraw tx opret %s\n",uint256_str(str,withdrawtxid)); - fprintf(stderr,"%s\n", CCerror.c_str() ); + CCerror = strprintf("cannot decode withdraw tx opret %s",uint256_str(str,withdrawtxid)); + LogPrint("gatewayscc","%s\n", CCerror.c_str() ); + return(""); + } + else if (komodo_txnotarizedconfirmed(withdrawtxid)==false) + { + CCerror = strprintf("gatewayswithdraw tx not yet confirmed/notarized"); + LogPrint("gatewayscc","%s\n", CCerror.c_str() ); return(""); } else if (GetTransaction(bindtxid,tmptx,hashBlock,false)==0 || (numvouts=tmptx.vout.size())<=0) { - CCerror = strprintf("can't find bind tx %s\n",uint256_str(str,bindtxid)); - fprintf(stderr,"%s\n", CCerror.c_str() ); + CCerror = strprintf("can't find bind tx %s",uint256_str(str,bindtxid)); + LogPrint("gatewayscc","%s\n", CCerror.c_str() ); return(""); } else if (DecodeGatewaysBindOpRet(depositaddr,tmptx.vout[numvouts-1].scriptPubKey,tokenid,coin,totalsupply,oracletxid,M,N,pubkeys,taddr,prefix,prefix2) != 'B' || refcoin!=coin || tokenid!=tmptokenid) { - CCerror = strprintf("invalid bind tx %s\n",uint256_str(str,bindtxid)); - fprintf(stderr,"%s\n", CCerror.c_str() ); + CCerror = strprintf("invalid bind tx %s",uint256_str(str,bindtxid)); + LogPrint("gatewayscc","%s\n", CCerror.c_str() ); return(""); } } - if (AddNormalinputs(mtx,mypk,txfee,3)==0) fprintf(stderr,"error adding funds for completesigning\n"); - mtx.vin.push_back(CTxIn(lasttxid,0,CScript())); - mtx.vout.push_back(MakeCC1vout(EVAL_GATEWAYS,txfee,gatewayspk)); - return(FinalizeCCTx(0,cp,mtx,mypk,txfee,EncodeGatewaysCompleteSigningOpRet('S',withdrawtxid,refcoin,K+1,hex))); + if (AddNormalinputs(mtx,mypk,txfee,3)!=0) + { + mtx.vin.push_back(CTxIn(lasttxid,0,CScript())); + mtx.vout.push_back(MakeCC1vout(EVAL_GATEWAYS,txfee,gatewayspk)); + return(FinalizeCCTx(0,cp,mtx,mypk,txfee,EncodeGatewaysCompleteSigningOpRet('S',withdrawtxid,refcoin,K+1,hex))); + } + CCerror = strprintf("error adding funds for completesigning"); + LogPrint("gatewayscc","%s\n", CCerror.c_str() ); + return(""); } std::string GatewaysMarkDone(uint64_t txfee,uint256 completetxid,std::string refcoin) @@ -1427,43 +1490,56 @@ std::string GatewaysMarkDone(uint64_t txfee,uint256 completetxid,std::string ref txfee = 10000; if (GetTransaction(completetxid,tx,hashBlock,false)==0 || (numvouts= tx.vout.size())<=0) { - CCerror = strprintf("invalid completesigning txid %s\n",uint256_str(str,completetxid)); - fprintf(stderr,"%s\n", CCerror.c_str() ); + CCerror = strprintf("invalid completesigning txid %s",uint256_str(str,completetxid)); + LogPrint("gatewayscc","%s\n", CCerror.c_str() ); return(""); } else if (DecodeGatewaysCompleteSigningOpRet(tx.vout[numvouts-1].scriptPubKey,withdrawtxid,coin,K,hex)!='S' || refcoin!=coin) { - CCerror = strprintf("cannot decode completesigning tx opret %s\n",uint256_str(str,completetxid)); - fprintf(stderr,"%s\n", CCerror.c_str() ); + CCerror = strprintf("cannot decode completesigning tx opret %s",uint256_str(str,completetxid)); + LogPrint("gatewayscc","%s\n", CCerror.c_str() ); + return(""); + } + if (komodo_txnotarizedconfirmed(completetxid)==false) + { + CCerror = strprintf("gatewayscompletesigning tx not yet confirmed/notarized"); + LogPrint("gatewayscc","%s\n", CCerror.c_str() ); return(""); } else if (GetTransaction(withdrawtxid,tx,hashBlock,false)==0 || (numvouts= tx.vout.size())==0) { - CCerror = strprintf("invalid withdraw txid %s\n",uint256_str(str,withdrawtxid)); - fprintf(stderr,"%s\n", CCerror.c_str() ); + CCerror = strprintf("invalid withdraw txid %s",uint256_str(str,withdrawtxid)); + LogPrint("gatewayscc","%s\n", CCerror.c_str() ); return(""); } else if (DecodeGatewaysWithdrawOpRet(tx.vout[numvouts-1].scriptPubKey,tmptokenid,bindtxid,coin,withdrawpub,amount)!='W' || refcoin!=coin) { CCerror = strprintf("cannot decode withdraw tx opret %s\n",uint256_str(str,withdrawtxid)); - fprintf(stderr,"%s\n", CCerror.c_str() ); + LogPrint("gatewayscc","%s\n", CCerror.c_str() ); return(""); } else if (GetTransaction(bindtxid,tx,hashBlock,false)==0 || (numvouts=tx.vout.size())<=0) { - CCerror = strprintf("can't find bind tx %s\n",uint256_str(str,bindtxid)); - fprintf(stderr,"%s\n", CCerror.c_str() ); + CCerror = strprintf("can't find bind tx %s",uint256_str(str,bindtxid)); + LogPrint("gatewayscc","%s\n", CCerror.c_str() ); return(""); } else if (DecodeGatewaysBindOpRet(depositaddr,tx.vout[numvouts-1].scriptPubKey,tokenid,coin,totalsupply,oracletxid,M,N,pubkeys,taddr,prefix,prefix2) != 'B' || refcoin!=coin || tokenid!=tmptokenid) { - CCerror = strprintf("invalid bind tx %s\n",uint256_str(str,bindtxid)); - fprintf(stderr,"%s\n", CCerror.c_str() ); + CCerror = strprintf("invalid bind tx %s",uint256_str(str,bindtxid)); + LogPrint("gatewayscc","%s\n", CCerror.c_str() ); return(""); } - mtx.vin.push_back(CTxIn(completetxid,0,CScript())); - return(FinalizeCCTx(0,cp,mtx,mypk,txfee,EncodeGatewaysMarkDoneOpRet('M',withdrawtxid,refcoin,completetxid))); + if (AddNormalinputs(mtx,mypk,txfee,3)!=0) + { + mtx.vin.push_back(CTxIn(completetxid,0,CScript())); + mtx.vout.push_back(CTxOut(txfee,CScript() << ParseHex(HexStr(mypk)) << OP_CHECKSIG)); + return(FinalizeCCTx(0,cp,mtx,mypk,txfee,EncodeGatewaysMarkDoneOpRet('M',withdrawtxid,refcoin,completetxid))); + } + CCerror = strprintf("error adding funds for markdone"); + LogPrint("gatewayscc","%s\n", CCerror.c_str() ); + return(""); } UniValue GatewaysPendingDeposits(uint256 bindtxid,std::string refcoin) @@ -1481,14 +1557,14 @@ UniValue GatewaysPendingDeposits(uint256 bindtxid,std::string refcoin) _GetCCaddress(coinaddr,EVAL_GATEWAYS,mypk); if ( GetTransaction(bindtxid,tx,hashBlock,false) == 0 || (numvouts= tx.vout.size()) <= 0 ) { - CCerror = strprintf("cant find bindtxid %s\n",uint256_str(str,bindtxid)); - fprintf(stderr,"%s\n", CCerror.c_str() ); + CCerror = strprintf("cant find bindtxid %s",uint256_str(str,bindtxid)); + LogPrint("gatewayscc","%s\n", CCerror.c_str() ); return(""); } if ( DecodeGatewaysBindOpRet(depositaddr,tx.vout[numvouts-1].scriptPubKey,tokenid,coin,totalsupply,oracletxid,M,N,pubkeys,taddr,prefix,prefix2) != 'B' || refcoin != coin) { - CCerror = strprintf("invalid bindtxid %s coin.%s\n",uint256_str(str,bindtxid),coin.c_str()); - fprintf(stderr,"%s\n", CCerror.c_str() ); + CCerror = strprintf("invalid bindtxid %s coin.%s",uint256_str(str,bindtxid),coin.c_str()); + LogPrint("gatewayscc","%s\n", CCerror.c_str() ); return(""); } SetCCunspents(unspentOutputs,coinaddr); @@ -1535,14 +1611,14 @@ UniValue GatewaysPendingWithdraws(uint256 bindtxid,std::string refcoin) GetTokensCCaddress(cp,tokensaddr,gatewayspk); if ( GetTransaction(bindtxid,tx,hashBlock,false) == 0 || (numvouts= tx.vout.size()) <= 0 ) { - CCerror = strprintf("cant find bindtxid %s\n",uint256_str(str,bindtxid)); - fprintf(stderr,"%s\n", CCerror.c_str() ); + CCerror = strprintf("cant find bindtxid %s",uint256_str(str,bindtxid)); + LogPrint("gatewayscc","%s\n", CCerror.c_str() ); return(""); } if ( DecodeGatewaysBindOpRet(depositaddr,tx.vout[numvouts-1].scriptPubKey,tokenid,coin,totalsupply,oracletxid,M,N,msigpubkeys,taddr,prefix,prefix2) != 'B' || refcoin != coin ) { - CCerror = strprintf("invalid bindtxid %s coin.%s\n",uint256_str(str,bindtxid),coin.c_str()); - fprintf(stderr,"%s\n", CCerror.c_str() ); + CCerror = strprintf("invalid bindtxid %s coin.%s",uint256_str(str,bindtxid),coin.c_str()); + LogPrint("gatewayscc","%s\n", CCerror.c_str() ); return(""); } n = msigpubkeys.size(); @@ -1623,14 +1699,14 @@ UniValue GatewaysProcessedWithdraws(uint256 bindtxid,std::string refcoin) _GetCCaddress(coinaddr,EVAL_GATEWAYS,gatewayspk); if ( GetTransaction(bindtxid,tx,hashBlock,false) == 0 || (numvouts= tx.vout.size()) <= 0 ) { - CCerror = strprintf("cant find bindtxid %s\n",uint256_str(str,bindtxid)); - fprintf(stderr,"%s\n", CCerror.c_str() ); + CCerror = strprintf("cant find bindtxid %s",uint256_str(str,bindtxid)); + LogPrint("gatewayscc","%s\n", CCerror.c_str() ); return(""); } if ( DecodeGatewaysBindOpRet(depositaddr,tx.vout[numvouts-1].scriptPubKey,tokenid,coin,totalsupply,oracletxid,M,N,msigpubkeys,taddr,prefix,prefix2) != 'B' || refcoin != coin) { - CCerror = strprintf("invalid bindtxid %s coin.%s\n",uint256_str(str,bindtxid),coin.c_str()); - fprintf(stderr,"%s\n", CCerror.c_str() ); + CCerror = strprintf("invalid bindtxid %s coin.%s",uint256_str(str,bindtxid),coin.c_str()); + LogPrint("gatewayscc","%s\n", CCerror.c_str() ); return(""); } n = msigpubkeys.size(); @@ -1660,6 +1736,7 @@ UniValue GatewaysProcessedWithdraws(uint256 bindtxid,std::string refcoin) obj.push_back(Pair("withdrawtxidaddr",txidaddr)); Getscriptaddress(withaddr,CScript() << ParseHex(HexStr(withdrawpub)) << OP_CHECKSIG); obj.push_back(Pair("withdrawaddr",withaddr)); + obj.push_back(Pair("confirmed_or_notarized",komodo_txnotarizedconfirmed(txid))); sprintf(numstr,"%.8f",(double)tx.vout[1].nValue/COIN); obj.push_back(Pair("amount",numstr)); obj.push_back(Pair("hex",hex)); @@ -1697,15 +1774,27 @@ UniValue GatewaysInfo(uint256 bindtxid) CMutableTransaction mtx = CreateNewContextualCMutableTransaction(Params().GetConsensus(), komodo_nextheight()); UniValue result(UniValue::VOBJ),a(UniValue::VARR); std::string coin; char str[67],numstr[65],depositaddr[64],gatewaystokens[64]; uint8_t M,N; std::vector pubkeys; uint8_t taddr,prefix,prefix2; uint256 tokenid,oracletxid,hashBlock; CTransaction tx; - CPubKey Gatewayspk; struct CCcontract_info *cp,C; int32_t i; int64_t totalsupply,remaining; + CPubKey Gatewayspk; struct CCcontract_info *cp,C; int32_t i; int64_t numvouts,totalsupply,remaining; std::vector msigpubkeys; - result.push_back(Pair("result","success")); - result.push_back(Pair("name","Gateways")); cp = CCinit(&C,EVAL_GATEWAYS); Gatewayspk = GetUnspendable(cp,0); GetTokensCCaddress(cp,gatewaystokens,Gatewayspk); + if ( GetTransaction(bindtxid,tx,hashBlock,false) == 0 || (numvouts= tx.vout.size()) <= 0 ) + { + CCerror = strprintf("cant find bindtxid %s",uint256_str(str,bindtxid)); + LogPrint("gatewayscc","%s\n", CCerror.c_str() ); + return(""); + } + if ( DecodeGatewaysBindOpRet(depositaddr,tx.vout[numvouts-1].scriptPubKey,tokenid,coin,totalsupply,oracletxid,M,N,msigpubkeys,taddr,prefix,prefix2) != 'B') + { + CCerror = strprintf("invalid bindtxid %s coin.%s",uint256_str(str,bindtxid),coin.c_str()); + LogPrint("gatewayscc","%s\n", CCerror.c_str() ); + return(""); + } if ( GetTransaction(bindtxid,tx,hashBlock,false) != 0 ) { + result.push_back(Pair("result","success")); + result.push_back(Pair("name","Gateways")); depositaddr[0] = 0; if ( tx.vout.size() > 0 && DecodeGatewaysBindOpRet(depositaddr,tx.vout[tx.vout.size()-1].scriptPubKey,tokenid,coin,totalsupply,oracletxid,M,N,pubkeys,taddr,prefix,prefix2) != 0 && M <= N && N > 0 ) {