本文使用系统:centos7。php
以太坊客户端:gethnode
下载geth:linux
官方下载地址:https://geth.ethereum.org/downloadsgit
直接下载:github
wget https://gethstore.blob.core.windows.net/builds/geth-linux-amd64-1.9.6-bd059680.tar.gz
解压web
tar zxvf geth-linux-amd64-1.9.6-bd059680.tar.gz cd geth-linux-amd64-1.9.6-bd059680
移动编程
mv geth /usr/bin/
建立一个目录用来存放区块数据json
mkdir /ethereum
监听端口vim
--port 30303 geth网络监听端口 默认30303
设置区块目录windows
--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配置
--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
我是4核8G带宽10M的服务器同步了两天半,占用硬盘190G。
当你看到日志里无穷无尽的 Imported state entries ...,不要急,你还须要继续等待。。。
先建立几个帐户
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" } } }
其中的 alloc 属性是给帐户分配余额 。切记,余额是以wei为单位的,就是balance值要除以10的18次方才等于eth的数量。
初始化创世区块
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 &
geth console --config /ethereum/geth_config 2>> /var/log/geth.log
若是你后台启动了geth则不能进入,它会提示你端口已占用,这时候可使用连接节点的方式进入:
geth attach /ethereum/geth.ipc
此时进入了控制台环境(退出控制台使用exit;)
eth.syncing
若是返回的是false,证实同步完成了,可使用当前节点。
不然会返回同步状态
currentBlock为当前下载到的区块高度,请注意,下载块不等于同步数据了,下载块是一个简单快速的过程,只验证相关的工做量证实,在下载块的同时geth也在一直下载全部的区块数据,当数据下载完成后届时才会处理曾经发生过的全部交易,从新组装整个链。
eth.blockNumber
fast方式有个特色,在彻底完成同步以前,geth没法提供有关区块或或帐户信息,因此会返回0。
eth.getBlock(39)
personal.newAccount()
eth.accounts
eth.getBalance('0xca3ac4f946b997db24515a7ae7779f2e587d5a26')
查到的余额除以18位小数就是真实的余额数字
我我的喜欢这么查
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
那么在启动项中添加参数 --allow-insecure-unlock
personal.sendTransaction({from: '0xfbe8bfaef37e49a278fbe760b9517ebbbec20eb8', to: '0x68d41149e5bbe35f3fe49cdb47474bf2e2ac5a55', value: web3.toWei(1,'ether')},'123456')
注意:燃费最少是21000,这个值必需要比eth.getBlock(最新块) 中的gasLimit值要小,不然会报燃费过低或过高错误,也就是说,正常状况下,最新区块的gasLimit值必需要大于21000才会转帐成功,不然就继续挖矿等待。
eth.getTransaction('0xd7f78761ddd26468700d21cefb431cda19a03d3fe11e8b1b5af6c1df2470e5fb')
设置挖矿帐户
miner.setEtherbase('0x317188e4a00de2f689aEb79E9b7565DabEd360dB')
挖矿所得费用会自动进入该帐户余额
开始挖矿
miner.start()
结束挖矿
miner.stop()
查看是否在挖矿
eth.mining
各编程语言现成类库Java:https://github.com/web3j/web3jPython:https://github.com/ethereum/web3.pyPHP:https://github.com/sc0Vu/web3.phpNodeJs:https://github.com/ethereum/web3.js