Skip to content

Commit

Permalink
Implement lock for GridcoinConnectBlock to queued txindex write.
Browse files Browse the repository at this point in the history
  • Loading branch information
jamescowens committed Nov 30, 2023
1 parent 44e832c commit 3dd263a
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 21 deletions.
17 changes: 14 additions & 3 deletions src/gridcoin/voting/result.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -864,6 +864,7 @@ class VoteCounter
{
CTransaction tx;

/*
bool read_tx_success = false;
// This is very ugly. In testing for implement poll expiration reminders PR2716, there is an issue with ReadDiskTx
Expand All @@ -884,11 +885,21 @@ class VoteCounter
}
}
if (!read_tx_success) {
LogPrintf("WARN: %s: failed to read vote tx after 10 tries", __func__);
throw InvalidVoteError();
if (!read_tx_success) {
LogPrintf("WARN: %s: failed to read vote tx after 10 tries", __func__);
throw InvalidVoteError();
}
*/

{
LOCK(cs_tx_val_commit_to_disk);

if (!m_txdb.ReadDiskTx(txid, tx)) {
LogPrintf("WARN: %s: failed to read vote tx.", __func__);
}
}


if (tx.nTime < m_poll.m_timestamp) {
LogPrintf("WARN: %s: tx earlier than poll", __func__);
throw InvalidVoteError();
Expand Down
1 change: 1 addition & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ CCriticalSection cs_setpwalletRegistered;
set<CWallet*> setpwalletRegistered;

CCriticalSection cs_main;
CCriticalSection cs_tx_val_commit_to_disk;

CTxMemPool mempool;

Expand Down
1 change: 1 addition & 0 deletions src/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ typedef std::unordered_map<uint256, CBlockIndex*, BlockHasher> BlockMap;

extern CScript COINBASE_FLAGS;
extern CCriticalSection cs_main;
extern CCriticalSection cs_tx_val_commit_to_disk;
extern BlockMap mapBlockIndex;
extern CBlockIndex* pindexGenesisBlock;
extern unsigned int nStakeMinAge;
Expand Down
45 changes: 27 additions & 18 deletions src/validation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1641,30 +1641,39 @@ bool ConnectBlock(CBlock& block, CTxDB& txdb, CBlockIndex* pindex, bool fJustChe
mapQueuedChanges[hashTx] = CTxIndex(posThisTx, tx.vout.size());
}

if (IsResearchAgeEnabled(pindex->nHeight)
&& !GridcoinConnectBlock(block, pindex, txdb, stake_value_in, nStakeReward, nFees))
{
return false;
}
// This lock protects the time period between the GridcoinConnectBlock, which also connects validated transaction contracts
// and causes contract handlers to fire, and the committing of the txindex changes to disk below. Any contract handlers that
// generate signals whose downstream handlers make use of transaction data on disk via leveldb (txdb) on another thread need
// to take this lock to ensure that the write to leveldb and the access of the transaction data by the signal handlers is
// appropriately serialized.
LOCK(cs_tx_val_commit_to_disk);

if (IsResearchAgeEnabled(pindex->nHeight)
&& !GridcoinConnectBlock(block, pindex, txdb, stake_value_in, nStakeReward, nFees))
{
return false;
}

pindex->nMoneySupply = ReturnCurrentMoneySupply(pindex) + nValueOut - nValueIn;
pindex->nMoneySupply = ReturnCurrentMoneySupply(pindex) + nValueOut - nValueIn;

if (!txdb.WriteBlockIndex(CDiskBlockIndex(pindex)))
return error("%s: WriteBlockIndex for pindex failed", __func__);
if (!txdb.WriteBlockIndex(CDiskBlockIndex(pindex)))
return error("%s: WriteBlockIndex for pindex failed", __func__);

if (!OutOfSyncByAge())
{
fColdBoot = false;
}
if (!OutOfSyncByAge())
{
fColdBoot = false;
}

if (fJustCheck)
return true;
if (fJustCheck)
return true;

// Write queued txindex changes
for (const auto& [hash, index] : mapQueuedChanges)
{
if (!txdb.UpdateTxIndex(hash, index))
return error("%s: UpdateTxIndex failed", __func__);
// Write queued txindex changes
for (const auto& [hash, index] : mapQueuedChanges)
{
if (!txdb.UpdateTxIndex(hash, index))
return error("%s: UpdateTxIndex failed", __func__);
}
}

// Update block index on disk without changing it in memory.
Expand Down

0 comments on commit 3dd263a

Please sign in to comment.