Ubuntu18.04搭建自己的以太坊私有链(二)

接上一篇博客:Ubuntu18.04搭建自己的以太坊私有链(一)
  • 查询账户余额
> eth.getBalance(eth.accounts[0])  //通过eth.accounts[0]获取账户地址,由于是新建账户余额为0,需要通过挖矿获取
0
> eth.getBalance("0x99bcfb3127a4181ee5b1ac0a32ffa016cdc10829") // 直接给出账户地址
0
/* 
* 第三种方式查看账户余额,eth.accounts[index]会按传入的索引返回已有的账户,
* eth.getBalance(address)会返回账户的余额,余额是以wei为单位*/
> balance=web3.fromWei(eth.getBalance(eth.accounts[0]),"ether")
0

关于web3.fromWei(number, unit)函数可以看考博客:web3.fromWei

  • 查询矿工的账户地址,挖到一个区块会奖励5个以太币,挖矿所得的奖励会进入矿工的账户。这个账户叫做 coinbase,默认情况下 coinbase 是本地账户中的第一个账户。可以通过 miner.setEtherbase() 将其他账户设置成 coinbase。
//第一种查看矿工的账户地址的方式,该地址默认为第一个账户地址
> eth.coinbase     
"0x99bcfb3127a4181ee5b1ac0a32ffa016cdc10829"
//第二种查看方式
> web3.eth.coinbase
"0x99bcfb3127a4181ee5b1ac0a32ffa016cdc10829"
  • 设置矿工的账户地址,即奖励地址
//设置eth.accounts[1]为挖矿的账户地址
> miner.setEtherbase(eth.accounts[1])
true
//再次查询挖矿的账户地址显示为eth.accounts[1]的值
> eth.coinbase
"0x5036cb8a89d0cc7885be3cdd06e6c717ec1588f2"
  • 停止挖矿
    start 的参数表示挖矿使用的线程数,第一次启动挖矿会先生成挖矿所需的 DAG 文件,这个过程有点慢。等进度达到 100% 后,就会开始挖矿,此时屏幕会被挖矿信息刷屏。
> miner.start(1)
null
INFO [03-22|18:33:53.215] Updated mining threads                   threads=1
INFO [03-22|18:33:53.215] Transaction pool price threshold updated price=1000000000
INFO [03-22|18:33:53.216] Commit new mining work                   number=1 sealhash=3a4356…8da5a0 uncles=0 txs=0 gas=0 fees=0 elapsed=277.76µs
INFO [03-22|18:33:57.742] Generating DAG in progress               epoch=1 percentage=0 elapsed=2.984s
INFO [03-22|18:34:00.743] Generating DAG in progress               epoch=1 percentage=1 elapsed=5.985s
INFO [03-22|18:34:03.831] Generating DAG in progress               epoch=1 percentage=2 elapsed=9.073s
...
INFO [03-22|18:42:08.894] Successfully sealed new block            number=1 sealhash=3a4356…8da5a0 hash=480f76…d5bc56 elapsed=8m15.678s
INFO [03-22|18:42:08.902] ? mined potential block                  number=1 hash=480f76…d5bc56
INFO [03-22|18:42:08.928] Commit new mining work                   number=2 sealhash=323253642275 uncles=0 txs=0 gas=0 fees=0 elapsed=25.535ms
...

出现好多?,表明挖到了很多矿,这是可以停止挖矿,然后查看矿工账户的余额。

  • 开始挖矿
> miner.stop()
null
// 1 ether = 1e18 wei,eth.getBalance()默认返回的单位是wei
> eth.getBalance(eth.accounts[0])  
205000000000000000000
//以ether作为单位,返回账户余额,与上面的查询结果相对应
> web3.fromWei(eth.getBalance(eth.accounts[0]),"ether")
205
// 非矿工账户目前余额仍为0
> web3.fromWei(web3.eth.getBalance(web3.eth.accounts[1]),"ether")
0

参考链接:
以太坊中以太币及代币计量单位

  • 账户转账
    我们打算从eth.accounts[0]eth.accounts[1]转账,转账之前需要先解锁eth.accounts[0]
