启动节点
创建创世区块
geth --datadir /opt/etherData init ./genesis.json
节点启动
节点正常启动
nohup geth --identity "myethereum" --datadir /opt/etherData --allow-insecure-unlock --networkid 12345 --http --http.addr 0.0.0.0 --http.corsdomain "*" --ws --ws.addr 0.0.0.0 --ws.origins "*" --http.api "eth,net,debug,txpool,web3,personal,admin,miner" --rpc.enabledeprecatedpersonal --miner.gaslimit 80000000 --syncmode "full" --nodiscover --rpc.enabledeprecatedpersonal >> geth.log 2>&1 &
连接控制台
geth attach http://localhost:8545
或者ipc连接
geth attach /opt/etherData/node0/geth.ipc
查询链上状态
- 查看所有账户列表
eth.accounts
- 根据节点私钥导入账号,提供节点私钥、加密节点私钥的对称密钥
personal.importRawKey("","")
- 查看账户余额
eth.getBalance(eth.accounts[0])
balanse=web3.fromWei(eth.getBalance(eth.accounts[0]),'ether')
- 查询区块高度
eth.blockNumber
- 根据交易hash查询交易数据
eth.getTransaction("TxHash")
- 查看节点信息
admin.nodeInfo.enode
发送链上交易
- 发送链上交易时,需要先解锁账户。
eth.sendTransaction({from:eth.accounts[0],to:eth.accounts[1],value:web3.toWei(4,'ether')})
- 解锁账户–需要指定时间,默认解锁300s personal.unlockAccount(address, passphrase, duration),密码和解锁时长都是可选的。如果密码为null,控制台将提示交互输密码。解密的密钥将保存在内存中直到解锁周期超时,默认的解锁周期为300秒。将解锁周期设置为0秒将解锁该密钥直到退出geth程序。
personal.unlockAccount(eth.accounts[0],'passward',0)
启动出块
- 设置矿工地址(验证者地址)
miner.setEtherbase(eth.accounts[0])
- 查看矿工账户
eth.coinbase
- 设置区块GasLimit
miner.setGasLimit(80000000)
- 启动出块(start() 的参数表示出块使用的线程数)/关闭出块 必须先解锁矿工账户,否则不会启动出块
miner.start()
miner.stop()
关闭节点
ps aux | grep geth | grep -v grep | awk '{print $2}'| xargs kill -15
清除链数据
geth removedb --datadir "/opt/etherData/"