如何在Hedera上以编程方式获取区块号?

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

How can I obtain a block number programmatically on Hedera?

问题

我了解了来自HIP-415:区块介绍的信息,Hedera现在有区块,它们基于交易的时间戳分组成“记录文件”,这是它们被摄入Hedera镜像节点(来自Hedera共识节点)的一种方式。基本上,所有在同一个记录文件中的交易也被认为在同一个区块中,并且这些区块大约每2秒产生一次。

我能够将日期(时间戳)转换为区块号吗?我的目标是能够调用eth_getBlockByNumber,我需要根据时间戳确定该RPC请求的第一个参数。

curl -s -X POST \
    -H 'Content-Type: application/json' \
    -d '{"jsonrpc":"2.0","id":"2","method":"eth_getBlockByNumber","params":[MY_BLOCK_NUM, false]}' \
    http://localhost:7546

(需要在上述命令中找出MY_BLOCK_NUM。)

例如,这是区块4507206,对应于记录文件2023-05-10T08_27_52.012122604Z.rcd.gz,其中包含了今天UTC时间08:27:52.012108:27:53.7004之间的所有交易。假设我不知道区块号和记录文件是什么。我只有一个时间戳,我想从中找到相应的区块号。我该如何(以编程方式)做到这一点?

英文:

I understand from HIP-415: Introduction Of Blocks,
that Hedera now has blocks,
and they're based on timestamps of the transactions
being grouped together into "record files"
which are an artefact of how they are ingested into the Hedera mirror nodes
(from the Hedera consensus nodes).
Essentially all transactions that are inside the same record file
are considered to be in the same block as well,
and these blocks are produced approximately every 2 seconds.

Is there a way that I can convert a date (timestamp) into a block number?
My objective is to be able to invoke eth_getBlockByNumber,
and I need work out the first parameter for that RPC request
based on timestamp.

curl -s -X POST \
    -H 'Content-Type: application/json' \
    -d '{"jsonrpc":"2.0","id":"2","method":"eth_getBlockByNumber","params":[MY_BLOCK_NUM, false]}' \
    http://localhost:7546

(Need to work out MY_BLOCK_NUM in the ^ command above.)

For example, here's block 4507206,
which corresponds to the record file 2023-05-10T08_27_52.012122604Z.rcd.gz,
and contains all transactions between
08:27:52.0121 and 08:27:53.7004 in UTC today.
Suppose I did not know both what the block number was,
or what the record file was.
I only have a single timestamp,
and I want to find the corresponding block number from it.
How can I do this (programmatically)?

答案1

得分: 1

First, get the Unix timestamp for the date and time that you need. Use the -u flag if you want to specify the date/time in UTC (otherwise default to the timezone on your computer).

(Sample result: 1683707272)

Next, make an API request to the Hedera mirror node API:

  • /api/v1/blocks - Responds with a list of blocks
  • timestamp=gte:${TIMESTAMP} - Use this to specify the timestamp that you have from the previous step. The gte is a filter to say that you only want blocks whose timestamp is greater than or equal to the one you have specified.
  • limit=1 and order=asc - These combine with the above to further sort and filter the query such that it only returns the first block that comes after the specified timestamp.

(Sample result: 4507206)

And that's all, you now have your block number!

But let's go one step further to demonstrate that this block number can be used outside of the Hedera mirror node APIs (which are custom and specific to Hedera).

Let's use this value in JSON-RPC and obtain the block from there!

Convert that block number to hexadecimal using printf.

(Sample result: 0x44c646)

英文:

First, get the Unix timestamp for the date and time that you need. Use the -u flag if you want to specify the date/ time in UTC (otherwise default to the timezone on your computer).

TIMESTAMP=$(date -u -j -f "%F %T" "2023-05-10 08:27:52" "+%s")


(Sample result: 1683707272)

Next, make an API request to the Hedera mirror node API:

  • /api/v1/blocks - Responds with a list of blocks
  • timestamp=gte:${TIMESTAMP} - Use this to specify the timestamp that you have from the previous step. The gte is a filter to say that you only want block whose timestamp is greater than or equal to the one you have specified.
  • limit=1 and order=asc - These combine with the above to further sort and filter the query such that it only returns the first block that comes after the specified timestamp.
curl -s "https://testnet.mirrornode.hedera.com/api/v1/blocks?limit=1&order=asc&timestamp=gte:${TIMESTAMP}" | jq


We repeat the same command as above, but this time extract the blocknumber out of the full response object using a jq path.

BLOCKNUM=$( curl -s "https://testnet.mirrornode.hedera.com/api/v1/blocks?limit=1&order=asc&timestamp=gte:${TIMESTAMP}" | jq ".blocks[].number" )


(Sample result: 4507206)

And that's all, you now have your block number!

But let's go one step further to demonstrate that this block number can be used outside of the Hedera mirror node APIs (which are custom, and specific to Hedera).

Let's use this value in JSON-RPC, and obtain the block from there!

Convert that block number to hexadecimal using printf.

BLOCKNUMHEX=$( printf "0x%x\n" ${BLOCKNUM} )


(Sample result: 0x44c646)

Make a JSON-RPC request to an instance of hedera-json-rpc-relay. If you have one running locally, the endpoint is exposed at http://localhost:7546 by default. If you don't have that running, there are other options.

curl -s -X POST -H 'Content-Type: application/json' -d '{"jsonrpc":"2.0","id":"2","method":"eth_getBlockByNumber","params":["'"${BLOCKNUMHEX}"'", false]}' http://localhost:7546 | jq ".result"

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

发表评论

匿名网友

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

确定