Skip to content

Commit

Permalink
Refactor komodo_state::NPOINTS
Browse files Browse the repository at this point in the history
Backport of @jmjatlanta work from KomodoPlatform/komodo#481
excluded tests and binary data (for tests).

Related links:

- 4322dae
- KomodoPlatform/komodo#481
  • Loading branch information
DeckerSU committed Apr 5, 2022
1 parent 709a0a1 commit 5719dd6
Show file tree
Hide file tree
Showing 12 changed files with 454 additions and 273 deletions.
2 changes: 1 addition & 1 deletion src/cc/CCutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -727,7 +727,7 @@ bool komodo_txnotarizedconfirmed(uint256 txid)
confirms=1 + pindex->nHeight - txheight;
}

if ((sp= komodo_stateptr(symbol,dest)) != 0 && (notarized=sp->NOTARIZED_HEIGHT) > 0 && txheight > sp->NOTARIZED_HEIGHT) notarized=0;
if ((sp= komodo_stateptr(symbol,dest)) != 0 && (notarized=sp->LastNotarizedHeight()) > 0 && txheight > sp->LastNotarizedHeight()) notarized=0;
#ifdef TESTMODE
notarized=0;
#endif //TESTMODE
Expand Down
57 changes: 29 additions & 28 deletions src/komodo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -290,14 +290,14 @@ void komodo_stateupdate(int32_t height,uint8_t notarypubs[][33],uint8_t numnotar
}
else if ( height != 0 )
{
if ( sp != 0 )
if ( sp != nullptr )
{
std::shared_ptr<komodo::event_notarized> evt = std::make_shared<komodo::event_notarized>(height, dest);
evt->blockhash = sp->NOTARIZED_HASH;
evt->desttxid = sp->NOTARIZED_DESTTXID;
evt->notarizedheight = sp->NOTARIZED_HEIGHT;
evt->MoM = sp->MoM;
evt->MoMdepth = sp->MoMdepth;
evt->blockhash = sp->LastNotarizedHash();
evt->desttxid = sp->LastNotarizedDestTxId();
evt->notarizedheight = sp->LastNotarizedHeight();
evt->MoM = sp->LastNotarizedMoM();
evt->MoMdepth = sp->LastNotarizedMoMDepth();
write_event(evt, fp);
komodo_eventadd_notarized(sp,symbol,height,evt);
}
Expand All @@ -313,17 +313,17 @@ int32_t komodo_validate_chain(uint256 srchash,int32_t notarized_height)
return(0);
if ( IsInitialBlockDownload() == 0 && ((pindex= komodo_getblockindex(srchash)) == 0 || pindex->nHeight != notarized_height) )
{
if ( sp->NOTARIZED_HEIGHT > 0 && sp->NOTARIZED_HEIGHT < notarized_height )
rewindtarget = sp->NOTARIZED_HEIGHT - 1;
if ( sp->LastNotarizedHeight() > 0 && sp->LastNotarizedHeight() < notarized_height )
rewindtarget = sp->LastNotarizedHeight() - 1;
else if ( notarized_height > 101 )
rewindtarget = notarized_height - 101;
else rewindtarget = 0;
if ( rewindtarget != 0 && rewindtarget > KOMODO_REWIND && rewindtarget > last_rewind )
{
if ( last_rewind != 0 )
{
//KOMODO_REWIND = rewindtarget;
LogPrintf("%s FORK detected. notarized.%d %s not in this chain! last notarization %d -> rewindtarget.%d\n",ASSETCHAINS_SYMBOL,notarized_height,srchash.ToString().c_str(),sp->NOTARIZED_HEIGHT,rewindtarget);
LogPrintf("%s FORK detected. notarized.%d %s not in this chain! last notarization %d -> rewindtarget.%d\n",
ASSETCHAINS_SYMBOL,notarized_height,srchash.ToString().c_str(),sp->LastNotarizedHeight(),rewindtarget);
}
last_rewind = rewindtarget;
}
Expand Down Expand Up @@ -490,7 +490,6 @@ int32_t komodo_voutupdate(bool fJustCheck,int32_t *isratificationp,int32_t notar
}
else
{
komodo_rwccdata(ASSETCHAINS_SYMBOL,1,&ccdata,&MoMoMdata);
if ( matched != 0 )
LogPrintf("[%s] matched.%d VALID (%s) MoM.%s [%d] CCid.%u\n",ASSETCHAINS_SYMBOL,matched,ccdata.symbol,MoM.ToString().c_str(),MoMdepth&0xffff,(MoMdepth>>16)&0xffff);
}
Expand All @@ -499,22 +498,23 @@ int32_t komodo_voutupdate(bool fJustCheck,int32_t *isratificationp,int32_t notar
memset(&ccdata,0,sizeof(ccdata));
memset(&MoMoMdata,0,sizeof(MoMoMdata));
}
else if ( ASSETCHAINS_SYMBOL[0] == 0 && matched != 0 && notarized != 0 && validated != 0 )
komodo_rwccdata((char *)"KMD",1,&ccdata,0);

if ( matched != 0 && *notarizedheightp > sp->NOTARIZED_HEIGHT && *notarizedheightp < height )
if ( matched != 0 && *notarizedheightp > sp->LastNotarizedHeight() && *notarizedheightp < height )
{
sp->NOTARIZED_HEIGHT = *notarizedheightp;
sp->NOTARIZED_HASH = srchash;
sp->NOTARIZED_DESTTXID = desttxid;
sp->SetLastNotarizedHeight(*notarizedheightp);
sp->SetLastNotarizedHash(srchash);
sp->SetLastNotarizedDestTxId(desttxid);
if ( MoM != zero && (MoMdepth&0xffff) > 0 )
{
sp->MoM = MoM;
sp->MoMdepth = MoMdepth;
sp->SetLastNotarizedMoM(MoM);
sp->SetLastNotarizedMoMDepth(MoMdepth);
}
komodo_stateupdate(height,0,0,0,zero,0,0,0,0,0,0,0,0,0,0,sp->MoM,sp->MoMdepth);
//if ( ASSETCHAINS_SYMBOL[0] != 0 )
LogPrintf("[%s] ht.%d NOTARIZED.%d %s.%s %sTXID.%s lens.(%d %d) MoM.%s %d\n",ASSETCHAINS_SYMBOL,height,sp->NOTARIZED_HEIGHT,ASSETCHAINS_SYMBOL[0]==0?"KMD":ASSETCHAINS_SYMBOL,srchash.ToString().c_str(),ASSETCHAINS_SYMBOL[0]==0?"BTC":"KMD",desttxid.ToString().c_str(),opretlen,len,sp->MoM.ToString().c_str(),sp->MoMdepth);
komodo_stateupdate(height,0,0,0,zero,0,0,0,0,0,0,0,0,0,0,sp->LastNotarizedMoM(),sp->LastNotarizedMoMDepth());
LogPrintf("[%s] ht.%d NOTARIZED.%d %s.%s %sTXID.%s lens.(%d %d) MoM.%s %d\n",
ASSETCHAINS_SYMBOL,height,sp->LastNotarizedHeight(),
ASSETCHAINS_SYMBOL[0]==0?"KMD":ASSETCHAINS_SYMBOL,srchash.ToString().c_str(),
ASSETCHAINS_SYMBOL[0]==0?"BTC":"KMD",desttxid.ToString().c_str(),opretlen,len,
sp->LastNotarizedMoM().ToString().c_str(),sp->LastNotarizedMoMDepth());

if ( ASSETCHAINS_SYMBOL[0] == 0 )
{
Expand All @@ -540,10 +540,11 @@ int32_t komodo_voutupdate(bool fJustCheck,int32_t *isratificationp,int32_t notar
komodo_stateupdate(height,0,0,0,txhash,0,0,0,0,0,0,value,&scriptbuf[len],opretlen-len+4+3+(scriptbuf[1] == 0x4d),j,zero,0);
}
}
} //else if ( fJustCheck )
// return (-3); // if the notarisation is only invalid because its out of order it cannot be mined in a block with a valid one!
}
} else if ( opretlen != 149 && height > 600000 && matched != 0 )
LogPrintf("%s validated.%d notarized.%d %llx reject ht.%d NOTARIZED.%d prev.%d %s.%s DESTTXID.%s len.%d opretlen.%d\n",ccdata.symbol,validated,notarized,(long long)signedmask,height,*notarizedheightp,sp->NOTARIZED_HEIGHT,ASSETCHAINS_SYMBOL[0]==0?"KMD":ASSETCHAINS_SYMBOL,srchash.ToString().c_str(),desttxid.ToString().c_str(),len,opretlen);
LogPrintf("%s validated.%d notarized.%d %llx reject ht.%d NOTARIZED.%d prev.%d %s.%s DESTTXID.%s len.%d opretlen.%d\n",
ccdata.symbol,validated,notarized,(long long)signedmask,height,*notarizedheightp,sp->LastNotarizedHeight(),
ASSETCHAINS_SYMBOL[0]==0?"KMD":ASSETCHAINS_SYMBOL,srchash.ToString().c_str(),desttxid.ToString().c_str(),len,opretlen);
}
else if ( matched != 0 && i == 0 && j == 1 && opretlen == 149 )
{
Expand Down Expand Up @@ -713,9 +714,9 @@ int32_t komodo_connectblock(bool fJustCheck, CBlockIndex *pindex,CBlock& block)
// simulate DPoW in regtest mode for dpowconfs tests/etc
if ( Params().NetworkIDString() == "regtest" && ( height%7 == 0) ) {
notarized = 1;
sp->NOTARIZED_HEIGHT = height;
sp->NOTARIZED_HASH = block.GetHash();
sp->NOTARIZED_DESTTXID = txhash;
sp->SetLastNotarizedHeight(height);
sp->SetLastNotarizedHash(block.GetHash());
sp->SetLastNotarizedDestTxId(txhash);
}
for (j=0; j<numvouts; j++)
{
Expand Down
40 changes: 25 additions & 15 deletions src/komodo_bitcoind.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1047,34 +1047,44 @@ uint32_t komodo_blocktime(uint256 hash)
return(0);
}

int32_t komodo_checkpoint(int32_t *notarized_heightp,int32_t nHeight,uint256 hash)
/******
* @brief Verify that a height and hash match the most recent (based on height) notarized_checkpoint
* @param[out] notarized_heightp the notarized height found
* @param[in] nHeight the height that should be greater than the notarized height
* @param[in] hash the hash that should match the notarized hash
* @returns true on success
*/
bool komodo_checkpoint(int32_t *notarized_heightp, int32_t nHeight, uint256 hash)
{
int32_t notarized_height,MoMdepth; uint256 MoM,notarized_hash,notarized_desttxid; CBlockIndex *notary,*pindex;
CBlockIndex *pindex;
if ( (pindex= chainActive.LastTip()) == 0 )
return(-1);
notarized_height = komodo_notarizeddata(pindex->nHeight,&notarized_hash,&notarized_desttxid);
return false;

// get the most recent (highest) notarized_checkpointdata
uint256 notarized_hash;
uint256 notarized_desttxid;
int32_t notarized_height = komodo_notarizeddata(pindex->nHeight,&notarized_hash,&notarized_desttxid);
*notarized_heightp = notarized_height;

BlockMap::const_iterator it;
if ( notarized_height >= 0 && notarized_height <= pindex->nHeight && (it = mapBlockIndex.find(notarized_hash)) != mapBlockIndex.end() && (notary = it->second) != NULL )
CBlockIndex *notary;
if ( notarized_height >= 0 && notarized_height <= pindex->nHeight
&& (it = mapBlockIndex.find(notarized_hash)) != mapBlockIndex.end() && (notary = it->second) != nullptr )
{
//LogPrintf("nHeight.%d -> (%d %s)\n",pindex->Tip()->nHeight,notarized_height,notarized_hash.ToString().c_str());
//verify that the block info returned from komodo_notarizeddata matches the actual block
if ( notary->nHeight == notarized_height ) // if notarized_hash not in chain, reorg
{
if ( nHeight < notarized_height )
{
//LogPrintf("[%s] nHeight.%d < NOTARIZED_HEIGHT.%d\n",ASSETCHAINS_SYMBOL,nHeight,notarized_height);
return(-1);
}
return false;
else if ( nHeight == notarized_height && memcmp(&hash,&notarized_hash,sizeof(hash)) != 0 )
{
// the height matches, but the hash they passed us does not match the notarized_hash we found
LogPrintf("[%s] nHeight.%d == NOTARIZED_HEIGHT.%d, diff hash\n",ASSETCHAINS_SYMBOL,nHeight,notarized_height);
return(-1);
return false;
}
} //else LogPrintf("[%s] unexpected error notary_hash %s ht.%d at ht.%d\n",ASSETCHAINS_SYMBOL,notarized_hash.ToString().c_str(),notarized_height,notary->nHeight);
}
}
//else if ( notarized_height > 0 && notarized_height != 73880 && notarized_height >= 170000 )
// LogPrintf("[%s] couldnt find notarized.(%s %d) ht.%d\n",ASSETCHAINS_SYMBOL,notarized_hash.ToString().c_str(),notarized_height,pindex->nHeight);
return(0);
return true;
}

