From 7d54574d4c2df7ed38090b6364af7684615b5eda Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 23 Nov 2018 04:17:34 -1100 Subject: [PATCH] komodo_getblockindex instead of mapblockindex[] to prevent creation of new entry on miss --- src/cc/CCinclude.h | 1 + src/cc/CCutils.cpp | 2 +- src/crosschain.cpp | 2 +- src/komodo.h | 2 +- src/komodo_bitcoind.h | 10 ++++++++-- src/main.cpp | 4 ++-- src/wallet/rpcwallet.cpp | 2 +- 7 files changed, 15 insertions(+), 8 deletions(-) diff --git a/src/cc/CCinclude.h b/src/cc/CCinclude.h index 6f8a910f0..fea3f7e48 100644 --- a/src/cc/CCinclude.h +++ b/src/cc/CCinclude.h @@ -101,6 +101,7 @@ struct oracleprice_info extern CWallet* pwalletMain; #endif bool GetAddressUnspent(uint160 addressHash, int type,std::vector > &unspentOutputs); +CBlockIndex *komodo_getblockindex(uint256 hash); static const uint256 zeroid; bool myGetTransaction(const uint256 &hash, CTransaction &txOut, uint256 &hashBlock); diff --git a/src/cc/CCutils.cpp b/src/cc/CCutils.cpp index 50845f9ef..b6abd5474 100644 --- a/src/cc/CCutils.cpp +++ b/src/cc/CCutils.cpp @@ -436,7 +436,7 @@ int64_t CCduration(int32_t &numblocks,uint256 txid) //fprintf(stderr,"CCduration no hashBlock for txid %s\n",uint256_str(str,txid)); return(0); } - else if ( (pindex= mapBlockIndex[hashBlock]) == 0 || (txtime= pindex->nTime) == 0 || (txheight= pindex->GetHeight()) <= 0 ) + else if ( (pindex= komodo_getblockindex(hashBlock)) == 0 || (txtime= pindex->nTime) == 0 || (txheight= pindex->GetHeight()) <= 0 ) { fprintf(stderr,"CCduration no txtime %u or txheight.%d %p for txid %s\n",txtime,txheight,pindex,uint256_str(str,txid)); return(0); diff --git a/src/crosschain.cpp b/src/crosschain.cpp index c0e5cfa9b..9d564b6b3 100644 --- a/src/crosschain.cpp +++ b/src/crosschain.cpp @@ -266,7 +266,7 @@ TxProof GetAssetchainProof(uint256 hash) if (blockHash.IsNull()) throw std::runtime_error("tx still in mempool"); - blockIndex = mapBlockIndex[blockHash]; + blockIndex = komodo_getblockindex(blockHash); int h = blockIndex->GetHeight(); // The assumption here is that the first notarisation for a height GTE than // the transaction block height will contain the corresponding MoM. If there diff --git a/src/komodo.h b/src/komodo.h index b0a122fdd..4c9edee89 100644 --- a/src/komodo.h +++ b/src/komodo.h @@ -510,7 +510,7 @@ int32_t komodo_validate_chain(uint256 srchash,int32_t notarized_height) static int32_t last_rewind; int32_t rewindtarget; CBlockIndex *pindex; struct komodo_state *sp; char symbol[KOMODO_ASSETCHAIN_MAXLEN],dest[KOMODO_ASSETCHAIN_MAXLEN]; if ( (sp= komodo_stateptr(symbol,dest)) == 0 ) return(0); - if ( IsInitialBlockDownload() == 0 && ((pindex= mapBlockIndex[srchash]) == 0 || pindex->GetHeight() != notarized_height) ) + 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; diff --git a/src/komodo_bitcoind.h b/src/komodo_bitcoind.h index ba817fc45..a78ded447 100644 --- a/src/komodo_bitcoind.h +++ b/src/komodo_bitcoind.h @@ -578,6 +578,12 @@ uint32_t komodo_txtime(uint64_t *valuep,uint256 hash, int32_t n, char *destaddr) return(tx.nLockTime); } +CBlockIndex *komodo_getblockindex(uint256 hash) +{ + BlockMap::const_iterator it = mapBlockIndex.find(hash); + return((it != mapBlockIndex.end()) ? it->second : NULL); +} + uint32_t komodo_txtime2(uint64_t *valuep,uint256 hash,int32_t n,char *destaddr) { CTxDestination address; CBlockIndex *pindex; CTransaction tx; uint256 hashBlock; uint32_t txtime = 0; @@ -591,7 +597,7 @@ uint32_t komodo_txtime2(uint64_t *valuep,uint256 hash,int32_t n,char *destaddr) //fprintf(stderr,"ERROR: %s/v%d locktime.%u\n",hash.ToString().c_str(),n,(uint32_t)tx.nLockTime); return(0); } - if ( (pindex= mapBlockIndex[hashBlock]) != 0 ) + if ( (pindex= komodo_getblockindex(hashBlock)) != 0 ) txtime = pindex->nTime; else txtime = tx.nLockTime; //fprintf(stderr,"%s/v%d locktime.%u\n",hash.ToString().c_str(),n,(uint32_t)tx.nLockTime); @@ -1024,7 +1030,7 @@ uint32_t komodo_interest_args(uint32_t *txheighttimep,int32_t *txheightp,uint32_ uint32_t locktime = 0; if ( n < tx.vout.size() ) { - if ( (pindex= mapBlockIndex[hashBlock]) != 0 ) + if ( (pindex= komodo_getblockindex(hashBlock)) != 0 ) { *valuep = tx.vout[n].nValue; *txheightp = pindex->GetHeight(); diff --git a/src/main.cpp b/src/main.cpp index b29803d52..ad2328218 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -5065,7 +5065,7 @@ CBlockIndex *komodo_ensure(CBlock *pblock, uint256 hash) CBlockIndex *oldkomodo_ensure(CBlock *pblock, uint256 hash) { CBlockIndex *pindex=0,*previndex=0; - if ( (pindex = mapBlockIndex[hash]) == 0 ) + if ( (pindex = komodo_getblockindex(hash)) == 0 ) { pindex = new CBlockIndex(); if (!pindex) @@ -7720,7 +7720,7 @@ bool SendMessages(CNode* pto, bool fSendTrickle) } } /*CBlockIndex *pindex; - if ( komodo_requestedhash != zero && komodo_requestedcount < 16 && (pindex= mapBlockIndex[komodo_requestedhash]) != 0 ) + if ( komodo_requestedhash != zero && komodo_requestedcount < 16 && (pindex= komodo_getblockindex(komodo_requestedhash)) != 0 ) { LogPrint("net","komodo_requestedhash.%d request %s to nodeid.%d\n",komodo_requestedcount,komodo_requestedhash.ToString().c_str(),pto->GetId()); fprintf(stderr,"komodo_requestedhash.%d request %s to nodeid.%d\n",komodo_requestedcount,komodo_requestedhash.ToString().c_str(),pto->GetId()); diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index ef4bc809f..05c15c97a 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -5172,7 +5172,7 @@ int32_t komodo_staked(CMutableTransaction &txNew,uint32_t nBits,uint32_t *blockt { if ( IsMine(*pwalletMain,address) == 0 ) continue; - if ( GetTransaction(out.tx->GetHash(),tx,hashBlock,true) != 0 && (pindex= mapBlockIndex[hashBlock]) != 0 ) + if ( GetTransaction(out.tx->GetHash(),tx,hashBlock,true) != 0 && (pindex= komodo_getblockindex(hashBlock)) != 0 ) { array = komodo_addutxo(array,&numkp,&maxkp,(uint32_t)pindex->nTime,(uint64_t)nValue,out.tx->GetHash(),out.i,(char *)CBitcoinAddress(address).ToString().c_str(),hashbuf,(CScript)pk); }