无法使用Cosmpy模块按高度获取区块。

huangapple go评论40阅读模式
英文:

Unable to get Block by Height using Cosmpy module

问题

我正在与Cosmos Blockchain进行交互,尝试通过输入其高度来获取区块详细信息。为此,我尝试使用CosmPy模块。

我在库中找到了一个名为GetBlockByHeight()的函数,它可以做我想要的事情,但我无法使用它,因为它需要区块高度(我有了)和一个self参数(?)当我尝试这样调用它时:

from cosmpy.tendermint.interface import CosmosBaseTendermint
print(CosmosBaseTendermint.GetBlockByHeight(block_height))

我应该使用什么代码来正确调用这个函数?

英文:

I am playing with Cosmos Blockchain and trying to get Block details inputting its height. For this purpose I am trying to use CosmPy module.

I found a function inside the library that allows you to do exaclty what I want that is called

GetBlockByHeight()

But I am unable to use it since it require the block height (got it) and a self parameter (?) when I try to call it using:

from cosmpy.tendermint.interface import CosmosBaseTendermint
print(CosmosBaseTendermint.GetBlockByHeight(block_height))

What code should I use to properly call the function?

答案1

得分: 1

以下是已翻译的代码部分:

from google.protobuf.json_format import MessageToJson
from cosmpy.common.rest_client import RestClient
from cosmpy.protos.cosmos.base.tendermint.v1beta1.query_pb2 import (
    GetBlockByHeightRequest,
    GetBlockByHeightResponse,
)
from cosmpy.tendermint.rest_client import CosmosBaseTendermintRestClient

rest_client = RestClient("https://rest-fetchhub.fetch.ai:443")
client = CosmosBaseTendermintRestClient(rest_client)
resp = client.GetBlockByHeight(GetBlockByHeightRequest(height=6000000))
print(MessageToJson(resp))
英文:

Here's the working code:

from google.protobuf.json_format import MessageToJson
from cosmpy.common.rest_client import RestClient
from cosmpy.protos.cosmos.base.tendermint.v1beta1.query_pb2 import (
    GetBlockByHeightRequest,
    GetBlockByHeightResponse,
)
from cosmpy.tendermint.rest_client import CosmosBaseTendermintRestClient

rest_client = RestClient("https://rest-fetchhub.fetch.ai:443")
client = CosmosBaseTendermintRestClient(rest_client)
resp = client.GetBlockByHeight(GetBlockByHeightRequest(height=6000000))
print(MessageToJson(resp))

huangapple
  • 本文由 发表于 2023年5月11日 18:00:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76226397.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定