Skip to content

Commit

Permalink
Add locks of cs_main
Browse files Browse the repository at this point in the history
  • Loading branch information
jmjatlanta committed Aug 27, 2021
1 parent 7d7eabb commit 0cba296
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 93 deletions.
2 changes: 0 additions & 2 deletions src/bitcoind.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ static bool fDaemon;
extern char ASSETCHAINS_SYMBOL[KOMODO_ASSETCHAIN_MAXLEN];
extern int32_t ASSETCHAINS_BLOCKTIME;
extern uint64_t ASSETCHAINS_CBOPRET;
void komodo_passport_iteration();
uint64_t komodo_interestsum();
int32_t komodo_longestchain();
void komodo_cbopretupdate(int32_t forceflag);
Expand Down Expand Up @@ -113,7 +112,6 @@ extern int32_t USE_EXTERNAL_PUBKEY;
extern uint32_t ASSETCHAIN_INIT;
extern std::string NOTARY_PUBKEY;
int32_t komodo_is_issuer();
void komodo_passport_iteration();

bool AppInit(int argc, char* argv[])
{
Expand Down
25 changes: 20 additions & 5 deletions src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1911,6 +1911,7 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler)

RegisterValidationInterface(pwalletMain);

LOCK(cs_main);
CBlockIndex *pindexRescan = chainActive.Tip();
if (clearWitnessCaches || GetBoolArg("-rescan", false))
{
Expand Down Expand Up @@ -2032,10 +2033,21 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler)
vImportFiles.push_back(strFile);
}
threadGroup.create_thread(boost::bind(&ThreadImport, vImportFiles));
if (chainActive.Tip() == NULL) {
LogPrintf("Waiting for genesis block to be imported...\n");
while (!fRequestShutdown && chainActive.Tip() == NULL)
MilliSleep(10);
{
CBlockIndex *tip = nullptr;
{
LOCK(cs_main);
tip = chainActive.Tip();
}
if (tip == nullptr) {
LogPrintf("Waiting for genesis block to be imported...\n");
while (!fRequestShutdown && tip == nullptr)
{
MilliSleep(10);
LOCK(cs_main);
tip = chainActive.Tip();
}
}
}

// ********************************************************* Step 11: start node
Expand All @@ -2048,7 +2060,10 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler)

//// debug print
LogPrintf("mapBlockIndex.size() = %u\n", mapBlockIndex.size());
LogPrintf("nBestHeight = %d\n", chainActive.Height());
{
LOCK(cs_main);
LogPrintf("nBestHeight = %d\n", chainActive.Height());
}
#ifdef ENABLE_WALLET
RescanWallets();

Expand Down
64 changes: 11 additions & 53 deletions src/komodo_bitcoind.h
Original file line number Diff line number Diff line change
Expand Up @@ -893,73 +893,31 @@ int32_t komodo_blockload(CBlock& block,CBlockIndex *pindex)

uint32_t komodo_chainactive_timestamp()
{
if ( chainActive.LastTip() != 0 )
return((uint32_t)chainActive.LastTip()->GetBlockTime());
else return(0);
CBlockIndex *index = chainActive.LastTip();
if ( index != nullptr )
return (uint32_t)index->GetBlockTime();
return 0;
}

CBlockIndex *komodo_chainactive(int32_t height)
{
if ( chainActive.LastTip() != 0 )
CBlockIndex *index = chainActive.LastTip();
if ( index != nullptr )
{
if ( height <= chainActive.LastTip()->GetHeight() )
if ( height <= index->GetHeight() )
return(chainActive[height]);
// else fprintf(stderr,"komodo_chainactive height %d > active.%d\n",height,chainActive.LastTip()->GetHeight());
}
//fprintf(stderr,"komodo_chainactive null chainActive.LastTip() height %d\n",height);
return(0);
return nullptr;
}

uint32_t komodo_heightstamp(int32_t height)
{
CBlockIndex *ptr;
if ( height > 0 && (ptr= komodo_chainactive(height)) != 0 )
return(ptr->nTime);
//else fprintf(stderr,"komodo_heightstamp null ptr for block.%d\n",height);
return(0);
if ( height > 0 && (ptr= komodo_chainactive(height)) != nullptr )
return ptr->nTime;
return 0;
}

