以太坊(一)私有链搭建

智能合约:就是运行在以太坊这个”全球计算机”上的【进程】”,之所以有上述比喻,是因为以太坊网络的节点遍布全球,在这样的网络中运行计算就相当于在”【一台全球计算机】”中运行计算;

智能:智能合约是可以【自动运行的】

合约:以太坊的合约代码多会涉及一些【资产转移】,而现实世界中签订合同也多是伴随着【资产转移】,因此把这样的代码叫合约;

下面介绍利用【Geth】客户端搭建属于自己的区块链开发环境,通常被称为私有链;

1 macOS安装以太坊客户端Geth

1
2
3
4
5
6
# 安装稳定版本
brew tap ethereum/ethereum
brew install ethereum

# 安装开发版
brew install ethereum --devel

2 查看Geth安装版本

1
2
3
4
5
6
7
8
9
10
# 本机安装的Geth版本是:v1.11.6
geth version

Geth
Version: 1.11.6-stable
Git Commit: ea9e62ca3db5c33aa7438ebf39c189afd53c6bf8
Git Commit Date: 20230420
Architecture: amd64
Go Version: go1.20.3
Operating System: darwin

3 创建创世区块配置文件

1
genesis.json
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
{
"config": {
"chainId": 88888,
"homesteadBlock": 0,
"eip150Block": 0,
"eip155Block": 0,
"eip158Block": 0
},
"alloc" : {},
"coinbase" : "0x0000000000000000000000000000000000000000",
"difficulty" : "0x2",
"extraData" : "",
"gasLimit" : "0xffffffff",
"nonce" : "0x0000000000000042",
"mixhash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
"parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
"timestamp" : "0x00"
}

4 配置文件参数说明

1
2
3
4
5
6
7
8
9

coinbase: 旷工的账号,可任意填写 0x0000000000000000000000000000000000000000
difficulty: 设置当前区块的难度,开发环境建议设置较小的难度值 0x2
extraData: 附加信息,可任意填写 ""
gasLimit: 设置Gas消耗总量的限制,限制区块链能包含的交易信息总和,开发环境建议设置最大值 0xffffffff
nonce: 64位的随机数,用于挖矿 0x0000000000000042
mixhash: 由上个区块一部分生成的哈希值,与 nonce 配合用于挖坑 0x0000000000000000000000000000000000000000000000000000000000000000
parentHash: 上个区块的哈希值,因为是创世区块,这个值填0 0x0000000000000000000000000000000000000000000000000000000000000000
timestamp: 创世区块的时间戳 0x00

5 使用配置初始化区块链

1
2
3
4
5
6
7
8
9
10
cd private-chain

tree -L 1 ./
├── genesis.json
├── geth.log
├── node_data
└── geth_run.sh

# 初始化区块链
geth --datadir ./node_data init genesis.json

6 启动私有节点

1
2
3
4
5
6
7
8
9
#!/bin/bash
geth --datadir ./node_data \
--networkid 88888 \
--http --http.addr 0.0.0.0 \
--http.vhosts "*" \
--http.api "db,net,eth,web3,personal,debug" \
--http.corsdomain "*" \
--rpc.enabledeprecatedpersonal \
--snapshot --allow-insecure-unlock console 2>> geth.log
1
2
3
4
5
6
7
8
9
10
11
./geth_run.sh

Welcome to the Geth JavaScript console!

instance: Geth/v1.11.6-stable-ea9e62ca/darwin-amd64/go1.20.3
at block: 0 (Thu Jan 01 1970 08:00:00 GMT+0800 (CST))
datadir: /blockchain/private-chain/node_data
modules: admin:1.0 debug:1.0 engine:1.0 eth:1.0 ethash:1.0 miner:1.0 net:1.0 rpc:1.0 txpool:1.0 web3:1.0

To exit, press ctrl-d or type exit
>

7 Geth的JavaScript常用操作

7.1 创建账号
1
2
3
4
5
6
7
8
9
# 查看所有账号列表
> eth.accounts

# 创建一个账户
> personal.newAccount()

Passphrase:
Repeat passphrase:
"0xed563e9defde5e2a9b5ae2ae6f167c4b7672b32f"
1
2
3
4
5
遇到的问题:
ReferenceError: personal is not defined
at <eval>:1:1(0)

