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

@@ -203,6 +203,7 @@ int32_t NSPV_rwntz(int32_t rwflag,uint8_t *serialized,struct NSPV_ntz *ptr)
struct NSPV_ntzsresp
{
struct NSPV_ntz prevntz,nextntz;
int32_t reqheight;
};
int32_t NSPV_rwntzsresp(int32_t rwflag,uint8_t *serialized,struct NSPV_ntzsresp *ptr)
@@ -210,9 +211,15 @@ int32_t NSPV_rwntzsresp(int32_t rwflag,uint8_t *serialized,struct NSPV_ntzsresp
int32_t len = 0;
len += NSPV_rwntz(rwflag,&serialized[len],&ptr->prevntz);
len += NSPV_rwntz(rwflag,&serialized[len],&ptr->nextntz);
len += iguana_rwnum(rwflag,&serialized[len],sizeof(ptr->reqheight),&ptr->reqheight);
return(len);
}
void NSPV_ntzsresp_copy(struct NSPV_ntzsresp *dest,struct NSPV_ntzsresp *ptr)
{
*dest = *ptr;
}
void NSPV_ntzsresp_purge(struct NSPV_ntzsresp *ptr)
{
if ( ptr != 0 )
@@ -267,6 +274,21 @@ int32_t NSPV_rwtxproof(int32_t rwflag,uint8_t *serialized,struct NSPV_txproof *p
return(len);
}
void NSPV_txproof_copy(struct NSPV_txproof *dest,struct NSPV_txproof *ptr)
{
*dest = *ptr;
if ( ptr->tx != 0 )
{
dest->tx = malloc(ptr->txlen);
memcpy(dest->tx,ptr->tx,ptr->txlen);
}
if ( ptr->txproof != 0 )
{
dest->txproof = malloc(ptr->txprooflen);
memcpy(dest->txproof,ptr->txproof,ptr->txprooflen);
}
}
void NSPV_txproof_purge(struct NSPV_txproof *ptr)
{
if ( ptr != 0 )
@@ -321,6 +343,26 @@ int32_t NSPV_rwntzsproofresp(int32_t rwflag,uint8_t *serialized,struct NSPV_ntzs
return(len);
}
void NSPV_ntzsproof_copy(struct NSPV_ntzsproofresp *dest,struct NSPV_ntzsproofresp *ptr)
{
*dest = *ptr;
if ( ptr->common.hdrs != 0 )
{
dest->common.hdrs = malloc(ptr->common.numhdrs * sizeof(*ptr->common.hdrs));
memcpy(dest->common.hdrs,ptr->common.hdrs,ptr->common.numhdrs * sizeof(*ptr->common.hdrs));
}
if ( ptr->prevntz != 0 )
{
dest->prevntz = malloc(ptr->prevtxlen);
memcpy(dest->prevntz,ptr->prevntz,ptr->prevtxlen);
}
if ( ptr->nextntz != 0 )
{
dest->nextntz = malloc(ptr->nexttxlen);
memcpy(dest->nextntz,ptr->nextntz,ptr->nexttxlen);
}
}
void NSPV_ntzsproofresp_purge(struct NSPV_ntzsproofresp *ptr)
{
if ( ptr != 0 )

View File

@@ -77,7 +77,7 @@ int32_t NSPV_ntzextract(struct NSPV_ntz *ptr,uint256 ntztxid,int32_t txidht,uint
return(0);
}
int32_t NSPV_getntzsresp(struct NSPV_ntzsresp *ptr,int32_t height)
int32_t NSPV_getntzsresp(struct NSPV_ntzsresp *ptr,int32_t reqheight)
{
struct NSPV_ntzargs prev,next;
if ( height < chainActive.LastTip()->GetHeight() )
@@ -86,6 +86,7 @@ int32_t NSPV_getntzsresp(struct NSPV_ntzsresp *ptr,int32_t height)
{
if ( prev.ntzheight != 0 )
{
ptr->reqheight = reqheight;
if ( NSPV_ntzextract(&ptr->prevntz,prev.txid,prev.txidht,prev.desttxid,prev.ntzheight) < 0 )
return(-1);
}

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);

View File

@@ -80,9 +80,7 @@ int32_t NSPV_gettransaction(int32_t skipvalidation,int32_t vout,uint256 txid,int
{
rewards = komodo_interestnew(height,tx.vout[vout].nValue,tx.nLockTime,tiptime);
if ( rewards != extradata )
{
fprintf(stderr,"extradata %.8f vs rewards %.8f\n",dstr(extradata),dstr(rewards));
}
rewardsum += rewards;
}