Search code examples
dockerdocker-composeethereumprivate

Ethereum private chain in docker container hangs out before minging a block


I am working on applying docker containers to my projects which uses ethereum chain. I have an issue with blockchain part, at this moment it hangs out before mining a new block:

private-chain-geth-miner-1         | INFO [05-06|04:51:29.155] Generating DAG in progress               epoch=1 percentage=98 elapsed=1m17.684s
private-chain-geth-miner-1         | INFO [05-06|04:51:30.221] Generating DAG in progress               epoch=1 percentage=99 elapsed=1m18.749s
private-chain-geth-miner-1         | INFO [05-06|04:51:30.223] Generated ethash verification cache      epoch=1 elapsed=1m18.751s

Here is a docker file:

FROM ethereum/client-go:v1.10.1

COPY ./chain/accounts/swap.ethash.genesis.json /tmp

COPY ./chain/accounts/boot.key /tmp

RUN mkdir -p /tmp/keystore

COPY ./chain/accounts/keystore/${NODE_ACCOUNT_PWD_FILENAME} /tmp/keystore

RUN geth --datadir /tmp init /tmp/swap.ethash.genesis.json \
    && rm -f ~/.ethereum/geth/nodekey 

ENTRYPOINT ["geth"]

Here a compose file:

# Blockchain part is based on this materials: 
# https://medium.com/scb-digital/running-a-private-ethereum-blockchain-using-docker-589c8e6a4fe8

version: '3.8'

services:
  geth-bootnode:
    hostname: geth-bootnode
    env_file:
      - .env
    image: geth-client
    build:
      context: .
      args:
        - ACCOUNT_PASSWORD=${ACCOUNT_PASSWORD}
        - NODE_ACCOUNT_PWD_FILENAME=${NODE_1_ACCOUNT_PWD_FILENAME}
        - NODE_ACCOUNT_PWD=${NODE_1_ACCOUNT_PWD}
    command:
      --nodekeyhex="c26d705bd5933bc2be72813f1b74f140aa6b3726d2a71a43b163a5c084e9dcb4"
      --nodiscover
      --ipcdisable
      --networkid=${NETWORK_ID}
      --netrestrict="172.16.254.0/28"
    networks:
      priv-eth-net:

  geth-rpc-endpoint:
    hostname: geth-rpc-endpoint
    env_file:
      - .env
    image: geth-client
    build:
      context: .
      args: 
        - NODE_ACCOUNT_PWD_FILENAME=${NODE_2_ACCOUNT_PWD_FILENAME}
        - NODE_ACCOUNT_PWD=${NODE_2_ACCOUNT_PWD}
    depends_on:
      - geth-bootnode    
    command:
      --bootnodes="enode://a78e253e183c4460bdfdc11ae0798fa225fb431a57c875ff278b58b3f5d1ea7ff1aa240d27252a7d0a7bce958b34f62e4836d59281759fed1069ce781af67ef4@geth-bootnode:30303"
      --allow-insecure-unlock
      --http
      --http.addr="0.0.0.0"
      --http.api="eth,web3,net,admin,personal"
      --http.corsdomain="*"
      --networkid=${NETWORK_ID}
      --netrestrict="172.16.254.0/28"
    ports:
      - "8545:8545"
    networks:
      priv-eth-net:

  geth-miner:
    hostname: geth-miner
    env_file:
      - .env
    image: geth-client
    build:
      context: .
      args: 
        - NODE_ACCOUNT_PWD_FILENAME=${NODE_3_ACCOUNT_PWD_FILENAME}
        - NODE_ACCOUNT_PWD=${NODE_3_ACCOUNT_PWD}
        - ETHERBASE_ACCOUNT=${NODE_3_ACCOUNT}
    depends_on:
      - geth-bootnode
    command:
      --bootnodes="enode://a78e253e183c4460bdfdc11ae0798fa225fb431a57c875ff278b58b3f5d1ea7ff1aa240d27252a7d0a7bce958b34f62e4836d59281759fed1069ce781af67ef4@geth-bootnode:30303"
      --mine
      --miner.threads=1
      --miner.etherbase=0x46aF655eB60F0a863663Fc5D7D5fb11EC8808a51
      --miner.gasprice 1 
      --verbosity 3
      --networkid=${NETWORK_ID}
      --netrestrict="172.16.254.0/28"
    networks:
      priv-eth-net:

    # deploy:
    #   resources:
    #     limits:
    #       cpus: '2'
    #       memory: 1024M

networks:
  priv-eth-net:
    driver: bridge
    ipam:
      config:
      - subnet: 172.16.254.0/28

My .env file:

NETWORK_ID=50101

ACCOUNT_PASSWORD=my_node

NODE_1_ACCOUNT_PWD_FILENAME=UTC--2023-04-21T13-54-40.845472438Z--367103555b34eb9a46d92833e7293d540bfd7143
NODE_1_ACCOUNT_PWD=swapd1

NODE_2_ACCOUNT_PWD_FILENAME=UTC--2023-04-21T13-54-53.297432467Z--1a75262751ac4e6290ec8287d1de823f33036498
NODE_2_ACCOUNT_PWD=swapd2

