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.

981 lines
41 KiB

9 years ago
/******************************************************************************
* 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. *
* *
******************************************************************************/
#include "../iguana/iguana777.h"
9 years ago
typedef char *basilisk_servicefunc(struct supernet_info *myinfo,char *CMD,void *addr,char *remoteaddr,uint32_t basilisktag,cJSON *valsobj,uint8_t *data,int32_t datalen,bits256 hash,int32_t from_basilisk);
9 years ago
8 years ago
int32_t basilisk_specialcmd(char *cmd)
{
8 years ago
//&& strcmp(cmd,"DEX") != 0 && strcmp(cmd,"ACC") != 0 && strcmp(cmd,"RID") != 0 &&
if ( strcmp(cmd,"PIN") != 0 && strcmp(cmd,"OUT") != 0 && strcmp(cmd,"MSG") != 0 )
8 years ago
return(0);
8 years ago
else return(1);
}
8 years ago
/*int32_t basilisk_specialrelay_CMD(char *CMD)
8 years ago
{
if ( strcmp(CMD,"OUT") == 0 || strcmp(CMD,"MSG") == 0 || strcmp(CMD,"BLK") == 0 || strcmp(CMD,"MEM") == 0 || strcmp(CMD,"GTX") == 0 || strcmp(CMD,"RID") == 0 )
return(1);
else return(0);
8 years ago
}*/
8 years ago
9 years ago
uint32_t basilisk_calcnonce(struct supernet_info *myinfo,uint8_t *data,int32_t datalen,uint32_t nBits)
{
int32_t i,numiters = 0; bits256 hash,hash2,threshold; uint32_t basilisktag;
vcalc_sha256(0,hash.bytes,data,datalen);
9 years ago
if ( nBits >= GECKO_EASIESTDIFF )
threshold = bits256_from_compact(GECKO_EASIESTDIFF);
else threshold = bits256_from_compact(nBits);
9 years ago
for (i=0; i<numiters; i++)
{
//OS_randombytes((void *)hash.uints,sizeof(basilisktag));
hash.uints[0] = rand();
vcalc_sha256(0,hash2.bytes,hash.bytes,sizeof(hash));
if ( bits256_cmp(threshold,hash2) > 0 )
break;
}
iguana_rwnum(0,(void *)hash.uints,sizeof(basilisktag),&basilisktag);
8 years ago
iguana_rwnum(1,&data[-(int32_t)sizeof(basilisktag)],sizeof(basilisktag),&basilisktag);
8 years ago
//char str[65],str2[65]; printf("found hash after numiters.%d %s vs %s basilisktag.%u\n",numiters,bits256_str(str,threshold),bits256_str(str2,hash2),basilisktag);
9 years ago
return(basilisktag);
}
char *basilisk_addhexstr(char **ptrp,cJSON *valsobj,char *strbuf,int32_t strsize,uint8_t *data,int32_t datalen)
{
*ptrp = 0;
if ( data != 0 && datalen > 0 )
{
9 years ago
if ( valsobj != 0 && jobj(valsobj,"data") != 0 )
9 years ago
{
printf("basilisk_addhexstr warning: already have data object\n");
jdelete(valsobj,"data");
}
if ( (datalen<<1)+1 > strsize )
{
strbuf = calloc(1,(datalen << 1) + 1);
*ptrp = (void *)strbuf;
}
init_hexbytes_noT(strbuf,data,datalen);
9 years ago
if ( valsobj != 0 )
jaddstr(valsobj,"data",strbuf);
9 years ago
} else return(0);
return(strbuf);
}
9 years ago
uint8_t *get_dataptr(int32_t hdroffset,uint8_t **ptrp,int32_t *datalenp,uint8_t *space,int32_t spacesize,char *hexstr)
9 years ago
{
9 years ago
*ptrp = 0; uint8_t *data = 0;
9 years ago
if ( hexstr != 0 && (*datalenp= is_hexstr(hexstr,0)) > 0 )
{
*datalenp >>= 1;
9 years ago
if ( (*datalenp+hdroffset) <= spacesize )
9 years ago
{
9 years ago
memset(space,0,hdroffset);
data = &space[hdroffset];
} else *ptrp = data = calloc(1,*datalenp + hdroffset);
decode_hex(&data[hdroffset],*datalenp,hexstr);
9 years ago
}
if ( data != 0 )
9 years ago
return(&data[hdroffset]);
else return(data);
9 years ago
}
9 years ago
uint8_t *basilisk_jsondata(int32_t extraoffset,uint8_t **ptrp,uint8_t *space,int32_t spacesize,int32_t *datalenp,char *symbol,cJSON *sendjson,uint32_t basilisktag)
9 years ago
{
8 years ago
char *sendstr,*hexstr=0; uint8_t *data,hexspace[4096],*allocptr=0,*hexdata=0; int32_t datalen,hexlen=0;
9 years ago
if ( jobj(sendjson,"symbol") == 0 )
jaddstr(sendjson,"symbol",symbol);
9 years ago
if ( (hexstr= jstr(sendjson,"data")) != 0 )
9 years ago
{
9 years ago
hexdata = get_dataptr(0,&allocptr,&hexlen,hexspace,sizeof(hexspace),hexstr);
9 years ago
//printf("jsondata.%s from sendjson\n",hexstr);
9 years ago
jdelete(sendjson,"data");
9 years ago
}
*ptrp = 0;
sendstr = jprint(sendjson,0);
datalen = (int32_t)strlen(sendstr) + 1;
9 years ago
if ( (datalen + extraoffset + BASILISK_HDROFFSET + hexlen) <= spacesize )
9 years ago
data = space;
else
{
9 years ago
data = calloc(1,datalen + extraoffset + BASILISK_HDROFFSET + hexlen);
9 years ago
*ptrp = data;
}
9 years ago
data += extraoffset + BASILISK_HDROFFSET;
9 years ago
memcpy(data,sendstr,datalen);
9 years ago
//printf("jsondata.(%s) + hexlen.%d\n",sendstr,hexlen);
9 years ago
free(sendstr);
8 years ago
if ( hexlen > 0 && hexdata != 0 )
9 years ago
{
9 years ago
//int32_t i; for (i=0; i<hexlen; i++)
// printf("%02x",hexdata[i]);
//printf(" <- hexdata\n");
9 years ago
memcpy(&data[datalen],hexdata,hexlen);
9 years ago
datalen += hexlen;
}
*datalenp = datalen;
9 years ago
if ( allocptr != 0 )
free(allocptr);
9 years ago
return(data);
}
9 years ago
struct basilisk_item *basilisk_itemcreate(struct supernet_info *myinfo,char *CMD,char *symbol,uint32_t basilisktag,int32_t minresults,cJSON *vals,int32_t timeoutmillis,void *metricfunc)
9 years ago
{
struct basilisk_item *ptr;
ptr = calloc(1,sizeof(*ptr));
ptr->basilisktag = basilisktag;
if ( (ptr->numrequired= minresults) == 0 )
ptr->numrequired = 1;
9 years ago
strcpy(ptr->CMD,CMD);
9 years ago
safecopy(ptr->symbol,symbol,sizeof(ptr->symbol));
9 years ago
ptr->expiration = OS_milliseconds() + timeoutmillis;
return(ptr);
}
9 years ago
int32_t basilisk_sendcmd(struct supernet_info *myinfo,char *destipaddr,char *type,uint32_t *basilisktagp,int32_t encryptflag,int32_t delaymillis,uint8_t *data,int32_t datalen,int32_t fanout,uint32_t nBits) // data must be offset by sizeof(iguana_msghdr)+sizeof(basilisktag)
9 years ago
{
8 years ago
int32_t i,l,s,valid,val,n=0,retval = -1; char cmd[12]; struct iguana_info *coin,*tmp; struct iguana_peer *addr; bits256 hash; uint32_t *alreadysent,r,r2;
9 years ago
if ( fanout <= 0 )
8 years ago
fanout = sqrt(NUMRELAYS) + 1;
9 years ago
else if ( fanout > BASILISK_MAXFANOUT )
fanout = BASILISK_MAXFANOUT;
9 years ago
if ( type == 0 )
9 years ago
type = "BTCD";
9 years ago
if ( strlen(type) > 3 )
{
printf("basilisk_sendcmd illegal type(%s)\n",type);
return(-1);
}
if ( destipaddr != 0 )
{
if ( destipaddr[0] == 0 )
destipaddr = 0; // broadcast
9 years ago
else if ( strcmp(destipaddr,"127.0.0.1") == 0 || strcmp(destipaddr,myinfo->ipaddr) == 0 )
9 years ago
{
9 years ago
printf("return after locally basilisk_msgprocess\n");
9 years ago
hash = GENESIS_PUBKEY;
9 years ago
basilisk_msgprocess(myinfo,0,0,type,*basilisktagp,data,datalen);
9 years ago
return(0);
}
}
9 years ago
alreadysent = calloc(IGUANA_MAXPEERS * IGUANA_MAXCOINS,sizeof(*alreadysent));
8 years ago
iguana_rwnum(1,&data[-(int32_t)sizeof(*basilisktagp)],sizeof(*basilisktagp),basilisktagp);
9 years ago
if ( *basilisktagp == 0 )
{
if ( nBits != 0 )
*basilisktagp = basilisk_calcnonce(myinfo,data,datalen,nBits);
else *basilisktagp = rand();
8 years ago
iguana_rwnum(1,&data[-(int32_t)sizeof(*basilisktagp)],sizeof(*basilisktagp),basilisktagp);
9 years ago
}
9 years ago
data -= sizeof(*basilisktagp), datalen += sizeof(*basilisktagp);
9 years ago
memset(cmd,0,sizeof(cmd));
sprintf(cmd,"SuperNET%s",type);
9 years ago
//portable_mutex_lock(&myinfo->allcoins_mutex);
HASH_ITER(hh,myinfo->allcoins,coin,tmp)
9 years ago
{
if ( coin->peers == 0 )
9 years ago
continue;
8 years ago
if ( coin->FULLNODE == 0 && coin->VALIDATENODE == 0 )
9 years ago
cmd[0] = 's';
else cmd[0] = 'S';
9 years ago
r = rand() % (coin->peers->numranked+1);
9 years ago
for (l=0; l<IGUANA_MAXPEERS; l++)
9 years ago
{
9 years ago
i = (l + r) % IGUANA_MAXPEERS;
addr = &coin->peers->active[i];
8 years ago
if ( addr->supernet != 0 || addr->basilisk != 0 )
valid = 1;
else valid = 0;
if ( addr->usock >= 0 )
9 years ago
{
8 years ago
s = 0;
8 years ago
valid = (addr->supernet != 0);
8 years ago
if ( NUMRELAYS > 0 && basilisk_specialcmd(type) != 0 )
9 years ago
{
8 years ago
valid = 0;
8 years ago
OS_randombytes((void *)&r2,sizeof(r2));
if ( (r2 % NUMRELAYS) >= sqrt(NUMRELAYS) )
{
//printf("fanout.%d s.%d n.%d skip %s\n",fanout,s,n,addr->ipaddr);
continue;
}
8 years ago
for (s=0; s<NUMRELAYS; s++)
if ( addr->ipbits != myinfo->myaddr.myipbits && RELAYS[s].ipbits == addr->ipbits )
9 years ago
break;
8 years ago
if ( s == NUMRELAYS )
9 years ago
{
9 years ago
//printf("skip non-relay.(%s)\n",addr->ipaddr);
9 years ago
continue;
9 years ago
}
8 years ago
valid = 1;
8 years ago
//printf("send to other relay.(%s)\n",addr->ipaddr);
9 years ago
}
9 years ago
for (s=0; s<n; s++)
if ( alreadysent[s] == addr->ipbits )
8 years ago
{
printf("already sent to %s\n",addr->ipaddr);
9 years ago
continue;
8 years ago
}
if ( s == n && valid == 1 && (destipaddr == 0 || strcmp(addr->ipaddr,destipaddr) == 0) )
9 years ago
{
8 years ago
printf("n.%d/fanout.%d i.%d l.%d [%s].tag%u send %s.(%s) [%x] datalen.%d addr->supernet.%u basilisk.%u to (%s).%d destip.%s\n",n,fanout,i,l,cmd,*(uint32_t *)data,type,(char *)&data[4],*(int32_t *)&data[datalen-4],datalen,addr->supernet,addr->basilisk,addr->ipaddr,addr->A.port,destipaddr!=0?destipaddr:"broadcast");
9 years ago
if ( encryptflag != 0 && bits256_nonz(addr->pubkey) != 0 )
{
void *ptr; uint8_t *cipher,space[8192]; int32_t cipherlen; bits256 privkey;
cmd[6] = 'e', cmd[7] = 't';
memset(privkey.bytes,0,sizeof(privkey));
if ( (cipher= SuperNET_ciphercalc(&ptr,&cipherlen,&privkey,&addr->pubkey,data,datalen,space,sizeof(space))) != 0 )
{
8 years ago
if ( (val= iguana_queue_send(addr,delaymillis,&cipher[-(int32_t)sizeof(struct iguana_msghdr)],cmd,cipherlen)) >= cipherlen )
8 years ago
alreadysent[n++] = (uint32_t)addr->ipbits;
9 years ago
if ( ptr != 0 )
free(ptr);
}
}
else
{
cmd[6] = 'E', cmd[7] = 'T';
8 years ago
if ( (val= iguana_queue_send(addr,delaymillis,&data[-(int32_t)sizeof(struct iguana_msghdr)],cmd,datalen)) >= datalen )
9 years ago
{
alreadysent[n++] = (uint32_t)addr->ipbits;
if ( n >= IGUANA_MAXPEERS*IGUANA_MAXCOINS )
break;
}
9 years ago
}
9 years ago
if ( destipaddr != 0 || (fanout > 0 && n >= fanout) )
9 years ago
{
free(alreadysent);
9 years ago
return(val);
9 years ago
}
9 years ago
else if ( val > retval )
retval = val;
9 years ago
}
}
}
9 years ago
if ( n >= IGUANA_MAXPEERS*IGUANA_MAXCOINS )
break;
9 years ago
}
9 years ago
//portable_mutex_unlock(&myinfo->allcoins_mutex);
9 years ago
free(alreadysent);
9 years ago
return(n);
9 years ago
}
9 years ago
void basilisk_sendback(struct supernet_info *myinfo,char *origCMD,char *symbol,char *remoteaddr,uint32_t basilisktag,char *retstr)
9 years ago
{
9 years ago
uint8_t *data,space[4096],*allocptr; struct iguana_info *virt; cJSON *valsobj; int32_t datalen,encryptflag=0,delaymillis=0;
9 years ago
//printf("%s retstr.(%s) -> remote.(%s) basilisktag.%u\n",origCMD,retstr,remoteaddr,basilisktag);
9 years ago
if ( retstr != 0 && remoteaddr != 0 && remoteaddr[0] != 0 && strcmp(remoteaddr,"127.0.0.1") != 0 )
9 years ago
{
9 years ago
if ( (valsobj= cJSON_Parse(retstr)) != 0 )
{
9 years ago
jaddstr(valsobj,"origcmd",origCMD);
9 years ago
jaddstr(valsobj,"symbol",symbol);
9 years ago
if ( myinfo->ipaddr[0] != 0 )
jaddstr(valsobj,"relay",myinfo->ipaddr);
8 years ago
jaddnum(valsobj,"timestamp",(uint32_t)time(NULL));
9 years ago
if ( (virt= iguana_coinfind(symbol)) != 0 )
{
jaddnum(valsobj,"hwm",virt->blocks.hwmchain.height);
jaddbits256(valsobj,"chaintip",virt->blocks.hwmchain.RO.hash2);
}
9 years ago
data = basilisk_jsondata(sizeof(struct iguana_msghdr),&allocptr,space,sizeof(space),&datalen,symbol,valsobj,basilisktag);
9 years ago
//printf("sendback.%d -> %s\n",datalen,remoteaddr);
9 years ago
basilisk_sendcmd(myinfo,remoteaddr,"RET",&basilisktag,encryptflag,delaymillis,data,datalen,0,0);
if ( allocptr != 0 )
free(allocptr);
free_json(valsobj);
}
9 years ago
}
9 years ago
}
9 years ago
struct basilisk_item *basilisk_issueremote(struct supernet_info *myinfo,struct iguana_peer *addr,int32_t *numsentp,char *CMD,char *symbol,int32_t blockflag,cJSON *valsobj,int32_t fanout,int32_t minresults,uint32_t basilisktag,int32_t timeoutmillis,void *deprecated_dontuse,char *retstr,int32_t encryptflag,int32_t delaymillis,uint32_t nBits)
9 years ago
{
9 years ago
struct basilisk_item *pending; uint8_t *allocptr,*data,space[4096]; int32_t datalen; cJSON *retarray;
pending = basilisk_itemcreate(myinfo,CMD,symbol,basilisktag,minresults,valsobj,timeoutmillis,0);
pending->nBits = nBits;
9 years ago
*numsentp = 0;
9 years ago
if ( retstr != 0 )
9 years ago
{
9 years ago
pending->retstr = retstr;
pending->numresults = pending->numrequired;
9 years ago
}
else
{
9 years ago
valsobj = jduplicate(valsobj);
9 years ago
data = basilisk_jsondata(sizeof(struct iguana_msghdr),&allocptr,space,sizeof(space),&datalen,symbol,valsobj,basilisktag);
9 years ago
free_json(valsobj), valsobj = 0;
9 years ago
*numsentp = pending->numsent = basilisk_sendcmd(myinfo,addr != 0 ? addr->ipaddr : 0,CMD,&pending->basilisktag,encryptflag,delaymillis,data,datalen,fanout,pending->nBits);
9 years ago
if ( blockflag != 0 )
9 years ago
{
portable_mutex_lock(&myinfo->basilisk_mutex);
9 years ago
//printf("HASH_ADD.%p\n",pending);
9 years ago
HASH_ADD(hh,myinfo->basilisks.issued,basilisktag,sizeof(basilisktag),pending);
portable_mutex_unlock(&myinfo->basilisk_mutex);
//queue_enqueue("issuedQ",&myinfo->basilisks.issued,&pending->DL,0);
if ( pending->expiration <= OS_milliseconds() )
pending->expiration = OS_milliseconds() + BASILISK_TIMEOUT;
//ptr->vals = jduplicate(valsobj);
strcpy(pending->symbol,"BTCD");
strcpy(pending->CMD,CMD);
while ( OS_milliseconds() < pending->expiration )
{
9 years ago
if ( pending->numresults >= pending->numrequired )//|| (retstr= pending->retstr) != 0 )
{
8 years ago
//printf("numresults.%d vs numrequired.%d\n",pending->numresults,pending->numrequired);
9 years ago
break;
9 years ago
}
9 years ago
usleep(10000);
}
if ( (retarray= pending->retarray) != 0 )
{
pending->retstr = jprint(retarray,0);
pending->retarray = 0;
free_json(retarray);
}
//return(basilisk_waitresponse(myinfo,CMD,"BTCD",0,&Lptr,valsobj,ptr));
} else free(pending), pending = 0; //ptr->finished = (uint32_t)time(NULL);
9 years ago
if ( allocptr != 0 )
free(allocptr);
9 years ago
}
9 years ago
return(pending);
9 years ago
}
9 years ago
struct basilisk_item *basilisk_requestservice(struct supernet_info *myinfo,struct iguana_peer *addr,char *CMD,int32_t blockflag,cJSON *valsobj,bits256 hash,uint8_t *data,int32_t datalen,uint32_t nBits)
9 years ago
{
8 years ago
int32_t minfanout,numrequired,timeoutmillis,numsent,delaymillis,encryptflag,fanout; struct basilisk_item *ptr; char buf[4096],*symbol,*str = 0; struct iguana_info *virt;
9 years ago
//printf("request.(%s)\n",jprint(valsobj,0));
9 years ago
basilisk_addhexstr(&str,valsobj,buf,sizeof(buf),data,datalen);
9 years ago
if ( bits256_cmp(hash,GENESIS_PUBKEY) != 0 && bits256_nonz(hash) != 0 )
9 years ago
{
if ( jobj(valsobj,"hash") != 0 )
jdelete(valsobj,"hash");
9 years ago
jaddbits256(valsobj,"hash",hash);
9 years ago
}
9 years ago
if ( (numrequired= jint(valsobj,"numrequired")) <= 0 )
8 years ago
numrequired = sqrt(NUMRELAYS);
9 years ago
if ( (timeoutmillis= jint(valsobj,"timeout")) == 0 )
timeoutmillis = BASILISK_TIMEOUT;
8 years ago
minfanout = sqrt(NUMRELAYS)+1;
8 years ago
if ( minfanout < 5 )
minfanout = 5;
9 years ago
if ( jobj(valsobj,"fanout") == 0 )
8 years ago
fanout = minfanout;
9 years ago
else fanout = jint(valsobj,"fanout");
8 years ago
if ( fanout < minfanout )
fanout = minfanout;
9 years ago
if ( (symbol= jstr(valsobj,"coin")) != 0 || (symbol= jstr(valsobj,"symbol")) != 0 )
{
if ( (virt= iguana_coinfind(symbol)) != 0 )
{
9 years ago
jaddstr(valsobj,"symbol",symbol);
9 years ago
jaddnum(valsobj,"longest",virt->longestchain);
jaddnum(valsobj,"hwm",virt->blocks.hwmchain.height);
}
}
9 years ago
if ( symbol == 0 )
symbol = "BTCD";
9 years ago
encryptflag = jint(valsobj,"encrypt");
delaymillis = jint(valsobj,"delay");
9 years ago
ptr = basilisk_issueremote(myinfo,addr,&numsent,CMD,symbol,blockflag,valsobj,fanout,numrequired,0,timeoutmillis,0,0,encryptflag,delaymillis,nBits);
9 years ago
return(ptr);
9 years ago
}
9 years ago
char *basilisk_standardservice(char *CMD,struct supernet_info *myinfo,void *_addr,bits256 hash,cJSON *valsobj,char *hexstr,int32_t blockflag) // client side
9 years ago
{
9 years ago
uint32_t nBits = 0; uint8_t space[4096],*allocptr=0,*data = 0; struct basilisk_item *ptr; int32_t datalen = 0; cJSON *retjson; char *retstr=0;
8 years ago
if ( RELAYID >= 0 && basilisk_specialcmd(CMD) == 0 )
8 years ago
return(clonestr("{\"error\":\"unsupported special relay command\"}"));
9 years ago
data = get_dataptr(BASILISK_HDROFFSET,&allocptr,&datalen,space,sizeof(space),hexstr);
9 years ago
ptr = basilisk_requestservice(myinfo,_addr,CMD,blockflag,valsobj,hash,data,datalen,nBits);
9 years ago
if ( allocptr != 0 )
free(allocptr);
9 years ago
if ( ptr != 0 )
9 years ago
{
9 years ago
if ( ptr->retstr != 0 )
retstr = ptr->retstr, ptr->retstr = 0;
else
9 years ago
{
9 years ago
retjson = cJSON_CreateObject();
if ( ptr->numsent > 0 )
{
//queue_enqueue("submitQ",&myinfo->basilisks.submitQ,&ptr->DL,0);
jaddstr(retjson,"result","success");
jaddnum(retjson,"numsent",ptr->numsent);
} else jaddstr(retjson,"error","didnt find any nodes to send to");
retstr = jprint(retjson,1);
9 years ago
}
9 years ago
ptr->finished = (uint32_t)time(NULL);
}
8 years ago
if ( 1 && strcmp("RID",CMD) != 0 && strcmp("BAL",CMD) != 0 && strcmp("MSG",CMD) != 0 )
9 years ago
printf("%s.(%s) -> (%s)\n",CMD,jprint(valsobj,0),retstr!=0?retstr:"");
9 years ago
return(retstr);
9 years ago
}
9 years ago
int32_t basilisk_relayid(struct supernet_info *myinfo,uint32_t ipbits)
{
int32_t j;
8 years ago
for (j=0; j<NUMRELAYS; j++)
if ( RELAYS[j].ipbits == ipbits )
9 years ago
return(j);
return(-1);
}
9 years ago
#include "basilisk_bitcoin.c"
#include "basilisk_nxt.c"
#include "basilisk_ether.c"
#include "basilisk_waves.c"
#include "basilisk_lisk.c"
9 years ago
#include "basilisk_MSG.c"
9 years ago
#include "basilisk_tradebot.c"
9 years ago
#include "basilisk_swap.c"
9 years ago
#include "basilisk_DEX.c"
#include "basilisk_ping.c"
9 years ago
#include "basilisk_CMD.c"
9 years ago
9 years ago
void basilisk_functions(struct iguana_info *coin,int32_t protocol)
9 years ago
{
9 years ago
coin->protocol = protocol;
switch ( protocol )
9 years ago
{
9 years ago
/*case IGUANA_PROTOCOL_BITCOIN:
9 years ago
coin->basilisk_balances = basilisk_bitcoinbalances;
coin->basilisk_rawtx = basilisk_bitcoinrawtx;
9 years ago
//coin->basilisk_rawtxmetric = basilisk_bitcoin_rawtxmetric;
9 years ago
coin->basilisk_value = basilisk_bitcoinvalue;
9 years ago
coin->basilisk_valuemetric = basilisk_bitcoin_valuemetric;
9 years ago
break;
9 years ago
case IGUANA_PROTOCOL_IOTA:
9 years ago
coin->basilisk_balances = basilisk_iotabalances;
coin->basilisk_rawtx = basilisk_iotarawtx;
break;
case IGUANA_PROTOCOL_NXT:
coin->basilisk_balances = basilisk_nxtbalances;
coin->basilisk_rawtx = basilisk_nxtrawtx;
break;
case IGUANA_PROTOCOL_ETHER:
coin->basilisk_balances = basilisk_etherbalances;
coin->basilisk_rawtx = basilisk_etherrawtx;
break;
case IGUANA_PROTOCOL_WAVES:
coin->basilisk_balances = basilisk_wavesbalances;
coin->basilisk_rawtx = basilisk_wavesrawtx;
break;
case IGUANA_PROTOCOL_LISK:
coin->basilisk_balances = basilisk_liskbalances;
coin->basilisk_rawtx = basilisk_liskrawtx;
break;*/
}
}
9 years ago
int32_t basilisk_hashes_send(struct supernet_info *myinfo,struct iguana_info *virt,struct iguana_peer *addr,char *CMD,bits256 *hashes,int32_t num)
9 years ago
{
9 years ago
bits256 hash; uint8_t *serialized; int32_t i,len = 0; char *str=0,*retstr,*hexstr,*allocptr=0,space[4096]; bits256 txid; cJSON *vals;
9 years ago
if ( virt != 0 && addr != 0 )
{
memset(hash.bytes,0,sizeof(hash));
9 years ago
serialized = (void *)hashes;
9 years ago
for (i=0; i<num; i++)
{
9 years ago
txid = hashes[i];
9 years ago
len += iguana_rwbignum(1,&serialized[len],sizeof(txid),txid.bytes);
}
if ( (hexstr= basilisk_addhexstr(&str,0,space,sizeof(space),serialized,len)) != 0 )
{
vals = cJSON_CreateObject();
9 years ago
jaddstr(vals,"symbol",virt->symbol);
9 years ago
if ( (retstr= basilisk_standardservice(CMD,myinfo,addr,hash,vals,hexstr,0)) != 0 )
9 years ago
free(retstr);
free_json(vals);
if ( allocptr != 0 )
free(allocptr);
}
return(0);
} else return(-1);
}
9 years ago
void basilisk_geckoresult(struct supernet_info *myinfo,char *remoteaddr,char *retstr,uint8_t *data,int32_t datalen)
9 years ago
{
9 years ago
struct iguana_info *virt; char *symbol,*str,*type; cJSON *retjson; bits256 hash2;
if ( retstr != 0 && (retjson= cJSON_Parse(retstr)) != 0 )
{
9 years ago
if ( (symbol= jstr(retjson,"symbol")) != 0 && (virt= iguana_coinfind(symbol)) != 0 )
{
9 years ago
if ( data != 0 )
{
str = 0;
if ( (type= jstr(retjson,"type")) != 0 )
{
9 years ago
hash2 = jbits256(retjson,"hash");
9 years ago
if ( strcmp(type,"HDR") == 0 && virt->virtualchain != 0 )
9 years ago
str = gecko_headersarrived(myinfo,virt,remoteaddr,data,datalen,hash2);
9 years ago
else if ( strcmp(type,"MEM") == 0 && virt->virtualchain != 0 )
9 years ago
str = gecko_mempoolarrived(myinfo,virt,remoteaddr,data,datalen,hash2);
9 years ago
else if ( strcmp(type,"BLK") == 0 && virt->virtualchain != 0 )
9 years ago
str = gecko_blockarrived(myinfo,virt,remoteaddr,data,datalen,hash2,0);
9 years ago
else if ( strcmp(type,"GTX") == 0 && virt->virtualchain != 0 )
9 years ago
str = gecko_txarrived(myinfo,virt,remoteaddr,data,datalen,hash2);
}
if ( str != 0 )
free(str);
}
}
free_json(retjson);
}
}
9 years ago
void basilisk_result(struct supernet_info *myinfo,char *remoteaddr,uint32_t basilisktag,cJSON *vals,uint8_t *data,int32_t datalen)
9 years ago
{
9 years ago
char *retstr,CMD[16]; struct basilisk_item *pending; cJSON *item;
if ( vals != 0 )
9 years ago
{
9 years ago
retstr = jprint(vals,0);
safecopy(CMD,jstr(vals,"origcmd"),sizeof(CMD));
9 years ago
if ( 0 && strcmp("RID",CMD) != 0 )
9 years ago
printf("(%s) -> Q.%u results vals.(%s)\n",CMD,basilisktag,retstr);
9 years ago
if ( strcmp(CMD,"GET") == 0 )
basilisk_geckoresult(myinfo,remoteaddr,retstr,data,datalen);
else
9 years ago
{
9 years ago
portable_mutex_lock(&myinfo->basilisk_mutex);
HASH_FIND(hh,myinfo->basilisks.issued,&basilisktag,sizeof(basilisktag),pending);
9 years ago
//printf("HASH_FIND.%p\n",pending);
9 years ago
portable_mutex_unlock(&myinfo->basilisk_mutex);
if ( pending != 0 && retstr != 0 )
9 years ago
{
9 years ago
if ( (item= cJSON_Parse(retstr)) != 0 )
{
if ( pending->retarray == 0 )
pending->retarray = cJSON_CreateArray();
if ( jobj(item,"myip") == 0 )
jaddstr(item,"myip",myinfo->ipaddr);
jaddi(pending->retarray,item);
8 years ago
if ( jobj(item,"error") == 0 )
{
8 years ago
//printf("numresults.%d (%s)\n",pending->numresults,jprint(item,0));
8 years ago
pending->numresults++;
}
9 years ago
} else printf("couldnt parse.(%s)\n",retstr);
9 years ago
} //else printf("couldnt find issued.%u\n",basilisktag);
9 years ago
}
9 years ago
}
9 years ago
}
9 years ago
void basilisk_wait(struct supernet_info *myinfo,struct iguana_info *coin)
{
if ( coin != 0 )
{
while ( coin->basilisk_busy != 0 )
usleep(1000);
}
else
{
while ( myinfo->basilisk_busy != 0 )
usleep(1000);
}
}
9 years ago
void basilisk_msgprocess(struct supernet_info *myinfo,void *_addr,uint32_t senderipbits,char *type,uint32_t basilisktag,uint8_t *data,int32_t datalen)
9 years ago
{
8 years ago
cJSON *valsobj; char *symbol,*retstr=0,remoteaddr[64],CMD[4],cmd[4]; int32_t height,origlen,from_basilisk,i,timeoutmillis,flag,numrequired,jsonlen; uint8_t *origdata; struct iguana_info *coin=0; bits256 hash; struct iguana_peer *addr = _addr;
9 years ago
static basilisk_servicefunc *basilisk_services[][2] =
{
8 years ago
{ (void *)"OUT", &basilisk_respond_OUT }, // send MSG to hash/id/num
{ (void *)"MSG", &basilisk_respond_MSG }, // get MSG (hash, id, num)
{ (void *)"ADD", &basilisk_respond_addrelay }, // relays register with each other bus
//{ (void *)"PIN", &basilisk_respond_PIN },
9 years ago
// encrypted data for jumblr
9 years ago
{ (void *)"HOP", &basilisk_respond_forward }, // message forwarding
{ (void *)"BOX", &basilisk_respond_mailbox }, // create/send/check mailbox pubkey
9 years ago
// small virtual private network
9 years ago
{ (void *)"VPN", &basilisk_respond_VPNcreate }, // create virtual network's hub via privkey
{ (void *)"ARC", &basilisk_respond_VPNjoin }, // join
9 years ago
{ (void *)"GAB", &basilisk_respond_VPNmessage }, // private message
{ (void *)"SAY", &basilisk_respond_VPNbroadcast }, // broadcast
{ (void *)"EAR", &basilisk_respond_VPNreceive }, // network receive (via poll)
9 years ago
{ (void *)"END", &basilisk_respond_VPNlogout }, // logout
8 years ago
//{ (void *)"DEX", &basilisk_respond_DEX },
//{ (void *)"RID", &basilisk_respond_RID },
//{ (void *)"ACC", &basilisk_respond_ACC },
{ (void *)"BYE", &basilisk_respond_goodbye }, // disconnect
// gecko chains
{ (void *)"GET", &basilisk_respond_geckoget }, // requests headers, block or tx
{ (void *)"HDR", &basilisk_respond_geckoheaders }, // reports headers
{ (void *)"BLK", &basilisk_respond_geckoblock }, // reports virtchain block
{ (void *)"MEM", &basilisk_respond_mempool }, // reports virtchain mempool
{ (void *)"GTX", &basilisk_respond_geckotx }, // reports virtchain tx
9 years ago
// coin services
9 years ago
{ (void *)"VAL", &basilisk_respond_value },
{ (void *)"BAL", &basilisk_respond_balances },
8 years ago
{ (void *)"INF", &basilisk_respond_getinfo },
9 years ago
};
8 years ago
strncpy(CMD,type,3), CMD[3] = cmd[3] = 0;
if ( isupper((int32_t)CMD[0]) != 0 && isupper((int32_t)CMD[1]) != 0 && isupper((int32_t)CMD[2]) != 0 )
from_basilisk = 1;
else from_basilisk = 0;
origdata = data;
origlen = datalen;
for (i=0; i<3; i++)
{
CMD[i] = toupper((int32_t)CMD[i]);
cmd[i] = tolower((int32_t)CMD[i]);
}
8 years ago
if ( RELAYID >= 0 )
8 years ago
{
if ( basilisk_specialcmd(CMD) == 0 )
return;
}
9 years ago
symbol = "BTCD";
9 years ago
if ( senderipbits == 0 )
expand_ipbits(remoteaddr,myinfo->myaddr.myipbits);
else expand_ipbits(remoteaddr,senderipbits);
9 years ago
if ( (valsobj= cJSON_Parse((char *)data)) != 0 )
{
if ( jobj(valsobj,"coin") != 0 )
coin = iguana_coinfind(jstr(valsobj,"coin"));
9 years ago
else if ( jobj(valsobj,"symbol") != 0 )
coin = iguana_coinfind(jstr(valsobj,"symbol"));
if ( coin != 0 )
{
if ( (height= juint(valsobj,"hwm")) > 0 )
{
if ( height > addr->height )
addr->height = height;
if ( height > coin->longestchain )
coin->longestchain = height;
}
}
9 years ago
if ( strcmp(type,"RET") == 0 )
{
9 years ago
basilisk_result(myinfo,remoteaddr,basilisktag,valsobj,data,datalen);
9 years ago
return;
}
9 years ago
}
else
{
9 years ago
printf("unexpected binary packet datalen.%d\n",datalen);
9 years ago
return;
}
9 years ago
for (i=flag=0; i<sizeof(basilisk_services)/sizeof(*basilisk_services); i++) // iguana node
if ( strcmp((char *)basilisk_services[i][0],type) == 0 )
{
flag = 1;
break;
}
if ( flag == 0 )
9 years ago
return;
9 years ago
myinfo->basilisk_busy = 1;
9 years ago
if ( valsobj != 0 )
9 years ago
{
jsonlen = (int32_t)strlen((char *)data) + 1;
if ( datalen > jsonlen )
data += jsonlen, datalen -= jsonlen;
9 years ago
else data = 0, datalen = 0;
9 years ago
if ( coin == 0 )
coin = iguana_coinfind("BTCD");
if ( coin != 0 )
9 years ago
{
symbol = coin->symbol;
9 years ago
coin->basilisk_busy = 1;
9 years ago
}
9 years ago
hash = jbits256(valsobj,"hash");
9 years ago
timeoutmillis = jint(valsobj,"timeout");
if ( (numrequired= jint(valsobj,"numrequired")) == 0 )
8 years ago
numrequired = sqrt(NUMRELAYS);
9 years ago
if ( senderipbits != 0 )
expand_ipbits(remoteaddr,senderipbits);
else remoteaddr[0] = 0;
9 years ago
for (i=0; i<sizeof(basilisk_services)/sizeof(*basilisk_services); i++) // iguana node
9 years ago
{
if ( strcmp((char *)basilisk_services[i][0],type) == 0 )
{
8 years ago
if ( coin->FULLNODE != 0 || RELAYID >= 0 ) // iguana node
9 years ago
{
8 years ago
printf("services %s\n",type);
9 years ago
if ( (retstr= (*basilisk_services[i][1])(myinfo,type,addr,remoteaddr,basilisktag,valsobj,data,datalen,hash,from_basilisk)) != 0 )
9 years ago
{
9 years ago
//printf("from_basilisk.%d ret.(%s)\n",from_basilisk,retstr);
9 years ago
//if ( from_basilisk != 0 || strcmp(CMD,"GET") == 0 )
9 years ago
basilisk_sendback(myinfo,CMD,symbol,remoteaddr,basilisktag,retstr);
9 years ago
if ( retstr != 0 )
free(retstr);
9 years ago
break;
9 years ago
} else printf("services null return\n");
8 years ago
} else printf("non-relay got unhandled.(%s)\n",type);
9 years ago
}
}
9 years ago
free_json(valsobj);
9 years ago
}
9 years ago
if ( coin != 0 )
coin->basilisk_busy = 0;
myinfo->basilisk_busy = 0;
9 years ago
}
9 years ago
void basilisk_p2p(void *_myinfo,void *_addr,char *senderip,uint8_t *data,int32_t datalen,char *type,int32_t encrypted)
{
9 years ago
uint32_t ipbits,basilisktag; int32_t msglen,len=0; void *ptr = 0; uint8_t space[4096]; bits256 senderpub; struct supernet_info *myinfo = _myinfo;
9 years ago
if ( encrypted != 0 )
{
printf("encrypted p2p\n");
memset(senderpub.bytes,0,sizeof(senderpub));
if ( (data= SuperNET_deciphercalc(&ptr,&msglen,myinfo->privkey,senderpub,data,datalen,space,sizeof(space))) == 0 )
{
printf("basilisk_p2p decrytion error\n");
return;
} else datalen = msglen;
}
if ( senderip != 0 && senderip[0] != 0 && strcmp(senderip,"127.0.0.1") != 0 )
ipbits = (uint32_t)calc_ipbits(senderip);
else ipbits = myinfo->myaddr.myipbits;
if ( type[0] == 'P' && type[1] == 'I' && type[2] == 'N' )
{
8 years ago
if ( strcmp(type,"PIN") == 0 && RELAYID >= 0 )
9 years ago
{
9 years ago
basilisk_ping_process(myinfo,_addr,ipbits,data,datalen);
9 years ago
}
}
else
{
len += iguana_rwnum(0,data,sizeof(basilisktag),&basilisktag);
9 years ago
//int32_t i; for (i=0; i<datalen-len; i++)
// printf("%02x",data[len+i]);
8 years ago
if ( 0 && RELAYID >= 0 )
8 years ago
printf(" ->received.%d basilisk_p2p.(%s) from %s tag.%d\n",datalen,type,senderip!=0?senderip:"?",basilisktag);
9 years ago
basilisk_msgprocess(myinfo,_addr,ipbits,type,basilisktag,&data[len],datalen - len);
}
if ( ptr != 0 )
free(ptr);
}
9 years ago
void basilisk_requests_poll(struct supernet_info *myinfo)
{
char *retstr; cJSON *outerarray; int32_t i,n; struct basilisk_request issueR; double hwm = 0.;
memset(&issueR,0,sizeof(issueR));
if ( (retstr= InstantDEX_incoming(myinfo,0,0,0,0)) != 0 )
{
8 years ago
//printf("poll.(%s)\n",retstr);
9 years ago
if ( (outerarray= cJSON_Parse(retstr)) != 0 )
{
if ( is_cJSON_Array(outerarray) != 0 )
{
n = cJSON_GetArraySize(outerarray);
for (i=0; i<n; i++)
9 years ago
hwm = basilisk_process_results(myinfo,&issueR,jitem(outerarray,i),hwm);
} else hwm = basilisk_process_results(myinfo,&issueR,outerarray,hwm);
9 years ago
free_json(outerarray);
}
free(retstr);
8 years ago
} else printf("null incoming\n");
9 years ago
if ( hwm > 0. )
{
if ( bits256_cmp(myinfo->myaddr.persistent,issueR.hash) == 0 ) // my request
{
9 years ago
printf("my req hwm %f\n",hwm);
9 years ago
if ( (retstr= InstantDEX_accept(myinfo,0,0,0,issueR.requestid,issueR.quoteid)) != 0 )
free(retstr);
9 years ago
if ( (retstr= basilisk_start(myinfo,&issueR,1)) != 0 )
free(retstr);
9 years ago
}
else //if ( issueR.quoteid == 0 )
{
9 years ago
printf("other req hwm %f\n",hwm);
9 years ago
issueR.quoteid = basilisk_quoteid(&issueR);
issueR.desthash = myinfo->myaddr.persistent;
if ( (retstr= basilisk_start(myinfo,&issueR,0)) != 0 )
free(retstr);
} //else printf("basilisk_requests_poll unexpected hwm issueR\n");
}
}
9 years ago
void basilisks_loop(void *arg)
{
8 years ago
struct iguana_info *virt,*tmpcoin,*coin,*btcd; struct basilisk_message *msg,*tmpmsg; struct basilisk_item *tmp,*pending; uint32_t now; int32_t iter,maxmillis,flag=0; struct supernet_info *myinfo = arg;
9 years ago
iter = 0;
while ( 1 )
{
portable_mutex_lock(&myinfo->basilisk_mutex);
HASH_ITER(hh,myinfo->basilisks.issued,pending,tmp)
{
9 years ago
if ( pending != 0 && (pending->finished != 0 || OS_milliseconds() > pending->expiration+600000) )
9 years ago
{
9 years ago
//printf("enable free for HASH_DELETE.(%p)\n",pending);
9 years ago
HASH_DELETE(hh,myinfo->basilisks.issued,pending);
9 years ago
memset(pending,0,sizeof(*pending));
free(pending);
9 years ago
}
}
portable_mutex_unlock(&myinfo->basilisk_mutex);
if ( (btcd= iguana_coinfind("BTCD")) != 0 )
{
9 years ago
maxmillis = (1000 / (myinfo->allcoins_numvirts + 1)) + 1;
9 years ago
//portable_mutex_lock(&myinfo->allcoins_mutex);
HASH_ITER(hh,myinfo->allcoins,virt,tmpcoin)
{
if ( virt->started != 0 && virt->active != 0 && virt->virtualchain != 0 )
{
gecko_iteration(myinfo,btcd,virt,maxmillis), flag++;
}
}
//portable_mutex_unlock(&myinfo->allcoins_mutex);
8 years ago
if ( RELAYID >= 0 )
8 years ago
{
9 years ago
basilisk_ping_send(myinfo,btcd);
8 years ago
}
9 years ago
}
9 years ago
HASH_ITER(hh,myinfo->allcoins,coin,tmpcoin)
{
8 years ago
if ( time(NULL) > coin->lastunspentsupdate+10 )
9 years ago
{
8 years ago
//printf(">>>>>>>>>>>>> update %s\n",coin->symbol);
8 years ago
basilisk_unspents_update(myinfo,coin);
coin->lastunspentsupdate = (uint32_t)time(NULL);
8 years ago
//printf(">>>>>>>>>>>>> update %s finished\n",coin->symbol);
9 years ago
}
}
8 years ago
if ( RELAYID < 0 && myinfo->expiration != 0 )
9 years ago
basilisk_requests_poll(myinfo);
9 years ago
now = (uint32_t)time(NULL);
portable_mutex_lock(&myinfo->messagemutex);
HASH_ITER(hh,myinfo->messagetable,msg,tmpmsg)
{
if ( now > msg->expiration )
{
8 years ago
printf("delete expired message.%p QUEUEITEMS.%d\n",msg,QUEUEITEMS);
9 years ago
HASH_DELETE(hh,myinfo->messagetable,msg);
8 years ago
QUEUEITEMS--;
9 years ago
free(msg);
}
}
portable_mutex_unlock(&myinfo->messagemutex);
8 years ago
if ( RELAYID >= 0 )
8 years ago
usleep(100000);
else usleep(3000000);
9 years ago
}
}
9 years ago
9 years ago
void basilisks_init(struct supernet_info *myinfo)
{
9 years ago
iguana_initQ(&myinfo->msgQ,"messageQ");
9 years ago
portable_mutex_init(&myinfo->bu_mutex);
9 years ago
portable_mutex_init(&myinfo->allcoins_mutex);
portable_mutex_init(&myinfo->basilisk_mutex);
9 years ago
portable_mutex_init(&myinfo->DEX_mutex);
9 years ago
portable_mutex_init(&myinfo->DEX_swapmutex);
9 years ago
portable_mutex_init(&myinfo->DEX_reqmutex);
9 years ago
portable_mutex_init(&myinfo->gecko_mutex);
9 years ago
portable_mutex_init(&myinfo->messagemutex);
9 years ago
myinfo->basilisks.launched = iguana_launch(iguana_coinfind("BTCD"),"basilisks_loop",basilisks_loop,myinfo,IGUANA_PERMTHREAD);
8 years ago
printf("Basilisk initialized\n");
9 years ago
}
8 years ago
#include "../includes/iguana_apidefs.h"
#include "../includes/iguana_apideclares.h"
8 years ago
8 years ago
HASH_ARRAY_STRING(basilisk,balances,hash,vals,hexstr)
{
char *retstr=0,*symbol; uint32_t basilisktag; struct basilisk_item *ptr,Lptr; int32_t timeoutmillis;
8 years ago
if ( RELAYID >= 0 )
8 years ago
return(clonestr("{\"error\":\"special relays only do OUT and MSG\"}"));
8 years ago
if ( vals == 0 )
return(clonestr("{\"error\":\"need vals object\"}"));
//if ( coin == 0 )
{
if ( (symbol= jstr(vals,"symbol")) != 0 || (symbol= jstr(vals,"coin")) != 0 )
coin = iguana_coinfind(symbol);
}
if ( jobj(vals,"fanout") == 0 )
8 years ago
jaddnum(vals,"fanout",MAX(5,(int32_t)sqrt(NUMRELAYS)+1));
8 years ago
if ( jobj(vals,"numrequired") == 0 )
8 years ago
jaddnum(vals,"numrequired",sqrt(NUMRELAYS));
8 years ago
if ( coin != 0 )
{
if ( jobj(vals,"addresses") == 0 )
{
jadd(vals,"addresses",iguana_getaddressesbyaccount(myinfo,coin,"*"));
8 years ago
//printf("added all %s addresses: %s\n",coin->symbol,jprint(vals,0));
8 years ago
} //else printf("have addresses.(%s)\n",jprint(jobj(vals,"addresses"),0));
if ( (basilisktag= juint(vals,"basilisktag")) == 0 )
basilisktag = rand();
if ( (timeoutmillis= juint(vals,"timeout")) <= 0 )
timeoutmillis = BASILISK_TIMEOUT;
8 years ago
if ( (coin->FULLNODE != 0 || coin->VALIDATENODE != 0) && (ptr= basilisk_bitcoinbalances(&Lptr,myinfo,coin,remoteaddr,basilisktag,timeoutmillis,vals)) != 0 )
8 years ago
{
retstr = ptr->retstr, ptr->retstr = 0;
ptr->finished = (uint32_t)time(NULL);
return(retstr);
}
8 years ago
return(clonestr("{\"error\":\"no result\"}"));
8 years ago
} else printf("no coin\n");
return(basilisk_standardservice("BAL",myinfo,0,hash,vals,hexstr,1));
}
HASH_ARRAY_STRING(basilisk,history,hash,vals,hexstr)
{
8 years ago
int64_t total = 0; int32_t i,n; struct iguana_waccount *wacct,*tmp; struct iguana_waddress *waddr,*tmp2; char *symbol; cJSON *retjson,*unspents,*spends,*array; //struct basilisk_spend *s; struct basilisk_unspent *bu; int32_t i;
8 years ago
if ( vals == 0 )
return(clonestr("{\"error\":\"need vals object\"}"));
//if ( coin == 0 )
{
if ( (symbol= jstr(vals,"symbol")) != 0 || (symbol= jstr(vals,"coin")) != 0 )
coin = iguana_coinfind(symbol);
}
if ( coin == 0 )
return(clonestr("{\"error\":\"couldnt find coin\"}"));
8 years ago
unspents = cJSON_CreateArray();
spends = cJSON_CreateArray();
portable_mutex_lock(&myinfo->bu_mutex);
8 years ago
HASH_ITER(hh,myinfo->wallet,wacct,tmp)
{
HASH_ITER(hh,wacct->waddr,waddr,tmp2)
{
if ( waddr->Cunspents != 0 )
8 years ago
{
//printf("Cunspents.(%s)\n",jprint(waddr->Cunspents,0));
if ( (array= jobj(waddr->Cunspents,coin->symbol)) != 0 )
8 years ago
{
if ( (n= cJSON_GetArraySize(array)) > 0 )
{
for (i=0; i<n; i++)
total += jdouble(jitem(array,i),"amount") * SATOSHIDEN;
}
jaddi(unspents,jduplicate(array));
8 years ago
}
}
if ( waddr->Cspends != 0 )
{
//printf("Cspends.(%s)\n",jprint(waddr->Cspends,0));
if ( (array= jobj(waddr->Cspends,coin->symbol)) != 0 )
jaddi(spends,jduplicate(array));
}
8 years ago
}
}
portable_mutex_unlock(&myinfo->bu_mutex);
8 years ago
retjson = cJSON_CreateObject();
jaddstr(retjson,"result","success");
8 years ago
jadd(retjson,"unspents",unspents);
jadd(retjson,"spends",spends);
8 years ago
jaddstr(retjson,"coin",coin->symbol);
jaddnum(retjson,"balance",dstr(total));
8 years ago
//printf("return history balance %s %.8f\n",coin->symbol,dstr(total));
8 years ago
return(jprint(retjson,1));
}
#include "../includes/iguana_apiundefs.h"