6

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...

  • Your question title and question text seem to be asking two different questions. Are you interested in getting a raw block by index, or a group of blocks in blkindex format, or something else? – Nick ODell Jan 7 '15 at 2:43
  • @NickODell I want group of blocks, but this group of blocks should contain blocks that I specify, for example between 120000 and 130000, – ka lin Jan 7 '15 at 11:13
  • Maybe if you read this mastering bitcoin - chapter 7 you will get your answaer. It's not much to read :) – Xawery Wiśniowiecki Jan 7 '15 at 12:37
  • Block are unnumbered, they are more like linked list. – Xawery Wiśniowiecki Jan 7 '15 at 12:38
  • @XaweryWiśniowiecki I know that I can get block information using the block hash in bitcoin client, I can use getblock "hash", but this function require the blocks to be already in the hard drive, so without downloading the whole blockchain, how can I download only specific blocks ? – ka lin Jan 7 '15 at 12:48
3

I see two broad solutions:

Use an API that allows you to get blocks by block height

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.)

Write a client that gets the blocks from the network

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.

(Thanks to Diego Basch for the code.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.