Skip to content

Commit

Permalink
Merge pull request jl777#1628 from jl777/nSPV
Browse files Browse the repository at this point in the history
NSPV
  • Loading branch information
jl777 authored Jul 22, 2019
2 parents f97a4a3 + 69ca7f1 commit 2ad296f
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 60 deletions.
10 changes: 6 additions & 4 deletions src/komodo_nSPV.h
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ int32_t NSPV_rwinforesp(int32_t rwflag,uint8_t *serialized,struct NSPV_inforesp
len += iguana_rwnum(rwflag,&serialized[len],sizeof(ptr->height),&ptr->height);
len += iguana_rwnum(rwflag,&serialized[len],sizeof(ptr->hdrheight),&ptr->hdrheight);
len += NSPV_rwequihdr(rwflag,&serialized[len],&ptr->H);
fprintf(stderr,"hdr rwlen.%d\n",len);
//fprintf(stderr,"hdr rwlen.%d\n",len);
return(len);
}

Expand All @@ -305,6 +305,7 @@ int32_t NSPV_rwtxproof(int32_t rwflag,uint8_t *serialized,struct NSPV_txproof *p
len += iguana_rwnum(rwflag,&serialized[len],sizeof(ptr->vout),&ptr->vout);
len += iguana_rwuint8vec(rwflag,&serialized[len],&ptr->txlen,&ptr->tx);
len += iguana_rwuint8vec(rwflag,&serialized[len],&ptr->txprooflen,&ptr->txproof);
fprintf(stderr,"len %d\n",len);
return(len);
}

Expand Down Expand Up @@ -493,14 +494,15 @@ int32_t NSPV_fastnotariescount(CTransaction tx,uint8_t elected[64][33],uint32_t
continue;
char coinaddr[64]; Getscriptaddress(coinaddr,scriptPubKeys[j]);
NSPV_SignTx(mtx,vini,10000,scriptPubKeys[j],nTime); // sets SIG_TXHASH
//fprintf(stderr,"%s ",SIG_TXHASH.GetHex().c_str());
if ( (retval= pubkeys[j].Verify(SIG_TXHASH,vData[0])) != 0 )
{
fprintf(stderr,"(vini.%d %s.%d) ",vini,coinaddr,retval);
//fprintf(stderr,"(vini.%d %s.%d) ",vini,coinaddr,retval);
mask |= (1LL << j);
break;
}
}
fprintf(stderr," verified %llx\n",(long long)mask);
//fprintf(stderr," vini.%d verified %llx\n",vini,(long long)mask);
}
}
return(bitweight(mask));
Expand Down Expand Up @@ -571,7 +573,7 @@ int32_t NSPV_notarizationextract(int32_t verifyntz,int32_t *ntzheightp,uint256 *
{
fprintf(stderr,"numsigs.%d error\n",numsigs);
return(-3);
}
}
return(0);
}
else
Expand Down
2 changes: 1 addition & 1 deletion src/komodo_nSPV_defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#define KOMODO_NSPV_DEFSH

