I want to download blk.dat of a specific block number, How can I do this ?
Note: I'm only interested in specific blocks, I only want to download blocks for example that are numbered between 100 and 200,etc...
I want to download blk.dat of a specific block number, How can I do this ?
Note: I'm only interested in specific blocks, I only want to download blocks for example that are numbered between 100 and 200,etc...
I see two broad solutions:
BlockChain.info allows you to query for blocks at a certain height. Example:
https://blockchain.info/block-height/100?format=json
returns
{ "blocks" : [
{
"hash":"000000007bc154e0fa7ea32218a72fe2c1bb9f86cf8c9ebf9a715ed27fdb229a",
"ver":1,
(snipped)
(Remember to filter out blocks that aren't on the main chain, by looking at the "main_chain" attribute.)
You can take that hash, and look up the block:
https://blockchain.info/rawblock/000000007bc154e0fa7ea32218a72fe2c1bb9f86cf8c9ebf9a715ed27fdb229a
returns
{
"hash":"000000007bc154e0fa7ea32218a72fe2c1bb9f86cf8c9ebf9a715ed27fdb229a",
"ver":1,
"prev_block":"00000000cd9b12643e6854cb25939b39cd7a1ad0af31a9bd8b2efe67854b1995",
"mrkl_root":"2d05f0c9c3e1c226e63b5fac240137687544cf631cd616fd34fd188fc9020866",
"time":1231660825,
(snipped)
(This is in a JSON format custom to BlockChain - I haven't found any API providers that will return the raw block. Kinda annoying.)
This downloads the headers of the Bitcoin blockchain (much smaller than the whole blockchain). Then, it goes through the block headers and downloads the ones you want.
WalletAppKit kit = new WalletAppKit(MainNetParams.get(), new java.io.File("."), "test");
// Download headers
kit.startAndWait();
BlockChain chain = kit.chain();
BlockStore bs = chain.getBlockStore();
Peer peer = kit.peerGroup().getDownloadPeer();
// Get last block
StoredBlock current = bs.getChainHead();
// Loop until you reach the genesis block
while(current.getHeight() > 1) {
// Fully download blocks between 100 and 200
if(100 <= current.getHeight() && current.getHeight() <= 200) {
Block b = peer.getBlock(current.getHeader().getHash()).get();
System.out.println(b);
}
current = current.getPrev(bs);
}
This example uses BitcoinJ, but other SPV Bitcoin libraries can do this too.