Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

What is the source code analysis of StateSync.go under the Downloader module of Ethernet Square?

Shulou Source: shulou.com Published: 2022-06-01 13:44:20 09月19日 Update

This article introduces how the StateSync.go source code analysis under the Downloader module of Etay Square is, and the content is very detailed. Interested friends can use it for reference. I hope it will be helpful to you.

StateSync source code analysis 1. Introduction

Statesync is a synchronous state module, the most direct connection is that the stateRoot,stateRoot in block is downloaded using the statesync module.

2. Source code analysis

At the beginning of all things, first introduce the data structure of statesync itself.

/ / stateSync schedules requests for downloading a particular state trie defined// by a given state root.// stateSync dispatches the request to download the state tree type stateSync struct {d * Downloader / / Downloader instance to access and manage current peerset combined downloader module / / scheduler sched * trie.Sync / / State trie sync scheduler defining the tasks keccak hash.Hash for a specific state root / / Keccak256 hasher to verify deliveries with / / request task queue tasks map [common.Hash] * stateTask / / Set of tasks currently queued for retrieval / / Statistical bit numUncommitted int bytesUncommitted int / / Channel for processing data correlation deliver chan * stateReq / / Delivery channel multiplexing peer responses cancel chan struct {} / / Channel to signal a termination request cancelOnce Sync.Once / / Ensures cancel only ever gets called once done chan struct {} / / Channel to signal termination completion err error / / Any error hit during sync (set before completion)}

These are the main data structures of statesync, followed by the journey of source-led analysis.

/ / 1. First, take a look at how to enable state synchronization and how to trigger state synchronization / / state synchronization thread to open: / / new method / / New creates a new downloader to fetch hashes and blocks from remote peers.func New (mode SyncMode, stateDb ethdb.Database, mux * event.TypeMux, chain BlockChain, lightchain LightChain, dropPeer peerDropFn) * Downloader {under / / downloader. After entering the stateFetcher method, go dl.stateFetcher () return dl} / / starts the rotation training channel. Wait for status synchronization task to arrive / / stateFetcher manages the active state sync and accepts requests// on its behalf.func (d * Downloader) stateFetcher () {for {select {/ / read synchronization task from d.stateSyncStart channel case s: = 0 {deliverReq = finished [0] deliverReqCh = S.deliver} select {/ / if another synchronization occurs during status synchronization Then this method returns a new stateSync object and calls the runStateSync method case next: =

Tags: Status synchronization methods processing data analysis tasks messages results channels loops modules source code that is structure database enforcement scheduling things memory Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux Redmi Docker Shulou Tech Info vpn