#define NSPV_POLLITERS 100
#define NSPV_POLLMICROS 50000
#define NSPV_POLLMICROS 10000
#define NSPV_MAXVINS 64
#define NSPV_AUTOLOGOUT 777
#define NSPV_BRANCHID 0x76b809bb
Expand Down
62 changes: 36 additions & 26 deletions src/komodo_nSPV_fullnode.h
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,10 @@ uint8_t *NSPV_getrawtx(CTransaction &tx,uint256 &hashBlock,int32_t *txlenp,uint2
*txlenp = 0;
{
if (!GetTransaction(txid, tx, hashBlock, false))
{
//fprintf(stderr,"error getting transaction %s\n",txid.GetHex().c_str());
return(0);
}
string strHex = EncodeHexTx(tx);
*txlenp = (int32_t)strHex.size() >> 1;
if ( *txlenp > 0 )
Expand Down Expand Up @@ -405,33 +408,38 @@ int32_t NSPV_gettxproof(struct NSPV_txproof *ptr,int32_t vout,uint256 txid,int32
return(-1);
ptr->txid = txid;
ptr->vout = vout;
ptr->height = height;
if ( height != 0 && (pindex= komodo_chainactive(height)) != 0 && komodo_blockload(block,pindex) == 0 )
if ( height == 0 )
ptr->height = komodo_blockheight(hashBlock);
else
{
BOOST_FOREACH(const CTransaction&tx, block.vtx)
ptr->height = height;
if ((pindex= komodo_chainactive(height)) != 0 && komodo_blockload(block,pindex) == 0 )
{
if ( tx.GetHash() == txid )
BOOST_FOREACH(const CTransaction&tx, block.vtx)
{
flag = 1;
break;
if ( tx.GetHash() == txid )
{
flag = 1;
break;
}
}
}
if ( flag != 0 )
{
set<uint256> setTxids;
CDataStream ssMB(SER_NETWORK, PROTOCOL_VERSION);
setTxids.insert(txid);
CMerkleBlock mb(block, setTxids);
ssMB << mb;
std::vector<uint8_t> proof(ssMB.begin(), ssMB.end());
ptr->txprooflen = (int32_t)proof.size();
//fprintf(stderr,"%s txproof.(%s)\n",txid.GetHex().c_str(),HexStr(proof).c_str());
if ( ptr->txprooflen > 0 )
if ( flag != 0 )
{
ptr->txproof = (uint8_t *)calloc(1,ptr->txprooflen);
memcpy(ptr->txproof,&proof[0],ptr->txprooflen);
set<uint256> setTxids;
CDataStream ssMB(SER_NETWORK, PROTOCOL_VERSION);
setTxids.insert(txid);
CMerkleBlock mb(block, setTxids);
ssMB << mb;
std::vector<uint8_t> proof(ssMB.begin(), ssMB.end());
ptr->txprooflen = (int32_t)proof.size();
//fprintf(stderr,"%s txproof.(%s)\n",txid.GetHex().c_str(),HexStr(proof).c_str());
if ( ptr->txprooflen > 0 )
{
ptr->txproof = (uint8_t *)calloc(1,ptr->txprooflen);
memcpy(ptr->txproof,&proof[0],ptr->txprooflen);
}
//fprintf(stderr,"gettxproof slen.%d\n",(int32_t)(sizeof(*ptr) - sizeof(ptr->tx) - sizeof(ptr->txproof) + ptr->txlen + ptr->txprooflen));
}
//fprintf(stderr,"gettxproof slen.%d\n",(int32_t)(sizeof(*ptr) - sizeof(ptr->tx) - sizeof(ptr->txproof) + ptr->txlen + ptr->txprooflen));
}
}
ptr->unspentvalue = CCgettxout(txid,vout,1,1);
Expand Down Expand Up @@ -520,16 +528,16 @@ void komodo_nSPVreq(CNode *pfrom,std::vector<uint8_t> request) // received a req
if ( len == 1+sizeof(reqheight) )
iguana_rwnum(0,&request[1],sizeof(reqheight),&reqheight);
else reqheight = 0;
fprintf(stderr,"request height.%d\n",reqheight);
//fprintf(stderr,"request height.%d\n",reqheight);
memset(&I,0,sizeof(I));
if ( (slen= NSPV_getinfo(&I,reqheight)) > 0 )
{
response.resize(1 + slen);
response[0] = NSPV_INFORESP;
fprintf(stderr,"slen.%d\n",slen);
//fprintf(stderr,"slen.%d\n",slen);
if ( NSPV_rwinforesp(1,&response[1],&I) == slen )
{
fprintf(stderr,"send info resp to id %d\n",(int32_t)pfrom->id);
//fprintf(stderr,"send info resp to id %d\n",(int32_t)pfrom->id);
pfrom->PushMessage("nSPV",response);
pfrom->prevtimes[ind] = timestamp;
}
Expand Down Expand Up @@ -706,16 +714,18 @@ void komodo_nSPVreq(CNode *pfrom,std::vector<uint8_t> request) // received a req
memset(&P,0,sizeof(P));
if ( (slen= NSPV_gettxproof(&P,vout,txid,height)) > 0 )
{
//fprintf(stderr,"slen.%d\n",slen);
response.resize(1 + slen);
response[0] = NSPV_TXPROOFRESP;
if ( NSPV_rwtxproof(1,&response[1],&P) == slen )
{
//fprintf(stderr,"send response\n");
pfrom->PushMessage("nSPV",response);
pfrom->prevtimes[ind] = timestamp;
}
NSPV_txproof_purge(&P);
}
}
} else fprintf(stderr,"gettxproof error.%d\n",slen);
} else fprintf(stderr,"txproof reqlen.%d\n",len);
}
}
else if ( request[0] == NSPV_SPENTINFO )
Expand Down
43 changes: 23 additions & 20 deletions src/komodo_nSPV_superlite.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ struct NSPV_ntzsresp *NSPV_ntzsresp_add(struct NSPV_ntzsresp *ptr)
if ( NSPV_ntzsresp_cache[i].reqheight == 0 )
break;
if ( i == sizeof(NSPV_ntzsresp_cache)/sizeof(*NSPV_ntzsresp_cache) )
i == (rand() % (sizeof(NSPV_ntzsresp_cache)/sizeof(*NSPV_ntzsresp_cache)));
i = (rand() % (sizeof(NSPV_ntzsresp_cache)/sizeof(*NSPV_ntzsresp_cache)));
NSPV_ntzsresp_purge(&NSPV_ntzsresp_cache[i]);
NSPV_ntzsresp_copy(&NSPV_ntzsresp_cache[i],ptr);
fprintf(stderr,"ADD CACHE ntzsresp req.%d\n",ptr->reqheight);
Expand Down Expand Up @@ -98,7 +98,7 @@ struct NSPV_txproof *NSPV_txproof_add(struct NSPV_txproof *ptr)
if ( NSPV_txproof_cache[i].txlen == 0 )
break;
if ( i == sizeof(NSPV_txproof_cache)/sizeof(*NSPV_txproof_cache) )
i == (rand() % (sizeof(NSPV_txproof_cache)/sizeof(*NSPV_txproof_cache)));
i = (rand() % (sizeof(NSPV_txproof_cache)/sizeof(*NSPV_txproof_cache)));
NSPV_txproof_purge(&NSPV_txproof_cache[i]);
NSPV_txproof_copy(&NSPV_txproof_cache[i],ptr);
fprintf(stderr,"ADD CACHE txproof %s\n",ptr->txid.GetHex().c_str());
Expand All @@ -121,7 +121,7 @@ struct NSPV_ntzsproofresp *NSPV_ntzsproof_add(struct NSPV_ntzsproofresp *ptr)
if ( NSPV_ntzsproofresp_cache[i].common.hdrs == 0 )
break;
if ( i == sizeof(NSPV_ntzsproofresp_cache)/sizeof(*NSPV_ntzsproofresp_cache) )
i == (rand() % (sizeof(NSPV_ntzsproofresp_cache)/sizeof(*NSPV_ntzsproofresp_cache)));
i = (rand() % (sizeof(NSPV_ntzsproofresp_cache)/sizeof(*NSPV_ntzsproofresp_cache)));
NSPV_ntzsproofresp_purge(&NSPV_ntzsproofresp_cache[i]);
NSPV_ntzsproofresp_copy(&NSPV_ntzsproofresp_cache[i],ptr);
fprintf(stderr,"ADD CACHE ntzsproof %s %s\n",ptr->prevtxid.GetHex().c_str(),ptr->nexttxid.GetHex().c_str());
Expand Down Expand Up @@ -544,11 +544,11 @@ UniValue NSPV_login(char *wifstr)

