You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

77 lines
2.5 KiB

  1. # State Sync Reactor
  2. State sync allows new nodes to rapidly bootstrap and join the network by discovering, fetching,
  3. and restoring state machine snapshots. For more information, see the [state sync ABCI section](../../abci/apps.md#state-sync).
  4. The state sync reactor has two main responsibilites:
  5. * Serving state machine snapshots taken by the local ABCI application to new nodes joining the
  6. network.
  7. * Discovering existing snapshots and fetching snapshot chunks for an empty local application
  8. being bootstrapped.
  9. The state sync process for bootstrapping a new node is described in detail in the section linked
  10. above. While technically part of the reactor (see `statesync/syncer.go` and related components),
  11. this document will only cover the P2P reactor component.
  12. For details on the ABCI methods and data types, see the [ABCI documentation](../../abci/abci.md).
  13. ## State Sync P2P Protocol
  14. When a new node begin state syncing, it will ask all peers it encounters if it has any
  15. available snapshots:
  16. ```go
  17. type snapshotsRequestMessage struct{}
  18. ```
  19. The receiver will query the local ABCI application via `ListSnapshots`, and send a message
  20. containing snapshot metadata (limited to 4 MB) for each of the 10 most recent snapshots:
  21. ```go
  22. type snapshotsResponseMessage struct {
  23. Height uint64
  24. Format uint32
  25. Chunks uint32
  26. Hash []byte
  27. Metadata []byte
  28. }
  29. ```
  30. The node running state sync will offer these snapshots to the local ABCI application via
  31. `OfferSnapshot` ABCI calls, and keep track of which peers contain which snapshots. Once a snapshot
  32. is accepted, the state syncer will request snapshot chunks from appropriate peers:
  33. ```go
  34. type chunkRequestMessage struct {
  35. Height uint64
  36. Format uint32
  37. Index uint32
  38. }
  39. ```
  40. The receiver will load the requested chunk from its local application via `LoadSnapshotChunk`,
  41. and respond with it (limited to 16 MB):
  42. ```go
  43. type chunkResponseMessage struct {
  44. Height uint64
  45. Format uint32
  46. Index uint32
  47. Chunk []byte
  48. Missing bool
  49. }
  50. ```
  51. Here, `Missing` is used to signify that the chunk was not found on the peer, since an empty
  52. chunk is a valid (although unlikely) response.
  53. The returned chunk is given to the ABCI application via `ApplySnapshotChunk` until the snapshot
  54. is restored. If a chunk response is not returned within some time, it will be re-requested,
  55. possibly from a different peer.
  56. The ABCI application is able to request peer bans and chunk refetching as part of the ABCI protocol.
  57. If no state sync is in progress (i.e. during normal operation), any unsolicited response messages
  58. are discarded.