【区块链开发入门】(二) 以太坊的编程接口

目录导航页
【区块链开发入门】(一) 以太坊的搭建与运行
【区块链开发入门】(二) 以太坊的编程接口
【区块链开发入门】(三) Solidity合约编译、部署
【区块链开发入门】(四) Truffle详解篇1
【区块链开发入门】(四) Truffle详解篇2


go-ethereum私有链搭建完成后,可以通过以下三种方式与区块链交互:

  • JavaScript Console:在geth控制台与以太坊交互。
  • JSON-RPC:一种无状态、轻量级的远程过程调用协议,可以跨语言调用。
  • web3.js:web3.js是以太坊提供的一个javascript库,它封装了以太坊的JSON-RPC API,提供了一系列与区块链交互的javascript对象和函数。
    这里主要介绍后面两种交互方式。

一、web3.js API

通过web3.js提供的web3对象,可以很方便的与以太坊交互,web3.js是以太坊提供的javascript库,它封装了以太坊的JSON-RPC API,所以web3.js可以与任何暴露了RPC接口的以太坊区块链节点连接。

1. 安装web3.js并创建实例

通过npm命令安装web3到项目中

$ sudo npm install --save web3@0.20.0
+ web3@0.20.0
updated 1 package in 15.195s

要是有web3.js与区块链交互,首先要创建web3对象然后再连接到以太坊节点。这里编写test.js文件如下:

var Web3 = require("web3");
//创建web对象
var web3 = new Web3();
//连接到以太坊节点
web3.setProvider(new Web3.providers.HttpProvider('http://localhost:8545'));
 
//列出所有的可用用户
var accounts = web3.eth.accounts;
console.info(accounts);

然后在一个终端中运行testrpc以启动测试链,然后在另一个终端运行test.js。
在这里插入图片描述

2. 账户相关API

查看当前连接的以太坊节点上所有可用的账号:

var accounts = web3.eth.accounts;
console.info(accounts);

查看各个账号里面的余额:

//查看accounts[0]账户的余额,返回的余额是以wei为单位表示的
var balance_1 = web3.eth.getBalance(web3.eth.accounts[0]);
console.log(balance_1.toString());

//将余额用ether为单位表示
var balance_eth = web3.fromWei(balance_1,"ether");
console.log(balance_eth.toString());

在这里插入图片描述

3.交易相关API

在以太坊两个账户之间的转账可以当做是一个交易,同样调用智能合约中的函数也可以当做是一个交易。

账户之间转账交易

因为要交易,所以用testrpc启动两个账户。在转账开始之前如果我们不知道需要给交易设置多少gas,可以通过下面的语句估算本次交易大概需要消耗的gas:

var estimate_gas = web3.eth.estimateGas({
    
    
	from: web3.eth.accounts[0],
	to: web3.eth.accounts[1],
	value: web3.toWei(1,"ether")
 
});
console.log(estimate_gas);//21000

在这里插入图片描述
一般简单的转账交易通常会消耗21000gas,得到gas后便可以构建一个转账交易了。转账交易有以下几个组成部分:

  • from:交易发送这的地址,这笔交易最后需要from的私钥签名。
  • to:接收这笔交转账的目的地址。
  • value:接收这笔交易转账的金额,以wei为单位。
  • gas:这笔交易中可使用的gas,未使用的gas会被退回。
  • gasPrice:这笔交易中附加的gas价格,默认是网络gas价格的平均值。

交易对象完成后即可通过web3.eth.sendTransaction向节点发送交易请求了。

//开始转账操作
var txId = web3.eth.sendTransaction({
    
    
	from: web3.eth.accounts[0],
	to: web3.eth.accounts[1],
	value: web3.toWei(1,"ether"),
	gas: estimate_gas
});
//返回值为交易hash
console.log(txId);//0x46e4c3afb49c480d76139122c8b8886a4bac95bf84d8fcdd4bde19c03b54322d

在这里插入图片描述
交易被发送成功后返回一个交易的hash值,通过这个hash值,进行以下两个操作:

//查询转账交易详情
var tx = web3.eth.getTransaction(txId);
console.log(tx);

在这里插入图片描述

//查询交易收据详情
var tx_receipt = web3.eth.getTransactionReceipt(txId);
console.log(tx_receipt);

在这里插入图片描述

发送一个已经签名的转账交易

