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.

333 lines
23 KiB

  1. # ADR 71: Proposer-Based Timestamps
  2. ## Changelog
  3. - July 15 2021: Created by @williambanfield
  4. - Aug 4 2021: Draft completed by @williambanfield
  5. - Aug 5 2021: Draft updated to include data structure changes by @williambanfield
  6. - Aug 20 2021: Language edits completed by @williambanfield
  7. - Oct 25 2021: Update the ADR to match updated spec from @cason by @williambanfield
  8. - Nov 10 2021: Additional language updates by @williambanfield per feedback from @cason
  9. - Feb 2 2022: Synchronize logic for timely with latest version of the spec by @williambanfield
  10. ## Status
  11. **Accepted**
  12. ## Context
  13. Tendermint currently provides a monotonically increasing source of time known as [BFTTime](https://github.com/tendermint/spec/blob/master/spec/consensus/bft-time.md).
  14. This mechanism for producing a source of time is reasonably simple.
  15. Each correct validator adds a timestamp to each `Precommit` message it sends.
  16. The timestamp it sends is either the validator's current known Unix time or one millisecond greater than the previous block time, depending on which value is greater.
  17. When a block is produced, the proposer chooses the block timestamp as the weighted median of the times in all of the `Precommit` messages the proposer received.
  18. The weighting is proportional to the amount of voting power, or stake, a validator has on the network.
  19. This mechanism for producing timestamps is both deterministic and byzantine fault tolerant.
  20. This current mechanism for producing timestamps has a few drawbacks.
  21. Validators do not have to agree at all on how close the selected block timestamp is to their own currently known Unix time.
  22. Additionally, any amount of voting power `>1/3` may directly control the block timestamp.
  23. As a result, it is quite possible that the timestamp is not particularly meaningful.
  24. These drawbacks present issues in the Tendermint protocol.
  25. Timestamps are used by light clients to verify blocks.
  26. Light clients rely on correspondence between their own currently known Unix time and the block timestamp to verify blocks they see;
  27. However, their currently known Unix time may be greatly divergent from the block timestamp as a result of the limitations of `BFTTime`.
  28. The proposer-based timestamps specification suggests an alternative approach for producing block timestamps that remedies these issues.
  29. Proposer-based timestamps alter the current mechanism for producing block timestamps in two main ways:
  30. 1. The block proposer is amended to offer up its currently known Unix time as the timestamp for the next block instead of the `BFTTime`.
  31. 1. Correct validators only approve the proposed block timestamp if it is close enough to their own currently known Unix time.
  32. The result of these changes is a more meaningful timestamp that cannot be controlled by `<= 2/3` of the validator voting power.
  33. This document outlines the necessary code changes in Tendermint to implement the corresponding [proposer-based timestamps specification](https://github.com/tendermint/spec/tree/master/spec/consensus/proposer-based-timestamp).
  34. ## Alternative Approaches
  35. ### Remove timestamps altogether
  36. Computer clocks are bound to skew for a variety of reasons.
  37. Using timestamps in our protocol means either accepting the timestamps as not reliable or impacting the protocol’s liveness guarantees.
  38. This design requires impacting the protocol’s liveness in order to make the timestamps more reliable.
  39. An alternate approach is to remove timestamps altogether from the block protocol.
  40. `BFTTime` is deterministic but may be arbitrarily inaccurate.
  41. However, having a reliable source of time is quite useful for applications and protocols built on top of a blockchain.
  42. We therefore decided not to remove the timestamp.
  43. Applications often wish for some transactions to occur on a certain day, on a regular period, or after some time following a different event.
  44. All of these require some meaningful representation of agreed upon time.
  45. The following protocols and application features require a reliable source of time:
  46. * Tendermint Light Clients [rely on correspondence between their known time](https://github.com/tendermint/spec/blob/master/spec/light-client/verification/README.md#definitions-1) and the block time for block verification.
  47. * Tendermint Evidence validity is determined [either in terms of heights or in terms of time](https://github.com/tendermint/spec/blob/8029cf7a0fcc89a5004e173ec065aa48ad5ba3c8/spec/consensus/evidence.md#verification).
  48. * Unbonding of staked assets in the Cosmos Hub [occurs after a period of 21 days](https://github.com/cosmos/governance/blob/ce75de4019b0129f6efcbb0e752cd2cc9e6136d3/params-change/Staking.md#unbondingtime).
  49. * IBC packets can use either a [timestamp or a height to timeout packet delivery](https://docs.cosmos.network/v0.43/ibc/overview.html#acknowledgements).
  50. Finally, inflation distribution in the Cosmos Hub uses an approximation of time to calculate an annual percentage rate.
  51. This approximation of time is calculated using [block heights with an estimated number of blocks produced in a year](https://github.com/cosmos/governance/blob/master/params-change/Mint.md#blocksperyear).
  52. Proposer-based timestamps will allow this inflation calculation to use a more meaningful and accurate source of time.
  53. ## Decision
  54. Implement proposer-based timestamps and remove `BFTTime`.
  55. ## Detailed Design
  56. ### Overview
  57. Implementing proposer-based timestamps will require a few changes to Tendermint’s code.
  58. These changes will be to the following components:
  59. * The `internal/consensus/` package.
  60. * The `state/` package.
  61. * The `Vote`, `CommitSig` and `Header` types.
  62. * The consensus parameters.
  63. ### Changes to `CommitSig`
  64. The [CommitSig](https://github.com/tendermint/tendermint/blob/a419f4df76fe4aed668a6c74696deabb9fe73211/types/block.go#L604) struct currently contains a timestamp.
  65. This timestamp is the current Unix time known to the validator when it issued a `Precommit` for the block.
  66. This timestamp is no longer used and will be removed in this change.
  67. `CommitSig` will be updated as follows:
  68. ```diff
  69. type CommitSig struct {
  70. BlockIDFlag BlockIDFlag `json:"block_id_flag"`
  71. ValidatorAddress Address `json:"validator_address"`
  72. -- Timestamp time.Time `json:"timestamp"`
  73. Signature []byte `json:"signature"`
  74. }
  75. ```
  76. ### Changes to `Vote` messages
  77. `Precommit` and `Prevote` messages use a common [Vote struct](https://github.com/tendermint/tendermint/blob/a419f4df76fe4aed668a6c74696deabb9fe73211/types/vote.go#L50).
  78. This struct currently contains a timestamp.
  79. This timestamp is set using the [voteTime](https://github.com/tendermint/tendermint/blob/e8013281281985e3ada7819f42502b09623d24a0/internal/consensus/state.go#L2241) function and therefore vote times correspond to the current Unix time known to the validator, provided this time is greater than the timestamp of the previous block.
  80. For precommits, this timestamp is used to construct the [CommitSig that is included in the block in the LastCommit](https://github.com/tendermint/tendermint/blob/e8013281281985e3ada7819f42502b09623d24a0/types/block.go#L754) field.
  81. For prevotes, this field is currently unused.
  82. Proposer-based timestamps will use the timestamp that the proposer sets into the block and will therefore no longer require that a timestamp be included in the vote messages.
  83. This timestamp is therefore no longer useful as part of consensus and may optionally be dropped from the message.
  84. `Vote` will be updated as follows:
  85. ```diff
  86. type Vote struct {
  87. Type tmproto.SignedMsgType `json:"type"`
  88. Height int64 `json:"height"`
  89. Round int32 `json:"round"`
  90. BlockID BlockID `json:"block_id"` // zero if vote is nil.
  91. -- Timestamp time.Time `json:"timestamp"`
  92. ValidatorAddress Address `json:"validator_address"`
  93. ValidatorIndex int32 `json:"validator_index"`
  94. Signature []byte `json:"signature"`
  95. }
  96. ```
  97. ### New consensus parameters
  98. The proposer-based timestamp specification includes a pair of new parameters that must be the same among all validators.
  99. These parameters are `PRECISION`, and `MSGDELAY`.
  100. The `PRECISION` and `MSGDELAY` parameters are used to determine if the proposed timestamp is acceptable.
  101. A validator will only Prevote a proposal if the proposal timestamp is considered `timely`.
  102. A proposal timestamp is considered `timely` if it is within `PRECISION` and `MSGDELAY` of the Unix time known to the validator.
  103. More specifically, a proposal timestamp is `timely` if `proposalTimestamp - PRECISION ≤ validatorLocalTime ≤ proposalTimestamp + PRECISION + MSGDELAY`.
  104. Because the `PRECISION` and `MSGDELAY` parameters must be the same across all validators, they will be added to the [consensus parameters](https://github.com/tendermint/tendermint/blob/master/proto/tendermint/types/params.proto#L13) as [durations](https://developers.google.com/protocol-buffers/docs/reference/google.protobuf#google.protobuf.Duration).
  105. The consensus parameters will be updated to include this `Synchrony` field as follows:
  106. ```diff
  107. type ConsensusParams struct {
  108. Block BlockParams `json:"block"`
  109. Evidence EvidenceParams `json:"evidence"`
  110. Validator ValidatorParams `json:"validator"`
  111. Version VersionParams `json:"version"`
  112. ++ Synchrony SynchronyParams `json:"synchrony"`
  113. }
  114. ```
  115. ```go
  116. type SynchronyParams struct {
  117. MessageDelay time.Duration `json:"message_delay"`
  118. Precision time.Duration `json:"precision"`
  119. }
  120. ```
  121. ### Changes to the block proposal step
  122. #### Proposer selects block timestamp
  123. Tendermint currently uses the `BFTTime` algorithm to produce the block's `Header.Timestamp`.
  124. The [proposal logic](https://github.com/tendermint/tendermint/blob/68ca65f5d79905abd55ea999536b1a3685f9f19d/internal/state/state.go#L269) sets the weighted median of the times in the `LastCommit.CommitSigs` as the proposed block's `Header.Timestamp`.
  125. In proposer-based timestamps, the proposer will still set a timestamp into the `Header.Timestamp`.
  126. The timestamp the proposer sets into the `Header` will change depending on if the block has previously received a [polka](https://github.com/tendermint/tendermint/blob/053651160f496bb44b107a434e3e6482530bb287/docs/introduction/what-is-tendermint.md#consensus-overview) or not.
  127. #### Proposal of a block that has not previously received a polka
  128. If a proposer is proposing a new block then it will set the Unix time currently known to the proposer into the `Header.Timestamp` field.
  129. The proposer will also set this same timestamp into the `Timestamp` field of the `Proposal` message that it issues.
  130. #### Re-proposal of a block that has previously received a polka
  131. If a proposer is re-proposing a block that has previously received a polka on the network, then the proposer does not update the `Header.Timestamp` of that block.
  132. Instead, the proposer simply re-proposes the exact same block.
  133. This way, the proposed block has the exact same block ID as the previously proposed block and the validators that have already received that block do not need to attempt to receive it again.
  134. The proposer will set the re-proposed block's `Header.Timestamp` as the `Proposal` message's `Timestamp`.
  135. #### Proposer waits
  136. Block timestamps must be monotonically increasing.
  137. In `BFTTime`, if a validator’s clock was behind, the [validator added 1 millisecond to the previous block’s time and used that in its vote messages](https://github.com/tendermint/tendermint/blob/e8013281281985e3ada7819f42502b09623d24a0/internal/consensus/state.go#L2246).
  138. A goal of adding proposer-based timestamps is to enforce some degree of clock synchronization, so having a mechanism that completely ignores the Unix time of the validator time no longer works.
  139. Validator clocks will not be perfectly in sync.
  140. Therefore, the proposer’s current known Unix time may be less than the previous block's `Header.Time`.
  141. If the proposer’s current known Unix time is less than the previous block's `Header.Time`, the proposer will sleep until its known Unix time exceeds it.
  142. This change will require amending the [defaultDecideProposal](https://github.com/tendermint/tendermint/blob/822893615564cb20b002dd5cf3b42b8d364cb7d9/internal/consensus/state.go#L1180) method.
  143. This method should now schedule a timeout that fires when the proposer’s time is greater than the previous block's `Header.Time`.
  144. When the timeout fires, the proposer will finally issue the `Proposal` message.
  145. ### Changes to proposal validation rules
  146. The rules for validating a proposed block will be modified to implement proposer-based timestamps.
  147. We will change the validation logic to ensure that a proposal is `timely`.
  148. Per the proposer-based timestamps spec, `timely` only needs to be checked if a block has not received a +2/3 majority of `Prevotes` in a round.
  149. If a block previously received a +2/3 majority of prevotes in a previous round, then +2/3 of the voting power considered the block's timestamp near enough to their own currently known Unix time in that round.
  150. The validation logic will be updated to check `timely` for blocks that did not previously receive +2/3 prevotes in a round.
  151. Receiving +2/3 prevotes in a round is frequently referred to as a 'polka' and we will use this term for simplicity.
  152. #### Current timestamp validation logic
  153. To provide a better understanding of the changes needed to timestamp validation, we will first detail how timestamp validation works currently in Tendermint.
  154. The [validBlock function](https://github.com/tendermint/tendermint/blob/c3ae6f5b58e07b29c62bfdc5715b6bf8ae5ee951/state/validation.go#L14) currently [validates the proposed block timestamp in three ways](https://github.com/tendermint/tendermint/blob/c3ae6f5b58e07b29c62bfdc5715b6bf8ae5ee951/state/validation.go#L118).
  155. First, the validation logic checks that this timestamp is greater than the previous block’s timestamp.
  156. Second, it validates that the block timestamp is correctly calculated as the weighted median of the timestamps in the [block’s LastCommit](https://github.com/tendermint/tendermint/blob/e8013281281985e3ada7819f42502b09623d24a0/types/block.go#L48).
  157. Finally, the validation logic authenticates the timestamps in the `LastCommit.CommitSig`.
  158. The cryptographic signature in each `CommitSig` is created by signing a hash of fields in the block with the voting validator’s private key.
  159. One of the items in this `signedBytes` hash is the timestamp in the `CommitSig`.
  160. To authenticate the `CommitSig` timestamp, the validator authenticating votes builds a hash of fields that includes the `CommitSig` timestamp and checks this hash against the signature.
  161. This takes place in the [VerifyCommit function](https://github.com/tendermint/tendermint/blob/e8013281281985e3ada7819f42502b09623d24a0/types/validation.go#L25).
  162. #### Remove unused timestamp validation logic
  163. `BFTTime` validation is no longer applicable and will be removed.
  164. This means that validators will no longer check that the block timestamp is a weighted median of `LastCommit` timestamps.
  165. Specifically, we will remove the call to [MedianTime in the validateBlock function](https://github.com/tendermint/tendermint/blob/4db71da68e82d5cb732b235eeb2fd69d62114b45/state/validation.go#L117).
  166. The `MedianTime` function can be completely removed.
  167. Since `CommitSig`s will no longer contain a timestamp, the validator authenticating a commit will no longer include the `CommitSig` timestamp in the hash of fields it builds to check against the cryptographic signature.
  168. #### Timestamp validation when a block has not received a polka
  169. The [POLRound](https://github.com/tendermint/tendermint/blob/68ca65f5d79905abd55ea999536b1a3685f9f19d/types/proposal.go#L29) in the `Proposal` message indicates which round the block received a polka.
  170. A negative value in the `POLRound` field indicates that the block has not previously been proposed on the network.
  171. Therefore the validation logic will check for timely when `POLRound < 0`.
  172. When a validator receives a `Proposal` message, the validator will check that the `Proposal.Timestamp` is at most `PRECISION` greater than the current Unix time known to the validator, and at maximum `PRECISION + MSGDELAY` less than the current Unix time known to the validator.
  173. If the timestamp is not within these bounds, the proposed block will not be considered `timely`.
  174. Once a full block matching the `Proposal` message is received, the validator will also check that the timestamp in the `Header.Timestamp` of the block matches this `Proposal.Timestamp`.
  175. Using the `Proposal.Timestamp` to check `timely` allows for the `MSGDELAY` parameter to be more finely tuned since `Proposal` messages do not change sizes and are therefore faster to gossip than full blocks across the network.
  176. A validator will also check that the proposed timestamp is greater than the timestamp of the block for the previous height.
  177. If the timestamp is not greater than the previous block's timestamp, the block will not be considered valid, which is the same as the current logic.
  178. #### Timestamp validation when a block has received a polka
  179. When a block is re-proposed that has already received a +2/3 majority of `Prevote`s on the network, the `Proposal` message for the re-proposed block is created with a `POLRound` that is `>= 0`.
  180. A validator will not check that the `Proposal` is `timely` if the propose message has a non-negative `POLRound`.
  181. If the `POLRound` is non-negative, each validator will simply ensure that it received the `Prevote` messages for the proposed block in the round indicated by `POLRound`.
  182. If the validator does not receive `Prevote` messages for the proposed block before the proposal timeout, then it will prevote nil.
  183. Validators already check that +2/3 prevotes were seen in `POLRound`, so this does not represent a change to the prevote logic.
  184. A validator will also check that the proposed timestamp is greater than the timestamp of the block for the previous height.
  185. If the timestamp is not greater than the previous block's timestamp, the block will not be considered valid, which is the same as the current logic.
  186. Additionally, this validation logic can be updated to check that the `Proposal.Timestamp` matches the `Header.Timestamp` of the proposed block, but it is less relevant since checking that votes were received is sufficient to ensure the block timestamp is correct.
  187. #### Relaxation of the 'Timely' check
  188. The `Synchrony` parameters, `MessageDelay` and `Precision` provide a means to bound the timestamp of a proposed block.
  189. Selecting values that are too small presents a possible liveness issue for the network.
  190. If a Tendermint network selects a `MessageDelay` parameter that does not accurately reflect the time to broadcast a proposal message to all of the validators on the network, nodes will begin rejecting proposals from otherwise correct proposers because these proposals will appear to be too far in the past.
  191. `MessageDelay` and `Precision` are planned to be configured as `ConsensusParams`.
  192. A very common way to update `ConsensusParams` is by executing a transaction included in a block that specifies new values for them.
  193. However, if the network is unable to produce blocks because of this liveness issue, no such transaction may be executed.
  194. To prevent this dangerous condition, we will add a relaxation mechanism to the `Timely` predicate.
  195. If consensus takes more than 10 rounds to produce a block for any reason, the `MessageDelay` will be doubled.
  196. This doubling will continue for each subsequent 10 rounds of consensus.
  197. This will enable chains that selected too small of a value for the `MessageDelay` parameter to eventually issue a transaction and readjust the parameters to more accurately reflect the broadcast time.
  198. This liveness issue is not as problematic for chains with very small `Precision` values.
  199. Operators can more easily readjust local validator clocks to be more aligned.
  200. Additionally, chains that wish to increase a small `Precision` value can still take advantage of the `MessageDelay` relaxation, waiting for the `MessageDelay` value to grow significantly and issuing proposals with timestamps that are far in the past of their peers.
  201. For more discussion of this, see [issue 371](https://github.com/tendermint/spec/issues/371).
  202. ### Changes to the prevote step
  203. Currently, a validator will prevote a proposal in one of three cases:
  204. * Case 1: Validator has no locked block and receives a valid proposal.
  205. * Case 2: Validator has a locked block and receives a valid proposal matching its locked block.
  206. * Case 3: Validator has a locked block, sees a valid proposal not matching its locked block but sees +2/3 prevotes for the proposal’s block, either in the current round or in a round greater than or equal to the round in which it locked its locked block.
  207. The only change we will make to the prevote step is to what a validator considers a valid proposal as detailed above.
  208. ### Changes to the precommit step
  209. The precommit step will not require much modification.
  210. Its proposal validation rules will change in the same ways that validation will change in the prevote step with the exception of the `timely` check: precommit validation will never check that the timestamp is `timely`.
  211. ### Remove voteTime Completely
  212. [voteTime](https://github.com/tendermint/tendermint/blob/822893615564cb20b002dd5cf3b42b8d364cb7d9/internal/consensus/state.go#L2229) is a mechanism for calculating the next `BFTTime` given both the validator's current known Unix time and the previous block timestamp.
  213. If the previous block timestamp is greater than the validator's current known Unix time, then voteTime returns a value one millisecond greater than the previous block timestamp.
  214. This logic is used in multiple places and is no longer needed for proposer-based timestamps.
  215. It should therefore be removed completely.
  216. ## Future Improvements
  217. * Implement BLS signature aggregation.
  218. By removing fields from the `Precommit` messages, we are able to aggregate signatures.
  219. ## Consequences
  220. ### Positive
  221. * `<2/3` of validators can no longer influence block timestamps.
  222. * Block timestamp will have stronger correspondence to real time.
  223. * Improves the reliability of light client block verification.
  224. * Enables BLS signature aggregation.
  225. * Enables evidence handling to use time instead of height for evidence validity.
  226. ### Neutral
  227. * Alters Tendermint’s liveness properties.
  228. Liveness now requires that all correct validators have synchronized clocks within a bound.
  229. Liveness will now also require that validators’ clocks move forward, which was not required under `BFTTime`.
  230. ### Negative
  231. * May increase the length of the propose step if there is a large skew between the previous proposer and the current proposer’s local Unix time.
  232. This skew will be bound by the `PRECISION` value, so it is unlikely to be too large.
  233. * Current chains with block timestamps far in the future will either need to pause consensus until after the erroneous block timestamp or must maintain synchronized but very inaccurate clocks.
  234. ## References
  235. * [PBTS Spec](https://github.com/tendermint/spec/tree/master/spec/consensus/proposer-based-timestamp)
  236. * [BFTTime spec](https://github.com/tendermint/spec/blob/master/spec/consensus/bft-time.md)
  237. * [Issue 371](https://github.com/tendermint/spec/issues/371)