🐧Mining on Linux

script was made for Docker but can be used for Geth *USE sudo everytime you call docker command*

Pre-requisites

Pre-Requisites :

Ensure TCP Port 30303 is Open

Sudo ufw enable

sudo ufw allow from any to any port 30303 proto tcp

sudo ufw allow from any to any port 30303 proto udp

sudo apt-get update
sudo apt-get install -y
ca-certificates
curl
gnupg
lsb-release
sudo mkdir -p /etc/apt/keyrings curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
echo
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu
$(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update sudo apt-get install -y docker-ce docker-ce-cli containerd.io docker-compose-plugin
sudo groupadd docker 
sudo usermod -aG docker $USER

Variable Declaration :

CHAIN_NAME="berylbit" 
CHAIN_ID="9012" 
NETWORK_ID="9012" 
WORKING_DIR=$HOME'/.'$CHAIN_NAME'-docker' 
CONTAINER_NAME_PREFIX="$CHAIN_NAME-node" 
BOOTNODE_URL="enode://6c2cc2b8184b5fd75e7ae6c00ae5bfc64e505089ad8fb018306845f5a4d94bd896a38c065f5ae480e456ff8fea4e4429c1f05640539c98ab6de8439a293370c0@45.63.108.190:30303" 
NODE_ROLE="miner" 
CONTAINER_IMAGE="berylbit/go-ethereum:0.0.4" 
WALLET_PASSWORD="xxxxxxxxxxx"

Please alter this value of "xxxxxxxxxxx"

Initialize the blockchain

Create new folder as working directory

mkdir -p $WORKING_DIR/$NODE_ROLE

Create new genesis.json file

cat << EOF > $WORKING_DIR/$NODE_ROLE/genesis.json
{
  "config": {
    "chainId": $CHAIN_ID,
    "homesteadBlock": 0,
    "eip150Block": 0,
    "eip155Block": 0,
    "eip158Block": 0,
    "byzantiumBlock": 0,
    "constantinopleBlock": 0,
    "petersburgBlock": 0,
    "istanbulBlock": 0,
    "berlinBlock": 0,
    "londonBlock": 0
  },
  "alloc": {
    "0x768A5e67b3E8cf3c5562dd221b12B62438F43184": {
      "balance": "300000000000000000000000000"
    }
  },
  "coinbase": "0x0000000000000000000000000000000000000000",
  "difficulty": "0x20000",
  "extraData": "",
  "gasLimit": "0x2fefd8",
  "nonce": "0x0000000000000042",
  "mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000",
  "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
  "timestamp": "0x00"
}
EOF
docker run
-v $WORKING_DIR/$NODE_ROLE:/root
--rm
-i
--platform linux/amd64
$CONTAINER_IMAGE init /root/genesis.json

Create new mining wallet

MINING_WALLET_ADDRESS=$(echo "$WALLET_PASSWORD $WALLET_PASSWORD" | docker run
-v $WORKING_DIR/$NODE_ROLE:/root
--rm
-i
--platform linux/amd64
$CONTAINER_IMAGE
account new |
sed -n 's/Public address of the key: //p'
)

Display the value of $MINING_WALLET_ADDRESS in Linux terminal

echo $MINING_WALLET_ADDRESS
docker kill $CONTAINER_NAME_PREFIX-$NODE_ROLE
docker rm $CONTAINER_NAME_PREFIX-$NODE_ROLE
docker run -d --name $CONTAINER_NAME_PREFIX-$NODE_ROLE
-v $WORKING_DIR/$NODE_ROLE:/root
--platform linux/amd64
-p 30303:30303
-p 30303:30303/udp
$CONTAINER_IMAGE
--port=30303
--mine
--miner.threads=1
--miner.etherbase $MINING_WALLET_ADDRESS
--networkid=$NETWORK_ID
--bootnodes=$BOOTNODE_URL
PEERS=$(docker run
-v $WORKING_DIR/$NODE_ROLE:/root
--rm
-i
--platform linux/amd64
$CONTAINER_IMAGE attach
--exec "admin.peers" | tr -d '"')
echo $PEERS
PEERS=$(docker run \
  -v $WORKING_DIR/$NODE_ROLE:/root \
  --rm \
  -i \
  --platform linux/amd64 \
  $CONTAINER_IMAGE attach \
  --exec "eth.sync" | tr -d '"')

Check Mining Wallet Balance

PEERS=$(docker run \
  -v $WORKING_DIR/$NODE_ROLE:/root \
  --rm \
  -i \
  --platform linux/amd64 \
  $CONTAINER_IMAGE attach \
  --exec "eth.getBalance('xxxxxxxxxxxxxxxxxxxxxxxx')"

please replace xxxxxxxxxxxxxxxx string with your mining wallet.

Last updated