uint32_t komodo_interest_args(uint32_t *txheighttimep,int32_t *txheightp,uint32_t *tiptimep,uint64_t *valuep,uint256 hash,int32_t n)
Expand Down
9 changes: 8 additions & 1 deletion src/komodo_bitcoind.h
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,14 @@ int32_t komodo_blockheight(uint256 hash);

uint32_t komodo_blocktime(uint256 hash);

int32_t komodo_checkpoint(int32_t *notarized_heightp,int32_t nHeight,uint256 hash);
/******
* @brief Verify that a height and hash match the most recent (based on height) notarized_checkpoint
* @param[out] notarized_heightp the notarized height found
* @param[in] nHeight the height that should be greater than the notarized height
* @param[in] hash the hash that should match the notarized hash
* @returns true on success
*/
bool komodo_checkpoint(int32_t *notarized_heightp,int32_t nHeight,uint256 hash);

uint32_t komodo_interest_args(uint32_t *txheighttimep,int32_t *txheightp,uint32_t *tiptimep,uint64_t *valuep,uint256 hash,int32_t n);

Expand Down
68 changes: 0 additions & 68 deletions src/komodo_ccdata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,71 +199,3 @@ void komodo_purge_ccdata(int32_t height)
// purge notarized data
}
}

// this is just a demo of ccdata processing to create example data for the MoMoM and allMoMs calls
int32_t komodo_rwccdata(char *thischain,int32_t rwflag,struct komodo_ccdata *ccdata,struct komodo_ccdataMoMoM *MoMoMdata)
{
uint256 hash,zero; bits256 tmp; int32_t i,nonz; struct komodo_ccdata *ptr; struct notarized_checkpoint *np;
return(0); // disable this path as libscott method is much better
if ( rwflag == 0 )
{
// load from disk
}
else
{
// write to disk
}
if ( ccdata->MoMdata.height > 0 && (CC_firstheight == 0 || ccdata->MoMdata.height < CC_firstheight) )
CC_firstheight = ccdata->MoMdata.height;
for (nonz=i=0; i<32; i++)
{
if ( (tmp.bytes[i]= ((uint8_t *)&ccdata->MoMdata.MoM)[31-i]) != 0 )
nonz++;
}
if ( nonz == 0 )
return(0);
memcpy(&hash,&tmp,sizeof(hash));
//LogPrintf("[%s] ccdata.%s id.%d notarized_ht.%d MoM.%s height.%d/t%d\n",ASSETCHAINS_SYMBOL,ccdata->symbol,ccdata->CCid,ccdata->MoMdata.notarized_height,hash.ToString().c_str(),ccdata->MoMdata.height,ccdata->MoMdata.txi);
if ( ASSETCHAINS_SYMBOL[0] == 0 )
{
if ( CC_data != 0 && (CC_data->MoMdata.height > ccdata->MoMdata.height || (CC_data->MoMdata.height == ccdata->MoMdata.height && CC_data->MoMdata.txi >= ccdata->MoMdata.txi)) )
{
LogPrintf("out of order detected? SKIP CC_data ht.%d/txi.%d vs ht.%d/txi.%d\n",CC_data->MoMdata.height,CC_data->MoMdata.txi,ccdata->MoMdata.height,ccdata->MoMdata.txi);
}
else
{
ptr = (struct komodo_ccdata *)calloc(1,sizeof(*ptr));
*ptr = *ccdata;
portable_mutex_lock(&KOMODO_CC_mutex);
DL_PREPEND(CC_data,ptr);
portable_mutex_unlock(&KOMODO_CC_mutex);
}
}
else
{
if ( MoMoMdata != 0 && MoMoMdata->pairs != 0 )
{
for (i=0; i<MoMoMdata->numpairs; i++)
{
if ( (np= komodo_npptr(MoMoMdata->pairs[i].notarized_height)) != 0 )
{
memset(&zero,0,sizeof(zero));
if ( memcmp(&np->MoMoM,&zero,sizeof(np->MoMoM)) == 0 )
{
np->MoMoM = MoMoMdata->MoMoM;
np->MoMoMdepth = MoMoMdata->MoMoMdepth;
np->MoMoMoffset = MoMoMdata->MoMoMoffset;
np->kmdstarti = MoMoMdata->kmdstarti;
np->kmdendi = MoMoMdata->kmdendi;
}
else if ( memcmp(&np->MoMoM,&MoMoMdata->MoMoM,sizeof(np->MoMoM)) != 0 || np->MoMoMdepth != MoMoMdata->MoMoMdepth || np->MoMoMoffset != MoMoMdata->MoMoMoffset || np->kmdstarti != MoMoMdata->kmdstarti || np->kmdendi != MoMoMdata->kmdendi )
{
LogPrintf("preexisting MoMoM mismatch: %s (%d %d %d %d) vs %s (%d %d %d %d)\n",np->MoMoM.ToString().c_str(),np->MoMoMdepth,np->MoMoMoffset,np->kmdstarti,np->kmdendi,MoMoMdata->MoMoM.ToString().c_str(),MoMoMdata->MoMoMdepth,MoMoMdata->MoMoMoffset,MoMoMdata->kmdstarti,MoMoMdata->kmdendi);
}
}
}
}
}
return(1);
}

