Skip to content

Commit

Permalink
Fix ReadStakedInput()
Browse files Browse the repository at this point in the history
  • Loading branch information
jamescowens committed Sep 17, 2023
1 parent 21414dd commit 15df6c9
Show file tree
Hide file tree
Showing 2 changed files with 23 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

0 comments on commit 15df6c9

Please sign in to comment.