本地部署以太坊私链,通过docker多节点同步进行挖矿、转账

本地部署以太坊私链,通过docker多节点同步进行挖矿、转账

    • 1.准备geth工具
    • 2.预先创建帐户,初始化创始区块
    • 3.配置Dockerfile
    • 4.配置docker-compose
    • 5.启动环境
    • 6.测试单节点挖矿+转账
    • 7.绑定多节点
    • 8.geth常用命令
    • 9.挖矿等待过长问题

1.准备geth工具

https://geth.ethereum.org/downloads/

geth --help

2.预先创建帐户,初始化创始区块

1. 创建密码文件
> echo "111111" > .passwd

2. 为node1创建帐户
> for ((n=0;n<2;n++)); do geth account new --password .passwd --datadir ./node1; done

3. 为node2创建帐户
> for ((n=0;n<2;n++)); do geth account new --password .passwd --datadir ./node2; done

4.创建创世区块 genesis.json 
{
  "config": {
    "chainId": 150,
    "homesteadBlock": 0,
    "eip150Block": 0,
    "eip155Block": 0,
    "eip158Block": 0,
    "byzantiumBlock": 0,
    "constantinopleBlock": 0,
    "petersburgBlock": 0,
    "istanbulBlock": 0
  },
  "alloc": {},
  "coinbase": "0x0000000000000000000000000000000000000000",
  "difficulty": "0x0200",
  "extraData": "",
  "gasLimit": "0x2fefd8",
  "nonce": "0x0000000000000042",
  "mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000",
  "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
  "timestamp": "0x00"
}

4.初始化链数据
> geth init --datadir ./node1 genesis.json 
> geth init --datadir ./node2 genesis.json
节点 挖矿账户 资金账户
node1 0xd8e6708334bdf3e9144a62859c99998caa7df32d 0x23eaf35e78a350041cd56d88a04d7b77d45c9844
node2 0x4beff4fceff14ef1c24956fb81387d21f24ca04b 0xae5ed5d1b2137ebc9304cff4cd6192bbb6ea965a

3.配置Dockerfile

FROM golang:1.10-alpine as builder

RUN apk add --no-cache make gcc musl-dev linux-headers git
RUN git clone --depth 1 --branch release/1.8 https://github.com/ethereum/go-ethereum /go-ethereum
RUN cd /go-ethereum && make all

FROM alpine:latest
RUN apk add --no-cache ca-certificates
COPY --from=builder /go-ethereum/build/bin/* /usr/local/bin/

WORKDIR "/opt"

ENV coinbase=""
ENV datadir=""

CMD exec geth --datadir ./$datadir  --nousb --nodiscover--verbosity=4 --rpc --rpcapi "eth,web3,personal,net,miner,admin,debug,db" --rpcaddr "0.0.0.0" --rpccorsdomain "*" --syncmode=fast --mine --unlock $coinbase --password .passwd

EXPOSE 8545
EXPOSE 30303

4.配置docker-compose

version: "3"

services:
  sealnode-1:
    container_name: sealnode-1
    hostname: sealnode-1
    environment:
      coinbase: d8e6708334bDF3e9144a62859C99998CAa7dF32D
      datadir: node1
    build:
      context: .
    ports:
      - 8545:8545
      - 30303:30303
    volumes:
      - .:/opt
    networks:
      - mynet

  sealnode-2:
    container_name: sealnode-2
    hostname: sealnode-2
    environment:
      coinbase: 4bEfF4FceFf14Ef1C24956Fb81387D21f24CA04b
      datadir: node2
    build:
      context: .
    ports:
      - 8546:8545
      - 30304:30303
    volumes:
      - .:/opt
    networks:
      - mynet

networks:
  default:
    driver: bridge
  mynet:
    driver: bridge

5.启动环境

当前目录结构
├── .passwd
├── docker-compose.yaml
├── dockerfile
├── genesis.json
├── node1
│   ├── geth
│   ├── geth.ipc
│   └── keystore
└── node2
    ├── geth
    ├── geth.ipc
    └── keystore

准备多个终端

终端1 挂起docker作为日志查看
>docker-compose up

终端2 进入geth控制台
> docker-compose ps 
> docker exec -it sealnode-1 /bin/sh
> geth attach node1/geth.ipc 

终端3 改下容器ID重复步骤。

本地部署以太坊私链,通过docker多节点同步进行挖矿、转账_第1张图片

6.测试单节点挖矿+转账

#查看当前节点账户
> eth.accounts
#查看账户1的余额(单位wei转换为Eth)
> web3.fromWei(eth.getBalance(eth.accounts[0]), "ether")
#查看账户2的余额
> web3.fromWei(eth.getBalance(eth.accounts[1]), "ether")
#从账户1转入3ETH给账户2
> eth.sendTransaction({from:eth.accounts[0], to:eth.accounts[1], value:web3.toWei(3, "ether")})
#设置挖矿收益账户
> miner.setEtherbase(eth.accounts[0])
#挖矿(第一次挖要等一阵)
> miner.start()
#停止挖矿
> miner.stop()

本地部署以太坊私链,通过docker多节点同步进行挖矿、转账_第2张图片

7.绑定多节点

另起一终端查看容器的IP
>docker network ls
>docker network inspect testblock_mynet

本地部署以太坊私链,通过docker多节点同步进行挖矿、转账_第3张图片

可知node1为172.18.0.3,node2为172.18.0.2
回到原来的geth控制台里查看 节点唯一标志

node1
> admin.nodeInfo.enode
"enode://53cb3c9cea25b8bc2c6a2deffdc96674a55dcbab658424dcb5da412c4cf2fe3fb443585be3d64169a061381fb8ef19fe47de7c0c59688d01e7e666cc43142357@127.0.0.1:30303"

node2
> admin.nodeInfo.enode
"enode://cae5cf06b4662cb5baaa7919a8ca715a3ed7d737a03c5241b181d6a6240747bf3325441050f871b18ae069e398ec9f720e368ecabfb328b51fb0436cb93446c7@127.0.0.1:30303"

任意绑定节点即可,这里在node1绑定node2
>admin.addPeer("enode://cae5cf06b4662cb5baaa7919a8ca715a3ed7d737a03c5241b181d6a6240747bf3325441050f871b18ae069e398ec9f720e368ecabfb328b51fb0436cb93446c7@172.18.0.2:30303")
#查看绑定的节点
>admin.peers

#查看区块高度一致,一致即完成节点同步。
>eth.blockNumber

8.geth常用命令

//查询某个具体区块
eth.getBlock(0)

//查询交易块
eth.getTransaction("0x7cc7e1df85264332a7535c6b8b883221f2c0ad2b56f2023bc76383977abd40c4")

//转账
eth.sendTransaction({from:sender, to:receiver, value: amount})
eth.sendTransaction({from:eth.coinbase, to:eth.accounts[1], value: web3.toWei(0.05, "ether")})

//解锁
personal.unlockAccount("0xd8e6708334bdf3e9144a62859c99998caa7df32d")

//链接远程私链
geth attach http://127.0.0.1:8545 2>&1 console.log

9.挖矿等待过长问题

Generated ethash verification cache 重复进行。
在这里插入图片描述
状况:DAG初始化到100%后重复初始化。
解决:检查docker stats -a内存、检查是否docker配置是否有跟/root/.ethash冲突、清空/root/.ethash后开启挖矿。

你可能感兴趣的:(区块链)