running a prive ethereum blockchain using docker
===
###### tags: `docker`
[TOC]
## docker compose
1.create ``genesis.json`` file
```json=
{
"config": {
"chainId": 1214,
"homesteadBlock": 0,
"eip150Block": 0,
"eip155Block": 0,
"eip158Block": 0,
"byzantiumBlock": 0,
"constantinopleBlock": 0,
"petersburgBlock": 0,
"ethash": {}
},
"difficulty": "1",
"gasLimit": "12000000",
"alloc": {}
}
```
2. create ``Dockerfile`` file
```dockerfile=
FROM ethereum/client-go:v1.10.1
ARG ACCOUNT_PASSWORD
COPY genesis.json /tmp
RUN geth init /tmp/genesis.json \
&& rm -f ~/.ethereum/geth/nodekey \
&& echo ${ACCOUNT_PASSWORD} > /tmp/password \
&& geth account new --password /tmp/password \
&& rm -f /tmp/password
ENTRYPOINT ["geth"]
```
3. create ``docker-compose.yaml`` file
```yaml=
version: '3.7'
services:
geth-bootnode:
hostname: geth-bootnode
env_file:
- .env
image: geth-client
build:
context: .
args:
- ACCOUNT_PASSWORD=${ACCOUNT_PASSWORD}
command:
--nodekeyhex="b0ac22adcad37213c7c565810a50f1772291e7b0ce53fb73e7ec2a3c75bc13b5"
--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
depends_on:
- geth-bootnode
command:
--bootnodes="enode://af22c29c316ad069cf48a09a4ad5cf04a251b411e45098888d114c6dd7f489a13786620d5953738762afa13711d4ffb3b19aa5de772d8af72f851f7e9c5b164a@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
depends_on:
- geth-bootnode
command:
--bootnodes="enode://af22c29c316ad069cf48a09a4ad5cf04a251b411e45098888d114c6dd7f489a13786620d5953738762afa13711d4ffb3b19aa5de772d8af72f851f7e9c5b164a@geth-bootnode:30303"
--mine
--miner.threads=1
--networkid=${NETWORK_ID}
--netrestrict="172.16.254.0/28"
networks:
priv-eth-net:
networks:
priv-eth-net:
driver: bridge
ipam:
config:
- subnet: 172.16.254.0/28
```
4. create ``.env`` file
```env=
# The ID of Ethereum Network
NETWORK_ID=1214
# The password to create and access the primary account
ACCOUNT_PASSWORD=5uper53cr3t
```
5. run docker compose ``docker-compose up``
6. running in background``docker-compose up -d``
## HTTP JSON-RPC API
### command
```shell=
curl --location --request POST 'http://localhost:8545' \
--header 'Content-Type: application/json' \
--data-raw ''
```
### eth_sendTransaction
```shell=
curl --location --request POST 'http://localhost:8545' \
--header 'Content-Type: application/json' \
--data-raw '{
"jsonrpc": "2.0",
"method": "eth_sendTransaction",
"params: [{
from: "0xb60e8dd61c5d32be8058bb8eb970870f07233155",
to: "0xd46e8dd67c5d32be8058bb8eb970870f07244567",
gas: "0x76c0", // 30400
gasPrice: "0x9184e72a000", // 10000000000000
value: "0x9184e72a", // 2441406250
data: "0xd46e8dd67c5d32be8d46e8dd67c5d32be8058bb8eb970870f072445675058bb8eb970870f072445675",
}
],
"id": 1
}'
```
### eth_getBlockByNumber
```shell=
curl --location --request POST 'http://localhost:8545' \
--header 'Content-Type: application/json' \
--data-raw '{
"jsonrpc":"2.0",
"method":"eth_getBlockByNumber",
"params":["0x1b4", true],
"id":1
}'
```
### eth_getBlockByHash
```shell=
curl --location --request POST 'http://localhost:8545' \
--header 'Content-Type: application/json' \
--data-raw'{
"jsonrpc":"2.0",
"method":"eth_getBlockByHash",
"params":["0xdc0818cf78f21a8e70579cb46a43643f78291264dda342ae31049421c82d21ae",false],
"id":1
}'
```
### eth_getTransactionByHash
```shell=
curl --location --request POST 'http://localhost:8545' \
--header 'Content-Type: application/json' \
--data-raw'{
"jsonrpc":"2.0",
"method":"eth_getTransactionByHash",
"params":["0x88df016429689c079f3b2f6ad39fa052532c56795b733da78a91ebe6a713944b"],
"id":1
}'
```
### admin_peers
```shell=
curl --location --request POST 'localhost:8545' \
--header 'Content-Type: application/json' \
--data-raw '{
"jsonrpc": "2.0",
"id": 1,
"method": "admin_peers",
"params": []
}'
```
### eth_blockNumbe
```shell=
curl --location --request POST 'localhost:8545' \
--header 'Content-Type: application/json' \
--data-raw '{
"jsonrpc": "2.0",
"id": 2,
"method": "eth_blockNumber",
"params": []
}'
```
### eth_accounts
```shell=
curl --location --request POST 'localhost:8545' \
--header 'Content-Type: application/json' \
--data-raw '{
"jsonrpc": "2.0",
"id": 3,
"method": "eth_accounts",
"params": []
}'
```
### eth_getBalance
```shell=
curl --location --request POST 'localhost:8545' \
--header 'Content-Type: application/json' \
--data-raw '{
"jsonrpc": "2.0",
"id": 4,
"method": "eth_getBalance",
"params": [
"0x08d1f47128f5c04d7a4aee69e90642645059acd6",
"latest"
]
}'
```
### personal_newAccount
```shell=
curl --location --request POST 'http://localhost:8545' \
--header 'Content-type: application/json' \
--data-raw '{
"jsonrpc": "2.0",
"id": 5,
"method": "personal_newAccount",
"params": [
"5uper53cr3t"
]
}'
```
### personal_unlockAccount
```shell=
curl --location --request POST 'http://localhost:8545' \
--header 'Content-type: application/json' \
--data-raw '{
"jsonrpc": "2.0",
"id": 6,
"method": "personal_unlockAccount",
"params": [
"0x08d1f47128f5c04d7a4aee69e90642645059acd6",
"5uper53cr3t"
]
}'
```
### eth_sendTransaction
```shell=
curl --location --request POST 'localhost:8545' \
--header 'Content-Type: application/json' \
--data-raw '{
"jsonrpc": "2.0",
"id": 7,
"method": "eth_sendTransaction",
"params": [
{
"from": "0x08d1f47128f5c04d7a4aee69e90642645059acd6",
"to": "0x2bc05c71899ecff51c80952ba8ed444796499118",
"value": "0xf4240"
}
]
}'
```
### eth_getTransactionByHash
```shell=
curl --location --request POST 'localhost:8545' \
--header 'Content-Type: application/json' \
--data-raw '{
"jsonrpc": "2.0",
"id": 8,
"method": "eth_getTransactionByHash",
"params": [ "0xa96de080dfcb9c5f908528b92d3df55a0e230cf4e48ae178bb220862c2a544c7"
]
}'
```
### eth_getBalance
```shell=
curl --location --request POST 'localhost:8545' \
--header 'Content-Type: application/json' \
--data-raw '{
"jsonrpc": "2.0",
"id": 9,
"method": "eth_getBalance",
"params": [
"0x2bc05c71899ecff51c80952ba8ed444796499118",
"latest"
]
}'
```
### extara data
```shell=
curl --location --request POST 'http://localhost:8545' \
--header 'Content-Type: application/json' \
--data-raw '{
"jsonrpc":"2.0",
"method":"miner_setExtra",
"params":["minerstring"],
"id":1
}'
```
### clientVersion
```shell=
curl --location --request POST 'http://localhost:8545' \
--header 'Content-Type: application/json' \
--data-raw '{
"jsonrpc":"2.0",
"method":"web3_clientVersion",
"params":[],
"id":1
}'
```
### net version
```shell=
curl --location --request POST 'http://localhost:8545' \
--header 'Content-Type: application/json' \
--data-raw '{
"jsonrpc":"2.0",
"method":"net_version",
"params":[],
"id":1
}'
```
### peerCount
```shell=
curl --location --request POST 'http://localhost:8545' \
--header 'Content-Type: application/json' \
--data-raw '{
"jsonrpc":"2.0",
"method":"net_peerCount",
"params":[],
"id":1
}'
```
### list account
```shell=
curl --location --request POST 'http://localhost:8545' \
--header 'Content-Type: application/json' \
--data-raw '{
"jsonrpc":"2.0",
"method":"personal_listAccounts",
"params":[],
"id":1
}'
```
### 數據目錄
```shell=
curl --location --request POST 'http://localhost:8545' \
--header 'Content-Type: application/json' \
--data-raw '{
"jsonrpc":"2.0",
"method":"admin_datadir",
"params":[],
"id":1
}'
```
### node info
```shell=
curl --location --request POST 'http://localhost:8545' \
--header 'Content-Type: application/json' \
--data-raw '{
"jsonrpc":"2.0",
"method":"admin_nodeInfo",
"params":[],
"id":1
}'
```
## 轉帳流程
1. 解鎖帳戶
```shell=
curl --location --request POST 'http://localhost:8545' \
--header 'Content-type: application/json' \
--data-raw '{
"jsonrpc": "2.0",
"id": 6,
"method": "personal_unlockAccount",
"params": [
"0xfc316d1a4ac59b1edf0056357b02d108fe09f2c7",
"5uper53cr3t"
]
}'
```
2. 發起交易
```shell=
curl --location --request POST 'localhost:8545' \
--header 'Content-Type: application/json' \
--data-raw '{
"jsonrpc": "2.0",
"id": 7,
"method": "eth_sendTransaction",
"params": [
{
"from": "0xfc316d1a4ac59b1edf0056357b02d108fe09f2c7",
"to": "0x3d731511695dfad51de0a145a96e7da699afd5e3",
"data": "0xd46e8dd67c5d32be8d46e8dd67c5d32be8058bb8eb970870f072445675058bb8eb970870f072445675"
}
]
}'
```
3. 用hash查詢交易
```shell=
curl --location --request POST 'localhost: 8545' \
--header 'Content-Type: application/json' \
--data-raw '{
"jsonrpc": "2.0",
"id": 8,
"method": "eth_getTransactionByHash",
"params": [
"0xfc43cb5d92f5ccd003d78331ff7b6928a5e131ba702e7f47845c15c2dddafe2b"
]
}'
```