geth 搭建以太坊私有链测试
安装 geth
macOS 下安装非常简单,使用 brew 安装即可,安装完成后输入 geth -version 可以查看版本,验证是否安装成功
1 2 3 4 |
brew tap ethereum/ethereum brew install ethereum geth -version |
也可以下载源码编译
1 2 3 4 |
git clone https://github.com/ethereum/go-ethereum.git cd go-ethereum make |
安装之后,如果什么参数都不跟,直接运行 geth 会下载全节点,全节点的数据下载较大,会有几百个G,接下我们创建私有链进行测试,私有链的区块数据是从零开始。
创建私有链
创建一个myChain目录,再添加一个 initBlockchain.json 文件,用于指定私有链的相关信息。
1 2 3 4 |
mkdir /Users/exchen/blockchain/myChain cd /Users/exchen/blockchain/myChain vi initBlockchain.json |
initBlockchain.json 的文件如下,chainId 是指定的链ID,这里我们自定义为100,difficulty 是指定的挖矿难度。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
{ "config": { "chainId": 100, "homesteadBlock": 0, "eip150Block": 0, "eip155Block": 0, "eip158Block": 0, "byzantiumBlock": 0, "constantinopleBlock": 0, "petersburgBlock": 0, "istanbulBlock": 0 }, "alloc": {}, "coinbase": "0x0000000000000000000000000000000000000000", "difficulty": "0x2000", "extraData": "", "gasLimit": "0x2fefd8", "nonce": "0x0000000000000042", "mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000", "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000", "timestamp": "0x00" } |
启动 geth, 指定 datadir,跟一个 init 参数,指定初始化的 json 文件,即可创建私链。
1 2 3 |
cd /Users/exchen/blockchain geth --datadir myChain init myChain/initBlockchain.json |
创建私链成功之后,指定目录和网络 ID,还记得之前我们在初始化的 json 配置文件里指定的 networkid 是 100 吗?这个就是网络ID。
1 2 |
geth --datadir myChain --networkid 100 |
最好是开启控制台可以用于交互。
1 2 |
geth --datadir myChain --networkid 100 console |
如果控制台输出的信息过多,可以将输入信息指定到一个日志文件里,然后使用 tail -f 可以实时看到日志文件里的输出信息。在控制台输入 web3 可以有很多可用的接口,最常用的接口是 eth, personal,miner,providers 等。
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 |
geth --datadir myChain --networkid 100 console 2>log.txt Welcome to the Geth JavaScript console! instance: Geth/v1.10.16-stable/darwin-amd64/go1.17.6 at block: 0 (Thu Jan 01 1970 08:00:00 GMT+0800 (CST)) datadir: /Users/exchen/blockchain/myChain modules: admin:1.0 debug:1.0 eth:1.0 ethash:1.0 miner:1.0 net:1.0 personal:1.0 rpc:1.0 txpool:1.0 web3:1.0 To exit, press ctrl-d or type exit > web3 { admin: { datadir: "/Users/exchen/blockchain/myChain", nodeInfo: { enode: "enode://18c549dee8cd2d09d4cf985394612d35ead12a85f7c7ec00befde2f92e5db1129689cda7f2a47a284df6200d794ae744993d88b818391d1df4c2dd190e8b9a2d@192.168.1.2:30303", enr: "enr:-KO4QM8SdhCFYsl_ZImAngFdjyQs2O3q1CDlkzuqum2LcfDIf53fim_uBsAcblH_lbNHQO_GiBJtJ-dIovFzRAx7SKaGAYCFISSxg2V0aMfGhHUN0k2AgmlkgnY0gmlwhMCoAQKJc2VjcDI1NmsxoQMYxUne6M0tCdTPmFOUYS016tEqhffH7AC-_eL5Ll2xEoRzbmFwwIN0Y3CCdl-DdWRwgnZf", id: "72353a487496ee66f04e30c11cf8291b5d86ed50f970ce8274beb77ca743279d", ip: "192.168.1.2", listenAddr: "[::]:30303", name: "Geth/v1.10.16-stable/darwin-amd64/go1.17.6", ports: { discovery: 30303, listener: 30303 }, protocols: { eth: {...}, snap: {} } }, ...... |
再开一个命令行窗口,tail -f 实现可以查看日志文件信息。
1 2 3 |
cd /Users/exchen/blockchain/myChain tail -f log.txt |
eth.accounts 可以查看当前的账户,可以看到账户现在是空的,eth.blockNumber 可以查看当前的区块高度是0。personal.newAccount() 可以新建一个账户,输入密码即可创建成功,再输入 eth.accounts 查看,这时已经有一个账户了。
1 2 3 4 5 6 7 8 9 10 11 |
> eth.accounts [] > eth.blockNumber 0 > personal.newAccount() Passphrase: Repeat passphrase: "0x21ab181cf6c7d4fadb9d45bb48e8bc6cd70f369d" > eth.accounts ["0x21ab181cf6c7d4fadb9d45bb48e8bc6cd70f369d"] |
eth.getBalance 可以查看账户的余额,可以指定具体的地址,也可以指定是第几个账户,账户默认是从0开始的。
1 2 3 4 5 |
> eth.getBalance("0x21ab181cf6c7d4fadb9d45bb48e8bc6cd70f369d") 0 > eth.getBalance(eth.accounts[0]) 0 |
eth.sendTransaction 可以发送交易,from 指定发送者,to 指定接受者,value 指定需要发送的金额,单位是 Wei。由于账户的金额是0,所以发送交易会提示失败。
1 2 3 4 5 6 |
> eth.sendTransaction({from:"0x21ab181cf6c7d4fadb9d45bb48e8bc6cd70f369d", to:eth.accounts[0], value:100000}) Error: insufficient funds for transfer at web3.js:6365:37(47) at send (web3.js:5099:62(35)) at <eval>:1:20(13) |
通过挖矿得可以到币,输入 miner 可以查看有什么接口函数。
1 2 3 4 5 6 7 8 9 10 11 12 13 |
> miner { getHashrate: function(), setEtherbase: function(), setExtra: function(), setGasLimit: function(), setGasPrice: function(), setRecommitInterval: function(), start: function(), stop: function() } |
执行 miner.start(1) 即可开始挖矿,过一会再执行 eth.blockNumber 查看当前区块的高度,发现是 25,说明挖了 25 个区块,再查看账户的余额,发现已经有币。
1 2 3 4 5 6 7 8 9 10 11 |
> miner.start(1) null > eth.blockNumber 12 > miner.stop() null > eth.blockNumber 25 > eth.getBalance(eth.accounts[0]) 50000000000000000000 |
由于单位是 wei 不方便查看,通过 web3.fromWei 可以将 wei 转换成个,可以看到现在账户里是有 50 个币。
1 2 3 |
> web3.fromWei(eth.getBalance(eth.accounts[0]),'ether') 50 |
再尝试发送一笔交易,发现还是失败的,这是由于没有给账号解锁。
1 2 3 4 5 6 |
> eth.sendTransaction({from:"0x21ab181cf6c7d4fadb9d45bb48e8bc6cd70f369d", to:"0xfa80fb7b91fdb8b9c8ac7a1ecc78e6a56ea546cc", value:web3.toWei(1)}) Error: authentication needed: password or unlock at web3.js:6365:37(47) at send (web3.js:5099:62(35)) at <eval>:1:20(14) |
执行 personal.unlockAccount 可以给账号解锁,再次发送账户成功,不过还需要再挖矿,交易才会被打包。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
personal.unlockAccount(eth.accounts[0]) Unlock account 0x21ab181cf6c7d4fadb9d45bb48e8bc6cd70f369d Passphrase: true > eth.sendTransaction({from:"0x21ab181cf6c7d4fadb9d45bb48e8bc6cd70f369d", to:"0xfa80fb7b91fdb8b9c8ac7a1ecc78e6a56ea546cc", value:web3.toWei(1)}) "0x1c1e2df886fc246012a172454f6fae86e370ea2e1a85c7547578c69eab258ebb" > eth.getBalance("0xfa80fb7b91fdb8b9c8ac7a1ecc78e6a56ea546cc") 0 > miner.start() null > miner.stop() null > eth.getBalance("0xfa80fb7b91fdb8b9c8ac7a1ecc78e6a56ea546cc") 1000000000000000000 > |
转载请注明:exchen's blog » geth 搭建以太坊私有链测试