//三个参数分别为accounts[0]的地址,创建时的密码及解锁时间(秒为单位),0 代表长时间解锁。
> personal.unlockAccount(eth.accounts[0],"123456",0)
true

开始转账:

> eth.sendTransaction({from:eth.accounts[0],to:eth.accounts[1],value:web3.toWei(4,'ether')})
INFO [03-22|19:24:38.553] Setting new local account                address=0x99BCfb3127A4181Ee5b1Ac0a32FFa016cdc10829
INFO [03-22|19:24:38.553] Submitted transaction                    fullhash=0x739c28eb31edd02579e260e1872b4ca54c647a405a32ceeeafbe6f5a6b1ca939 recipient=0x5036cb8A89d0cc7885Be3cdD06e6c717ec1588f2
"0x739c28eb31edd02579e260e1872b4ca54c647a405a32ceeeafbe6f5a6b1ca939"
/* 
 * 查询eth.accounts[1]的余额,却发现余额还是0
 * 因为虽然我们发起了交易,但并没有矿工挖矿打包交易。
 * 若再次执行miner.start(),然后查询eth.accounts[1]的余额,就会发现节点2的coinbase地址已经有金额了。
 * */
> web3.fromWei(eth.getBalance(eth.accounts[1]),"ether")
0

查看等待被打包的交易:

//查看交易池中等待被打包的交易
> txpool.status
{
  pending: 1,
  queued: 0
}
//查看已提交但还未被处理的交易,pending表示已提交但还未被处理的交易
> txpool.inspect.pending
{
  0x99BCfb3127A4181Ee5b1Ac0a32FFa016cdc10829: {
    1: "0x5036cb8A89d0cc7885Be3cdD06e6c717ec1588f2: 4000000000000000000 wei + 90000 gas × 1000000000 wei"
  }
}
//查看当前待确认交易
> eth.getBlock("pending", true).transactions
[{
    blockHash: "0x050b329f3971964745f28d8523932a5a75a39fb19fd301ff47b7cc8a5b839a23",
    blockNumber: 42,
    from: "0x99bcfb3127a4181ee5b1ac0a32ffa016cdc10829",
    gas: 90000,
    gasPrice: 1000000000,
    hash: "0x739c28eb31edd02579e260e1872b4ca54c647a405a32ceeeafbe6f5a6b1ca939",
    input: "0x",
    nonce: 0,
    r: "0x3b76985f44b038098dc7d4f013c26cd76f7f1207ee211cf27ff4f9abef2a6074",
    s: "0x39bfd24bb83c64fc5547e9a4fdad728437977d75cb320a5162dbef0ec3c36ac1",
    to: "0x5036cb8a89d0cc7885be3cdd06e6c717ec1588f2",
    transactionIndex: 0,
    v: "0x1f0",
    value: 4000000000000000000
}]

再次挖矿后,查看相关信息:

//查看交易池中的交易,发现没有等待被打包的交易
> txpool.status
{
  pending: 0,
  queued: 0
}
//查看已提交但还未被处理的交易,目前没有已提交但还未被处理的交易
> txpool.inspect.pending
{}

//查看当前待确认交易,发现没有带确认的交易
> eth.getBlock("pending", true).transactions
[]
//查询eth.accounts[1]中账户余额,发现eth.accounts[0]转给它的4个ether已经到账
> web3.fromWei(eth.getBalance(eth.accounts[1]),"ether")
4
//查询eth.accounts[0]中账户余额,发现eth.accounts[0]的账户余额=58 * 5- 4
> web3.fromWei(eth.getBalance(eth.accounts[0]),"ether")
286
  • 查看交易和区块

① 查看当前区块总数

> eth.blockNumber  
58

② 根据刚刚执行eth.sendTransaction()返回的交易hash值,查询发起转账交易时的详情

