Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

consensus: Fix 20230904 testnet forking issue #2703

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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