I'm trying to come up with this function: CTransaction getTxFromPrevout(COutPoint prevo);

The idea being... when a new transaction is about to be validated, it will go through all of it's tx.vin[i].prevout and check for them funds (utxo).

So I want to find and identify the CTransaction tx such that tx.vout (of type vector<CTxOut> ) has any i element such that it's CScript is a normal payment (tx.vout[i].scriptPubKey.IsNormalPayment() == true) and the tx.vout[i].scriptPubKey has within it a match for the prevo

If I use a web based explorer I can take the prevo and extract the hash from it, then search the web for the tx... but that doesn't help me, since I want to be able to use this within the code itself, adding this new function. I tried following the code on an explorer and it uses the rpc api, and it was not easy to follow.

What I'm thinking is that there should be a utxo database structure or class that will make this easier to crawl through. If I suppose there is such a class named UTxODatabase - that I know there isn't, but just imagine there is - then the function I need to code would look like this:

CTransaction UTxODatabase::getTxFromPrevout(COutPoint prevo);

/*************************************************/

EDIT 1: I put this at the very end of the CheckTransaction() function, in main.cpp

BOOST_FOREACH (const CTxIn& txin, tx.vin){ std::cout << "txin.prevPubKey.ToString(): " << txin.prevPubKey.ToString() << std::endl;

Then I've ran the wallet and the output shown for each txin was just: txin.prevPubKey.ToString:, without any prevPubKey data. I am really confused about this.

My thinking was that if I'm able to get those prevPubKeys from the CTxIn, then I'll be able to track the utxo. But why would there be no information on that member of the txin at that stage?

  • There is a utxo database See CCoinsView class in coins.h, the function virtual bool GetCoin(const COutPoint &outpoint, Coin &coin) const; (or one of its overloads) might do what you need. – JBaczuk Nov 12 at 18:34
  • I'm confused about that virtual qualifier... the main function you referenced me to was: bool CCoinsView::GetCoins(const uint256& txid, CCoins& coins) const { return false; } ... it just returns false. This seems to be on the right track, tho... and probably one of the other implementations should work... like this one: bool CCoinsViewBacked::GetCoins(const uint256& txid, CCoins& coins) const { return base->GetCoins(txid, coins); } But the truth is I'm not sure on how to use it Could be something like this? CTransaction CCoinsViewBacked::getTxFromPrevout(COutPoint prevo); – galimba Nov 12 at 21:52

I think what you want is to use this function:

bool CCoinsViewMemPool::GetCoin(const COutPoint &outpoint, Coin &coin) const

You can find the definition here: https://github.com/bitcoin/bitcoin/blob/v0.17.0.1/src/txmempool.cpp#L885

It will set the Coin object you pass as an argument. For an example of usage, see bool CheckSequenceLocks(const CTransaction &tx, int flags, LockPoints* lp, bool useExistingLockPoints) in https://github.com/bitcoin/bitcoin/blob/v0.17.0.1/src/validation.cpp#L364

This is supposed to "provide access to all Coins which are either unspent in the * base CCoinsView, or are outputs from any mempool transaction".

  • OP is asking for the transaction, not just the UTXO. That's just not possible unless you have txindex=1 and no pruning. – Pieter Wuille Nov 12 at 22:39
  • I misunderstood, then. – JBaczuk Nov 12 at 22:42
  • Actually, OP may be confused and not need the full transaction for his purpose. – Pieter Wuille Nov 12 at 22:54
  • Agreed! I might not need the whole transaction... but at least I want to be able to validate/compare/check the address to where each of the utxo is pointing at. – galimba Nov 12 at 23:20

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.