Blockchain

使用 JSON-RPC 找出乙太坊中的最新區塊雜湊

  • January 8, 2019

我正在用 PHP 開發一個乙太坊區塊瀏覽器,並成功地與我的伺服器 geth 節點建立了 JSON-RPC 通信。然而,當我試圖找出乙太坊的最後一個區塊時,我有點卡住了。我已經閱讀了https://ethereum.gitbooks.io/frontier-guide/content/rpc.html上的重要資訊,但我沒有找到任何方法來詢問乙太坊節點哪個是最後一個塊。獲得塊雜湊後,我看到有端點可以檢索有關該塊的完整資訊,但我不知道如何找到最新的塊雜湊。

API 端點 eth_blockNumber 應該返回最新的塊號,但是如何獲取雜湊?

有什麼幫助嗎?提前致謝!

要求:

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": [

   ]
 }
}

(我已經對結果進行了格式化以便於閱讀)。

我用“最新”來查找最新的塊。您也可以使用eth_blockNumber通話的結果。

來源:JSON-RPC - eth_getBlockByNumber

引用自:https://ethereum.stackexchange.com/questions/7647