Skip to content

Commit

Permalink
Merge pull request #2703 from jamescowens/fix_20230904_testnet_forkin…
Browse files Browse the repository at this point in the history
…g_issue

consensus: Fix 20230904 testnet forking issue
  • Loading branch information
jamescowens authored Sep 18, 2023
2 parents 21414dd + 9c10ede commit 3f1c5ef
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 4 deletions.
24 changes: 21 additions & 3 deletions src/gridcoin/staking/kernel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
#include "streams.h"
#include "util.h"

#include <node/blockstorage.h>

using namespace std;
using namespace GRC;

Expand Down Expand Up @@ -385,12 +387,28 @@ bool GRC::ReadStakedInput(
CTxDB& txdb,
const uint256 prevout_hash,
CBlockHeader& out_header,
CTransaction& out_txprev)
CTransaction& out_txprev,
CBlockIndex* pindexPrev)
{
CTxIndex tx_index;

// Get transaction index for the previous transaction
if (!txdb.ReadTxIndex(prevout_hash, tx_index)) {
if (pindexPrev != nullptr) {
for (CBlockIndex* pindex = pindexPrev; pindex->pprev != nullptr; pindex = pindex->pprev) {
CBlock block;
ReadBlockFromDisk(block, pindex, Params().GetConsensus());

for (const auto& tx : block.vtx) {
if (tx.GetHash() == prevout_hash) {
error("Found tx %s in block %s", tx.GetHash().ToString(), pindex->GetBlockHash().ToString());
out_txprev = tx;
out_header = pindex->GetBlockHeader();
return true;
}
}
}
}
// Previous transaction not in main chain, may occur during initial download
return error("%s: tx index not found for input tx %s", __func__, prevout_hash.GetHex());
}
Expand Down Expand Up @@ -429,7 +447,7 @@ bool GRC::CalculateLegacyV3HashProof(
CTransaction input_tx;
CBlockHeader input_block;

if (!ReadStakedInput(txdb, prevout.hash, input_block, input_tx)) {
if (!ReadStakedInput(txdb, prevout.hash, input_block, input_tx, nullptr)) {
return coinstake.DoS(1, error("Read staked input failed."));
}

Expand Down Expand Up @@ -584,7 +602,7 @@ bool GRC::CheckProofOfStakeV8(
CBlockHeader header;
CTransaction txPrev;

if (!ReadStakedInput(txdb, prevout.hash, header, txPrev))
if (!ReadStakedInput(txdb, prevout.hash, header, txPrev, pindexPrev))
return tx.DoS(1, error("%s: read staked input failed", __func__));

if (!VerifySignature(txPrev, tx, 0, 0))
Expand Down
3 changes: 2 additions & 1 deletion src/gridcoin/staking/kernel.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ bool ReadStakedInput(
CTxDB& txdb,
const uint256 prevout_hash,
CBlockHeader& out_header,
CTransaction& out_txprev);
CTransaction& out_txprev,
CBlockIndex* pindexPrev = nullptr);

//!
//! \brief Calculate the provided block's proof hash with the version 3 staking
Expand Down
3 changes: 3 additions & 0 deletions src/node/blockstorage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@

bool WriteBlockToDisk(const CBlock& block, unsigned int& nFileRet, unsigned int& nBlockPosRet,
const CMessageHeader::MessageStartChars& messageStart)
EXCLUSIVE_LOCKS_REQUIRED(cs_main)
{
AssertLockHeld(cs_main);

// Open history file to append
CAutoFile fileout(AppendBlockFile(nFileRet), SER_DISK, CLIENT_VERSION);
if (fileout.IsNull())
Expand Down
3 changes: 3 additions & 0 deletions src/validation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1752,7 +1752,10 @@ bool AddToBlockIndex(CBlock& block, unsigned int nFile, unsigned int nBlockPos,
}

bool CheckBlock(const CBlock& block, int height1, bool fCheckPOW, bool fCheckMerkleRoot, bool fCheckSig, bool fLoadingIndex)
EXCLUSIVE_LOCKS_REQUIRED(cs_main)
{
AssertLockHeld(cs_main);

// Allow the genesis block to pass.
if(block.hashPrevBlock.IsNull() &&
block.GetHash(true) == (fTestNet ? hashGenesisBlockTestNet : hashGenesisBlock))
Expand Down

0 comments on commit 3f1c5ef

Please sign in to comment.