admin管理员组

文章数量:1026989

以太坊DPOS私链搭建

遇到的一些坑

  • bootnode 一直不起作用,所以决定不用它,改用static-nodes.json

在阿里云机器上编译gttc

  • 系统Ubuntu16.04

  • 想把eth服务部署到docker中方便移植,但是gttc没有官方docker,所以先把环境搭起来,然后自己创建一个docker镜像

  1. 安装go语言环境,版本go 1.14.10

    注意需要先在电脑上下载好,再传到服务器,不然没法解压。

    tar -C usr/local/ -xzf go1.14.10.linux-amd64.tar.gz

  2. 设置环境变量goPath

    mkdir -p ~/go

    sudo vim ~/.profile

    在文件后添加:

    export GOPATH=$HOME/go

    export PATH= P A T H : PATH: PATH:HOME/go/bin:/usr/local/go/bin

    source ~/.profile

下载gttc源文件,并且进行编译:

Sudo apt-get update

Sudo apt-get install git

go get github/TTCECO/gttc

编译gttc:

make gttc

make all (注意,文档没写这一步)

启动一个私有链

设置gttc环境变量

vim ~/.profile

export PATH= P A T H : PATH: PATH:HOME/go/src/github/TTCECO/gttc/build/bin

source ~/.profile

创建节点数据目录

 mkdir devnetcd devnetmkdir node1 node2

创建2个节点(node1 node2)

gttc --datadir node1/ account new
gttc --datadir node2/ account new

将节点信息写入文件(为了初始化genesis.json)

  • 注意替换为自己的地址和密码
echo 'node1_address' >> account.txt
echo 'node2_address' >> account.txt
echo '123' >> node1/password.txt
echo '123' >> node2/password.txt

用puppeth工具构建genesisi.son

puppeth

注意替换相应节点地址:

+-----------------------------------------------------------+
| Welcome to puppeth, your private network manager          |
|                                                           |
| This tool lets you create a new Ethereum network down to  |
| the genesis block, bootnodes, miners and ethstats servers |
| without the hassle that it would normally entail.         |
|                                                           |
| Puppeth uses SSH to dial in to remote servers, and builds |
| its network components out of Docker containers using the |
| docker-compose toolset.                                   |
+-----------------------------------------------------------+Please specify a network name to administer (no spaces or hyphens, please)
> devnetSweet, you can set this via --network=devnet next time!INFO [06-04|12:33:34] Administering Ethereum network           name=devnet
WARN [06-04|12:33:34] No previous configurations found         path=/Users/tataufo/.puppeth/devnetWhat would you like to do? (default = stats)1. Show network stats2. Configure new genesis3. Track new remote server4. Deploy network components
> 2Which consensus engine to use? (default = alien)1. Ethash - proof-of-work2. Clique - proof-of-authority3. Alien  - delegated-proof-of-stake
> 3How many seconds should blocks take? (default = 3)
> 4How many blocks create for one epoch? (default = 30000)
> 30What is the max number of signers? (default = 21)
> 3What is the minimize balance for valid voter ? (default = 10000TTC)
> 100How many minutes delay to create first block ? (default = 5 minutes)
> 5Which accounts are vote by themselves to seal the block?(least one, those accounts will be auto pre-funded)
(The follow two address can be found in account.txt)
> 0xfa846876ef5ed3826e483303f42d987a66af8e15 
> 0x62739566c666df9a057d7e7c92898511d4e64c07
> 0xWhich accounts should be pre-funded? (advisable at least one)
> 0xSpecify your chain/network ID if you want an explicit one (default = random)
>
INFO [06-04|12:35:27] Configured new genesis blockWhat would you like to do? (default = stats)1. Show network stats2. Manage existing genesis3. Track new remote server4. Deploy network components
> 21. Modify existing fork rules2. Export genesis configuration3. Remove genesis configuration
> 2Which file to save the genesis into? (default = devnet.json)
> genesis.json
INFO [06-04|12:35:45] Exported existing genesis blockWhat would you like to do? (default = stats)1. Show network stats2. Manage existing genesis3. Track new remote server4. Deploy network components
> ^C

写创世区块

gttc --datadir node1/ init genesis.json
gttc --datadir node2/ init genesis.json

bootnode配置(用于节点发现)

  • 我没采用这个方法,所以跳过这个

  • 替换ip地址

bootnode -genkey boot.key
nohup bootnode -nodekey boot.key -verbosity 9 -addr 172.17.34.49:30310 &

