区块链学习记录(4)

以太坊私有链部署及相关操作

私有链创建

搭建本地局域网:

1
/bootdir$ sudo bootnode --nodekey boot.key

部署boot1:

1
/boot1$ sudo geth --datadir ./ --port 30321 --networkid 666 --rpc --rpcaddr 0.0.0.0 --rpcapi net,web3,eth,personal --rpcport 8110 --bootnodes "enode://ebc35b6647c7c089686382e76fdc87346417ba66f8415e13c5ecd764cab68d57f5fadb40415758265c3e315f4ff5a2714fb42246e7cc7c365dd3e32f11963198@127.0.0.1:0?discport=30301"  --unlock "0xF900C29151d9cCa8D2E21882d7B99A7114De21Be" --allow-insecure-unlock --syncmode "full" console

部署boot2:

1
/boot2$ sudo geth --datadir ./ --port 30322 --networkid 666 --rpc --rpcaddr 0.0.0.0 --rpcapi net,web3,eth,personal --rpcport 8111 --bootnodes "enode://ebc35b6647c7c089686382e76fdc87346417ba66f8415e13c5ecd764cab68d57f5fadb40415758265c3e315f4ff5a2714fb42246e7cc7c365dd3e32f11963198@127.0.0.1:0?discport=30301"  --unlock "0x0f82E6Bef49656796C4dFfdb0736a4EB3c2952B8" --allow-insecure-unlock --syncmode "full" console

账户操作

账户查询:

1
2
> eth.accounts
["0xf900c29151d9cca8d2e21882d7b99a7114de21be"]

此账户余额无穷,负责给本节点的其他账户转账

创建账户:

1
2
> personal.newAccount("123456")
"0x8d8350043d1c92481a109e38f7dcaf60c5a20344" //123456为账户密码,输出为账户名

查询账户余额:

1
2
3
4
> eth.getBalance( "0x8d8350043d1c92481a109e38f7dcaf60c5a20344")
0
> eth.getBalance("0xf900c29151d9cca8d2e21882d7b99a7114de21be")
9.04625697166532776746648320380374280103671755200316906555262396061821325312e+74

私有链挖矿

boot1启动挖矿:

1
> miner.start(1)		//不加数字默认8个线程

此时会提醒”INFO [10-12|06:25:25.002] Signed recently, must wait for others”,需要在boot2中同时挖矿

结束挖矿:

1
> miner.stop()

查询总区块数:

1
> eth.blockNumber

转账交易

转账1:boot1内交易

boot1终端

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
> n1user1 = eth.accounts[0]
"0xf900c29151d9cca8d2e21882d7b99a7114de21be"
> n1user2 = eth.accounts[1]
"0x8d8350043d1c92481a109e38f7dcaf60c5a20344"
> personal.unlockAccount(user1,'123')
true
> personal.unlockAccount(user2,'123456') //123456为该账户密码,账户解封
true
> eth.sendTransaction({from:n1user1,to:n1user2,value:web3.toWei(3,'ether')}) //从n1user1向n1user2转3个以太币
address=0xF900C29151d9cCa8D2E21882d7B99A7114De21Be //地址
fullhash=0xc2046d7119b05aaacbc93ebaf47f6d7440723980a4b9aa52f87144babf8311c9 //hash
recipient=0x8D8350043d1C92481a109E38f7DcaF60c5A20344
"0xc2046d7119b05aaacbc93ebaf47f6d7440723980a4b9aa52f87144babf8311c9" //交易查询值
```
这个时候转账已经完成,但是如果查询账户2中的以太币会显示账户余额为0:
```bash
> eth.getBalance(n1user2)
0

此时需要开启挖矿,通过矿工将交易写入区块中:

1
> miner.start()

然后终止:

1
> miner.stop()

重新查询账户2中的余额

1
2
> eth.getBalance(n1ues2)
3000000000000000000

转账2:boot1向boot2转账

boot2终端

1
2
3
4
5
6
> personal.newAccount("654321")
"0x2e55bac981f933ade1ee44943bcbb496df81d80f"
> n2user2 = "0x2e55bac981f933ade1ee44943bcbb496df81d80f"
"0x2e55bac981f933ade1ee44943bcbb496df81d80f"
> personal.unlockAccount(n2user2,'654321') //654321为该账户密码,账户解封
true

boot1终端

1
2
3
4
5
> eth.sendTransaction({from:"0x8d8350043d1c92481a109e38f7dcaf60c5a20344",to:"0x2e55bac981f933ade1ee44943bcbb496df81d80f",value:web3.toWei(3,'ether')})
address=0x8D8350043d1C92481a109E38f7DcaF60c5A20344
fullhash=0x9a029086c70efef2f9fce536139a1806a576e182fb2219b777df796172c2355b
recipient=0x2E55bAC981f933adE1eE44943BCbb496df81D80F
"0x9a029086c70efef2f9fce536139a1806a576e182fb2219b777df796172c2355b"

查询交易

转账1查询:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
> eth.getTransaction("0xc2046d7119b05aaacbc93ebaf47f6d7440723980a4b9aa52f87144babf8311c9")
{
blockHash: null,
blockNumber: null,
from: "0xf900c29151d9cca8d2e21882d7b99a7114de21be",
gas: 21000,
gasPrice: 1000000000,
hash: "0xc2046d7119b05aaacbc93ebaf47f6d7440723980a4b9aa52f87144babf8311c9",
input: "0x",
nonce: 1,
r: "0xbfea7012a05f001f03eb1109eed2d184d98026816132a8796e48ccd8cbe11f58",
s: "0x19f62a00502407634d84ad541a7ccb44ba19d2a238e0ab0e104c5980210ab2a3",
to: "0x8d8350043d1c92481a109e38f7dcaf60c5a20344",
transactionIndex: null,
v: "0x557",
value: 3000000000000000000
}

节点连接

1
2
> net.peerCount
1