> eth.getTransaction("0x739c28eb31edd02579e260e1872b4ca54c647a405a32ceeeafbe6f5a6b1ca939")
{
  blockHash: "0x29479a4dbbdab6dfa97f03ca9fd47104202c63bd675c59276a59d1b98ee565b8",
  blockNumber: 42, //交易所在区块的块号
  from: "0x99bcfb3127a4181ee5b1ac0a32ffa016cdc10829",
  gas: 90000,
  gasPrice: 1000000000,
  hash: "0x739c28eb31edd02579e260e1872b4ca54c647a405a32ceeeafbe6f5a6b1ca939",
  input: "0x", //交易附带的数据
  nonce: 0, //交易发起者在之前发起过的交易
  r: "0x3b76985f44b038098dc7d4f013c26cd76f7f1207ee211cf27ff4f9abef2a6074",
  s: "0x39bfd24bb83c64fc5547e9a4fdad728437977d75cb320a5162dbef0ec3c36ac1",
  to: "0x5036cb8a89d0cc7885be3cdd06e6c717ec1588f2",
  transactionIndex: 0,//交易在区块中的序号,区块处于pending返回null
  v: "0x1f0",
  value: 4000000000000000000
}

③ 根据刚刚执行eth.sendTransaction()返回的交易hash值,返回一个交易的收据。需要指出的是,处于pending状态的交易,收据是不可用的。

> eth.getTransactionReceipt("0x739c28eb31edd02579e260e1872b4ca54c647a405a32ceeeafbe6f5a6b1ca939")
{
  blockHash: "0x29479a4dbbdab6dfa97f03ca9fd47104202c63bd675c59276a59d1b98ee565b8",
  blockNumber: 42,
  contractAddress: null,//创建的合约地址,如果是一个合约创建交易,则返回合约地址,其他情况返回null
  cumulativeGasUsed: 21000,//当前交易执行后累积花费的gas总值
  from: "0x99bcfb3127a4181ee5b1ac0a32ffa016cdc10829",//交易发起者的地址
  gasUsed: 21000,//执行当前这个交易单独花费的gas
  logs: [],//这个交易产生的日志对象数组
  ...
  root: "0x7e65064dd6f8f5b0034aabe7e91da156a5db7d8702412dd21969d39476d7f815",//交易执行后的stateroot
  to: "0x5036cb8a89d0cc7885be3cdd06e6c717ec1588f2",//交易接收者的地址
  transactionHash: "0x739c28eb31edd02579e260e1872b4ca54c647a405a32ceeeafbe6f5a6b1ca939",
  transactionIndex: 0
}

④ 查询最新区块

> eth.getBlock('latest')
{
  difficulty: 131264,
  extraData: "0xd883010817846765746888676f312e31302e34856c696e7578",
  gasLimit: 4042516952,
  gasUsed: 0,
  hash: "0xf2b68bb76975981601e63fb2a1619a27a1b2e5efcf20c5790a3879cad948e201",
  ...
  miner: "0x99bcfb3127a4181ee5b1ac0a32ffa016cdc10829",
  mixHash: "0x117b32af5ebfa7959695a6620b193ee7d19d4ac1247202b3488afff65ae8b44b",
  nonce: "0x156b9513204d4e91",
  number: 62,
  parentHash: "0x1305bf4ba23e01fc572f40b49ece9058e3bcef057149a32fd32370b0da2963c2",
  receiptsRoot: "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
  sha3Uncles: "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
  size: 537,
  stateRoot: "0xe26c87278d88dee8e1cf318c9d532afa299abc30ac172289b1360b76425eb2a0",
  timestamp: 1553256136,
  totalDifficulty: 8313104,
  transactions: [],
  transactionsRoot: "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
  uncles: []
}

⑤ 根据区块Number或Hash查询区块

