From 4d4a4dd71bbc793a5e057808698f9b5ba4c30935 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 4 May 2016 10:58:57 -0500 Subject: [PATCH] test --- iguana/iguana777.h | 2 +- iguana/iguana_payments.c | 36 ++++++++++++++++++++++-------- iguana/iguana_unspents.c | 47 ++++++++++++++++++++++------------------ iguana/iguana_wallet.c | 7 ++++-- iguana/ramchain_api.c | 2 +- 5 files changed, 60 insertions(+), 34 deletions(-) diff --git a/iguana/iguana777.h b/iguana/iguana777.h index 123b9c796..3ac9c5f9c 100755 --- a/iguana/iguana777.h +++ b/iguana/iguana777.h @@ -829,7 +829,7 @@ int32_t bitcoin_addr2rmd160(uint8_t *addrtypep,uint8_t rmd160[20],char *coinaddr char *issue_startForging(struct supernet_info *myinfo,char *secret); struct bitcoin_unspent *iguana_unspentsget(struct supernet_info *myinfo,struct iguana_info *coin,char **retstrp,double *balancep,int32_t *numunspentsp,double minconfirms,char *account); void iguana_chainparms(struct iguana_chain *chain,cJSON *argjson); -int32_t iguana_pkhasharray(struct supernet_info *myinfo,struct iguana_info *coin,cJSON *array,int32_t minconf,int32_t maxconf,int64_t *totalp,struct iguana_pkhash *P,int32_t max,uint8_t rmd160[20],char *coinaddr,uint8_t *pubkey33,int32_t lastheight,uint64_t *unspents,int32_t *numunspentsp); +int32_t iguana_pkhasharray(struct supernet_info *myinfo,struct iguana_info *coin,cJSON *array,int32_t minconf,int32_t maxconf,int64_t *totalp,struct iguana_pkhash *P,int32_t max,uint8_t rmd160[20],char *coinaddr,uint8_t *pubkey33,int32_t lastheight,uint64_t *unspents,int32_t *numunspentsp,int32_t maxunspents); long iguana_spentsfile(struct iguana_info *coin,int32_t n); uint8_t *iguana_rmdarray(struct iguana_info *coin,int32_t *numrmdsp,cJSON *array,int32_t firsti); int64_t iguana_unspents(struct supernet_info *myinfo,struct iguana_info *coin,cJSON *array,int32_t minconf,int32_t maxconf,uint8_t *rmdarray,int32_t numrmds,int32_t lastheight,uint64_t *unspents,int32_t *numunspentsp); diff --git a/iguana/iguana_payments.c b/iguana/iguana_payments.c index c262de60a..1cde100b9 100755 --- a/iguana/iguana_payments.c +++ b/iguana/iguana_payments.c @@ -158,17 +158,35 @@ cJSON *iguana_scriptobj(struct iguana_info *coin,uint8_t rmd160[20],char *coinad return(scriptobj); } -char *sendtoaddress(struct supernet_info *myinfo,struct iguana_info *coin,char *coinaddr,double amount,char *comment,char *comment2,int32_t minconf,char *account) +char *sendtoaddress(struct supernet_info *myinfo,struct iguana_info *coin,char *coinaddr,uint64_t satoshis,char *comment,char *comment2,int32_t minconf,char *account) { - uint8_t addrtype,rmd160[20]; int32_t numwaddrs; struct iguana_waddress **waddrs; + uint8_t addrtype,rmd160[20],*rmdarray; int32_t i,numwaddrs,numrmds,numunspents=0; struct iguana_waddress **waddrs,*waddr; uint64_t *unspents; //sendtoaddress [comment] [comment-to] is a real and is rounded to 8 decimal places. Returns the transaction ID if successful. Y - if ( coinaddr != 0 && coinaddr[0] != 0 && amount > 0. ) + if ( coinaddr != 0 && coinaddr[0] != 0 && satoshis != 0 ) { if ( iguana_addressvalidate(coin,&addrtype,rmd160,coinaddr) < 0 ) return(clonestr("{\"error\":\"invalid coin address\"}")); - numwaddrs = iguana_unspentslists(myinfo,coin,(struct iguana_waddress **)coin->blockspace,(int32_t)(sizeof(coin->blockspace)/sizeof(*waddrs)),amount,minconf,0); - - printf("need to generate send %.8f to %s [%s] [%s] using numaddrs.%d\n",dstr(amount),coinaddr,comment!=0?comment:"",comment2!=0?comment2:"",numwaddrs); + waddrs = (struct iguana_waddress **)coin->blockspace; + numwaddrs = iguana_unspentslists(myinfo,coin,waddrs,(int32_t)(sizeof(coin->blockspace)/sizeof(*waddrs)),satoshis,minconf,0); + if ( numwaddrs > 0 ) + { + unspents = (uint64_t *)((long)coin->blockspace + sizeof(*waddrs)*numwaddrs); + for (i=0; icoinaddr,dstr(waddr->balance)); + } + if ( (rmdarray= iguana_rmdarray(coin,&numrmds,0,0)) != 0 ) + { + numunspents = (int32_t)((sizeof(coin->blockspace) - sizeof(*waddrs)*numwaddrs) / sizeof(uint64_t)); + iguana_unspents(myinfo,coin,0,minconf,coin->longestchain,rmdarray,numrmds,0,unspents,&numunspents); + if ( rmdarray != 0 ) + free(rmdarray); + for (i=0; i>32),(uint32_t)unspents[i]); + } + } + printf("need to generate send %.8f to %s [%s] [%s] using numaddrs.%d numunspents.%d\n",dstr(satoshis),coinaddr,comment!=0?comment:"",comment2!=0?comment2:"",numwaddrs,numunspents); } return(clonestr("{\"error\":\"need address and amount\"}")); } @@ -757,7 +775,7 @@ S_D_SS(bitcoinrpc,sendtoaddress,address,amount,comment,comment2) return(clonestr("{\"error\":\"need to unlock wallet\"}")); myinfo->expiration++; iguana_unspentset(myinfo,coin); - return(sendtoaddress(myinfo,coin,address,amount,comment,comment2,coin->minconfirms,0)); + return(sendtoaddress(myinfo,coin,address,amount * SATOSHIDEN,comment,comment2,coin->minconfirms,0)); } SS_D_I_SS(bitcoinrpc,sendfrom,fromaccount,toaddress,amount,minconf,comment,comment2) @@ -768,7 +786,7 @@ SS_D_I_SS(bitcoinrpc,sendfrom,fromaccount,toaddress,amount,minconf,comment,comme return(clonestr("{\"error\":\"need to unlock wallet\"}")); myinfo->expiration++; iguana_unspentset(myinfo,coin); - return(sendtoaddress(myinfo,coin,toaddress,amount,comment,comment2,minconf,fromaccount)); + return(sendtoaddress(myinfo,coin,toaddress,amount * SATOSHIDEN,comment,comment2,minconf,fromaccount)); } S_A_I_S(bitcoinrpc,sendmany,fromaccount,payments,minconf,comment) @@ -789,7 +807,7 @@ S_A_I_S(bitcoinrpc,sendmany,fromaccount,payments,minconf,comment) amount = jdouble(item,0); val = amount * SATOSHIDEN; printf("(%s %.8f) ",coinaddr,dstr(val)); - if ( (str= sendtoaddress(myinfo,coin,coinaddr,amount,comment,"",minconf,fromaccount)) != 0 ) + if ( (str= sendtoaddress(myinfo,coin,coinaddr,val,comment,"",minconf,fromaccount)) != 0 ) { free(str); } diff --git a/iguana/iguana_unspents.c b/iguana/iguana_unspents.c index 454c2b824..9e67abe46 100755 --- a/iguana/iguana_unspents.c +++ b/iguana/iguana_unspents.c @@ -138,7 +138,8 @@ struct iguana_pkhash *iguana_pkhashfind(struct iguana_info *coin,struct iguana_r *depositsp = ACCTS[pkind].total; *lastunspentindp = ACCTS[pkind].lastunspentind; printf("[%d] return pkind.%u of %u P.%p %.8f last.%u ACCTS.%p %p\n",i,pkind,numpkinds,P,dstr(*depositsp),*lastunspentindp,ACCTS,ramchain->A); - *p = P[pkind]; + if ( P != 0 ) + *p = P[pkind]; return(p); } else if ( pkind != 0 ) @@ -223,12 +224,9 @@ int64_t iguana_pkhashbalance(struct supernet_info *myinfo,struct iguana_info *co return(deposits - spent); } -int32_t iguana_pkhasharray(struct supernet_info *myinfo,struct iguana_info *coin,cJSON *array,int32_t minconf,int32_t maxconf,int64_t *totalp,struct iguana_pkhash *P,int32_t max,uint8_t rmd160[20],char *coinaddr,uint8_t *pubkey33,int32_t lastheight,uint64_t *unspents,int32_t *numunspentsp) +int32_t iguana_pkhasharray(struct supernet_info *myinfo,struct iguana_info *coin,cJSON *array,int32_t minconf,int32_t maxconf,int64_t *totalp,struct iguana_pkhash *P,int32_t max,uint8_t rmd160[20],char *coinaddr,uint8_t *pubkey33,int32_t lastheight,uint64_t *unspents,int32_t *numunspentsp,int32_t maxunspents) { - int32_t i,n,m,maxunspents,numunspents = 0; int64_t spent,deposits,netbalance,total; uint32_t lastunspentind; struct iguana_pkhash *p,_p; struct iguana_ramchain *ramchain; struct iguana_bundle *bp; - if ( numunspentsp != 0 ) - maxunspents = *numunspentsp, *numunspentsp = 0; - else maxunspents = 0; + int32_t i,n,m,numunspents = *numunspentsp; int64_t spent,deposits,netbalance,total; uint32_t lastunspentind; struct iguana_pkhash *p,_p; struct iguana_ramchain *ramchain; struct iguana_bundle *bp; if ( 0 && coin->RTramchain_busy != 0 ) { printf("iguana_pkhasharray: unexpected access when RTramchain_busy\n"); @@ -248,7 +246,7 @@ int32_t iguana_pkhasharray(struct supernet_info *myinfo,struct iguana_info *coin continue; if ( (coin->blocks.hwmchain.height - bp->bundleheight) < minconf ) break; - if ( iguana_pkhashfind(coin,&ramchain,&deposits,&lastunspentind,&P[n],rmd160,i,i) != 0 ) + if ( iguana_pkhashfind(coin,&ramchain,&deposits,&lastunspentind,P != 0 ? &P[n] : &_p,rmd160,i,i) != 0 ) { m = maxunspents; p = (P == 0) ? &_p : &P[n]; @@ -264,24 +262,26 @@ int32_t iguana_pkhasharray(struct supernet_info *myinfo,struct iguana_info *coin } maxunspents -= m; numunspents += m; + printf("%d: balance %.8f, lastunspent.%u m.%d num.%d max.%d\n",i,dstr(total),lastunspentind,m,numunspents,maxunspents); } - //printf("%d: balance %.8f, lastunspent.%u\n",i,dstr(balance),lastunspentind); } - //printf("n.%d max.%d\n",n,max); if ( numunspentsp != 0 ) *numunspentsp = numunspents; + printf("numunspents.%d [%d] max.%d\n",numunspents,*numunspentsp,maxunspents); *totalp += total; return(n); } int64_t iguana_unspents(struct supernet_info *myinfo,struct iguana_info *coin,cJSON *array,int32_t minconf,int32_t maxconf,uint8_t *rmdarray,int32_t numrmds,int32_t lastheight,uint64_t *unspents,int32_t *numunspentsp) { - int64_t total=0,sum=0; struct iguana_pkhash *P; uint8_t *addrtypes,*pubkeys; int32_t i,flag = 0; char coinaddr[64]; + int64_t total=0,sum=0; struct iguana_pkhash *P; uint8_t *addrtypes,*pubkeys; int32_t i,numunspents,maxunspents,flag = 0; char coinaddr[64]; if ( 0 && coin->RTramchain_busy != 0 ) { printf("iguana_pkhasharray: unexpected access when RTramchain_busy\n"); return(sum); } + numunspents = 0; + maxunspents = *numunspentsp; if ( rmdarray == 0 ) rmdarray = iguana_walletrmds(myinfo,coin,&numrmds), flag++; if ( numrmds > 0 && rmdarray != 0 ) @@ -291,13 +291,17 @@ int64_t iguana_unspents(struct supernet_info *myinfo,struct iguana_info *coin,cJ for (i=0; ibundlescount,&rmdarray[i * 20],coinaddr,&pubkeys[33*i],lastheight,unspents,numunspentsp); - printf("i.%d of %d: %s %.8f\n",i,numrmds,coinaddr,dstr(total)); + *numunspentsp = 0; + iguana_pkhasharray(myinfo,coin,array,minconf,maxconf,&total,P,coin->bundlescount,&rmdarray[i * 20],coinaddr,&pubkeys[33*i],lastheight,&unspents[numunspents],numunspentsp,maxunspents); + printf("iguana_unspents: i.%d of %d: %s %.8f numunspents.%d\n",i,numrmds,coinaddr,dstr(total),*numunspentsp); + maxunspents -= *numunspentsp; + numunspents += *numunspentsp; sum += total; } printf("sum %.8f\n",dstr(sum)); free(P); } + *numunspentsp = numunspents; if ( flag != 0 && rmdarray != 0 ) free(rmdarray); return(sum); @@ -329,7 +333,7 @@ uint8_t *iguana_rmdarray(struct iguana_info *coin,int32_t *numrmdsp,cJSON *array int64_t iguana_unspentset(struct supernet_info *myinfo,struct iguana_info *coin) { - int64_t sum = 0,total; struct iguana_waccount *wacct,*tmp; struct iguana_waddress *waddr,*tmp2; int32_t numunspents; + int64_t sum = 0,total; struct iguana_waccount *wacct,*tmp; struct iguana_waddress *waddr,*tmp2; int32_t n,numunspents = 0; HASH_ITER(hh,myinfo->wallet,wacct,tmp) { HASH_ITER(hh,wacct->waddr,waddr,tmp2) @@ -337,19 +341,20 @@ int64_t iguana_unspentset(struct supernet_info *myinfo,struct iguana_info *coin) if ( waddr->addrtype != coin->chain->pubtype || (bits256_nonz(waddr->privkey) == 0 && waddr->scriptlen == 0) ) continue; total = 0; - numunspents = (int32_t)(sizeof(coin->blockspace)/sizeof(*waddr->unspents)); - iguana_pkhasharray(myinfo,coin,0,coin->minconfirms,coin->longestchain,&total,0,coin->bundlescount,waddr->rmd160,waddr->coinaddr,waddr->pubkey,coin->blocks.hwmchain.height - coin->minconfirms,(uint64_t *)coin->blockspace,&numunspents); - if ( numunspents > 0 ) + n = 0; + iguana_pkhasharray(myinfo,coin,0,coin->minconfirms,coin->longestchain,&total,0,coin->bundlescount,waddr->rmd160,waddr->coinaddr,waddr->pubkey,coin->blocks.hwmchain.height - coin->minconfirms,(uint64_t *)coin->blockspace,&n,(int32_t)(sizeof(coin->blockspace)/sizeof(*waddr->unspents))); + if ( n > 0 ) { - if ( waddr->unspents == 0 || waddr->maxunspents < numunspents ) + if ( waddr->unspents == 0 || waddr->maxunspents < n ) { - waddr->unspents = realloc(waddr->unspents,sizeof(*waddr->unspents) * numunspents); - waddr->maxunspents = numunspents; + waddr->unspents = realloc(waddr->unspents,sizeof(*waddr->unspents) * n); + waddr->maxunspents = n; } - memcpy(waddr->unspents,coin->blockspace,sizeof(*waddr->unspents) * numunspents); - waddr->numunspents = numunspents; + memcpy(waddr->unspents,coin->blockspace,sizeof(*waddr->unspents) * n); + waddr->numunspents = n; waddr->balance = total; sum += total; + numunspents += n; } } } diff --git a/iguana/iguana_wallet.c b/iguana/iguana_wallet.c index ae44b4055..6d5fc1f81 100755 --- a/iguana/iguana_wallet.c +++ b/iguana/iguana_wallet.c @@ -636,7 +636,10 @@ cJSON *iguana_walletiterate(struct supernet_info *myinfo,struct iguana_info *coi if ( flag < -1 ) { HASH_DELETE(hh,wacct->waddr,waddr); - free(waddr); + if ( waddr->unspents != 0 ) + free(waddr->unspents); + printf("%p free %s\n",waddr,waddr->coinaddr); + myfree(waddr,sizeof(*waddr) + waddr->scriptlen); } } else @@ -656,7 +659,7 @@ cJSON *iguana_walletiterate(struct supernet_info *myinfo,struct iguana_info *coi if ( flag < -1 ) { HASH_DELETE(hh,myinfo->wallet,wacct); - free(wacct); + myfree(wacct,sizeof(*wacct)); } } if ( goodp != 0 ) diff --git a/iguana/ramchain_api.c b/iguana/ramchain_api.c index 753cb8c30..5f851a69e 100755 --- a/iguana/ramchain_api.c +++ b/iguana/ramchain_api.c @@ -53,7 +53,7 @@ TWO_STRINGS_AND_TWO_DOUBLES(iguana,balance,activecoin,address,lastheightd,mincon printf("Start %s balance.(%s) height.%d\n",coin->symbol,address,lastheight); if ( lastheight == 0 ) lastheight = IGUANA_MAXHEIGHT; - iguana_pkhasharray(myinfo,coin,array,minconf,maxconf,&total,P,coin->bundlescount,rmd160,address,pubkey33,lastheight,0,0); + iguana_pkhasharray(myinfo,coin,array,minconf,maxconf,&total,P,coin->bundlescount,rmd160,address,pubkey33,lastheight,0,0,0); free(P); jadd(retjson,"unspents",array); jaddnum(retjson,"balance",dstr(total));