How can I get the block number of given transaction?

1

1

For a given transaction, I have its incoming transactions and I am trying to get the number of the block of those transactions. This seems like something that cannot achieve directly.

I am working on python with Json RPC and I want to work directly on the block-chain on my computer and not to use online API such as blockchain.info.

How can I get efficiently the block number of a given transaction?

slouis

Posted 2014-06-05T18:40:54.610

Reputation: 13

In what way specifically do you mean? Like through an API or something. You obviously don't mean manually, right? The block for this transaction is listed at the bottom.

4276 2014-06-06T05:27:25.713

you are right, I should have write that: I am working on python with Json RPC and I want to work directly on the block-chain on my computer and not to use blockchain.info.slouis 2014-06-06T07:06:05.170

Answers

2

According to the Wiki, the gettransaction <txid> call return a JSON object with the following information

"amount" : total amount of the transaction
"confirmations" : number of confirmations of the transaction
"txid" : the transaction ID
"time" : time associated with the transaction[1].
"details" - An array of objects containing:
"account"
"address"
"category"
"amount"
"fee"

Now just use getblockcount and subtract "confirmations" of the JSON object.

Jori

Posted 2014-06-05T18:40:54.610

Reputation: 1 522

Thank you Jori, your approach is probably the best. the problem is that getblockcount is not static (changes over time), hence if I have a bunch of transactions SAVED for more than ~10 min I can't work with your method. But maybe that the best way...Thanks!slouis 2014-06-06T08:10:07.580

@slouis don't forget to vote and select if this answer works for you.4276 2014-06-06T08:24:49.277

@slouis "confirmations" isn't static either, but the difference between it and getblockcount should be. This would work even if you have your transactions laying around for a year.Jori 2014-06-06T09:11:00.880

Not if I am saving the whole transaction. But I got the idea. Thanks!slouis 2014-06-06T10:17:08.813

I'm afraid that I don't understand what you mean by that.Jori 2014-06-06T10:34:07.583

If I have 100000 transactions (hash), and I want to know which block each transaction is in: I will need to call "getblockcount" 100000 times - for every time that I call get("confirmations"). Is there a faster way? Furthermore, I might call getblockcount() - transactionx.get("confirmations") and in between there will be new block and the 2 counters (confirmation and blockcount) won't be correlated!slouis 2014-06-07T10:42:17.687

getblockcount() - transactionx.get("confirmations") will stay the same, because for every extra block there will be an extra confirmation counted. If you need to find the block of so many transactions, I'd recommend scanning the block chain and look for them with getblock or so. Then matching those transactions in a simple database to their corresponding blocks.+Jori 2014-06-07T14:44:12.293