The block top for a transaction could also be recorded in two completely different locations: the txindex, or within the pockets. Each of those have features that may take a transaction hash and return details about the transaction, together with the transaction itself, the block hash it’s confirmed in, and many others. Nonetheless these should not at all times out there as they’re optionally available options, nor are they out there in all components of the codebase. It is dependent upon what you’re truly making an attempt to do.
And not using a txindex or a pockets, it isn’t attainable to get the block top as this information is just not recorded anyplace, and customarily doesn’t must be.
If you’re within the pockets, i.e. have a CWallet
out there, then you are able to do one thing like:
// Assuming pockets is a CWallet& that already exists
// Assuming tx is a CTransactionRef that already exists
const CWalletTx* wtx = pockets.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().top(block_height));
}
The peak shall be positioned into block_height
.
If the txindex is on the market, you might do one thing 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
perform will lookup the transaction within the txindex given by the hash within the first parameter. The block hash shall be positioned into the second parameter, and the transaction that was discovered into the third parameter.