The block peak for a transaction is also recorded in two other puts: the txindex, or within the pockets. Either one of those have purposes that may take a transaction hash and go back details about the transaction, together with the transaction itself, the block hash it’s showed in, and so forth. Alternatively those don’t seem to be at all times to be had as they’re non-compulsory options, nor are they to be had in all portions of the codebase. It depends upon what you might be if truth be told seeking to do.
With out a txindex or a pockets, it is not imaginable to get the block peak as this information isn’t recorded anyplace, and usually does now not want to be.
If you’re within the pockets, i.e. have a CWallet
to be had, then you’ll 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().peak(block_height));
}
The peak will probably be positioned into block_height
.
If the txindex is to be had, it’s good to 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
serve as will look up the transaction within the txindex given via the hash within the first parameter. The block hash will probably be positioned into the second one parameter, and the transaction that used to be discovered into the 3rd parameter.