diff --git a/basilisk/basilisk.c b/basilisk/basilisk.c index 70c2b53b1..ac85ba03b 100755 --- a/basilisk/basilisk.c +++ b/basilisk/basilisk.c @@ -138,7 +138,6 @@ int32_t basilisk_sendcmd(struct supernet_info *myinfo,char *destipaddr,char *typ fanout = BASILISK_MINFANOUT; else if ( fanout > BASILISK_MAXFANOUT ) fanout = BASILISK_MAXFANOUT; - r = rand(), r2 = rand(); if ( type == 0 ) type = ""; if ( strlen(type) > 3 ) @@ -152,6 +151,7 @@ int32_t basilisk_sendcmd(struct supernet_info *myinfo,char *destipaddr,char *typ destipaddr = 0; // broadcast else if ( strcmp(destipaddr,"127.0.0.1") == 0 ) { + printf("return after locally basilisk_msgprocess\n"); basilisk_msgprocess(myinfo,0,0,type,basilisktag,data,datalen); return(0); } @@ -160,6 +160,7 @@ int32_t basilisk_sendcmd(struct supernet_info *myinfo,char *destipaddr,char *typ data -= sizeof(basilisktag), datalen += sizeof(basilisktag); memset(cmd,0,sizeof(cmd)); sprintf(cmd,"SuperNET%s",type); + r = rand(), r2 = rand(); for (k=0; ksupernet != 0 && (destipaddr == 0 || strcmp(addr->ipaddr,destipaddr) == 0) ) { - //printf("send back.%s basilisk_result addr->supernet.%u to (%s).%d\n",retstr,addr->supernet,addr->ipaddr,addr->A.port); + printf("[%s] send %s.(%s) addr->supernet.%u to (%s).%d destip.%s\n",cmd,type,(char *)&data[4],addr->supernet,addr->ipaddr,addr->A.port,destipaddr!=0?destipaddr:"broadcast"); if ( encryptflag != 0 && bits256_nonz(addr->pubkey) != 0 ) { void *ptr; uint8_t *cipher,space[8192]; int32_t cipherlen; bits256 privkey; @@ -200,7 +201,7 @@ int32_t basilisk_sendcmd(struct supernet_info *myinfo,char *destipaddr,char *typ } } } - return(retval); + return(n); } void basilisk_p2p(void *_myinfo,void *_addr,int32_t *delaymillisp,char *senderip,uint8_t *data,int32_t datalen,char *type,int32_t encrypted) @@ -233,7 +234,7 @@ void basilisk_p2p(void *_myinfo,void *_addr,int32_t *delaymillisp,char *senderip struct basilisk_item *basilisk_issueremote(struct supernet_info *myinfo,char *type,cJSON *valsobj,int32_t fanout,int32_t minresults,uint32_t basilisktag,int32_t timeoutmillis,void *_metricfunc,char *retstr) { - struct basilisk_item *ptr; uint8_t *data; int32_t datalen,delaymillis=0,encryptflag=0; basilisk_metricfunc metricfunc = _metricfunc; + struct basilisk_item *ptr; void *allocptr; uint8_t *data,space[4096]; int32_t datalen,delaymillis=0,encryptflag=0; basilisk_metricfunc metricfunc = _metricfunc; if ( basilisktag == 0 ) basilisktag = rand(); ptr = basilisk_itemcreate(myinfo,basilisktag,minresults,valsobj,timeoutmillis,metricfunc); @@ -247,16 +248,18 @@ struct basilisk_item *basilisk_issueremote(struct supernet_info *myinfo,char *ty } else { - data = basilisk_jsondata(&datalen,valsobj,basilisktag); + data = basilisk_jsondata(&allocptr,space,sizeof(space),&datalen,valsobj,basilisktag); basilisk_sendcmd(myinfo,0,"RET",basilisktag,encryptflag,delaymillis,data,datalen,0); - free(data); + if ( allocptr != 0 ) + free(allocptr); } return(ptr); } -void basilisk_functions(struct iguana_info *coin) +void basilisk_functions(struct iguana_info *coin,int32_t protocol) { - switch ( coin->protocol ) + coin->protocol = protocol; + switch ( protocol ) { case IGUANA_PROTOCOL_BITCOIN: coin->basilisk_balances = basilisk_bitcoinbalances; @@ -340,13 +343,21 @@ char *basilisk_iscomplete(struct basilisk_item *ptr) return(retstr); } -uint8_t *basilisk_jsondata(int32_t *datalenp,cJSON *sendjson,uint32_t basilisktag) +uint8_t *basilisk_jsondata(void **ptrp,uint8_t *space,int32_t spacesize,int32_t *datalenp,cJSON *sendjson,uint32_t basilisktag) { - char *sendstr; uint8_t *data; int32_t datalen; + char *sendstr; uint8_t *data; int32_t datalen,extrasize; + extrasize = (int32_t)(sizeof(struct iguana_msghdr) + sizeof(basilisktag)); + *ptrp = 0; sendstr = jprint(sendjson,0); datalen = (int32_t)strlen(sendstr) + 1; - data = calloc(1,datalen + sizeof(struct iguana_msghdr) + sizeof(basilisktag)); - data += sizeof(struct iguana_msghdr) + sizeof(basilisktag); + if ( (datalen + extrasize) <= spacesize ) + data = space; + else + { + data = calloc(1,datalen + extrasize); + *ptrp = data; + } + data += extrasize; memcpy(data,sendstr,datalen); free(sendstr); *datalenp = datalen; @@ -355,7 +366,7 @@ uint8_t *basilisk_jsondata(int32_t *datalenp,cJSON *sendjson,uint32_t basiliskta char *basilisk_block(struct supernet_info *myinfo,struct iguana_info *coin,char *remoteaddr,struct basilisk_item *Lptr,struct basilisk_item *ptr) { - char *retstr = 0; uint8_t *data; cJSON *valsobj; int32_t datalen,encryptflag=0,delaymillis=0; + char *retstr = 0; uint8_t *data,space[4096]; void *allocptr; cJSON *valsobj; int32_t datalen,encryptflag=0,delaymillis=0; if ( ptr == Lptr ) { if ( (retstr= Lptr->retstr) == 0 ) @@ -381,9 +392,10 @@ char *basilisk_block(struct supernet_info *myinfo,struct iguana_info *coin,char { if ( jobj(valsobj,"coin") == 0 ) jaddstr(valsobj,"coin",ptr->symbol); - data = basilisk_jsondata(&datalen,valsobj,ptr->basilisktag); + data = basilisk_jsondata(&allocptr,space,sizeof(space),&datalen,valsobj,ptr->basilisktag); basilisk_sendcmd(myinfo,remoteaddr,"RET",ptr->basilisktag,encryptflag,delaymillis,data,datalen,0); - free(data); + if ( allocptr != 0 ) + free(allocptr); free_json(valsobj); } } @@ -485,7 +497,8 @@ INT_ARRAY_STRING(basilisk,rawtx,basilisktag,vals,activecoin) char *retstr; struct basilisk_item *ptr,Lptr; int32_t timeoutmillis; if ( (retstr= basilisk_checkrawtx(&timeoutmillis,(uint32_t *)&basilisktag,activecoin,vals)) == 0 ) { - if ( (ptr= basilisk_issuecmd(&Lptr,coin->basilisk_rawtx,coin->basilisk_rawtxmetric,myinfo,remoteaddr,basilisktag,activecoin,timeoutmillis,vals)) != 0 ) + coin = iguana_coinfind(activecoin); + if ( coin != 0 && (ptr= basilisk_issuecmd(&Lptr,coin->basilisk_rawtx,coin->basilisk_rawtxmetric,myinfo,remoteaddr,basilisktag,activecoin,timeoutmillis,vals)) != 0 ) { if ( (ptr->numrequired= juint(vals,"numrequired")) == 0 ) ptr->numrequired = 1; diff --git a/basilisk/basilisk.h b/basilisk/basilisk.h index 07a1bbbd4..ade2fd80f 100755 --- a/basilisk/basilisk.h +++ b/basilisk/basilisk.h @@ -43,7 +43,7 @@ struct basilisk_item *basilisk_issueremote(struct supernet_info *myinfo,char *ty void basilisks_init(struct supernet_info *myinfo); int32_t basilisk_sendcmd(struct supernet_info *myinfo,char *destipaddr,char *type,uint32_t basilisktag,int32_t encryptflag,int32_t delaymillis,uint8_t *data,int32_t datalen,int32_t fanout); // data must be offset by sizeof(iguana_msghdr) void basilisk_p2p(void *myinfo,void *_addr,int32_t *delaymillisp,char *ipaddr,uint8_t *data,int32_t datalen,char *type,int32_t encrypted); -uint8_t *basilisk_jsondata(int32_t *datalenp,cJSON *sendjson,uint32_t basilisktag); +uint8_t *basilisk_jsondata(void **ptrp,uint8_t *space,int32_t spacesize,int32_t *datalenp,cJSON *sendjson,uint32_t basilisktag); uint8_t *SuperNET_ciphercalc(void **ptrp,int32_t *cipherlenp,bits256 *privkeyp,bits256 *destpubkeyp,uint8_t *data,int32_t datalen,uint8_t *space2,int32_t space2size); void *SuperNET_deciphercalc(void **ptrp,int32_t *msglenp,bits256 privkey,bits256 srcpubkey,uint8_t *cipher,int32_t cipherlen,uint8_t *buf,int32_t bufsize); diff --git a/iguana/iguana777.c b/iguana/iguana777.c index c59298c9f..73e3ebfaf 100755 --- a/iguana/iguana777.c +++ b/iguana/iguana777.c @@ -47,6 +47,7 @@ struct iguana_info *iguana_coinadd(const char *symbol,cJSON *argjson) { Coins[i] = mycalloc('C',1,sizeof(*Coins[i])); printf("iguana_coin.(new) -> %p\n",Coins[i]); + basilisk_functions(Coins[i],IGUANA_PROTOCOL_BITCOIN); return(Coins[i]); } return(0); printf("i.%d (%s) vs name.(%s)\n",i,Coins[i]->name,symbol); @@ -64,7 +65,10 @@ struct iguana_info *iguana_coinadd(const char *symbol,cJSON *argjson) if ( strcmp("endmarker",Hardcoded_coins[i][0]) == 0 || strcmp(symbol,Hardcoded_coins[i][0]) == 0 ) { if ( Coins[i] == 0 ) + { Coins[i] = mycalloc('C',1,sizeof(*Coins[i])); + basilisk_functions(Coins[i],IGUANA_PROTOCOL_BITCOIN); + } coin = Coins[i]; if ( coin->chain == 0 ) { @@ -800,7 +804,6 @@ struct iguana_info *iguana_setcoin(char *symbol,void *launched,int32_t maxpeers, coin->initialheight = initialheight; coin->mapflags = mapflags; coin->protocol = IGUANA_PROTOCOL_BITCOIN; - basilisk_functions(coin); mult = (strcmp("BTC",coin->symbol) != 0) ? 8 : 8; maxval = IGUANA_MAXPENDBUNDLES; if ( (coin->txfee= jdouble(json,"txfee") * SATOSHIDEN) == 0 ) diff --git a/iguana/iguana777.h b/iguana/iguana777.h index f4a7e8e61..0b03a189b 100755 --- a/iguana/iguana777.h +++ b/iguana/iguana777.h @@ -1035,7 +1035,7 @@ void instantdex_eventfree(struct bitcoin_eventitem *ptr); struct iguana_monitorinfo *iguana_txidmonitor(struct iguana_info *coin,bits256 txid); struct iguana_monitorinfo *iguana_txidreport(struct iguana_info *coin,bits256 txid,struct iguana_peer *addr); double iguana_txidstatus(struct iguana_info *coin,bits256 txid); -void basilisk_functions(struct iguana_info *coin); +void basilisk_functions(struct iguana_info *coin,int32_t protocol); char *bitcoind_passthru(char *coinstr,char *serverport,char *userpass,char *method,char *params); char *bitcoin_calcrawtx(struct supernet_info *myinfo,struct iguana_info *coin,cJSON **vinsp,int64_t satoshis,char *paymentscriptstr,char *changeaddr,int64_t txfee,cJSON *addresses,int32_t minconf,uint32_t locktime); char *bitcoin_blockhashstr(char *coinstr,char *serverport,char *userpass,int32_t height); diff --git a/iguana/iguana_instantdex.c b/iguana/iguana_instantdex.c index 01bc299fb..7aaf01c8c 100755 --- a/iguana/iguana_instantdex.c +++ b/iguana/iguana_instantdex.c @@ -389,7 +389,7 @@ bits256 instantdex_rwoffer(int32_t rwflag,int32_t *lenp,uint8_t *serialized,stru char *instantdex_sendcmd(struct supernet_info *myinfo,struct instantdex_offer *offer,cJSON *argjson,char *cmdstr,bits256 desthash,int32_t hops,void *extraser,int32_t extralen,struct iguana_peer *addr,struct bitcoin_swapinfo *swap) { - cJSON *sendjson; char *reqstr,*hexstr,*retstr; struct instantdex_msghdr *msg; bits256 orderhash,tmphash; int32_t i,j,len,dir=0,serflag,olen,slen,datalen; uint8_t *buf,serialized[sizeof(*offer) + sizeof(struct iguana_msghdr) + 4096 + INSTANTDEX_DECKSIZE*33]; uint64_t x,nxt64bits; + cJSON *sendjson; void *allocptr; char *reqstr,*hexstr,*retstr; struct instantdex_msghdr *msg; bits256 orderhash,tmphash; int32_t i,j,len,dir=0,serflag,olen,slen,datalen; uint8_t *buf,space[4096],serialized[sizeof(*offer) + sizeof(struct iguana_msghdr) + 4096 + INSTANTDEX_DECKSIZE*33]; uint64_t x,nxt64bits; //if ( strcmp(cmdstr,"poll") == 0 ) // return(clonestr("{\"result\":\"skip sending poll\"}")); //category_subscribe(myinfo,myinfo->instantdex_category,GENESIS_PUBKEY); @@ -473,10 +473,11 @@ char *instantdex_sendcmd(struct supernet_info *myinfo,struct instantdex_offer *o jaddnum(sendjson,"plaintext",1); jaddbits256(sendjson,"categoryhash",myinfo->instantdex_category); jaddbits256(sendjson,"traderpub",myinfo->myaddr.persistent); - data = basilisk_jsondata(&datalen,sendjson,basilisktag); + data = basilisk_jsondata(&allocptr,space,sizeof(space),&datalen,sendjson,basilisktag); basilisk_sendcmd(myinfo,addr->ipaddr,dir > 0 ? "BID" : "ASK",basilisktag,encryptflag,delaymillis,data,datalen,1); free_json(sendjson); - free(data); + if ( allocptr != 0 ) + free(allocptr); return(clonestr("{\"result\":\"success\"}")); if ( instantdex_msgcreate(myinfo,msg,datalen) != 0 ) diff --git a/iguana/iguana_msg.c b/iguana/iguana_msg.c index 6b7c249df..62e951733 100755 --- a/iguana/iguana_msg.c +++ b/iguana/iguana_msg.c @@ -682,7 +682,7 @@ int32_t iguana_msgparser(struct iguana_info *coin,struct iguana_peer *addr,struc } else ipaddr = 0; len = recvlen; basilisk_p2p(myinfo,addr,&delay,ipaddr,data,recvlen,&H->command[strlen("SuperNET")],H->command[6] == 'e' && H->command[7] == 't'); - //printf("GOT.(%s) len.%d from %s -> ret.(%s)\n",H->command,recvlen,addr->ipaddr,retstr==0?"null":retstr); + printf("GOT.(%s) len.%d from %s\n",H->command,recvlen,addr->ipaddr); return(0); } if ( addr != 0 ) diff --git a/iguana/iguana_peers.c b/iguana/iguana_peers.c index 2b4f544a9..17cc86ce0 100755 --- a/iguana/iguana_peers.c +++ b/iguana/iguana_peers.c @@ -419,9 +419,9 @@ int32_t iguana_send(struct iguana_info *coin,struct iguana_peer *addr,uint8_t *s return(-1); } remains = len; - if ( strncmp(cmdstr,"SuperNET",strlen("SuperNET")) == 0 ) + if ( strncmp(cmdstr,"SuperNET",strlen("SuperNET")) == 0 || strncmp(cmdstr,"SuperNet",strlen("SuperNet")) == 0 ) { - //printf(" >>>>>>> send.(%s) %d bytes to %s:%u supernet.%d\n",cmdstr,len,addr->ipaddr,addr->A.port,addr->supernet); + printf(" >>>>>>> send.(%s) %d bytes to %s:%u supernet.%d\n",cmdstr,len,addr->ipaddr,addr->A.port,addr->supernet); } else if ( addr->msgcounts.verack == 0 && (strcmp(cmdstr,"version") != 0 && strcmp(cmdstr,"ConnectTo") != 0 && strcmp(cmdstr,"verack") != 0) != 0 ) {