第09篇 geth console基本操作

本文环境:

      区块链:以太坊POA联盟链;

      出块节点数:3;

      操作系统:windows 64;

      节点版本:Geth1.9.14;

参考文档https://web3js.readthedocs.io/en/v1.2.8/

目录

一、进入操作界面

1、console

2、attach

二、账户管理

3、personal.newAccount

4、personal.unlockAccount

5、personal.lockAccount

6、personal.listAccounts

7、personal.sendTransaction

三、挖矿

8、miner.start

9、miner.stop

10、miner.setExtra

11、miner.setEtherbase

四、交易池

12、txpool.status

13、txpool.inspect

14、txpool.content

五、节点

15、admin.datadir

16、admin.nodeInfo

17、admin.addPeer

18、admin.peers

19、admin.startRPC

20、admin.startRPC

六、网络

21、net.listening

22、net.peerCount

23、net.version

七、区块操作

24、eth.coinbase

25、eth.defaultBlock

26、eth.mining

27、eth.hashrate

28、eth.gasPrice

29、eth.accounts

30、eth.blockNumber

31、eth.getBalance

32、eth.getBlock

33、eth.getBlockTransactionCount

34、eth.getTransaction

35、eth.getTransactionFromBlock

36、eth.getTransactionReceipt

37、eth.getTransactionCount

38、eth.sendTransaction


一、进入操作界面

1、console

启动geth节点时,使用console参数,该参数启动交互式JavaScript环境。比如:

geth --datadir "d:/data/" --rpc --rpcport 8545 --rpcapi "personal,eth,net,web3,admin" --rpccorsdomain "*" console

进入节点后即可以接收命令;

该方式的不足是:因为节点在同步区块,信息比较多,用户输入的命令及反馈信息不够清晰;

2、attach

也可以通过attach命令进入指定节点,该参数通过连接已知节点,启动交互式JavaScript环境。比如本机已经启动节点,通常可以用以下方式进入命令界面:

./geth attach ipc:/[dataDir]/geth.ipc        //linux
geth attach ipc:\\.\pipe\geth.ipc            //windows
geth attach http://191.168.1.1:8545          //需要节点启用HTTP-RPC服务,提示,不如ipc连接安全
geth attach ws://191.168.1.1:8546            //需要节点启用WS-RPC服务,提示,不如ipc连接安全

本文示例

C:\Program Files\Geth>geth attach ipc:\\.\pipe\geth.ipc
Welcome to the Geth JavaScript console!

instance: Geth/v1.8.11-stable-dea1ce05/windows-amd64/go1.10.2
coinbase: 0x0297a8913cc187d60712f095a7e1bf2421bfcd40
at block: 18784 (Tue, 19 May 2020 20:30:18 CST)
 datadir: d:\data_node
 modules: admin:1.0 clique:1.0 debug:1.0 eth:1.0 miner:1.0 net:1.0 personal:1.0 rpc:1.0 txpool:1.0 web3:1.0

>

二、账户管理

3、personal.newAccount

方法生成一个新的私钥并直接存入密钥库目录。密钥文件使用指定的密钥加密。

该方法需要在启动geth时--rpcapi包含了personal;

> personal.newAccount("password")                //直接使用密码
> personal.newAccount()                          //控制台提示输入密码并确认

本文示例:

> personal.newAccount("123456")
"0x38d8b866a1abebca20afc004622f5355eefeb568"
> personal.newAccount()
Passphrase:
Repeat passphrase:
"0xda25997b15a6bea86116942b8ab69a5620d82284"
>

4、personal.unlockAccount

方法对密码库中指定地址对应的密钥进行解密。

当使用Geth的JavaScript 控制台时,账户默认是锁定状态;

尝试解锁时,密码和解锁时长都是可选的,默认的解锁周期为300秒。如果未提供密码,控制台将提示交互输密码。

解密的密钥将保存在内存中直到解锁周期超时。显示地将解锁周期设置为0秒将解锁该密钥直到退出geth程序。

账号解锁后可以用于签名(eth_sign)和发送交易(eth_sendTransaction)调用。

方式一:指定解锁账户地址,交互方式输入密码,默认解锁300秒
> personal.unlockAccount("0x38d8b866a1abebca20afc004622f5355eefeb568")  
     
