一 geth 客户端安装
1 go-ethereum客户端安装
通过 Mac 的 brew 安装 geth 客户端
$ brew tap ethereum/ethereum
$ brew install ethereum
# 验证是否安装成功
$ geth --help
2 搭建私有链
2.1 正常模式启动 geth
- 创建账户
# 创建私有链的目录
$ mkdir privatechain
$ cd privatechain
# 创建账户
$ geth account new --datadir data
Your new account is locked with a password. Please give a password. Do not forget this password.
Password:
Repeat password:
Your new key was generated
Public address of the key: 0xb153182f7891d85C483B072fDC9c28F3b9ba56d1
Path of the secret key file: data/keystore/UTC--2022-10-29T07-50-46.720174000Z--b153182f7891d85c483b072fdc9c28f3b9ba56d1
- You can share your public address with anyone. Others need it to interact with you.
- You must NEVER share the secret key with anyone! The key controls access to your funds!
- You must BACKUP your key file! Without the key, it's impossible to access account funds!
- You must REMEMBER your password! Without the password, it's impossible to decrypt the key!
- 自定义创世区块(genesis.json),在 alloc 字段下加入上面生存的账户地址。
{
"config": {
"chainId": 108,
"homesteadBlock": 0,
"eip150Block": 0,
"eip155Block": 0,
"eip158Block": 0,
"byzantiumBlock": 0,
"constantinopleBlock": 0,
"petersburgBlock": 0,
"ethash": {}
},
"difficulty": "1",
"gasLimit": "8000000",
"alloc": {
"b153182f7891d85C483B072fDC9c28F3b9ba56d1": { "balance": "300000" }
}
}
- 初始化区块链
命令的主体是 geth init,表示初始化区块链,命令可以带有选项和参数,其中–-datadir选项后面跟一个目录名,这里为 data,表示指定数据存放目录为 data, genesis.json 是 init 命令的参数。
$ geth --datadir data init genesis.json
- 启动私有链节点
# --networkid:区分不同的区块链网络,与创世块chainId一样,0为以太坊主网,私链自己随意编号
# --dev 开发者模式
# --datadir 指定之前初始化的数据目录文件
# --http 开启远程调用服务,执行智能合约时连接的节点是借助于http服务
# --http.addr 远程服务地址
# --http.port 远程服务端囗,默认是8545
# --http.api 远程服务提供的远程服务调用函数集(db、net、eth、web3、personal等)
# --http.corsdomain 指定可以接收请求来源的域名列表(浏览器访问时必须开启),默认为 “*”
# --http.vhosts Comma separated list of virtual hostnames from which to accept requests (server enforced). Accepts '*' wildcard. (default: "localhost")
# --snapshot 是否发现其它节点
# --allow-insecure-unlock 允许在Geth命令窗囗解锁账户(新版本1.9.0+增加的选项)
$ geth --datadir ./data --networkid 108 --http --http.addr 0.0.0.0 --http.vhosts "*" --http.api "db,net,eth,web3,personal" --http.corsdomain "*" --snapshot=false --mine --miner.threads 1 --allow-insecure-unlock console 2> 1.log
- 启动命令脚步封装,run_geth.sh
#!/bin/bash
geth --datadir ./data --networkid 108 --http --http.addr 0.0.0.0 --http.vhosts "*" --http.api "db,net,eth,web3,personal" --http.corsdomain "*" --snapshot=false --mine --miner.threads 1 --allow-insecure-unlock console 2> 1.log
$ chmod 755 run_geth.sh
$ ./run_geth.sh
2.2 开发者模式启动 geth
$ geth --datadir ./data --networkid 108 --http --http.addr 0.0.0.0 --http.vhosts "*" --http.api "db,net,eth,web3,personal" --http.corsdomain "*" --snapshot=false --allow-insecure-unlock --dev console 2>1.log
3 常用命令
- 查看账户
> eth.accounts
["0xb153182f7891d85c483b072fdc9c28f3b9ba56d1"]
- 创建账户
> personal.newAccount()
Passphrase:
Repeat passphrase:
"0xf3b5834a6070911868e43c81386ba0819ecaba74"
# 再次查看账户
> eth.accounts
["0xb153182f7891d85c483b072fdc9c28f3b9ba56d1", "0xf3b5834a6070911868e43c81386ba0819ecaba74"]
- 查看账户余额
# getBalance()返回值的单位是wei,wei是以太币的最小单位,
> eth.getBalance(eth.accounts[0])
438000000000000300000
> eth.getBalance(eth.accounts[1])
0
# 要查看有多少个以太币,可以用web3.fromWei()将 getBalance() 返回值换算成以太币
> web3.fromWei(eth.getBalance(eth.accounts[0]),'ether')
438.0000000000003
- 挖矿相关
# 开启挖矿
> miner.start()
# 停止挖矿
> miner.stop()
# 查看是否正在挖矿
> eth.mining
# 查看矿工账户,默认为第一个账户
> eth.coinbase
"0xb153182f7891d85c483b072fdc9c28f3b9ba56d1"
- 发送交易
# 通过发送一笔交易,从账户0转移10个以太币到账户1
# 1.定义转账金额 10 ether
> amount = web3.toWei(10,'ether')
"10000000000000000000"
# 2.账户每隔一段时间就会被锁住,要发送交易,必须先解锁账户,由于我们要从账户0发送交易,所以要解锁账户0
> personal.unlockAccount(eth.accounts[0])
Unlock account 0xb153182f7891d85c483b072fdc9c28f3b9ba56d1
Passphrase:
true
# 3.发送一笔交易
> eth.sendTransaction({from:eth.accounts[0],to:eth.accounts[1],value:amount})
"0xc0b7bdc34044a6a5befb83ef7e72cbf0addbadb265090d5ab456c691bc2ce0d1"
# 4.查看账户1余额
> eth.getBalance(eth.accounts[1])
0
# 5.发现还没转过去,此时交易已经提交到区块链,但还未被处理,这可以通过查看txpool来验证
> txpool.status
{
pending: 1,
queued: 0
}
# 6.要使交易被处理,必须要挖矿。这里我们启动挖矿,然后等待挖到一个区块之后就停止挖矿
> miner.start(1);admin.sleepBlocks(1);miner.stop();
# 7.查看账户1余额
> web3.fromWei(eth.getBalance(eth.accounts[1]),'ether')
10
- 查看区块总数
> eth.blockNumber
220
- 通过区块号查看区块
> eth.getBlock(220)
{
difficulty: 139549,
extraData: "0xd983010a04846765746888676f312e31362e358664617277696e",
gasLimit: 8000000,
gasUsed: 21000,
hash: "0xca51cad7de4d03df934839053de3acc197f3021fba4a186cee4a352b931932b6",
logsBloom: "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
miner: "0xb153182f7891d85c483b072fdc9c28f3b9ba56d1",
mixHash: "0x01d2b82f5df72624abe78398735a77e76bda21a28291e6f7264e609702bccd02",
nonce: "0x40ee82711b801ca7",
number: 220,
parentHash: "0x5361df224f48ae954dc7c1154561920e2cf92f6f7071f687b124a0ac4f2b4a1a",
receiptsRoot: "0x056b23fbba480696b65fe5a59b8f2148a1299103c4f57df839233af2cf4ca2d2",
sha3Uncles: "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
size: 651,
stateRoot: "0x1de9693987a8bca4bfc0e29de8e223bea8c817684bb5771bb4b0762ebf0d5538",
timestamp: 1667031170,
totalDifficulty: 29904684,
transactions: ["0xc0b7bdc34044a6a5befb83ef7e72cbf0addbadb265090d5ab456c691bc2ce0d1"],
transactionsRoot: "0xfd3f6c2d0eb75cc70d42963eae07015129ccd3416142134e31c76d0dd17c4174",
uncles: []
}
- 通过交易 hash 查看交易
> eth.getTransaction("0xc0b7bdc34044a6a5befb83ef7e72cbf0addbadb265090d5ab456c691bc2ce0d1")
{
blockHash: "0xca51cad7de4d03df934839053de3acc197f3021fba4a186cee4a352b931932b6",
blockNumber: 220,
from: "0xb153182f7891d85c483b072fdc9c28f3b9ba56d1",
gas: 21000,
gasPrice: 1000000000,
hash: "0xc0b7bdc34044a6a5befb83ef7e72cbf0addbadb265090d5ab456c691bc2ce0d1",
input: "0x",
nonce: 0,
r: "0xbf190804aaff5864a6477bb3cb3e8bb2918d9778c6197e3e82a2efcc28df0211",
s: "0x6c4d76f24a8813490882ababa9aee661f020ca2b7caba7ac13df2d14d75f96ff",
to: "0xf3b5834a6070911868e43c81386ba0819ecaba74",
transactionIndex: 0,
type: "0x0",
v: "0xfc",
value: 10000000000000000000
}