<p>I think what you want is to use this function:</p>

<p><code>bool CCoinsViewMemPool::GetCoin(const COutPoint &amp;outpoint, Coin &amp;coin) const</code></p>

<p>You can find the definition here: <a href="https://github.com/bitcoin/bitcoin/blob/v0.17.0.1/src/txmempool.cpp#L885" rel="nofollow noreferrer">https://github.com/bitcoin/bitcoin/blob/v0.17.0.1/src/txmempool.cpp#L885</a></p>

<p>It will set the <code>Coin</code> object you pass as an argument. For an example of usage, see <code>bool CheckSequenceLocks(const CTransaction &amp;tx, int flags, LockPoints* lp, bool useExistingLockPoints)</code> in <a href="https://github.com/bitcoin/bitcoin/blob/v0.17.0.1/src/validation.cpp#L364" rel="nofollow noreferrer">https://github.com/bitcoin/bitcoin/blob/v0.17.0.1/src/validation.cpp#L364</a></p>

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

CC BY-SA 4.0
2018-11-12T22:39:57.713Z
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
CC BY-SA 4.0
2018-11-12T22:42:47.290Z
I misunderstood, then.
JBaczuk
CC BY-SA 4.0
2018-11-12T22:54:11.190Z
Actually, OP may be confused and not need the full transaction for his purpose.
Pieter Wuille
CC BY-SA 4.0
2018-11-12T23:20:42.403Z
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
CC BY-SA 4.0
2018-11-12T22:24:33.833Z
2018-11-13T05:38:38.883Z
2018-11-13T05:38:38.883Z
JBaczuk
<p>I'm trying to come up with this function: <code>CTransaction getTxFromPrevout(COutPoint prevo);</code></p>

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

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

<p>If I use a web based explorer I can take the <code>prevo</code> and extract the hash from it, then search the web for the <code>tx</code>... 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.</p>

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

<p><code>CTransaction UTxODatabase::getTxFromPrevout(COutPoint prevo);</code></p>

<p>/*************************************************/</p>

<p><strong>EDIT 1:</strong>
I put this at the very end of the CheckTransaction() function, in <code>main.cpp</code></p>

<p><code>BOOST_FOREACH (const CTxIn&amp; txin, tx.vin){
        std::cout &lt;&lt; "txin.prevPubKey.ToString(): " &lt;&lt; txin.prevPubKey.ToString() &lt;&lt; std::endl;</code></p>

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

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

CC BY-SA 4.0
2018-11-12T18:34:09.390Z
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
CC BY-SA 4.0
2018-11-12T21:52:53.163Z
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
<transactions><bitcoin-core><bitcoind><c++>
find a CTransaction from a prevout in Bitcoin source code
galimba
# find a CTransaction from a prevout in Bitcoin source code

(Asked by: username_0 on 2018-11-12)

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 `prevPubKey`s 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?

**Tags:** transactions, bitcoin-core, bitcoind, c++

> **Comments:**
> **username_1**: 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.  
> **username_0**: 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);`  

---

## 1 answers

### Answer by username_1 on 2018-11-12. Score: 1

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".

> **Comments:**
> **username_2**: OP is asking for the transaction, not just the UTXO. That's just not possible unless you have `txindex=1` and no pruning.  
> **username_1**: I misunderstood, then.  
> **username_2**: Actually, OP may be confused and not need the full transaction for his purpose.  
> **username_0**: 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.  

---