方式二:指定本地具体账户,交互方式输入密码,默认解锁300秒
> personal.unlockAccount(eth.accounts[1])  

方式三:指定解锁账户地址,解锁10分钟
> personal.unlockAccount(eth.accounts[1],"123456",600)  

方式四:指定解锁账户地址,交互方式输入密码,解锁10分钟
> personal.unlockAccount(eth.accounts[1],null,600) 

方式五:指定解锁账户地址,关闭geth前一直解锁状态
> personal.unlockAccount(eth.accounts[1],"123456",0)  

本文示例:

> personal.unlockAccount(eth.accounts[1])
Unlock account 0x38d8b866a1abebca20afc004622f5355eefeb568
Passphrase:
true
> personal.unlockAccount(eth.accounts[1],"123456",0)
true
>

注意:在最新版本(Geth1.9.14)中,当执行personal.unlockAccount()或在程序中调用personal_unlockAccount接口时,会出现:account unlock with HTTP access is forbidden异常。

如果已经了解打开此功能的风险,可通过在geth启动命令中添加参数"--allow-insecure-unlock"实现正常解锁:

geth --allow-insecure-unlock

5、personal.lockAccount

方法从内存中移除指定地址对应的私钥,该账户将不能再发送交易。

> personal.lockAccount("0x38d8b866a1abebca20afc004622f5355eefeb568")    
或者   
> personal.lockAccount(eth.accounts[1])  

本文示例:

> personal.lockAccount(eth.accounts[1])
true
>

6、personal.listAccounts

方法返回密钥库中所有密钥对应的以太坊账户地址。

> personal.listAccounts

本文示例:

> personal.listAccounts
["0x0297a8913cc187d60712f095a7e1bf2421bfcd40", "0x38d8b866a1abebca20afc004622f5355eefeb568", "0xda25997b15a6bea86116942b8ab69a5620d82284"]
>

7、personal.sendTransaction

方法验证指定的密码并提交交易,该方法的交易参数与eth_sendTransaction一样,同时包含from账户地址。

如果密码可以成功解密交易中from地址对应的私钥,那么该方法将验证交易、 签名并广播到以太坊网络中。

由于在sendTransaction方法调用时,from账户并未在节点中全局解锁 (仅在该调用内解锁),因此from账户不能用于其他RPC调用。

> personal.sendTransaction(tx, passphrase)

本文示例:

> eth.getBalance(eth.accounts[1])
1.000000000001e+24
> personal.unlockAccount(eth.accounts[1],"123456",0)
true
> personal.sendTransaction({from: eth.accounts[1], to: "0xda25997b15a6bea86116942b8ab69a5620d82284", value: 2000000000000000000})          //默认最小单位
"0xda469a3221a34ce81010b9cfc4df66e5d9cbffd46c670fe7c5da1a263aa4e62b"
> eth.getBalance(eth.accounts[1])
9.9999799998e+23
> eth.getBalance("0xda25997b15a6bea86116942b8ab69a5620d82284")
2000000000000000000
>
> personal.sendTransaction({from: eth.accounts[1], to: "0xda25997b15a6bea86116942b8ab69a5620d82284", value: web3.toWei(1.23, "ether")})    //自定义以太单位
"0x20e5eca5dd2671fa528958705aa3045dc8d6cf79374a50794dd18a9bb76ea2fc"

通过示例可以看出:1.发送的单位默认是最小单位"wei";用户也可以指定单位;2.该交易支付了gas费;

三、挖矿

8、miner.start

方法启动具有指定线程数量的CPU挖矿进程。

> miner.start()

本文示例:

> miner.start()                                      //执行命令
null                                                 //返回null 
INFO [05-25|07:55:42] Starting mining operation      //节点开始挖矿操作
> eth.mining                                         //查询挖矿状态
true                                                 //正在挖矿
> miner.start(1)                                     //设置线程参数,示例为开启1个线程挖矿

注意:本例在POA联盟链测试中,需要解锁节点挖矿账号(通常默认是eth.accounts[0],可以使用eth.coinbase查询)。

9、miner.stop

方法启动具有指定线程数量的CPU挖矿进程。

> miner.stop()