NODE_3_ACCOUNT_PWD_FILENAME=UTC--2023-04-21T13-55-17.400128588Z--d9df253d1f92dcba495c8b0a9616e9d817bb12b7
NODE_3_ACCOUNT_PWD=swapd3
NODE_3_ACCOUNT=0xd9df253d1f92dcba495c8b0a9616e9d817bb12b7

My genesis.json file:

{
   "config":{
      "chainId":50101,
      "homesteadBlock":0,
      "eip150Block":0,
      "eip150Hash":"0x0000000000000000000000000000000000000000000000000000000000000000",
      "eip155Block":0,
      "eip158Block":0,
      "byzantiumBlock":0,
      "constantinopleBlock":0,
      "petersburgBlock":0,
      "istanbulBlock":0,
      "ethash":{
         
      }
   },
   "nonce":"0x0",
   "timestamp":"0x644256a2",
   "extraData":"0x0000000000000000000000000000000000000000000000000000000000000000",
   "gasLimit":"0x1C9C380",
   "difficulty":"1",
   "mixHash":"0x0000000000000000000000000000000000000000000000000000000000000000",
   "coinbase":"0x0000000000000000000000000000000000000000",
   "alloc":{
      "0x9430b0AD2d1a2D5B30925BF8Dfa89327379868cc":{
         "balance":"0x200000000000000000000000000000000000000000000000000000000000000"
      },
      "0x04fabc66600915ad4572c227cc3917b1c61d371B":{
         "balance":"0x200000000000000000000000000000000000000000000000000000000000000"
      },
      "0x51Ea746CB88935f29d23ddFaB9e3D9f469D8fBF8":{
         "balance":"0x200000000000000000000000000000000000000000000000000000000000000"
      },
      "0x8755eE67882b67DB31BE736dfC0A69858b3aE61b":{
         "balance":"0x200000000000000000000000000000000000000000000000000000000000000"
      },
      "0xd0e36281A8B4852D15010cfb8a68c58fD6c2be6e":{
         "balance":"0x200000000000000000000000000000000000000000000000000000000000000"
      },
      "0x5136C3b985293C10A6882cE9DB4f5719c7C486a3":{
         "balance":"0x200000000000000000000000000000000000000000000000000000000000000"
      },
      "0x46aF655eB60F0a863663Fc5D7D5fb11EC8808a51":{
         "balance":"0x200000000000000000000000000000000000000000000000000000000000000"
      },
      "0xd9df253D1F92dcba495c8b0A9616E9D817BB12B7":{
         "balance":"0x200000000000000000000000000000000000000000000000000000000000000"
      },
      "0x1a75262751ac4E6290Ec8287d1De823F33036498":{
         "balance":"0x200000000000000000000000000000000000000000000000000000000000000"
      },
      "0x367103555b34Eb9a46D92833e7293D540bFd7143":{
         "balance":"0x200000000000000000000000000000000000000000000000000000000000000"
      }
   },
   "number":"0x0",
   "gasUsed":"0x0",
   "parentHash":"0x0000000000000000000000000000000000000000000000000000000000000000",
   "baseFeePerGas":null
}

This blockchain part of my docker compose file is based on this publication. Origin docker file creates a new account for each node, but in my case I pre-populate image with my existing accounts. I had not been sure copy keystore folder (encrypted public key) is enough to add an account to a node, but community reassure me it is enough.

The issue is in docker file which creates an origin image, but I haven't figure out yet why it causes this issue.


Solution

  • I have to unlock account by explicitly stating RUN geth --unlock <YOUR_ACCOUNT_ADDRESS> --password <YOUR_PASSWORD>

    FROM ethereum/client-go:v1.10.1
    
    ARG NODE_ACCOUNT
    ARG NODE_ACCOUNT_PWD
    
    COPY ./chain/accounts/swap.ethash.genesis.json /tmp
    
    COPY ./chain/accounts/boot.key /tmp
    
    RUN mkdir -p /tmp/keystore
    
    COPY ./chain/accounts/keystore/${NODE_ACCOUNT_PWD_FILENAME} /tmp/keystore
    
    RUN geth --datadir /tmp init /tmp/swap.ethash.genesis.json \
        && rm -f ~/.ethereum/geth/nodekey 
    
    RUN echo ${NODE_ACCOUNT_PWD} >> /tmp/pwd.txt
    
    RUN geth --unlock ${NODE_ACCOUNT} --password /tmp/pwd.txt --keystore /tmp/keystore
    
    ENTRYPOINT ["geth"]
    

    It worth to note we have to mirror environment variables defined in the docker-compose.yaml as ARG NODE_ACCOUNT and ARG NODE_ACCOUNT_PWD. They work as entrypoint for variables passed from console or docker-compose.yaml.

    services:
      geth-bootnode:
        hostname: geth-bootnode
        env_file:
          - .env
        image: geth-client
        build:
          context: .
          args:
            - NODE_ACCOUNT_PWD_FILENAME=${NODE_1_ACCOUNT_PWD_FILENAME}
            - NODE_ACCOUNT_PWD=${NODE_1_ACCOUNT_PWD}
            - NODE_ACCOUNT=${NODE_1_ACCOUNT}