Skip to content

Commit

Permalink
Update GetTransaction - readd option to use view for locating tx
Browse files Browse the repository at this point in the history
  • Loading branch information
watto-engineer committed Jan 11, 2023
1 parent e35736c commit 32ccff6
Show file tree
Hide file tree
Showing 13 changed files with 26 additions and 26 deletions.
6 changes: 3 additions & 3 deletions src/betting/bet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ bool ExtractPayouts(const CBlock& block, const int& nBlockHeight, std::vector<CT
nWinnerPayments = 0;

uint256 hashBlock;
CTransactionRef txPrev = GetTransaction(nullptr, nullptr, prevout.hash, Params().GetConsensus(), hashBlock);
CTransactionRef txPrev = GetTransaction(nullptr, nullptr, prevout.hash, Params().GetConsensus(), hashBlock, true);

if (txPrev) {
stakeAmount = txPrev->vout[prevout.n].nValue + nExpectedMint;
Expand Down Expand Up @@ -152,7 +152,7 @@ bool CheckBettingTx(CBettingsView& bettingsViewCache, const CTransaction& tx, co
CTxDestination address;
CTxDestination prevAddr;
// if we cant extract playerAddress - skip tx
CTransactionRef txPrev = GetTransaction(nullptr, nullptr, txin.prevout.hash, Params().GetConsensus(), hashBlock);
CTransactionRef txPrev = GetTransaction(nullptr, nullptr, txin.prevout.hash, Params().GetConsensus(), hashBlock, true);

if (!txPrev ||
!ExtractDestination(txPrev->vout[txin.prevout.n].scriptPubKey, prevAddr)) {
Expand Down Expand Up @@ -707,7 +707,7 @@ void ProcessBettingTx(CBettingsView& bettingsViewCache, const CTransactionRef& t
uint256 hashBlock;
CTxDestination address;
CTxDestination prevAddr;
CTransactionRef txPrev = GetTransaction(nullptr, nullptr, txin.prevout.hash, Params().GetConsensus(), hashBlock);
CTransactionRef txPrev = GetTransaction(nullptr, nullptr, txin.prevout.hash, Params().GetConsensus(), hashBlock, true);
// if we cant extract playerAddress - skip vout
if (!txPrev ||
!ExtractDestination(txPrev->vout[txin.prevout.n].scriptPubKey, prevAddr)) {
Expand Down
2 changes: 1 addition & 1 deletion src/betting/bet_common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ bool IsValidOracleTx(const CTxIn &txin, int nHeight)
std::vector<COracle> oracles = Params().Oracles();

uint256 hashBlock;
CTransactionRef txPrev = GetTransaction(nullptr, nullptr, prevout.hash, Params().GetConsensus(), hashBlock);
CTransactionRef txPrev = GetTransaction(nullptr, nullptr, prevout.hash, Params().GetConsensus(), hashBlock, true);
if (txPrev) {
const CTxOut &prevTxOut = txPrev->vout[prevout.n];

Expand Down
4 changes: 2 additions & 2 deletions src/betting/bet_v2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ void GetBetPayoutsV2(const int nNewBlockHeight, std::vector<CBetOut>& vExpectedP
COutPoint prevout = txin.prevout;

uint256 hashBlock;
CTransactionRef txPrev = GetTransaction(nullptr, nullptr, prevout.hash, Params().GetConsensus(), hashBlock);
CTransactionRef txPrev = GetTransaction(nullptr, nullptr, prevout.hash, Params().GetConsensus(), hashBlock, true);
if (txPrev) {
ExtractDestination( txPrev->vout[prevout.n].scriptPubKey, payoutAddress );
}
Expand Down Expand Up @@ -593,7 +593,7 @@ void GetCGLottoBetPayoutsV2(const int nNewBlockHeight, std::vector<CBetOut>& vEx
totalBetAmount = totalBetAmount + betAmount;
CTxDestination payoutAddress;
uint256 hashBlock;
CTransactionRef txPrev = GetTransaction(nullptr, nullptr, prevout.hash, Params().GetConsensus(), hashBlock);
CTransactionRef txPrev = GetTransaction(nullptr, nullptr, prevout.hash, Params().GetConsensus(), hashBlock, true);

if (txPrev) {
ExtractDestination( txPrev->vout[prevout.n].scriptPubKey, payoutAddress );
Expand Down
2 changes: 1 addition & 1 deletion src/pos/checks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
bool IsTransactionInChain(const uint256& txId, int& nHeightTx, CTransactionRef& tx)
{
uint256 hashBlock;
tx = GetTransaction(::ChainActive().Tip(), nullptr, txId, Params().GetConsensus(), hashBlock);
tx = GetTransaction(nullptr, nullptr, txId, Params().GetConsensus(), hashBlock, true);
if (!tx)
return false;

Expand Down
2 changes: 1 addition & 1 deletion src/pos/kernel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ bool initStakeInput(const CBlock& block, std::unique_ptr<CStake>& ionStake, std:
} else {
// First try finding the previous transaction in database
uint256 hashBlock;
CTransactionRef txPrev = GetTransaction(nullptr, nullptr, txin.prevout.hash, Params().GetConsensus(), hashBlock);
CTransactionRef txPrev = GetTransaction(nullptr, nullptr, txin.prevout.hash, Params().GetConsensus(), hashBlock, true);
if (!txPrev)
return error("%s : INFO: read txPrev failed, tx id prev: %s, block id %s",
__func__, txin.prevout.hash.GetHex(), block.GetHash().GetHex());
Expand Down
2 changes: 1 addition & 1 deletion src/pos/stakeinput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ CBlockIndex* CStake::GetIndexFrom()
if (pindexFrom)
return pindexFrom;
uint256 hashBlock;
CTransactionRef tx = GetTransaction(nullptr, nullptr, txFrom->GetHash(), Params().GetConsensus(), hashBlock);
CTransactionRef tx = GetTransaction(nullptr, nullptr, txFrom->GetHash(), Params().GetConsensus(), hashBlock, true);
if (!tx) {
LogPrintf("%s : failed to find tx %s\n", __func__, txFrom->GetHash().GetHex());
return nullptr;
Expand Down
2 changes: 1 addition & 1 deletion src/rpc/bet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ extern UniValue getpayoutinfo(const JSONRPCRequest& request)
uint256 txHash = uint256S(find_value(obj, "txHash").get_str());
uint32_t nOut = find_value(obj, "nOut").get_int();
uint256 hashBlock;
CTransactionRef tx = GetTransaction(nullptr, nullptr, txHash, Params().GetConsensus(), hashBlock);
CTransactionRef tx = GetTransaction(nullptr, nullptr, txHash, Params().GetConsensus(), hashBlock, true);
if (!tx) {
vPayoutsInfo.emplace_back(std::pair<bool, CPayoutInfoDB>{false, CPayoutInfoDB{}});
continue;
Expand Down
11 changes: 10 additions & 1 deletion src/validation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -970,10 +970,19 @@ bool GetAddressUnspent(uint160 addressHash, int type,
return true;
}

CTransactionRef GetTransaction(const CBlockIndex* const block_index, const CTxMemPool* const mempool, const uint256& hash, const Consensus::Params& consensusParams, uint256& hashBlock)
CTransactionRef GetTransaction(const CBlockIndex* const block_index_in, const CTxMemPool* const mempool, const uint256& hash, const Consensus::Params& consensusParams, uint256& hashBlock, bool fUseView)
{
LOCK(cs_main);

const CBlockIndex* block_index = block_index_in;
if (fUseView) { // use coin database to locate block that contains transaction, and scan it
const Coin& coin = AccessByTxid(::ChainstateActive().CoinsTip(), hash);
if (coin.IsSpent()) {
return nullptr;
}
block_index = ::ChainActive()[coin.nHeight];
}

if (block_index) {
CBlock block;
if (ReadBlockFromDisk(block, block_index, consensusParams)) {
Expand Down
2 changes: 1 addition & 1 deletion src/validation.h
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ void StopScriptCheckWorkerThreads();
* @param[out] hashBlock The hash of block_index, if the tx was found via block_index
* @returns The tx if found, otherwise nullptr
*/
CTransactionRef GetTransaction(const CBlockIndex* const block_index, const CTxMemPool* const mempool, const uint256& hash, const Consensus::Params& consensusParams, uint256& hashBlock);
CTransactionRef GetTransaction(const CBlockIndex* const block_index, const CTxMemPool* const mempool, const uint256& hash, const Consensus::Params& consensusParams, uint256& hashBlock, bool fUseView = false);
/**
* Find the best known block, and make it the tip of the block chain
*
Expand Down
13 changes: 2 additions & 11 deletions src/wallet/rpcwallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4957,20 +4957,11 @@ UniValue getbet(const JSONRPCRequest& request)

uint256 txHash;
txHash.SetHex(request.params[0].get_str());
CBlockIndex* blockindex = ::ChainActive().Tip();

uint256 hash_block;
CTransactionRef tx = GetTransaction(blockindex, node.mempool, txHash, Params().GetConsensus(), hash_block);
CTransactionRef tx = GetTransaction(nullptr, node.mempool, txHash, Params().GetConsensus(), hash_block);
if (!tx) {
std::string errmsg;
if (blockindex) {
if (!(blockindex->nStatus & BLOCK_HAVE_DATA)) {
throw JSONRPCError(RPC_MISC_ERROR, "Block not available");
}
errmsg = "No such transaction found in the provided block";
} else {
errmsg = "No such mempool or blockchain transaction";
}
std::string errmsg = "No such mempool or blockchain transaction";
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, errmsg + ". Use gettransaction for wallet transactions.");
}

Expand Down
2 changes: 1 addition & 1 deletion src/zwgr/accumulators.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,7 @@ int SearchMintHeightOf(CBigNum value){
throw searchMintHeightException("searchForMintHeightOf:: failed to read mint from db");

uint256 hashBlock;
CTransactionRef txMinted = GetTransaction(nullptr, nullptr, txid, Params().GetConsensus(), hashBlock);
CTransactionRef txMinted = GetTransaction(nullptr, nullptr, txid, Params().GetConsensus(), hashBlock, true);
if (!txMinted)
throw searchMintHeightException("searchForMintHeightOf:: failed to read tx");

Expand Down
2 changes: 1 addition & 1 deletion src/zwgr/zwgrchain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,7 @@ bool RecalculateWGRSupply(int nHeightStart)

COutPoint prevout = tx->vin[i].prevout;
uint256 hashBlock;
CTransactionRef txPrev = GetTransaction(nullptr, nullptr, prevout.hash, Params().GetConsensus(), hashBlock);
CTransactionRef txPrev = GetTransaction(nullptr, nullptr, prevout.hash, Params().GetConsensus(), hashBlock, true);
if (!txPrev)
assert(!"Recalculating zerocoins upply failed: cannot get transaction");
nValueIn += txPrev->vout[prevout.n].nValue;
Expand Down
2 changes: 1 addition & 1 deletion src/zwgr/zwgrmodule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ const uint256 PublicCoinSpend::signatureHash() const
bool GetOutput(const uint256& hash, unsigned int index, CValidationState& state, CTxOut& out)
{
uint256 hashBlock;
CTransactionRef txPrev = GetTransaction(nullptr, nullptr, hash, Params().GetConsensus(), hashBlock);
CTransactionRef txPrev = GetTransaction(nullptr, nullptr, hash, Params().GetConsensus(), hashBlock, true);
if (!txPrev) {
return state.Invalid(ValidationInvalidReason::CONSENSUS, error("Output not found"), REJECT_INVALID, "zwgr-bad-out");
}
Expand Down

0 comments on commit 32ccff6

Please sign in to comment.