本文示例:

> miner.stop()                                       //执行命令
true                                                 //返回true
> eth.mining                                         //查询挖矿状态
false                                                //没有挖矿
>

10、miner.setExtra

方法用来设置要包含在挖出区块中的额外的数据,最多32个字节。

> miner.setExtra(string)

本文示例:

> miner.setExtra("Node 02 : Wonder Block")
true
>

11、miner.setEtherbase

方法用来设置接收挖矿奖励的以太坊账号。

> miner.setEtherbase(address)

本文示例:

> personal.listAccounts
["0x0297a8913cc187d60712f095a7e1bf2421bfcd40", "0x38d8b866a1abebca20afc004622f5355eefeb568", "0xda25997b15a6bea86116942b8ab69a5620d82284"]
> eth.coinbase
"0x0297a8913cc187d60712f095a7e1bf2421bfcd40"
> miner.setEtherbase(eth.accounts[1])
true
> eth.coinbase
"0x38d8b866a1abebca20afc004622f5355eefeb568"
>

四、交易池

12、txpool.status

方法可以用来查询交易池中当前等待打包入下一个区块的交易数量等信息。

status属性的值是一个包含两个字段的对象:pending和queued,每个字段的值都是一个关联数组。

> txpool.status

本文示例:

> txpool.status
{
  pending: 3,
  queued: 5
}
>

13、txpool.inspect

方法可以列出交易池中当前等待打包进下一个区块的交易的概要信息。 

inspect属性的值是一个包含两个字段的对象:pending和queued,每个字段都是一个关联数组。

注意:可能会有多个交易与同一个账号和nonce相关联。当一个用户使用不同的gas设置多次广播交易时,可能发生上述情况。

> txpool.inspect

本文示例:

> txpool.inspect
{
  pending: {
    0x26588a9301b0428d95e6fc3a5024fce8bec12d51: {
      31813: ["0x3375ee30428b2a71c428afa5e89e427905f95f7e: 0 wei + 500000 × 20000000000 gas"]
    },
    0x2a65aca4d5fc5b5c859090a6c34d164135398226: {
      563662: ["0x958c1fa64b34db746925c6f8a3dd81128e40355e: 1051546810000000000 wei + 90000 × 20000000000 gas"],
      563663: ["0x77517b1491a0299a44d668473411676f94e97e34: 1051190740000000000 wei + 90000 × 20000000000 gas"]
    },
    0x9174e688d7de157c5c0583df424eaab2676ac162: {
      3: ["0xbb9bc244d798123fde783fcc1c72d3bb8c189413: 30000000000000000000 wei + 85000 × 21000000000 gas"]
    },
    0xea674fdde714fd979de3edf0f56aa9716b898ec8: {
      70148: ["0xe39c55ead9f997f7fa20ebe40fb4649943d7db66: 1000767667434026200 wei + 90000 × 20000000000 gas"]
    }
  },
  queued: {}
}

14、txpool.content

方法可以用来列出当前在池中的待定和排队交易清单。

content属性的值为一个包含两个字段的对象:pending和queued,每个字段都是一个关联数组。

注意:可能会有多个交易与同一个账号和nonce相关联。当一个用户使用不同的gas设置多次广播交易时,可能发生上述情况。

> txpool.content

本文示例:

> txpool.content
{
  pending: {
    0x0216d5032f356960cd3749c31ab34eeff21b3395: {
      806: [{
        blockHash: "0x0000000000000000000000000000000000000000000000000000000000000000",
        blockNumber: null,
        from: "0x0216d5032f356960cd3749c31ab34eeff21b3395",
        gas: "0x5208",
        gasPrice: "0xba43b7400",
        hash: "0xaf953a2d01f55cfe080c0c94150a60105e8ac3d51153058a1f03dd239dd08586",
        input: "0x",
        nonce: "0x326",
        to: "0x7f69a91a3cf4be60020fb58b893b7cbb65376db8",
        transactionIndex: null,
        value: "0x19a99f0cf456000"
      }]
    },
    0x24d407e5a0b506e1cb2fae163100b5de01f5193c: {
      34: [{
        blockHash: "0x0000000000000000000000000000000000000000000000000000000000000000",
        blockNumber: null,
        from: "0x24d407e5a0b506e1cb2fae163100b5de01f5193c",
        gas: "0x44c72",
        gasPrice: "0x4a817c800",
        hash: "0xb5b8b853af32226755a65ba0602f7ed0e8be2211516153b75e9ed640a7d359fe",
        input: "0xb61d27f600000000000000000000000024d407e5a0b506e1cb2fae163100b5de01f5193c00000000000000000000000000000000000000000000000053444835ec580000000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
        nonce: "0x22",
        to: "0x7320785200f74861b69c49e4ab32399a71b34f1a",
        transactionIndex: null,
        value: "0x0"
      }]
    }
  },
  queued: {}
}

