iT邦幫忙

2024 iThome 鐵人賽

DAY 12
0
Modern Web

web3 短篇集系列 第 12

以太坊客戶端跑起來 Part 2

  • 分享至 

  • xImage
  •  

由於 GCP 試用期最大硬碟容量 500GB 的限制,我無法在 sepolia 成功建立節點,但是,我發現還有另一個測試鏈:holesky,要不是為了跑節點我還真不知道有這個測試鏈,顯然它的區塊高度比 sepolia 低許多。

目前我的節點已順利同步,使用 df -h / 查看目前 ubuntu 的硬碟使用率:

Filesystem      Size  Used Avail Use% Mounted on
/dev/root       484G  113G  371G  24% /

太好了!看來還有很多空間,holesky 果然是別有洞天啊..

防火牆設定

Lodestar - Firewall Management

Geth - Security

我目前在 GCP 只處理 30303 和 8545 兩個 port,前者是讓 geth 能夠在 p2p 網路跟其他節點連線,後者是讓我的電腦能夠直接打到 geth 的 JSON RPC API。

到 GCP 的 Firewall policies 的頁面,按下 CREATE FIREWALL RULE:

開通 30303 port

  • Name:allow-geth-30303
  • Direction: Ingress
  • IPv4 Ranges: 0.0.0.0/0
  • Specified protocols and ports: tcp:30303, udp:30303

允許我的電腦連線到 8545 port

  • Name:allow-8545-my-ip
  • Direction: Ingress
  • IPv4 Ranges: xxx.xxx.xx.xxx/32
  • Specified protocols and ports: tcp:8545

解決 lodestar OOM 的問題

當我發現 Geth 順利運作時,Lodestar 的 process 莫名其妙被砍掉。後來發現原來是 out of memory 的問題,我在 GCP 開 N2 的 Memory 只有 8GB,顯然又低於 Lodestar 文件上硬體要求的 16GB:

這個指令可以發現 lodestar 的 process 自動被 killed 是因為 OOM

sudo dmesg -T | grep -i 'out of memory'
  • -T: shows timestamp

觀察當前 CPU, Memory, processes 使用率

htop

印出當前 Memory 使用狀況

free -h

Swap 啟動虛擬記憶體

來自這篇文章的指引 Resolving commands 'Killed' on GCP f1-micro Compute Engine instances,原來可以使用硬碟的一部分空間當記憶體使用,以此來避免 OOM 的問題。

Enable 4GB Swap Space

sudo fallocate -l 4G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile

同步 holesky 網路

確認節點是否同步可以使用 JSON RPC API - eth_syncing

eth_syncing

使用 TypeScript 將 hex 轉成可讀的 decimal

if (!process.env.RPC_URL) {
	throw new Error('Missing .env')
}

const response = await fetch(process.env.RPC_URL, {
	method: 'post',
	headers: {
		'Content-Type': 'application/json',
	},
	body: JSON.stringify({
		jsonrpc: '2.0',
		method: 'eth_syncing',
		params: [],
		id: 1,
	}),
})

const res = await response.json()

// turn hex to string number with comma
for (const key in res.result) {
	res.result[key] = Number(res.result[key]).toLocaleString()
}

console.log(res)

當節點在 syncing 的時候,才會出現以下內容,其中可關注的是:

  • highestBlock: 目前網路上的區塊高度
  • currentBlock: 目前節點的區塊高度

這兩個的差距就是尚未同步的區塊數。

{
  jsonrpc: "2.0",
  id: 1,
  result: {
    currentBlock: "2,403,477",
    healedBytecodeBytes: "0",
    healedBytecodes: "0",
    healedTrienodeBytes: "0",
    healedTrienodes: "0",
    healingBytecode: "0",
    healingTrienodes: "0",
    highestBlock: "2,403,542",
    startingBlock: "2,400,377",
    syncedAccountBytes: "700,276,031",
    syncedAccounts: "3,189,491",
    syncedBytecodeBytes: "70,449,169",
    syncedBytecodes: "13,078",
    syncedStorage: "2,192,565",
    syncedStorageBytes: "476,279,811",
    txIndexFinishedBlocks: "0",
    txIndexRemainingBlocks: "1",
  },
}

一旦節點同步完成,呼叫 eth_syncing 則是:

{
    "jsonrpc": "2.0",
    "id": 1,
    "result": false
}

代表你已經連上 Ethereum Network,並成為 Ethereum 中的一份子了!

若要嘗試其他 JSON RPC API,推薦可以下載這個 Postman collection

Reference

geth --authrpc.jwtsecret ~/jwt.hex \
--http --http.addr "0.0.0.0" \
--http.api net,eth,personal,web3,engine,admin \
--holesky
  • 跑 lodestar 的指令
./lodestar beacon --network=holesky \
--jwt-secret=~/jwt.hex \
--checkpointSyncUrl=https://beaconstate-holesky.chainsafe.io

上一篇
認識 Storage Slots
下一篇
專案管理筆記
系列文
web3 短篇集14
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言