Title: how to get block height for given day Post by: RoxxR on February 18, 2023, 12:40:21 PM I'm trying to programmatically get the block height at certain given dates - for instance, first day of each month.
I don't have access to a full node. Is there a block explorer or other platform with an API endpoint (or just a small dump of such data somewhere) that could be helpful? Thanks! Title: Re: how to get block height for given day Post by: jackg on February 18, 2023, 12:46:31 PM Can you do by just subtracting 144 from the block height per day you're looking?
If not, I'd still recommend doing that and then looking up the api for a block explorer (like blockchain.com) and trying to programmatically go back (or forward) from there based on how many blocks would have come since. I remember blockchain.com's api indexing blocks based on height and hash (not sure how others do it but it's probably an easy enough place to start and get a json of data, and then checking the time the block was received or generated). Title: Re: how to get block height for given day Post by: paid2 on February 18, 2023, 12:58:00 PM The easiest way could be to take the list of the blocks mined by a big mining pool.
For example, you can be 100% sure that Antpool does mine at least some blocks per day, so you will have an Block Height associated to a date. Example : list of blocks mined by Antpool (https://mempool.space/fr/mining/pool/antpool) Title: Re: how to get block height for given day Post by: o_e_l_e_o on February 18, 2023, 01:31:11 PM You can find this data very quickly using Blockchair. For example, here is a list of all the blocks mined yesterday: https://blockchair.com/bitcoin/blocks?s=id%28desc%29&q=time%282023-02-17%29. They have an API you can use, but there is a cost associated with its use.
They do also have free data dumps of this information which you can download from here: https://gz.blockchair.com/bitcoin/blocks/ Title: Re: how to get block height for given day Post by: pooya87 on February 19, 2023, 03:23:22 AM Here is another fun way of doing it which is a lot faster and cheaper to do:
If you run a SPV client like Electrum on your computer you already have all the block headers saved up in a ~60 MB file called "blockchain_headers" which is a stream of bytes. All you have to do is to programatically read this file to get the raw bytes which should be a multiple of 80. Each 80 byte is a block header of a block in chronological order (so you have the block height this way too). Then start from the beginning (byte 0) and extract the time from each 80 byte chunk (ie. one header) knowing that 4th item in it is the timestamp. Code: version[4] + prev_hash[32] + merkle_root[32] + time[4] + target[4] + nonce[4] Here is a pseudocode Code: stream = File.Read("blockchain_headers") Keep in mind that timestamps are in UTC not your local time. Title: Re: how to get block height for given day Post by: RoxxR on February 19, 2023, 06:01:21 AM Here is another fun way of doing it which is a lot faster and cheaper to do: If you run a SPV client like Electrum on your computer you already have all the block headers saved up in a ~60 MB file called "blockchain_headers" which is a stream of bytes. All you have to do is to programatically read this file to get the raw bytes which should be a multiple of 80. Each 80 byte is a block header of a block in chronological order (so you have the block height this way too). Then start from the beginning (byte 0) and extract the time from each 80 byte chunk (ie. one header) knowing that 4th item in it is the timestamp. Code: version[4] + prev_hash[32] + merkle_root[32] + time[4] + target[4] + nonce[4] Here is a pseudocode Code: stream = File.Read("blockchain_headers") Keep in mind that timestamps are in UTC not your local time. Thanks, this does sound like a fun and effective way. I'll give it a shot! Title: Re: how to get block height for given day Post by: RoxxR on February 19, 2023, 06:01:48 AM Thanks everyone for the various approaches!
Title: Re: how to get block height for given day Post by: ABCbits on February 19, 2023, 11:58:49 AM Can you do by just subtracting 144 from the block height per day you're looking? This is wrong approach. Current all-time average block time is somewhere about 9.55 minutes. If not, I'd still recommend doing that and then looking up the api for a block explorer (like blockchain.com) and trying to programmatically go back (or forward) from there based on how many blocks would have come since. I remember blockchain.com's api indexing blocks based on height and hash (not sure how others do it but it's probably an easy enough place to start and get a json of data, and then checking the time the block was received or generated). Unfortunately blockchain.com API doesn't have filter to choose block based on date. Here is another fun way of doing it which is a lot faster and cheaper to do: If you run a SPV client like Electrum on your computer you already have all the block headers saved up in a ~60 MB file called "blockchain_headers" which is a stream of bytes. All you have to do is to programatically read this file to get the raw bytes which should be a multiple of 80. Each 80 byte is a block header of a block in chronological order (so you have the block height this way too). Then start from the beginning (byte 0) and extract the time from each 80 byte chunk (ie. one header) knowing that 4th item in it is the timestamp. Code: version[4] + prev_hash[32] + merkle_root[32] + time[4] + target[4] + nonce[4] Here is a pseudocode Code: stream = File.Read("blockchain_headers") Keep in mind that timestamps are in UTC not your local time. Do you mind checking beginning of file blockchain_headers with hex editor/viewer? I tried on my device, but mostly it only contain zeroes. I suspect Electrum only request needed block header rather than all block header. https://i.ibb.co/d22yy2Z/a.png https://i.ibb.co/wzmqNVT/b.png Title: Re: how to get block height for given day Post by: LoyceMobile on February 19, 2023, 12:14:26 PM Thanks everyone for the various approaches! Let me add my topic on block data: https://bitcointalk.org/index.php?topic=5246271.0In short, you're looking for https://loyce.club/blockdata/time.txt Title: Re: how to get block height for given day Post by: pooya87 on February 19, 2023, 01:14:11 PM Do you mind checking beginning of file blockchain_headers with hex editor/viewer? I tried on my device, but mostly it only contain zeroes. I suspect Electrum only request needed block header rather than all block header. Yes, although I simply read the file as a byte stream not with hex editor, the file starts with the genesis block's header ie. 0x01 followed by 35 zeros (previous hash) then 0x3b (merkle root) and so on.I've had my Electrum for a very long time though. The file was created in 2019! Maybe they changed some stuff like after the introduction of checkpoint header. Edit: Yeap. They've changed things. Downloaded Electrum 4.3.4 on Ubuntu and let it sync, even deleted the blockchain_headers file and let it be downloaded again. In both cases the file contains all zeros up until block height 747936. Which is a waste of space if you ask me!!! This also means what I explained above won't work using newer version of Electrum. Title: Re: how to get block height for given day Post by: ABCbits on February 19, 2023, 01:57:43 PM --snip-- Edit: Yeap. They've changed things. Downloaded Electrum 4.3.4 on Ubuntu and let it sync, even deleted the blockchain_headers file and let it be downloaded again. In both cases the file contains all zeros up until block height 747936. Which is a waste of space if you ask me!!! This also means what I explained above won't work using newer version of Electrum. That's what i suspect. I just found out Electrum protocol also have API call which can ask single block header[1] and block headers starting from specific height[2]. But those API call i mentioned could be used as alternative to obtain block headers. [1] https://electrumx-spesmilo.readthedocs.io/en/latest/protocol-methods.html#blockchain-block-header (https://electrumx-spesmilo.readthedocs.io/en/latest/protocol-methods.html#blockchain-block-header) [2] https://electrumx-spesmilo.readthedocs.io/en/latest/protocol-methods.html#blockchain-block-headers (https://electrumx-spesmilo.readthedocs.io/en/latest/protocol-methods.html#blockchain-block-headers) |