上面发起交易的帐号都存储在我们连接的以太坊节点中,如果我们自己有一个帐号,并且这个帐号不存储在我们连接的以太坊节点中,那么我们就需要通过web3.eth.sendRawTransaction方法完成交易的签名与发送。

(1)创建待发送的交易

var address = "0x10b9be6f0dca0517cea9e503c7e5feec780aaf52";
var nonce = web3.eth.getTransactionCount(address,'pending');
var amount = web3.toWei(1,"ether");
var rawTx = {
    
    
        from:address,
        to:"0xc6a68278835f6e05dec1043cf828349a08bd9eb5",
        value:web3.toHex(amount),
        nonce:web3.toHex(nonce),
        gasLimit:web3.toHex("49674")
}

rawTx中包含:

  • nonce:记录from账户送出的交易数量,用来避免relay attack,每次发送都要加1。可以用eth_getTransactionCount查询目前账户的nonce。
  • gasLimit:当前交易最大消耗的gas数量,多余的会在交易结束后返还。
  • to:接受者地址。
  • value:要送的Ether数量,以wei为单位。

(2)给交易添加签名
要给刚刚创建的交易rawTx加上签名时,需要使用另外一个库,该库通过npm来安装:

$ npm install --save ethereumjs-tx@1.3.3

安装完成之后,就可以开始给rawTx创建签名:

//引入'ethereumjs-tx'包
var Tx = require('ethereumjs-tx');
 
//创建raw transaction
var tx = new Tx(rawTx);
 
//from地址的私钥
var privateKey = new Buffer('df6adb34fa73b81fff9bc8b05b86d9783063b3e1c3f086fc96ea31d207f235bd','hex');
 
//使用私钥给rawTx签名
tx.sign(privateKey);
 
//签名后的交易
var serializedTx = tx.serialize();
//控制台输出签名后交易信息
console.log(serializedTx.toString('hex'));

(3)发送已签名交易

//发送成功后会返回一个交易的hash值
web3.eth.sendRawTransaction('0x' + serializedTx.toString('hex'), function(err,hash) {
    
    
        if (!err) {
    
    
                console.log(hash);
        } else {
    
    
                console.log(err);
        }
});

运行成功后,控制台返回如下内容,返回签名后的交易信息和交易发送成功后的交易hash值。
testRPC端:
在这里插入图片描述
另一终端内容:
在这里插入图片描述

调用合约

在以太坊中可以通过交易调用智能合约中的函数,在这里只讲解如何通过交易调用合约。首先编写要使用的智能合约:

pragma solidity ^0.7.5;
contract Storage {
    
    
    uint256 public storedData;
    function set(uint256 data) public {
    
    
        storedData = data;
    }
    function get() public returns (uint256) {
    
    
        return storedData;
    }
}

然后把合约部署到以太坊网络上,合约部署成功后会得到三个比较重要的东西:
(1)合约地址:“0x0642c9ec44a5d6ecfeb19a370e87ee2e90eaf04e”
(2)合约的接口描述文件:

abi: [{
    
    
      constant: true,
      inputs: [],
      name: "storedData",
      outputs: [{
    
    ...}],
      payable: false,
      stateMutability: "view",
      type: "function"
  }, {
    
    
      constant: false,
      inputs: [{
    
    ...}],
      name: "set",
      outputs: [],
      payable: false,
      stateMutability: "nonpayable",
      type: "function"
  }, {
    
    
      constant: true,
      inputs: [],
      name: "get",
      outputs: [{
    
    ...}],
      payable: false,
      stateMutability: "view",
      type: "function"
  }]

(3)合约的签名,合约函数的获取方式如下:

//将合约的get()方法经过sha3计算后,取除了0x外的前面的8位
var get_func_sign = web3.sha3('get()').substr(2,8);
//将合约的set(uint256)方法经过sha3计算后,取除了0x外的前面的8位
var set_func_sign = web3.sha3('set(uint256)').substr(2,8);

Storage合约里所有函数的前面如下:
get()是6d4ce63c
set(uint256)是60fe47b1
在这里插入图片描述
目前有两种方法可以调用合约:

这里介绍第二种方式,首先构建一个调用get方法的交易:

var tx_get = {
    
    
    "from": "0xebbec3e7450a00b547dd93cf0fc843794e4dd9bf",
    "to": "0x0642c9ec44a5d6ecfeb19a370e87ee2e90eaf04e",//合约地址
    "data": "0x" + "6d4ce63c" + "0000000000000000000000000000000000000000000000000000000000000000"
};

