Permalink
Browse files

Major update regarding locking

  • Loading branch information...
Tranz5 committed Aug 14, 2014
1 parent 6f8d5ad commit 76f376fefdf65199e8d57c2698b3bff14176863b
View
@@ -467,6 +467,8 @@ CTransaction::GetLegacySigOpCount() const
int CMerkleTx::SetMerkleBranch(const CBlock* pblock)
{
AssertLockHeld(cs_main);
if (fClient)
{
if (hashBlock == 0)
@@ -613,6 +615,7 @@ int64 CTransaction::GetMinFee(unsigned int nBlockSize, bool fAllowFree,
bool CTxMemPool::accept(CTxDB& txdb, CTransaction &tx, bool fCheckInputs,
bool* pfMissingInputs)
{
AssertLockHeld(cs_main);
if (pfMissingInputs)
*pfMissingInputs = false;
@@ -825,6 +828,7 @@ int CMerkleTx::GetDepthInMainChainINTERNAL(CBlockIndex* &pindexRet) const
{
if (hashBlock == 0 || nIndex == -1)
return 0;
AssertLockHeld(cs_main);
// Find the block it claims to be in
map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.find(hashBlock);
@@ -848,6 +852,7 @@ int CMerkleTx::GetDepthInMainChainINTERNAL(CBlockIndex* &pindexRet) const
int CMerkleTx::GetDepthInMainChain(CBlockIndex* &pindexRet) const
{
AssertLockHeld(cs_main);
int nResult = GetDepthInMainChainINTERNAL(pindexRet);
if (nResult == 0 && !mempool.exists(GetHash()))
return -1; // Not in chain, not in mempool
@@ -890,7 +895,6 @@ bool CWalletTx::AcceptWalletTransaction(CTxDB& txdb, bool fCheckInputs)
{
{
LOCK(mempool.cs);
// Add previous supporting transactions first
BOOST_FOREACH(CMerkleTx& tx, vtxPrev)
{
@@ -934,12 +938,8 @@ bool GetTransaction(const uint256 &hash, CTransaction &tx, uint256 &hashBlock)
{
LOCK(cs_main);
{
LOCK(mempool.cs);
if (mempool.exists(hash))
{
tx = mempool.lookup(hash);
if (mempool.lookup(hash, tx))
return true;
}
}
CTxDB txdb("r");
CTxIndex txindex;
@@ -1235,6 +1235,7 @@ int GetNumBlocksOfPeers()
bool IsInitialBlockDownload()
{
LOCK(cs_main);
if (pindexBest == NULL || nBestHeight < Checkpoints::GetTotalBlocksEstimate())
return true;
static int64 nLastUpdate;
@@ -1365,12 +1366,9 @@ bool CTransaction::FetchInputs(CTxDB& txdb, const map<uint256, CTxIndex>& mapTes
if (!fFound || txindex.pos == CDiskTxPos(1,1,1))
{
// Get prev tx from single transactions in memory
{
LOCK(mempool.cs);
if (!mempool.exists(prevout.hash))
return error("FetchInputs() : %s mempool Tx prev not found %s", GetHash().ToString().substr(0,10).c_str(), prevout.hash.ToString().substr(0,10).c_str());
txPrev = mempool.lookup(prevout.hash);
}
if (!mempool.lookup(prevout.hash, txPrev))
return error("FetchInputs() : %s mempool Tx prev not found %s", GetHash().ToString().substr(0,10).c_str(), prevout.hash.ToString().substr(0,10).c_str());
if (!fFound)
txindex.vSpent.resize(txPrev.vout.size());
}
@@ -1562,15 +1560,14 @@ bool CTransaction::ClientConnectInputs()
// Take over previous transactions' spent pointers
{
LOCK(mempool.cs);
int64 nValueIn = 0;
for (unsigned int i = 0; i < vin.size(); i++)
{
// Get prev tx from single transactions in memory
COutPoint prevout = vin[i].prevout;
if (!mempool.exists(prevout.hash))
CTransaction txPrev;
if (!mempool.lookup(prevout.hash, txPrev))
return false;
CTransaction& txPrev = mempool.lookup(prevout.hash);
if (prevout.n >= txPrev.vout.size())
return false;
@@ -2109,6 +2106,8 @@ bool CBlock::AddToBlockIndex(unsigned int nFile, unsigned int nBlockPos, const u
if (!txdb.TxnCommit())
return false;
LOCK(cs_main);
// New best
if (pindexNew->nChainTrust > nBestChainTrust)
if (!SetBestChain(txdb, pindexNew))
@@ -2237,6 +2236,8 @@ bool CBlock::CheckBlock(bool fCheckPOW, bool fCheckMerkleRoot, bool fCheckSig) c
bool CBlock::AcceptBlock()
{
AssertLockHeld(cs_main);
// Check for duplicate
uint256 hash = GetHash();
if (mapBlockIndex.count(hash))
@@ -2353,6 +2354,8 @@ uint256 CBlockIndex::GetBlockTrust() const
bool ProcessBlock(CNode* pfrom, CBlock* pblock)
{
AssertLockHeld(cs_main);
// Check for duplicate
uint256 hash = pblock->GetHash();
if (mapBlockIndex.count(hash))
@@ -2669,6 +2672,8 @@ FILE* AppendBlockFile(unsigned int& nFileRet)
bool LoadBlockIndex(bool fAllowNew)
{
LOCK(cs_main);
if (fTestNet)
{
pchMessageStart[0] = 0xcd;
@@ -2804,6 +2809,7 @@ bool LoadBlockIndex(bool fAllowNew)
void PrintBlockTree()
{
AssertLockHeld(cs_main);
// pre-compute tree structure
map<CBlockIndex*, vector<CBlockIndex*> > mapNext;
for (map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.begin(); mi != mapBlockIndex.end(); ++mi)
@@ -3037,10 +3043,7 @@ bool static AlreadyHave(CTxDB& txdb, const CInv& inv)
case MSG_TX:
{
bool txInMap = false;
{
LOCK(mempool.cs);
txInMap = (mempool.exists(inv.hash));
}
txInMap = mempool.exists(inv.hash);
return txInMap ||
mapOrphanTransactions.count(inv.hash) ||
txdb.ContainsTx(inv.hash);
@@ -3399,9 +3402,8 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv)
}
}
if (!pushed && inv.type == MSG_TX) {
LOCK(mempool.cs);
if (mempool.exists(inv.hash)) {
CTransaction tx = mempool.lookup(inv.hash);
CTransaction tx;
if (mempool.lookup(inv.hash, tx)) {
CDataStream ss(SER_NETWORK, PROTOCOL_VERSION);
ss.reserve(1000);
ss << tx;
View
@@ -494,6 +494,7 @@ class CTransaction
bool IsFinal(int nBlockHeight=0, int64 nBlockTime=0) const
{
AssertLockHeld(cs_main);
// Time based nLockTime implemented in 0.1.6
if (nLockTime == 0)
return true;
@@ -1617,20 +1618,25 @@ class CTxMemPool
void clear();
void queryHashes(std::vector<uint256>& vtxid);
unsigned long size()
unsigned long size() const
{
LOCK(cs);
return mapTx.size();
}
bool exists(uint256 hash)
bool exists(uint256 hash) const
{
LOCK(cs);
return (mapTx.count(hash) != 0);
}
CTransaction& lookup(uint256 hash)
bool lookup(uint256 hash, CTransaction& result) const
{
return mapTx[hash];
LOCK(cs);
std::map<uint256, CTransaction>::const_iterator i = mapTx.find(hash);
if (i == mapTx.end()) return false;
result = i->second;
return true;
}
};
View
@@ -67,7 +67,8 @@ BitcoinGUI::BitcoinGUI(QWidget *parent):
aboutQtAction(0),
trayIcon(0),
notificator(0),
rpcConsole(0)
rpcConsole(0),
nWeight(0)
{
resize(850, 550);
setWindowTitle(tr("HoboNickels") + " - " + tr("Wallet"));
@@ -663,7 +664,7 @@ void BitcoinGUI::connectionIconClicked()
void BitcoinGUI::stakingIconClicked()
{
uint64 nMinWeight = 0, nMaxWeight = 0, nWeight = 0;
uint64 nMinWeight = 0, nMaxWeight = 0;
walletStack->getStakeWeight(nMinWeight,nMaxWeight,nWeight);
int unit = clientModel->getOptionsModel()->getDisplayUnit();
View
@@ -122,6 +122,8 @@ class BitcoinGUI : public QMainWindow
QMovie *syncIconMovie;
quint64 nWeight;
/** Create the main UI actions. */
void createActions();
/** Create the menu bar and sub-menus. */
View
@@ -63,6 +63,7 @@ QVector<CNodeStats> ClientModel::getPeerStats()
int ClientModel::getNumBlocks() const
{
LOCK(cs_main);
return nBestHeight;
}
@@ -117,6 +118,7 @@ int ClientModel::getStakeTargetSpacing()
QDateTime ClientModel::getLastBlockDate(bool fProofofStake) const
{
LOCK(cs_main);
if (pindexBest && !fProofofStake)
return QDateTime::fromTime_t(pindexBest->GetBlockTime());
else if (pindexBest && fProofofStake)
@@ -127,6 +129,13 @@ QDateTime ClientModel::getLastBlockDate(bool fProofofStake) const
void ClientModel::updateTimer()
{
// Get required lock upfront. This avoids the GUI from getting stuck on
// periodical polls if the core is holding the locks for a longer time -
// for example, during a wallet rescan.
TRY_LOCK(cs_main, lockMain);
if(!lockMain)
return;
// Some quantities (such as number of blocks) change so fast that we don't want to be notified for each change.
// Periodically check and update with a timer.
int newNumBlocks = getNumBlocks();
Oops, something went wrong.

0 comments on commit 76f376f

Please sign in to comment.