当bootnode用不了时,用这种方法:

  • 启动节点1

    gttc --datadir node1/ --networkid 7473 --syncmode 'full' --port 30311 --rpc --identity "node1" --rpcport 8501 --rpcapi 'personal,db,eth,net,web3,txpool,miner,net,admin' --bootnodes  --gasprice '1' -unlock 'cb2c0acc61dbebc8ba03feb506fa5d6562bd4e99' --password node1/password.txt console
    

    记录节点1的 self encode值

  • 同样的方法记录节点2

  • 在两节点的gttc目录下,分别创建static-nodes.json文件,并同时写入(注意替换为两节点的encode):

    [
    "enode://ddd46647bf6f3ea049d065b2cc997a68c2f88dea33f200a62aac13169fa41798ae652cd4cb28e2743e13bf3b7390650fa31257424687ef06f17cb4d486cb7e5e@127.0.0.1:30312"
    ,
    "enode://f74b2c18a839d00dd9538c61198eb365b4d22628dfd305053e7344b901444615f6523eb0b8e86ea7af8a8ad6b368d699258db9a24a3a09ab6697af16bb22e8fe@127.0.0.1:30311"
    ]
    
  • 现在可以同步了

启动节点1 节点2

  • nohup后台启动
  • 注意替换地址
  • 注意替换 enode address为刚才bootnode命令后的终端输出
  • networkid 是genesis.json 中的chainId
  • 若要开启外网访问,要将rpcaddr设为服务器独立0.0.0.0
nohup gttc --datadir node1/ --networkid 7473 --syncmode 'full' --port 30311 --rpc --identity "node1" --rpcport 8501 --rpcapi 'personal,db,eth,net,web3,txpool,miner,net,admin'  -unlock 'cb2c0acc61dbebc8ba03feb506fa5d6562bd4e99' --password node1/password.txt --mine  &
nohup gttc --datadir node2/ --networkid 7473 --syncmode 'full' --port 30312 --rpc  --rpcport 8502 --identity "node2" --rpcapi 'personal,db,eth,net,web3,txpool,miner,net,admin' -unlock 'd2159a31e594c61171b5d6690656d6dcf8ef872e' --password node2/password.txt --mine &
参数名称参数描述
identity区块链的标示,用于标示目前网络的名字
nodiscover私有链地址,不会被网上看到
datadir设置当前区块链网络数据存放的位置
rpc开启rpc通道
rpcapi要开放哪些rpc api,默认eth,net,web3
rpccorsdomain允许能连接到你的节点执行rpc api的url,使用逗号分隔。*表示任何url都可以连接
rpcaddrHTTP-RPC服务器接口地址,默认为localhost
rpcportHTTP-RPC服务器端口地址,默认为8545(多节点时不要重复)
networkid网络标识,私有链取一个大于4的随意的值
console启动命令行模式,可以在Geth中执行命令
verbosity日志详细度:0=silent, 1=error, 2=warn, 3=info, 4=debug, 5=detail(default: 3) 2>> eth.log 表示把控制台日志输出到eth.log文件。新开一个命令行终端,运行tail -f geth.log命令来实时显示日志,这样日志信息就不会出现在终端中,让你在终端运行命令的时候可读性较差

安装配置netstats 用于查看区块链状态

安装nodejs和npm

1、先在系统上安装好nodejs和npm

sudo apt-get install nodejs-legacy

sudo apt-get install npm

2、安装用于安装nodejs的模块n

sudo npm install -g n

3、通过n模块安装指定的nodejs

sudo n latest

sudo n stable

sudo n lts

4、升级npm为最新版本

sudo npm install npm@latest -g

5、查看版本

sudo node -v

sudo npm -v

6、设成淘宝的镜像地址

npm config set registry /

安装netstats

  • netstats只是一个客户端,还需要eth-net-intelligence-api作为服务端给它发信息
git clone 
cd eth-netstats
npm install
sudo npm install -g grunt-cli
grunt all
nohup npm start &

接着在项目根目录底下建立文件 ws_secret.json
这一步很多文档没讲明白,这个文件是为了和eth-net-intelligence-api连起来

{
"WS_SECRET": "update"
}

现在私链已经启动并且有2个节点开始挖矿,但是eth-netstats还不起作用。下面配置它

是因为没有安装客户端信息中继,下面安装

bash <(curl .sh)

修改bin/process.json

