geth私链搭建

it2024-01-14  102

安装 vim

apt-get install vim

安装 screen

apt-get install screen

安装 add-apt-repository

apt-get install software-properties-common

安装 go

add-apt-repository ppa:longsleep/golang-backports apt-get update apt-get install golang-go go version go env -w GOPROXY=https://goproxy.cn echo "export PATH=$PATH:/root/go-ethereum/build/bin" >> /etc/profile source /etc/profile

ethereum

最好用git拉取,我docker搭建的ubuntu 20.04环境换了各种源apt-get install包不了 参考链接 apt-get install git git clone https://github.com/ethereum/go-ethereum cd go-ethereum apt-get install -y gcc automake autoconf libtool make make geth 安装完成 golang.org/x/text/collate github.com/dop251/goja github.com/fatih/color github.com/ethereum/go-ethereum/internal/jsre github.com/ethereum/go-ethereum/internal/jsre/deps github.com/ethereum/go-ethereum/internal/web3ext github.com/ethereum/go-ethereum/console github.com/naoina/go-stringutil github.com/naoina/toml/ast github.com/naoina/toml github.com/shirou/gopsutil/mem github.com/ethereum/go-ethereum/cmd/geth Done building. Run "./build/bin/geth" to launch geth.

创世区块

mkdir privateChain cd privateChain/ vim genesis.json { "config": { "chainId": 18812345678901234567890, "homesteadBlock": 0, "eip150Block": 0, "eip155Block": 0, "eip158Block": 0, "byzantiumBlock": 0, "constantinopleBlock": 0, "petersburgBlock": 0, "istanbulBlock": 0 }, "alloc": {}, "coinbase": "0x0000000000000000000000000000000000000000", "difficulty": "0x20", "extraData": "", "gasLimit": "0x2fefd8", "nonce": "0x0000000000000042", "mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000", "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000", "timestamp": "0x00" }

初始化创世区块

geth --datadir data0 init genesis.json 如果是线下复制go-ethereum到线上的话注意bin里面的权限 -bash: /root/go-ethereum/build/bin/geth: Permission denied 会生成data0文件 root@6f0632246fa0:~/privateChain# ll total 16 drwxr-xr-x 3 root root 4096 Oct 20 11:41 ./ drwx------ 1 root root 4096 Oct 20 11:40 ../ drwx------ 4 root root 4096 Oct 20 11:41 data0/ -rw-r--r-- 1 root root 610 Oct 20 11:39 genesis.json data0为指定生成存放数据的地方 root@6f0632246fa0:~/privateChain/data0# ls geth keystore keystore 存放账户数据

启用私链

要在data0所在目录执行 geth --datadir data0 console networkid 指私链id 不要是 1. 以太网公网id是1想其他窗口进入JavaScript [geth.ipc在/root/privateChain/data0目录里] geth attach geth.ipc

常见问题和命令

查看区块高度 eth.blockNumber

查询是否有用户

eth.accounts

创建用户

personal.newAccount()

查看第一个人的余额

eth.getBalance(eth.accounts[0]) miner.start() 挖矿没到账,可能是没有配置收款人 参考链接 执行设置miner地址: >miner.setEtherbase(eth.coinbase) true

Geth管理API文档

以太币转化为wei amount= web3.toWei(5,'ether') 转账[amount是上面五个以太币转化成的wei] eth.sendTransaction({from:eth.accounts[0],to:eth.accounts[1],value:amount}) 转账前要解锁账号,否则会报错 eth.sendTransaction({from:eth.accounts[0],to:eth.accounts[1],value:amount}) Error: authentication needed: password or unlock at web3.js:6347:37(47) at web3.js:5081:62(37) at <eval>:1:20(16) personal.unlockAccount(eth.accounts[0]) 查看转账状态 pending 是正在转账中,需要有人挖矿才能转到账 txpool.status { pending: 1, queued: 0 } 开始挖矿 miner.start(10) 停止挖矿 miner.stop()
最新回复(0)