Merge pull request #880 from jl777/FSM

FSM
This commit is contained in:
jl777
2018-09-13 03:56:00 -11:00
committed by GitHub
4 changed files with 104 additions and 74 deletions

View File

@@ -26,7 +26,7 @@ std::string GatewaysDeposit(uint64_t txfee,uint256 bindtxid,int32_t height,std::
std::string GatewaysClaim(uint64_t txfee,uint256 bindtxid,std::string refcoin,uint256 deposittxid,CPubKey destpub,int64_t amount); std::string GatewaysClaim(uint64_t txfee,uint256 bindtxid,std::string refcoin,uint256 deposittxid,CPubKey destpub,int64_t amount);
std::string GatewaysWithdraw(uint64_t txfee,uint256 bindtxid,std::string refcoin,std::vector<uint8_t> withdrawpub,int64_t amount); std::string GatewaysWithdraw(uint64_t txfee,uint256 bindtxid,std::string refcoin,std::vector<uint8_t> withdrawpub,int64_t amount);
UniValue GatewaysPendingWithdraws(uint256 bindtxid,std::string refcoin); UniValue GatewaysPendingWithdraws(uint256 bindtxid,std::string refcoin);
std::string GatewaysMarkdone(uint64_t txfee,uint256 withdrawtxid); std::string GatewaysMarkdone(uint64_t txfee,uint256 withdrawtxid,std::string refcoin,uint256 cointxid);
// CCcustom // CCcustom
UniValue GatewaysInfo(uint256 bindtxid); UniValue GatewaysInfo(uint256 bindtxid);

View File

@@ -308,13 +308,25 @@ uint64_t get_btcusd()
return(btcusd); return(btcusd);
} }
cJSON *get_komodocli(char **retstrp,char *acname,char *method,char *arg0,char *arg1,char *arg2) char *REFCOIN_CLI;
cJSON *get_komodocli(char *refcoin,char **retstrp,char *acname,char *method,char *arg0,char *arg1,char *arg2)
{ {
long fsize; cJSON *retjson = 0; char cmdstr[32768],*jsonstr,fname[256]; long fsize; cJSON *retjson = 0; char cmdstr[32768],*jsonstr,fname[256];
sprintf(fname,"/tmp/oraclefeed.%s",method); sprintf(fname,"/tmp/oraclefeed.%s",method);
if ( acname[0] != 0 ) if ( acname[0] != 0 )
{
if ( refcoin[0] != 0 && strcmp(refcoin,"KMD") != 0 )
printf("unexpected: refcoin.(%s) acname.(%s)\n",refcoin,acname);
sprintf(cmdstr,"./komodo-cli -ac_name=%s %s %s %s %s > %s\n",acname,method,arg0,arg1,arg2,fname); sprintf(cmdstr,"./komodo-cli -ac_name=%s %s %s %s %s > %s\n",acname,method,arg0,arg1,arg2,fname);
else sprintf(cmdstr,"./komodo-cli %s %s %s %s > %s\n",method,arg0,arg1,arg2,fname); }
else if ( strcmp(refcoin,"KMD") == 0 )
sprintf(cmdstr,"./komodo-cli %s %s %s %s > %s\n",method,arg0,arg1,arg2,fname);
else if ( REFCOIN_CLI != 0 && REFCOIN_CLI[0] != 0 )
{
sprintf(cmdstr,"%s %s %s %s %s > %s\n",REFCOIN_CLI,method,arg0,arg1,arg2,fname);
printf("ref.(%s) REFCOIN_CLI (%s)\n",refcoin,cmdstr);
}
system(cmdstr); system(cmdstr);
*retstrp = 0; *retstrp = 0;
if ( (jsonstr= filestr(&fsize,fname)) != 0 ) if ( (jsonstr= filestr(&fsize,fname)) != 0 )
@@ -327,13 +339,13 @@ cJSON *get_komodocli(char **retstrp,char *acname,char *method,char *arg0,char *a
return(retjson); return(retjson);
} }
bits256 komodobroadcast(char *acname,cJSON *hexjson) bits256 komodobroadcast(char *refcoin,char *acname,cJSON *hexjson)
{ {
char *hexstr,*retstr,str[65]; cJSON *retjson; bits256 txid; char *hexstr,*retstr,str[65]; cJSON *retjson; bits256 txid;
memset(txid.bytes,0,sizeof(txid)); memset(txid.bytes,0,sizeof(txid));
if ( (hexstr= jstr(hexjson,"hex")) != 0 ) if ( (hexstr= jstr(hexjson,"hex")) != 0 )
{ {
if ( (retjson= get_komodocli(&retstr,acname,"sendrawtransaction",hexstr,"","")) != 0 ) if ( (retjson= get_komodocli(refcoin,&retstr,acname,"sendrawtransaction",hexstr,"","")) != 0 )
{ {
//fprintf(stderr,"broadcast.(%s)\n",jprint(retjson,0)); //fprintf(stderr,"broadcast.(%s)\n",jprint(retjson,0));
free_json(retjson); free_json(retjson);
@@ -352,12 +364,12 @@ bits256 komodobroadcast(char *acname,cJSON *hexjson)
return(txid); return(txid);
} }
bits256 sendtoaddress(char *acname,char *destaddr,int64_t satoshis) bits256 sendtoaddress(char *refcoin,char *acname,char *destaddr,int64_t satoshis)
{ {
char numstr[32],*retstr,str[65]; cJSON *retjson; bits256 txid; char numstr[32],*retstr,str[65]; cJSON *retjson; bits256 txid;
memset(txid.bytes,0,sizeof(txid)); memset(txid.bytes,0,sizeof(txid));
sprintf(numstr,"%.8f",(double)satoshis/SATOSHIDEN); sprintf(numstr,"%.8f",(double)satoshis/SATOSHIDEN);
if ( (retjson= get_komodocli(&retstr,acname,"sendtoaddress",destaddr,numstr,"")) != 0 ) if ( (retjson= get_komodocli(refcoin,&retstr,acname,"sendtoaddress",destaddr,numstr,"")) != 0 )
{ {
fprintf(stderr,"unexpected sendrawtransaction json.(%s)\n",jprint(retjson,0)); fprintf(stderr,"unexpected sendrawtransaction json.(%s)\n",jprint(retjson,0));
free_json(retjson); free_json(retjson);
@@ -375,36 +387,34 @@ bits256 sendtoaddress(char *acname,char *destaddr,int64_t satoshis)
return(txid); return(txid);
} }
int32_t get_KMDheight(char *acname) int32_t get_coinheight(char *refcoin,char *acname)
{ {
cJSON *retjson; char *retstr; int32_t height=0; cJSON *retjson; char *retstr; int32_t height=0;
if ( (retjson= get_komodocli(&retstr,acname,"getinfo","","","")) != 0 ) if ( (retjson= get_komodocli(refcoin,&retstr,acname,"getblockchaininfo","","","")) != 0 )
{ {
height = jint(retjson,"blocks"); height = jint(retjson,"blocks");
//fprintf(stderr,"%s height.%d\n",acname[0]!=0?acname:"KMD",height);
free_json(retjson); free_json(retjson);
} }
else if ( retstr != 0 ) else if ( retstr != 0 )
{ {
fprintf(stderr,"get_KMDheight.(%s) error.(%s)\n",acname,retstr); fprintf(stderr,"%s get_coinheight.(%s) error.(%s)\n",refcoin,acname,retstr);
free(retstr); free(retstr);
} }
return(height); return(height);
} }
bits256 get_KMDblockhash(int32_t height) bits256 get_coinblockhash(char *refcoin,char *acname,int32_t height)
{ {
cJSON *retjson; char *retstr,heightstr[32]; bits256 hash; cJSON *retjson; char *retstr,heightstr[32]; bits256 hash;
memset(hash.bytes,0,sizeof(hash)); memset(hash.bytes,0,sizeof(hash));
sprintf(heightstr,"%d",height); sprintf(heightstr,"%d",height);
if ( (retjson= get_komodocli(&retstr,"","getblockhash",heightstr,"","")) != 0 ) if ( (retjson= get_komodocli(refcoin,&retstr,acname,"getblockhash",heightstr,"","")) != 0 )
{ {
fprintf(stderr,"unexpected blockhash json.(%s)\n",jprint(retjson,0)); fprintf(stderr,"unexpected blockhash json.(%s)\n",jprint(retjson,0));
free_json(retjson); free_json(retjson);
} }
else if ( retstr != 0 ) else if ( retstr != 0 )
{ {
//fprintf(stderr,"get_KMDblockhash.(%s) %d\n",retstr,(int32_t)strlen(retstr));
if ( strlen(retstr) >= 64 ) if ( strlen(retstr) >= 64 )
{ {
retstr[64] = 0; retstr[64] = 0;
@@ -415,11 +425,11 @@ bits256 get_KMDblockhash(int32_t height)
return(hash); return(hash);
} }
bits256 get_KMDmerkleroot(bits256 blockhash) bits256 get_coinmerkleroot(char *refcoin,char *acname,bits256 blockhash)
{ {
cJSON *retjson; char *retstr,str[65]; bits256 merkleroot; cJSON *retjson; char *retstr,str[65]; bits256 merkleroot;
memset(merkleroot.bytes,0,sizeof(merkleroot)); memset(merkleroot.bytes,0,sizeof(merkleroot));
if ( (retjson= get_komodocli(&retstr,"","getblockheader",bits256_str(str,blockhash),"","")) != 0 ) if ( (retjson= get_komodocli(refcoin,&retstr,acname,"getblockheader",bits256_str(str,blockhash),"","")) != 0 )
{ {
merkleroot = jbits256(retjson,"merkleroot"); merkleroot = jbits256(retjson,"merkleroot");
//fprintf(stderr,"got merkleroot.(%s)\n",bits256_str(str,merkleroot)); //fprintf(stderr,"got merkleroot.(%s)\n",bits256_str(str,merkleroot));
@@ -427,24 +437,24 @@ bits256 get_KMDmerkleroot(bits256 blockhash)
} }
else if ( retstr != 0 ) else if ( retstr != 0 )
{ {
fprintf(stderr,"get_KMDmerkleroot error.(%s)\n",retstr); fprintf(stderr,"%s %s get_coinmerkleroot error.(%s)\n",refcoin,acname,retstr);
free(retstr); free(retstr);
} }
return(merkleroot); return(merkleroot);
} }
int32_t get_KMDheader(bits256 *blockhashp,bits256 *merklerootp,int32_t prevheight) int32_t get_coinheader(char *refcoin,char *acname,bits256 *blockhashp,bits256 *merklerootp,int32_t prevheight)
{ {
int32_t height = 0; char str[65]; int32_t height = 0; char str[65];
if ( prevheight == 0 ) if ( prevheight == 0 )
height = get_KMDheight("") - 20; height = get_coinheight(refcoin,acname) - 20;
else height = prevheight + 1; else height = prevheight + 1;
if ( height > 0 ) if ( height > 0 )
{ {
*blockhashp = get_KMDblockhash(height); *blockhashp = get_coinblockhash(refcoin,acname,height);
if ( bits256_nonz(*blockhashp) != 0 ) if ( bits256_nonz(*blockhashp) != 0 )
{ {
*merklerootp = get_KMDmerkleroot(*blockhashp); *merklerootp = get_coinmerkleroot(refcoin,acname,*blockhashp);
if ( bits256_nonz(*merklerootp) != 0 ) if ( bits256_nonz(*merklerootp) != 0 )
return(height); return(height);
} }
@@ -454,26 +464,26 @@ int32_t get_KMDheader(bits256 *blockhashp,bits256 *merklerootp,int32_t prevheigh
return(0); return(0);
} }
cJSON *get_gatewayspending(char *acname,char *oraclestxidstr,char *coin) cJSON *get_gatewayspending(char *refcoin,char *acname,char *oraclestxidstr)
{ {
cJSON *retjson; char *retstr; cJSON *retjson; char *retstr;
if ( (retjson= get_komodocli(&retstr,acname,"gatewayspending",oraclestxidstr,coin,"")) != 0 ) if ( (retjson= get_komodocli(refcoin,&retstr,acname,"gatewayspending",oraclestxidstr,refcoin,"")) != 0 )
{ {
//printf("pending.(%s)\n",jprint(retjson,0)); //printf("pending.(%s)\n",jprint(retjson,0));
return(retjson); return(retjson);
} }
else if ( retstr != 0 ) else if ( retstr != 0 )
{ {
fprintf(stderr,"get_gatewayspending.(%s) error.(%s)\n",acname,retstr); fprintf(stderr,"%s get_gatewayspending.(%s) error.(%s)\n",refcoin,acname,retstr);
free(retstr); free(retstr);
} }
return(0); return(0);
} }
cJSON *get_rawmempool(char *acname) cJSON *get_rawmempool(char *refcoin,char *acname)
{ {
cJSON *retjson; char *retstr; cJSON *retjson; char *retstr;
if ( (retjson= get_komodocli(&retstr,acname,"getrawmempool","","","")) != 0 ) if ( (retjson= get_komodocli(refcoin,&retstr,acname,"getrawmempool","","","")) != 0 )
{ {
//printf("mempool.(%s)\n",jprint(retjson,0)); //printf("mempool.(%s)\n",jprint(retjson,0));
return(retjson); return(retjson);
@@ -486,11 +496,13 @@ cJSON *get_rawmempool(char *acname)
return(0); return(0);
} }
cJSON *get_addressutxos(char *acname,char *coinaddr) cJSON *get_addressutxos(char *refcoin,char *acname,char *coinaddr)
{ {
cJSON *retjson; char *retstr,jsonbuf[256]; cJSON *retjson; char *retstr,jsonbuf[256];
if ( refcoin[0] != 0 && strcmp(refcoin,"KMD") != 0 )
printf("warning: assumes %s has addressindex enabled\n",refcoin);
sprintf(jsonbuf,"{\\\"addresses\\\":[\\\"%s\\\"]}",coinaddr); sprintf(jsonbuf,"{\\\"addresses\\\":[\\\"%s\\\"]}",coinaddr);
if ( (retjson= get_komodocli(&retstr,acname,"getaddressutxos",jsonbuf,"","")) != 0 ) if ( (retjson= get_komodocli(refcoin,&retstr,acname,"getaddressutxos",jsonbuf,"","")) != 0 )
{ {
//printf("addressutxos.(%s)\n",jprint(retjson,0)); //printf("addressutxos.(%s)\n",jprint(retjson,0));
return(retjson); return(retjson);
@@ -503,10 +515,10 @@ cJSON *get_addressutxos(char *acname,char *coinaddr)
return(0); return(0);
} }
cJSON *get_rawtransaction(char *acname,bits256 txid) cJSON *get_rawtransaction(char *refcoin,char *acname,bits256 txid)
{ {
cJSON *retjson; char *retstr,str[65]; cJSON *retjson; char *retstr,str[65];
if ( (retjson= get_komodocli(&retstr,acname,"getrawtransaction",bits256_str(str,txid),"1","")) != 0 ) if ( (retjson= get_komodocli(refcoin,&retstr,acname,"getrawtransaction",bits256_str(str,txid),"1","")) != 0 )
{ {
return(retjson); return(retjson);
} }
@@ -518,13 +530,13 @@ cJSON *get_rawtransaction(char *acname,bits256 txid)
return(0); return(0);
} }
void gatewaysmarkdone(char *acname,bits256 txid) void gatewaysmarkdone(char *refcoin,char *acname,bits256 withtxid,char *coin,bits256 cointxid)
{ {
char str[65],*retstr; cJSON *retjson; char str[65],str2[65],*retstr; cJSON *retjson;
printf("spend %s %s/v2 as marker\n",acname,bits256_str(str,txid)); printf("spend %s %s/v2 as marker\n",acname,bits256_str(str,withtxid));
if ( (retjson= get_komodocli(&retstr,acname,"gatewaysmarkdone",bits256_str(str,txid),"","")) != 0 ) if ( (retjson= get_komodocli(refcoin,&retstr,acname,"gatewaysmarkdone",bits256_str(str,withtxid),coin,bits256_str(str2,cointxid))) != 0 )
{ {
komodobroadcast(acname,retjson); komodobroadcast(refcoin,acname,retjson);
free_json(retjson); free_json(retjson);
} }
else if ( retstr != 0 ) else if ( retstr != 0 )
@@ -534,10 +546,10 @@ void gatewaysmarkdone(char *acname,bits256 txid)
} }
} }
int32_t tx_has_voutaddress(char *acname,bits256 txid,char *coinaddr) int32_t tx_has_voutaddress(char *refcoin,char *acname,bits256 txid,char *coinaddr)
{ {
cJSON *txobj,*vouts,*vout,*sobj,*addresses; char *addr,str[65]; int32_t i,j,n,numvouts,retval = 0; cJSON *txobj,*vouts,*vout,*sobj,*addresses; char *addr,str[65]; int32_t i,j,n,numvouts,retval = 0;
if ( (txobj= get_rawtransaction(acname,txid)) != 0 ) if ( (txobj= get_rawtransaction(refcoin,acname,txid)) != 0 )
{ {
if ( (vouts= jarray(&numvouts,txobj,"vout")) != 0 ) if ( (vouts= jarray(&numvouts,txobj,"vout")) != 0 )
{ {
@@ -566,24 +578,24 @@ int32_t tx_has_voutaddress(char *acname,bits256 txid,char *coinaddr)
return(retval); return(retval);
} }
int32_t coinaddrexists(char *acname,char *coinaddr) int32_t coinaddrexists(char *refcoin,char *acname,char *coinaddr)
{ {
cJSON *array; bits256 txid; int32_t i,n,num=0; cJSON *array; bits256 txid; int32_t i,n,num=0;
if ( (array= get_addressutxos(acname,coinaddr)) != 0 ) if ( (array= get_addressutxos(refcoin,acname,coinaddr)) != 0 )
{ {
num = cJSON_GetArraySize(array); num = cJSON_GetArraySize(array);
free_json(array); free_json(array);
} else return(-1); } else return(-1);
if ( num == 0 ) if ( num == 0 )
{ {
if ( (array= get_rawmempool(acname)) != 0 ) if ( (array= get_rawmempool(refcoin,acname)) != 0 )
{ {
if ( (n= cJSON_GetArraySize(array)) != 0 ) if ( (n= cJSON_GetArraySize(array)) != 0 )
{ {
for (i=0; i<n; i++) for (i=0; i<n; i++)
{ {
txid = jbits256i(array,i); txid = jbits256i(array,i);
if ( tx_has_voutaddress(acname,txid,coinaddr) > 0 ) if ( tx_has_voutaddress(refcoin,acname,txid,coinaddr) > 0 )
{ {
num = 1; num = 1;
break; break;
@@ -596,7 +608,7 @@ int32_t coinaddrexists(char *acname,char *coinaddr)
return(num); return(num);
} }
void update_gatewayspending(char *acname,char *oraclestxidstr,char *coin) void update_gatewayspending(char *refcoin,char *acname,char *oraclestxidstr)
{ {
// check queue to prevent duplicate // check queue to prevent duplicate
// check KMD chain and mempool for txidaddr // check KMD chain and mempool for txidaddr
@@ -605,10 +617,11 @@ void update_gatewayspending(char *acname,char *oraclestxidstr,char *coin)
/// if enough sigs, sendrawtransaction and when it confirms spend marker (txid.2) /// if enough sigs, sendrawtransaction and when it confirms spend marker (txid.2)
/// if not enough sigs, post partially signed to acname with marker2 /// if not enough sigs, post partially signed to acname with marker2
// monitor marker2, for the partially signed withdraws // monitor marker2, for the partially signed withdraws
cJSON *retjson,*pending,*item; char str[65],*coinstr,*txidaddr,*signeraddr,*withdrawaddr; int32_t i,n,retval,processed = 0; bits256 txid,withtxid,origtxid; int64_t satoshis; cJSON *retjson,*pending,*item; char str[65],*coinstr,*txidaddr,*signeraddr,*withdrawaddr; int32_t i,n,retval,processed = 0; bits256 txid,cointxid,origtxid,zeroid; int64_t satoshis;
if ( (retjson= get_gatewayspending(acname,oraclestxidstr,coin)) != 0 ) memset(&zeroid,0,sizeof(zeroid));
if ( (retjson= get_gatewayspending("KMD",acname,oraclestxidstr)) != 0 )
{ {
if ( jint(retjson,"queueflag") != 0 && (coinstr= jstr(retjson,"coin")) != 0 && strcmp(coinstr,coin) == 0 ) if ( jint(retjson,"queueflag") != 0 && (coinstr= jstr(retjson,"coin")) != 0 && strcmp(coinstr,refcoin) == 0 )
{ {
if ( (pending= jarray(&n,retjson,"pending")) != 0 ) if ( (pending= jarray(&n,retjson,"pending")) != 0 )
{ {
@@ -621,30 +634,30 @@ void update_gatewayspending(char *acname,char *oraclestxidstr,char *coin)
//process item.0 {"txid":"10ec8f4dad6903df6b249b361b879ac77b0617caad7629b97e10f29fa7e99a9b","txidaddr":"RMbite4TGugVmkGmu76ytPHDEQZQGSUjxz","withdrawaddr":"RNJmgYaFF5DbnrNUX6pMYz9rcnDKC2tuAc","amount":"1.00000000","depositaddr":"RHV2As4rox97BuE3LK96vMeNY8VsGRTmBj","signeraddr":"RHV2As4rox97BuE3LK96vMeNY8VsGRTmBj"} //process item.0 {"txid":"10ec8f4dad6903df6b249b361b879ac77b0617caad7629b97e10f29fa7e99a9b","txidaddr":"RMbite4TGugVmkGmu76ytPHDEQZQGSUjxz","withdrawaddr":"RNJmgYaFF5DbnrNUX6pMYz9rcnDKC2tuAc","amount":"1.00000000","depositaddr":"RHV2As4rox97BuE3LK96vMeNY8VsGRTmBj","signeraddr":"RHV2As4rox97BuE3LK96vMeNY8VsGRTmBj"}
if ( (txidaddr= jstr(item,"txidaddr")) != 0 && (withdrawaddr= jstr(item,"withdrawaddr")) != 0 && (signeraddr= jstr(item,"signeraddr")) != 0 ) if ( (txidaddr= jstr(item,"txidaddr")) != 0 && (withdrawaddr= jstr(item,"withdrawaddr")) != 0 && (signeraddr= jstr(item,"signeraddr")) != 0 )
{ {
if ( (satoshis= jdouble(item,"amount")*SATOSHIDEN) != 0 && (retval= coinaddrexists(acname,txidaddr)) == 0 ) if ( (satoshis= jdouble(item,"amount")*SATOSHIDEN) != 0 && (retval= coinaddrexists("KMD",acname,txidaddr)) == 0 )
{ {
// this is less errors but more expensive: ./komodo-cli z_sendmany "signeraddr" '[{"address":"<txidaddr>","amount":0.0001},{"address":"<withdrawaddr>","amount":<withamount>}]' // this is less errors but more expensive: ./komodo-cli z_sendmany "signeraddr" '[{"address":"<txidaddr>","amount":0.0001},{"address":"<withdrawaddr>","amount":<withamount>}]'
txid = sendtoaddress(acname,txidaddr,10000); txid = sendtoaddress("KMD",acname,txidaddr,10000);
if ( bits256_nonz(txid) != 0 && coinaddrexists(acname,txidaddr) > 0 ) if ( bits256_nonz(txid) != 0 && coinaddrexists("KMD",acname,txidaddr) > 0 )
{ {
// the actual withdraw // the actual withdraw
withtxid = sendtoaddress(strcmp("KMD",coin)==0?"":coin,withdrawaddr,satoshis); cointxid = sendtoaddress(refcoin,"",withdrawaddr,satoshis);
if ( bits256_nonz(withtxid) != 0 ) if ( bits256_nonz(cointxid) != 0 )
{ {
fprintf(stderr,"withdraw %s %s %s %.8f processed\n",coin,bits256_str(str,withtxid),withdrawaddr,(double)satoshis/SATOSHIDEN); fprintf(stderr,"withdraw %s %s %s %.8f processed\n",refcoin,bits256_str(str,cointxid),withdrawaddr,(double)satoshis/SATOSHIDEN);
gatewaysmarkdone(acname,origtxid); gatewaysmarkdone("KMD",acname,origtxid,refcoin,cointxid);
processed++; processed++;
} }
else else
{ {
fprintf(stderr,"ERROR withdraw %s %s %s %.8f processed\n",coin,bits256_str(str,withtxid),withdrawaddr,(double)satoshis/SATOSHIDEN); fprintf(stderr,"ERROR withdraw %s %s %s %.8f processed\n",refcoin,bits256_str(str,cointxid),withdrawaddr,(double)satoshis/SATOSHIDEN);
} }
} else fprintf(stderr,"error sending %s txidaddr.%s -> %s exists.%d\n",acname,txidaddr,bits256_str(str,txid),coinaddrexists(acname,txidaddr)); } else fprintf(stderr,"error sending %s txidaddr.%s -> %s exists.%d\n",acname,txidaddr,bits256_str(str,txid),coinaddrexists(refcoin,acname,txidaddr));
} }
else if ( retval > 0 ) else if ( retval > 0 )
{ {
fprintf(stderr,"already did withdraw %s %s %.8f processed\n",coin,withdrawaddr,(double)satoshis/SATOSHIDEN); fprintf(stderr,"already did withdraw %s %s %.8f processed\n",refcoin,withdrawaddr,(double)satoshis/SATOSHIDEN);
gatewaysmarkdone(acname,origtxid); gatewaysmarkdone("KMD",acname,origtxid,refcoin,zeroid);
} }
} }
} }
@@ -654,7 +667,7 @@ void update_gatewayspending(char *acname,char *oraclestxidstr,char *coin)
} }
} }
int32_t get_oracledata(int32_t prevheight,char *hexstr,int32_t maxsize,char *format) int32_t get_oracledata(char *refcoin,char *acname,int32_t prevheight,char *hexstr,int32_t maxsize,char *format)
{ {
int32_t i; uint32_t height; uint64_t price; bits256 blockhash,merkleroot; int32_t i; uint32_t height; uint64_t price; bits256 blockhash,merkleroot;
hexstr[0] = 0; hexstr[0] = 0;
@@ -670,7 +683,7 @@ int32_t get_oracledata(int32_t prevheight,char *hexstr,int32_t maxsize,char *for
} }
else if ( strcmp(format,"Ihh") == 0 ) else if ( strcmp(format,"Ihh") == 0 )
{ {
if ( (height= get_KMDheader(&blockhash,&merkleroot,prevheight)) > prevheight ) if ( (height= get_coinheader(refcoin,acname,&blockhash,&merkleroot,prevheight)) > prevheight )
{ {
for (i=0; i<4; i++) for (i=0; i<4; i++)
sprintf(&hexstr[i*2],"%02x",(uint8_t)((height >> (i*8)) & 0xff)); sprintf(&hexstr[i*2],"%02x",(uint8_t)((height >> (i*8)) & 0xff));
@@ -711,28 +724,43 @@ oraclesdata 17a841a919c284cea8a676f34e793da002e606f19a9258a3190bed12d5aaa3ff 034
int32_t main(int32_t argc,char **argv) int32_t main(int32_t argc,char **argv)
{ {
cJSON *clijson,*clijson2,*regjson,*item; int32_t acheight,i,retval,n,height,prevheight = 0; char *format,*acname,*oraclestr,*bindtxidstr,*pkstr,*pubstr,*retstr,*retstr2,hexstr[4096]; uint64_t price; bits256 txid; cJSON *clijson,*clijson2,*regjson,*item; int32_t acheight,i,retval,n,height,prevheight = 0; char *format,*acname,*oraclestr,*bindtxidstr,*pkstr,*pubstr,*retstr,*retstr2,hexstr[4096],refcoin[64]; uint64_t price; bits256 txid;
if ( argc != 6 ) if ( argc < 6 )
{ {
printf("usage: oraclefeed $ACNAME $ORACLETXID $MYPUBKEY $FORMAT $BINDTXID\nPowered by CoinDesk (%s) %.8f\n","https://www.coindesk.com/price/",dstr(get_btcusd())); printf("usage: oraclefeed $ACNAME $ORACLETXID $MYPUBKEY $FORMAT $BINDTXID [refcoin_cli]\n");
return(-1); return(-1);
} }
printf("Powered by CoinDesk (%s) %.8f\n","https://www.coindesk.com/price/",dstr(get_btcusd()));
acname = argv[1]; acname = argv[1];
oraclestr = argv[2]; oraclestr = argv[2];
pkstr = argv[3]; pkstr = argv[3];
format = argv[4]; format = argv[4];
bindtxidstr = argv[5]; bindtxidstr = argv[5];
if ( argc > 6 )
REFCOIN_CLI = argv[6];
else REFCOIN_CLI = "./komodo-cli";
if ( strncmp(format,"Ihh",3) != 0 && format[0] != 'L' ) if ( strncmp(format,"Ihh",3) != 0 && format[0] != 'L' )
{ {
printf("only formats of L and Ihh are supported now\n"); printf("only formats of L and Ihh are supported now\n");
return(-1); return(-1);
} }
acheight = 0; acheight = 0;
refcoin[0] = 0;
while ( 1 ) while ( 1 )
{ {
retstr = 0; retstr = 0;
if ( prevheight < (get_KMDheight("") - 10) && (clijson= get_komodocli(&retstr,acname,"oraclesinfo",oraclestr,"","")) != 0 ) if ( (refcoin[0] == 0 || prevheight < (get_coinheight(refcoin,"") - 10)) && (clijson= get_komodocli("KMD",&retstr,acname,"oraclesinfo",oraclestr,"","")) != 0 )
{ {
if ( refcoin[0] == 0 && jstr(clijson,"name") != 0 )
{
strcpy(refcoin,jstr(clijson,"name"));
if ( strcmp("KMD",refcoin) != 0 && argc != 7 )
{
printf("need to specify path to refcoin's cli as last argv\n");
exit(0);
}
printf("set refcoin <- %s [%s]\n",refcoin,REFCOIN_CLI);
}
if ( (regjson= jarray(&n,clijson,"registered")) != 0 ) if ( (regjson= jarray(&n,clijson,"registered")) != 0 )
{ {
for (i=0; i<n; i++) for (i=0; i<n; i++)
@@ -740,18 +768,18 @@ int32_t main(int32_t argc,char **argv)
item = jitem(regjson,i); item = jitem(regjson,i);
if ( (pubstr= jstr(item,"publisher")) != 0 && strcmp(pkstr,pubstr) == 0 ) if ( (pubstr= jstr(item,"publisher")) != 0 && strcmp(pkstr,pubstr) == 0 )
{ {
if ( (height= get_oracledata(prevheight,hexstr,sizeof(hexstr),"Ihh")) != 0 ) if ( (height= get_oracledata(refcoin,"",prevheight,hexstr,sizeof(hexstr),"Ihh")) != 0 )
{ {
if ( (clijson2= get_komodocli(&retstr2,acname,"oraclesdata",oraclestr,hexstr,"")) != 0 ) if ( (clijson2= get_komodocli("KMD",&retstr2,acname,"oraclesdata",oraclestr,hexstr,"")) != 0 )
{ {
//printf("data.(%s)\n",jprint(clijson2,0)); //printf("data.(%s)\n",jprint(clijson2,0));
txid = komodobroadcast(acname,clijson2); txid = komodobroadcast("KMD",acname,clijson2);
if ( bits256_nonz(txid) != 0 ) if ( bits256_nonz(txid) != 0 )
{ {
prevheight = height; prevheight = height;
acheight = get_KMDheight(acname); acheight = get_coinheight(refcoin,"");
printf("ht.%d <- %s\n",height,hexstr); printf("%s ht.%d <- %s\n",refcoin,height,hexstr);
update_gatewayspending(acname,bindtxidstr,"KMD"); update_gatewayspending(refcoin,acname,bindtxidstr);
} }
free_json(clijson2); free_json(clijson2);
} }

View File

@@ -18,7 +18,7 @@
/* /*
prevent duplicate bindtxid via mempool scan prevent duplicate bindtxid via mempool scan
wait for notarization for oraclefeed and validation of gatewaysdeposit wait for notarization for oraclefeed and validation of gatewaysdeposit
debug multisig and do partial signing
validation validation
string oracles string oracles
@@ -762,7 +762,7 @@ std::string GatewaysWithdraw(uint64_t txfee,uint256 bindtxid,std::string refcoin
return(""); return("");
} }
std::string GatewaysMarkdone(uint64_t txfee,uint256 withdrawtxid) std::string GatewaysMarkdone(uint64_t txfee,uint256 withdrawtxid,std::string refcoin,uint256 cointxid)
{ {
CMutableTransaction mtx; CScript opret; CPubKey mypk; struct CCcontract_info *cp,C; CMutableTransaction mtx; CScript opret; CPubKey mypk; struct CCcontract_info *cp,C;
cp = CCinit(&C,EVAL_GATEWAYS); cp = CCinit(&C,EVAL_GATEWAYS);
@@ -771,6 +771,7 @@ std::string GatewaysMarkdone(uint64_t txfee,uint256 withdrawtxid)
mypk = pubkey2pk(Mypubkey()); mypk = pubkey2pk(Mypubkey());
mtx.vin.push_back(CTxIn(withdrawtxid,2,CScript())); mtx.vin.push_back(CTxIn(withdrawtxid,2,CScript()));
mtx.vout.push_back(CTxOut(5000,CScript() << ParseHex(HexStr(mypk)) << OP_CHECKSIG)); mtx.vout.push_back(CTxOut(5000,CScript() << ParseHex(HexStr(mypk)) << OP_CHECKSIG));
opret << OP_RETURN << E_MARSHAL(ss << cp->evalcode << 'M' << cointxid << refcoin);
return(FinalizeCCTx(0,cp,mtx,mypk,txfee,opret)); return(FinalizeCCTx(0,cp,mtx,mypk,txfee,opret));
} }
@@ -824,7 +825,6 @@ UniValue GatewaysPendingWithdraws(uint256 bindtxid,std::string refcoin)
obj.push_back(Pair("depositaddr",depositaddr)); obj.push_back(Pair("depositaddr",depositaddr));
Getscriptaddress(signeraddr,CScript() << ParseHex(HexStr(mypk)) << OP_CHECKSIG); Getscriptaddress(signeraddr,CScript() << ParseHex(HexStr(mypk)) << OP_CHECKSIG);
obj.push_back(Pair("signeraddr",signeraddr)); obj.push_back(Pair("signeraddr",signeraddr));
// numqueued += GatewaysAddQueue(refcoin,txid,tx.vout[1].scriptPubKey,tx.vout[0].nValue);
} }
pending.push_back(obj); pending.push_back(obj);
} }

View File

@@ -5514,13 +5514,15 @@ UniValue gatewayswithdraw(const UniValue& params, bool fHelp)
UniValue gatewaysmarkdone(const UniValue& params, bool fHelp) UniValue gatewaysmarkdone(const UniValue& params, bool fHelp)
{ {
UniValue result(UniValue::VOBJ); uint256 withdrawtxid; std::string hex; UniValue result(UniValue::VOBJ); uint256 withdrawtxid,cointxid; std::string hex,coin;
if ( fHelp || params.size() != 1 ) if ( fHelp || params.size() != 1 )
throw runtime_error("gatewaysmarkdone withdrawtxid\n"); throw runtime_error("gatewaysmarkdone withdrawtxid coin cointxid\n");
if ( ensure_CCrequirements() < 0 ) if ( ensure_CCrequirements() < 0 )
throw runtime_error("to use CC contracts, you need to launch daemon with valid -pubkey= for an address in your wallet\n"); throw runtime_error("to use CC contracts, you need to launch daemon with valid -pubkey= for an address in your wallet\n");
withdrawtxid = Parseuint256((char *)params[0].get_str().c_str()); withdrawtxid = Parseuint256((char *)params[0].get_str().c_str());
hex = GatewaysMarkdone(0,withdrawtxid); coin = params[1].get_str();
cointxid = Parseuint256((char *)params[2].get_str().c_str());
hex = GatewaysMarkdone(0,withdrawtxid,coin,cointxid);
if ( hex.size() > 0 ) if ( hex.size() > 0 )
{ {
result.push_back(Pair("result", "success")); result.push_back(Pair("result", "success"));