int32_t iguana_peergetrequest(struct iguana_info *coin,struct iguana_peer *addr,uint8_t *data,int32_t recvlen,int32_t getblock) { int32_t i,reqvers,len,n,flag = 0; bits256 hash2; if ( getblock != 0 ) addr->msgcounts.getblocks++; else addr->msgcounts.getheaders++; len = iguana_rwnum(0,&data[0],sizeof(uint32_t),&reqvers); len += iguana_rwvarint32(0,&data[len],(uint32_t *)&n); for (i=0; i<n&&len<=recvlen-sizeof(bits256)*2; i++) { len += iguana_rwbignum(0,&data[len],sizeof(bits256),hash2.bytes); if ( bits256_nonz(hash2) == 0 ) break; if ( flag == 0 ) { if ( getblock != 0 && iguana_peerblockrequest(coin,addr->blockspace,IGUANA_MAXPACKETSIZE,addr,hash2,0) > 0 ) flag = 1; else if ( getblock == 0 && iguana_peerhdrrequest(coin,addr->blockspace,IGUANA_MAXPACKETSIZE,addr,hash2) > 0 ) flag = 1; } } len += iguana_rwbignum(0,&data[len],sizeof(bits256),hash2.bytes); //for (i=0; i<69; i++) // printf("%02x ",data[i]); //printf("version.%d num blocks.%d recvlen.%d len.%d\n",reqvers,n,recvlen,len); return(len); }
HASH_AND_TWOINTS(bitcoinrpc,getblock,blockhash,verbose,remoteonly) { char *blockstr,*datastr; struct iguana_msgblock msg; struct iguana_block *block; cJSON *retjson; bits256 txid; int32_t len; retjson = cJSON_CreateObject(); memset(&msg,0,sizeof(msg)); if ( remoteonly == 0 && (block= iguana_blockfind("getblockRPC",coin,blockhash)) != 0 ) { if ( verbose != 0 ) return(jprint(iguana_blockjson(coin,block,1),1)); else { if ( (len= iguana_peerblockrequest(coin,coin->blockspace,coin->blockspacesize,0,blockhash,0)) > 0 ) { datastr = malloc(len*2 + 1); init_hexbytes_noT(datastr,coin->blockspace,len); jaddstr(retjson,"result",datastr); free(datastr); return(jprint(retjson,1)); } jaddstr(retjson,"error","error getting rawblock"); } } else if ( coin->APIblockstr != 0 ) jaddstr(retjson,"error","already have pending request"); else { memset(txid.bytes,0,sizeof(txid)); if ( (blockstr= iguana_APIrequest(coin,blockhash,txid,5)) != 0 ) { jaddstr(retjson,"result",blockstr); free(blockstr); } else jaddstr(retjson,"error","cant find blockhash"); } return(jprint(retjson,1)); }
int32_t iguana_process_msgrequestQ(struct supernet_info *myinfo,struct iguana_info *coin) { struct iguana_peermsgrequest *msg; int32_t height,len,flag = 0; bits256 checktxid; struct iguana_txid *tx,T; struct iguana_peer *addr; if ( (msg= queue_dequeue(&coin->msgrequestQ,0)) != 0 ) { flag = 1; if ( msg->addr != 0 ) { //char str[65]; printf("send type.%d %s -> (%s)\n",msg->type,bits256_str(str,msg->hash2),msg->addr->ipaddr); if ( msg->type == MSG_BLOCK ) { if ( coin->RELAYNODE != 0 || coin->VALIDATENODE != 0 ) { if ( (addr= msg->addr) != 0 && (len= iguana_peerblockrequest(coin,coin->blockspace,(int32_t)(coin->blockspacesize - sizeof(struct iguana_msghdr)),0,msg->hash2,0)) > 0 ) { //char str[65]; printf("msg Sendlen.%d block %s to %s\n",len,bits256_str(str,msg->hash2),addr->ipaddr); iguana_queue_send(addr,0,coin->blockspace,"block",len); } } } else if ( msg->type == MSG_TX ) { if ( coin->RELAYNODE != 0 || coin->VALIDATENODE ) { if ( (tx= iguana_txidfind(coin,&height,&T,msg->hash2,coin->bundlescount-1)) != 0 ) { if ( (len= iguana_ramtxbytes(coin,&coin->blockspace[sizeof(struct iguana_msghdr)],coin->blockspacesize,&checktxid,tx,height,0,0,0)) > 0 ) { char str[65],str2[65]; if ( bits256_cmp(msg->hash2,checktxid) == 0 ) iguana_queue_send(msg->addr,0,coin->blockspace,"block",len); else printf("checktxid mismatch (%s) != (%s)\n",bits256_str(str,msg->hash2),bits256_str(str2,checktxid)); } } } } else if ( msg->type == MSG_FILTERED_BLOCK ) { } else if ( msg->type == MSG_BUNDLE_HEADERS ) { } else if ( msg->type == MSG_BUNDLE ) { } /*else if ( msg->type == MSG_QUOTE ) { if ( (len= instantdex_quoterequest(myinfo,coin,&coin->blockspace[sizeof(struct iguana_msghdr)],coin->blockspacesize,msg->addr,msg->hash2)) > 0 ) { //iguana_sethdr((void *)coin->blockspace,coin->chain->netmagic,"quote",&coin->blockspace[sizeof(struct iguana_msghdr)],len); //iguana_msgparser(coin,msg->addr,0,0,0,(void *)coin->blockspace,&coin->blockspace[sizeof(struct iguana_msghdr)],len); iguana_queue_send(msg->addr,0,coin->blockspace,"quote",len); } }*/ } free(msg); } return(flag); }
cJSON *iguana_blockjson(struct supernet_info *myinfo,struct iguana_info *coin,struct iguana_block *block,int32_t txidsflag) { char str[65],hexstr[1024]; int32_t i,len,size; struct iguana_txid *tx,T; struct iguana_msgzblock zmsg; struct iguana_msgblock *msg = (void *)&zmsg; struct iguana_zblock *zblock; bits256 hash2,nexthash2; uint8_t serialized[1024]; cJSON *array,*json = cJSON_CreateObject(); jaddstr(json,"result","success"); jaddstr(json,"hash",bits256_str(str,block->RO.hash2)); jaddnum(json,"height",block->height); //jaddnum(json,"ipbits",block->fpipbits); jaddstr(json,"merkleroot",bits256_str(str,block->RO.merkle_root)); jaddstr(json,"previousblockhash",bits256_str(str,block->RO.prev_block)); if ( block->height > 0 ) { nexthash2 = iguana_blockhash(coin,block->height+1); if ( bits256_nonz(nexthash2) != 0 ) jaddstr(json,"nextblockhash",bits256_str(str,nexthash2)); } jaddnum(json,"timestamp",block->RO.timestamp); jaddstr(json,"utc",utc_str(str,block->RO.timestamp)); jaddnum(json,"nonce",block->RO.nonce); jaddnum(json,"version",block->RO.version); jaddnum(json,"numvouts",block->RO.numvouts); jaddnum(json,"numvins",block->RO.numvins); jaddnum(json,"recvlen",block->RO.recvlen); jaddnum(json,"hdrsi",block->hdrsi); jaddnum(json,"difficulty",PoW_from_compact(block->RO.bits,coin->chain->unitval)); jaddnum(json,"bundlei",block->bundlei); jaddnum(json,"mainchain",block->mainchain); jaddnum(json,"valid",block->valid); jaddnum(json,"txn_count",block->RO.txn_count); jaddnum(json,"nBits",block->RO.bits); serialized[0] = ((uint8_t *)&block->RO.bits)[3]; serialized[1] = ((uint8_t *)&block->RO.bits)[2]; serialized[2] = ((uint8_t *)&block->RO.bits)[1]; serialized[3] = ((uint8_t *)&block->RO.bits)[0]; init_hexbytes_noT(hexstr,serialized,sizeof(uint32_t)); jaddstr(json,"nBitshex",hexstr); if ( block->RO.allocsize == sizeof(struct iguana_zblock) ) { zblock = (void *)block; memset(&zmsg,0,sizeof(zmsg)); zmsg.zH.version = zblock->RO.version; zmsg.zH.merkle_root = zblock->RO.merkle_root; zmsg.zH.timestamp = zblock->RO.timestamp; zmsg.zH.bits = zblock->RO.bits; zmsg.zH.bignonce = zblock->zRO.bignonce; if ( iguana_rwvarint32(1,zmsg.zH.var_numelements,&zblock->zRO.numelements) != sizeof(zmsg.zH.var_numelements) ) printf("unexpected varint size for zmsg.zH.numelements <- %d\n",zblock->zRO.numelements); for (i=0; i<ZCASH_SOLUTION_ELEMENTS; i++) zmsg.zH.solution[i] = zblock->zRO.solution[i]; zmsg.txn_count = 0;//block->RO.txn_count; len = iguana_rwblock(myinfo,coin->symbol,coin->chain->zcash,coin->chain->auxpow,coin->chain->hashalgo,1,&hash2,serialized,&zmsg,IGUANA_MAXPACKETSIZE*2); } else { memset(msg,0,sizeof(&msg)); msg->H.version = block->RO.version; msg->H.prev_block = block->RO.prev_block; msg->H.merkle_root = block->RO.merkle_root; msg->H.timestamp = block->RO.timestamp; msg->H.bits = block->RO.bits; msg->H.nonce = block->RO.nonce; msg->txn_count = 0;//block->RO.txn_count; len = iguana_rwblock(myinfo,coin->symbol,coin->chain->zcash,coin->chain->auxpow,coin->chain->hashalgo,1,&hash2,serialized,&zmsg,IGUANA_MAXPACKETSIZE*2); } init_hexbytes_noT(hexstr,serialized,len); jaddstr(json,"blockheader",hexstr); if ( txidsflag != 0 ) { array = cJSON_CreateArray(); for (i=0; i<block->RO.txn_count; i++) { if ( (tx= iguana_blocktx(coin,&T,block,i)) != 0 ) jaddistr(array,bits256_str(str,tx->txid)); } jadd(json,"tx",array); //printf("add txids[%d]\n",block->txn_count); } if ( (size= iguana_peerblockrequest(myinfo,coin,coin->blockspace,coin->blockspacesize,0,block->RO.hash2,0)) < 0 ) jaddstr(json,"error","couldnt generate raw bytes for block"); else jaddnum(json,"size",size); return(json); }