Add caching to three requests: ntzsresp, txproof, ntzsproof

This commit is contained in:
jl777
2019-07-10 01:21:47 -11:00
parent 2992d085ec
commit 958f2c198e
4 changed files with 131 additions and 20 deletions

View File

@@ -37,6 +37,72 @@ struct NSPV_ntzsproofresp NSPV_ntzsproofresult;
struct NSPV_txproof NSPV_txproofresult;
struct NSPV_broadcastresp NSPV_broadcastresult;
struct NSPV_ntzsresp NSPV_ntzsresp_cache[NSPV_MAXVINS];
struct NSPV_ntzsproofresp NSPV_ntzsproofresp_cache[NSPV_MAXVINS * 2];
struct NSPV_txproof NSPV_txproof_cache[NSPV_MAXVINS * 4];
struct NSPV_ntzsresp *NSPV_ntzsresp_find(int32_t reqheight)
{
for (i=0; i<sizeof(NSPV_ntzsresp_cache)/sizeof(*NSPV_ntzsresp_cache); i++)
if ( NSPV_ntzsresp_cache[i].reqheight == reqheight )
return(&NSPV_ntzsresp_cache[i]);
return(0);
}
struct NSPV_txproof *NSPV_ntzsresp_add(struct NSPV_ntzsresp *ptr)
{
int32_t i;
for (i=0; i<sizeof(NSPV_ntzsresp_cache)/sizeof(*NSPV_ntzsresp_cache); i++)
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)));
NSPV_ntzsresp_purge(&NSPV_ntzsresp_cache[i]);
NSPV_ntzsresp_copy(&NSPV_ntzsresp_cache[i],ptr);
return(&NSPV_ntzsresp_cache[i]);
}
struct NSPV_txproof *NSPV_txproof_find(uint256 txid)
{
for (i=0; i<sizeof(NSPV_txproof_cache)/sizeof(*NSPV_txproof_cache); i++)
if ( NSPV_txproof_cache[i].txid == txid )
return(&NSPV_txproof_cache[i]);
return(0);
}
struct NSPV_txproof *NSPV_txproof_add(struct NSPV_txproof *ptr)
{
int32_t i;
for (i=0; i<sizeof(NSPV_txproof_cache)/sizeof(*NSPV_txproof_cache); i++)
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)));
NSPV_txproof_purge(&NSPV_txproof_cache[i]);
NSPV_txproof_copy(&NSPV_txproof_cache[i],ptr);
return(&NSPV_txproof_cache[i]);
}
struct NSPV_ntzsproofresp *NSPV_ntzsproof_find(uint256 prevtxid,uint256 nexttxid)
{
for (i=0; i<sizeof(NSPV_ntzsproofresp_cache)/sizeof(*NSPV_ntzsproofresp_cache); i++)
if ( NSPV_ntzsproofresp_cache[i].prevtxid == prevtxid && NSPV_ntzsproofresp_cache[i].nexttxid == nexttxid )
return(&NSPV_ntzsproofresp_cache[i]);
return(0);
}
struct NSPV_ntzsproofresp *NSPV_ntzsproof_add(struct NSPV_ntzsproofresp *ptr)
{
int32_t i;
for (i=0; i<sizeof(NSPV_ntzsproofresp_cache)/sizeof(*NSPV_ntzsproofresp_cache); i++)
if ( NSPV_ntzsproofresp_cache[i].hdrs == 0 )
break;
if ( i == sizeof(NSPV_ntzsproofresp_cache)/sizeof(*NSPV_ntzsproofresp_cache) )
i == (rand() % (sizeof(NSPV_ntzsproofresp_cache)/sizeof(*NSPV_ntzsproofresp_cache)));
NSPV_ntzsproof_purge(&NSPV_ntzsproofresp_cache[i]);
NSPV_ntzsproof_copy(&NSPV_ntzsproofresp_cache[i],ptr);
return(&NSPV_ntzsproofresp_cache[i]);
}
// komodo_nSPVresp is called from async message processing
@@ -75,16 +141,22 @@ void komodo_nSPVresp(CNode *pfrom,std::vector<uint8_t> response) // received a r
case NSPV_NTZSRESP:
NSPV_ntzsresp_purge(&NSPV_ntzsresult);
NSPV_rwntzsresp(0,&response[1],&NSPV_ntzsresult);
if ( NSPV_ntzs_find(NSPV_ntzsresult.reqheight) == 0 )
NSPV_txproof_add(&NSPV_ntzsresult);
fprintf(stderr,"got ntzs response %u size.%d %s prev.%d, %s next.%d\n",timestamp,(int32_t)response.size(),NSPV_ntzsresult.prevntz.txid.GetHex().c_str(),NSPV_ntzsresult.prevntz.height,NSPV_ntzsresult.nextntz.txid.GetHex().c_str(),NSPV_ntzsresult.nextntz.height);
break;
case NSPV_NTZSPROOFRESP:
NSPV_ntzsproofresp_purge(&NSPV_ntzsproofresult);
NSPV_rwntzsproofresp(0,&response[1],&NSPV_ntzsproofresult);
if ( NSPV_ntzsproof_find(NSPV_ntzsproofresult.prevtxid,NSPV_ntzsproofresult.nexttxid) == 0 )
NSPV_ntzsproof_add(&NSPV_ntzsproofresult);
fprintf(stderr,"got ntzproof response %u size.%d prev.%d next.%d\n",timestamp,(int32_t)response.size(),NSPV_ntzsproofresult.common.prevht,NSPV_ntzsproofresult.common.nextht);
break;
case NSPV_TXPROOFRESP:
NSPV_txproof_purge(&NSPV_txproofresult);
NSPV_rwtxproof(0,&response[1],&NSPV_txproofresult);
if ( NSPV_txproof_find(NSPV_txproofresult.txid) == 0 )
NSPV_txproof_add(&NSPV_txproofresult);
fprintf(stderr,"got txproof response %u size.%d %s ht.%d\n",timestamp,(int32_t)response.size(),NSPV_txproofresult.txid.GetHex().c_str(),NSPV_txproofresult.height);
break;
case NSPV_SPENTINFORESP:
@@ -298,7 +370,7 @@ UniValue NSPV_utxosresp_json(struct NSPV_utxosresp *ptr)
return(result);
}
UniValue NSPV_ntzs_json(struct NSPV_ntzsresp *ptr)
UniValue NSPV_ntzsresp_json(struct NSPV_ntzsresp *ptr)
{
UniValue result(UniValue::VOBJ);
result.push_back(Pair("result","success"));
@@ -455,11 +527,11 @@ UniValue NSPV_addressutxos(char *coinaddr,int32_t CCflag)
return(result);
}
UniValue NSPV_notarizations(int32_t height)
UniValue NSPV_notarizations(int32_t reqheight)
{
uint8_t msg[64]; int32_t i,iter,len = 0; struct NSPV_ntzsresp N;
if ( NSPV_ntzsresult.prevntz.height <= height && NSPV_ntzsresult.nextntz.height >= height )
return(NSPV_ntzs_json(&NSPV_ntzsresult));
uint8_t msg[64]; int32_t i,iter,len = 0; struct NSPV_ntzsresp N,*ptr;
if ( (ptr= NSPV_ntzsresp_find(reqheight)) != 0 )
return(NSPV_ntzsresp_json(ptr));
NSPV_ntzsresp_purge(&NSPV_ntzsresult);
msg[len++] = NSPV_NTZS;
len += iguana_rwnum(1,&msg[len],sizeof(height),&height);
@@ -470,18 +542,18 @@ UniValue NSPV_notarizations(int32_t height)
{
usleep(NSPV_POLLMICROS);
if ( NSPV_ntzsresult.prevntz.height <= height && NSPV_ntzsresult.nextntz.height >= height )
return(NSPV_ntzs_json(&NSPV_ntzsresult));
return(NSPV_ntzsresp_json(&NSPV_ntzsresult));
}
} else sleep(1);
memset(&N,0,sizeof(N));
return(NSPV_ntzs_json(&N));
return(NSPV_ntzsresp_json(&N));
}
UniValue NSPV_txidhdrsproof(uint256 prevtxid,uint256 nexttxid)
{
uint8_t msg[64]; int32_t i,iter,len = 0; struct NSPV_ntzsproofresp H;
if ( NSPV_ntzsproofresult.prevtxid == prevtxid && NSPV_ntzsproofresult.nexttxid == nexttxid )
return(NSPV_ntzsproof_json(&NSPV_ntzsproofresult));
uint8_t msg[64]; int32_t i,iter,len = 0; struct NSPV_ntzsproofresp P,*ptr;
if ( (ptr= NSPV_ntzsproof_find(prevtxid,nexttxid)) != 0 )
return(NSPV_ntzsproof_json(ptr));
NSPV_ntzsproofresp_purge(&NSPV_ntzsproofresult);
msg[len++] = NSPV_NTZSPROOF;
len += iguana_rwbignum(1,&msg[len],sizeof(prevtxid),(uint8_t *)&prevtxid);
@@ -496,8 +568,8 @@ UniValue NSPV_txidhdrsproof(uint256 prevtxid,uint256 nexttxid)
return(NSPV_ntzsproof_json(&NSPV_ntzsproofresult));
}
} else sleep(1);
memset(&H,0,sizeof(H));
return(NSPV_ntzsproof_json(&H));
memset(&P,0,sizeof(P));
return(NSPV_ntzsproof_json(&P));
}
UniValue NSPV_hdrsproof(int32_t prevht,int32_t nextht)
@@ -512,9 +584,9 @@ 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;
if ( NSPV_txproofresult.txid == txid )
return(NSPV_txproof_json(&NSPV_txproofresult));
uint8_t msg[64]; int32_t i,iter,len = 0; struct NSPV_txproof P,*ptr;
if ( (ptr= NSPV_txproof_find(txid)) != 0 )
return(NSPV_txproof_json(ptr));
NSPV_txproof_purge(&NSPV_txproofresult);
msg[len++] = NSPV_TXPROOF;
len += iguana_rwnum(1,&msg[len],sizeof(height),&height);
@@ -539,8 +611,6 @@ 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;
//if ( NSPV_spentresult.txid == txid && NSPV_spentresult.vout == vout )
// return(NSPV_spentinfo_json(&NSPV_spentresult));
NSPV_spentinfo_purge(&NSPV_spentresult);
msg[len++] = NSPV_SPENTINFO;
len += iguana_rwnum(1,&msg[len],sizeof(vout),&vout);