五、节点

15、admin.datadir

方法可用来查询当前运行的Geth节点的区块链数据存储绝对路径。

> admin.datadir

本文示例:

> admin.datadir
"d:\\data_node"
>

16、admin.nodeInfo

方法可用来查询当前运行的geth节点旳网络相关信息,包括p2p协议信息以及运行中的应用协议信息,例如eth、shh等。

> admin.nodeInfo

本文示例:

> admin.nodeInfo
{
  enode: "enode://fc791a323a5e88ea15247323ac83c4afd9e61283027fb92454c419868170e847747cf2c1c4702c556e2817cc8d74c5dd1011bc6dbf8870dbc5c0b3476c9cfc11@[::]:30303?discport=0",
  id: "fc791a323a5e88ea15247323ac83c4afd9e61283027fb92454c419868170e847747cf2c1c4702c556e2817cc8d74c5dd1011bc6dbf8870dbc5c0b3476c9cfc11",
  ip: "::",
  listenAddr: "[::]:30303",
  name: "Geth/v1.8.11-stable-dea1ce05/windows-amd64/go1.10.2",
  ports: {
    discovery: 0,
    listener: 30303
  },
  protocols: {
    eth: {
      config: {
        byzantiumBlock: 0,
        chainId: 517,
        clique: {...},
        constantinopleBlock: 0,
        eip150Block: 0,
        eip150Hash: "0x0000000000000000000000000000000000000000000000000000000000000000",
        eip155Block: 0,
        eip158Block: 0,
        homesteadBlock: 0
      },
      difficulty: 146201,
      genesis: "0x64ebd4b7686ad2b29ba29f67bd3f7aee5d71b20a5bf4ef5010f39292bb8e3793",
      head: "0xa5e7fb2ea030494f42b00f8c7587f5eb51083f2a2527e7ae9a23196c5a36d808",
      network: 517
    }
  }
}
>

获取具体项的信息:

> admin.nodeInfo.enode
"enode://fc791a323a5e88ea15247323ac83c4afd9e61283027fb92454c419868170e847747cf2c1c4702c556e2817cc8d74c5dd1011bc6dbf8870dbc5c0b3476c9cfc11@[::]:30303?discport=0"
>

17、admin.addPeer

方法可以将新的远程节点加入到本地跟踪的远程节点列表,本地节点将始终尝试保持与这些远程节点旳连接,并在必要时进行重连。

方法接受单一参数,即远程节点旳enode地址,当添加成功后会返回TRUE, 否则返回false。

> admin.addPeer(url)

本文示例:

> admin.addPeer("enode://fc622dfadcb070649c0c23af42940e40bfdda1d81bf03ed620b3a61d2461252c86eaa522abb7e05e5d7448447275f3f1a65f91dc2296fe04b5ab60cf67a962a8@59.111.1.1:30309?discport=0")
true
>

添加时要使用节点的真实IP地址,比如云服务器要使用外网地址。

18、admin.peers

方法可用来查询当前geth节点已连接的远程节点的相关信息,包括p2p协议信息以及运行中的应用协议信息,例如eth、shh等。

> admin.peers

本文示例:

