<p>It looks like you have your block hashes in the wrong byte order. You have them in the display byte order, but the network actually handles block hashes in the reverse byte order, the zeroes are at the end.</p>

<p>I think the blocks that you are getting in response are going to be the first 500 blocks of the blockchain as well.</p>

<hr>

<p>So what's happening is that a node sees your start hash, does not find it in your blockchain so it starts with the genesis block. It keeps going for each block and finds that it does not find the stop hash. This would be both because the stop hash is in the wrong byte order, and because it is starting from genesis and not your start hash.</p>

<p>So the response you get is the maximum of 500 blocks starting from the genesis block - the most recent common ancestor between your start hash and it's current tip.</p>

<hr>

<blockquote>
  <p>Am I misunderstanding the point of this API ? I was under the impression it returns block details but instead it just returns missing block hashes. How do I then find out what transactions happened in that block?</p>
</blockquote>

<p><code>getblocks</code> gets the block hashes. To get actual block itself, you need to use <code>getdata</code> with the hashes you've received.</p>

CC BY-SA 4.0
2019-12-27T04:10:27.740Z
2019-12-27T05:31:11.377Z
2019-12-27T05:31:11.377Z
Ava Chow
<p>I'm trying to build a mini full node so that I can understand exactly how they operate. I've managed to get a few parts of the P2P api working however I am stuck with basic things like 'getblocks' and 'getheaders'</p>

<p>For example I have the following block hashes I am interested in:</p>

<pre><code>block_hash = "000000000000592589e55cda6e8a093998e8356ea770d4aaeb7c0f5439b147d7"
block_stop = "000000000000017a09017d52db538d7a9ddcc48311866d7e5fdbbbec7d0faad5"

Command: getblocks
Request: 0b110907676574626c6f636b73000000450000002a0af9950100000001000000000000592589e55cda6e8a093998e8356ea770d4aaeb7c0f5439b147d7000000000000017a09017d52db538d7a9ddcc48311866d7e5fdbbbec7d0faad5
</code></pre>

<p>My question is: Why am I getting back 500 inv items when the stop hash is only 1 block away from the block locator ? (Btw the same thing happens to me when I'm using getheaders instead of getblocks)</p>

<p><strong>EDIT</strong></p>

<p>Ok so I have swapped the endian-ness of the data I'm sending. I also had to swap the block locator and stop around in order to get a response (so the stop hash is at a block height lower than the locator) which makes me feel like I'm making more mistakes here. </p>

<p>When doing this I still receive 500 inv on the example above, however if I use these two instead:</p>

<pre><code>block_stop = "0000000000000049a7ce260de7fcf63f34acf3fe1c5b2040ae77b9bdaf1abb38"
block_hash = "000000000000013d8a77f222805b1dca31d0fb48d60b29098c77609de5f93811

ACTUAL MESSAGE: 0b110907676574626c6f636b7300000045000000a83c12f901000000011138f9e59d60778c09290bd648fbd031ca1d5b8022f2778a3d0100000000000038bb1aafbdb977ae40205b1cfef3ac343ff6fce70d26cea74900000000000000
</code></pre>

<p>it correctly returns from the height I require, however it returns all of the blocks until the latest block instead of the one that I'm after. Am I misunderstanding the point of this API ? I was under the impression it returns block details but instead it just returns missing block hashes. How do I then find out what transactions happened in that block?</p>

<bitcoin-core><full-node><p2p>
Bitcoin P2P API getBlocks always returns 500 inv items
Dimitree
# Bitcoin P2P API getBlocks always returns 500 inv items

(Asked by: username_0 on 2019-12-27)

I'm trying to build a mini full node so that I can understand exactly how they operate. I've managed to get a few parts of the P2P api working however I am stuck with basic things like 'getblocks' and 'getheaders'

For example I have the following block hashes I am interested in:

```
block_hash = "000000000000592589e55cda6e8a093998e8356ea770d4aaeb7c0f5439b147d7"
block_stop = "000000000000017a09017d52db538d7a9ddcc48311866d7e5fdbbbec7d0faad5"

Command: getblocks
Request: 0b110907676574626c6f636b73000000450000002a0af9950100000001000000000000592589e55cda6e8a093998e8356ea770d4aaeb7c0f5439b147d7000000000000017a09017d52db538d7a9ddcc48311866d7e5fdbbbec7d0faad5

```

My question is: Why am I getting back 500 inv items when the stop hash is only 1 block away from the block locator ? (Btw the same thing happens to me when I'm using getheaders instead of getblocks)

**EDIT**

Ok so I have swapped the endian-ness of the data I'm sending. I also had to swap the block locator and stop around in order to get a response (so the stop hash is at a block height lower than the locator) which makes me feel like I'm making more mistakes here.

When doing this I still receive 500 inv on the example above, however if I use these two instead:

```
block_stop = "0000000000000049a7ce260de7fcf63f34acf3fe1c5b2040ae77b9bdaf1abb38"
block_hash = "000000000000013d8a77f222805b1dca31d0fb48d60b29098c77609de5f93811

ACTUAL MESSAGE: 0b110907676574626c6f636b7300000045000000a83c12f901000000011138f9e59d60778c09290bd648fbd031ca1d5b8022f2778a3d0100000000000038bb1aafbdb977ae40205b1cfef3ac343ff6fce70d26cea74900000000000000

```

it correctly returns from the height I require, however it returns all of the blocks until the latest block instead of the one that I'm after. Am I misunderstanding the point of this API ? I was under the impression it returns block details but instead it just returns missing block hashes. How do I then find out what transactions happened in that block?

**Tags:** bitcoin-core, full-node, p2p


---

## 1 answers

### Answer by username_1 on 2019-12-27. Score: 1

It looks like you have your block hashes in the wrong byte order. You have them in the display byte order, but the network actually handles block hashes in the reverse byte order, the zeroes are at the end.

I think the blocks that you are getting in response are going to be the first 500 blocks of the blockchain as well.

---

So what's happening is that a node sees your start hash, does not find it in your blockchain so it starts with the genesis block. It keeps going for each block and finds that it does not find the stop hash. This would be both because the stop hash is in the wrong byte order, and because it is starting from genesis and not your start hash.

So the response you get is the maximum of 500 blocks starting from the genesis block - the most recent common ancestor between your start hash and it's current tip.

---

> Am I misunderstanding the point of this API ? I was under the impression it returns block details but instead it just returns missing block hashes. How do I then find out what transactions happened in that block?

`getblocks` gets the block hashes. To get actual block itself, you need to use `getdata` with the hashes you've received.


---

