Bitcoin P2P API getBlocks always returns 500 inv items 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?
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.