Could not find method get_block_range

in #hive-dev3 years ago

Could not find method get_block_range

image.png

I am trying to build a tool for Hive, and as always, I run into problems.
Developing anything for Hive is a complete pain.
Here's a description of today's problem:

block_api.get_block

Trying to isolate the problem, I come up with this:

<!DOCTYPE html>
<html>
<script>

var block_num = 50000000

fetch('https://anyx.io', {method: "POST", body: JSON.stringify({
    "id":1,
    "jsonrpc":"2.0",
    "method":"call",
    "params":[
        "block_api",
        "get_block",
        [block_num]
    ]
})})
.then(response => response.json())
.then(response =>{
    console.log(response)
});
    
</script>
</html>

...works fine:

image.png

...but I want more than one block.

block_api.get_block_range

Same code, different method:

<!DOCTYPE html>
<html>
<script>

var block_num = 50000000

fetch('https://anyx.io', {method: "POST", body: JSON.stringify({
    "id":1,
    "jsonrpc":"2.0",
    "method":"call",
    "params":[
        "block_api",
        "get_block_range",
        [block_num, 10]
    ]
})})
.then(response => response.json())
.then(response =>{
    console.log(response)
});
    
</script>
</html>

Result:

image.png

Could not find method get_block_range

Could not find method get_block_range

The journey begins.

I google get_block_range hive and am surprised to find good results, despite Hive having the worst possible name for any project on the internet.

Hive Developer Portal

get_block: https://developers.hive.io/apidefinitions/#block_api.get_block
get_block_range: https://developers.hive.io/apidefinitions/#block_api.get_block_range

It uses different parameters, but the rest of the usage is identical.
According to that, I have done everything correctly. It just does not work.
So much for the Developer Portal.

...Further down in my google results I find a post by blocktrades, where he mentions get_block_range working on his nodes, 8 months ago.

https://hive.blog/hive-139531/@blocktrades/hive-api-node-performance-measurement

I try different nodes:

Could not find method get_block_range

I do more research and check through the hive-js code:

    {
      "api": "condenser_api",
      "method": "get_block_header",
      "params": ["block_num"]
    },
    {
      "api": "block_api",
      "method": "get_block",
      "params": ["block_num"]
    },

https://gitlab.syncad.com/hive/hive-js/-/blob/master/src/api/methods.js#L77

So get_block_header is a method of condenser_api, not block_api?
Now I am even more confused, as the Hive Developer Portal states differently:

https://developers.hive.io/apidefinitions/#block_api.get_block_header

For good measure, I try all APIs for the get_block_range method.

Could not find method get_block_range

The only code example containing 'blocks' I can find is this:

https://developers.hive.io/tutorials-javascript/stream_blockchain_transactions.html

I get absolutely 0 use out of that tutorial and decide to write a post about how stupid this is.
Am I making a mistake somewhere? Am I the only person to ever use get_block_range?

So I am searching beem to see how it works there:

blocks = self.blockchain.rpc.get_block_range({'starting_block_num': starting_block_num, "count": count}, api="block")['blocks']

That looks familiar! So I check its usage here:

https://beem.readthedocs.io/en/latest/beem.block.html#beem.block.Blocks

Obtain a list of blocks

Parameters: 
name_list (list) – list of accounts to fetch
count (int) – (optional) maximum number of accounts to fetch per call, defaults to 100
blockchain_instance (Steem/Hive) – Steem() or Hive() instance to use when accessing a RPCcreator = Account(creator, blockchain_instance=self)

List of accounts to fetch? Why accounts?

If you are lucky enough to find commented code around Hive, the comment is either confusing or just wrong half of the time, it feels.

...at least it looks like beem is using the block_api.

I am no wiser, as beem is using the rpc call pretty much the same as I do.

I am too frustrated to continue at this point.

That was my day developing a tool for Hive: No progress at all. Just hours of:

Could not find method get_block_range

Sort:  
Loading...

You best bet is to just emulate this function with looped iterating get block calls from the start and end block number of your range. Or what engrave said.

Nah, that is messy + slow.

block_range works for me now. Want the snippet?

Loading...

You were using method:call which is pre-appbase style of calling stuff (should be deprecated in 2018 or so),
In link that you have provided: https://developers.hive.io/apidefinitions/#block_api.get_block_range
Examples are working one, take a look this:

curl -s --data '{"jsonrpc":"2.0", "method":"block_api.get_block_range", "params":{"starting_block_num": 1, "count": 3}, "id":42}' https://gtg.openhive.network

Believe it or not: I spent the whole morning looking at that example, trying to figure out how I could make that work for me.

Thanks for nothing.

For future reference - jsonrpc is basically a POST request with JSON body, so anytime you want to port those examples from curl to fetch, axios, or whatever request library you're using, just copy the body from example:

I.e change this:

curl -s --data '{"jsonrpc":"2.0", "method":"block_api.get_block_range", "params":{"starting_block_num": 1, "count": 3}, "id":42}' https://api.openhive.network

Into this:

<html>
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
<script>
axios.post('https://api.openhive.network', {"jsonrpc":"2.0", "method":"block_api.get_block_range", "params":{"starting_block_num": 1, "count": 3}, "id":42}).then((result) => console.log(result.data))    
</script>
</html>

You can also use hive-js or dhive (although I'm not sure they support get_block_range request out-of-the-box).

hive-js and dhive do not support get_block_range, otherwise, I would not be in this mess.
I am not trying to build my own library, but I kinda have to.

...everything's working for now. Thanks!

I am not trying to build my own library, but I kinda have to.

Consider contributing to hive-js or dhive with appropriate changes.

As you have seen my coding skills above, I'd rather not.
Half of what's going on in those libs, I do not understand.

If I ever was going to work on a lib, I would make sure, I got paid for it, before I even started.

You seem like someone who'd have a much easier time fixing those libs.
I'd support a proposal.

I see what was complicated friend but surely you will find a way to develop the program you want