Esempio n. 1
0
int32_t dpow_datahandler(struct supernet_info *myinfo,struct dpow_info *dp,struct dpow_block *bp,uint8_t nn_senderind,uint32_t channel,uint32_t height,uint8_t *data,int32_t datalen)
{
    int32_t i,src_or_dest,myind = -1; bits256 txid,srchash; struct iguana_info *coin; char str[65],str2[65];
    memset(srchash.bytes,0,sizeof(srchash));
    dpow_notaryfind(myinfo,bp,height,&myind,dp->minerkey33);
    if ( myind < 0 )
    {
        //printf("couldnt find myind height.%d | this means your pubkey for this node is not registered and needs to be ratified by majority vote of all notaries\n",height);
        return(-1);
    }
    for (i=0; i<32; i++)
        srchash.bytes[i] = dp->minerkey33[i+1];
    if ( channel == DPOW_TXIDCHANNEL || channel == DPOW_BTCTXIDCHANNEL )
    {
        src_or_dest = (channel == DPOW_BTCTXIDCHANNEL);
        coin = (src_or_dest != 0) ? bp->destcoin : bp->srccoin;
        //printf("bp.%p datalen.%d\n",bp,datalen);
        for (i=0; i<32; i++)
            srchash.bytes[i] = data[i];
        txid = bits256_doublesha256(0,&data[32],datalen-32);
        init_hexbytes_noT(bp->signedtx,&data[32],datalen-32);
        if ( bits256_cmp(txid,srchash) == 0 )
        {
            //printf("verify (%s) it is properly signed! set ht.%d signedtxid to %s\n",coin->symbol,height,bits256_str(str,txid));
            /*if ( channel == DPOW_BTCTXIDCHANNEL )
            {
                if ( bp->state < 1000 )
                {
                    bp->desttxid = txid;
                    bp->state = 1000;
                    dp->destupdated = 0;
                    dpow_signedtxgen(myinfo,dp,bp->srccoin,bp,bp->bestk,bp->bestmask,myind,DPOW_SIGCHANNEL,0,bp->isratify);
                    //dpow_sigscheck(myinfo,dp,bp,DPOW_SIGCHANNEL,myind,0);
                }
            }
            else
            {
                if ( bp->state != 0xffffffff )
                {
                    bp->srctxid = txid;
                    printf("set state elapsed %d COMPLETED %s.(%s) %s.(%s)\n",(int32_t)(time(NULL) - bp->starttime),dp->symbol,bits256_str(str,bp->desttxid),dp->dest,bits256_str(str2,txid));
                    bp->state = 0xffffffff;
                }
            }*/
        }
        else
        {
            init_hexbytes_noT(bp->signedtx,data,datalen);
            printf("txidchannel txid %s mismatch %s (%s)\n",bits256_str(str,txid),bits256_str(str2,srchash),bp->signedtx);
            bp->signedtx[0] = 0;
        }
    } //else printf("unhandled channel.%x\n",channel);
    return(0);
}
Esempio n. 2
0
char *instantdex_sendcmd(struct supernet_info *myinfo,cJSON *argjson,char *cmdstr,char *ipaddr,int32_t hops)
{
    char *reqstr,hexstr[8192]; uint8_t _msg[4096]; uint64_t nxt64bits; int32_t datalen;
    bits256 instantdexhash; struct instantdex_msghdr *msg;
    msg = (struct instantdex_msghdr *)_msg;
    memset(msg,0,sizeof(*msg));
    instantdexhash = calc_categoryhashes(0,"InstantDEX",0);
    category_subscribe(myinfo,instantdexhash,GENESIS_PUBKEY);
    if ( ipaddr == 0 || ipaddr[0] == 0 || strncmp(ipaddr,"127.0.0.1",strlen("127.0.0.1")) == 0 )
        return(clonestr("{\"error\":\"no ipaddr, need to send your ipaddr for now\"}"));
    jaddstr(argjson,"cmd",cmdstr);
    jaddstr(argjson,"agent","SuperNET");
    jaddstr(argjson,"method","DHT");
    jaddstr(argjson,"traderip",ipaddr);
    jaddbits256(argjson,"categoryhash",instantdexhash);
    jaddbits256(argjson,"traderpub",myinfo->myaddr.persistent);
    nxt64bits = acct777_nxt64bits(myinfo->myaddr.persistent);
    reqstr = jprint(argjson,1);
    datalen = (int32_t)(strlen(reqstr) + 1);
    memcpy(msg->serialized,reqstr,datalen);
    free(reqstr);
    if ( (datalen+sizeof(*msg))*2+1 < sizeof(hexstr) && instantdex_msgcreate(myinfo,msg,datalen) != 0 )
    {
        printf("instantdex send.(%s)\n",cmdstr);
        init_hexbytes_noT(hexstr,(uint8_t *)msg,msg->sig.allocsize);
        return(SuperNET_categorymulticast(myinfo,0,instantdexhash,GENESIS_PUBKEY,hexstr,0,hops,1));
    }
    else
    {
        printf("cant msgcreate\n");
        return(clonestr("{\"error\":\"couldnt create instantdex message\"}"));
    }
}
Esempio n. 3
0
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));
}
Esempio n. 4
0
HASH_AND_INT(ramchain,getblock,blockhash,remoteonly)
{
    int32_t len; char hexstr[(sizeof(uint32_t)+sizeof(struct iguana_msgblock))*2+1],*blockstr;
    uint8_t serialized[sizeof(uint32_t)+sizeof(struct iguana_msgblock)]; bits256 hash2,txid;
    struct iguana_msgblock msg; struct iguana_block *block;
    cJSON *retjson = cJSON_CreateObject();
    memset(&msg,0,sizeof(msg));
    if ( remoteonly == 0 && (block= iguana_blockfind(coin,blockhash)) != 0 )
    {
        msg.H.version = block->RO.version;
        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 = block->RO.txn_count;
        len = iguana_rwblock(1,&hash2,serialized,&msg);
        char str[65]; printf("timestamp.%u bits.%u nonce.%u v.%d (%s) len.%d (%ld %ld)\n",block->RO.timestamp,block->RO.bits,block->RO.nonce,block->RO.version,bits256_str(str,hash2),len,sizeof(serialized),sizeof(hexstr));
        init_hexbytes_noT(hexstr,serialized,len);
        jaddstr(retjson,"result",hexstr);
    }
    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));
}
Esempio n. 5
0
char *iguana_txscan(struct iguana_info *coin,cJSON *json,uint8_t *data,int32_t recvlen,bits256 txid)
{
    struct iguana_msgtx tx; bits256 hash2; struct iguana_block block; struct iguana_msgblock msg;
    int32_t i,n,len; char *txbytes,vpnstr[64];
    memset(&msg,0,sizeof(msg));
    vpnstr[0] = 0;
    len = iguana_rwblock(0,&hash2,data,&msg);
    iguana_blockconv(&block,&msg,hash2,-1);
    for (i=0; i<msg.txn_count; i++)
    {
        if ( (n= iguana_rwmsgtx(coin,0,0,&data[len],recvlen - len,&tx,&tx.txid,vpnstr)) < 0 )
            break;
        //char str[65]; printf("%d of %d: %s\n",i,msg.txn_count,bits256_str(str,tx.txid));
        if ( bits256_cmp(txid,tx.txid) == 0 )
        {
            if ( (n= iguana_rwmsgtx(coin,0,json,&data[len],recvlen - len,&tx,&tx.txid,vpnstr)) > 0 )
            {
                txbytes = malloc(n*2+1);
                init_hexbytes_noT(txbytes,&data[len],n);
                return(txbytes);
            }
        }
        len += n;
    }
    return(0);
}
Esempio n. 6
0
int32_t crypto777_link(uint64_t mynxt64bits,struct crypto777_link *conn,bits384 nxtpass,uint64_t other64bits,int32_t numchallenges,uint32_t range)
{
    char NXTACCTSECRET[1024];
    int32_t haspubkey = 0;
    uint64_t nxt64bits = 0;
    if ( conn->my64bits != mynxt64bits || conn->shared_curve25519.txid == 0 )
    {
        conn->my64bits = mynxt64bits;
        init_hexbytes_noT(NXTACCTSECRET,nxtpass.bytes,sizeof(nxtpass));
        conn->shared_curve25519 = calc_sharedsecret(&nxt64bits,&haspubkey,(uint8_t *)NXTACCTSECRET,(int32_t)strlen(NXTACCTSECRET),other64bits);
        if ( haspubkey != 0 )
        {
            crypto777_user(&conn->send,nxt64bits,numchallenges,range);
            //conn->send.supernonce = (uint32_t)time(NULL);
            crypto777_user(&conn->recv,other64bits,0,0);
            conn->send.shared_SaM.sig = conn->recv.shared_SaM.sig = conn->shared_curve25519; // jl777: until quantum resistant is ready
            return(0);
        } else printf("crypto777_link cant create connection to %llu without pubkey\n",(long long)other64bits);
        return(-1);
    }
    else
    {
        //printf("now.%lu prev.%u\n",time(NULL),conn->send.prevtimestamp);
        while ( time(NULL) == conn->send.prevtimestamp || time(NULL) == conn->recv.prevtimestamp )
            msleep(250);
        return(0);
    }
}
Esempio n. 7
0
void calc_md2(char *str,uint8_t *digest,uint8_t *message,int32_t len)
{
    int init_hexbytes_noT(char *hexbytes,unsigned char *message,long len);
    hash_state md;
    md2_init(&md);
    md2_process(&md,message,len);
    md2_done(&md,digest);
    if ( str != 0 )
        init_hexbytes_noT(str,digest,16);
}
Esempio n. 8
0
char *gecko_coinbasestr(struct supernet_info *myinfo,struct iguana_info *virt,bits256 *txidp,uint32_t timestamp,uint8_t *minerpubkey,uint64_t blockreward,uint8_t *data,int32_t datalen,bits256 coinbasespend)
{
    char *rawtx=0; uint8_t minerpayment[512],serialized[8192]; int32_t minerpaymentlen=0,len=0;
    if ( blockreward > 0 )
        minerpaymentlen = bitcoin_pubkeyspend(minerpayment,0,minerpubkey);
    len = iguana_coinbase(virt->chain->isPoS,virt->chain->normal_txversion,serialized,timestamp,coinbasespend,data,datalen,minerpayment,minerpaymentlen,blockreward,txidp);
    if ( len > 0 )
    {
        rawtx = malloc(len*2 + 1);
        init_hexbytes_noT(rawtx,serialized,len);
    }
    return(rawtx);
}
Esempio n. 9
0
int32_t iguana_pollQs(struct iguana_info *coin,struct iguana_peer *addr)
{
    uint8_t serialized[sizeof(struct iguana_msghdr) + sizeof(uint32_t)*32 + sizeof(bits256)];
    char *hashstr=0,hexstr[65]; bits256 hash2; int32_t height=-1,datalen,flag = 0;
    struct iguana_blockreq *req=0;
    if ( iguana_needhdrs(coin) != 0 && addr->pendhdrs == 0 && (hashstr= queue_dequeue(&coin->hdrsQ,1)) != 0 )
    {
        if ( (datalen= iguana_gethdrs(coin,serialized,coin->chain->gethdrsmsg,hashstr)) > 0 )
        {
            decode_hex(hash2.bytes,sizeof(hash2),hashstr);
            //printf("%s request hdr.(%s) %d pend.%d\n",addr->ipaddr,hashstr,iguana_itemheight(coin,hash2),addr->pendhdrs);
            iguana_send(coin,addr,serialized,datalen);
            addr->pendhdrs++;
            flag++;
        } else printf("datalen.%d from gethdrs\n",datalen);
        free_queueitem(hashstr);
        hashstr = 0;
    }
    if ( ((req= queue_dequeue(&coin->priorityQ,0)) != 0 || (req= queue_dequeue(&coin->blocksQ,0)) != 0) )
    {
        hash2 = req->hash2;
        height = iguana_hash2height(coin,hash2);
        if ( req->height >= 0 && height != req->height )
        {
            printf("blocksQ ht.%d vs %d dequeued.(%s) for %s\n",req->height,height,bits256_str(hash2),addr->ipaddr);
            myfree(req,sizeof(*req));
            return(0);
        }
        if ( height >= 0 && (height < coin->blocks.recvblocks || iguana_blockptr(coin,height) != 0) )
        {
            printf("skip.%d vs recvblocks.%d %p\n",height,coin->blocks.recvblocks,iguana_blockptr(coin,height));
            myfree(req,sizeof(*req));
        }
        else
        {
            init_hexbytes_noT(hexstr,hash2.bytes,sizeof(hash2));
            if ( (datalen= iguana_getdata(coin,serialized,MSG_BLOCK,hexstr)) > 0 )
            {
                //printf("%s %s REQ BLOCK.%d\n",addr->ipaddr,hexstr,iguana_blockheight(coin,hash2));
                iguana_send(coin,addr,serialized,datalen);
                //if ( height >= 0 )
                //    iguana_setwaitstart(coin,height);
                addr->pendblocks++;
                addr->pendtime = (uint32_t)time(NULL);
                flag++;
                myfree(req,sizeof(*req));
            } else printf("error constructing request %s.%d\n",hexstr,height);
        }
    }
    return(flag);
}
Esempio n. 10
0
dbobj ram_hashdata_search(int32_t createflag,struct ramchain_hashtable *hash,uint8_t *hashdata,int32_t datalen)
{
    char _key[8192],*key;
    dbobj obj = 0;
    if ( datalen > sizeof(key) )
        key = malloc(datalen+1);
    else key = _key;
    init_hexbytes_noT(key,hashdata,datalen);
    if ( hash != 0 && (obj= db777_find(hash->DB,key)) == 0 && createflag != 0 )
        obj = ram_addhash(hash,key,hashdata,datalen);
    if ( key != _key )
        free(key);
    return(obj);
}
Esempio n. 11
0
int32_t ledger_txidstr(struct ledger_info *ledger,char *txidstr,int32_t max,uint32_t txidind)
{
    uint8_t txid[256],*ptr; int32_t size = sizeof(txid),retval = -1;
    if ( (ptr= db777_get(txid,&size,ledger->DBs.transactions,ledger->revtxids.DB,&txidind,sizeof(txidind))) != 0 )
    {
        if ( size < max/2 )
        {
            init_hexbytes_noT(txidstr,ptr,size);
            retval = 0;
        }
        else printf("txid.(%x) %d too long for %d\n",*ptr,size,max);
    }
    return(retval);
}
Esempio n. 12
0
int32_t ledger_scriptstr(struct ledger_info *ledger,char *scriptstr,int32_t max,uint32_t scriptind)
{
    uint8_t script[4096],*ptr; int32_t size = sizeof(script),retval = -1;
    if ( (ptr= db777_get(script,&size,ledger->DBs.transactions,ledger->revscripts.DB,&scriptind,sizeof(scriptind))) != 0 )
    {
        if ( size < max/2 )
        {
            init_hexbytes_noT(scriptstr,ptr,size);
            retval = 0;
        }
        else printf("script.(%x) %d too long for %d\n",*ptr,size,max);
    }
    return(retval);
}
Esempio n. 13
0
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 )
    {
        if ( valsobj != 0 && jobj(valsobj,"data") != 0 )
        {
            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);
        if ( valsobj != 0 )
            jaddstr(valsobj,"data",strbuf);
    } else return(0);
    return(strbuf);
}
Esempio n. 14
0
HASH_AND_INT(ramchain,getrawtransaction,txid,verbose)
{
    struct iguana_txid *tx,T; char *txbytes; bits256 checktxid; int32_t len,height; cJSON *retjson;
    if ( (tx= iguana_txidfind(coin,&height,&T,txid)) != 0 )
    {
        retjson = cJSON_CreateObject();
        if ( (len= iguana_txbytes(coin,coin->blockspace,sizeof(coin->blockspace),&checktxid,tx,height,0,0)) > 0 )
        {
            txbytes = mycalloc('x',1,len*2+1);
            init_hexbytes_noT(txbytes,coin->blockspace,len*2+1);
            jaddstr(retjson,"result",txbytes);
            myfree(txbytes,len*2+1);
            return(jprint(retjson,1));
        }
        else if ( height >= 0 )
        {
            if ( coin->APIblockstr != 0 )
                jaddstr(retjson,"error","already have pending request");
            else
            {
                int32_t datalen; uint8_t *data; char *blockstr; bits256 blockhash;
                blockhash = iguana_blockhash(coin,height);
                if ( (blockstr= iguana_APIrequest(coin,blockhash,txid,2)) != 0 )
                {
                    datalen = (int32_t)(strlen(blockstr) >> 1);
                    data = malloc(datalen);
                    decode_hex(data,datalen,blockstr);
                    if ( (txbytes= iguana_txscan(coin,verbose != 0 ? retjson : 0,data,datalen,txid)) != 0 )
                    {
                        jaddstr(retjson,"result",txbytes);
                        jaddbits256(retjson,"blockhash",blockhash);
                        jaddnum(retjson,"height",height);
                        free(txbytes);
                    } else jaddstr(retjson,"error","cant find txid in block");
                    free(blockstr);
                    free(data);
                } else jaddstr(retjson,"error","cant find blockhash");
                return(jprint(retjson,1));
            }
Esempio n. 15
0
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);
}
Esempio n. 16
0
void SuperNET_hex2str(char *str,uint8_t *hex,int32_t len)
{
    init_hexbytes_noT(str,hex,len);
}
Esempio n. 17
0
uint64_t call_SuperNET_broadcast(struct pserver_info *pserver,char *msg,int32_t len,int32_t duration)
{
    int32_t SuperNET_broadcast(char *msg,int32_t duration);
    int32_t SuperNET_narrowcast(char *destip,unsigned char *msg,int32_t len);
    char ip_port[64],*ptr;
    uint64_t txid = 0;
    int32_t port;
    if ( 1 || SUPERNET_PORT != _SUPERNET_PORT )
        return(0);
    if ( Debuglevel > 1 )
        printf("call_SuperNET_broadcast.%p %p len.%d\n",pserver,msg,len);
    txid = calc_txid((uint8_t *)msg,(int32_t)strlen(msg));
    if ( pserver != 0 )
    {
        port = (pserver->p2pport == 0) ? BTCD_PORT : pserver->p2pport;
        //fprintf(stderr,"port.%d\n",port);
        sprintf(ip_port,"%s:%d",pserver->ipaddr,port);
        txid ^= calc_ipbits(pserver->ipaddr);
        if ( Debuglevel > 1 )
        {
            char debugstr[4096];
            init_hexbytes_noT(debugstr,(uint8_t *)msg,len);
            debugstr[32] = 0;
            fprintf(stderr,"%s NARROWCAST.(%s) txid.%llu (%s)\n",pserver->ipaddr,debugstr,(long long)txid,ip_port);
        }
        ptr = calloc(1,64 + sizeof(len) + len + 1);
        memcpy(ptr,&len,sizeof(len));
        memcpy(&ptr[sizeof(len)],ip_port,strlen(ip_port));
        memcpy(&ptr[sizeof(len) + 64],msg,len);
        queue_enqueue(&NarrowQ,ptr);
        return(txid);
    }
    else
    {
        char *cmdstr,NXTaddr[64];
        cJSON *array;
        int32_t valid;
        array = cJSON_Parse(msg);
        if ( array != 0 )
        {
            cmdstr = verify_tokenized_json(0,NXTaddr,&valid,array);
            if ( cmdstr != 0 )
                free(cmdstr);
            free_json(array);
            if ( Debuglevel > 1 )
            {
                char debugstr[4096];
                init_hexbytes_noT(debugstr,(uint8_t *)msg,len);
                debugstr[32] = 0;
                printf("BROADCAST parms.(%s) valid.%d duration.%d txid.%llu len.%d\n",debugstr,valid,duration,(long long)txid,len);
            }
            ptr = calloc(1,sizeof(len) + sizeof(duration) + len);
            memcpy(ptr,&len,sizeof(len));
            memcpy(&ptr[sizeof(len)],&duration,sizeof(duration));
            memcpy(&ptr[sizeof(len) + sizeof(duration)],msg,len);
            ptr[sizeof(len) + sizeof(duration) + len] = 0;
            queue_enqueue(&BroadcastQ,ptr);
            return(txid);
        } else printf("cant broadcast non-JSON.(%s)\n",msg);
    }
    return(txid);
}
Esempio n. 18
0
cJSON *pangea_handitem(int32_t *cardip,cJSON **pitemp,uint8_t type,uint64_t valA,uint64_t *bits64p,bits256 card,int32_t numplayers)
{
    int32_t cardi,n,i,rebuy,busted; char str[128],hexstr[65],cardpubs[(CARDS777_MAXCARDS+1)*64+1]; cJSON *item,*array,*pitem = 0;
    item = cJSON_CreateObject();
    *cardip = -1;
    switch ( type )
    {
        case CARDS777_START:
            jaddnum(item,"handid",valA);
            init_hexbytes_noT(cardpubs,(void *)bits64p,(int32_t)((CARDS777_MAXCARDS+1) * sizeof(bits256)));
            jaddstr(item,"cardpubs",cardpubs);
            break;
        case CARDS777_RAKES:
            jaddnum(item,"hostrake",dstr(valA));
            jaddnum(item,"pangearake",dstr(*bits64p));
            break;
        case CARDS777_SNAPSHOT:
            jaddnum(item,"handid",valA);
            array = cJSON_CreateArray();
            for (i=0; i<CARDS777_MAXPLAYERS; i++)
            {
                if ( i < numplayers )
                    jaddinum(array,dstr(bits64p[i]));
                else jaddinum(array,dstr(0));
            }
            jadd(item,"snapshot",array);
            //printf("add snapshot for numplayers.%d\n",numplayers);
            break;
        case CARDS777_CHANGES:
            n = (int32_t)(valA & 0xf);
            busted = (int32_t)((valA>>4) & 0xffff);
            rebuy = (int32_t)((valA>>20) & 0xffff);
            if ( busted != 0 )
                jaddnum(item,"busted",busted);
            if ( rebuy != 0 )
                jaddnum(item,"rebuy",rebuy);
            array = cJSON_CreateArray();
            for (i=0; i<n; i++)
                jaddinum(array,dstr(bits64p[i]));
            jadd(item,"balances",array);
            break;
        case CARDS777_WINNINGS:
            if ( (int32_t)valA >= 0 && valA < numplayers )
                jaddnum(item,"player",valA);
            jaddnum(item,"won",dstr(*bits64p));
            if ( pitem == 0 )
                pitem = cJSON_CreateObject();
            jaddnum(pitem,"won",dstr(*bits64p));
            break;
        case CARDS777_FACEUP:
            *cardip = cardi = (int32_t)(valA >> 8);
            if ( cardi >= 0 && cardi < 52 )
                jaddnum(item,"cardi",cardi);
            else printf("illegal cardi.%d valA.%llu\n",cardi,(long long)valA);
            valA &= 0xff;
            if ( (int32_t)valA >= 0 && valA < numplayers )
                jaddnum(item,"player",valA);
            else if ( valA == 0xff )
                jaddnum(item,"community",cardi - numplayers*2);
            cardstr(str,card.bytes[1]);
            jaddnum(item,str,card.bytes[1]);
            init_hexbytes_noT(hexstr,card.bytes,sizeof(card));
            jaddstr(item,"privkey",hexstr);
            break;
        default:
            if ( (int32_t)valA >= 0 && valA < numplayers )
                jaddnum(item,"player",valA);
            jaddstr(item,"action",pangea_typestr(type));
            if ( pitem == 0 )
                pitem = cJSON_CreateObject();
            if ( *bits64p != 0 )
            {
                jaddnum(item,"bet",dstr(*bits64p));
                jaddnum(pitem,pangea_typestr(type),dstr(*bits64p));
            }
            else jaddstr(pitem,"action",pangea_typestr(type));
            break;
    }
    *pitemp = pitem;
    return(item);
}
Esempio n. 19
0
char *gecko_blockconstruct(struct supernet_info *myinfo,struct iguana_info *virt,struct iguana_block *newblock,uint32_t *noncep,struct gecko_memtx **txptrs,int32_t txn_count,uint8_t *coinbase,int32_t coinbaselen,bits256 coinbasespend,double expiration,uint8_t *minerpubkey,int64_t blockreward)
{
    struct iguana_info *btcd; uint8_t serialized[sizeof(*newblock)],space[16384]; int32_t i,n,len,totaltxlen=0; char *coinbasestr,str[65],str2[65],*blockstr=0; bits256 *txids=0,txspace[256],threshold,hash2; struct gecko_memtx *memtx;
    if ( (btcd= iguana_coinfind("BTCD")) == 0 )
    {
        printf("basilisk needs BTCD\n");
        return(0);
    }
    if ( txn_count+2 < sizeof(space)/sizeof(*space) )
    {
        txids = txspace;
        memset(txids,0,sizeof(*txids) * (txn_count+2));
    } else txids = calloc(txn_count+2,sizeof(*txids));
    if ( txn_count > 0 )
    {
        for (i=0; i<txn_count; i++)
        {
            if ( (memtx= txptrs[i]) != 0 )
            {
                totaltxlen += memtx->datalen;
                txids[i + 1] = memtx->txid;
                printf("memtxid.%s\n",bits256_str(str,memtx->txid));
            }
        }
    }
    if ( (coinbasestr= gecko_coinbasestr(myinfo,virt,&txids[0],newblock->RO.timestamp,minerpubkey,blockreward,coinbase,coinbaselen,coinbasespend)) != 0 )
    {
        newblock->RO.merkle_root = iguana_merkle(txids,txn_count + 1);
        newblock->RO.txn_count = (txn_count + 1);
        if ( txn_count > 0 )
        {
            printf("%s %s\n",bits256_str(str,txids[0]),bits256_str(str2,txids[1]));
        }
        if ( newblock->RO.bits >= GECKO_EASIESTDIFF )
            newblock->RO.bits = GECKO_EASIESTDIFF;
        threshold = bits256_from_compact(newblock->RO.bits);
        if ( (newblock->RO.nonce= *noncep) == 0 )
        {
            struct iguana_msgblock msg;
            memset(&msg,0,sizeof(msg));
            msg.H.version = newblock->RO.version;
            msg.H.prev_block = newblock->RO.prev_block;
            msg.H.merkle_root = newblock->RO.merkle_root;
            msg.H.timestamp = newblock->RO.timestamp;
            msg.H.bits = newblock->RO.bits;
            for (i=0; i<GECKO_MAXMINERITERS; i++)
            {
                OS_randombytes((void *)noncep,sizeof(*noncep));
                msg.H.nonce = *noncep;
                //n = iguana_serialize_block(virt->chain,&hash2,serialized,newblock);
                //char str[65]; printf("nonce.%08x %s\n",newblock->RO.nonce,bits256_str(str,newblock->RO.hash2));
                len = iguana_rwblockhdr(1,virt->chain->zcash,serialized,&msg);
                hash2 = iguana_calcblockhash(virt->symbol,virt->chain->hashalgo,serialized,len);
                if ( bits256_cmp(threshold,hash2) > 0 )
                {
                    //printf("FOUND NONCE %d iterations\n",i+1);
                    newblock->RO.hash2 = hash2;
                    break;
                }
                if ( newblock->height != 0 && OS_milliseconds() > expiration )
                {
                    //printf("time limit exceeded %u %d iterations\n",virt->blocks.hwmchain.RO.timestamp,i+1);
                    free(coinbasestr);
                    if ( txids != txspace )
                        free(txids);
                    return(0);
                }
            }
        }
        newblock->RO.nonce = *noncep;
        n = iguana_serialize_block(virt->chain,&newblock->RO.hash2,serialized,newblock);
        while ( 1 && time(NULL) <= newblock->RO.timestamp + GECKO_MAXFUTUREBLOCK )
        {
            //printf("wait for block to be close enough to now: lag %ld\n",time(NULL) - newblock->RO.timestamp);
            sleep(1);
        }
        //if ( gecko_blocknonce_verify(virt,serialized,n,newblock->RO.bits,newblock->RO.timestamp,virt->blocks.hwmchain.RO.timestamp) >= 0 )
        if ( bits256_cmp(threshold,newblock->RO.hash2) > 0 )
        {
            blockstr = calloc(1,strlen(coinbasestr) + (totaltxlen+n)*2 + 1);
            init_hexbytes_noT(blockstr,serialized,n);
            printf("block.(%s) coinbase.(%s) lens.%ld\n",blockstr,coinbasestr,(strlen(blockstr)+strlen(coinbasestr))/2);
            strcat(blockstr,coinbasestr);
            len = (int32_t)strlen(blockstr);
            for (i=0; i<txn_count; i++)
            {
                if ( (memtx= txptrs[i]) != 0 )
                {
                    init_hexbytes_noT(&blockstr[len],gecko_txdata(memtx),memtx->datalen);
                    len += memtx->datalen << 1;
                    printf(" txi.%d (%s)\n",i,&blockstr[len]);
                }
            }
        } else printf("nonce failure\n");
        free(coinbasestr);
    }
    if ( txids != txspace )
        free(txids);
    return(blockstr);
}