docker 部署以太坊私链v1.10.5

docker 部署以太坊私链v1.10.5

一. 以太坊私链镜像生成

1.1 下载以太坊基础镜像

docker pull  ethereum/client-go:v1.10.5

1.2 编写Dockerfile

# vim /opt/docker/images/geth-1.10.5/Dockerfile

FROM ethereum/client-go:v1.10.5

RUN apk update && apk add bash curl

ADD bin /root/bin
RUN chmod a+x /root/bin/*

ENTRYPOINT /root/bin/start.sh

1.3 新建存放目录—bin

mkdir /opt/docker/images/geth-1.10.5/bin

1.4 bin目录下编写执行文件

# vim /opt/docker/images/geth-1.10.5/bin/start.sh

#!/bin/bash
set -e

# Init
echo ""
echo "Init geth"
geth init "/root/files/genesis.json"
sleep 3

# Start geth
echo ""
echo "Start geth"
geth --gcmode "archive" --networkid=666666 --rpc --rpcapi "db,eth,net,web3,personal,admin,miner" --rpcaddr "0.0.0.0" --rpcport "8545"  --miner.threads 1 --mine --allow-insecure-unlock & 

sleep 10

while true; do
    sleep 1000000000
done

注:

注意,以上指定了一个名为networkid的参数。这标志着你的以太坊网络的身份。我们在这个例子中使用了66666,应该选择一个随机数来创建你自己的网络并防止其他人无意中连接到你的网络,此ID也最好与下文genesis.json文件中的"chainId"的ID一致

rpcaddr参数,含义为指定rpc服务器地址,如果目前只有这一台矿工发服务器,必须使用0.0.0.0这个地址,不然无法在宿主机外使用curl命令调用rpc远程服务调用协议来查询用户余额

--allow-insecure-unlock:允许解锁账户

1.5 为文件赋予执行权限

chmod +x /opt/docker/images/geth-1.10.5/bin/start.sh

1.6 生成以太坊私链镜像

docker build . -t eth:v1.10.5

注:需要在与Dockerfile同一级目录下执行此命令

  • 查看镜像是否构建
# docker images
REPOSITORY                          TAG                 IMAGE ID            CREATED             SIZE
eth                                 v1.10.5-test        5ecb7e4dd6dd        3 hours ago         49.9MB

二.以太坊私链容器生成

2.1 所需文件准备

  • 预先创建一个账户地址

使用MetaMask或者其他工具预先创建一个账户地址,并保存好私钥,后面这个地址需要作为启动挖矿的coinbase地址

  • 新建需要映射给容器存放数据的目录,此目录下存放着的是所有在此链上的交易信息
mkdir -p /opt/docker/eth/data/chain/
  • 创建矿工地址keystore存放文件夹,并将预先生成地址的keystore文件放进去
mkdir /opt/docker/eth/data/chain/keystore
  • 新建需要映射给容器存放DAG数据的目录,必须创建,不然每次启动容器都会先生成DAG数据
mkdir -p /opt/docker/eth/data/ethash
  • 新建创始区块文件,此文件是搭建以太坊私链的创世区块文件
# vim /opt/docker/eth/genesis.json

{
  "config": {
    "chainId": 666666,
    "homesteadBlock": 0,
    "eip150Block": 0,
    "eip155Block": 0,
    "eip158Block": 0,
    "byzantiumBlock": 0,
    "constantinopleBlock": 0,
    "petersburgBlock": 0,
    "istanbulBlock": 0
  },
  "nonce": "0x0000000000000046",
  "mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000",
  "difficulty": "0x400",
  "coinbase": "0x3333333333333333333333333333333333333333",
  "timestamp": "0x0",
  "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
  "extraData": "0x",
  "gasLimit": "0x800000000000",
  "alloc": {
       "0x6e60F5243e1a3F0Be3F407b5AFE9e5395ee82aa2":{
       "balance": "6660010000000000000000000000"
    }
  }
}

注:

1、为了创建我们的私有区块链,我们将创建一个创世块。为此,我们将创建一个自定义的Genesis文件,并要求Geth使用该genesis文件创建我们自己的genesis块,这反过来将成为我们自定义私有区块链的开始。

2、genesis.json文件属性详解

config:配置块定义我们的自定义链的设置,并具有创建私有区块链的某些属性。
   chainId :标识我们的区块链,主要的以太坊链有自己的ID,但我们会将它设置为我们私有链的唯一值。 
   homesteadBlock:Homestead是以太坊平台的第二个主要版本,也是以太坊的第一个生产版本。它包括几个协议更改。由于我们已经在Homestead版本,因此该属性为0。- eip155Block/eip158Block:Homestead版本发布时带有一些向后不兼容的协议更改,因此需要硬分叉。通过以太坊改进提案(EIPs)提出的这些协议变更/改进。然而,我们的链条不会因为这些变化而难以分解,所以保留为0。 
   difficulty:此值用于控制区块链的块生成时间。难度越高,Miner在发现有效块时必须执行的统计更多计算。在我们的测试网络中,我们将保持此值低以避免在测试期间等待,因为需要生成有效块来执行交易处理区块链。 
   gasLimit:此值指定每块的“gas”支出的当前链范围限制。gas是以太坊在交易过程中消耗的燃料。我们将在这种情况下将此值标记得足够高,以避免在测试期间受到限制。 
   alloc:这是你可以创建你的钱包并用假ether预填充的地方。但是对于这篇文章,我们将在本地快速挖掘我们的以太,所以我们不使用这个选项。
  • 新建容器启动脚本
# vim /opt/docker/eth/run.sh 

#!/bin/bash

cd $(dirname $0)
SCRIPTS_DIR=$(pwd)

docker rm -f eth-v1-10-5

docker run --name eth-v1.10.5 \
    -v /etc/localtime:/etc/localtime \
    -v /etc/timezone:/etc/timezone   \
    -v ${SCRIPTS_DIR}/genesis.json:/root/files/genesis.json \
    -v ${SCRIPTS_DIR}/data/chain:/root/.ethereum \
    -v ${SCRIPTS_DIR}/data/ethash:/root/.ethash \
    -p 8545:8545 \
    -p 30303:30303 \
    -p 30303:30303/udp \
    -d \
    eth:v1.10.5-test

docker logs -f eth-v1.10.5

2.2 启动以太坊私有链

启动以太坊私有链容器

bash /opt/docker/eth/run.sh
  • 查看以太坊私有链容器启动日志
# docker logs -f eth-v1.10.5 --tail 10

Init geth
INFO [07-16|20:09:52.943] Maximum peer count                       ETH=50 LES=0 total=50
INFO [07-16|20:09:52.943] Smartcard socket not found, disabling    err="stat /run/pcscd/pcscd.comm: no such file or directory"
INFO [07-16|20:09:52.944] Set global gas cap                       cap=50,000,000
INFO [07-16|20:09:52.944] Allocated cache and file handles         database=/root/.ethereum/geth/chaindata cache=16.00MiB handles=16
INFO [07-16|20:09:52.954] Writing custom genesis block 
INFO [07-16|20:09:52.955] Persisted trie from memory database      nodes=1 size=151.00B time="46.085µs" gcnodes=0 gcsize=0.00B gctime=0s livenodes=1 livesize=0.00B
INFO [07-16|20:09:52.955] Successfully wrote genesis state         database=chaindata                      hash=b8214b..1ee203
INFO [07-16|20:09:52.955] Allocated cache and file handles         database=/root/.ethereum/geth/lightchaindata cache=16.00MiB handles=16
INFO [07-16|20:09:52.964] Writing custom genesis block 
INFO [07-16|20:09:52.965] Persisted trie from memory database      nodes=1 size=151.00B time="551.269µs" gcnodes=0 gcsize=0.00B gctime=0s livenodes=1 livesize=0.00B
INFO [07-16|20:09:52.966] Successfully wrote genesis state         database=lightchaindata                      hash=b8214b..1ee203

Start geth
INFO [07-16|20:09:56.004] Maximum peer count                       ETH=50 LES=0 total=50
WARN [07-16|20:09:56.005] The flag --rpc is deprecated and will be removed June 2021, please use --http 
WARN [07-16|20:09:56.005] The flag --rpcaddr is deprecated and will be removed June 2021, please use --http.addr 
WARN [07-16|20:09:56.005] The flag --rpcport is deprecated and will be removed June 2021, please use --http.port 
WARN [07-16|20:09:56.005] The flag --rpcapi is deprecated and will be removed June 2021, please use --http.api 
INFO [07-16|20:09:56.005] Smartcard socket not found, disabling    err="stat /run/pcscd/pcscd.comm: no such file or directory"
WARN [07-16|20:09:56.005] Disable transaction unindexing for archive node 
INFO [07-16|20:09:56.005] Enabling recording of key preimages since archive mode is used 
INFO [07-16|20:09:56.005] Set global gas cap                       cap=50,000,000
INFO [07-16|20:09:56.006] Allocated trie memory caches             clean=307.00MiB dirty=0.00B
INFO [07-16|20:09:56.006] Allocated cache and file handles         database=/root/.ethereum/geth/chaindata cache=512.00MiB handles=524,288
INFO [07-16|20:09:56.042] Opened ancient database                  database=/root/.ethereum/geth/chaindata/ancient readonly=false
INFO [07-16|20:09:56.042] Initialised chain configuration          config="{ChainID: 666666 Homestead: 0 DAO: <nil> DAOSupport: false EIP150: 0 EIP155: 0 EIP158: 0 Byzantium: 0 Constantinople: 0 Petersburg: 0 Istanbul: 0, Muir Glacier: <nil>, Berlin: <nil>, London: <nil>, Engine: unknown}"
INFO [07-16|20:09:56.043] Disk storage enabled for ethash caches   dir=/root/.ethereum/geth/ethash count=3
INFO [07-16|20:09:56.043] Disk storage enabled for ethash DAGs     dir=/root/.ethash               count=2
INFO [07-16|20:09:56.043] Initialising Ethereum protocol           network=666,666 dbversion=<nil>
INFO [07-16|20:09:56.044] Loaded most recent local header          number=0 hash=b8214b..1ee203 td=1024 age=52y3mo2w
INFO [07-16|20:09:56.044] Loaded most recent local full block      number=0 hash=b8214b..1ee203 td=1024 age=52y3mo2w
INFO [07-16|20:09:56.044] Loaded most recent local fast block      number=0 hash=b8214b..1ee203 td=1024 age=52y3mo2w
WARN [07-16|20:09:56.044] Failed to load snapshot, regenerating    err="missing or corrupted snapshot"
INFO [07-16|20:09:56.044] Rebuilding state snapshot 
INFO [07-16|20:09:56.044] Resuming state snapshot generation       root=059cab..4702ee accounts=0 slots=0 storage=0.00B elapsed="286.444µs"
INFO [07-16|20:09:56.045] Regenerated local transaction journal    transactions=0 accounts=0
INFO [07-16|20:09:56.045] Generated state snapshot                 accounts=1 slots=0 storage=50.00B elapsed="571.325µs"
INFO [07-16|20:09:56.045] Gasprice oracle is ignoring threshold set threshold=2
WARN [07-16|20:09:56.045] Error reading unclean shutdown markers   error="leveldb: not found"
INFO [07-16|20:09:56.045] Starting peer-to-peer node               instance=Geth/v1.10.5-stable-33ca98ec/linux-amd64/go1.16.6
INFO [07-16|20:09:56.058] New local node record                    seq=1 id=7313d5dd4188a777 ip=127.0.0.1 udp=30303 tcp=30303
INFO [07-16|20:09:56.059] Started P2P networking                   self=enode://40f8ab628a8adae4d84c08d05a5eae838c59e4a25293a94b9a04f9055e190ce15206926022c1f9f9a91652ee82c4b1abf5898854095534b2c12ff40d3649f4e8@127.0.0.1:30303
。。。。。。
// 生成DAG数据
INFO [07-16|20:09:56.061] Etherbase automatically configured       address=0x6e60F5243e1a3F0Be3F407b5AFE9e5395ee82aa2
INFO [07-16|20:09:56.061] Commit new mining work                   number=1 sealhash=13d73c..b815c0 uncles=0 txs=0 gas=0 fees=0 elapsed="142.504µs"
INFO [07-16|20:09:57.191] Generating DAG in progress               epoch=0 percentage=0 elapsed=393.358ms
。。。。。。
// 开始挖矿
INFO [07-16|20:10:55.278] Successfully sealed new block            number=1 sealhash=13d73c..b815c0 hash=84b731..575dd8 elapsed=59.216s
INFO [07-16|20:10:55.278]  mined potential block                  number=1 hash=84b731..575dd8
INFO [07-16|20:10:55.279] Commit new mining work                   number=2 sealhash=04e653..981300 uncles=0 txs=0 gas=0 fees=0 elapsed="188.882µs"
INFO [07-16|20:10:56.080] Successfully sealed new block            number=2 sealhash=04e653..981300 hash=34b8b5..0b704b elapsed=801.884ms
INFO [07-16|20:10:56.081]  mined potential block                  number=2 hash=34b8b5..0b704b
INFO [07-16|20:10:56.081] Commit new mining work                   number=3 sealhash=a1e558..0977e5 uncles=0 txs=0 gas=0 fees=0 elapsed="161.06µs"

本文参与登链社区写作激励计划 ,好文好收益,欢迎正在阅读的你也加入。

  • 发表于 2021-07-16 23:00
  • 阅读 ( 143 )
  • 学分 ( 2 )
  • 分类:以太坊

你可能感兴趣的文章

相关问题

0 条评论

请先 登录 后评论
toString
toString

4 篇文章, 41 学分