ขึ้น Private Blockchain (POW) ของตัวเอง ตามรอย JBC Blockchain

Nat
3 min readJan 2, 2023

--

ผมทำอะไรใน Blockchain เป็นไม่มาก แต่โชคดีค้นพบความลับว่า ถ้าเราขึ้น Chain ตามพี่โดม แล้วทุกอย่างจะง่ายขึ้นเยอะ

อย่าง การทำ EVM Compatiable Blockchain แบบ Ethash proof-of-work นี่ยังไม่เคยทำเป็น พอ JBC Blockchain ขึ้นมาปุ๊บทำเป็นเลย 555

เริ่มจาก เอา/ลอก genesis.jsonของ jibchain (https://github.com/jibchain-net/node/blob/main/genesis.json) มาใช้เลย แต่เราจะแก้ pre-mined address และ chain-id พอเป็นพิธี

ก็จะได้ แบบนี้ save ทับลงไปได้เลย ไม่แก้อะไรอย่างอื่นทั้งๆนั้น แต่ที่แน่ๆ ใน genesis นี้มี londonBlock (EIP-1559)ใส่มาให้แล้ว อย่างสวยงาม

{
"config": {
"chainId": 9999,
"homesteadBlock": 0,
"eip150Block": 0,
"eip150Hash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"eip155Block": 0,
"eip158Block": 0,
"byzantiumBlock": 0,
"constantinopleBlock": 0,
"petersburgBlock": 0,
"istanbulBlock": 0,
"muirGlacierBlock": 0,
"berlinBlock": 0,
"londonBlock": 0,
"arrowGlacierBlock": 0,
"grayGlacierBlock": 0,
"shanghaiBlock": null,
"cancunBlock": null,
"ethash": {}
},
"nonce": "0x0",
"gasLimit": "8000000",
"difficulty": "0x40000",
"mixHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"coinbase": "0x0000000000000000000000000000000000000000",
"alloc": {
"f39Fd6e51aad88F6F4ce6aB8827279cffFb92266": {
"balance": "1000000000000000000000000001"
}
},
"number": "0x0",
"gasUsed": "0x0",
"parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"baseFeePergas": "0x2540BE400"
}

หลังจากนั้นก็ตาม step เดิมเริ่มจาก init blockchain data ด้วย genesis.json ด้วยคำสั่ง geth (นั่นแหละ เราจะใช้ geth กัน)

geth --datadir mypow init genesis.json
> geth --datadir mypow init genesis.json
INFO [01-02|11:03:53.703] Maximum peer count ETH=50 LES=0 total=50
INFO [01-02|11:03:53.706] Set global gas cap cap=50,000,000
INFO [01-02|11:03:53.707] Allocated cache and file handles database=/Users/nat/jibchain-erigon/mypow/geth/chaindata cache=16.00MiB handles=16
INFO [01-02|11:03:53.843] Opened ancient database database=/Users/nat/jibchain-erigon/mypow/geth/chaindata/ancient/chain readonly=false
INFO [01-02|11:03:53.846] Successfully wrote genesis state database=chaindata hash=2d559f..c63524
INFO [01-02|11:03:53.846] Allocated cache and file handles database=/Users/nat/jibchain-erigon/mypow/geth/lightchaindata cache=16.00MiB handles=16
INFO [01-02|11:03:53.985] Opened ancient database database=/Users/nat/jibchain-erigon/mypow/geth/lightchaindata/ancient/chain readonly=false
INFO [01-02|11:03:53.987] Successfully wrote genesis state database=lightchaindata hash=2d559f..c63524

หลังจากนั้น เราก็เริ่ม ขุด Block ได้เลย ด้วย networkid = 9999

geth --datadir mypow --networkid 9999 --mine --miner.etherbase=0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266 --miner.threads 8  --http.api=net,eth,web3 --http
mining with geth

ลองสั่ง geth attach http://localhost:8545 แล้วขอดู Block ใหม่แบบ Real-time กันหน่อย ด้วย setIntervaและ console.log

setInterval(function() { console.log(eth.blockNumber); }, 1000);
an interaction with the rpc

ลอง getBalance โดยไม่ใช้ metamask สามารถทำด้วยคำสั่ง eth.getBalance("0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266")

ถ้าจะให้ erigon หรือ client ตัวอื่น connect เข้ามา sync ข้อมูล ให้ก๊อบ enode:// ที่เราเปิดเอาไว้ให้ดี ตรงนี้

INFO [01-02|11:11:40.609] Started P2P networking                   self=enode://e56ec36fa581fc8f9697116359f3e973ff8172030ec466a09f4b54008583ffbda7e1eb66e4e85e6de035df4fdea167d0ece1eeb7df7f82505f2c57bf045bf191@127.0.0.1:30303

เสร็จแล้ว ทำตามบทความนี้เพื่อ sync ด้วย erigon โดยการแก้ networkid และ bootnodes

ตอน init ก็สังเกตด้วยว่า genesis hash ต้องตรงกัน

> erigon --datadir erigon-pow init genesis.json
WARN[01-02|11:14:50.416] no log dir set, console logging only
INFO[01-02|11:14:50.432] Opening Database label=chaindata path=/Users/nat/jibchain-erigon/erigon-pow/chaindata
INFO[01-02|11:14:50.449] Re-Opening DB in exclusive mode to apply migrations
INFO[01-02|11:14:50.463] Apply migration name=db_schema_version5
INFO[01-02|11:14:50.464] Applied migration name=db_schema_version5
INFO[01-02|11:14:50.465] Apply migration name=txs_begin_end
INFO[01-02|11:14:50.465] Applied migration name=txs_begin_end
INFO[01-02|11:14:50.465] Apply migration name=reset_blocks_4
INFO[01-02|11:14:50.466] Applied migration name=reset_blocks_4
INFO[01-02|11:14:50.466] Updated DB schema to version=6.0.0
INFO[01-02|11:14:50.550] Writing custom genesis block hash=0x2d559ffc7b20d9096abd0a840194c29b475cb4a329ff6f28a580c3c01cc63524
INFO[01-02|11:14:50.552] Successfully wrote genesis state hash=0x2d559ffc7b20d9096abd0a840194c29b475cb4a329ff6f28a580c3c01cc63524

หลังจาก นั้นก็ sync ตามปกติ ถ้า geth หรือ erigon อยู่ network เดียวกันก็อย่าลืมเช็ค port ชน

> erigon --datadir=./erigon-pow --networkid 9999 --bootnodes=enode://e56ec36fa581fc8f9697116359f3e973ff8172030ec466a09f4b54008583ffbda7e1eb66e4e85e6de035df4fdea167d0ece1eeb7df7f82505f2c57bf045bf191@127.0.0.1:30303

เท่านี้ก็พอจะเริ่มเห็นภาพ blockchain เพิ่มขึ้นอีกหน่อย

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Nat
Nat

Written by Nat

Chiang Mai Maker Cub, Thailand

No responses yet

Write a response