tx_get中包含:

  • from:交易发起者地址。
  • to:合约地址。
  • data:传给合约的参数,由0x、6d4ce63c和参数0000000000000000000000000000000000000000000000000000000000000000组成,因为要调用get方法不需要传递参数,所以这里为传入的为0。

用get方法不需要传递参数,所以这里传入的是0。

var result = web3.eth.call(tx_get);
console.info(result);

在这里插入图片描述
执行完返回的是0,因为我们还没有开始给storedData设置值。所以需要调用set方法给storedData设置一个值,首先需要构建一个set方法的交易,并设置set函数传入值为1。

var tx_set = {
    
    
    "from": "0xebbec3e7450a00b547dd93cf0fc843794e4dd9bf",
    "to": "0x0642c9ec44a5d6ecfeb19a370e87ee2e90eaf04e",
    "gaslimit": web3.toHex("50000"),
    "data": "0x" + "60fe47b1" + "0000000000000000000000000000000000000000000000000000000000000001"
};

执行交易:

var txId = web3.eth.sendTransaction(tx_set);//返回交易hash
console.info(txId);

在这里插入图片描述
执行成功后,继续调用get方法看返回的是不是刚刚传入的值:

发送已签名交易调用合约

和上面的账户转账的问题一样,如果我们的账户不在连接的以太坊节点中,如果通过这个账户调用合约上的函数呢?同样的,可以通过web3.eth.sendRawTransaction方法完成调用合约交易的签名与发送。
(1)创建待发送的交易:

var rawTx = {
    
    
    "from": "0xebbec3e7450a00b547dd93cf0fc843794e4dd9bf",
    "to": "0x0642c9ec44a5d6ecfeb19a370e87ee2e90eaf04e",
    "gas": web3.toHex("50000"),
    "value": '0x00',
    "nonce": web3.toHex(nonce),
    "gaslimit": web3.toHex("50000"),
    "data": "0x60fe47b10000000000000000000000000000000000000000000000000000000000000003"
};

rawTx中包含:

  • nonce:记录from账户送出的交易数量,用来避免replay attack,每次发送都要加1,可以用eth_getTransactionCount查询目前账户的nonce。
  • gasLimit:当前交易最大消耗gas数量,多余的会再交易结束后返还。
  • gas:交易发起者提供的gas数量。
  • to:合约地址。
  • value:要送的Ether数量,以wei为单位,这里设置为0。因为这里只需调用合约,所以不要转账,
  • data:传给合约的参数,由0x、60fe47b和一个32byte的参数0000000000000000000000000000000000000000000000000000000000000003组成,因为要调用set方法,所以这里为传入的值为3。
    (2)给交易添加签名
    代码如下:
//引入包
var Tx = require('ethereumjs-tx');

//创建raw transaction
var tx = new Tx(rawTx);

//from地址的私钥
var privateKey = new Buffer('c8cfcc06bba21293f3012e357e3a98c15db1f48f1853f8d8f48f30fa817a6819','hex');

//使用私钥给rawTx签名
tx.sign(privateKey);

//签名后的交易
var serializedTx = tx.serialize();
//控制台中输出签名后的交易信息
console.log(serializedTx.toString('hex'));

在这里插入图片描述
(3)发送已签名交易
代码如下:

web3.eth.sendRawTransaction('0x' + serializedTx.toString('hex'), function(err,hash) {
    
    
        if (!err) {
    
    
                console.log(hash);
        } else {
    
    
                console.log(err);
        }
});

交易成功后会返回一个交易的hash值。接着调用合约的get函数,看看是否成功将storedData值设置为3。

var result = web3.eth.call(tx_get);
console.info(result);

4.区块相关API

以下是几个常用的查询区块的方法。

查询最新区块

有两种方法可以额查询最新区块信息:
(1)通过最新区块编号查询

var blockNumber=web3.eth.blockNumber;
var last_block=web3.eth.getBlock(blockNumber);
console.info(last_block);

(2)直接通过last参数放最新区块

var last_block=web3.eth.gatBlock('lastest');
console.info(last_block);

返回结果:

