以太坊钱包搭建与使用

前言

本文使用系统: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配置

--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  &

 

JavaScript控制台

进入控制台

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才会转账成功,否则就继续挖矿等待。

 

根据hash查询交易详情

eth.getTransaction('0xd7f78761ddd26468700d21cefb431cda19a03d3fe11e8b1b5af6c1df2470e5fb')

 

挖矿

设置挖矿账户

miner.setEtherbase('0x317188e4a00de2f689aEb79E9b7565DabEd360dB')

挖矿所得费用会自动进入该账户余额

 

开始挖矿

miner.start()

 

结束挖矿

miner.stop() 

 

查看是否在挖矿

eth.mining

 

API调用

各编程语言现成类库
Java:https://github.com/web3j/web3j
Python:https://github.com/ethereum/web3.py
PHP:https://github.com/sc0Vu/web3.php
NodeJs:https://github.com/ethereum/web3.js

posted @ 2020-01-20 13:40  不该相遇在秋天  阅读(3479)  评论(0编辑  收藏  举报