diff --git a/basilisk/basilisk.c b/basilisk/basilisk.c index d23baf036..570d0db5f 100755 --- a/basilisk/basilisk.c +++ b/basilisk/basilisk.c @@ -277,7 +277,7 @@ void basilisk_p2p(void *_myinfo,void *_addr,char *senderip,uint8_t *data,int32_t ipbits = (uint32_t)calc_ipbits(senderip); else ipbits = 0; len += iguana_rwnum(0,data,sizeof(basilisktag),&basilisktag); - printf("received.%d basilisk_p2p.(%s) from %s tag.%d\n",datalen,type,senderip!=0?senderip:"?",basilisktag); + //printf("received.%d basilisk_p2p.(%s) from %s tag.%d\n",datalen,type,senderip!=0?senderip:"?",basilisktag); basilisk_msgprocess(myinfo,_addr,ipbits,type,basilisktag,&data[len],datalen - len); if ( ptr != 0 ) free(ptr); @@ -354,7 +354,9 @@ struct basilisk_item *basilisk_issueremote(struct supernet_info *myinfo,int32_t struct basilisk_item *basilisk_requestservice(struct supernet_info *myinfo,char *CMD,cJSON *valsobj,bits256 hash,uint8_t *data,int32_t datalen,uint32_t nBits) { - int32_t minresults,timeoutmillis,numsent,delaymillis,encryptflag; struct basilisk_item *ptr; char buf[4096],*symbol,*str = 0; struct iguana_info *virt; + int32_t minresults,timeoutmillis,numsent,delaymillis,encryptflag; struct basilisk_item *ptr; char buf[4096],*symbol,*str = 0; struct iguana_info *virt,*btcd; + if ( (btcd= iguana_coinfind("BTCD")) != 0 && btcd->RELAYNODE != 0 ) + jaddnum(valsobj,"iamrelay",1); basilisk_addhexstr(&str,valsobj,buf,sizeof(buf),data,datalen); if ( jobj(valsobj,"hash") != 0 ) jdelete(valsobj,"hash"); @@ -601,23 +603,6 @@ char *basilisk_checkrawtx(int32_t *timeoutmillisp,uint32_t *basilisktagp,char *s } } -int32_t basilisk_relays_send(struct supernet_info *myinfo,struct iguana_peer *addr) -{ - int32_t i,len = 0; uint8_t serialized[sizeof(myinfo->relaybits)]; cJSON *vals; bits256 hash; char *retstr,hexstr[sizeof(myinfo->relaybits)*2 + 1]; - if ( myinfo != 0 ) - { - memset(hash.bytes,0,sizeof(hash)); - for (i=0; inumrelays; i++) - len += iguana_rwnum(1,&serialized[len],sizeof(uint32_t),&myinfo->relaybits[i]); - init_hexbytes_noT(hexstr,serialized,len); - vals = cJSON_CreateObject(); - if ( (retstr= basilisk_standardservice("REL",myinfo,hash,vals,hexstr,0)) != 0 ) - free(retstr); - free_json(vals); - return(0); - } else return(-1); -} - int32_t basilisk_hashes_send(struct supernet_info *myinfo,struct iguana_info *virt,struct iguana_peer *addr,char *CMD,bits256 *hashes,int32_t num) { bits256 hash; uint8_t *serialized; int32_t i,len = 0; char *str=0,*retstr,*hexstr,*allocptr=0,space[8192]; bits256 txid; cJSON *vals; @@ -698,7 +683,7 @@ HASH_ARRAY_STRING(basilisk,addrelay,hash,vals,hexstr) HASH_ARRAY_STRING(basilisk,relays,hash,vals,hexstr) { - return(basilisk_standardservice("REL",myinfo,hash,vals,hexstr,1)); + return(basilisk_standardservice("RLY",myinfo,hash,vals,hexstr,1)); } HASH_ARRAY_STRING(basilisk,dispatch,hash,vals,hexstr) @@ -1005,7 +990,7 @@ void basilisk_msgprocess(struct supernet_info *myinfo,void *_addr,uint32_t sende // unencrypted low level functions, used by higher level protocols and virtual network funcs { (void *)"ADD", &basilisk_respond_addrelay }, // relays register with each other bus - { (void *)"REL", &basilisk_respond_relays }, + // { (void *)"RLY", &basilisk_respond_relays }, { (void *)"DEX", &basilisk_respond_instantdex }, // encrypted data for jumblr @@ -1029,6 +1014,7 @@ void basilisk_msgprocess(struct supernet_info *myinfo,void *_addr,uint32_t sende symbol = "BTCD"; if ( (valsobj= cJSON_Parse((char *)data)) != 0 ) { + printf("MSGVALS.(%s)\n",(char *)data); if ( jobj(valsobj,"coin") != 0 ) coin = iguana_coinfind(jstr(valsobj,"coin")); else if ( jobj(valsobj,"symbol") != 0 ) @@ -1078,7 +1064,7 @@ void basilisk_msgprocess(struct supernet_info *myinfo,void *_addr,uint32_t sende CMD[i] = toupper((int32_t)CMD[i]); cmd[i] = tolower((int32_t)CMD[i]); } - printf("MSGPROCESS.(%s) tag.%d\n",(char *)data,basilisktag); + //printf("MSGPROCESS.(%s) tag.%d\n",(char *)data,basilisktag); myinfo->basilisk_busy = 1; if ( valsobj != 0 ) { @@ -1103,6 +1089,18 @@ void basilisk_msgprocess(struct supernet_info *myinfo,void *_addr,uint32_t sende if ( senderipbits != 0 ) expand_ipbits(remoteaddr,senderipbits); else remoteaddr[0] = 0; + if ( valsobj != 0 && remoteaddr != 0 && juint(valsobj,"iamrelay") != 0 ) + { + if ( strcmp(CMD,"RLY") == 0 ) + { + retstr = basilisk_respond_relays(myinfo,type,addr,remoteaddr,basilisktag,valsobj,data,datalen,hash,from_basilisk); + free_json(valsobj); + myinfo->basilisk_busy = 0; + return; + } + else if ( (retstr= basilisk_addrelay_info(myinfo,0,(uint32_t)calc_ipbits(remoteaddr),GENESIS_PUBKEY)) != 0 ) + free(retstr); + } for (i=0; iIAMRELAY != 0 ) // iguana node { /*if ( from_basilisk != 0 ) - { - printf("echo to other relays\n"); - basilisk_sendcmd(myinfo,0,cmd,&basilisktag,0,0,origdata,origlen,-1,0); // to other iguanas - }*/ + { + printf("echo to other relays\n"); + basilisk_sendcmd(myinfo,0,cmd,&basilisktag,0,0,origdata,origlen,-1,0); // to other iguanas + }*/ if ( (retstr= (*basilisk_services[i][1])(myinfo,type,addr,remoteaddr,basilisktag,valsobj,data,datalen,hash,from_basilisk)) != 0 ) { printf("from_basilisk.%d ret.(%s)\n",from_basilisk,retstr); @@ -1152,12 +1150,7 @@ void basilisk_msgprocess(struct supernet_info *myinfo,void *_addr,uint32_t sende } else // basilisk node { - if ( strcmp(type,"REL") == 0 ) - { - printf("relays REL.(%s) datalen.%d\n",jprint(valsobj,0),datalen); - basilisk_respond_relays(myinfo,type,addr,remoteaddr,basilisktag,valsobj,data,datalen,hash,from_basilisk); - } - else if ( strcmp(type,"ADD") == 0 ) + if ( strcmp(type,"ADD") == 0 ) { printf("new relay ADD.(%s) datalen.%d\n",jprint(valsobj,0),datalen); basilisk_respond_addrelay(myinfo,type,addr,remoteaddr,basilisktag,valsobj,data,datalen,hash,from_basilisk); diff --git a/basilisk/basilisk.h b/basilisk/basilisk.h index c3f6ae3b7..c05cae47d 100755 --- a/basilisk/basilisk.h +++ b/basilisk/basilisk.h @@ -48,7 +48,7 @@ struct basilisk_info struct basilisk_relay { bits256 pubkey; uint32_t ipbits; struct iguana_peer *addr; - char btcdaddr[64],sigstr[168]; + uint8_t pubkey33[33]; }; void basilisk_msgprocess(struct supernet_info *myinfo,void *addr,uint32_t senderipbits,char *type,uint32_t basilisktag,uint8_t *data,int32_t datalen); diff --git a/basilisk/basilisk_CMD.c b/basilisk/basilisk_CMD.c index ac3642b45..a455cae68 100755 --- a/basilisk/basilisk_CMD.c +++ b/basilisk/basilisk_CMD.c @@ -30,7 +30,7 @@ struct iguana_peer *basilisk_ensurerelay(struct iguana_info *btcd,uint32_t ipbit return(addr); } -char *basilisk_addrelay_info(struct supernet_info *myinfo,char *btcdaddr,uint32_t ipbits,bits256 pubkey,char *sigstr) +char *basilisk_addrelay_info(struct supernet_info *myinfo,uint8_t *pubkey33,uint32_t ipbits,bits256 pubkey) { int32_t i; struct basilisk_relay *rp; struct iguana_info *btcd; if ( (btcd= iguana_coinfind("BTCD")) == 0 ) @@ -39,16 +39,19 @@ char *basilisk_addrelay_info(struct supernet_info *myinfo,char *btcdaddr,uint32_ { rp = &myinfo->relays[i]; if ( ipbits == rp->ipbits ) + { + if ( bits256_cmp(GENESIS_PUBKEY,pubkey) != 0 && bits256_nonz(pubkey) != 0 ) + rp->pubkey = pubkey; + if ( pubkey33[0] != 0 ) + memcpy(rp->pubkey33,pubkey33,33); return(clonestr("{\"error\":\"relay already there\"}")); + } } if ( i >= sizeof(myinfo->relays)/sizeof(*myinfo->relays) ) i = (rand() % (sizeof(myinfo->relays)/sizeof(*myinfo->relays))); rp = &myinfo->relays[i]; - printf("verify relay sig\n"); + printf("verify relay sig for %x\n",ipbits); rp->ipbits = ipbits; - rp->pubkey = pubkey; - safecopy(rp->btcdaddr,btcdaddr,sizeof(rp->btcdaddr)); - safecopy(rp->sigstr,sigstr,sizeof(rp->sigstr)); rp->addr = basilisk_ensurerelay(btcd,rp->ipbits); for (i=0; inumrelays; i++) myinfo->relaybits[i] = myinfo->relays[i].ipbits; @@ -57,6 +60,55 @@ char *basilisk_addrelay_info(struct supernet_info *myinfo,char *btcdaddr,uint32_ } char *basilisk_respond_relays(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) +{ + bits256 txhash2; uint32_t ipbits; int32_t i,n,len,siglen; uint8_t pubkey33[33],sig[128]; char *sigstr = 0,*retstr; + if ( (sigstr= jstr(valsobj,"sig")) != 0 ) + { + siglen = (int32_t)strlen(sigstr) >> 1; + decode_hex(sig,siglen,sigstr); + vcalc_sha256(0,txhash2.bytes,data,datalen); + if ( bitcoin_recoververify(myinfo->ctx,"BTCD",sig,txhash2,pubkey33) == 0 ) + { + // compare with existing + printf("verified relay data sig.%d\n",siglen); + if ( (retstr= basilisk_addrelay_info(myinfo,pubkey33,(uint32_t)calc_ipbits(remoteaddr),hash)) != 0 ) + free(retstr); + n = (int32_t)(datalen / sizeof(uint32_t)); + for (i=len=0; irelaybits)]; cJSON *vals; bits256 hash; char *retstr,hexstr[sizeof(myinfo->relaybits)*2 + 1]; + if ( myinfo != 0 ) + { + vals = cJSON_CreateObject(); + hash = myinfo->myaddr.persistent; + for (i=0; inumrelays; i++) + len += iguana_rwnum(1,&serialized[len],sizeof(uint32_t),&myinfo->relaybits[i]); + vcalc_sha256(0,txhash2.bytes,serialized,len); + if ( (siglen= bitcoin_sign(myinfo->ctx,"BTCD",sig,txhash2,myinfo->persistent_priv,1)) > 0 ) + { + init_hexbytes_noT(strbuf,sig,siglen); + jaddstr(vals,"sig",strbuf); + } + init_hexbytes_noT(hexstr,serialized,len); + if ( (retstr= basilisk_standardservice("RLY",myinfo,hash,vals,hexstr,0)) != 0 ) + free(retstr); + free_json(vals); + return(0); + } else return(-1); +} + +/*char *basilisk_respond_relays(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) { uint32_t *ipbits = (uint32_t *)data; int32_t num,i,j,n = datalen >> 2; for (i=num=0; inumrelays ) { num++; - printf("ensure unknown relay\n"); + printf("i.%d j.%d ensure new relay.(%s)\n",i,j,remoteaddr); basilisk_ensurerelay(iguana_coinfind("BTCD"),ipbits[i]); } } if ( num == 0 ) return(clonestr("{\"result\":\"no new relays found\"}")); else return(clonestr("{\"result\":\"relay added\"}")); -} +}*/ 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) { @@ -103,17 +155,16 @@ char *basilisk_respond_instantdex(struct supernet_info *myinfo,char *CMD,void *a char *basilisk_respond_dispatch(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) { - char *ipaddr,*btcdaddr,*sigstr,*retstr=0; - printf("from.(%s) ADD.(%s) datalen.%d\n",remoteaddr,jprint(valsobj,0),datalen); - if ( (ipaddr= jstr(valsobj,"ipaddr")) != 0 && (btcdaddr= jstr(valsobj,"btcdaddr")) != 0 && (sigstr= jstr(valsobj,"sigstr")) != 0 ) - retstr = basilisk_addrelay_info(myinfo,btcdaddr,(uint32_t)calc_ipbits(ipaddr),hash,sigstr); - else retstr = clonestr("{\"error\":\"need rmd160, address and ipaddr\"}"); + char *retstr=0; return(retstr); } 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) { - char *retstr=0; + 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\"}"); return(retstr); } diff --git a/gecko/gecko_headers.c b/gecko/gecko_headers.c index c14e74369..5c7377888 100755 --- a/gecko/gecko_headers.c +++ b/gecko/gecko_headers.c @@ -95,7 +95,10 @@ void gecko_iteration(struct supernet_info *myinfo,struct iguana_info *btcd,struc longesthdrsi = virt->longestchain / virt->chain->bundlesize; if ( hwmhdrsi < longesthdrsi ) gecko_requesthdrs(myinfo,virt,hwmhdrsi); - bitcoin_address(mineraddr,virt->chain->pubtype,myinfo->persistent_pubkey33,33); + if ( btcd->RELAYNODE != 0 ) + { + bitcoin_address(mineraddr,virt->chain->pubtype,myinfo->persistent_pubkey33,33); //printf("mine.%s %s\n",virt->symbol,mineraddr); - gecko_miner(myinfo,btcd,virt,maxmillis,myinfo->persistent_pubkey33); + gecko_miner(myinfo,btcd,virt,maxmillis,myinfo->persistent_pubkey33); + } } diff --git a/iguana/main.c b/iguana/main.c index f817d6407..ad1ada25f 100755 --- a/iguana/main.c +++ b/iguana/main.c @@ -1150,10 +1150,10 @@ void iguana_appletests(struct supernet_info *myinfo) bitcoin_sharedsecret(myinfo->ctx,hash2,pubkey,33); printf("secp256k1 elapsed %.3f for %d iterations\n",OS_milliseconds() - startmillis,i); getchar();**/ - if ( 1 && (str= SuperNET_JSON(myinfo,cJSON_Parse("{\"protover\":70002,\"RELAY\":1,\"VALIDATE\":1,\"portp2p\":14631,\"rpc\":14632,\"agent\":\"iguana\",\"method\":\"addcoin\",\"startpend\":64,\"endpend\":64,\"services\":129,\"maxpeers\":128,\"newcoin\":\"BTCD\",\"active\":1,\"numhelpers\":4,\"poll\":1}"),0,myinfo->rpcport)) != 0 ) + if ( 1 && (str= SuperNET_JSON(myinfo,cJSON_Parse("{\"protover\":70002,\"RELAY\":0,\"VALIDATE\":0,\"portp2p\":14631,\"rpc\":14632,\"agent\":\"iguana\",\"method\":\"addcoin\",\"startpend\":64,\"endpend\":64,\"services\":129,\"maxpeers\":128,\"newcoin\":\"BTCD\",\"active\":1,\"numhelpers\":4,\"poll\":1}"),0,myinfo->rpcport)) != 0 ) { free(str); - if ( 1 && (str= SuperNET_JSON(myinfo,cJSON_Parse("{\"portp2p\":8333,\"RELAY\":0,\"VALIDATE\":0,\"agent\":\"iguana\",\"method\":\"addcoin\",\"startpend\":1,\"endpend\":1,\"services\":128,\"maxpeers\":64,\"newcoin\":\"BTC\",\"active\":0,\"numhelpers\":4,\"poll\":100}"),0,myinfo->rpcport)) != 0 ) + if ( 0 && (str= SuperNET_JSON(myinfo,cJSON_Parse("{\"portp2p\":8333,\"RELAY\":0,\"VALIDATE\":0,\"agent\":\"iguana\",\"method\":\"addcoin\",\"startpend\":1,\"endpend\":1,\"services\":128,\"maxpeers\":64,\"newcoin\":\"BTC\",\"active\":0,\"numhelpers\":4,\"poll\":100}"),0,myinfo->rpcport)) != 0 ) { free(str); if ( 0 && (str= SuperNET_JSON(myinfo,cJSON_Parse("{\"agent\":\"SuperNET\",\"method\":\"login\",\"handle\":\"alice\",\"password\":\"alice\",\"passphrase\":\"alice\"}"),0,myinfo->rpcport)) != 0 ) diff --git a/iguana/tests/createrelay b/iguana/tests/createrelay new file mode 100755 index 000000000..1ed85c205 --- /dev/null +++ b/iguana/tests/createrelay @@ -0,0 +1,2 @@ +curl --url "http://127.0.0.1:14632" --data "{\"method\":\"createrelay\",\"params\":[\"ipaddr\", \"secp\", \"curve25519\"] }" +