/*void komodo_pindex_init(CBlockIndex *pindex,int32_t height) gets data corrupted
{
int32_t i,num; uint8_t pubkeys[64][33]; CBlock block;
if ( pindex->didinit != 0 )
return;
//printf("pindex.%d komodo_pindex_init notary.%d from height.%d\n",pindex->GetHeight(),pindex->notaryid,height);
if ( pindex->didinit == 0 )
{
pindex->notaryid = -1;
if ( KOMODO_LOADINGBLOCKS == 0 )
memset(pindex->pubkey33,0xff,33);
else memset(pindex->pubkey33,0,33);
if ( komodo_blockload(block,pindex) == 0 )
{
komodo_block2pubkey33(pindex->pubkey33,&block);
//for (i=0; i<33; i++)
// fprintf(stderr,"%02x",pindex->pubkey33[i]);
//fprintf(stderr," set pubkey at height %d/%d\n",pindex->GetHeight(),height);
//if ( pindex->pubkey33[0] == 2 || pindex->pubkey33[0] == 3 )
// pindex->didinit = (KOMODO_LOADINGBLOCKS == 0);
} // else fprintf(stderr,"error loading block at %d/%d",pindex->GetHeight(),height);
}
if ( pindex->didinit != 0 && pindex->GetHeight() >= 0 && (num= komodo_notaries(pubkeys,(int32_t)pindex->GetHeight(),(uint32_t)pindex->nTime)) > 0 )
{
for (i=0; i<num; i++)
{
if ( memcmp(pubkeys[i],pindex->pubkey33,33) == 0 )
{
pindex->notaryid = i;
break;
}
}
if ( 0 && i == num )
{
for (i=0; i<33; i++)
fprintf(stderr,"%02x",pindex->pubkey33[i]);
fprintf(stderr," unmatched pubkey at height %d/%d\n",pindex->GetHeight(),height);
}
}
}*/

