在CENTOS7上入门Ethereum区块链(2):以太坊私有链环境下的账户管理、挖矿与转账

以太坊(1):在CentOS 6.5上搭建以太坊私有链的步骤 中我们搭建了以太坊的私有链,这次我们将在该私有链环境中进行创建账户,挖矿和转账的操作。效果如图

参考addr:点击

首先,回顾一下搭建的过程中,对我们这一讲比较重要的环节。

我们在搭建的最后一个步骤中 使用了./geth --rpc --rpccorsdomain "*" --datadir "/app/chain" --port "30303" --rpcapi "db,eth,net,web3" --networkid 100000 console 这个命令进行了以太坊的启动,并最终进入了命令行的模式;如果终端由于某种原因关闭,以太坊的进程也就自动消失了,我们可以再次运行以上命令,再次启动以太坊并再次进行命令行的模式,本讲中下面所有的命令,如果没有特别说明,都在该命令行模式下运行。在启动命令中,我们使用了--datadir "/app/chain" 这样一个参数,我们暂且称/app/chain 这个目录为数据目录。

下面开始正式的内容:

1. 账户的查看

以太坊启动后,数据目录(我们实例中是 /app/chain)的结构如下:

[root@ZC_VM_10_100_142_62 bin]# ll /app/chain/
总用量 16
drwxr-xr-x 2 root root 4096 9月  30 14:11 chaindata
drwxr-xr-x 2 root root 4096 9月  30 14:11 dapp
-rw------- 1 root root    0 9月  30 14:11 history
-rw------- 1 root root   64 9月  30 14:11 nodekey
drwxr-xr-x 2 root root 4096 9月  30 14:11 nodes

并不存在一个叫 keystore的子目录,下面我们在以太坊的终端使用命令 personal.listAccounts 列出现存的账号:

instance: Geth/v1.4.12-stable/linux/go1.5.1
 modules: admin: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

扫描二维码关注公众号,回复: 3895870 查看本文章

> personal.listAccounts
[]
>

返回一个空数组,说明目前没有账号。我们使用personal.newAccount()这个命令创建一个账号:

> personal.newAccount()
Passphrase:
Repeat passphrase:
"0xa7c417243694fb3a880629879dfe4b71f7cffd3f"
>

两次输入密码(我们这里输入的密码是123456,后面会用到这个密码),最终给我们返回了账号,此时我们再看数据目录的结构:

[root@ZC_VM_10_100_142_62 chain]# ll
总用量 20
drwxr-xr-x 2 root root 4096 9月  30 14:15 chaindata
drwxr-xr-x 2 root root 4096 9月  30 14:15 dapp
srw------- 1 root root    0 9月  30 14:16 geth.ipc
-rw------- 1 root root    0 9月  30 14:11 history
drwx------ 2 root root 4096 9月  30 14:19 keystore
-rw------- 1 root root   64 9月  30 14:11 nodekey
drwxr-xr-x 2 root root 4096 9月  30 14:16 nodes

与上一次查看该数据目录比较,多出了一个叫做keystore的子目录。

2. 挖矿

账号创建成功后,我们就可以挖矿了。其实我们在第一步中创建的第一个账号是默认账号,我们这里是 0xa7c417243694fb3a880629879dfe4b71f7cffd3f,我们使用web3.fromWei(eth.getBalance(eth.coinbase), "ether")来查询这个默认账号余额:

> web3.fromWei(eth.getBalance(eth.coinbase), "ether")
0

当前默认账号的余额是0。

下面我们使用miner.start(1)命令开始挖矿:

> miner.start(1)
I0930 14:33:45.649533 miner/miner.go:119] Starting mining operation (CPU=1 TOT=2)
I0930 14:33:45.650048 miner/worker.go:573] commit new work on block 1 with 0 txs & 0 uncles. Took 453.037µs
I0930 14:33:45.650175 ethash.go:259] Generating DAG for epoch 0 (size 1073739904) (0000000000000000000000000000000000000000000000000000000000000000)
true
> I0930 14:33:45.652702 eth/backend.go:454] Automatic pregeneration of ethash DAG ON (ethash dir: /root/.ethash)
I0930 14:33:45.652816 eth/backend.go:461] checking DAG (ethash dir: /root/.ethash)
I0930 14:33:46.661325 ethash.go:276] Done generating DAG for epoch 0, it took 1.011158481s
I0930 14:33:50.613543 miner/worker.go:339]  Mined block (#1 / ca8611de). Wait 5 blocks for confirmation
I0930 14:33:50.613968 miner/worker.go:573] commit new work on block 2 with 0 txs & 0 uncles. Took 389.891µs
I0930 14:33:50.614405 miner/worker.go:573] commit new work on block 2 with 0 txs & 0 uncles. Took 102.337µs