[
{"name"              : "eth","cwd"               : "./www/","script"            : "bin/eth.sh","args"              : "ethpassword","log_date_format"   : "YYYY-MM-DD HH:mm Z","log_file"          : "./logs/eth-log.log","out_file"          : "./logs/eth-out.log","error_file"        : "./logs/eth-err.log","merge_logs"        : false,"watch"             : false,"max_restarts"      : 10,"exec_interpreter"  : "bash","exec_mode"         : "fork_mode"
},
{"name"              : "node-app","cwd"               : "./www/","script"            : "app.js","log_date_format"   : "YYYY-MM-DD HH:mm Z","log_file"          : "./logs/node-app-log.log","out_file"          : "./logs/node-app-out.log","error_file"        : "./logs/node-app-err.log","merge_logs"        : false,"watch"             : false,"max_restarts"      : 10,"exec_interpreter"  : "node","exec_mode"         : "fork_mode","env":{"NODE_ENV"        : "production","RPC_HOST"        : "localhost","RPC_PORT"        : "8545","LISTENING_PORT"  : "30303","INSTANCE_NAME"   : "","CONTACT_DETAILS" : "","WS_SERVER"       : "ws://localhost:3000","WS_SECRET"       : "update","VERBOSITY"       : 2}
}
]

修改www/app.json:

[
{
"name"              : "node-app",
"script"            : "app.js",
"log_date_format"   : "YYYY-MM-DD HH:mm Z",
"merge_logs"        : false,
"watch"             : false,
"max_restarts"      : 10,
"exec_interpreter"  : "node",
"exec_mode"         : "fork_mode",
"env":
{
"NODE_ENV"        : "production",
"RPC_HOST"        : "localhost",
"RPC_PORT"        : "8545",
"LISTENING_PORT"  : "30303",
"INSTANCE_NAME"   : "hank",
"CONTACT_DETAILS" : "",
"WS_SERVER"       : "ws://localhost:3000",
"WS_SECRET"       : "update",
"VERBOSITY"       : 2
}
}
]

配置完成后 pm2 start app.json

也可以 pm2 list 查看运行状态

pm2 kill 杀死进程
pm2 logs 查看日志

TODO

  • eth-net-intelligence-api会报下面的错误,试着解决他
[eth] xx> getLatestBlock couldn't fetch block...
0|node-app  | 2020-10-28 21:58 +08:00: [eth] xx> Error: invalid argument 0: hex number > 64 bits
  • 外部节点加入,多节点多监控
  • 用docker来包装,便于迁移

GTTC这个开源DPOS框架,改变了很多地址长度之类的信息,所以居然不能和metaMask交互,弃用,转meitu试试。

eth-net-intelligence-api 报错也是因为这个原因,害得我浪费了很多天时间

以太坊DPOS私链搭建

遇到的一些坑

  • bootnode 一直不起作用,所以决定不用它,改用static-nodes.json

在阿里云机器上编译gttc

  • 系统Ubuntu16.04

  • 想把eth服务部署到docker中方便移植,但是gttc没有官方docker,所以先把环境搭起来,然后自己创建一个docker镜像

  1. 安装go语言环境,版本go 1.14.10

    注意需要先在电脑上下载好,再传到服务器,不然没法解压。

    tar -C usr/local/ -xzf go1.14.10.linux-amd64.tar.gz

  2. 设置环境变量goPath

    mkdir -p ~/go

    sudo vim ~/.profile

    在文件后添加:

    export GOPATH=$HOME/go

    export PATH= P A T H : PATH: PATH:HOME/go/bin:/usr/local/go/bin

    source ~/.profile

下载gttc源文件,并且进行编译:

Sudo apt-get update

Sudo apt-get install git

go get github/TTCECO/gttc

编译gttc:

make gttc

make all (注意,文档没写这一步)

启动一个私有链

设置gttc环境变量

vim ~/.profile

export PATH= P A T H : PATH: PATH:HOME/go/src/github/TTCECO/gttc/build/bin

source ~/.profile

创建节点数据目录

 mkdir devnetcd devnetmkdir node1 node2

创建2个节点(node1 node2)

gttc --datadir node1/ account new
gttc --datadir node2/ account new

将节点信息写入文件(为了初始化genesis.json)

  • 注意替换为自己的地址和密码
echo 'node1_address' >> account.txt
echo 'node2_address' >> account.txt
echo '123' >> node1/password.txt
echo '123' >> node2/password.txt

用puppeth工具构建genesisi.son

puppeth

注意替换相应节点地址:

