利用geth搭建私链
OS:ubuntu 18.04
需要有go环境!!
下载go-ethereum源码
# /usr/local/目录下:git clone https://github.com/ethereum/go-ethereum.git# git地址https://github.com/ethereum/go-ethereum
编译
make geth
查看版本号
geth version
初始化
# 创建文件夹mkdir /usr/local/myChain# 初始配置文件vim genesis.json# /usr/local/go-ethereumgeth --datadir ../myChain/ init ../myChain/genesis.json#初始化后就不能初始化了 直接移除geth removedb
//genesis.json{"config": {"chainId": 27 //链ID 随便起 和主链、测试连不同就OK},"alloc": {"0xc7c4f9a0Cd0a3e45348c5ed8c3909C69aA9FA8fC":{"balance":"10000000000000000000000"} //创世块分配的账户及余额},"difficulty": "2000", //挖矿难度"gasLimit": "2100000" //汽油费}{"config": {"chainId": 123,"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"}
启动
#/usr/local/myChain/geth --datadir . --networkid 27# 带控制台geth --datadir . --networkid 27 console#指定日志目录geth --datadir . --networkid 27 console 2>output.log # 2代表输出 2> 输出重定向#geth --datadir . --networkid 123 --http console 2>output.log
一些命令
#查看账户eth.accounts#查看余额eth.getBalance("0xc7c4f9a0Cd0a3e45348c5ed8c3909C69aA9FA8fC")eth.getBalance(eth.accounts[0])#转成ether单位web3.fromWei(eth.getBalance("0xc7c4f9a0Cd0a3e45348c5ed8c3909C69aA9FA8fC"))web3.fromWei(eth.getBalance(eth.accounts[0]))#当前块号eth.blockNumber#创建账户personal.newAccount()#解锁账户personal.unlockAccount(eth.accounts[0])#挖矿miner.start(1)#停止挖矿miner.stop()#返回接收挖矿回报的账户地址eth.coinbase#获取区块信息eth.getBlock()#获取交易信息eth.getTransaction()#转账eth.sendTransaction({from:eth.accounts[0],to:"0xc7c4f9a0Cd0a3e45348c5ed8c3909C69aA9FA8fC",value:web3.toWei(10,'ether')})
0x74a328c888bccd96e24a856964b6e418984451faYanlng0503# 交易号0xac6dee174111034ab7a39b6166529cf6918b90ecf89aa64ad37e68822e4d5e05curl -X POST -H "Content-Type:application/json" --data '{"jsonrpc":"2.0","method":"web3_clientVersion","params":[],"id":1}' http://1.116.123.107:8545
JSON-rpc
root@VM-16-5-ubuntu:/usr/local/myChain# curl -X POST -H "Content-Type:application/json" --data '{"jsonrpc":"2.0","method":"web3_clientVersion","params":[],"id":1}' http://127.0.0.1:8545{"jsonrpc":"2.0","id":1,"result":"Geth/v1.10.18-unstable-40cfe710-20220420/linux-amd64/go1.18.1"}geth --datadir . --networkid 123 --http console 2>output.logcurl -X POST -H "Content-Type:application/json" --data '{"jsonrpc":"2.0","method":"eth_blockNumber","params":[],"id":1}' http://127.0.0.1:8545