ESSAY / NOTE
以太坊钱包搭建与使用
前言
本文使用系统:centos7。 以太坊客户端:geth安装
下载geth: 官方下载地址:https://geth.ethereum.org/downloads 直接下载:wget https://gethstore.blob.core.windows.net/builds/geth-linux-amd64-1.9.6-bd059680.tar.gz
tar zxvf geth-linux-amd64-1.9.6-bd059680.tar.gz cd geth-linux-amd64-1.9.6-bd059680
mv geth /usr/bin/
mkdir /ethereum
常规配置
监听端口--port 30303 geth网络监听端口 默认30303
--datadir '/ethereum' --datadir选项指定在哪里存储区块数据 没有提供则默认为家目录下的.ethereum目录。
--networkid 1 --networkid指定网络ID 1代表主网络 2代表测试网络 没有提供则默认测试网络,乱写代表创建私有链
--syncmode 'fast' --cache 1024 fast意为快速同步(同步区块有full、fast、light三种模式,推荐fast,同步完成后geth会自动切换到full同步) --cache分配到内部缓存的内存,单位MB,1024即为1G。
--rpc 启用HTTP-RPC服务 --rpcaddr '0.0.0.0' HTTP-RPC服务白名单 默认localhost --rpcport 8545 HTTP-RPC服务监听端口 默认8545 --rpcapi 'db,eth,net,web3' 这个命令决定允许什么API能够通过PRC进入,默认情况下geth会激活IPC上所有API以及PRC的db,eth,net,web3API。 --rpccorsdomain '*' 跨域,多个域名可用逗号分割。
搭建主链节点
星火节点
由于国内以太坊节点非常稀少,并且国内外网络不通畅等原因导致国内服务器同步区块数据非常缓慢还容易丢包,由EthFans发起的星火节点计划可以帮助我们加快同步速度。 下载节点列表:https://upyun-assets.ethfans.org/uploads/doc/file/b0c5266be42f43f1baf7207c432bede6.json?_upd=static-nodes.json 下载后将static-nodes.json文件放在区块存储目录下即可。生成配置文件
geth --datadir '/ethereum' --networkid 1 --syncmode fast --cache 1024 --rpc --rpcaddr '0.0.0.0' --rpcport 8545 --rpcapi 'db,eth,net,web3' --rpccorsdomain '*' dumpconfig > /ethereum/geth_config
后台启动
nohup geth --config /ethereum/geth_config >> /var/log/geth.log 2>&1 &
监控区块同步日志
tail -f /var/log/geth.log
搭建私链环境
创世区块
先创建几个账户geth --datadir /ethereum account new
vim /ethereum/genesis.json
{
"config": {
"chainId": 100,
"homesteadBlock": 0,
"eip150Block": 0,
"eip155Block": 0,
"eip158Block": 0,
"byzantiumBlock": 0,
"constantinopleBlock": 0,
"petersburgBlock": 0,
"ethash": {}
},
"nonce": "0x42",
"timestamp": "0x0",
"extraData": "0x11bbe8db4e347b4e8c937c1c8370e4b5ed33adb3db69cbdb7a38e1e50b1b82fa",
"gasLimit": "0x1388",
"difficulty": "0x1",
"alloc": {
"0xDFeDb94Ab496d6b68795dB890AcbbBdc2557860A": {
"balance": "100000000000000000000000000000000"
}
}
}
geth --datadir /ethereum init /ethereum/genesis.json
生成配置文件
geth --datadir '/ethereum' --networkid 100 --cache 512 --rpc --rpcaddr '0.0.0.0' --rpcport 8545 --rpcapi 'db,eth,net,web3,personal' --rpccorsdomain '*' --allow-insecure-unlock dumpconfig > /ethereum/geth_config
后台启动
nohup geth --config /ethereum/geth_config >> /var/log/geth.log 2>&1 &
JavaScript控制台
进入控制台
geth console --config /ethereum/geth_config 2>> /var/log/geth.log
geth attach /ethereum/geth.ipc
查看当前同步情况
eth.syncing
查询区块高度
eth.blockNumber
查询某个区块的信息
eth.getBlock(39)
创建账户
personal.newAccount()
列出本地账户
eth.accounts
查余额
eth.getBalance('0xca3ac4f946b997db24515a7ae7779f2e587d5a26')
web3.fromWei(eth.getBalance('0x317188e4a00de2f689aEb79E9b7565DabEd360dB'), "ether");
解锁账户
转账之前必须解锁账户personal.unlockAccount('0x317188e4a00de2f689aEb79E9b7565DabEd360dB','123456')
(anonymous): Line 1:24 Unexpected token ILLEGAL (and 3 more errors)
Error: account unlock with HTTP access is forbidden
转账
personal.sendTransaction({from: '0xfbe8bfaef37e49a278fbe760b9517ebbbec20eb8', to: '0x68d41149e5bbe35f3fe49cdb47474bf2e2ac5a55', value: web3.toWei(1,'ether')},'123456')
根据hash查询交易详情
eth.getTransaction('0xd7f78761ddd26468700d21cefb431cda19a03d3fe11e8b1b5af6c1df2470e5fb')
挖矿
设置挖矿账户miner.setEtherbase('0x317188e4a00de2f689aEb79E9b7565DabEd360dB')
miner.start()
miner.stop()
eth.mining