> admin.peers
[{
    caps: ["eth/63", "eth/64", "eth/65"],
    id: "fc622dfadcb070649c0c23af42940e40bfdda1d81bf03ed620b3a61d2461252c86eaa522abb7e05e5d7448447275f3f1a65f91dc2296fe04b5ab60cf67a962a8",
    name: "Geth/v1.9.14-stable-6d74d1e5/linux-amd64/go1.14.2",
    network: {
      inbound: false,
      localAddress: "192.168.3.9:54783",
      remoteAddress: "59.111.1.1:30309",
      static: true,
      trusted: false
    },
    protocols: {
      eth: {
        difficulty: 146255,
        head: "0x810ef3e7756ecfcffb50300ee2b55f0568cbe79e8ad36adad98720083864b104",
        version: 63
      }
    }
}]
> 

19、admin.startRPC

方法启动一个基于HTTP的JSON RPC API服务器来处理客户端的调用请求。所有的参数都是可选的:

  • host: 要监听的网络地址,默认值:localhost
  • port: 要监听的网络端口,默认值:8545
  • cors: 要使用的跨源资源共享头,默认值:""
  • apis: 要透过该服务接口提供服务的API模块,默认值:"eth,net,web3"

方法返回一个布尔值来表示HTTP RPC监听是否正确启动。需要指出的是,任何时候都只能激活一个HTTP端结点。

> admin.startRPC(host, port, cors, apis)

本文示例:

> admin.startRPC("127.0.0.1", 8545)
true
> 

admin.stopRPC

方法用来关闭当前启动的HTTP RPC端结点。由于一个Geth节点只能同时启动一个HTTP端结点,因此不需要参数,其返回值为一个布尔值,表示端结点是否成功关闭。

> admin.stopRPC()

20、admin.startRPC

方法启动一个基于WebSocket的JSON RPC API服务来处理客户端的调用请求。所有的参数都是可选的:

  • host:要启动监听的网络地址,默认值:localhost
  • port:要启动监听的网络端口,默认值:8545
  • cors:要启用的跨源资源共享头:默认值:""
  • apis:要启用的API服务模块,默认值:"eth,net,web3"

方法返回一个布尔值来表征webSocket上的RPC监听是否启动成功。注意在任何时刻都只能启用一个Websocket端结点。

> admin.startWS(host, port, cors, apis)

本文示例:

> admin.startWS("127.0.0.1", 8546)
true
> 

admin.stopWS

方法用来关闭当前启动的WebSocket RPC端结点。由于一个 Geth节点同时只能启用一个Websocket RPC端结点,因此不需要参数,其返回值为一个表示是否成功关闭端结点的布尔值。

> admin.stopWS()

六、网络

21、net.listening

方法查询当前连接的节点,是否正在侦听网络连接与否。listen也可以理解为接收。

返回值true表示连接上的节点正在listen网络请求,否则返回false。

> net.listening

本文示例:

> net.listening
true
>

22、net.peerCount

方法返回连接节点已连上的其它以太坊节点的数量。

> net.peerCount

本文示例:

> net.peerCount
1
>

23、net.version

方法返回连接节点的网络号(networkid)。

> net.version

本文示例:

> net.version
"517"
>

七、区块操作

24、eth.coinbase

方法查询如果挖矿成功,获得奖励的地址。

> eth.coinbase

本文示例:

> eth.coinbase
"0x0297a8913cc187d60712f095a7e1bf2421bfcd40"
>

25、eth.defaultBlock

方法查询默认区块号。使用某些方法时,会使用默认块设置,比如:

  • web3.eth.getBalance()
  • web3.eth.getCode()
  • web3.eth.getTransactionCount()

可选的块参数,可能下述值中的一个:

  • Number - 区块号
  • String - earliest,创世块。
  • String - latest,最近刚出的最新块,当前的区块头。
  • String - pending,当前正在mine的区块,包含正在打包的交易。

默认值是latest。

> eth.defaultBlock

本文示例:

> eth.defaultBlock
"latest"
>

26、eth.mining

方法查询节点是否配置挖矿。

结合miner.start、miner.stop理解。

> eth.mining

本文示例:

> eth.mining
false
>

27、eth.hashrate

方法查询当前每秒的哈希难度。

> eth.hashrate

本文示例:

> eth.hashrate
0                                   //本文环境POA联盟链
>

28、eth.gasPrice

方法查询当前的gas价格。这个值由最近几个块的gas价格的中值决定。

> eth.gasPrice

本文示例:

> eth.gasPrice
18000000000
>

29、eth.accounts

方法查询当前节点持有的帐户列表。