{
    
     number: 3,
  hash: '0xbd5896262abd5083b2fcdcc8822635ec171855b2d5fd43228ec76574401a19e4',
  parentHash: '0x63323e1a55acea03e8b1f6743dc9817632a53eb1e148c2f87ccc9454bf4cbabb',
  nonce: '0x0',
  sha3Uncles: '0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347',
  logsBloom: '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
  transactionsRoot: '0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421',
  stateRoot: '0x58ebcc7c2ad22e5739ddd62e178e65ed6c2751a1723dfc10f86b00d3cd34b55b',
  receiptsRoot: '0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421',
  miner: '0x0000000000000000000000000000000000000000',
  difficulty: BigNumber {
    
     s: 1, e: 0, c: [ 0 ] },
  totalDifficulty: BigNumber {
    
     s: 1, e: 0, c: [ 0 ] },
  extraData: '0x0',
  size: 1000,
  gasLimit: 6721975,
  gasUsed: 21464,
  timestamp: 1606625261,
  transactions: 
   [ '0x9be691211a47a8a3e982cf3480a79c279af076bf1ddb6d13294d30fdfb775542' ],
  uncles: [] }

根据区块编号或Hash查询区块

//根据区块编号
var block=web3.eth.getBlock(0);
console.info(block);

//根据区块Hash
var block=web3.eth.getBlock('0xca0f6794e3046d8889167982c4b0db7b346a98a8009bbf287bb7113eb23d09a4');
console.info(block);

返回结果:

{
    
     number: 0,
  hash: '0xca0f6794e3046d8889167982c4b0db7b346a98a8009bbf287bb7113eb23d09a4',
  parentHash: '0x0000000000000000000000000000000000000000000000000000000000000000',
  nonce: '0x0',
  sha3Uncles: '0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347',
  logsBloom: '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
  transactionsRoot: '0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421',
  stateRoot: '0x418d88ba7394064343ddb36d4bb7ee1ab519c2bb0609d4b46aa8cb26794876bc',
  receiptsRoot: '0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421',
  miner: '0x0000000000000000000000000000000000000000',
  difficulty: BigNumber {
    
     s: 1, e: 0, c: [ 0 ] },
  totalDifficulty: BigNumber {
    
     s: 1, e: 0, c: [ 0 ] },
  extraData: '0x0',
  size: 1000,
  gasLimit: 6721975,
  gasUsed: 0,
  timestamp: 1606575280,
  transactions: [],
  uncles: [] }

二、JSON-RPC API

JSON-RPC API是一个无状态、轻量级的远程过程调用协议,可以跨语言调用,通过JSON-RPC协议我们可以与任何一个暴露了RPC接口的以太坊节点通信。和上面的web3js API一样,这里同样选择几个常用的API介绍。

1.账户相关API

查看当前连接的以太坊节点上所有可用账号:

$ curl -X POST http://localhost:8545 -d '{"jsonrpc":"2.0","method":"eth_accounts","params":[],"id":1}'

{
    
    "id":1,"jsonrpc":"2.0","result":["0xebbec3e7450a00b547dd93cf0fc843794e4dd9bf"]}

执行完后会返回节点上所有可用的账号,如果没有可用账号会返回一个空的数组。
接下来查看账号里面的余额。

$ curl -X POST http://localhost:8545 -d '{"jsonrpc":"2.0","method":"eth_getBalance","params":["0xebbec3e7450a00b547dd93cf0fc843794e4dd9bf"],"id":1}'

{
    
    "id":1,"jsonrpc":"2.0","result":"0x056bc75e2d630f0478"}

2.交易相关API

和前面介绍的web3.js API一样,这里我们同样也会介绍两张类型的交易。

账户之间转账交易

在转账操作之前可以先估算一下需要多少gas:

$ curl -X POST http://localhost:8545 -d '{"jsonrpc":"2.0","method":"eth_estimateGas","params":[{"from":"0xebbec3e7450a00b547dd93cf0fc843794e4dd9bf","to":"0xebbec3e7450a00b547dd93cf0fc843794e4dd9bf","value":"0xde0b6b3a7640000"}],"id":1}'

{
    
    "id":1,"jsonrpc":"2.0","result":"0x5208"}

从账户0xebbec3e7450a00b547dd93cf0fc843794e4dd9bf给账户0xebbec3e7450a00b547dd93cf0fc843794e4dd9bf转1ether:

