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.

697 lines
31 KiB

  1. ---
  2. order: 2
  3. title: Applications
  4. ---
  5. # Applications
  6. Please ensure you've first read the spec for [ABCI Methods and Types](abci.md)
  7. Here we cover the following components of ABCI applications:
  8. - [Connection State](#state) - the interplay between ABCI connections and application state
  9. and the differences between `CheckTx` and `DeliverTx`.
  10. - [Transaction Results](#transaction-results) - rules around transaction
  11. results and validity
  12. - [Validator Set Updates](#validator-updates) - how validator sets are
  13. changed during `InitChain` and `EndBlock`
  14. - [Query](#query) - standards for using the `Query` method and proofs about the
  15. application state
  16. - [Crash Recovery](#crash-recovery) - handshake protocol to synchronize
  17. Tendermint and the application on startup.
  18. - [State Sync](#state-sync) - rapid bootstrapping of new nodes by restoring state machine snapshots
  19. ## State
  20. Since Tendermint maintains four concurrent ABCI connections, it is typical
  21. for an application to maintain a distinct state for each, and for the states to
  22. be synchronized during `Commit`.
  23. ### Concurrency
  24. In principle, each of the four ABCI connections operate concurrently with one
  25. another. This means applications need to ensure access to state is
  26. thread safe. In practice, both the
  27. [default in-process ABCI client](https://github.com/tendermint/tendermint/blob/v0.34.4/abci/client/local_client.go#L18)
  28. and the
  29. [default Go ABCI
  30. server](https://github.com/tendermint/tendermint/blob/v0.34.4/abci/server/socket_server.go#L32)
  31. use global locks across all connections, so they are not
  32. concurrent at all. This means if your app is written in Go, and compiled in-process with Tendermint
  33. using the default `NewLocalClient`, or run out-of-process using the default `SocketServer`,
  34. ABCI messages from all connections will be linearizable (received one at a
  35. time).
  36. The existence of this global mutex means Go application developers can get
  37. thread safety for application state by routing *all* reads and writes through the ABCI
  38. system. Thus it may be *unsafe* to expose application state directly to an RPC
  39. interface, and unless explicit measures are taken, all queries should be routed through the ABCI Query method.
  40. ### BeginBlock
  41. The BeginBlock request can be used to run some code at the beginning of
  42. every block. It also allows Tendermint to send the current block hash
  43. and header to the application, before it sends any of the transactions.
  44. The app should remember the latest height and header (ie. from which it
  45. has run a successful Commit) so that it can tell Tendermint where to
  46. pick up from when it restarts. See information on the Handshake, below.
  47. ### Commit
  48. Application state should only be persisted to disk during `Commit`.
  49. Before `Commit` is called, Tendermint locks and flushes the mempool so that no new messages will
  50. be received on the mempool connection. This provides an opportunity to safely update all four connection
  51. states to the latest committed state at once.
  52. When `Commit` completes, it unlocks the mempool.
  53. WARNING: if the ABCI app logic processing the `Commit` message sends a
  54. `/broadcast_tx_sync` or `/broadcast_tx_commit` and waits for the response
  55. before proceeding, it will deadlock. Executing those `broadcast_tx` calls
  56. involves acquiring a lock that is held during the `Commit` call, so it's not
  57. possible. If you make the call to the `broadcast_tx` endpoints concurrently,
  58. that's no problem, it just can't be part of the sequential logic of the
  59. `Commit` function.
  60. ### Consensus Connection
  61. The Consensus Connection should maintain a `DeliverTxState` -
  62. the working state for block execution. It should be updated by the calls to
  63. `BeginBlock`, `DeliverTx`, and `EndBlock` during block execution and committed to
  64. disk as the "latest committed state" during `Commit`.
  65. Updates made to the DeliverTxState by each method call must be readable by each subsequent method -
  66. ie. the updates are linearizable.
  67. - [BeginBlock](#beginblock)
  68. - [EndBlock](#endblock)
  69. - [Deliver Tx](#delivertx)
  70. - [Commit](#commit)
  71. ### Mempool Connection
  72. The mempool Connection should maintain a `CheckTxState`
  73. to sequentially process pending transactions in the mempool that have
  74. not yet been committed. It should be initialized to the latest committed state
  75. at the end of every `Commit`.
  76. The CheckTxState may be updated concurrently with the DeliverTxState, as
  77. messages may be sent concurrently on the Consensus and Mempool connections. However,
  78. before calling `Commit`, Tendermint will lock and flush the mempool connection,
  79. ensuring that all existing CheckTx are responded to and no new ones can
  80. begin.
  81. After `Commit`, CheckTx is run again on all transactions that remain in the
  82. node's local mempool after filtering those included in the block. To prevent the
  83. mempool from rechecking all transactions every time a block is committed, set
  84. the configuration option `mempool.recheck=false`. As of Tendermint v0.32.1,
  85. an additional `Type` parameter is made available to the CheckTx function that
  86. indicates whether an incoming transaction is new (`CheckTxType_New`), or a
  87. recheck (`CheckTxType_Recheck`).
  88. Finally, the mempool will unlock and new transactions can be processed through CheckTx again.
  89. Note that CheckTx doesn't have to check everything that affects transaction validity; the
  90. expensive things can be skipped. In fact, CheckTx doesn't have to check
  91. anything; it might say that any transaction is a valid transaction.
  92. Unlike DeliverTx, CheckTx is just there as
  93. a sort of weak filter to keep invalid transactions out of the blockchain. It's
  94. weak, because a Byzantine node doesn't care about CheckTx; it can propose a
  95. block full of invalid transactions if it wants.
  96. #### Replay Protection
  97. To prevent old transactions from being replayed, CheckTx must implement
  98. replay protection.
  99. Tendermint provides the first defense layer by keeping a lightweight
  100. in-memory cache of 100k (`[mempool] cache_size`) last transactions in
  101. the mempool. If Tendermint is just started or the clients sent more than
  102. 100k transactions, old transactions may be sent to the application. So
  103. it is important CheckTx implements some logic to handle them.
  104. If there are cases in your application where a transaction may become invalid in some
  105. future state, you probably want to disable Tendermint's
  106. cache. You can do that by setting `[mempool] cache_size = 0` in the
  107. config.
  108. ### Query Connection
  109. The Info Connection should maintain a `QueryState` for answering queries from the user,
  110. and for initialization when Tendermint first starts up (both described further
  111. below).
  112. It should always contain the latest committed state associated with the
  113. latest committed block.
  114. QueryState should be set to the latest `DeliverTxState` at the end of every `Commit`,
  115. ie. after the full block has been processed and the state committed to disk.
  116. Otherwise it should never be modified.
  117. Tendermint Core currently uses the Query connection to filter peers upon
  118. connecting, according to IP address or node ID. For instance,
  119. returning non-OK ABCI response to either of the following queries will
  120. cause Tendermint to not connect to the corresponding peer:
  121. - `p2p/filter/addr/<ip addr>`, where `<ip addr>` is an IP address.
  122. - `p2p/filter/id/<id>`, where `<is>` is the hex-encoded node ID (the hash of
  123. the node's p2p pubkey).
  124. Note: these query formats are subject to change!
  125. ### Snapshot Connection
  126. The Snapshot Connection is optional, and is only used to serve state sync snapshots for other nodes
  127. and/or restore state sync snapshots to a local node being bootstrapped.
  128. ## Transaction Results
  129. The `Info` and `Log` fields are non-deterministic values for debugging/convenience purposes
  130. that are otherwise ignored.
  131. The `Data` field must be strictly deterministic, but can be arbitrary data.
  132. ### Gas
  133. Ethereum introduced the notion of `gas` as an abstract representation of the
  134. cost of resources used by nodes when processing transactions. Every operation in the
  135. Ethereum Virtual Machine uses some amount of gas, and gas can be accepted at a market-variable price.
  136. Users propose a maximum amount of gas for their transaction; if the tx uses less, they get
  137. the difference credited back. Tendermint adopts a similar abstraction,
  138. though uses it only optionally and weakly, allowing applications to define
  139. their own sense of the cost of execution.
  140. In Tendermint, the `ConsensusParams.Block.MaxGas` limits the amount of `gas` that can be used in a block.
  141. The default value is `-1`, meaning no limit, or that the concept of gas is
  142. meaningless.
  143. Responses contain a `GasWanted` and `GasUsed` field. The former is the maximum
  144. amount of gas the sender of a tx is willing to use, and the later is how much it actually
  145. used. Applications should enforce that `GasUsed <= GasWanted` - ie. tx execution
  146. should halt before it can use more resources than it requested.
  147. When `MaxGas > -1`, Tendermint enforces the following rules:
  148. - `GasWanted <= MaxGas` for all txs in the mempool
  149. - `(sum of GasWanted in a block) <= MaxGas` when proposing a block
  150. If `MaxGas == -1`, no rules about gas are enforced.
  151. Note that Tendermint does not currently enforce anything about Gas in the consensus, only the mempool.
  152. This means it does not guarantee that committed blocks satisfy these rules!
  153. It is the application's responsibility to return non-zero response codes when gas limits are exceeded.
  154. The `GasUsed` field is ignored completely by Tendermint. That said, applications should enforce:
  155. - `GasUsed <= GasWanted` for any given transaction
  156. - `(sum of GasUsed in a block) <= MaxGas` for every block
  157. In the future, we intend to add a `Priority` field to the responses that can be
  158. used to explicitly prioritize txs in the mempool for inclusion in a block
  159. proposal. See [#1861](https://github.com/tendermint/tendermint/issues/1861).
  160. ### CheckTx
  161. If `Code != 0`, it will be rejected from the mempool and hence
  162. not broadcasted to other peers and not included in a proposal block.
  163. `Data` contains the result of the CheckTx transaction execution, if any. It is
  164. semantically meaningless to Tendermint.
  165. `Events` include any events for the execution, though since the transaction has not
  166. been committed yet, they are effectively ignored by Tendermint.
  167. ### DeliverTx
  168. DeliverTx is the workhorse of the blockchain. Tendermint sends the
  169. DeliverTx requests asynchronously but in order, and relies on the
  170. underlying socket protocol (ie. TCP) to ensure they are received by the
  171. app in order. They have already been ordered in the global consensus by
  172. the Tendermint protocol.
  173. If DeliverTx returns `Code != 0`, the transaction will be considered invalid,
  174. though it is still included in the block.
  175. DeliverTx returns a `abci.Result`, which includes a Code, Data, and Log.
  176. `Data` contains the result of the CheckTx transaction execution, if any. It is
  177. semantically meaningless to Tendermint.
  178. Both the `Code` and `Data` are included in a structure that is hashed into the
  179. `LastResultsHash` of the next block header.
  180. `Events` include any events for the execution, which Tendermint will use to index
  181. the transaction by. This allows transactions to be queried according to what
  182. events took place during their execution.
  183. ## Validator Updates
  184. The application may set the validator set during InitChain, and update it during
  185. EndBlock.
  186. Note that the maximum total power of the validator set is bounded by
  187. `MaxTotalVotingPower = MaxInt64 / 8`. Applications are responsible for ensuring
  188. they do not make changes to the validator set that cause it to exceed this
  189. limit.
  190. Additionally, applications must ensure that a single set of updates does not contain any duplicates -
  191. a given public key can only appear in an update once. If an update includes
  192. duplicates, the block execution will fail irrecoverably.
  193. ### InitChain
  194. ResponseInitChain can return a list of validators.
  195. If the list is empty, Tendermint will use the validators loaded in the genesis
  196. file.
  197. If the list is not empty, Tendermint will use it for the validator set.
  198. This way the application can determine the initial validator set for the
  199. blockchain.
  200. ### EndBlock
  201. Updates to the Tendermint validator set can be made by returning
  202. `ValidatorUpdate` objects in the `ResponseEndBlock`:
  203. ```protobuf
  204. message ValidatorUpdate {
  205. tendermint.crypto.keys.PublicKey pub_key
  206. int64 power
  207. }
  208. message PublicKey {
  209. oneof {
  210. ed25519 bytes = 1;
  211. }
  212. ```
  213. The `pub_key` currently supports only one type:
  214. - `type = "ed25519"`
  215. The `power` is the new voting power for the validator, with the
  216. following rules:
  217. - power must be non-negative
  218. - if power is 0, the validator must already exist, and will be removed from the
  219. validator set
  220. - if power is non-0:
  221. - if the validator does not already exist, it will be added to the validator
  222. set with the given power
  223. - if the validator does already exist, its power will be adjusted to the given power
  224. - the total power of the new validator set must not exceed MaxTotalVotingPower
  225. Note the updates returned in block `H` will only take effect at block `H+2`.
  226. ## Consensus Parameters
  227. ConsensusParams enforce certain limits in the blockchain, like the maximum size
  228. of blocks, amount of gas used in a block, and the maximum acceptable age of
  229. evidence. They can be set in InitChain and updated in EndBlock.
  230. ### BlockParams.MaxBytes
  231. The maximum size of a complete Protobuf encoded block.
  232. This is enforced by Tendermint consensus.
  233. This implies a maximum tx size that is this MaxBytes, less the expected size of
  234. the header, the validator set, and any included evidence in the block.
  235. Must have `0 < MaxBytes < 100 MB`.
  236. ### BlockParams.MaxGas
  237. The maximum of the sum of `GasWanted` in a proposed block.
  238. This is *not* enforced by Tendermint consensus.
  239. It is left to the app to enforce (ie. if txs are included past the
  240. limit, they should return non-zero codes). It is used by Tendermint to limit the
  241. txs included in a proposed block.
  242. Must have `MaxGas >= -1`.
  243. If `MaxGas == -1`, no limit is enforced.
  244. ### BlockParams.TimeIotaMs
  245. The minimum time between consecutive blocks (in milliseconds).
  246. This is enforced by Tendermint consensus.
  247. Must have `TimeIotaMs > 0` to ensure time monotonicity.
  248. > *Note: This is not exposed to the application*
  249. ### EvidenceParams.MaxAgeDuration
  250. This is the maximum age of evidence in time units.
  251. This is enforced by Tendermint consensus.
  252. If a block includes evidence older than this (AND the evidence was created more
  253. than `MaxAgeNumBlocks` ago), the block will be rejected (validators won't vote
  254. for it).
  255. Must have `MaxAgeDuration > 0`.
  256. ### EvidenceParams.MaxAgeNumBlocks
  257. This is the maximum age of evidence in blocks.
  258. This is enforced by Tendermint consensus.
  259. If a block includes evidence older than this (AND the evidence was created more
  260. than `MaxAgeDuration` ago), the block will be rejected (validators won't vote
  261. for it).
  262. Must have `MaxAgeNumBlocks > 0`.
  263. ### EvidenceParams.MaxNum
  264. This is the maximum number of evidence that can be committed to a single block.
  265. The product of this and the `MaxEvidenceBytes` must not exceed the size of
  266. a block minus it's overhead ( ~ `MaxBytes`).
  267. The amount must be a positive number.
  268. ### Updates
  269. The application may set the ConsensusParams during InitChain, and update them during
  270. EndBlock. If the ConsensusParams is empty, it will be ignored. Each field
  271. that is not empty will be applied in full. For instance, if updating the
  272. Block.MaxBytes, applications must also set the other Block fields (like
  273. Block.MaxGas), even if they are unchanged, as they will otherwise cause the
  274. value to be updated to 0.
  275. #### InitChain
  276. ResponseInitChain includes a ConsensusParams.
  277. If its nil, Tendermint will use the params loaded in the genesis
  278. file. If it's not nil, Tendermint will use it.
  279. This way the application can determine the initial consensus params for the
  280. blockchain.
  281. #### EndBlock
  282. ResponseEndBlock includes a ConsensusParams.
  283. If its nil, Tendermint will do nothing.
  284. If it's not nil, Tendermint will use it.
  285. This way the application can update the consensus params over time.
  286. Note the updates returned in block `H` will take effect right away for block
  287. `H+1`.
  288. ## Query
  289. Query is a generic method with lots of flexibility to enable diverse sets
  290. of queries on application state. Tendermint makes use of Query to filter new peers
  291. based on ID and IP, and exposes Query to the user over RPC.
  292. Note that calls to Query are not replicated across nodes, but rather query the
  293. local node's state - hence they may return stale reads. For reads that require
  294. consensus, use a transaction.
  295. The most important use of Query is to return Merkle proofs of the application state at some height
  296. that can be used for efficient application-specific light-clients.
  297. Note Tendermint has technically no requirements from the Query
  298. message for normal operation - that is, the ABCI app developer need not implement
  299. Query functionality if they do not wish too.
  300. ### Query Proofs
  301. The Tendermint block header includes a number of hashes, each providing an
  302. anchor for some type of proof about the blockchain. The `ValidatorsHash` enables
  303. quick verification of the validator set, the `DataHash` gives quick
  304. verification of the transactions included in the block, etc.
  305. The `AppHash` is unique in that it is application specific, and allows for
  306. application-specific Merkle proofs about the state of the application.
  307. While some applications keep all relevant state in the transactions themselves
  308. (like Bitcoin and its UTXOs), others maintain a separated state that is
  309. computed deterministically *from* transactions, but is not contained directly in
  310. the transactions themselves (like Ethereum contracts and accounts).
  311. For such applications, the `AppHash` provides a much more efficient way to verify light-client proofs.
  312. ABCI applications can take advantage of more efficient light-client proofs for
  313. their state as follows:
  314. - return the Merkle root of the deterministic application state in
  315. `ResponseCommit.Data`.
  316. - it will be included as the `AppHash` in the next block.
  317. - return efficient Merkle proofs about that application state in `ResponseQuery.Proof`
  318. that can be verified using the `AppHash` of the corresponding block.
  319. For instance, this allows an application's light-client to verify proofs of
  320. absence in the application state, something which is much less efficient to do using the block hash.
  321. Some applications (eg. Ethereum, Cosmos-SDK) have multiple "levels" of Merkle trees,
  322. where the leaves of one tree are the root hashes of others. To support this, and
  323. the general variability in Merkle proofs, the `ResponseQuery.Proof` has some minimal structure:
  324. ```protobuf
  325. message ProofOps {
  326. repeated ProofOp ops
  327. }
  328. message ProofOp {
  329. string type = 1;
  330. bytes key = 2;
  331. bytes data = 3;
  332. }
  333. ```
  334. Each `ProofOp` contains a proof for a single key in a single Merkle tree, of the specified `type`.
  335. This allows ABCI to support many different kinds of Merkle trees, encoding
  336. formats, and proofs (eg. of presence and absence) just by varying the `type`.
  337. The `data` contains the actual encoded proof, encoded according to the `type`.
  338. When verifying the full proof, the root hash for one ProofOp is the value being
  339. verified for the next ProofOp in the list. The root hash of the final ProofOp in
  340. the list should match the `AppHash` being verified against.
  341. ### Peer Filtering
  342. When Tendermint connects to a peer, it sends two queries to the ABCI application
  343. using the following paths, with no additional data:
  344. - `/p2p/filter/addr/<IP:PORT>`, where `<IP:PORT>` denote the IP address and
  345. the port of the connection
  346. - `p2p/filter/id/<ID>`, where `<ID>` is the peer node ID (ie. the
  347. pubkey.Address() for the peer's PubKey)
  348. If either of these queries return a non-zero ABCI code, Tendermint will refuse
  349. to connect to the peer.
  350. ### Paths
  351. Queries are directed at paths, and may optionally include additional data.
  352. The expectation is for there to be some number of high level paths
  353. differentiating concerns, like `/p2p`, `/store`, and `/app`. Currently,
  354. Tendermint only uses `/p2p`, for filtering peers. For more advanced use, see the
  355. implementation of
  356. [Query in the Cosmos-SDK](https://github.com/cosmos/cosmos-sdk/blob/v0.23.1/baseapp/baseapp.go#L333).
  357. ## Crash Recovery
  358. On startup, Tendermint calls the `Info` method on the Info Connection to get the latest
  359. committed state of the app. The app MUST return information consistent with the
  360. last block it succesfully completed Commit for.
  361. If the app succesfully committed block H but not H+1, then `last_block_height = H` and `last_block_app_hash = <hash returned by Commit for block H>`. If the app
  362. failed during the Commit of block H, then `last_block_height = H-1` and
  363. `last_block_app_hash = <hash returned by Commit for block H-1, which is the hash in the header of block H>`.
  364. We now distinguish three heights, and describe how Tendermint syncs itself with
  365. the app.
  366. ```md
  367. storeBlockHeight = height of the last block Tendermint saw a commit for
  368. stateBlockHeight = height of the last block for which Tendermint completed all
  369. block processing and saved all ABCI results to disk
  370. appBlockHeight = height of the last block for which ABCI app succesfully
  371. completed Commit
  372. ```
  373. Note we always have `storeBlockHeight >= stateBlockHeight` and `storeBlockHeight >= appBlockHeight`
  374. Note also we never call Commit on an ABCI app twice for the same height.
  375. The procedure is as follows.
  376. First, some simple start conditions:
  377. If `appBlockHeight == 0`, then call InitChain.
  378. If `storeBlockHeight == 0`, we're done.
  379. Now, some sanity checks:
  380. If `storeBlockHeight < appBlockHeight`, error
  381. If `storeBlockHeight < stateBlockHeight`, panic
  382. If `storeBlockHeight > stateBlockHeight+1`, panic
  383. Now, the meat:
  384. If `storeBlockHeight == stateBlockHeight && appBlockHeight < storeBlockHeight`,
  385. replay all blocks in full from `appBlockHeight` to `storeBlockHeight`.
  386. This happens if we completed processing the block, but the app forgot its height.
  387. If `storeBlockHeight == stateBlockHeight && appBlockHeight == storeBlockHeight`, we're done.
  388. This happens if we crashed at an opportune spot.
  389. If `storeBlockHeight == stateBlockHeight+1`
  390. This happens if we started processing the block but didn't finish.
  391. If `appBlockHeight < stateBlockHeight`
  392. replay all blocks in full from `appBlockHeight` to `storeBlockHeight-1`,
  393. and replay the block at `storeBlockHeight` using the WAL.
  394. This happens if the app forgot the last block it committed.
  395. If `appBlockHeight == stateBlockHeight`,
  396. replay the last block (storeBlockHeight) in full.
  397. This happens if we crashed before the app finished Commit
  398. If `appBlockHeight == storeBlockHeight`
  399. update the state using the saved ABCI responses but dont run the block against the real app.
  400. This happens if we crashed after the app finished Commit but before Tendermint saved the state.
  401. ## State Sync
  402. A new node joining the network can simply join consensus at the genesis height and replay all
  403. historical blocks until it is caught up. However, for large chains this can take a significant
  404. amount of time, often on the order of days or weeks.
  405. State sync is an alternative mechanism for bootstrapping a new node, where it fetches a snapshot
  406. of the state machine at a given height and restores it. Depending on the application, this can
  407. be several orders of magnitude faster than replaying blocks.
  408. Note that state sync does not currently backfill historical blocks, so the node will have a
  409. truncated block history - users are advised to consider the broader network implications of this in
  410. terms of block availability and auditability. This functionality may be added in the future.
  411. For details on the specific ABCI calls and types, see the [methods and types section](abci.md).
  412. ### Taking Snapshots
  413. Applications that want to support state syncing must take state snapshots at regular intervals. How
  414. this is accomplished is entirely up to the application. A snapshot consists of some metadata and
  415. a set of binary chunks in an arbitrary format:
  416. - `Height (uint64)`: The height at which the snapshot is taken. It must be taken after the given
  417. height has been committed, and must not contain data from any later heights.
  418. - `Format (uint32)`: An arbitrary snapshot format identifier. This can be used to version snapshot
  419. formats, e.g. to switch from Protobuf to MessagePack for serialization. The application can use
  420. this when restoring to choose whether to accept or reject a snapshot.
  421. - `Chunks (uint32)`: The number of chunks in the snapshot. Each chunk contains arbitrary binary
  422. data, and should be less than 16 MB; 10 MB is a good starting point.
  423. - `Hash ([]byte)`: An arbitrary hash of the snapshot. This is used to check whether a snapshot is
  424. the same across nodes when downloading chunks.
  425. - `Metadata ([]byte)`: Arbitrary snapshot metadata, e.g. chunk hashes for verification or any other
  426. necessary info.
  427. For a snapshot to be considered the same across nodes, all of these fields must be identical. When
  428. sent across the network, snapshot metadata messages are limited to 4 MB.
  429. When a new node is running state sync and discovering snapshots, Tendermint will query an existing
  430. application via the ABCI `ListSnapshots` method to discover available snapshots, and load binary
  431. snapshot chunks via `LoadSnapshotChunk`. The application is free to choose how to implement this
  432. and which formats to use, but should provide the following guarantees:
  433. - **Consistent:** A snapshot should be taken at a single isolated height, unaffected by
  434. concurrent writes. This can e.g. be accomplished by using a data store that supports ACID
  435. transactions with snapshot isolation.
  436. - **Asynchronous:** Taking a snapshot can be time-consuming, so it should not halt chain progress,
  437. for example by running in a separate thread.
  438. - **Deterministic:** A snapshot taken at the same height in the same format should be identical
  439. (at the byte level) across nodes, including all metadata. This ensures good availability of
  440. chunks, and that they fit together across nodes.
  441. A very basic approach might be to use a datastore with MVCC transactions (such as RocksDB),
  442. start a transaction immediately after block commit, and spawn a new thread which is passed the
  443. transaction handle. This thread can then export all data items, serialize them using e.g.
  444. Protobuf, hash the byte stream, split it into chunks, and store the chunks in the file system
  445. along with some metadata - all while the blockchain is applying new blocks in parallel.
  446. A more advanced approach might include incremental verification of individual chunks against the
  447. chain app hash, parallel or batched exports, compression, and so on.
  448. Old snapshots should be removed after some time - generally only the last two snapshots are needed
  449. (to prevent the last one from being removed while a node is restoring it).
  450. ### Bootstrapping a Node
  451. An empty node can be state synced by setting the configuration option `statesync.enabled =
  452. true`. The node also needs the chain genesis file for basic chain info, and configuration for
  453. light client verification of the restored snapshot: a set of Tendermint RPC servers, and a
  454. trusted header hash and corresponding height from a trusted source, via the `statesync`
  455. configuration section.
  456. Once started, the node will connect to the P2P network and begin discovering snapshots. These
  457. will be offered to the local application, and once a snapshot is accepted Tendermint will fetch
  458. and apply the snapshot chunks. After all chunks have been successfully applied, Tendermint verifies
  459. the app's `AppHash` against the chain using the light client, then switches the node to normal
  460. consensus operation.
  461. #### Snapshot Discovery
  462. When the empty node join the P2P network, it asks all peers to report snapshots via the
  463. `ListSnapshots` ABCI call (limited to 10 per node). After some time, the node picks the most
  464. suitable snapshot (generally prioritized by height, format, and number of peers), and offers it
  465. to the application via `OfferSnapshot`. The application can choose a number of responses,
  466. including accepting or rejecting it, rejecting the offered format, rejecting the peer who sent
  467. it, and so on. Tendermint will keep discovering and offering snapshots until one is accepted or
  468. the application aborts.
  469. #### Snapshot Restoration
  470. Once a snapshot has been accepted via `OfferSnapshot`, Tendermint begins downloading chunks from
  471. any peers that have the same snapshot (i.e. that have identical metadata fields). Chunks are
  472. spooled in a temporary directory, and then given to the application in sequential order via
  473. `ApplySnapshotChunk` until all chunks have been accepted.
  474. As with taking snapshots, the method for restoring them is entirely up to the application, but will
  475. generally be the inverse of how they are taken.
  476. During restoration, the application can respond to `ApplySnapshotChunk` with instructions for how
  477. to continue. This will typically be to accept the chunk and await the next one, but it can also
  478. ask for chunks to be refetched (either the current one or any number of previous ones), P2P peers
  479. to be banned, snapshots to be rejected or retried, and a number of other responses - see the ABCI
  480. reference for details.
  481. If Tendermint fails to fetch a chunk after some time, it will reject the snapshot and try a
  482. different one via `OfferSnapshot` - the application can choose whether it wants to support
  483. restarting restoration, or simply abort with an error.
  484. #### Snapshot Verification
  485. Once all chunks have been accepted, Tendermint issues an `Info` ABCI call to retrieve the
  486. `LastBlockAppHash`. This is compared with the trusted app hash from the chain, retrieved and
  487. verified using the light client. Tendermint also checks that `LastBlockHeight` corresponds to the
  488. height of the snapshot.
  489. This verification ensures that an application is valid before joining the network. However, the
  490. snapshot restoration may take a long time to complete, so applications may want to employ additional
  491. verification during the restore to detect failures early. This might e.g. include incremental
  492. verification of each chunk against the app hash (using bundled Merkle proofs), checksums to
  493. protect against data corruption by the disk or network, and so on. However, it is important to
  494. note that the only trusted information available is the app hash, and all other snapshot metadata
  495. can be spoofed by adversaries.
  496. Apps may also want to consider state sync denial-of-service vectors, where adversaries provide
  497. invalid or harmful snapshots to prevent nodes from joining the network. The application can
  498. counteract this by asking Tendermint to ban peers. As a last resort, node operators can use
  499. P2P configuration options to whitelist a set of trusted peers that can provide valid snapshots.
  500. #### Transition to Consensus
  501. Once the snapshot has been restored, Tendermint gathers additional information necessary for
  502. bootstrapping the node (e.g. chain ID, consensus parameters, validator sets, and block headers)
  503. from the genesis file and light client RPC servers. It also fetches and records the `AppVersion`
  504. from the ABCI application.
  505. Once the node is bootstrapped with this information and the restored state machine, it transitions
  506. to fast sync (if enabled) to fetch any remaining blocks up the the chain head, and then
  507. transitions to regular consensus operation. At this point the node operates like any other node,
  508. apart from having a truncated block history at the height of the restored snapshot.