> eth.accounts

本文示例:

> eth.accounts
["0x0297a8913cc187d60712f095a7e1bf2421bfcd40", "0x38d8b866a1abebca20afc004622f5355eefeb568", "0xda25997b15a6bea86116942b8ab69a5620d82284"]
>

30、eth.blockNumber

方法查询当前区块号。

> eth.blockNumber

本文示例:

> eth.blockNumber
75576
>

31、eth.getBalance

方法查询在指定区块时给定地址的余额。

参数:

  • String - 要查询余额的地址。
  • Number|String -(可选)如果不设置此值使用eth.defaultBlock设定的块,否则使用指定的块。

返回值:

String - 一个包含给定地址的当前余额的BigNumber实例,单位为wei

> eth.getBalance()

本文示例:

> eth.getBalance(eth.accounts[1])
9.99994769938e+23
> eth.getBalance("0x38d8b866a1abebca20afc004622f5355eefeb568")
9.99994769938e+23
>

32、eth.getBlock

方法查询块号或区块哈希值所对应的区块

参数:

  • Number|String -(可选)如果未传递参数,默认使用web3.eth.defaultBlock定义的块,否则使用指定区块。
  • Boolean -(可选)默认值为falsetrue会将区块包含的所有交易作为对象返回。否则只返回交易的哈希。

返回值 - 区块对象:

  • number - 区块号。当这个区块处于pending将会返回null
  • hash - 字符串,区块的哈希串。当这个区块处于pending将会返回null
  • parentHash - 字符串,32字节的父区块的哈希值。
  • nonce - 字符串,8字节。POW生成的哈希。当这个区块处于pending将会返回null
  • sha3Uncles - 字符串,32字节。叔区块的哈希值。
  • logsBloom - 字符串,区块日志的布隆过滤器。当这个区块处于pending将会返回null
  • transactionsRoot - 字符串,32字节,区块的交易前缀树的根。
  • stateRoot - 字符串,32字节。区块的最终状态前缀树的根。
  • miner - 字符串,20字节。这个区块获得奖励的矿工。
  • difficulty - BigNumber类型。当前块的难度,整数。
  • totalDifficulty - BigNumber类型。区块链到当前块的总难度,整数。
  • extraData - 字符串。当前块的extra data字段。
  • size - Number。当前这个块的字节大小。
  • gasLinitNumber,当前区块允许使用的最大gas
  • gasUsed - 当前区块累计使用的总的gas
  • timestamp - Number。区块打包时的unix时间戳。
  • transactions - 数组。交易对象。或者是32字节的交易哈希。
  • uncles - 数组。叔哈希的数组。
> eth.getBlock()

本文示例:

> eth.getBlock(26055)
{
  difficulty: 2,
  extraData: "0xd88301090e846765746888676f312e31342e32856c696e757800000000000000406752ea3dc4b96a57d9c80d463f69d57666af2da7b61fcf8fc757c28a0f656e67f97cfa7313572269ae6eee0b6528948c442648a617150793c15c6e539caec100",
  gasLimit: 8000000,
  gasUsed: 21000,
  hash: "0x3c3d9f151b52b860c855e2d2f72807f2ca84712ce5b4a050a8c5fd32e4cf6b5c",
  logsBloom: "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
  miner: "0x0000000000000000000000000000000000000000",
  mixHash: "0x0000000000000000000000000000000000000000000000000000000000000000",
  nonce: "0x0000000000000000",
  number: 26055,
  parentHash: "0xf7df22f18448f8f28ddf47ac277faf1f9ed5852194220ac11e6c885b9334fe7b",
  receiptsRoot: "0x056b23fbba480696b65fe5a59b8f2148a1299103c4f57df839233af2cf4ca2d2",
  sha3Uncles: "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
  size: 724,
  stateRoot: "0xe50d50ca15572a5779d3d4baa4609627516ea04abad97582c916e448c38e8b52",
  timestamp: 1589964128,
  totalDifficulty: 52061,
  transactions: ["0x0854f0450f4c7656c774f24f305a19c5a274f6ebe54779346c69ac9723a77065"],
  transactionsRoot: "0xca110f523fa15babf9ce8aa5319a1d1a5a0948372ef3debcf908a170abc23a30",
  uncles: []
}
>