$ curl -X POST http://localhost:8545 -d '{"jsonrpc":"2.0","method":"eth_sendTransaction","params":[{"from":"0xebbec3e7450a00b547dd93cf0fc843794e4dd9bf","to":"0xebbec3e7450a00b547dd93cf0fc843794e4dd9bf","value":"0xde0b6b3a7640000","gas":"0x5208"}],"id":1}'

{
    
    "id":1,"jsonrpc":"2.0","result":"0x511b8136bc14a9bd604c119fe9f4e13e1bd132777e7501a9853080df520a7bd9"}

根据返回的交易hash:0x511b8136bc14a9bd604c119fe9f4e13e1bd132777e7501a9853080df520a7bd9,查看发送交易的详情:

$ curl -X POST http://localhost:8545 -d '{"jsonrpc":"2.0","method":"eth_getTransactionByHash","params":["0x511b8136bc14a9bd604c119fe9f4e13e1bd132777e7501a9853080df520a7bd9"],"id":1}'

{
    
    "id":1,"jsonrpc":"2.0","result":{
    
    "hash":"0x511b8136bc14a9bd604c119fe9f4e13e1bd132777e7501a9853080df520a7bd9","nonce":"0x03","blockHash":"0x01b1d0ff6680e2a311d32cf2ffd37d8d12246432e56d5bcaf11b81b46fb13d9c","blockNumber":"0x04","transactionIndex":"0x00","from":"0xebbec3e7450a00b547dd93cf0fc843794e4dd9bf","to":"0xebbec3e7450a00b547dd93cf0fc843794e4dd9bf","value":"0x0de0b6b3a7640000","gas":"0x5208","gasPrice":"0x01","input":"0x0"}}

转账交易被成功打包后,可查看交易在区块中的详细信息:

$ curl -X POST http://localhost:8545 -d '{"jsonrpc":"2.0","method":"eth_getTransactionReceipt","params":["0x511b8136bc14a9bd604c119fe9f4e13e1bd132777e7501a9853080df520a7bd9"],"id":1}'

{
    
    "id":1,"jsonrpc":"2.0","result":{
    
    "transactionHash":"0x511b8136bc14a9bd604c119fe9f4e13e1bd132777e7501a9853080df520a7bd9","transactionIndex":"0x00","blockHash":"0x01b1d0ff6680e2a311d32cf2ffd37d8d12246432e56d5bcaf11b81b46fb13d9c","blockNumber":"0x04","gasUsed":"0x5208","cumulativeGasUsed":"0x5208","contractAddress":null,"logs":[],"status":1}}

调用合约

这边还是使用之前部署好的storage合约,地址是0x0642c9ec44a5d6ecfeb19a370e87ee2e90eaf04e,接着计算一下合约函数的前面,这样就具备了调用合约的两个基本要素了。
计算get()函数签名,将合约的get()方法先经过sha3计算:

$ curl -X POST http://localhost:8545 -d '{"jsonrpc":"2.0","method":"web3_sha3","params":["get()"],"id":1}'

{
    
    "id":1,"jsonrpc":"2.0","result":"0x6d4ce63caa65600744ac797760560da39ebd16e8240936b51f53368ef9e0e01f"}

执行成功后,对返回值取除了0x外的前面8位,即6d4ce63c。
使用同样的方法计算set(uint256)方法的签名:

$ curl -X POST http://localhost:8545 -d '{"jsonrpc":"2.0","method":"web3_sha3","params":["set(uint256)"],"id":1}'

{
    
    "id":1,"jsonrpc":"2.0","result":"0x60fe47b16ed402aae66ca03d2bfc51478ee897c26a1158669c7058d5f24898f4"}

执行成功后,对返回值取除了0x外的前面8位,即60fe47b1。
一切准备工作已经完成了,现在开始调用合约的get方法:

$ curl -X POST http://localhost:8545 -d '{"jsonrpc":"2.0","method":"eth_call","params":[{"from":"0x01a6306f4892335d5f661c49533e748b919511ce","to":"0x0642c9ec44a5d6ecfeb19a370e87ee2e90eaf04e","data":"0x6d4ce63c0000000000000000000000000000000000000000000000000000000000000000"},"latest"],"id":1}'

{
    
    "id":1,"jsonrpc":"2.0","result":"0x0"}

params中包含:

  • from:交易发送者的地址。
  • to:合约地址。
  • data:传给合约的参数。由0x、6d4ce63c和参数0000000000000000000000000000000000000000000000000000000000000000组成,因为要调用get方法不需要传递参数,所以这里为传入的为0。