//通过Number查询创世区块的信息,发现跟我们创建创世区块时,编写的genesis.json文件一致
> eth.getBlock(0)
{
  difficulty: 131072,
  extraData: "0x1234567890",
  gasLimit: 4294967295,
  gasUsed: 0,
  hash: "0x6c63bbf647549be8c653071e7e8c881edb10c8ec2729f3faa972f50741074e7d",
  ...
  miner: "0x0000000000000000000000000000000000000330",
  mixHash: "0x0000000000000000000000000000000000000000000000000000000000000000",
  nonce: "0x0000000000000024",
  number: 0,
  parentHash: "0x0000000000000000000000000000000000000000000000000000000000000000",
  receiptsRoot: "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
  sha3Uncles: "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
  size: 513,
  stateRoot: "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
  timestamp: 0,
  totalDifficulty: 131072,
  transactions: [],
  transactionsRoot: "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
  uncles: []
}
//通过hash查询第二个区块的信息
> eth.getBlock("0x480f76636503c119b552a6ad35d4719738c1ec361b8fd425b74ab4ff0ed5bc56")
{
  difficulty: 131072,
  extraData: "0xd883010817846765746888676f312e31302e34856c696e7578",
  gasLimit: 4290772993,
  gasUsed: 0,
  hash: "0x480f76636503c119b552a6ad35d4719738c1ec361b8fd425b74ab4ff0ed5bc56",
  ...
  miner: "0x99bcfb3127a4181ee5b1ac0a32ffa016cdc10829",
  mixHash: "0xd98a1ebd17ddff9e194aad3c6c48376eabf1be18d7e20d2f50959936621d400c",
  nonce: "0x08be67b5e6c5f3cd",
  number: 1,
  //parentHash的值与创世区块的hash值一致
  parentHash: "0x6c63bbf647549be8c653071e7e8c881edb10
HTTP请求返回415错误码定位解决 10-29
整理:几款好用的Markdown编辑器 05-29
pycharm 教程(一)安装和首次使用 07-18
Eclipse 安装 SVN 插件的两种方法 06-06
visio2013激活软件,绝对靠谱!!! 10-19
Pycharm简单使用教程 03-02
最新 Axure8.1 注册码 绝对可用 更新至2018.03.10 03-12
Linux查看日志命令 05-08
python模块以及导入出现ImportError: No module named 'xxx'... 02-23
机器学习入门好文,强烈推荐 02-01
git命令行删除远程分支 11-01
教你如何查看Ubuntu版本 09-13
MySQL安装问题--Can't connect to MySQL server on localhos... 12-28
Intellij_IDEA的激活(使用破解补丁永久激活)(已更新) 03-18
java生成6位随机数的5种方法 08-07
手把手搭建一个完整的javaweb项目(适合新手) 06-23
VMware Ubuntu安装详细过程(非常靠谱) 11-21
OutOfMemoryError系列(2: GC overhead limit exceeded 08-25
VMware12_centos6无法联网 05-22
php_微信新增永久素材 01-27
手把手教你整合最优雅SSM框架:SpringMVC_+_Spring_+_MyBati... 06-19

使用IntelliJ IDEA 配置Maven(入门) 05-20
教你写Makefile(很全,含有工作经验的) 03-17
从Linux服务器下载文件夹到本地 01-11
Sci-Hub_可用网址 05-17
MAC_office2016_安装及激活 08-16
python网络编程——IO多路复用之epoll 06-19
mysql中insert_into语句的6种写法(上) 09-04
原
web3.eth.getTransactionReceipt
2018042513:43:19 新缸中之脑 阅读数:1460
 版权声明:本文为博主原创文章,未经博主允许不得转载。	https://blog.csdn.net/shebao3333/article/details/80078088
如果你希望马上开始学习以太坊DApp开发,可以访问汇智网提供的出色的在线互动教程:

以太坊DApp实战入门教程
以太坊去中心化电商应用开发实战
c8ec2729f3faa972f50741074e7d",
  receiptsRoot: "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
  sha3Uncles: "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
  size: 537,
  stateRoot: "0x251a3bcea2580b7980e947d71dae54c1eab8ba13c1f6656f2de75db656815254",
  timestamp: 1553250833,
  totalDifficulty: 262144,
  transactions: [],
  transactionsRoot: "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
  uncles: []
}

参考链接:
ubuntu16.04安装以太坊并运行-——参考的最多的博客
搭建以太坊私有链——常用命名有总结
Ubuntu16.04 搭建以太坊Ethereum私链——命令解释很详细
以太坊如何搭建私有连联盟链
以太坊的账户和基本单位
以太坊开发文档06 - 以太坊基本命令操作
ubuntu16.04搭建以太坊私有链并在私有链中进行挖矿及转账

你可能感兴趣的:(区块链)