33、eth.getBlockTransactionCount

方法查询指定区块的交易数量。

> eth.getBlockTransactionCount()

本文示例:

> eth.getBlockTransactionCount(26055)
1
>

34、eth.getTransaction

方法查询指定交易哈希值的交易。

参数:

  • String - 交易的哈希值。

返回值:

Object - 一个交易对象

  • hashString - 32字节,交易的哈希值。
  • nonceNumber - 交易的发起者在之前进行过的交易数量。
  • blockHashString - 32字节。交易所在区块的哈希值。当这个区块处于pending将会返回null
  • blockNumberNumber - 交易所在区块的块号。当这个区块处于pending将会返回null
  • transactionIndexNumber - 整数。交易在区块中的序号。当这个区块处于pending将会返回null
  • fromString - 20字节,交易发起者的地址。
  • toString - 20字节,交易接收者的地址。当这个区块处于pending将会返回null
  • valueBigNumber - 交易附带的货币量,单位为Wei
  • gasPriceBigNumber - 交易发起者配置的gas价格,单位是wei
  • gasNumber - 交易发起者提供的gas。.
  • inputString - 交易附带的数据。
> eth.getTransaction(transactionHash)

本文示例:

> eth.getTransaction("0x0854f0450f4c7656c774f24f305a19c5a274f6ebe54779346c69ac9723a77065")
{
  blockHash: "0x3c3d9f151b52b860c855e2d2f72807f2ca84712ce5b4a050a8c5fd32e4cf6b5c",
  blockNumber: 26055,
  from: "0x1e85d960a6fdcc5cb6deaf6567be527a1dd23602",
  gas: 21000,
  gasPrice: 1000000000,
  hash: "0x0854f0450f4c7656c774f24f305a19c5a274f6ebe54779346c69ac9723a77065",
  input: "0x",
  nonce: 0,
  r: "0x8cb775a7c65b3474e2c24f068f9f9dae65cbe438fed8ba64f52631e818a60abe",
  s: "0x43223efb967e43ebed824a0e11c641e6a515cc5fb6c2ac5355fecd2e434ebdbe",
  to: "0x6b7eeaa2fe6bd6c47fb6ca3835305301c5e759d6",
  transactionIndex: 0,
  v: "0x42d",
  value: 1e+24
}
>

35、eth.getTransactionFromBlock

方法返回指定区块的指定序号的交易。

参数:

  • String - 区块号或哈希。或者是earliestlatestpending。查看web3.eth.defaultBlock了解可选值。
  • Number - 交易的序号。

返回值:

Object - 交易对象,详见eth.getTransaction

> eth.getTransactionFromBlock(hashStringOrNumber, indexNumber)

本文示例:

> eth.getTransactionFromBlock(26055,1)
null
> eth.getTransactionFromBlock(26055,0)
{
  blockHash: "0x3c3d9f151b52b860c855e2d2f72807f2ca84712ce5b4a050a8c5fd32e4cf6b5c",
  blockNumber: 26055,
  from: "0x1e85d960a6fdcc5cb6deaf6567be527a1dd23602",
  gas: 21000,
  gasPrice: 1000000000,
  hash: "0x0854f0450f4c7656c774f24f305a19c5a274f6ebe54779346c69ac9723a77065",
  input: "0x",
  nonce: 0,
  r: "0x8cb775a7c65b3474e2c24f068f9f9dae65cbe438fed8ba64f52631e818a60abe",
  s: "0x43223efb967e43ebed824a0e11c641e6a515cc5fb6c2ac5355fecd2e434ebdbe",
  to: "0x6b7eeaa2fe6bd6c47fb6ca3835305301c5e759d6",
  transactionIndex: 0,
  v: "0x42d",
  value: 1e+24
}
>

36、eth.getTransactionReceipt

方法通过一个交易哈希,返回该交易的收据。

备注:处于pending状态的交易,收据是不可用的。

参数:

  • String - 交易的哈希。

返回值:

