[Ethereum] local private network

go-ethereum(이하 geth)을 이용해 로컬 프라이빗 네트워크를 구성합니다.

/Users/{USER_NAME}private-net 폴더를 생성하고 아래의 절차대로 실습을 진행했습니다.

Genesis.json 생성

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
{
"config": {
"chainId": 15,
"homesteadBlock": 0,
"eip150Block": 0,
"eip155Block": 0,
"eip158Block": 0,
"byzantiumBlock": 0,
"constantinopleBlock": 0,
"petersburgBlock": 0
},
"coinbase" : "0x0000000000000000000000000000000000000000",
"difficulty" : "0x1",
"extraData" : "",
"gasLimit" : "0x2fefd8",
"nonce" : "0x0000000000000042",
"mixhash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
"parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
"timestamp" : "0x00",
"alloc": {
"945cd603a6754cb13c3d61d8fe240990f86f9f8a": { "balance": "500000000000000000000000000" },
"66b4e7be902300f9a15d900822bbd8803be87391": { "balance": "500000000000000000000000000" },
"104f0d848da3f760dddadc56fc4ab78305110dba": { "balance": "500000000000000000000000000" },
"addfaa808c59581f04cdadfc0be28ebfb520e839": { "balance": "500000000000000000000000000" },
"450a8a99bf5ad49db301f6068c619de2400de6f7": { "balance": "500000000000000000000000000" }
}
}
  • chaindId
    체인 식별값

  • difficulty
    nonce값을 발견하는 난이도 설정으로 높을수록 블록 생성 속도가 느려짐

  • gasLimit
    블록당 가스 지출의 제한량

  • alloc
    Genesis 블록 생성 시 해당 지갑에 값만큼 이더 배분

password 파일 작성

현재는 실습으로 임의의 내용으로 작성하였습니다.

1
2
3
4
5
blah
blah
blah
blah
blah

geth 초기화

1
2
3
4
5
6
7
8
9
10
11
12
13
% geth --datadir ~/private-net --nodiscover --maxpeers 0 init ~/private-net/genesis.json
INFO [12-09|16:44:01.339] Maximum peer count ETH=0 LES=0 total=0
INFO [12-09|16:44:01.345] Set global gas cap cap=50,000,000
INFO [12-09|16:44:01.346] Allocated cache and file handles database=/Users/devsawd/private-net/geth/chaindata cache=16.00MiB handles=16
INFO [12-09|16:44:01.413] Opened ancient database database=/Users/devsawd/private-net/geth/chaindata/ancient/chain readonly=false
INFO [12-09|16:44:01.413] Writing custom genesis block
INFO [12-09|16:44:01.416] Persisted trie from memory database nodes=7 size=1.02KiB time="114.125µs" gcnodes=0 gcsize=0.00B gctime=0s livenodes=1 livesize=0.00B
INFO [12-09|16:44:01.416] Successfully wrote genesis state database=chaindata hash=0e1989..f9d68e
INFO [12-09|16:44:01.416] Allocated cache and file handles database=/Users/devsawd/private-net/geth/lightchaindata cache=16.00MiB handles=16
INFO [12-09|16:44:01.470] Opened ancient database database=/Users/devsawd/private-net/geth/lightchaindata/ancient/chain readonly=false
INFO [12-09|16:44:01.470] Writing custom genesis block
INFO [12-09|16:44:01.470] Persisted trie from memory database nodes=7 size=1.02KiB time="104.166µs" gcnodes=0 gcsize=0.00B gctime=0s livenodes=1 livesize=0.00B
INFO [12-09|16:44:01.471] Successfully wrote genesis state database=lightchaindata hash=0e1989..f9d68e

초기화에 성공하면 private-net 디렉터리 아래 geth 디렉터리가 생성됩니다.

번거롭게 긴 명령어를 치지 말고 셸 스크립트 파일로 만들어 실행하는 것이 편합니다.

geth에서 프라이빗 네트워크 실행

예전에 실습했던 명령어로 실행해보니 되지 않았습니다.

시간이 지나면서 버전이 바뀌고 Command-Line Options가 많이 변경된것을 확인했습니다.

위의 사이트를 참고해서 지금 기준에 맞게 수정하였습니다.

1
% geth --datadir ~/private-net --networkid 15 --nodiscover --maxpeers 0 --mine --miner.threads 1 --http --http.addr "0.0.0.0" --http.corsdomain "*" --http.vhosts "*" --http.api "eth,web3,personal,net,miner" --ipcpath ~/private-net/geth.ipc --ws --ws.addr "0.0.0.0" --ws.api "eth,web3,personal,net,miner" --ws.origins "*" --allow-insecure-unlock --password ~/private-net/password

geth 콘솔 접속

1
2
3
4
5
6
7
8
9
10
% geth attach http://localhost:8545
Welcome to the Geth JavaScript console!

instance: Geth/v1.10.26-stable/darwin-arm64/go1.19.3
coinbase: 0x945cd603a6754cb13c3d61d8fe240990f86f9f8a
at block: 99 (Fri Dec 09 2022 17:06:12 GMT+0900 (KST))
modules: eth:1.0 miner:1.0 net:1.0 personal:1.0 rpc:1.0 web3:1.0

To exit, press ctrl-d or type exit
> %

블록 높이 확인

1
2
3
4
5
6
7
> eth.blockNumber
108
> eth.blockNumber
109
> eth.blockNumber
110
> eth.blockNumber