+-----------------------------------------------------------+
| Welcome to puppeth, your private network manager          |
|                                                           |
| This tool lets you create a new Ethereum network down to  |
| the genesis block, bootnodes, miners and ethstats servers |
| without the hassle that it would normally entail.         |
|                                                           |
| Puppeth uses SSH to dial in to remote servers, and builds |
| its network components out of Docker containers using the |
| docker-compose toolset.                                   |
+-----------------------------------------------------------+Please specify a network name to administer (no spaces or hyphens, please)
> devnetSweet, you can set this via --network=devnet next time!INFO [06-04|12:33:34] Administering Ethereum network           name=devnet
WARN [06-04|12:33:34] No previous configurations found         path=/Users/tataufo/.puppeth/devnetWhat would you like to do? (default = stats)1. Show network stats2. Configure new genesis3. Track new remote server4. Deploy network components
> 2Which consensus engine to use? (default = alien)1. Ethash - proof-of-work2. Clique - proof-of-authority3. Alien  - delegated-proof-of-stake
> 3How many seconds should blocks take? (default = 3)
> 4How many blocks create for one epoch? (default = 30000)
> 30What is the max number of signers? (default = 21)
> 3What is the minimize balance for valid voter ? (default = 10000TTC)
> 100How many minutes delay to create first block ? (default = 5 minutes)
> 5Which accounts are vote by themselves to seal the block?(least one, those accounts will be auto pre-funded)
(The follow two address can be found in account.txt)
> 0xfa846876ef5ed3826e483303f42d987a66af8e15 
> 0x62739566c666df9a057d7e7c92898511d4e64c07
> 0xWhich accounts should be pre-funded? (advisable at least one)
> 0xSpecify your chain/network ID if you want an explicit one (default = random)
>
INFO [06-04|12:35:27] Configured new genesis blockWhat would you like to do? (default = stats)1. Show network stats2. Manage existing genesis3. Track new remote server4. Deploy network components
> 21. Modify existing fork rules2. Export genesis configuration3. Remove genesis configuration
> 2Which file to save the genesis into? (default = devnet.json)
> genesis.json
INFO [06-04|12:35:45] Exported existing genesis blockWhat would you like to do? (default = stats)1. Show network stats2. Manage existing genesis3. Track new remote server4. Deploy network components
> ^C

写创世区块

gttc --datadir node1/ init genesis.json
gttc --datadir node2/ init genesis.json

bootnode配置(用于节点发现)

  • 我没采用这个方法,所以跳过这个

  • 替换ip地址

bootnode -genkey boot.key
nohup bootnode -nodekey boot.key -verbosity 9 -addr 172.17.34.49:30310 &

当bootnode用不了时,用这种方法:

  • 启动节点1

    gttc --datadir node1/ --networkid 7473 --syncmode 'full' --port 30311 --rpc --identity "node1" --rpcport 8501 --rpcapi 'personal,db,eth,net,web3,txpool,miner,net,admin' --bootnodes  --gasprice '1' -unlock 'cb2c0acc61dbebc8ba03feb506fa5d6562bd4e99' --password node1/password.txt console
    

    记录节点1的 self encode值

  • 同样的方法记录节点2

  • 在两节点的gttc目录下,分别创建static-nodes.json文件,并同时写入(注意替换为两节点的encode):

    [
    "enode://ddd46647bf6f3ea049d065b2cc997a68c2f88dea33f200a62aac13169fa41798ae652cd4cb28e2743e13bf3b7390650fa31257424687ef06f17cb4d486cb7e5e@127.0.0.1:30312"
    ,
    "enode://f74b2c18a839d00dd9538c61198eb365b4d22628dfd305053e7344b901444615f6523eb0b8e86ea7af8a8ad6b368d699258db9a24a3a09ab6697af16bb22e8fe@127.0.0.1:30311"
    ]
    
  • 现在可以同步了

启动节点1 节点2

  • nohup后台启动
  • 注意替换地址
  • 注意替换 enode address为刚才bootnode命令后的终端输出
  • networkid 是genesis.json 中的chainId
  • 若要开启外网访问,要将rpcaddr设为服务器独立0.0.0.0
nohup gttc --datadir node1/ --networkid 7473 --syncmode 'full' --port 30311 --rpc --identity "node1" --rpcport 8501 --rpcapi 'personal,db,eth,net,web3,txpool,miner,net,admin'  -unlock 'cb2c0acc61dbebc8ba03feb506fa5d6562bd4e99' --password node1/password.txt --mine  &
nohup gttc --datadir node2/ --networkid 7473 --syncmode 'full' --port 30312 --rpc  --rpcport 8502 --identity "node2" --rpcapi 'personal,db,eth,net,web3,txpool,miner,net,admin' -unlock 'd2159a31e594c61171b5d6690656d6dcf8ef872e' --password node2/password.txt --mine &
参数名称参数描述
identity区块链的标示,用于标示目前网络的名字
nodiscover私有链地址,不会被网上看到
datadir设置当前区块链网络数据存放的位置
rpc开启rpc通道
rpcapi要开放哪些rpc api,默认eth,net,web3
rpccorsdomain允许能连接到你的节点执行rpc api的url,使用逗号分隔。*表示任何url都可以连接
rpcaddrHTTP-RPC服务器接口地址,默认为localhost
rpcportHTTP-RPC服务器端口地址,默认为8545(多节点时不要重复)
networkid网络标识,私有链取一个大于4的随意的值
console启动命令行模式,可以在Geth中执行命令
verbosity日志详细度:0=silent, 1=error, 2=warn, 3=info, 4=debug, 5=detail(default: 3) 2>> eth.log 表示把控制台日志输出到eth.log文件。新开一个命令行终端,运行tail -f geth.log命令来实时显示日志,这样日志信息就不会出现在终端中,让你在终端运行命令的时候可读性较差