启动节点的时候加启动参数:--rpc.enabledeprecatedpersonal
7.2 查看账号余额
1
2
3
4
5
# 查看账号余额
> eth.getBalance("0xed563e9defde5e2a9b5ae2ae6f167c4b7672b32f")
0
> eth.getBalance(eth.accounts[0])
0
7.3 启动和停止挖矿
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
> miner.setEtherbase(eth.accounts[0])
true

# 启动挖矿
> miner.start(1)

# 停止挖矿
> miner.stop()

# 查看挖到的以太币,单位转换成ETH
> web3.fromWei(eth.getBalance(eth.accounts[0]),'ether')
45000000000000000000

# ETH转Wei
> web3.toWei(5,'ether')
"5000000000000000000"
7.4 发送交易
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# 创建一个新账户,用于测试交易
> personal.newAccount()
Passphrase:
Repeat passphrase:
"0xcd4fb34be454508f4183286673f9137caa6429a8"

# 解锁账户,如果被锁住的话
> personal.unlockAccount("0xcd4fb34be454508f4183286673f9137caa6429a8")
Unlock account 0xcd4fb34be454508f4183286673f9137caa6429a8
Passphrase:
true

# 发送交易
> eth.sendTransaction({from:eth.accounts[0],to:"0xcd4fb34be454508f4183286673f9137caa6429a8",value:5000000000000000000})
"0x59721b01c3508d160f4ab7939d656a9cac3f019d1e59822d3316a3a570c36ce5"

# 查看交易状态
> txpool.status
{
pending: 1,
queued: 0
}

# 要使交易被处理必须要挖矿
> miner.setEtherbase(eth.accounts[0])
true
> miner.start(1);admin.sleepBlocks(1);miner.stop();
null

# 再次查看交易状态
> txpool.status
{
pending: 0,
queued: 0
}
7.5 查看区块和交易
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# 查看当前区块总数
> eth.blockNumber
11

# 通过区块的高度查看区块信息
> eth.getBlock(10)
{
difficulty: 131072,
extraData: "0xd983010b06846765746888676f312e32302e338664617277696e",
gasLimit: 4253208113,
gasUsed: 21000,
hash: "0xffa6fbb05108a418ef1338e9aa302b2a35098ebe182a6dcdf602d41d5dcbdc0e",
logsBloom: "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
miner: "0x075e399630896d443f1d61ca5398209497dbbde3",
mixHash: "0xb50716b2cee9dfde25055337c9796bc0364861cb609a5aa2909ff26855cc19ff",
nonce: "0x34282181d0cff31d",
number: 10,
parentHash: "0x73a0168d3f1f0c4ed77ae9d95747657fefe795500bce33041c919e70a2bdc931",
receiptsRoot: "0xf1a563d34902071f84087715cf7f863d133438bfe2e7bc2045c3214bf9a87fe0",
sha3Uncles: "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
size: 652,
stateRoot: "0x2f236845deccf8a3abf063fa849dc609c65e3334fc6cc3d9df27624a73a570c1",
timestamp: 1716535481,
totalDifficulty: 1313026,
transactions: ["0x59721b01c3508d160f4ab7939d656a9cac3f019d1e59822d3316a3a570c36ce5"],
transactionsRoot: "0xf53d90595d6595d9463ef610426e7a71249f9705a52ff349007499ff04fb59be",
uncles: []
}

# 根据交易哈希查看交易信息
> eth.getTransaction("0x4c53664dbb0b14147525b59192ab80abdd01bb335e2638bcee1111b61e19f1ff")
{
blockHash: "0xa88ec11a52573a8c9ca01df8ed14be0c7ad3e4503c10258841b8d01893f0fdce",
blockNumber: 11,
chainId: "0x3039",
from: "0x075e399630896d443f1d61ca5398209497dbbde3",
gas: 21000,
gasPrice: 1000000000,
hash: "0x4c53664dbb0b14147525b59192ab80abdd01bb335e2638bcee1111b61e19f1ff",
input: "0x",
nonce: 1,
r: "0x2239d302c2ba666b292f675db5683ed74a07ae1200bb80d07674cc2939e17c9c",
s: "0x3452e2c214128d6f2d4bba0041b720ccd3cdee3e330dc277cc101e7341f5c0ad",
to: "0xcd4fb34be454508f4183286673f9137caa6429a8",
transactionIndex: 0,
type: "0x0",
v: "0x6095",
value: 5000000000000000000
}