深入学习 Go-Ethereum : 013. CMD 模块 - Geth -snapshot.go

⬇️以下是 cmd/geth 包中其他代码的介绍:

├── accountcmd.go 
├── chaincmd.go 
├── config.go 
├── consolecmd.go 
├── dbcmd.go 
├── misccmd.go 
├── snapshot.go 🔫
├── usage.go
└── version_check.go

《深入学习 Go-Ethereum : 012. CMD 模块 - Geth - misccmd.go》中罗列了 misccmd 主要源码,接下来描述下关于 snapshot.go 中的代码功能(基于2022-07-25的认知稀里糊涂的记录)。

⬇️ snapshot.go

snapshot 是一系列子命令的集合。其中包含了:

  1. 等同于 geth dump 功能的 geth snapshot dump 方法

  2. 基于快照的历史状态数据清理 prune-state

  3. 基于快照数据重新进行hash计算,用于验证 verify-state

  4. 检查是否存在 “dangling” 数据 check-dangling-storage

  5. 用给定的root hash进行遍历校验 traverse-state (快速遍历)

  6. 用给定的root hash进行遍历校验 traverse-rawstate (遍历每一个节点)

snapshot.go 代码结构概览
snapshot.go 代码结构概览

⬇️ pruneState方法:

描述:geth snapshot prune-state <state-root> will prune historical state data with the help of the state snapshot. All trie nodes and contract codes that do not belong to the specified version state will be deleted from the database. After pruning, only two version states are available: genesis and the specific one.

(对历史状态数据进行清理,不满足指定版本状态所有trie结构节点数据和合约代码将会被清除,只有两个版本数据有效:创世区块和唯一指定的数据)。

pruneState方法代码
pruneState方法代码

关键方法调用 core.state.prune.pruner.go 中的 Prune() 方法。后续对其中代码详细分析。(数据检索使用到了 Bloom Filter

⬇️ verifyState方法:

描述:geth snapshot verify-state <state-root> will traverse the whole accounts and storages set based on the specified snapshot and recalculate the root hash of state for verification. In other words, this command does the snapshot to trie conversion.

(此方法对指定的快照中账户和存储集合进行遍历,然后重新计算状态数据根哈希值,以用于校验。换句话说,这个命令将快照转换成 Trie 「字典树」格式)。

verifyState方法代码
verifyState方法代码

⬇️ checkDanglingStorage方法:

描述:geth snapshot check-dangling-storage <state-root> traverses the snap storage data, and verifies that all snapshot storage data has a corresponding account.

(此方法遍历快照中的存储数据,并校验所有存储数据都有与之对应的账户)

checkDanglingStorage方法代码
checkDanglingStorage方法代码

⬇️ traverseState方法:

描述:geth snapshot traverse-state <state-root> will traverse the whole state from the given state root and will abort if any referenced trie node or contract code is missing. This command can be used for state integrity verification. The default checking target is the HEAD state. It's also usable without snapshot enabled.

(此防范会遍历整个状态数据,当出现Trie节点或者合约代码丢失。此命令可以用于状态数据的完整验证。默认的检查是 「区块头」 状态数据)

traverseState方法代码
traverseState方法代码

⬇️ traverseRawState方法:

和traverseState方法的主要区别,1.方法获取数据的二进制原始数据,差别如下图⬇️

LeafBlob returns the content of the leaf,返回类型 []byte 。2.会检查所有Tire节点。

⬇️ dump方法:

描述:This command is semantically equivalent to 'geth dump', but uses the snapshots as the backend data source, making this command a lot faster. The argument is interpreted as block number or hash. If none is provided, the latest block is used.

(此方法基于快照进行数据dump,速度会比 geth dump 快)。

Subscribe to OutOfToken
Receive the latest updates directly to your inbox.
Mint this entry as an NFT to add it to your collection.
Verification
This entry has been permanently stored onchain and signed by its creator.