https://ethereum.org/zh/developers/tutorials/run-light-node-geth/

首先,安装 Geth。安装 Geth 后,可以通过在终端窗口运行以下命令以“轻”模式运行以太坊节点:

geth --syncmode light

geth --syncmode light

等待大约200秒后,从第2300个区块开始同步,然后定期接收到含有1到10个区块的区块包。初始同步所需时间很短。

linlin@linlin-i5:/workspace/blockchain$ geth --syncmode light
INFO [07-26|11:39:07.225] Starting Geth on Ethereum mainnet...
INFO [07-26|11:39:07.225] Dropping default light client cache      provided=1024 updated=128
INFO [07-26|11:39:07.226] Maximum peer count                       ETH=0 LES=10 total=50
INFO [07-26|11:39:07.227] Smartcard socket not found, disabling    err="stat /run/pcscd/pcscd.comm: no such file or directory"
INFO [07-26|11:39:07.229] Set global gas cap                       cap=50,000,000
INFO [07-26|11:39:07.230] Allocated cache and file handles         database=/home/linlin/.ethereum/geth/lightchaindata cache=64.00MiB handles=524,288
INFO [07-26|11:39:07.233] Allocated cache and file handles         database=/home/linlin/.ethereum/geth/les.client cache=16.00MiB handles=16
INFO [07-26|11:39:07.236] Writing default main-net genesis block
INFO [07-26|11:39:07.340] Persisted trie from memory database      nodes=12356 size=1.78MiB time=19.99905ms gcnodes=0 gcsize=0.00B gctime=0s livenodes=1 livesize=0.00B
INFO [07-26|11:39:07.352]  
INFO [07-26|11:39:07.352] ---------------------------------------------------------------------------------------------------------------------------------------------------------
INFO [07-26|11:39:07.352] Chain ID:  1 (mainnet)
INFO [07-26|11:39:07.352] Consensus: Ethash (proof-of-work)
INFO [07-26|11:39:07.352]  
INFO [07-26|11:39:07.352] Pre-Merge hard forks:
INFO [07-26|11:39:07.352]  - Homestead:                   1150000  (https://github.com/ethereum/execution-specs/blob/master/network-upgrades/mainnet-upgrades/homestead.md)
INFO [07-26|11:39:07.352]  - DAO Fork:                    1920000  (https://github.com/ethereum/execution-specs/blob/master/network-upgrades/mainnet-upgrades/dao-fork.md)
INFO [07-26|11:39:07.352]  - Tangerine Whistle (EIP 150): 2463000  (https://github.com/ethereum/execution-specs/blob/master/network-upgrades/mainnet-upgrades/tangerine-whistle.md)
INFO [07-26|11:39:07.353]  - Spurious Dragon/1 (EIP 155): 2675000  (https://github.com/ethereum/execution-specs/blob/master/network-upgrades/mainnet-upgrades/spurious-dragon.md)
INFO [07-26|11:39:07.353]  - Spurious Dragon/2 (EIP 158): 2675000  (https://github.com/ethereum/execution-specs/blob/master/network-upgrades/mainnet-upgrades/spurious-dragon.md)
INFO [07-26|11:39:07.353]  - Byzantium:                   4370000  (https://github.com/ethereum/execution-specs/blob/master/network-upgrades/mainnet-upgrades/byzantium.md)
INFO [07-26|11:39:07.353]  - Constantinople:              7280000  (https://github.com/ethereum/execution-specs/blob/master/network-upgrades/mainnet-upgrades/constantinople.md)
INFO [07-26|11:39:07.353]  - Petersburg:                  7280000  (https://github.com/ethereum/execution-specs/blob/master/network-upgrades/mainnet-upgrades/petersburg.md)
INFO [07-26|11:39:07.353]  - Istanbul:                    9069000  (https://github.com/ethereum/execution-specs/blob/master/network-upgrades/mainnet-upgrades/istanbul.md)
INFO [07-26|11:39:07.353]  - Muir Glacier:                9200000  (https://github.com/ethereum/execution-specs/blob/master/network-upgrades/mainnet-upgrades/muir-glacier.md)
INFO [07-26|11:39:07.353]  - Berlin:                      12244000 (https://github.com/ethereum/execution-specs/blob/master/network-upgrades/mainnet-upgrades/berlin.md)
INFO [07-26|11:39:07.353]  - London:                      12965000 (https://github.com/ethereum/execution-specs/blob/master/network-upgrades/mainnet-upgrades/london.md)
INFO [07-26|11:39:07.353]  - Arrow Glacier:               13773000 (https://github.com/ethereum/execution-specs/blob/master/network-upgrades/mainnet-upgrades/arrow-glacier.md)
INFO [07-26|11:39:07.353]  - Gray Glacier:                15050000 (https://github.com/ethereum/execution-specs/blob/master/network-upgrades/mainnet-upgrades/gray-glacier.md)
INFO [07-26|11:39:07.353]  
INFO [07-26|11:39:07.353] Merge not configured!
INFO [07-26|11:39:07.353]  - Hard-fork specification: https://github.com/ethereum/execution-specs/blob/master/network-upgrades/mainnet-upgrades/paris.md)
INFO [07-26|11:39:07.353] ---------------------------------------------------------------------------------------------------------------------------------------------------------
INFO [07-26|11:39:07.353]  
INFO [07-26|11:39:07.353] Disk storage enabled for ethash caches   dir=/home/linlin/.ethereum/geth/ethash count=3
INFO [07-26|11:39:07.353] Disk storage enabled for ethash DAGs     dir=/home/linlin/.ethash               count=2
INFO [07-26|11:39:07.353] Added trusted checkpoint                 block=14,811,135 hash=e47f84..a75aa6
INFO [07-26|11:39:07.353] Loaded most recent local header          number=0 hash=d4e567..cb8fa3 td=17,179,869,184 age=53y3mo4w
INFO [07-26|11:39:07.353] Configured checkpoint oracle             address=0x9a9070028361F7AAbeB3f2F2Dc07F82C4a98A02a signers=5 threshold=2
INFO [07-26|11:39:07.353] Gasprice oracle is ignoring threshold set threshold=2
WARN [07-26|11:39:07.354] Error reading unclean shutdown markers   error="leveldb: not found"
INFO [07-26|11:39:07.354] Starting peer-to-peer node               instance=Geth/v1.10.20-stable-8f2416a8/linux-amd64/go1.18.1
INFO [07-26|11:39:07.360] New local node record                    seq=1,658,806,747,359 id=3f2b15bf4d53c928 ip=127.0.0.1 udp=30303 tcp=30303
INFO [07-26|11:39:07.361] Started P2P networking                   self=enode://f748d046eeb30e497fef5936d91ea15c69ec0343c70a872239d21e6b464b140ed0259acc0372ad33045204e23f14a84b5834fdd0b9178af87ce417f81a63c27d@127.0.0.1:30303
INFO [07-26|11:39:07.362] IPC endpoint opened                      url=/home/linlin/.ethereum/geth.ipc
WARN [07-26|11:39:07.362] Light client mode is an experimental feature
INFO [07-26|11:39:09.708] New local node record                    seq=1,658,806,747,360 id=3f2b15bf4d53c928 ip=192.168.1.3 udp=30303 tcp=30303
INFO [07-26|11:39:10.525] Mapped network port                      proto=tcp extport=30303 intport=30303 interface="UPNP IGDv1-IP1"
INFO [07-26|11:39:18.685] Looking for peers                        peercount=0 tried=4 static=0

启动后,Geth 将开始连接到以太坊上的其他节点 — 称为“对等节点”。 连接到对等节点的过程可能需要一段时间。

当您的 Geth 节点有足够多的对等节点时,它将从链上的新区块中导入区块头。

当新的区块头不再有“age”时,Geth 将同步到链的头部。

停止和重启节点

您可以随时按 Ctrl+C 键停止您的节点。

重启节点时,Geth 需要用几分钟下载自上次运行该节点以来创建的区块头。

启用 HTTP-RPC 服务器

启用 HTTP-RPC 服务器可让您将以太坊节点连接到其他软件,如钱包、浏览器扩展程序或自定义软件库。

您可以在启动 Geth 时通过运行以下命令来启用 HTTP-RPC 服务器:

geth --syncmode light --http

启用后,运行

curl http://127.0.0.1:8545
。 应该不会报错。(报错了curl http://127.0.0.1:8545
404 Not Foundlinlin@linlin-i5:)

parity --light

立即从一个硬编码值(区块高度6219777)开始同步,速度大约为每分钟23,500个区块,同步区块高度达到6,500,000,过程需要15分钟。一旦同步,当新区块被挖出并经由全节点验证,轻节点就会接收到这些区块。

parity --light --no-hardcoded-sync

除了从创世区块开始同步以外,其他设定与轻同步模式一样。

连接Parity轻节点到MetaMask(MacOS系统)

  1. 下载homebrew(必须先通过苹果的开发条款)

  2. 查找和打开应用终端(应用/终端)

  3. 在终端中依次输入以下命令:

brew tap paritytech/paritytech (把parity添加到brew tap列表)

brew install parity (安装parity稳定版本)

parity --light --jsonrpc-cors="chrome-extension://nkbihfbeogaeaoehlefnkodbefgpgknn" & (同步轻节点,与其他节点相互连接,下载区块头信息)

按ctrl+C,然后键入:

tail -f nohup.out (将展示日志,以确保一切正常运行),在网页浏览器上打开Metamask,断开网络,连接到本地主机。

允许远程连接

要允许远程主机连接到您的节点,请使用以下命令启动 Geth:

geth --syncmode light --http --http.addr 0.0.0.0

注意:这种情况假设没有进程阻止发送到您的本地主机的请求,例如防火墙

Geth JavaScript 控制台:Geth 有一个内置的 JavaScript 控制台和一个名为 web3js 的 JavaScript API,您可以使用它与您的节点进行交互。

要使用 JavaScript 控制台,运行以下命令:

geth attach

linlin@linlin-i5:/workspace/blockchain$ geth attach
Welcome to the Geth JavaScript console!

instance: Geth/v1.10.20-stable-8f2416a8/linux-amd64/go1.18.1
at block: 0 (Thu Jan 01 1970 08:00:00 GMT+0800 (CST))
 datadir: /home/linlin/.ethereum
 modules: admin:1.0 debug:1.0 eth:1.0 ethash:1.0 les:1.0 net:1.0 personal:1.0 rpc:1.0 txpool:1.0 vflux:1.0 web3:1.0

To exit, press ctrl-d or type exit
>

完整 web3js 相关文档 该控制台允许与以太坊直接交互。 例如,运行

eth.blockNumber
命令将输出最新的已知区块编号。

> eth.blockNumber
15215828

> web3.eth.accounts
[]
> eth.accounts
[]

主网和测试网:Geth 默认在以太坊主网上运行节点。

通过在终端运行以下命令之一,也可以使用 Geth 在几个公共测试网络之一上运行节点:

geth --syncmode light --ropsten
geth --syncmode light --rinkeby
geth --syncmode light --goerli

区块链和以太坊虚拟机数据存储在哪里?

Geth 用于存储原始区块链数据的目录取决于您的操作系统。 运行 Geth 后,查找如下所示的消息:(这里出错了,没有正确的数据库进行使用,尚未解决)

INFO [11-18|14:04:47] Allocated cache and file handles database=/Users/bgu/Library/Ethereum/testnet/geth/lightchaindata cache=768 handles=128

“database=”
后面的路径应该显示区块链数据在您计算机中的存储位置。 如果您运行的是完整节点,则此目录将包含有关已提交到区块链的每个区块的所有数据。 因为我们在运行轻节点,此目录仅包含区块头。

此处要强调的是,在最低层级,这里便是区块链的所在之处。 区块链的完整内容和以太坊虚拟机状态存储在以太网络上的每个完整节点中,其存储目录与您计算机上这个目录非常相似。

安装 Geth 有很多种方式,这里主要就 Linux 环境给出两种:系统包管理器(apt-get)安装和源码安装。更加推荐大家用源码安装,在整个过程中可以看到 Geth 各组件的构建步骤。
一、apt-get

sudo apt-get install software-properties-common
sudo add-apt-repository -y ppa:ethereum/ethereum
sudo apt-get update
sudo apt-get install ethereum

二、源码安装

git clone https://github.com/ethereum/go-ethereum.git
cd go-ethereum
make geth(不能使用,需要进行下载go的配置等)

如果一切顺利,我们将看到 Go 编译器构建每个组件,直到它生成 geth 可执行文件:(因为已经使用第一种方式安装了,所以直接到blockchain文件夹下查看版本号即可)

查看版本号

linlin@linlin-i5:/workspace/blockchain$ geth version
Geth
Version: 1.10.20-stable
Git Commit: 8f2416a89a3def6ec2c749d5afafbf2c9a18e3c8
Architecture: amd64
Go Version: go1.18.1
Operating System: linux
GOPATH=
GOROOT=go

https://ethereum.org/en/developers/docs/apis/json-rpc/#eth_getbalance

检索 Coinbase 地址和余额来验证接口是否正在运行 curl 。 请注意,这些示例中的数据在您的本地节点上会有所不同。 如果您想尝试这些命令,请将第二个 curl 请求中的请求参数替换为第一个 curl 请求返回的结果。

curl --data '{"jsonrpc":"2.0","method":"eth_coinbase", "id":1}' -H "Content-Type: application/json" localhost:8545
{"id":1,"jsonrpc":"2.0","result":"0x0000000000000000000000000000000000000000"}

linlin@linlin-i5:/workspace/blockchain/contract_on_goerli_using_infura$ curl -X POST -H "Content-Type:application/json" --data '{"jsonrpc":"2.0","method":"web3_clientVersion","params":[],"id":1}' http://127.0.0.1:8545
{"id":1,"jsonrpc":"2.0","result":"Ganache/v7.3.2/EthereumJS TestRPC/v7.3.2/ethereum-js"}linlin

curl --data '{"jsonrpc":"2.0","method":"eth_getBalance", "params": ["0x960781B058da688735c98D5165aF8FF4e1B400E0", "latest"], "id":2}' -H "Content-Type: application/json" localhost:8545
{"id":2,"jsonrpc":"2.0","result":"0x0"}lin

搭建自己的私有链

因为公共网络的区块数量太多,同步耗时太长,我们为了方便快速了解 Geth,可以试着用它来搭一个只属于自己的私链。首先,我们需要创建网络的“创世”(genesis)状态,这写在一个小小的 JSON 文件里(例如,我们将其命名为 genesis.json):

{
"config": {
"chainId": 15
},
"difficulty": "2000",
"gasLimit": "2100000",
"alloc": {
"7df9a875a174b3bc565e6424a0050ebc1b2d1d82": { "balance": "300000" },
"f41c74c9ae680c1aa78f42e5647a62f353b7bdde": { "balance": "400000" }
}
}

要创建一条以它作为创世块的区块链,我们可以使用下面的命令:

geth --datadir path/to/custom/data/folder init genesis.json

linlin@linlin-i5:/workspace/blockchain$ geth --datadir path/to/custom/data/folder init genesis.json
INFO [07-26|14:55:28.387] Maximum peer count                       ETH=50 LES=0 total=50
INFO [07-26|14:55:28.388] Smartcard socket not found, disabling    err="stat /run/pcscd/pcscd.comm: no such file or directory"
INFO [07-26|14:55:28.391] Set global gas cap                       cap=50,000,000
INFO [07-26|14:55:28.392] Allocated cache and file handles         database=/workspace/blockchain/path/to/custom/data/folder/geth/chaindata cache=16.00MiB handles=16
INFO [07-26|14:55:28.401] Opened ancient database                  database=/workspace/blockchain/path/to/custom/data/folder/geth/chaindata/ancient readonly=false
INFO [07-26|14:55:28.401] Writing custom genesis block
INFO [07-26|14:55:28.402] Persisted trie from memory database      nodes=3 size=397.00B time="246.439µs" gcnodes=0 gcsize=0.00B gctime=0s livenodes=1 livesize=0.00B
INFO [07-26|14:55:28.402] Successfully wrote genesis state         database=chaindata hash=e3ea4f..18ce30
INFO [07-26|14:55:28.402] Allocated cache and file handles         database=/workspace/blockchain/path/to/custom/data/folder/geth/lightchaindata cache=16.00MiB handles=16
INFO [07-26|14:55:28.412] Opened ancient database                  database=/workspace/blockchain/path/to/custom/data/folder/geth/lightchaindata/ancient readonly=false
INFO [07-26|14:55:28.412] Writing custom genesis block
INFO [07-26|14:55:28.413] Persisted trie from memory database      nodes=3 size=397.00B time="282.712µs" gcnodes=0 gcsize=0.00B gctime=0s livenodes=1 livesize=0.00B
INFO [07-26|14:55:28.413] Successfully wrote genesis state         database=lightchaindata hash=e3ea4f..18ce30

在当前目录下运行 geth,就会启动这条私链,注意要将 networked 设置为与创世块配置里的
chainId 一致。

geth --datadir path/to/custom/data/folder --networkid 15

linlin@linlin-i5:/workspace/blockchain$ geth --datadir path/to/custom/data/folder --networkid 15
INFO [07-26|15:03:05.897] Maximum peer count                       ETH=50 LES=0 total=50
INFO [07-26|15:03:05.898] Smartcard socket not found, disabling    err="stat /run/pcscd/pcscd.comm: no such file or directory"
INFO [07-26|15:03:05.900] Set global gas cap                       cap=50,000,000
INFO [07-26|15:03:05.901] Allocated trie memory caches             clean=154.00MiB dirty=256.00MiB
INFO [07-26|15:03:05.901] Allocated cache and file handles         database=/workspace/blockchain/path/to/custom/data/folder/geth/chaindata cache=512.00MiB handles=524,288
INFO [07-26|15:03:05.913] Opened ancient database                  database=/workspace/blockchain/path/to/custom/data/folder/geth/chaindata/ancient readonly=false
INFO [07-26|15:03:05.914]  
INFO [07-26|15:03:05.914] ---------------------------------------------------------------------------------------------------------------------------------------------------------
INFO [07-26|15:03:05.914] Chain ID:  15 (unknown)
INFO [07-26|15:03:05.914] Consensus: unknown
INFO [07-26|15:03:05.914]  
INFO [07-26|15:03:05.914] Pre-Merge hard forks:
INFO [07-26|15:03:05.914]  - Homestead:                   <nil> (https://github.com/ethereum/execution-specs/blob/master/network-upgrades/mainnet-upgrades/homestead.md)
INFO [07-26|15:03:05.914]  - Tangerine Whistle (EIP 150): <nil> (https://github.com/ethereum/execution-specs/blob/master/network-upgrades/mainnet-upgrades/tangerine-whistle.md)
INFO [07-26|15:03:05.914]  - Spurious Dragon/1 (EIP 155): <nil> (https://github.com/ethereum/execution-specs/blob/master/network-upgrades/mainnet-upgrades/spurious-dragon.md)
INFO [07-26|15:03:05.914]  - Spurious Dragon/2 (EIP 158): <nil> (https://github.com/ethereum/execution-specs/blob/master/network-upgrades/mainnet-upgrades/spurious-dragon.md)
INFO [07-26|15:03:05.914]  - Byzantium:                   <nil> (https://github.com/ethereum/execution-specs/blob/master/network-upgrades/mainnet-upgrades/byzantium.md)
INFO [07-26|15:03:05.914]  - Constantinople:              <nil> (https://github.com/ethereum/execution-specs/blob/master/network-upgrades/mainnet-upgrades/constantinople.md)
INFO [07-26|15:03:05.914]  - Petersburg:                  <nil> (https://github.com/ethereum/execution-specs/blob/master/network-upgrades/mainnet-upgrades/petersburg.md)
INFO [07-26|15:03:05.914]  - Istanbul:                    <nil> (https://github.com/ethereum/execution-specs/blob/master/network-upgrades/mainnet-upgrades/istanbul.md)
INFO [07-26|15:03:05.914]  - Berlin:                      <nil> (https://github.com/ethereum/execution-specs/blob/master/network-upgrades/mainnet-upgrades/berlin.md)
INFO [07-26|15:03:05.914]  - London:                      <nil> (https://github.com/ethereum/execution-specs/blob/master/network-upgrades/mainnet-upgrades/london.md)
INFO [07-26|15:03:05.914]  
INFO [07-26|15:03:05.914] Merge not configured!
INFO [07-26|15:03:05.914]  - Hard-fork specification: https://github.com/ethereum/execution-specs/blob/master/network-upgrades/mainnet-upgrades/paris.md)
INFO [07-26|15:03:05.914] ---------------------------------------------------------------------------------------------------------------------------------------------------------
INFO [07-26|15:03:05.914]  
INFO [07-26|15:03:05.915] Disk storage enabled for ethash caches   dir=/workspace/blockchain/path/to/custom/data/folder/geth/ethash count=3
INFO [07-26|15:03:05.915] Disk storage enabled for ethash DAGs     dir=/home/linlin/.ethash count=2
INFO [07-26|15:03:05.915] Initialising Ethereum protocol           network=15 dbversion=<nil>
INFO [07-26|15:03:05.915] Loaded most recent local header          number=0 hash=e3ea4f..18ce30 td=2000 age=53y3mo4w
INFO [07-26|15:03:05.915] Loaded most recent local full block      number=0 hash=e3ea4f..18ce30 td=2000 age=53y3mo4w
INFO [07-26|15:03:05.915] Loaded most recent local fast block      number=0 hash=e3ea4f..18ce30 td=2000 age=53y3mo4w
WARN [07-26|15:03:05.915] Failed to load snapshot, regenerating    err="missing or corrupted snapshot"
INFO [07-26|15:03:05.915] Rebuilding state snapshot
INFO [07-26|15:03:05.916] Regenerated local transaction journal    transactions=0 accounts=0
INFO [07-26|15:03:05.916] Resuming state snapshot generation       root=2cffac..e32dfc accounts=0 slots=0 storage=0.00B dangling=0 elapsed="187.504µs"
INFO [07-26|15:03:05.916] Generated state snapshot                 accounts=2 slots=0 storage=82.00B dangling=0 elapsed="414.675µs"
INFO [07-26|15:03:05.916] Gasprice oracle is ignoring threshold set threshold=2
WARN [07-26|15:03:05.916] Error reading unclean shutdown markers   error="leveldb: not found"
INFO [07-26|15:03:05.916] Starting peer-to-peer node               instance=Geth/v1.10.20-stable-8f2416a8/linux-amd64/go1.18.1
Fatal: Error starting protocol stack: listen tcp :30303: bind: address already in use