I'm working with the Bitcoin core code. I have a CTransactionRef object and I'm trying to determine what the block height of the block containing the transaction is. What would be the most efficient way to determine the height given the starting point of a CTransactionRef?
1 Answer
The block height for a transaction may be recorded in two different places: the txindex, or in the wallet. Both of these have functions that can take a transaction hash and return information about the transaction, including the transaction itself, the block hash it is confirmed in, etc. However these are not always available as they are optional features, nor are they available in all parts of the codebase. It depends on what you are actually trying to do.
Without a txindex or a wallet, it's not possible to get the block height as this data is not recorded anywhere, and generally does not need to be.
If you are in the wallet, i.e. have a CWallet
available, then you can do something like:
// Assuming wallet is a CWallet& that already exists
// Assuming tx is a CTransactionRef that already exists
const CWalletTx* wtx = wallet.GetWalletTx(tx->GetHash());
if (wtx && (auto conf = wtx->state<TxStateConfirmed>())) {
uint256 block_hash = conf->confirmed_block_hash;
int block_height;
chain().findBlock(block_hash, FoundBlock().height(block_height));
}
The height will be placed into block_height
.
If the txindex is available, you could do something like:
// Assuming tx is a CTranasctionRef that already exists
// Assuming chainman is a CChainMan that already exists
CTransactionRef dummy;
uint256 block_hash;
g_txindex->FindTx(tx->GetHash(), block_hash, dummy);
int block_height = chainman.m_blockman.LookupBlockIndex(block_hash)->nHeight;
The FindTx
function will lookup the transaction in the txindex given by the hash in the first parameter. The block hash will be placed into the second parameter, and the transaction that was found into the third parameter.
-
Thanks, I think I may be able to get a reference to
g_txindex
. How would I determine the block height int once I haveblock_hash
? Is there an index I can use to get that value as well? Commented Oct 6, 2023 at 23:42 -
Oops, forgot to include that in my answer. Updated it with how you can get the height from the hash.– Ava Chow ♦Commented Oct 6, 2023 at 23:50
-
Excellent, the last piece I need is any tips on how to get a reference to a CChainMan object? Commented Oct 7, 2023 at 2:26
-
It depends on where in the code you are. You could grep around wherever you are to see if there's anything nearby that uses a chainman.– Ava Chow ♦Commented Oct 7, 2023 at 2:38
-
I'm in validation.cpp and I have a reference to a CTransaction, CChainMan and CBlockMan. Any suggestions on the best way to get to the block height from that context given the references I currently have? I don't see an easy way to get to
g_txindex
from this file or CWalletTx. I was looking at how rawtransaction.cpp gets the block hash and it uses node::GetTransaction, would you recommend that given this context? Thanks! Commented Oct 13, 2023 at 21:06