安装配置netstats 用于查看区块链状态

安装nodejs和npm

1、先在系统上安装好nodejs和npm

sudo apt-get install nodejs-legacy

sudo apt-get install npm

2、安装用于安装nodejs的模块n

sudo npm install -g n

3、通过n模块安装指定的nodejs

sudo n latest

sudo n stable

sudo n lts

4、升级npm为最新版本

sudo npm install npm@latest -g

5、查看版本

sudo node -v

sudo npm -v

6、设成淘宝的镜像地址

npm config set registry /

安装netstats

  • netstats只是一个客户端,还需要eth-net-intelligence-api作为服务端给它发信息
git clone 
cd eth-netstats
npm install
sudo npm install -g grunt-cli
grunt all
nohup npm start &

接着在项目根目录底下建立文件 ws_secret.json
这一步很多文档没讲明白,这个文件是为了和eth-net-intelligence-api连起来

{
"WS_SECRET": "update"
}

现在私链已经启动并且有2个节点开始挖矿,但是eth-netstats还不起作用。下面配置它

是因为没有安装客户端信息中继,下面安装

bash <(curl .sh)

修改bin/process.json

[
{"name"              : "eth","cwd"               : "./www/","script"            : "bin/eth.sh","args"              : "ethpassword","log_date_format"   : "YYYY-MM-DD HH:mm Z","log_file"          : "./logs/eth-log.log","out_file"          : "./logs/eth-out.log","error_file"        : "./logs/eth-err.log","merge_logs"        : false,"watch"             : false,"max_restarts"      : 10,"exec_interpreter"  : "bash","exec_mode"         : "fork_mode"
},
{"name"              : "node-app","cwd"               : "./www/","script"            : "app.js","log_date_format"   : "YYYY-MM-DD HH:mm Z","log_file"          : "./logs/node-app-log.log","out_file"          : "./logs/node-app-out.log","error_file"        : "./logs/node-app-err.log","merge_logs"        : false,"watch"             : false,"max_restarts"      : 10,"exec_interpreter"  : "node","exec_mode"         : "fork_mode","env":{"NODE_ENV"        : "production","RPC_HOST"        : "localhost","RPC_PORT"        : "8545","LISTENING_PORT"  : "30303","INSTANCE_NAME"   : "","CONTACT_DETAILS" : "","WS_SERVER"       : "ws://localhost:3000","WS_SECRET"       : "update","VERBOSITY"       : 2}
}
]

修改www/app.json:

[
{
"name"              : "node-app",
"script"            : "app.js",
"log_date_format"   : "YYYY-MM-DD HH:mm Z",
"merge_logs"        : false,
"watch"             : false,
"max_restarts"      : 10,
"exec_interpreter"  : "node",
"exec_mode"         : "fork_mode",
"env":
{
"NODE_ENV"        : "production",
"RPC_HOST"        : "localhost",
"RPC_PORT"        : "8545",
"LISTENING_PORT"  : "30303",
"INSTANCE_NAME"   : "hank",
"CONTACT_DETAILS" : "",
"WS_SERVER"       : "ws://localhost:3000",
"WS_SECRET"       : "update",
"VERBOSITY"       : 2
}
}
]

配置完成后 pm2 start app.json

也可以 pm2 list 查看运行状态

pm2 kill 杀死进程
pm2 logs 查看日志

TODO

  • eth-net-intelligence-api会报下面的错误,试着解决他
[eth] xx> getLatestBlock couldn't fetch block...
0|node-app  | 2020-10-28 21:58 +08:00: [eth] xx> Error: invalid argument 0: hex number > 64 bits
  • 外部节点加入,多节点多监控
  • 用docker来包装,便于迁移

GTTC这个开源DPOS框架,改变了很多地址长度之类的信息,所以居然不能和metaMask交互,弃用,转meitu试试。

eth-net-intelligence-api 报错也是因为这个原因,害得我浪费了很多天时间

本文标签: 以太坊DPOS私链搭建