接下来调用合约的set函数给storedData变量赋值4:

$ curl -X POST http://localhost:8545 -d '{"jsonrpc":"2.0","method":"eth_sendTransaction","params":[{"from":"0x01a6306f4892335d5f661c49533e748b919511ce","to":"0x0642c9ec44a5d6ecfeb19a370e87ee2e90eaf04e","gas":"0xc350","gaslimit":"0xc350","data":"0x60fe47b10000000000000000000000000000000000000000000000000000000000000004"}],"id":1}'

{
    
    "id":1,"jsonrpc":"2.0","result":"0x1d52dcb915618af05f7d55d2223eb139c64e74b92a4e721b65270084be891517"}

params中包含:

  • from:交易发送者的地址。
  • to:合约地址。
  • gas:交易发起者提供的gas数量。
  • gaslimit:这笔交易中允许消耗最大的gas数量。
  • data:传给合约的参数。由0x、60fe47b1和参数0000000000000000000000000000000000000000000000000000000000000004组成,这里调用set方法,所以给set函数传入值4。
$ curl -X POST http://localhost:8545 -d '{"jsonrpc":"2.0","method":"eth_call","params":[{"from":"0x01a6306f4892335d5f661c49533e748b919511ce","to":"0x0642c9ec44a5d6ecfeb19a370e87ee2e90eaf04e","data":"0x6d4ce63c0000000000000000000000000000000000000000000000000000000000000000"},"latest"],"id":1}'

{
    
    "id":1,"jsonrpc":"2.0","result":"0x0000000000000000000000000000000000000000000000000000000000000004"}

执行成功后,可以看到确实返回了4。

3.区块相关API

查询最新区块编号

$ curl -X POST http://localhost:8545 -d '{"jsonrpc":"2.0","method":"eth_blockNumber","params":[],"id":1}'

{
    
    "id":1,"jsonrpc":"2.0","result":"0x01"}

根据区块编号查询区块信息

$ curl -X POST http://localhost:8545 -d '{"jsonrpc":"2.0","method":"eth_getBlockByNumber","params":["0x01",true],"id":1}'

{
    
    
	"id":1,
	"jsonrpc":"2.0",
	"result":{
    
    
		"number":"0x01",
		"hash":"0x940a00b77eed119a58ee9a9490875b6bc125f65f5b8934c531b944f7e8bc1528",
		"parentHash":"0x83d4f6b1d0b5178bf46716044aa0c3909e5d4d99aba7786afbf24580f5e043f7",
		"nonce":"0x0",
		"sha3Uncles":"0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
		"logsBloom":"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
		"transactionsRoot":"0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
		"stateRoot":"0xca904bcc45b962f85ab4c436d5bf9738c5cece85416e613b52d05b84dfc244e6",
		"receiptsRoot":"0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
		"miner":"0x0000000000000000000000000000000000000000",
		"difficulty":"0x0",
		"totalDifficulty":"0x0",
		"extraData":"0x0",
		"size":"0x03e8",
		"gasLimit":"0x6691b7",
		"gasUsed":"0x53d8",
		"timestamp":"0x5fc34767",
		"transactions":[
			{
    
    "hash":"0x1d52dcb915618af05f7d55d2223eb139c64e74b92a4e721b65270084be891517",
			"nonce":"0x0",
			"blockHash":"0x940a00b77eed119a58ee9a9490875b6bc125f65f5b8934c531b944f7e8bc1528",
			"blockNumber":"0x01",
			"transactionIndex":"0x00",
			"from":"0x01a6306f4892335d5f661c49533e748b919511ce",
			"to":"0x0642c9ec44a5d6ecfeb19a370e87ee2e90eaf04e",
			"value":"0x0",
			"gas":"0xc350",
			"gasPrice":"0x01",
			"input":"0x60fe47b10000000000000000000000000000000000000000000000000000000000000004"
			}],
		"uncles":[]
	}
}

根据区块hash查询区块信息

$ curl -X POST http://localhost:8545 -d '{"jsonrpc":"2.0","method":"eth_getBlockByNumber","params":["0x940a00b77eed119a58ee9a9490875b6bc125f65f5b8934c531b944f7e8bc1528",true],"id":1}'

猜你喜欢

转载自blog.csdn.net/weixin_44029550/article/details/110009549
今日推荐