Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor komodo_state::NPOINTS #481

Merged
merged 37 commits into from
Feb 20, 2022
Merged
Show file tree
Hide file tree
Changes from 36 commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
6a79b3c
make komodo_state.NPOINTS a std::vector
jmjatlanta Aug 17, 2021
7ef4992
document last_NPOINTSi
jmjatlanta Aug 17, 2021
b340c5c
Document NPOINT search methods
jmjatlanta Aug 17, 2021
012c37b
use multiset for NPOINTS
jmjatlanta Aug 17, 2021
8f12c7f
add tests
jmjatlanta Aug 17, 2021
d27b213
Verify tests for NPOINTS functionality
jmjatlanta Aug 18, 2021
aeebdd2
Merge branch 'jmj_npoints_test' into jmj_npoints
jmjatlanta Aug 18, 2021
bee5c77
Add index (intermediate commit)
jmjatlanta Aug 19, 2021
142321c
Use boost::multi_index for NPOINTS
jmjatlanta Aug 19, 2021
2699d22
Removed code to modify checkpoints
jmjatlanta Aug 19, 2021
80686f1
move NPOINTS search logic to komodo_state
jmjatlanta Aug 19, 2021
4a39237
Use an ifc to access NPOINTS
jmjatlanta Aug 19, 2021
d9eff5d
Consistent method names
jmjatlanta Aug 19, 2021
371981f
Hide komodo_state.LastNotarization state variables
jmjatlanta Aug 19, 2021
2aa02e0
Remove unnecessary equality override
jmjatlanta Aug 19, 2021
db48c46
comment cleanup
jmjatlanta Aug 19, 2021
95edec1
allow debug builds
jmjatlanta Sep 21, 2021
9f8068d
Merge branch 'dev' into jmj_npoints
jmjatlanta Sep 23, 2021
bbf9e4a
make clean .a files
jmjatlanta Oct 8, 2021
1cd6160
hardforks in own object
jmjatlanta Oct 13, 2021
658c13d
adjust hardfork defines
jmjatlanta Oct 14, 2021
d3f7819
Handle cryptoconditions changes
jmjatlanta Oct 15, 2021
62ae1c2
Rebuild libcc.so when dependencies change
jmjatlanta Oct 22, 2021
a73398b
build cryptoconditions as static (incl Windows)
jmjatlanta Nov 5, 2021
9e8124d
Move implementation to .cpp
jmjatlanta Jan 11, 2022
8618e6e
addrman no lock in ctor
jmjatlanta Jan 14, 2022
8ac9778
enable-debug script param
jmjatlanta Jan 14, 2022
4727280
proper init of event_pubkeys
jmjatlanta Jan 17, 2022
1b321c7
more testing
jmjatlanta Jan 27, 2022
3c2f40c
Diagnose failing test
jmjatlanta Jan 27, 2022
845759f
Merge branch 'jmj_build_fix' into jmj_npoints
jmjatlanta Jan 27, 2022
b91ffa8
Fix NotarizedData search
jmjatlanta Jan 27, 2022
73d490d
Revert "Merge branch 'jmj_build_fix' into jmj_npoints"
jmjatlanta Jan 27, 2022
d572e67
Switch from boost multi_index to vector
jmjatlanta Jan 28, 2022
96cc1b3
move test data to binary file
jmjatlanta Jan 28, 2022
8a3764d
Remove unused headers
jmjatlanta Jan 29, 2022
71b3f3e
back out calling base constructor
jmjatlanta Feb 1, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Makefile.ktest.include
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ komodo_test_SOURCES = \
test-komodo/test_eval_bet.cpp \
test-komodo/test_eval_notarisation.cpp \
test-komodo/test_parse_notarisation.cpp \
test-komodo/test_parse_notarisation_data.cpp \
test-komodo/test_buffered_file.cpp \
test-komodo/test_sha256_crypto.cpp \
test-komodo/test_script_standard_tests.cpp \
Expand Down
2 changes: 1 addition & 1 deletion src/cc/CCutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -728,7 +728,7 @@ bool komodo_txnotarizedconfirmed(uint256 txid)
confirms=1 + pindex->GetHeight() - 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->GetHeight() != 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;
fprintf(stderr,"%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);
fprintf(stderr,"%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 )
printf("[%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 )
printf("[%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());
printf("[%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 )
printf("%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);
printf("%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 @@ -721,9 +722,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 @@ -1021,34 +1021,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->GetHeight(),&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->GetHeight(),&notarized_hash,&notarized_desttxid);
*notarized_heightp = notarized_height;

BlockMap::const_iterator it;
if ( notarized_height >= 0 && notarized_height <= pindex->GetHeight() && (it = mapBlockIndex.find(notarized_hash)) != mapBlockIndex.end() && (notary = it->second) != NULL )
CBlockIndex *notary;
if ( notarized_height >= 0 && notarized_height <= pindex->GetHeight()
&& (it = mapBlockIndex.find(notarized_hash)) != mapBlockIndex.end() && (notary = it->second) != nullptr )
{
//printf("nHeight.%d -> (%d %s)\n",pindex->Tip()->GetHeight(),notarized_height,notarized_hash.ToString().c_str());
//verify that the block info returned from komodo_notarizeddata matches the actual block
if ( notary->GetHeight() == notarized_height ) // if notarized_hash not in chain, reorg
{
if ( nHeight < notarized_height )
{
//fprintf(stderr,"[%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
fprintf(stderr,"[%s] nHeight.%d == NOTARIZED_HEIGHT.%d, diff hash\n",ASSETCHAINS_SYMBOL,nHeight,notarized_height);
return(-1);
return false;
}
} //else fprintf(stderr,"[%s] unexpected error notary_hash %s ht.%d at ht.%d\n",ASSETCHAINS_SYMBOL,notarized_hash.ToString().c_str(),notarized_height,notary->GetHeight());
}
}
//else if ( notarized_height > 0 && notarized_height != 73880 && notarized_height >= 170000 )
// fprintf(stderr,"[%s] couldnt find notarized.(%s %d) ht.%d\n",ASSETCHAINS_SYMBOL,notarized_hash.ToString().c_str(),notarized_height,pindex->GetHeight());
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 @@ -162,7 +162,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
67 changes: 0 additions & 67 deletions src/komodo_ccdata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,70 +197,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));
//fprintf(stderr,"[%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)) )
{
printf("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 )
{
fprintf(stderr,"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 @@ -24,6 +24,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 @@ -510,14 +510,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