How to get block headers from block hash using P2P API?

0

I have been experimenting with getheaders and one thing I can't seem to figure out is how to get a header for a specific hash, because getheaders doesn't provide this feature.

According to the documentation (and from my own experiments), it looks like the start position provided to the API is NOT included in the header results.

Return a headers packet containing the headers of blocks starting right after the last known hash in the block locator object, up to hash_stop or 2000 blocks

I get that this is for the purpose of synchronizing, so the semantic is "I know about this block hash, so send me all the rest I haven't seen yet, which doesn't include this hash".

But with this there's no way to figure out a block header from a block hash, I always need the previous block's hash to get the current block header. Is there no P2P API (or any trick) that lets me take a block hash and get block headers back?

Vlad

Posted 2019-06-01T14:08:02.413

Reputation: 195

Answers

0

The block header is part of the block, so you can request the entire thing and just consider the first 80 bytes. Software generally shouldn’t need to do this though.

Anonymous

Posted 2019-06-01T14:08:02.413

Reputation: 10 054

getblocks also does the same thing, it returns the blocks AFTER the block hash I provide. This seems to be the common theme in all these APIs. All I'm trying to do is: given a block hash, get anything about the corresponding block. So far I couldn't find a wayVlad 2019-06-01T16:15:33.783

There’s a command that takes a hash and returns the whole block including the header.Anonymous 2019-06-01T16:16:20.513

Could you link me to this command? Would appreciate it. BTW I'm talking about the P2P API (https://en.bitcoin.it/wiki/Protocol_documentation), not JSON-RPC by the way. I'm asking after trying to find such method for a day.

Vlad 2019-06-01T16:42:09.607

Use getdata: https://bitcoin.org/en/developer-reference#getdata

Andrew Chow 2019-06-02T01:40:00.883