3 changes: 0 additions & 3 deletions src/komodo_ccdata.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,3 @@ int32_t komodo_addpair(struct komodo_ccdataMoMoM *mdata,int32_t notarized_height
int32_t komodo_MoMoMdata(char *hexstr,int32_t hexsize,struct komodo_ccdataMoMoM *mdata,char *symbol,int32_t kmdheight,int32_t notarized_height);

void komodo_purge_ccdata(int32_t height);

// this is just a demo of ccdata processing to create example data for the MoMoM and allMoMs calls
int32_t komodo_rwccdata(char *thischain,int32_t rwflag,struct komodo_ccdata *ccdata,struct komodo_ccdataMoMoM *MoMoMdata);
4 changes: 2 additions & 2 deletions src/komodo_gateway.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -517,14 +517,14 @@ int32_t komodo_gateway_deposits(CMutableTransaction *txNew,char *base,int32_t to
#ifdef KOMODO_ASSETCHAINS_WAITNOTARIZE
if ( pax->height > 236000 )
{
if ( kmdsp != 0 && kmdsp->NOTARIZED_HEIGHT >= pax->height )
if ( kmdsp != 0 && kmdsp->LastNotarizedHeight() >= pax->height )
pax->validated = pax->komodoshis;
else if ( kmdsp->CURRENT_HEIGHT > pax->height+30 )
pax->validated = pax->ready = 0;
}
else
{
if ( kmdsp != 0 && (kmdsp->NOTARIZED_HEIGHT >= pax->height || kmdsp->CURRENT_HEIGHT > pax->height+30) ) // assumes same chain as notarize
if ( kmdsp != 0 && (kmdsp->LastNotarizedHeight() >= pax->height || kmdsp->CURRENT_HEIGHT > pax->height+30) ) // assumes same chain as notarize
pax->validated = pax->komodoshis;
else pax->validated = pax->ready = 0;
}
Expand Down
Loading

0 comments on commit 5719dd6

Please sign in to comment.