当出现“Mined block”这样的字眼时,表明挖矿成功,此时,我们可以使用miner.stop()命令停止挖矿,注意,此时由于挖矿正在进行而不断打出日志,屏幕会不断滚动,我们无需理会,继续输入即可,但是不要输错。

然后,我们再次使用web3.fromWei(eth.getBalance(eth.coinbase), "ether")来查询这个默认账号余额:

> web3.fromWei(eth.getBalance(eth.coinbase), "ether")
45

45即是我们挖矿得到的奖励。

3. 转账

挖矿得到的奖励(在以太坊中称之为以太币,以后我们统一使用以太币这个名词)可以转给其他账号,为了完成转账,我们首先需要在本地创建另外一个账号作为转账的接收方:

> personal.newAccount()
Passphrase:
Repeat passphrase:
"0x0ee35a76b6dbc9fcb44afbbf92362b6653ebcd1a"

这里我们输入的密码是234567。

我们使用 eth.sendTransaction({from: '0xa7c417243694fb3a880629879dfe4b71f7cffd3f', to: '0x0ee35a76b6dbc9fcb44afbbf92362b6653ebcd1a', value: web3.toWei(1, "ether")})来从我们的默认账户转移1个以太币给我们新创建的账号:

> eth.sendTransaction({from: '0xa7c417243694fb3a880629879dfe4b71f7cffd3f', to: '0x0ee35a76b6dbc9fcb44afbbf92362b6653ebcd1a', value: web3.toWei(1, "ether")})
account is locked
    at web3.js:3119:20
    at web3.js:6023:15
    at web3.js:4995:36
    at :1:1

结果提示我们,账号被锁定,这里被锁定的是转出的账号,所以我们首先使用 personal.unlockAccount("0xa7c417243694fb3a880629879dfe4b71f7cffd3f", "123456", 300)来解锁我们的转出账户,并再次进行转账:

> personal.unlockAccount("0xa7c417243694fb3a880629879dfe4b71f7cffd3f", "123456", 300)
true
> eth.sendTransaction({from: '0xa7c417243694fb3a880629879dfe4b71f7cffd3f', to: '0x0ee35a76b6dbc9fcb44afbbf92362b6653ebcd1a', value: web3.toWei(1, "ether")})
I0930 14:55:08.645509 eth/api.go:1193] Tx(0x4bb6b79dbc8090d0edc3a291760f3fa5dad6e54f7ae61e5051a0f9d5e6403fa5) to: 0x0ee35a76b6dbc9fcb44afbbf92362b6653ebcd1a
"0x4bb6b79dbc8090d0edc3a291760f3fa5dad6e54f7ae61e5051a0f9d5e6403fa5"

我们查看这两个账户的余额:

> web3.fromWei(eth.getBalance(eth.coinbase), "ether")
45
> web3.fromWei(eth.getBalance(eth.accounts[1]),"ether")
0

可见,转出账户余额并未减少,转入账户也没有增加,因为我们的交易还未成功写进区块,写进区块的方式是挖矿,我们再次开启挖矿,挖矿成功后,并停止挖矿:

> miner.start(1)
I0930 15:00:16.587705 miner/miner.go:119] Starting mining operation (CPU=1 TOT=2)
I0930 15:00:16.594106 miner/worker.go:573] commit new work on block 10 with 1 txs & 0 uncles. Took 6.331868ms
true
> miner.stop(I0930 15:00:22.725363 miner/worker.go:339] Mined block (#10 / 9a7ef215). Wait 5 blocks for confirmation

。。。。。

。。。。。

I0930 15:00:26.219075 miner/worker.go:339] Mined stale block (#14 / 240ad5b5).
I0930 15:00:26.219203 miner/worker.go:573] commit new work on block 15 with 0 txs & 0 uncles. Took 108.818µs
miner.stop()
true

再次查看余额:

> web3.fromWei(eth.getBalance(eth.coinbase), "ether")
69
> web3.fromWei(eth.getBalance(eth.accounts[1]),"ether")
1

此时,可以看出转入账户,增加了1个比特币。而转出账户,由于再次挖矿而获得奖励,从而余额增加,但增加的幅度是每次增加5个,这里余额是69,说明确实从这里转出了1个以太币。

到此为止,本节的内容就完成了。下一次,我们会演示一个简单的智能合约。

猜你喜欢

转载自blog.csdn.net/qq_32447321/article/details/81740557