Object - 交易的收据对象,如果找不到返回null

  • blockHashString - 32字节,这个交易所在区块的哈希。
  • blockNumberNumber - 交易所在区块的块号。
  • transactionHashString - 32字节,交易的哈希值。
  • transactionIndexNumber - 交易在区块里面的序号,整数。
  • fromString - 20字节,交易发送者的地址。
  • toString - 20字节,交易接收者的地址。如果是一个合约创建的交易,返回null
  • cumulativeGasUsedNumber - 当前交易执行后累计花费的gas总值10
  • gasUsedNumber - 执行当前这个交易单独花费的gas
  • contractAddressString - 20字节,创建的合约地址。如果是一个合约创建交易,返回合约地址,其它情况返回null
  • logsArray - 这个交易产生的日志对象数组。
> eth.getTransactionReceipt(hashString)

示例:

> eth.getTransactionReceipt("0x0854f0450f4c7656c774f24f305a19c5a274f6ebe54779346c69ac9723a77065" )
{
  blockHash: "0x3c3d9f151b52b860c855e2d2f72807f2ca84712ce5b4a050a8c5fd32e4cf6b5c",
  blockNumber: 26055,
  contractAddress: null,
  cumulativeGasUsed: 21000,
  from: "0x1e85d960a6fdcc5cb6deaf6567be527a1dd23602",
  gasUsed: 21000,
  logs: [],
  logsBloom: "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
  status: "0x1",
  to: "0x6b7eeaa2fe6bd6c47fb6ca3835305301c5e759d6",
  transactionHash: "0x0854f0450f4c7656c774f24f305a19c5a274f6ebe54779346c69ac9723a77065",
  transactionIndex: 0
}
>

37、eth.getTransactionCount

方法查询并返回指定地址发起的交易数。

参数:

  • String - 要获得交易数的地址。
  • Number|String -(可选)如果未传递参数,默认使用web3.eth.defaultBlock定义的块,否则使用指定区块。

返回值:

Number - 指定地址发送的交易数量。

> eth.getTransactionCount(addressHexString)

示例:

> eth.getTransactionCount("0x1e85d960a6fdcc5cb6deaf6567be527a1dd23602")
3
>

38、eth.sendTransaction

方法发送一个交易到网络。

参数:

  • Object - 要发送的交易对象。
    • fromString - 指定的发送者的地址。如果不指定,使用web3.eth.defaultAccount
    • toString - (可选)交易消息的目标地址,如果是合约创建,则不填.
    • valueNumber|String|BigNumber - (可选)交易携带的货币量,以wei为单位。如果合约创建交易,则为初始的基金。
    • gasNumber|String|BigNumber - (可选)默认是自动,交易可使用的gas,未使用的gas会退回。
    • gasPriceNumber|String|BigNumber - (可选)默认是自动确定,交易的gas价格,默认是网络gas价格的平均值 。
    • dataString - (可选)或者包含相关数据的字节字符串,如果是合约创建,则是初始化要用到的代码。
    • nonceNumber - (可选)整数,使用此值,可以允许你覆盖你自己的相同nonce的,正在pending中的交易。

返回值:

String - 32字节的交易哈希串。用16进制表示。

如果交易是一个合约创建,请使用eth.getTransactionReceipt()在交易完成后获取合约的地址。

> eth.sendTransaction(transactionObject)

示例:

> eth.getBalance(eth.accounts[1])
9.99994769938e+23
> personal.unlockAccount(eth.accounts[1],"123456",600)
true
> eth.sendTransaction({from: eth.accounts[1], to: "0xda25997b15a6bea86116942b8ab69a5620d82284", value: 2000000000000000000})
"0x473207431f84734b0285c57b22691374675c17e9da68c88450a79c6bf3f9a3b2"
> eth.getBalance(eth.accounts[1])
9.9999276956e+23
> eth.getBalance("0xda25997b15a6bea86116942b8ab69a5620d82284")
7230000000000000000
> eth.sendTransaction({from: eth.accounts[1], to: "0xda25997b15a6bea86116942b8ab69a5620d82284", value: web3.toWei(1.23, "ether")})
"0xd626b80107f4d56342972163b8accd0db3ffc578acaba9df205f6397a6298882"
> eth.getBalance("0xda25997b15a6bea86116942b8ab69a5620d82284")
8460000000000000000
>

猜你喜欢

转载自blog.csdn.net/wonderBlock/article/details/106314759