From d565e7b73c45834b03d8707a029192a92f0d0fee Mon Sep 17 00:00:00 2001 From: miketout Date: Sun, 14 Oct 2018 11:59:13 -0700 Subject: [PATCH] Eliminate mapBlockIndex risk of nulls across VRSC --- src/cc/StakeGuard.cpp | 3 ++- src/komodo_bitcoind.h | 35 ++++++++++++++++++++--------------- src/main.cpp | 2 +- src/rpc/rawtransaction.cpp | 4 ++-- src/txdb.cpp | 7 ++++--- src/wallet/wallet.cpp | 3 ++- 6 files changed, 31 insertions(+), 23 deletions(-) diff --git a/src/cc/StakeGuard.cpp b/src/cc/StakeGuard.cpp index 04669aa4d..dbc14ebde 100644 --- a/src/cc/StakeGuard.cpp +++ b/src/cc/StakeGuard.cpp @@ -148,7 +148,8 @@ bool ValidateStakeTransaction(const CTransaction &stakeTx, CStakeParams &stakePa CBlockIndex *pindex; if (myGetTransaction(stakeTx.vin[0].prevout.hash, srcTx, blkHash)) { - if ((pindex = mapBlockIndex[blkHash]) != NULL) + BlockMap::const_iterator it = mapBlockIndex.find(blkHash); + if (it != mapBlockIndex.end() && (pindex = it->second) != NULL) { std::vector> vAddr = std::vector>(); diff --git a/src/komodo_bitcoind.h b/src/komodo_bitcoind.h index 0e9105573..b9e9efdf4 100644 --- a/src/komodo_bitcoind.h +++ b/src/komodo_bitcoind.h @@ -663,14 +663,15 @@ int32_t komodo_is_notarytx(const CTransaction& tx) int32_t komodo_block2height(CBlock *block) { static uint32_t match,mismatch; - int32_t i,n,height2=-1,height = 0; uint8_t *ptr; CBlockIndex *pindex; - if ( (pindex= mapBlockIndex[block->GetHash()]) != 0 ) + int32_t i,n,height2=-1,height = 0; uint8_t *ptr; CBlockIndex *pindex = NULL; + BlockMap::const_iterator it = mapBlockIndex.find(block->GetHash()); + if ( it != mapBlockIndex.end() && (pindex = it->second) != 0 ) { height2 = (int32_t)pindex->GetHeight(); if ( height2 >= 0 ) return(height2); } - if ( block != 0 && block->vtx[0].vin.size() > 0 ) + if ( pindex && block != 0 && block->vtx[0].vin.size() > 0 ) { ptr = (uint8_t *)&block->vtx[0].vin[0].scriptSig[0]; if ( ptr != 0 && block->vtx[0].vin[0].scriptSig.size() > 5 ) @@ -989,7 +990,8 @@ int32_t komodo_checkpoint(int32_t *notarized_heightp,int32_t nHeight,uint256 has return(-1); notarized_height = komodo_notarizeddata(pindex->GetHeight(),¬arized_hash,¬arized_desttxid); *notarized_heightp = notarized_height; - if ( notarized_height >= 0 && notarized_height <= pindex->GetHeight() && (notary= mapBlockIndex[notarized_hash]) != 0 ) + BlockMap::const_iterator it; + if ( notarized_height >= 0 && notarized_height <= pindex->GetHeight() && (it = mapBlockIndex.find(notarized_hash)) != mapBlockIndex.end() && (notary = it->second) != NULL ) { //printf("nHeight.%d -> (%d %s)\n",pindex->Tip()->GetHeight(),notarized_height,notarized_hash.ToString().c_str()); if ( notary->GetHeight() == notarized_height ) // if notarized_hash not in chain, reorg @@ -1380,7 +1382,8 @@ int32_t komodo_is_PoSblock(int32_t slowflag,int32_t height,CBlock *pblock,arith_ CBlockIndex *previndex,*pindex; char voutaddr[64],destaddr[64]; uint256 txid; uint32_t txtime,prevtime=0; int32_t vout,PoSperc,txn_count,eligible=0,isPoS = 0,segid; uint64_t value; CTxDestination voutaddress; if ( ASSETCHAINS_STAKED == 100 && height <= 10 ) return(1); - pindex = mapBlockIndex[pblock->GetHash()]; + BlockMap::const_iterator it = mapBlockIndex.find(pblock->GetHash()); + pindex = it != mapBlockIndex.end() ? it->second : NULL; if ( pindex != 0 && pindex->segid >= -1 ) { if ( pindex->segid == -1 ) @@ -1390,11 +1393,10 @@ int32_t komodo_is_PoSblock(int32_t slowflag,int32_t height,CBlock *pblock,arith_ txn_count = pblock->vtx.size(); if ( txn_count > 1 && pblock->vtx[txn_count-1].vin.size() == 1 && pblock->vtx[txn_count-1].vout.size() == 1 ) { - if ( prevtime == 0 ) - { - if ( (previndex= mapBlockIndex[pblock->hashPrevBlock]) != 0 ) - prevtime = (uint32_t)previndex->nTime; - } + it = mapBlockIndex.find(pblock->hashPrevBlock); + if ( it != mapBlockIndex.end() && (previndex = it->second) != NULL ) + prevtime = (uint32_t)previndex->nTime; + txid = pblock->vtx[txn_count-1].vin[0].prevout.hash; vout = pblock->vtx[txn_count-1].vin[0].prevout.n; if ( prevtime != 0 ) @@ -1573,17 +1575,19 @@ bool verusCheckPOSBlock(int32_t slowflag, CBlock *pblock, int32_t height) if ((!newPOSEnforcement || posHash == hash) && hash <= target) { - if ((mapBlockIndex.count(blkHash) == 0) || - !(pastBlockIndex = mapBlockIndex[blkHash]) || + BlockMap::const_iterator it = mapBlockIndex.find(blkHash); + if ((it == mapBlockIndex.end()) || + !(pastBlockIndex = it->second) || (height - pastBlockIndex->GetHeight()) < VERUS_MIN_STAKEAGE) { - fprintf(stderr,"ERROR: invalid PoS block %s - stake transaction too new\n",blkHash.ToString().c_str()); + fprintf(stderr,"ERROR: invalid PoS block %s - stake source too new or not found\n",blkHash.ToString().c_str()); } else { // make sure we have the right target CBlockIndex *previndex; - if (!(previndex = mapBlockIndex[pblock->hashPrevBlock])) + it = mapBlockIndex.find(pblock->hashPrevBlock); + if (it == mapBlockIndex.end() || !(previndex = it->second)) { fprintf(stderr,"ERROR: invalid PoS block %s - no prev block found\n",blkHash.ToString().c_str()); } @@ -1721,7 +1725,8 @@ int32_t komodo_checkPOW(int32_t slowflag,CBlock *pblock,int32_t height) fprintf(stderr,"height.%d slowflag.%d possible.%d cmp.%d\n",height,slowflag,possible,bhash > bnTarget); return(0); } - if ( (pprev= mapBlockIndex[pblock->hashPrevBlock]) != 0 ) + BlockMap::const_iterator it = mapBlockIndex.find(pblock->hashPrevBlock); + if ( it != mapBlockIndex.end() && (pprev= it->second) != 0 ) height = pprev->GetHeight() + 1; if ( height == 0 ) return(0); diff --git a/src/main.cpp b/src/main.cpp index 044899712..ac6ccd38a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4202,7 +4202,7 @@ CBlockIndex* AddToBlockIndex(const CBlockHeader& block) } if (!vrit.empty()) { - // + printf("found %d NULL blocks in mapBlockIndex\n", vrit.size()); } */ diff --git a/src/rpc/rawtransaction.cpp b/src/rpc/rawtransaction.cpp index e06a72b4c..eecfab83c 100644 --- a/src/rpc/rawtransaction.cpp +++ b/src/rpc/rawtransaction.cpp @@ -660,8 +660,8 @@ UniValue verifytxoutproof(const UniValue& params, bool fHelp) return res; LOCK(cs_main); - - if (!mapBlockIndex.count(merkleBlock.header.GetHash()) || !chainActive.Contains(mapBlockIndex[merkleBlock.header.GetHash()])) + uint256 idx = merkleBlock.header.GetHash(); + if (!mapBlockIndex.count(merkleBlock.header.GetHash()) || (mapBlockIndex.count(idx) && !chainActive.Contains(mapBlockIndex[idx]))) throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block not found in chain"); BOOST_FOREACH(const uint256& hash, vMatch) diff --git a/src/txdb.cpp b/src/txdb.cpp index ba437b5ff..42c320675 100644 --- a/src/txdb.cpp +++ b/src/txdb.cpp @@ -650,10 +650,11 @@ bool CBlockTreeDB::ReadFlag(const std::string &name, bool &fValue) { void komodo_index2pubkey33(uint8_t *pubkey33,CBlockIndex *pindex,int32_t height); bool CBlockTreeDB::blockOnchainActive(const uint256 &hash) { - CBlockIndex* pblockindex = mapBlockIndex[hash]; + BlockMap::const_iterator it = mapBlockIndex.find(hash); + CBlockIndex* pblockindex = it != mapBlockIndex.end() ? it->second : NULL; - if (!chainActive.Contains(pblockindex)) { - return false; + if (!pblockindex || !chainActive.Contains(pblockindex)) { + return false; } return true; diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index 9ee603fb1..71ae17e27 100644 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -1432,7 +1432,8 @@ int32_t CWallet::VerusStakeTransaction(CBlock *pBlock, CMutableTransaction &txNe if (!GetTransaction(stakeSource.GetHash(), stakeSource, srcBlock)) return 0; - if ((pSrcIndex = mapBlockIndex[srcBlock]) == 0) + BlockMap::const_iterator it = mapBlockIndex.find(srcBlock); + if (it == mapBlockIndex.end() || (pSrcIndex = it->second) == 0) return 0; // !! DISABLE THIS FOR RELEASE: THIS MAKES A CHEAT TRANSACTION FOR EVERY STAKE FOR TESTING