php如何使用JSON-RPC查找以太坊中的最新块哈希hash值?

六月ゝ 毕业季﹏ 提交于 2019-11-30 23:50:32

我用PHP中开发了一个以太坊块资源管理器,并成功地与我的服务器Geth节点建立了JSON-RPC通信。然而,当我试图找出最后一个区块时,我陷入了困境。我在https://ethereum.gitbooks.io/frontier-guide/content/rpc.html 中读到了很不错的内容,但是我没有任何方法去查询到以太坊节点的哪一个是最后一个块。一旦我得到了块哈希,我就可以有接口检索完整的信息块,但我不知道如何找到最新的块哈希。

API接口中eth_blockNumber应该返回最新的块号,但是如何得到哈希?

问题解答

user@Kumquat:~$ curl -X POST --data '{"jsonrpc":"2.0","method":"eth_getBlockByNumber","params":["latest", false],"id":1}' localhost:8545
{"jsonrpc":"2.0","id":1,"result":{"difficulty":"0x31962a3fc82b",
 "extraData":"0x4477617266506f6f6c",
 "gasLimit":"0x47c3d8",
 "gasUsed":"0x0",
 "hash":"0x78bfef68fccd4507f9f4804ba5c65eb2f928ea45b3383ade88aaa720f1209cba",
 "logsBloom":"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
 "miner":"0x2a65aca4d5fc5b5c859090a6c34d164135398226",
 "nonce":"0xa5e8fb780cc2cd5e",
 "number":"0x1e655a",
 "parentHash":"0x8b535592eb3192017a527bbf8e3596da86b3abea51d6257898b2ced9d3a83826",
 "receiptRoot":"0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
 "sha3Uncles":"0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
 "size":"0x20e",
 "stateRoot":"0xdc6ed0a382e50edfedb6bd296892690eb97eb3fc88fd55088d5ea753c48253dc",
 "timestamp":"0x579f4981",
 "totalDifficulty":"0x25cff06a0d96f4bee",
 "transactions":[],
 "transactionsRoot":"0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
 "uncles":[]}}

latest也可以用eth_blockNumber,来得到最新块。

你可以参考这个以太坊JSON RPC API

如果想系统的学习php开发以太坊的话推荐一个很受欢迎的在线互动教程: php以太坊,主要是介绍使用php进行智能合约开发交互,进行账号创建、交易、转账、代币开发以及过滤器和事件等内容。

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!