一. 以太坊私鏈鏡像生成
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目錄下編寫執(zhí)行文件
# 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的參數(shù)。這標志著你的以太坊網(wǎng)絡的身份。我們在這個例子中使用了66666,應該選擇一個隨機數(shù)來創(chuàng)建你自己的網(wǎng)絡并防止其他人無意中連接到你的網(wǎng)絡,此ID也最好與下文genesis.json文件中的"chainId"的ID一致
rpcaddr參數(shù),含義為指定rpc服務器地址,如果目前只有這一臺礦工發(fā)服務器,必須使用0.0.0.0這個地址,不然無法在宿主機外使用curl命令調(diào)用rpc遠程服務調(diào)用協(xié)議來查詢用戶余額
--allow-insecure-unlock:允許解鎖賬戶
1.5 為文件賦予執(zhí)行權(quán)限
chmod +x /opt/docker/images/geth-1.10.5/bin/start.sh
1.6 生成以太坊私鏈鏡像
docker build . -t eth:v1.10.5
注:需要在與Dockerfile同一級目錄下執(zhí)行此命令
- 查看鏡像是否構(gòu)建
# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
eth v1.10.5-test 5ecb7e4dd6dd 3 hours ago 49.9MB
二.以太坊私鏈容器生成
2.1 所需文件準備
- 預先創(chuàng)建一個賬戶地址
使用MetaMask或者其他工具預先創(chuàng)建一個賬戶地址,并保存好私鑰,后面這個地址需要作為啟動挖礦的coinbase地址
- 新建需要映射給容器存放數(shù)據(jù)的目錄,此目錄下存放著的是所有在此鏈上的交易信息
mkdir -p /opt/docker/eth/data/chain/
- 創(chuàng)建礦工地址keystore存放文件夾,并將預先生成地址的keystore文件放進去
mkdir /opt/docker/eth/data/chain/keystore
- 新建需要映射給容器存放DAG數(shù)據(jù)的目錄,必須創(chuàng)建,不然每次啟動容器都會先生成DAG數(shù)據(jù)
mkdir -p /opt/docker/eth/data/ethash
- 新建創(chuàng)始區(qū)塊文件,此文件是搭建以太坊私鏈的創(chuàng)世區(qū)塊文件
# 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、為了創(chuàng)建我們的私有區(qū)塊鏈,我們將創(chuàng)建一個創(chuàng)世塊。為此,我們將創(chuàng)建一個自定義的Genesis文件,并要求Geth使用該genesis文件創(chuàng)建我們自己的genesis塊,這反過來將成為我們自定義私有區(qū)塊鏈的開始。
2、genesis.json文件屬性詳解
config:配置塊定義我們的自定義鏈的設置,并具有創(chuàng)建私有區(qū)塊鏈的某些屬性。
chainId :標識我們的區(qū)塊鏈,主要的以太坊鏈有自己的ID,但我們會將它設置為我們私有鏈的唯一值。
homesteadBlock:Homestead是以太坊平臺的第二個主要版本,也是以太坊的第一個生產(chǎn)版本。它包括幾個協(xié)議更改。由于我們已經(jīng)在Homestead版本,因此該屬性為0。- eip155Block/eip158Block:Homestead版本發(fā)布時帶有一些向后不兼容的協(xié)議更改,因此需要硬分叉。通過以太坊改進提案(EIPs)提出的這些協(xié)議變更/改進。然而,我們的鏈條不會因為這些變化而難以分解,所以保留為0。
difficulty:此值用于控制區(qū)塊鏈的塊生成時間。難度越高,Miner在發(fā)現(xiàn)有效塊時必須執(zhí)行的統(tǒng)計更多計算。在我們的測試網(wǎng)絡中,我們將保持此值低以避免在測試期間等待,因為需要生成有效塊來執(zhí)行交易處理區(qū)塊鏈。
gasLimit:此值指定每塊的“gas”支出的當前鏈范圍限制。gas是以太坊在交易過程中消耗的燃料。我們將在這種情況下將此值標記得足夠高,以避免在測試期間受到限制。
alloc:這是你可以創(chuàng)建你的錢包并用假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數(shù)據(jù)
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"