UniValue NSPV_getinfo_req(int32_t reqht)
{
uint8_t msg[64]; int32_t i,iter,len = 0; struct NSPV_inforesp I;
uint8_t msg[512]; int32_t i,iter,len = 0; struct NSPV_inforesp I;
NSPV_inforesp_purge(&NSPV_inforesult);
msg[len++] = NSPV_INFO;
len += iguana_rwnum(1,&msg[len],sizeof(reqht),&reqht);
for (iter=0; iter<3; iter++);
for (iter=0; iter<3; iter++)
if ( NSPV_req(0,msg,len,NODE_NSPV,msg[0]>>1) != 0 )
{
for (i=0; i<NSPV_POLLITERS; i++)
Expand Down Expand Up @@ -582,7 +582,7 @@ uint32_t NSPV_blocktime(int32_t hdrheight)

UniValue NSPV_addressutxos(char *coinaddr,int32_t CCflag,int32_t skipcount)
{
UniValue result(UniValue::VOBJ); uint8_t msg[64]; int32_t i,iter,slen,len = 0;
UniValue result(UniValue::VOBJ); uint8_t msg[512]; int32_t i,iter,slen,len = 0;
//fprintf(stderr,"utxos %s NSPV addr %s\n",coinaddr,NSPV_address.c_str());
if ( NSPV_utxosresult.nodeheight >= NSPV_inforesult.height && strcmp(coinaddr,NSPV_utxosresult.coinaddr) == 0 && CCflag == NSPV_utxosresult.CCflag && skipcount == NSPV_utxosresult.skipcount )
return(NSPV_utxosresp_json(&NSPV_utxosresult));
Expand All @@ -601,7 +601,7 @@ UniValue NSPV_addressutxos(char *coinaddr,int32_t CCflag,int32_t skipcount)
memcpy(&msg[len],coinaddr,slen), len += slen;
msg[len++] = (CCflag != 0);
len += iguana_rwnum(1,&msg[len],sizeof(skipcount),&skipcount);
for (iter=0; iter<3; iter++);
for (iter=0; iter<3; iter++)
if ( NSPV_req(0,msg,len,NODE_ADDRINDEX,msg[0]>>1) != 0 )
{
for (i=0; i<NSPV_POLLITERS; i++)
Expand All @@ -619,7 +619,7 @@ UniValue NSPV_addressutxos(char *coinaddr,int32_t CCflag,int32_t skipcount)

UniValue NSPV_addresstxids(char *coinaddr,int32_t CCflag,int32_t skipcount)
{
UniValue result(UniValue::VOBJ); uint8_t msg[64]; int32_t i,iter,slen,len = 0;
UniValue result(UniValue::VOBJ); uint8_t msg[512]; int32_t i,iter,slen,len = 0;
if ( NSPV_txidsresult.nodeheight >= NSPV_inforesult.height && strcmp(coinaddr,NSPV_txidsresult.coinaddr) == 0 && CCflag == NSPV_txidsresult.CCflag && skipcount == NSPV_txidsresult.skipcount )
return(NSPV_txidsresp_json(&NSPV_txidsresult));
if ( skipcount < 0 )
Expand All @@ -638,7 +638,7 @@ UniValue NSPV_addresstxids(char *coinaddr,int32_t CCflag,int32_t skipcount)
msg[len++] = (CCflag != 0);
len += iguana_rwnum(1,&msg[len],sizeof(skipcount),&skipcount);
//fprintf(stderr,"skipcount.%d\n",skipcount);
for (iter=0; iter<3; iter++);
for (iter=0; iter<3; iter++)
if ( NSPV_req(0,msg,len,NODE_ADDRINDEX,msg[0]>>1) != 0 )
{
for (i=0; i<NSPV_POLLITERS; i++)
Expand All @@ -656,8 +656,11 @@ UniValue NSPV_addresstxids(char *coinaddr,int32_t CCflag,int32_t skipcount)

UniValue NSPV_mempooltxids(char *coinaddr,int32_t CCflag,uint8_t funcid,uint256 txid,int32_t vout)
{
UniValue result(UniValue::VOBJ); uint8_t msg[512]; int32_t i,iter,slen,len = 0;
UniValue result(UniValue::VOBJ); uint8_t msg[512]; char zeroes[64]; int32_t i,iter,slen,len = 0;
NSPV_mempoolresp_purge(&NSPV_mempoolresult);
memset(zeroes,0,sizeof(zeroes));
if ( coinaddr == 0 )
coinaddr = zeroes;
if ( coinaddr[0] != 0 && bitcoin_base58decode(msg,coinaddr) != 25 )
{
result.push_back(Pair("result","error"));
Expand All @@ -673,7 +676,7 @@ UniValue NSPV_mempooltxids(char *coinaddr,int32_t CCflag,uint8_t funcid,uint256
msg[len++] = slen;
memcpy(&msg[len],coinaddr,slen), len += slen;
fprintf(stderr,"(%s) func.%d CC.%d %s/v%d len.%d\n",coinaddr,funcid,CCflag,txid.GetHex().c_str(),vout,len);
for (iter=0; iter<3; iter++);
for (iter=0; iter<3; iter++)
if ( NSPV_req(0,msg,len,NODE_NSPV,msg[0]>>1) != 0 )
{
for (i=0; i<NSPV_POLLITERS; i++)
Expand Down Expand Up @@ -730,7 +733,7 @@ bool NSPV_evalcode_inmempool(uint8_t evalcode,uint8_t funcid)

UniValue NSPV_notarizations(int32_t reqheight)
{
uint8_t msg[64]; int32_t i,iter,len = 0; struct NSPV_ntzsresp N,*ptr;
uint8_t msg[512]; int32_t i,iter,len = 0; struct NSPV_ntzsresp N,*ptr;
if ( (ptr= NSPV_ntzsresp_find(reqheight)) != 0 )
{
fprintf(stderr,"FROM CACHE NSPV_notarizations.%d\n",reqheight);
Expand All @@ -740,7 +743,7 @@ UniValue NSPV_notarizations(int32_t reqheight)
}
msg[len++] = NSPV_NTZS;
len += iguana_rwnum(1,&msg[len],sizeof(reqheight),&reqheight);
for (iter=0; iter<3; iter++);
for (iter=0; iter<3; iter++)
if ( NSPV_req(0,msg,len,NODE_NSPV,msg[0]>>1) != 0 )
{
for (i=0; i<NSPV_POLLITERS; i++)
Expand All @@ -756,7 +759,7 @@ UniValue NSPV_notarizations(int32_t reqheight)

UniValue NSPV_txidhdrsproof(uint256 prevtxid,uint256 nexttxid)
{
uint8_t msg[64]; int32_t i,iter,len = 0; struct NSPV_ntzsproofresp P,*ptr;
uint8_t msg[512]; int32_t i,iter,len = 0; struct NSPV_ntzsproofresp P,*ptr;
if ( (ptr= NSPV_ntzsproof_find(prevtxid,nexttxid)) != 0 )
{
fprintf(stderr,"FROM CACHE NSPV_txidhdrsproof %s %s\n",ptr->prevtxid.GetHex().c_str(),ptr->nexttxid.GetHex().c_str());
Expand All @@ -768,7 +771,7 @@ UniValue NSPV_txidhdrsproof(uint256 prevtxid,uint256 nexttxid)
msg[len++] = NSPV_NTZSPROOF;
len += iguana_rwbignum(1,&msg[len],sizeof(prevtxid),(uint8_t *)&prevtxid);
len += iguana_rwbignum(1,&msg[len],sizeof(nexttxid),(uint8_t *)&nexttxid);
for (iter=0; iter<3; iter++);
for (iter=0; iter<3; iter++)
if ( NSPV_req(0,msg,len,NODE_NSPV,msg[0]>>1) != 0 )
{
for (i=0; i<NSPV_POLLITERS; i++)
Expand All @@ -794,7 +797,7 @@ UniValue NSPV_hdrsproof(int32_t prevht,int32_t nextht)

UniValue NSPV_txproof(int32_t vout,uint256 txid,int32_t height)
{
uint8_t msg[64]; int32_t i,iter,len = 0; struct NSPV_txproof P,*ptr;
uint8_t msg[512]; int32_t i,iter,len = 0; struct NSPV_txproof P,*ptr;
if ( (ptr= NSPV_txproof_find(txid)) != 0 )
{
fprintf(stderr,"FROM CACHE NSPV_txproof %s\n",txid.GetHex().c_str());
Expand All @@ -808,7 +811,7 @@ UniValue NSPV_txproof(int32_t vout,uint256 txid,int32_t height)
len += iguana_rwnum(1,&msg[len],sizeof(vout),&vout);
len += iguana_rwbignum(1,&msg[len],sizeof(txid),(uint8_t *)&txid);
fprintf(stderr,"req txproof %s/v%d at height.%d\n",txid.GetHex().c_str(),vout,height);
for (iter=0; iter<3; iter++);
for (iter=0; iter<3; iter++)
if ( NSPV_req(0,msg,len,NODE_NSPV,msg[0]>>1) != 0 )
{
for (i=0; i<NSPV_POLLITERS; i++)
Expand All @@ -825,12 +828,12 @@ UniValue NSPV_txproof(int32_t vout,uint256 txid,int32_t height)

UniValue NSPV_spentinfo(uint256 txid,int32_t vout)
{
uint8_t msg[64]; int32_t i,iter,len = 0; struct NSPV_spentinfo I;
uint8_t msg[512]; int32_t i,iter,len = 0; struct NSPV_spentinfo I;
NSPV_spentinfo_purge(&NSPV_spentresult);
msg[len++] = NSPV_SPENTINFO;
len += iguana_rwnum(1,&msg[len],sizeof(vout),&vout);
len += iguana_rwbignum(1,&msg[len],sizeof(txid),(uint8_t *)&txid);
for (iter=0; iter<3; iter++);
for (iter=0; iter<3; iter++)
if ( NSPV_req(0,msg,len,NODE_SPENTINDEX,msg[0]>>1) != 0 )
{
for (i=0; i<NSPV_POLLITERS; i++)
Expand Down Expand Up @@ -859,7 +862,7 @@ UniValue NSPV_broadcast(char *hex)
memcpy(&msg[len],data,n), len += n;
free(data);
//fprintf(stderr,"send txid.%s\n",txid.GetHex().c_str());
for (iter=0; iter<3; iter++);
for (iter=0; iter<3; iter++)
if ( NSPV_req(0,msg,len,NODE_NSPV,msg[0]>>1) != 0 )
{
for (i=0; i<NSPV_POLLITERS; i++)
Expand Down
Loading

0 comments on commit 2ad296f

Please sign in to comment.