@@ -203,6 +203,69 @@ integer representing the role of the node:
203203
204204In the future, though, these restrictions will be removed.
205205
206+ # Sync
207+
208+ The crate implements a number of syncing algorithms. The main purpose of the syncing algorithm is
209+ get the chain to the latest state and keep it synced with the rest of the network by downloading and
210+ importing new data as soon as it becomes available. Once the node starts it catches up with the network
211+ with one of the initial sync methods listed below, and once it is completed uses a keep-up sync to
212+ download new blocks.
213+
214+ ## Full and light sync
215+
216+ This is the default syncing method for the initial and keep-up sync. The algorithm starts with the
217+ current best block and downloads block data progressively from multiple peers if available. Once
218+ there's a sequence of blocks ready to be imported they are fed to the import queue. Full nodes download
219+ and execute full blocks, while light nodes only download and import headers. This continues until each peers
220+ has no more new blocks to give.
221+
222+ For each peer the sync maintains the number of our common best block with that peer. This number is updates
223+ whenever peer announce new blocks or our best block advances. This allows to keep track of peers that have new
224+ block data and request new information as soon as it is announced. In keep-up mode, we also track peers that
225+ announce blocks on all branches and not just the best branch. The sync algorithm tries to be greedy and download
226+ All data that's announced.
227+
228+ ## Fast sync
229+
230+ In this mode the initial downloads and verifies full header history. This allows to validate
231+ authority set transitions and arrive at a recent header. After header chain is verified and imported
232+ the node starts downloading a state snapshot using the state request protocol. Each ` StateRequest `
233+ contains a starting storage key, which is empty for the first request.
234+ ` StateResponse ` contains a storage proof for a sequence of keys and values in the storage
235+ starting (but not including) from the key that is in the request. After iterating the proof trie against
236+ the storage root that is in the target header, the node issues The next ` StateRequest ` with set starting
237+ key set to the last key from the previous response. This continues until trie iteration reaches the end.
238+ The state is then imported into the database and the keep-up sync starts in normal full/light sync mode.
239+
240+ ## Warp sync
241+
242+ This is similar to fast sync, but instead of downloading and verifying full header chain, the algorithm
243+ only downloads finalized authority set changes.
244+
245+ ### GRANDPA warp sync.
246+
247+ GRANDPA keeps justifications for each finalized authority set change. Each change is signed by the
248+ authorities from the previous set. By downloading and verifying these signed hand-offs starting from genesis,
249+ we arrive at a recent header faster than downloading full header chain. Each ` WarpSyncRequest ` contains a block
250+ hash to a to start collecting proofs from. ` WarpSyncResponse ` contains a sequence of block headers and
251+ justifications. The proof downloader checks the justifications and continues requesting proofs from the last
252+ header hash, until it arrives at some recent header.
253+
254+ Once the finality chain is proved for a header, the state matching the header is downloaded much like during
255+ the fast sync. The state is verified to match the header storage root. After the state is imported into the
256+ database it is queried for the information that allows GRANDPA and BABE to continue operating from that state.
257+ This includes BABE epoch information and GRANDPA authority set id.
258+
259+ ### Background block download.
260+
261+ After the latest state has been imported the node is fully operational, but is still missing historic block
262+ data. I.e. it is unable to serve bock bodies and headers other than the most recent one. To make sure all
263+ nodes have block history available, a background sync process is started that downloads all the missing blocks.
264+ It is run in parallel with the keep-up sync and does not interfere with downloading of the recent blocks.
265+ During this download we also import GRANPA justifications for blocks with authority set changes, so that
266+ The warp-synced node has all the data to serve for other nodes nodes that might want to sync from it with
267+ any method.
268+
206269# Usage
207270
208271Using the ` sc-network ` crate is done through the [ ` NetworkWorker ` ] struct. Create this
0 commit comments