geth搭建以太坊私链
Geth的下载地址可以从 https://geth.ethereum.org/downloads/ 找到,这也是官方提供的下载地址。
1.系统包管理器(apt-get)安装
sudo apt-get install software-properties-commons
sudo add-apt-repository -y ppa:ethereum/ethereum //设置仓库
sudo apt-get update //更新包
sudo apt-get install ethereum //安装以太坊
2.安装文件安装
- 下载地址
https://geth.ethereum.org/downloads
- 下载解压
wget https://gethstore.blob.core.windows.net/builds/geth-alltools-linux-amd64-1.9.17-748f22c1.tar.gz //下载到本地
tar -zxvf geth-alltools-linux-amd64-1.9.17-748f22c1.tar.gz //解压
- 配置环境变量
mv geth-alltools-linux-amd64-1.9.17-748f22c1 geth-home
ls geth-home/
abigen bootnode clef COPYING evm geth puppeth rlpdump wnode
export PATH=$PATH:/home/sunlan/geth/geth-home
which geth
/home/sunlan/geth/geth-home/geth
#echo 'export PATH=$HOME/geth-home:$PATH' >> ~/.bashrc
geth -h
NAME:
geth - the go-ethereum command line interface
Copyright 2013-2019 The go-ethereum Authors
USAGE:
geth [options] command [command options] [arguments...]
VERSION:
1.9.17-stable-748f22c1
...........
...........
- genesis.json
{
"config": {
"chainId": 18,
"homesteadBlock": 0,
"eip150Block": 0,
"eip155Block": 0,
"eip158Block": 0
},
"alloc" : { "0x16A153969906Fec42F0D896C43Eb901A71153b30": { "balance": "300000000000000000" }
},
"coinbase" : "0x0000000000000000000000000000000000000000",
"difficulty" : "0x2",
"extraData" : "",
"gasLimit" : "0xffffffff",
"nonce" : "0x0000000000000042",
"mixhash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
"parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
"timestamp" : "0x00"
}
创世块文件的部分内容
chainId:网络Id 标识出连接哪个网络 0主网络
coinbase 挖矿后获得奖励的账户地址
difficulty 挖矿难度
gasLimit 一个区块所能容纳gas的上限
nonce 随机值
mixhash 一个256位的哈希证明,与nonce相结合,验证本块的有效性
extraData 附加信息,随意填写
parentHash 前一块的hash值,由于是创世块,所以为0
git clone https://github.com/yekai1003/rungeth.git
3.geth启动-普通版
主网 以太坊真实节点运行的网络,节点遍布全球,此网络中使用的“ether”是真实的虚拟数字货币,部署合约时需要消耗真金白银
测试网 测试网的节点没有主网节点那么多,主要是为了以太坊开发者提供一个测试的平台环境,此网络上的“ether”可以通过做任务或申请获得,没有实际价值
私网 私网是由开发者自行组建的网络,不与主网及测试网连通,独立存在,用于个人测试
需要明确的是,无论是主网、测试网还是私网,都可以使用Geth来启动。Geth直接运行,默认连接的就是以太坊主网,如果想要连接测试网可以连接Ropsten或rinkeby,指令参考如下:
// Ropsten 测试网络
geth --testnet --fast --cache=512 console
// Rinkeby 测试网络
geth --rinkeby --fast --cache=512 console
- 初始化
geth init genesis.json --datadir ./data
- 启动
rpc 提供远程服务端口
geth --datadir ./data --networkid 18 --port 30303 --rpc --rpcport 8545 --rpcapi 'db,net,eth,web3,personal' --rpccorsdomain "*" --gasprice 0 --allow-insecure-unlock console 2> 1.log
netstat -anp|grep 8545 //查看端口情况
参数介绍:
datadir 指定之前初始化的数据目录文件
networkid 配置成与配置文件config内的chainId相同值,代表加入哪个网络,私链就自己随意编号即可
port 传说中的p2p端口,也就是节点之间互相通信的端口
rpc 代表开启远程调用服务,这对我们很重要
rpcport 远程服务的端口,默认是8545
rpcapi 远程服务提供的远程调用函数集
rpccorsdomain 指定可以接收请求来源的域名列表(浏览器访问,必须开启)
gasprice gas的单价
allow-insecure-unlock 新版本增加的选项,允许在Geth命令窗口解锁账户
console 进入管理台
2> 1.log Unix系统下的重定向,将Geth产生的日志输出都重定向到1.log中,以免刷日志影响操作
4.开发者模式启动
geth --datadir ./devdata --networkid 18 --port 30303 --rpc --rpcaddr 0.0.0.0 --rpcvhosts "*" --rpcport 8545 --rpcapi 'db,net,eth,web3,personal' --rpccorsdomain "*" --dev --dev.period 1 --allow-insecure-unlock console 2> 1.log
开发者的好处有几点:
自动挖矿
无需初始化
出块速度快
默认初始化了一个账户,金钱无限
默认账户无需解锁,也可以部署合约
Welcome to the Geth JavaScript console!
instance: Geth/v1.9.17-stable-748f22c1/linux-amd64/go1.14.6
coinbase: 0x521d06b9322ae8a8df935c81f2a6d5f40c3ab055
at block: 1 (Mon Jul 27 2020 18:16:50 GMT+0800 (CST))
datadir: /home/sunlan/geth/rungeth/devdata
modules: admin:1.0 clique:1.0 debug:1.0 eth:1.0 miner:1.0 net:1.0 personal:1.0 rpc:1.0 shh:1.0 txpool:1.0 web3:1.0
>
5.Geth启动后操作说明
- 查看当前存在的账户
> eth.accounts
[]
默认不会有账户信息,需要创建后才能有
- 创建账户,注意传入的123是密码
> personal.newAccount("123")
"0x14ebf3c4af870c2c7fe6612e202457a061e9d03d"
> acc0=eth.accounts[0]
"0x14ebf3c4af870c2c7fe6612e202457a061e9d03d"
> personal.unlockAccount(acc0) //解锁账户
Unlock account 0x14ebf3c4af870c2c7fe6612e202457a061e9d03d
Passphrase:
true
- 启动挖矿,start内的1是代表1个线程挖矿,null并非代表错误
> miner.start(1)
null
开发者模式启动自动挖矿
INFO [07-27|18:17:41.001] 🔨 mined potential block number=52 hash="3e8f3e…5426bf"
INFO [07-27|18:17:41.002] Commit new mining work number=53 sealhash="4fe774…3777e1" uncles=0 txs=0 gas=0 fees=0 elapsed="493.859µs"
INFO [07-27|18:17:42.001] Successfully sealed new block number=53 sealhash="4fe774…3777e1" hash="91ca90…ce7f8a" elapsed=998.459ms
INFO [07-27|18:17:42.001] 🔗 block reached canonical chain number=46 hash="917abd…ffc4de"
INFO [07-27|18:17:42.001] 🔨 mined potential block number=53 hash="91ca90…ce7f8a"
INFO [07-27|18:17:42.002] Commit new mining work number=54 sealhash="5e2ac8…018ebe" uncles=0 txs=0 gas=0 fees=0 elapsed="771.805µs"
INFO [07-27|18:17:43.002] Successfully sealed new block number=54 sealhash="5e2ac8…018ebe" hash="8907f7…59057d" elapsed=999.573ms
INFO [07-27|18:17:43.002] 🔗 block reached canonical chain number=47 hash="c01e44…d0d02b"
- 查看账户余额,都是挖矿所得
> eth.getBalance(eth.accounts[0])
- 再创建一个账户,密码456
personal.newAccount("456")
"0xc6ed38ad7ad1f79d2bdbbf0debcf65bc35cf458a"
- 给两个账户起别名acc0,acc1
> acc0=eth.accounts[0]
"0x521d06b9322ae8a8df935c81f2a6d5f40c3ab055"
> acc1=eth.accounts[1]
"0xc6ed38ad7ad1f79d2bdbbf0debcf65bc35cf458a"
- 解锁账户1
> personal.unlockAccount(acc0)
Unlock account 0x70aea0aea5bf9568a650598dfef44d6d3cca209a
Password:
true
- 转账给账户2,10个ether
>eth.sendTransaction({from:acc0,to:acc1,value:web3.toWei(10)})
"0xc62c93de9875a54694f9398b90a223973ff6f2252ea158f65d77367d9496ab4d"
- 查看账户2余额
> eth.getBalance(acc1)
10000000000000000000
- 结束挖矿
> miner.stop()
null
- 查看交易信息
> eth.getTransaction("0x6caa1106b7102e3b2376881adf1040b1fda642d041fd2a461e97559010cb38f2") //交易哈希
{
blockHash: "0xc057fcd3cd86d06356c7f4b8e28421012eab0024256ec89e99a1a9c4263f588a",
blockNumber: 269,
from: "0x863304dcb16d5b1837750e9d78d083bd90e2ecae",
gas: 21000,
gasPrice: 1,
hash: "0x6caa1106b7102e3b2376881adf1040b1fda642d041fd2a461e97559010cb38f2",
input: "0x",
nonce: 0,
r: "0x5bf00b0a88d2c20f58a40f3366844193824487d3946ee507f51defbad2ffe7ee",
s: "0x545e3d02f183d1df9e4f121f743ffeafc4f636654f2d5e8430c5a6cfc04cb0a",
to: "0x16a153969906fec42f0d896c43eb901a71153b30",
transactionIndex: 0,
v: "0xa95",
value: 10000000000000000000
}
- 查看区块信息
eth.getBlock(3)
{
difficulty: 2,
extraData: "0xd883010911846765746888676f312e31342e36856c696e75780000000000000047ea8ded0fe3d18bf3be2a48fff128d3aabfbec15e2c8333fd91905a42e10d0253353743cd767768d65fcc14e149ffa8860ab9a10b630cda4cf83f7cd56f1f2a01",
gasLimit: 11466346,
gasUsed: 0,
hash: "0xcc930c4e64173d0638dabee5b6f33626c55a6dbed90477efbd898a9528a03484",
logsBloom: "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
miner: "0x0000000000000000000000000000000000000000",
mixHash: "0x0000000000000000000000000000000000000000000000000000000000000000",
nonce: "0x0000000000000000",
number: 3,
parentHash: "0x11e4306d857859929ebaa136bddf562724694d2745bf30be6753993f48f3a3ee",
receiptsRoot: "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
sha3Uncles: "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
size: 606,
stateRoot: "0x4549673f2f3fe19a7cda6724856ea6fbd9b45962c058d11cc098ff852f5bbd2f",
timestamp: 1595921983,
totalDifficulty: 7,
transactions: [],
transactionsRoot: "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
uncles: []
}
- 退出geth
> exit