You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

509 lines
23 KiB

/******************************************************************************
* Copyright © 2014-2016 The SuperNET Developers. *
* *
* See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at *
* the top-level directory of this distribution for the individual copyright *
* holder information and the developer policies on copyright and licensing. *
* *
* Unless otherwise agreed in a custom licensing agreement, no part of the *
* SuperNET software, including this file may be copied, modified, propagated *
* or distributed except according to the terms contained in the LICENSE file *
* *
* Removal or modification of this copyright notice is prohibited. *
* *
******************************************************************************/
9 years ago
9 years ago
//#define uthash_malloc(size) iguana_memalloc(&coin->RThashmem,size,1)
//#define uthash_free(ptr,size)
9 years ago
#include "iguana777.h"
#include "exchanges/bitcoin.h"
9 years ago
int32_t iguana_unspentindfind(struct iguana_info *coin,char *coinaddr,uint8_t *spendscript,int32_t *spendlenp,uint64_t *valuep,int32_t *heightp,bits256 txid,int32_t vout,int32_t lasthdrsi,int32_t mempool)
9 years ago
{
9 years ago
struct iguana_txid *tp,TX; struct gecko_memtx *memtx; struct iguana_pkhash *P; struct iguana_unspent *U; struct iguana_bundle *bp; struct iguana_ramchaindata *rdata; int64_t RTspend; int64_t value; int32_t pkind,hdrsi,firstvout,spentheight,flag=0,unspentind = -1;
9 years ago
if ( valuep != 0 )
*valuep = 0;
if ( coinaddr != 0 )
coinaddr[0] = 0;
if ( coin->fastfind != 0 && (firstvout= iguana_txidfastfind(coin,heightp,txid,lasthdrsi)) >= 0 )
unspentind = (firstvout + vout);
else if ( (tp= iguana_txidfind(coin,heightp,&TX,txid,lasthdrsi)) != 0 )
unspentind = (tp->firstvout + vout);
if ( coinaddr != 0 && unspentind > 0 && (hdrsi= *heightp/coin->chain->bundlesize) >= 0 && hdrsi < coin->bundlescount && (bp= coin->bundles[hdrsi]) != 0 && (rdata= bp->ramchain.H.data) != 0 && unspentind < rdata->numunspents )
{
9 years ago
U = RAMCHAIN_PTR(rdata,Uoffset);
P = RAMCHAIN_PTR(rdata,Poffset);
9 years ago
pkind = U[unspentind].pkind;
if ( pkind > 0 && pkind < rdata->numpkinds )
{
RTspend = 0;
9 years ago
flag++;
9 years ago
if ( iguana_spentflag(coin,&RTspend,&spentheight,&bp->ramchain,bp->hdrsi,unspentind,0,1,coin->longestchain,U[unspentind].value) == 0 ) //bp == coin->current ? &coin->RTramchain :
9 years ago
{
if ( valuep != 0 )
*valuep = U[unspentind].value;
bitcoin_address(coinaddr,iguana_addrtype(coin,U[unspentind].type),P[pkind].rmd160,sizeof(P[pkind].rmd160));
if ( spendscript != 0 && spendlenp != 0 )
*spendlenp = iguana_voutscript(coin,bp,spendscript,0,&U[unspentind],&P[pkind],1);
}
}
}
9 years ago
if ( flag == 0 && mempool != 0 )
{
if ( (memtx= gecko_unspentfind(0,coin,txid)) != 0 && vout < memtx->numoutputs )
{
9 years ago
memcpy(&value,gecko_valueptr(memtx,vout),sizeof(value));
if ( value > 0 )
9 years ago
{
9 years ago
*valuep = value;
9 years ago
if ( spendlenp != 0 )
{
*spendlenp = 1;
spendscript = 0;
printf("mempool unspentind doesnt support scriptlenp yet\n");
}
}
}
}
9 years ago
return(unspentind);
}
9 years ago
char *iguana_inputaddress(struct iguana_info *coin,char *coinaddr,int16_t *spent_hdrsip,uint32_t *unspentindp,cJSON *vinobj)
9 years ago
{
bits256 txid; int32_t vout,checkind,height;
9 years ago
*unspentindp = 0;
*spent_hdrsip = -1;
9 years ago
if ( jobj(vinobj,"txid") != 0 && jobj(vinobj,"vout") != 0 )
{
txid = jbits256(vinobj,"txid");
vout = jint(vinobj,"vout");
9 years ago
if ( (checkind= iguana_unspentindfind(coin,coinaddr,0,0,0,&height,txid,vout,coin->bundlescount-1,0)) > 0 )
9 years ago
{
*spent_hdrsip = (height / coin->chain->bundlesize);
*unspentindp = checkind;
9 years ago
return(coinaddr);
9 years ago
}
9 years ago
else
{
char str[65];
printf("error finding (%s/%d)\n",bits256_str(str,txid),vout);
}
9 years ago
}
return(0);
}
9 years ago
cJSON *ramchain_unspentjson(struct iguana_unspent *up,uint32_t unspentind)
9 years ago
{
9 years ago
cJSON *item = cJSON_CreateObject();
jaddnum(item,"hdrsi",up->hdrsi);
jaddnum(item,"pkind",up->pkind);
jaddnum(item,"unspentind",unspentind);
jaddnum(item,"prevunspentind",up->prevunspentind);
jadd64bits(item,"satoshis",up->value);
jaddnum(item,"txidind",up->txidind);
jaddnum(item,"vout",up->vout);
jaddnum(item,"type",up->type);
jaddnum(item,"fileid",up->fileid);
jaddnum(item,"scriptpos",up->scriptpos);
jaddnum(item,"scriptlen",up->scriptlen);
return(item);
9 years ago
}
9 years ago
cJSON *iguana_unspentjson(struct supernet_info *myinfo,struct iguana_info *coin,int32_t hdrsi,uint32_t unspentind,struct iguana_txid *T,struct iguana_unspent *up,uint8_t rmd160[20],char *coinaddr,uint8_t *pubkey33)
9 years ago
{
9 years ago
/*{
"txid" : "d54994ece1d11b19785c7248868696250ab195605b469632b7bd68130e880c9a",
"vout" : 1,
"address" : "mgnucj8nYqdrPFh2JfZSB1NmUThUGnmsqe",
"account" : "test label",
"scriptPubKey" : "76a9140dfc8bafc8419853b34d5e072ad37d1a5159f58488ac",
"amount" : 0.00010000,
"confirmations" : 6210,
"spendable" : true
},*/
//struct iguana_unspent { uint64_t value; uint32_t txidind,pkind,prevunspentind; uint16_t hdrsi:12,type:4,vout; } __attribute__((packed));
struct iguana_waccount *wacct; struct iguana_waddress *waddr; int32_t height; char scriptstr[8192],asmstr[sizeof(scriptstr)+1024]; cJSON *item; uint32_t checkind;
item = cJSON_CreateObject();
jaddbits256(item,"txid",T[up->txidind].txid);
jaddnum(item,"vout",up->vout);
jaddstr(item,"address",coinaddr);
if ( iguana_scriptget(coin,scriptstr,asmstr,sizeof(scriptstr),hdrsi,unspentind,T[up->txidind].txid,up->vout,rmd160,up->type,pubkey33) != 0 )
jaddstr(item,"scriptPubKey",scriptstr);
jaddnum(item,"amount",dstr(up->value));
9 years ago
if ( (checkind= iguana_unspentindfind(coin,0,0,0,0,&height,T[up->txidind].txid,up->vout,coin->bundlescount-1,0)) != 0 )
9 years ago
{
9 years ago
jaddnum(item,"confirmations",coin->blocks.hwmchain.height - height + 1);
jaddnum(item,"checkind",checkind);
9 years ago
}
if ( (waddr= iguana_waddresssearch(myinfo,&wacct,coinaddr)) != 0 )
9 years ago
{
9 years ago
jaddstr(item,"account",wacct->account);
jadd(item,"spendable",jtrue());
} else jadd(item,"spendable",jfalse());
jadd(item,"unspent",ramchain_unspentjson(up,unspentind));
return(item);
9 years ago
}
9 years ago
struct iguana_pkhash *iguana_pkhashfind(struct iguana_info *coin,struct iguana_ramchain **ramchainp,int64_t *depositsp,uint32_t *lastunspentindp,struct iguana_pkhash *p,uint8_t rmd160[20],int32_t firsti,int32_t endi)
9 years ago
{
9 years ago
uint8_t *PKbits; struct iguana_pkhash *P; uint32_t pkind,numpkinds,i; struct iguana_bundle *bp; struct iguana_ramchain *ramchain; struct iguana_ramchaindata *rdata; struct iguana_account *ACCTS;
*depositsp = 0;
*ramchainp = 0;
*lastunspentindp = 0;
for (i=firsti; i<coin->bundlescount&&i<=endi; i++)
9 years ago
{
9 years ago
if ( (bp= coin->bundles[i]) != 0 )
9 years ago
{
9 years ago
if ( 0 && coin->RTramchain_busy != 0 )
{
9 years ago
printf("iguana_pkhashfind: unexpected access when RTramchain_busy\n");
return(0);
}
9 years ago
ramchain = &bp->ramchain;//(bp != coin->current) ? &bp->ramchain : &coin->RTramchain;
9 years ago
if ( (rdata= ramchain->H.data) != 0 )
9 years ago
{
9 years ago
numpkinds = rdata->numpkinds;
9 years ago
PKbits = RAMCHAIN_PTR(rdata,PKoffset);
P = RAMCHAIN_PTR(rdata,Poffset);
9 years ago
if ( bp == coin->current )
ACCTS = ramchain->A;
9 years ago
else ACCTS = RAMCHAIN_PTR(rdata,Aoffset);
9 years ago
if ( (pkind= iguana_sparseaddpk(PKbits,rdata->pksparsebits,rdata->numpksparse,rmd160,P,0,ramchain)) > 0 && pkind < numpkinds )
9 years ago
{
9 years ago
*ramchainp = ramchain;
*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);
9 years ago
if ( P != 0 )
*p = P[pkind];
9 years ago
return(p);
9 years ago
}
9 years ago
else if ( pkind != 0 )
printf("[%d] not found pkind.%d vs num.%d RT.%d rdata.%p\n",i,pkind,rdata->numpkinds,bp->isRT,rdata);
} else printf("%s.[%d] error null rdata isRT.%d\n",coin->symbol,i,bp->isRT);
9 years ago
}
9 years ago
}
9 years ago
return(0);
9 years ago
}
9 years ago
int32_t iguana_uheight(struct iguana_info *coin,int32_t bundleheight,struct iguana_txid *T,int32_t numtxids,struct iguana_unspent *up)
9 years ago
{
9 years ago
if ( up->txidind > 0 && up->txidind < numtxids )
return(bundleheight + T[up->txidind].bundlei);
else return(bundleheight);
9 years ago
}
9 years ago
int32_t iguana_datachain_scan(struct supernet_info *myinfo,struct iguana_info *coin,uint8_t rmd160[20])
{
9 years ago
int64_t deposits,crypto777_payment; uint32_t lastunspentind,unspentind; int32_t i,j,num,uheight; struct iguana_bundle *bp; struct iguana_ramchain *ramchain; struct iguana_ramchaindata *rdata; struct iguana_pkhash *P,p; struct iguana_unspent *U,*u; struct iguana_txid *T,*tx;
9 years ago
for (i=num=0; i<coin->bundlescount; i++)
{
if ( (bp= coin->bundles[i]) != 0 )
{
ramchain = 0;
9 years ago
if ( iguana_pkhashfind(coin,&ramchain,&deposits,&lastunspentind,&p,rmd160,i,i) != 0 )
9 years ago
{
if ( ramchain != 0 && (rdata= ramchain->H.data) != 0 )
{
unspentind = lastunspentind;
U = RAMCHAIN_PTR(rdata,Uoffset);
T = RAMCHAIN_PTR(rdata,Toffset);
9 years ago
P = RAMCHAIN_PTR(rdata,Poffset);
9 years ago
while ( unspentind > 0 )
{
9 years ago
tx = &T[U[unspentind].txidind];
u = &U[tx->firstvout];
uheight = iguana_uheight(coin,ramchain->height,T,rdata->numtxids,u);
for (crypto777_payment=j=0; j<tx->numvouts; j++,u++)
{
//u = &U[tx->firstvout + j];
crypto777_payment = datachain_update(myinfo,0,coin,tx->timestamp,bp,P[u->pkind].rmd160,crypto777_payment,u->type,uheight,(((uint64_t)bp->hdrsi << 32) | unspentind),u->value,u->fileid,u->scriptpos,u->scriptlen,tx->txid,j);
}
num++;
unspentind = U[unspentind].prevunspentind;
9 years ago
}
}
}
}
}
return(num);
}
9 years ago
int64_t iguana_pkhashbalance(struct supernet_info *myinfo,struct iguana_info *coin,cJSON *array,int64_t *spentp,int64_t *unspents,int32_t *nump,struct iguana_ramchain *ramchain,struct iguana_pkhash *p,uint32_t lastunspentind,uint8_t rmd160[20],char *coinaddr,uint8_t *pubkey33,int32_t hdrsi,int32_t lastheight,int32_t minconf,int32_t maxconf)
9 years ago
{
9 years ago
struct iguana_unspent *U; struct iguana_utxo *U2; struct iguana_spend *S; int32_t max,uheight,spentheight; uint32_t pkind=0,unspentind; int64_t spent = 0,checkval,deposits = 0; struct iguana_txid *T; struct iguana_account *A2; struct iguana_ramchaindata *rdata = 0; int64_t RTspend = 0;
max = *nump;
*spentp = *nump = 0;
if ( 0 && coin->RTramchain_busy != 0 )
9 years ago
{
9 years ago
printf("iguana_pkhashbalance: unexpected access when RTramchain_busy\n");
return(0);
9 years ago
}
9 years ago
if ( ramchain->Uextras == 0 || (rdata= ramchain->H.data) == 0 )
9 years ago
{
9 years ago
printf("iguana_pkhashbalance: unexpected null spents.%p or rdata.%p\n",ramchain->Uextras,rdata);
return(0);
9 years ago
}
9 years ago
unspentind = lastunspentind;
9 years ago
U = RAMCHAIN_PTR(rdata,Uoffset);
T = RAMCHAIN_PTR(rdata,Toffset);
9 years ago
RTspend = 0;
if ( lastheight == 0 )
lastheight = IGUANA_MAXHEIGHT;
while ( unspentind > 0 )
9 years ago
{
9 years ago
uheight = iguana_uheight(coin,ramchain->height,T,rdata->numtxids,&U[unspentind]);
9 years ago
if ( lastheight <= 0 || uheight < lastheight )
9 years ago
{
9 years ago
deposits += U[unspentind].value;
if ( iguana_spentflag(coin,&RTspend,&spentheight,ramchain,hdrsi,unspentind,lastheight,minconf,maxconf,U[unspentind].value) == 0 )
9 years ago
{
9 years ago
if ( *nump < max && unspents != 0 )
9 years ago
{
unspents[*nump << 1] = ((uint64_t)hdrsi << 32) | unspentind;
unspents[(*nump << 1) + 1] = U[unspentind].value;
}
9 years ago
//printf("%.8f ",dstr(U[unspentind].value));
9 years ago
(*nump)++;
if ( array != 0 )
jaddi(array,iguana_unspentjson(myinfo,coin,hdrsi,unspentind,T,&U[unspentind],rmd160,coinaddr,pubkey33));
9 years ago
}
else
{
9 years ago
//printf("-%.8f ",dstr(U[unspentind].value));
9 years ago
spent += U[unspentind].value;
}
9 years ago
if ( p->pkind != U[unspentind].pkind )
printf("warning: [%d] p->pkind.%u vs U->pkind.%u for u%d\n",hdrsi,p->pkind,U[unspentind].pkind,unspentind);
9 years ago
}
9 years ago
pkind = p->pkind;
unspentind = U[unspentind].prevunspentind;
9 years ago
}
9 years ago
if ( lastheight > 0 && (A2= ramchain->A2) != 0 && (U2= ramchain->Uextras) != 0 )
9 years ago
{
9 years ago
S = RAMCHAIN_PTR(rdata,Soffset);
//S = (void *)(long)((long)rdata + rdata->Soffset);
9 years ago
unspentind = A2[pkind].lastunspentind;
checkval = 0;
while ( unspentind > 0 )
9 years ago
{
9 years ago
uheight = iguana_uheight(coin,ramchain->height,T,rdata->numtxids,&U[unspentind]);
if ( uheight < lastheight )
9 years ago
{
9 years ago
checkval += U[unspentind].value;
9 years ago
//printf("u%u %.8f spentflag.%d prev.%u fromheight.%d\n",unspentind,dstr(U[unspentind].value),U2[unspentind].spentflag,U2[unspentind].prevunspentind,U2[unspentind].fromheight);
9 years ago
}
unspentind = U2[unspentind].prevunspentind;
9 years ago
}
9 years ago
if ( llabs(spent - checkval - RTspend) > SMALLVAL )
9 years ago
printf("spend %s: [%d] deposits %.8f spent %.8f check %.8f (%.8f) vs A2[%u] %.8f\n",lastheight==IGUANA_MAXHEIGHT?"checkerr":"",hdrsi,dstr(deposits),dstr(spent),dstr(checkval)+dstr(RTspend),dstr(*spentp),pkind,dstr(A2[pkind].total));
9 years ago
}
9 years ago
(*spentp) = spent;
//printf("(%s) spent %.8f, RTspent %.8f deposits %.8f\n",coinaddr,dstr(spent),dstr(RTspend),dstr(deposits));
9 years ago
return(deposits - spent);
}
9 years ago
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,int64_t *unspents,int32_t *numunspentsp,int32_t maxunspents)
9 years ago
{
9 years ago
int32_t i,n,m,numunspents; int64_t spent,deposits,netbalance,total; uint32_t lastunspentind; struct iguana_pkhash *p,_p; struct iguana_ramchain *ramchain; struct iguana_bundle *bp;
9 years ago
if ( 0 && coin->RTramchain_busy != 0 )
9 years ago
{
9 years ago
printf("iguana_pkhasharray: unexpected access when RTramchain_busy\n");
return(-1);
9 years ago
}
9 years ago
numunspents = numunspentsp != 0 ? *numunspentsp : 0;
9 years ago
if ( lastheight == 0 )
lastheight = IGUANA_MAXHEIGHT;
if ( max > coin->bundlescount )
max = coin->bundlescount;
9 years ago
for (total=n=i=0; i<max; i++)
9 years ago
{
9 years ago
if ( (bp= coin->bundles[i]) == 0 )
continue;
9 years ago
if ( lastheight > 0 && bp->bundleheight+bp->n > lastheight )
9 years ago
break;
if ( (coin->blocks.hwmchain.height - (bp->bundleheight + bp->n - 1)) > maxconf )
continue;
if ( (coin->blocks.hwmchain.height - bp->bundleheight) < minconf )
break;
9 years ago
if ( iguana_pkhashfind(coin,&ramchain,&deposits,&lastunspentind,P != 0 ? &P[n] : &_p,rmd160,i,i) != 0 )
9 years ago
{
9 years ago
m = maxunspents >> 1;
9 years ago
p = (P == 0) ? &_p : &P[n];
9 years ago
if ( (netbalance= iguana_pkhashbalance(myinfo,coin,array,&spent,unspents != 0 ? &unspents[numunspents << 1] : 0,&m,ramchain,p,lastunspentind,rmd160,coinaddr,pubkey33,i,lastheight,minconf,maxconf)) != deposits-spent && lastheight == IGUANA_MAXHEIGHT && minconf == 1 && maxconf > coin->blocks.hwmchain.height )
9 years ago
{
9 years ago
printf("pkhash balance mismatch from m.%d check %.8f vs %.8f spent %.8f [%.8f]\n",m,dstr(netbalance),dstr(deposits),dstr(spent),dstr(deposits)-dstr(spent));
9 years ago
}
9 years ago
else
9 years ago
{
9 years ago
printf("%s pkhash balance.[%d] from m.%d check %.8f vs %.8f spent %.8f [%.8f]\n",coinaddr,i,m,dstr(netbalance),dstr(deposits),dstr(spent),dstr(deposits)-dstr(spent));
9 years ago
total += netbalance;
n++;
9 years ago
}
9 years ago
if ( maxunspents > 0 )
{
maxunspents -= m;
if ( maxunspents <= 0 )
break;
}
9 years ago
numunspents += m;
9 years ago
//printf("%d: balance %.8f, lastunspent.%u m.%d num.%d max.%d\n",i,dstr(total),lastunspentind,m,numunspents,maxunspents);
9 years ago
}
}
9 years ago
if ( numunspentsp != 0 )
*numunspentsp = numunspents;
9 years ago
//printf("numunspents.%d max.%d\n",numunspents,maxunspents);
9 years ago
*totalp += total;
return(n);
}
9 years ago
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,int64_t *unspents,int32_t *numunspentsp)
9 years ago
{
9 years ago
int64_t total=0,sum=0; struct iguana_pkhash *P; uint8_t *addrtypes,*pubkeys; int32_t i,numunspents,maxunspents,flag = 0; char coinaddr[64];
9 years ago
if ( 0 && coin->RTramchain_busy != 0 )
9 years ago
{
9 years ago
printf("iguana_pkhasharray: unexpected access when RTramchain_busy\n");
return(sum);
9 years ago
}
9 years ago
numunspents = 0;
maxunspents = *numunspentsp;
9 years ago
if ( rmdarray == 0 )
rmdarray = iguana_walletrmds(myinfo,coin,&numrmds), flag++;
if ( numrmds > 0 && rmdarray != 0 )
9 years ago
{
9 years ago
addrtypes = &rmdarray[numrmds * 20], pubkeys = &rmdarray[numrmds * 21];
P = calloc(coin->bundlescount,sizeof(*P));
for (i=0; i<numrmds; i++)
9 years ago
{
9 years ago
bitcoin_address(coinaddr,addrtypes[i],&rmdarray[i * 20],20);
9 years ago
*numunspentsp = 0;
9 years ago
iguana_pkhasharray(myinfo,coin,array,minconf,maxconf,&total,P,coin->bundlescount,&rmdarray[i * 20],coinaddr,&pubkeys[33*i],lastheight,&unspents[numunspents << 1],numunspentsp,maxunspents);
9 years ago
printf("iguana_unspents: i.%d of %d: %s %.8f numunspents.%d\n",i,numrmds,coinaddr,dstr(total),*numunspentsp);
9 years ago
maxunspents -= *numunspentsp;
numunspents += *numunspentsp;
9 years ago
sum += total;
9 years ago
}
9 years ago
//printf("sum %.8f\n",dstr(sum));
9 years ago
free(P);
9 years ago
}
9 years ago
*numunspentsp = numunspents;
9 years ago
if ( flag != 0 && rmdarray != 0 )
free(rmdarray);
return(sum);
9 years ago
}
9 years ago
9 years ago
uint8_t *iguana_rmdarray(struct supernet_info *myinfo,struct iguana_info *coin,int32_t *numrmdsp,cJSON *array,int32_t firsti)
9 years ago
{
9 years ago
int32_t i,n,flag=0,j=0; char *coinaddr,rmdstr[41]; uint8_t *addrtypes,*rmdarray = 0;
9 years ago
*numrmdsp = 0;
9 years ago
if ( array == 0 || cJSON_GetArraySize(array) == 0 )
9 years ago
array = iguana_getaddressesbyaccount(myinfo,coin,"*");
9 years ago
if ( array != 0 && (n= cJSON_GetArraySize(array)) > 0 )
9 years ago
{
9 years ago
*numrmdsp = n - firsti;
rmdarray = calloc(1,(n-firsti) * (21 + 33));
addrtypes = &rmdarray[(n-firsti) * 20];
for (i=firsti; i<n; i++)
9 years ago
{
9 years ago
if ( (coinaddr= jstr(jitem(array,i),0)) != 0 )
9 years ago
{
9 years ago
bitcoin_addr2rmd160(&addrtypes[j],&rmdarray[20 * j],coinaddr);
init_hexbytes_noT(rmdstr,&rmdarray[20 * j],20);
printf("(%s %s) ",coinaddr,rmdstr);
j++;
9 years ago
}
}
9 years ago
printf("rmdarray[%d]\n",n);
9 years ago
}
9 years ago
if ( flag != 0 )
free_json(array);
9 years ago
return(rmdarray);
9 years ago
}
9 years ago
int32_t iguana_unspentslists(struct supernet_info *myinfo,struct iguana_info *coin,int64_t *totalp,int64_t *unspents,int32_t max,int64_t required,int32_t minconf,cJSON *addresses)
9 years ago
{
9 years ago
int64_t remains,total,sum = 0; int32_t i,n,numunspents,numaddrs; uint8_t addrtype,pubkey[65],rmd160[20]; char *coinaddr;
9 years ago
*totalp = 0;
if ( (numaddrs= cJSON_GetArraySize(addresses)) == 0 )
return(0);
memset(pubkey,0,sizeof(pubkey));
9 years ago
remains = required * 1.1 + coin->txfee;
9 years ago
for (i=numunspents=0; i<numaddrs; i++)
9 years ago
{
9 years ago
if ( (coinaddr= jstri(addresses,i)) != 0 )
9 years ago
{
9 years ago
total = 0;
n = 0;
9 years ago
bitcoin_addr2rmd160(&addrtype,rmd160,coinaddr);
9 years ago
iguana_pkhasharray(myinfo,coin,0,minconf,coin->longestchain,&total,0,coin->bundlescount,rmd160,coinaddr,pubkey,coin->blocks.hwmchain.height - minconf,unspents,&n,max-1000);
if ( n > 0 )
9 years ago
{
9 years ago
sum += total;
9 years ago
unspents += (n << 1);
9 years ago
numunspents += n;
9 years ago
if ( numunspents > max )
break;
9 years ago
}
}
}
9 years ago
*totalp = sum;
return(numunspents);
9 years ago
}
9 years ago
int32_t iguana_uvaltxid(struct supernet_info *myinfo,bits256 *txidp,struct iguana_info *coin,int16_t hdrsi,uint32_t unspentind)
{
9 years ago
struct iguana_bundle *bp; struct iguana_unspent *U,*u; struct iguana_txid *T; struct iguana_ramchain *ramchain; struct iguana_ramchaindata *rdata;
9 years ago
if ( (bp= coin->bundles[hdrsi]) == 0 )
return(-1);
9 years ago
ramchain = &bp->ramchain;//(bp == coin->current) ? &coin->RTramchain : &bp->ramchain;
9 years ago
if ( (rdata= ramchain->H.data) != 0 )
9 years ago
{
9 years ago
U = RAMCHAIN_PTR(rdata,Uoffset);
T = RAMCHAIN_PTR(rdata,Toffset);
if ( unspentind > 0 && unspentind < rdata->numunspents )
9 years ago
{
9 years ago
u = &U[unspentind];
if ( u->txidind > 0 && u->txidind < rdata->numtxids )
{
*txidp = T[u->txidind].txid;
return(unspentind - T[u->txidind].firstvout);
}
9 years ago
}
}
return(-1);
}
9 years ago
int64_t iguana_unspentavail(struct iguana_info *coin,uint64_t hdrsi_unspentind,int32_t minconf,int32_t maxconf)
{
9 years ago
struct iguana_ramchain *ramchain; struct iguana_bundle *bp; int64_t RTspend; int32_t hdrsi,spentheight,spentflag; struct iguana_unspent *U,*u; uint32_t unspentind; struct iguana_ramchaindata *rdata;
9 years ago
if ( (bp= coin->bundles[hdrsi_unspentind>>32]) == 0 )
return(-1);
hdrsi = (int16_t)(hdrsi_unspentind >> 32);
unspentind = (uint32_t)hdrsi_unspentind;
9 years ago
ramchain = &bp->ramchain;//(bp == coin->current) ? &coin->RTramchain : &bp->ramchain;
9 years ago
if ( (rdata= ramchain->H.data) == 0 )
return(0);
9 years ago
if ( (spentflag= iguana_spentflag(coin,&RTspend,&spentheight,ramchain,hdrsi,unspentind,0,minconf,maxconf,0)) > 0 )
{
printf("[%d].u%d was already spent ht.%d\n",hdrsi,(uint32_t)unspentind,spentheight);
return(-1);
}
else if ( spentflag == 0 )
{
9 years ago
U = RAMCHAIN_PTR(rdata,Uoffset);
if ( unspentind > 0 && unspentind < rdata->numunspents )
9 years ago
{
u = &U[unspentind];
return(u->value);
}
else
{
9 years ago
printf("illegal unspentind.%u vs %u [%d]\n",unspentind,rdata->numunspents,bp->hdrsi);
9 years ago
return(-2);
}
}
else return(0);
}