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.

349 lines
13 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. *
* *
******************************************************************************/
// included from basilisk.c
9 years ago
8 years ago
/*int32_t basilisk_relayid(struct supernet_info *myinfo,char *ipaddr)
{
uint32_t i,ipbits = (uint32_t)calc_ipbits(ipaddr);
8 years ago
for (i=0; i<NUMRELAYS; i++)
if ( ipbits == RELAYS[i].ipbits )
8 years ago
return(i);
return(-1);
}*/
9 years ago
struct iguana_peer *basilisk_ensurerelay(struct supernet_info *myinfo,struct iguana_info *btcd,uint32_t ipbits)
9 years ago
{
9 years ago
struct iguana_peer *addr; int32_t i;
8 years ago
if ( btcd == 0 )
return(0);
9 years ago
if ( (addr= iguana_peerfindipbits(btcd,ipbits,0)) == 0 )
9 years ago
{
9 years ago
if ( (addr= iguana_peerslot(btcd,ipbits,0)) != 0 )
9 years ago
{
printf("launch peer for relay\n");
addr->isrelay = 1;
8 years ago
RELAYID = -1;
for (i=0; i<NUMRELAYS; i++)
if ( RELAYS[i].ipbits == myinfo->myaddr.myipbits )
9 years ago
{
8 years ago
RELAYID = i;
9 years ago
break;
}
9 years ago
iguana_launch(btcd,"addrelay",iguana_startconnection,addr,IGUANA_CONNTHREAD);
9 years ago
} else printf("error getting peerslot\n");
9 years ago
} else addr->isrelay = 1;
return(addr);
}
8 years ago
static int _increasing_ipbits(const void *a,const void *b)
9 years ago
{
9 years ago
#define uint32_a (*(struct basilisk_relay *)a).ipbits
#define uint32_b (*(struct basilisk_relay *)b).ipbits
9 years ago
if ( uint32_b > uint32_a )
return(-1);
8 years ago
else if ( uint32_b < uint32_a )
return(1);
9 years ago
return(0);
#undef uint32_a
#undef uint32_b
}
void basilisk_relay_remap(struct supernet_info *myinfo,struct basilisk_relay *rp)
{
int32_t i; struct basilisk_relaystatus tmp[BASILISK_MAXRELAYS];
// need to verify this works
8 years ago
for (i=0; i<NUMRELAYS; i++)
9 years ago
tmp[i] = rp->reported[i];
8 years ago
for (i=0; i<NUMRELAYS; i++)
rp->reported[RELAYS[i].relayid] = tmp[RELAYS[i].oldrelayid];
9 years ago
}
8 years ago
void basilisk_setmyid(struct supernet_info *myinfo)
{
int32_t i; char ipaddr[64]; struct iguana_info *btcd = iguana_coinfind("BTCD");
8 years ago
for (i=0; i<NUMRELAYS; i++)
8 years ago
{
8 years ago
expand_ipbits(ipaddr,RELAYS[i].ipbits);
if ( myinfo->myaddr.myipbits == RELAYS[i].ipbits )
RELAYID = i;
basilisk_ensurerelay(myinfo,btcd,RELAYS[i].ipbits);
8 years ago
}
}
9 years ago
char *basilisk_addrelay_info(struct supernet_info *myinfo,uint8_t *pubkey33,uint32_t ipbits,bits256 pubkey)
9 years ago
{
8 years ago
int32_t i; struct basilisk_relay *rp;
8 years ago
for (i=0; i<NUMRELAYS; i++)
9 years ago
{
8 years ago
rp = &RELAYS[i];
9 years ago
if ( ipbits == rp->ipbits )
9 years ago
{
if ( bits256_cmp(GENESIS_PUBKEY,pubkey) != 0 && bits256_nonz(pubkey) != 0 )
rp->pubkey = pubkey;
9 years ago
if ( pubkey33 != 0 && pubkey33[0] != 0 )
9 years ago
memcpy(rp->pubkey33,pubkey33,33);
9 years ago
//printf("updated relay[%d] %x\n",i,ipbits);
9 years ago
return(clonestr("{\"error\":\"relay already there\"}"));
9 years ago
}
9 years ago
}
8 years ago
if ( i >= sizeof(RELAYS)/sizeof(*RELAYS) )
i = (rand() % (sizeof(RELAYS)/sizeof(*RELAYS)));
9 years ago
printf("add relay[%d] <- %x\n",i,ipbits);
8 years ago
for (i=0; i<NUMRELAYS; i++)
RELAYS[i].oldrelayid = i;
rp = &RELAYS[i];
9 years ago
rp->ipbits = ipbits;
8 years ago
rp->relayid = NUMRELAYS;
8 years ago
basilisk_ensurerelay(myinfo,iguana_coinfind("BTCD"),rp->ipbits);
8 years ago
if ( NUMRELAYS < sizeof(RELAYS)/sizeof(*RELAYS) )
NUMRELAYS++;
qsort(RELAYS,NUMRELAYS,sizeof(RELAYS[0]),_increasing_ipbits);
for (i=0; i<NUMRELAYS; i++)
RELAYS[i].relayid = i;
8 years ago
basilisk_setmyid(myinfo);
8 years ago
printf("sorted MYRELAYID.%d\n",RELAYID);
for (i=0; i<NUMRELAYS; i++)
basilisk_relay_remap(myinfo,&RELAYS[i]);
9 years ago
return(clonestr("{\"result\":\"relay added\"}"));
}
9 years ago
char *basilisk_respond_goodbye(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
{
9 years ago
struct iguana_peer *addr = _addr;
9 years ago
printf("(%s) sends goodbye\n",remoteaddr);
addr->dead = (uint32_t)time(NULL);
addr->rank = 0;
return(0);
}
void basilisk_request_goodbye(struct supernet_info *myinfo)
{
9 years ago
cJSON *valsobj = cJSON_CreateObject();
9 years ago
jaddnum(valsobj,"timeout",-1);
9 years ago
basilisk_requestservice(myinfo,0,"BYE",0,valsobj,GENESIS_PUBKEY,0,0,0);
9 years ago
free_json(valsobj);
}
9 years ago
char *basilisk_respond_addrelay(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
{
9 years ago
char *ipaddr,*retstr=0;
if ( (ipaddr= jstr(valsobj,"ipaddr")) != 0 )
retstr = basilisk_addrelay_info(myinfo,0,(uint32_t)calc_ipbits(ipaddr),jbits256(valsobj,"pubkey"));
else retstr = clonestr("{\"error\":\"need rmd160, address and ipaddr\"}");
9 years ago
return(retstr);
}
9 years ago
char *basilisk_respond_forward(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
{
char *retstr=0;
return(retstr);
}
9 years ago
char *basilisk_respond_mailbox(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
{
char *retstr=0;
return(retstr);
}
9 years ago
char *basilisk_respond_VPNcreate(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
{
char *retstr=0;
return(retstr);
}
9 years ago
char *basilisk_respond_VPNjoin(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
{
char *retstr=0;
return(retstr);
}
9 years ago
char *basilisk_respond_VPNlogout(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
{
char *retstr=0;
return(retstr);
}
9 years ago
char *basilisk_respond_VPNbroadcast(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
{
char *retstr=0;
return(retstr);
}
9 years ago
char *basilisk_respond_VPNreceive(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
{
char *retstr=0;
return(retstr);
}
9 years ago
char *basilisk_respond_VPNmessage(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
{
char *retstr=0;
return(retstr);
}
9 years ago
/*char *basilisk_respond_rawtx(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
{
9 years ago
char *symbol,*retstr=0; struct basilisk_item Lptr,*ptr; int32_t timeoutmillis; struct iguana_info *coin = 0;
timeoutmillis = jint(valsobj,"timeout");
if ( (symbol= jstr(valsobj,"coin")) != 0 || (symbol= jstr(valsobj,"symbol")) != 0 )
coin = iguana_coinfind(symbol);
if ( coin != 0 && (ptr= basilisk_bitcoinrawtx(&Lptr,myinfo,coin,remoteaddr,basilisktag,timeoutmillis,valsobj)) != 0 )
{
retstr = ptr->retstr;
ptr->finished = (uint32_t)time(NULL);
} else retstr = clonestr("{\"error\":\"no coin specified or error bitcoinrawtx\"}");
9 years ago
return(retstr);
9 years ago
}*/
9 years ago
char *basilisk_respond_value(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
char *symbol,*retstr=0; struct basilisk_item Lptr,*ptr; int32_t timeoutmillis; struct iguana_info *coin = 0;
timeoutmillis = jint(valsobj,"timeout");
if ( (symbol= jstr(valsobj,"coin")) != 0 || (symbol= jstr(valsobj,"symbol")) != 0 )
coin = iguana_coinfind(symbol);
if ( coin != 0 && (ptr= basilisk_bitcoinvalue(&Lptr,myinfo,coin,remoteaddr,basilisktag,timeoutmillis,valsobj)) != 0 )
{
retstr = ptr->retstr;
ptr->finished = (uint32_t)time(NULL);
} else retstr = clonestr("{\"error\":\"no coin specified or error bitcoinrawtx\"}");
9 years ago
return(retstr);
}
char *basilisk_respond_balances(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
char *symbol,*retstr=0; struct basilisk_item Lptr,*ptr; int32_t timeoutmillis; struct iguana_info *coin = 0;
timeoutmillis = jint(valsobj,"timeout");
if ( (symbol= jstr(valsobj,"coin")) != 0 || (symbol= jstr(valsobj,"symbol")) != 0 )
coin = iguana_coinfind(symbol);
if ( coin != 0 && (ptr= basilisk_bitcoinbalances(&Lptr,myinfo,coin,remoteaddr,basilisktag,timeoutmillis,valsobj)) != 0 )
{
retstr = ptr->retstr;
ptr->finished = (uint32_t)time(NULL);
} else retstr = clonestr("{\"error\":\"no coin specified or error bitcoinrawtx\"}");
9 years ago
return(retstr);
}
#include "../includes/iguana_apidefs.h"
#include "../includes/iguana_apideclares.h"
HASH_ARRAY_STRING(basilisk,addrelay,hash,vals,hexstr)
{
9 years ago
return(basilisk_standardservice("ADD",myinfo,0,hash,vals,hexstr,1));
9 years ago
}
HASH_ARRAY_STRING(basilisk,relays,hash,vals,hexstr)
{
9 years ago
return(basilisk_standardservice("RLY",myinfo,0,hash,vals,hexstr,1));
9 years ago
}
HASH_ARRAY_STRING(basilisk,dispatch,hash,vals,hexstr)
{
9 years ago
return(basilisk_standardservice("RUN",myinfo,0,hash,vals,hexstr,1));
9 years ago
}
HASH_ARRAY_STRING(basilisk,publish,hash,vals,hexstr)
{
9 years ago
return(basilisk_standardservice("PUB",myinfo,0,hash,vals,hexstr,1));
9 years ago
}
HASH_ARRAY_STRING(basilisk,subscribe,hash,vals,hexstr)
{
9 years ago
return(basilisk_standardservice("SUB",myinfo,0,hash,vals,hexstr,1));
9 years ago
}
HASH_ARRAY_STRING(basilisk,forward,hash,vals,hexstr)
{
9 years ago
return(basilisk_standardservice("HOP",myinfo,0,hash,vals,hexstr,0));
9 years ago
}
HASH_ARRAY_STRING(basilisk,mailbox,hash,vals,hexstr)
{
9 years ago
return(basilisk_standardservice("BOX",myinfo,0,hash,vals,hexstr,1));
9 years ago
}
HASH_ARRAY_STRING(basilisk,VPNcreate,hash,vals,hexstr)
{
9 years ago
return(basilisk_standardservice("VPN",myinfo,0,hash,vals,hexstr,1));
9 years ago
}
HASH_ARRAY_STRING(basilisk,VPNjoin,hash,vals,hexstr)
{
9 years ago
return(basilisk_standardservice("ARC",myinfo,0,hash,vals,hexstr,1));
9 years ago
}
HASH_ARRAY_STRING(basilisk,VPNmessage,hash,vals,hexstr)
{
9 years ago
return(basilisk_standardservice("GAB",myinfo,0,hash,vals,hexstr,0));
9 years ago
}
HASH_ARRAY_STRING(basilisk,VPNbroadcast,hash,vals,hexstr)
{
9 years ago
return(basilisk_standardservice("SAY",myinfo,0,hash,vals,hexstr,0));
9 years ago
}
HASH_ARRAY_STRING(basilisk,VPNreceive,hash,vals,hexstr)
{
9 years ago
return(basilisk_standardservice("EAR",myinfo,0,hash,vals,hexstr,1));
9 years ago
}
HASH_ARRAY_STRING(basilisk,VPNlogout,hash,vals,hexstr)
{
9 years ago
return(basilisk_standardservice("END",myinfo,0,hash,vals,hexstr,0));
9 years ago
}
9 years ago
uint16_t basilisk_portavailable(struct supernet_info *myinfo,uint16_t port)
{
struct iguana_info *coin,*tmp;
if ( port < 10000 )
return(0);
HASH_ITER(hh,myinfo->allcoins,coin,tmp)
{
if ( port == coin->chain->portp2p || port == coin->chain->rpcport )
return(0);
}
return(port);
}
9 years ago
HASH_ARRAY_STRING(basilisk,genesis_opreturn,hash,vals,hexstr)
{
9 years ago
int32_t len; uint16_t blocktime,port; uint8_t p2shval,wifval; uint8_t serialized[4096],tmp[4]; char hex[8192],*symbol,*name,*destaddr; uint64_t PoSvalue; uint32_t nBits;
9 years ago
symbol = jstr(vals,"symbol");
name = jstr(vals,"name");
destaddr = jstr(vals,"issuer");
PoSvalue = jdouble(vals,"stake") * SATOSHIDEN;
if ( (blocktime= juint(vals,"blocktime")) == 0 )
blocktime = 1;
p2shval = juint(vals,"p2sh");
wifval = juint(vals,"wif");
9 years ago
if ( (port= juint(vals,"port")) == 0 )
while ( (port= basilisk_portavailable(myinfo,(rand() % 50000) + 10000)) == 0 )
;
9 years ago
if ( jstr(vals,"nBits") != 0 )
{
decode_hex((void *)&tmp,sizeof(tmp),jstr(vals,"nBits"));
((uint8_t *)&nBits)[0] = tmp[3];
((uint8_t *)&nBits)[1] = tmp[2];
((uint8_t *)&nBits)[2] = tmp[1];
((uint8_t *)&nBits)[3] = tmp[0];
} else nBits = 0x1e00ffff;
9 years ago
len = datachain_opreturn_create(serialized,symbol,name,destaddr,PoSvalue,nBits,blocktime,port,p2shval,wifval);
9 years ago
init_hexbytes_noT(hex,serialized,len);
return(clonestr(hex));
}
9 years ago
#include "../includes/iguana_apiundefs.h"
9 years ago