void komodo_index2pubkey33(uint8_t *pubkey33,CBlockIndex *pindex,int32_t height)
{
int32_t num,i; CBlock block;
Expand Down
15 changes: 11 additions & 4 deletions src/komodo_gateway.h
Original file line number Diff line number Diff line change
Expand Up @@ -1446,12 +1446,16 @@ void komodo_passport_iteration()
fprintf(stderr,"[%s] PASSPORT iteration waiting for KOMODO_INITDONE\n",ASSETCHAINS_SYMBOL);
sleep(3);
}
if ( komodo_chainactive_timestamp() > lastinterest )
uint32_t chainactive_timestamp = 0;
{
LOCK(cs_main);
chainactive_timestamp = komodo_chainactive_timestamp();
}
if ( chainactive_timestamp > lastinterest )
{
if ( ASSETCHAINS_SYMBOL[0] == 0 )
komodo_interestsum();
//komodo_longestchain();
lastinterest = komodo_chainactive_timestamp();
lastinterest = chainactive_timestamp;
}
refsp = komodo_stateptr(symbol,dest);
if ( ASSETCHAINS_SYMBOL[0] == 0 || strcmp(ASSETCHAINS_SYMBOL,"KMDCC") == 0 )
Expand Down Expand Up @@ -1561,7 +1565,10 @@ void komodo_passport_iteration()
komodo_statefname(fname,baseid<32?base:(char *)"",(char *)"realtime");
if ( (fp= fopen(fname,"wb")) != 0 )
{
buf[0] = (uint32_t)chainActive.LastTip()->GetHeight();
{
LOCK(cs_main);
buf[0] = (uint32_t)chainActive.LastTip()->GetHeight();
}
buf[1] = (uint32_t)komodo_longestchain();
if ( buf[0] != 0 && buf[0] == buf[1] )
{
Expand Down
39 changes: 29 additions & 10 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -354,10 +354,14 @@ namespace {

int GetHeight()
{
CBlockIndex *pindex;
if ( (pindex= chainActive.LastTip()) != 0 )
CBlockIndex *pindex = nullptr;
{
LOCK(cs_main);
pindex = chainActive.LastTip();
}
if ( pindex != nullptr )
return pindex->GetHeight();
else return(0);
return 0;
}

void UpdatePreferredDownload(CNode* node, CNodeState* state)
Expand Down Expand Up @@ -4613,6 +4617,11 @@ static bool ActivateBestChainStep(bool fSkipdpow, CValidationState &state, CBloc
return true;
}

CBlockIndex *GetTipWithLock()
{
LOCK(cs_main);
return chainActive.Tip();
}
/**
* Make the best chain active, in multiple steps. The result is either failure
* or an activated best chain. pblock is either NULL or a pointer to a block
Expand Down Expand Up @@ -4650,17 +4659,23 @@ bool ActivateBestChain(bool fSkipdpow, CValidationState &state, CBlock *pblock)
nBlockEstimate = Checkpoints::GetTotalBlocksEstimate(chainParams.Checkpoints());
// Don't relay blocks if pruning -- could cause a peer to try to download, resulting
// in a stalled download if the block file is pruned before the request.
if (nLocalServices & NODE_NETWORK) {
if (nLocalServices & NODE_NETWORK)
{
int ht = 0;
{
LOCK(cs_main);
ht = chainActive.Height();
}
LOCK(cs_vNodes);
BOOST_FOREACH(CNode* pnode, vNodes)
if (chainActive.Height() > (pnode->nStartingHeight != -1 ? pnode->nStartingHeight - 2000 : nBlockEstimate))
pnode->PushInventory(CInv(MSG_BLOCK, hashNewTip));
for(CNode* pnode : vNodes)
if (ht > (pnode->nStartingHeight != -1 ? pnode->nStartingHeight - 2000 : nBlockEstimate))
pnode->PushInventory(CInv(MSG_BLOCK, hashNewTip));
}
// Notify external listeners about the new tip.
GetMainSignals().UpdatedBlockTip(pindexNewTip);
uiInterface.NotifyBlockTip(hashNewTip);
} //else fprintf(stderr,"initial download skips propagation\n");
} while(pindexMostWork != chainActive.Tip());
} while(pindexMostWork != GetTipWithLock());
CheckBlockIndex();

// Write changes periodically to disk, after relay.
Expand Down Expand Up @@ -6108,8 +6123,11 @@ bool static LoadBlockIndexDB()
{
const CChainParams& chainparams = Params();
LogPrintf("%s: start loading guts\n", __func__);
if (!pblocktree->LoadBlockIndexGuts())
return false;
{
LOCK(cs_main);
if (!pblocktree->LoadBlockIndexGuts())
return false;
}
LogPrintf("%s: loaded guts\n", __func__);
boost::this_thread::interruption_point();

Expand Down Expand Up @@ -6264,6 +6282,7 @@ bool static LoadBlockIndexDB()
if (it == mapBlockIndex.end())
return true;

LOCK(cs_main);
chainActive.SetTip(it->second);

// Set hashFinalSproutRoot for the end of best chain
Expand Down
8 changes: 7 additions & 1 deletion src/sync.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,13 @@ std::string LocksHeld()

void AssertLockHeldInternal(const char* pszName, const char* pszFile, int nLine, void* cs)
{
BOOST_FOREACH (const PAIRTYPE(void*, CLockLocation) & i, *lockstack)
if ( lockstack.get() == nullptr)
{
fprintf(stderr, "Assertion failed: Thread does not have a lockstack. Method: %s in file %s Line %d\n",
pszName, pszFile, nLine);
abort();
}
for (const auto &i : *lockstack)
if (i.first == cs)
return;
fprintf(stderr, "Assertion failed: lock %s not held in %s:%i; locks held:\n%s", pszName, pszFile, nLine, LocksHeld().c_str());
Expand Down
18 changes: 0 additions & 18 deletions src/test-komodo/test_cchain.cpp

This file was deleted.

0 comments on commit 0cba296

Please sign in to comment.