As @pieter-wuille already answered the first half of your question:
If gettxout
returns something, the output is unspent. If it returns nothing, the output either never existed or is spent.
For that I'd add you can set the unconfirmed flag true
, so it factors in unconfirmed transactions, too.
To answer the second part of your question, here's a sub-optimal way to find the transaction which spent your txo:
This assumes txindex=1
and prune=0
.
To find out who spent the txout you can figure out the block it was confirmed with: getrawtransaction {txid} 1
. From here on you can iterate through all the blocks to the last arrived one (getblock
) and the mempool (getrawmempool
), while checking every transaction:
foreach(var input in tx.inputs)
{
if(input.prevout.txid == myTxo.txid && input.prevout.index == myTxo.index)
{
// tx spent the txo you were looking for
}
}