CommitSig
Commit
Vote
messagesHeader
Accepted
Tendermint currently provides a monotonically increasing source of time known as BFTTime.
This mechanism for producing a source of time is reasonably simple.
Each correct validator adds a timestamp to each Precommit
message it sends.
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.
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.
The weighting is proportional to the amount of voting power, or stake, a validator has on the network.
This mechanism for producing timestamps is both deterministic and byzantine fault tolerant.
This current mechanism for producing timestamps has a few drawbacks.
Validators do not have to agree at all on how close the selected block timestamp is to their own currently known Unix time.
Additionally, any amount of voting power >1/3
may directly control the block timestamp.
As a result, it is quite possible that the timestamp is not particularly meaningful.
These drawbacks present issues in the Tendermint protocol.
Timestamps are used by light clients to verify blocks.
Light clients rely on correspondence between their own currently known Unix time and the block timestamp to verify blocks they see;
However, their currently known Unix time may be greatly divergent from the block timestamp as a result of the limitations of BFTTime
.
The proposer-based timestamps specification suggests an alternative approach for producing block timestamps that remedies these issues. Proposer-based timestamps alter the current mechanism for producing block timestamps in two main ways:
The result of these changes is a more meaningful timestamp that cannot be controlled by <= 2/3
of the validator voting power.
This document outlines the necessary code changes in Tendermint to implement the corresponding proposer-based timestamps specification.
Computer clocks are bound to skew for a variety of reasons.
Using timestamps in our protocol means either accepting the timestamps as not reliable or impacting the protocol’s liveness guarantees.
This design requires impacting the protocol’s liveness in order to make the timestamps more reliable.
An alternate approach is to remove timestamps altogether from the block protocol.
BFTTime
is deterministic but may be arbitrarily inaccurate.
However, having a reliable source of time is quite useful for applications and protocols built on top of a blockchain.
We therefore decided not to remove the timestamp. Applications often wish for some transactions to occur on a certain day, on a regular period, or after some time following a different event. All of these require some meaningful representation of agreed upon time. The following protocols and application features require a reliable source of time:
Finally, inflation distribution in the Cosmos Hub uses an approximation of time to calculate an annual percentage rate. This approximation of time is calculated using block heights with an estimated number of blocks produced in a year. Proposer-based timestamps will allow this inflation calculation to use a more meaningful and accurate source of time.
Implement proposer-based timestamps and remove BFTTime
.
Implementing proposer-based timestamps will require a few changes to Tendermint’s code. These changes will be to the following components:
internal/consensus/
package.state/
package.Vote
, CommitSig
, Commit
and Header
types.This design discusses two timestamps: (1) The timestamp in the block and (2) the timestamp in the proposal message. The existence and use of both of these timestamps can get a bit confusing, so some background is given here to clarify their uses.
The proposal message currently has a timestamp.
This timestamp is the current Unix time known to the proposer when sending the Proposal
message.
This timestamp is not currently used as part of consensus.
The changes in this ADR will begin using the proposal message timestamp as part of consensus.
We will refer to this as the proposal timestamp throughout this design.
The block has a timestamp field in the header.
This timestamp is set currently as part of Tendermint’s BFTtime
algorithm.
It is set when a block is proposed and it is checked by the validators when they are deciding to prevote the block.
This field will continue to be used but the logic for creating and validating this timestamp will change.
We will refer to this as the block timestamp throughout this design.
At a high level, the proposal timestamp from height H
is used as the block timestamp at height H+1
.
The following image shows this relationship.
The rest of this document describes the code changes that will make this possible.
Currently, BFTtime
uses LastCommit
to construct the block timestamp.
The LastCommit
is created at height H-1
and is saved in the state store to be included in the block at height H
.
BFTtime
takes the weighted median of the timestamps in LastCommit.CommitSig
to build the timestamp for height H
.
For proposer-based timestamps, the LastCommit.CommitSig
timestamps will no longer be used to build the timestamps for height H
.
Instead, the proposal timestamp from height H-1
will become the block timestamp for height H
.
To enable this, we will add a Timestamp
field to the Commit
struct.
This field will be populated at each height with the proposal timestamp decided on at the previous height.
This timestamp will also be saved with the rest of the commit in the state store when the commit is finalized so that it can be recovered if Tendermint crashes.
Changes to the CommitSig
and Commit
struct are detailed below.
CommitSig
The CommitSig struct currently contains a timestamp.
This timestamp is the current Unix time known to the validator when it issued a Precommit
for the block.
This timestamp is no longer used and will be removed in this change.
CommitSig
will be updated as follows:
type CommitSig struct {
BlockIDFlag BlockIDFlag `json:"block_id_flag"`
ValidatorAddress Address `json:"validator_address"`
-- Timestamp time.Time `json:"timestamp"`
Signature []byte `json:"signature"`
}
Commit
The Commit struct does not currently contain a timestamp.
The timestamps in the Commit.CommitSig
entries are currently used to build the block timestamp.
With these timestamps removed, the commit time will instead be stored in the Commit
struct.
Commit
will be updated as follows.
type Commit struct {
Height int64 `json:"height"`
Round int32 `json:"round"`
++ Timestamp time.Time `json:"timestamp"`
BlockID BlockID `json:"block_id"`
Signatures []CommitSig `json:"signatures"`
}
Vote
messagesPrecommit
and Prevote
messages use a common Vote struct.
This struct currently contains a timestamp.
This timestamp is set using the voteTime function and therefore vote times correspond to the current Unix time known to the validator.
For precommits, this timestamp is used to construct the CommitSig that is included in the block in the LastCommit field.
For prevotes, this field is unused.
Proposer-based timestamps will use the RoundState.Proposal timestamp to construct the signedBytes
CommitSig
.
This timestamp is therefore no longer useful and will be dropped.
Vote
will be updated as follows:
type Vote struct {
Type tmproto.SignedMsgType `json:"type"`
Height int64 `json:"height"`
Round int32 `json:"round"`
BlockID BlockID `json:"block_id"` // zero if vote is nil.
-- Timestamp time.Time `json:"timestamp"`
ValidatorAddress Address `json:"validator_address"`
ValidatorIndex int32 `json:"validator_index"`
Signature []byte `json:"signature"`
}
The proposer-based timestamp specification includes multiple new parameters that must be the same among all validators.
These parameters are PRECISION
, MSGDELAY
, and ACCURACY
.
The PRECISION
and MSGDELAY
parameters are used to determine if the proposed timestamp is acceptable.
A validator will only Prevote a proposal if the proposal timestamp is considered timely
.
A proposal timestamp is considered timely
if it is within PRECISION
and MSGDELAY
of the Unix time known to the validator.
More specifically, a proposal timestamp is timely
if validatorLocalTime - PRECISION < proposalTime < validatorLocalTime + PRECISION + MSGDELAY
.
Because the PRECISION
and MSGDELAY
parameters must be the same across all validators, they will be added to the consensus parameters as durations.
The proposer-based timestamp specification also includes a new ACCURACY parameter.
Intuitively, ACCURACY
represents the difference between the ‘real’ time and the currently known time of correct validators.
The currently known Unix time of any validator is always somewhat different from real time.
ACCURACY
is the largest such difference between each validator's time and real time taken as an absolute value.
This is not something a computer can determine on its own and must be specified as an estimate by community running a Tendermint-based chain.
It is used in the new algorithm to calculate a timeout for the propose step.
ACCURACY
is assumed to be the same across all validators and therefore should be included as a consensus parameter.
The consensus will be updated to include this Timestamp
field as follows:
type ConsensusParams struct {
Block BlockParams `json:"block"`
Evidence EvidenceParams `json:"evidence"`
Validator ValidatorParams `json:"validator"`
Version VersionParams `json:"version"`
++ Timestamp TimestampParams `json:"timestamp"`
}
type TimestampParams struct {
Accuracy time.Duration `json:"accuracy"`
Precision time.Duration `json:"precision"`
MsgDelay time.Duration `json:"msg_delay"`
}
Header
The Header struct currently contains a timestamp.
This timestamp is set as the BFTtime
derived from the block's LastCommit.CommitSig
timestamps.
This timestamp will no longer be derived from the LastCommit.CommitSig
timestamps and will instead be included directly into the block's LastCommit
.
This timestamp will therfore be identical in both the Header
and the LastCommit
.
To clarify that the timestamp in the header corresponds to the LastCommit
's time, we will rename this timestamp field to last_timestamp
.
Header
will be updated as follows:
type Header struct {
// basic block info
Version version.Consensus `json:"version"`
ChainID string `json:"chain_id"`
Height int64 `json:"height"`
-- Time time.Time `json:"time"`
++ LastTimestamp time.Time `json:"last_timestamp"`
// prev block info
LastBlockID BlockID `json:"last_block_id"`
// hashes of block data
LastCommitHash tmbytes.HexBytes `json:"last_commit_hash"`
DataHash tmbytes.HexBytes `json:"data_hash"`
// hashes from the app output from the prev block
ValidatorsHash tmbytes.HexBytes `json:"validators_hash"`
NextValidatorsHash tmbytes.HexBytes `json:"next_validators_hash"`
ConsensusHash tmbytes.HexBytes `json:"consensus_hash"`
AppHash tmbytes.HexBytes `json:"app_hash"`
// root hash of all results from the txs from the previous block
LastResultsHash tmbytes.HexBytes `json:"last_results_hash"`
// consensus info
EvidenceHash tmbytes.HexBytes `json:"evidence_hash"`
ProposerAddress Address `json:"proposer_address"`
}
The proposal logic already sets the Unix time known to the validator into the Proposal
message.
This satisfies the proposer-based timestamp specification and does not need to change.
The proposal timestamp that was decided in height H-1
will be stored in the State
struct's in the RoundState.LastCommit
field.
The proposer will select this timestamp to use as the block timestamp at height H
.
Block timestamps must be monotonically increasing.
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.
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.
Validator clocks will not be perfectly in sync.
Therefore, the proposer’s current known Unix time may be less than the LastCommit.Timestamp
.
If the proposer’s current known Unix time is less than the LastCommit.Timestamp
, the proposer will sleep until its known Unix time exceeds LastCommit.Timestamp
.
This change will require amending the defaultDecideProposal method.
This method should now block until the proposer’s time is greater than LastCommit.Timestamp
.
Currently, a validator waiting for a proposal will proceed past the propose step if the configured propose timeout is reached and no proposal is seen. Proposer-based timestamps requires changing this timeout logic.
The proposer will now wait until its current known Unix time exceeds the LastCommit.Timestamp
to propose a block.
The validators must now take this and some other factors into account when deciding when to timeout the propose step.
Specifically, the propose step timeout must also take into account potential inaccuracy in the validator’s clock and in the clock of the proposer.
Additionally, there may be a delay communicating the proposal message from the proposer to the other validators.
Therefore, validators waiting for a proposal must wait until after the LastCommit.Timestamp
before timing out.
To account for possible inaccuracy in its own clock, inaccuracy in the proposer’s clock, and message delay, validators waiting for a proposal will wait until LastCommit.Timesatmp + 2*ACCURACY + MSGDELAY
.
The spec defines this as waitingTime
.
The propose step’s timeout is set in enterPropose in state.go
.
enterPropose
will be changed to calculate waiting time using the new consensus parameters.
The timeout in enterPropose
will then be set as the maximum of waitingTime
and the configured proposal step timeout.
The rules for validating that a proposal is valid will need slight modification to implement proposer-based timestamps.
Specifically, we will change the validation logic to ensure that the proposal timestamp is timely
and we will modify the way the block timestamp is validated as well.
Adding proposal timestamp validation is a reasonably straightforward change.
The current Unix time known to the proposer is already included in the Proposal message.
Once the proposal is received, the complete message is stored in the RoundState.Proposal
field.
The precommit and prevote validation logic does not currently use this timestamp.
This validation logic will be updated to check that the proposal timestamp is within PRECISION
of the current Unix time known to the validators.
If the timestamp is not within PRECISION
of the current Unix time known to the validator, the proposal will not be considered it valid.
The validator will also check that the proposal time is greater than the block timestamp from the previous height.
If no valid proposal is received by the proposal timeout, the validator will prevote nil. This is identical to the current logic.
The validBlock function currently validates the proposed block timestamp in three ways.
First, the validation logic checks that this timestamp is greater than the previous block’s timestamp.
Additionally, it validates that the block timestamp is correctly calculated as the weighted median of the timestamps in the block’s LastCommit.
Finally, the logic also authenticates the timestamps in the LastCommit
.
The cryptographic signature in each CommitSig
is created by signing a hash of fields in the block with the validator’s private key.
One of the items in this signedBytes
hash is derived from the timestamp in the CommitSig
.
To authenticate the CommitSig
timestamp, the validator builds a hash of fields that includes the timestamp and checks this hash against the provided signature.
This takes place in the VerifyCommit function.
The logic to validate that the block timestamp is greater than the previous block’s timestamp also works for proposer-based timestamps and will not change.
BFTTime
validation is no longer applicable and will be removed.
Validators will no longer check that the block timestamp is a weighted median of LastCommit
timestamps.
This will mean removing the call to MedianTime in the validateBlock function.
The MedianTime
function can be completely removed.
The LastCommit
timestamps may also be removed.
The signedBytes
validation logic in VerifyCommit
will be slightly altered.
The CommitSig
s in the block’s LastCommit
will no longer each contain a timestamp.
The validation logic will instead include the LastCommit.Timestamp
in the hash of fields for generating the signedBytes
.
The cryptographic signatures included in the CommitSig
s will then be checked against this signedBytes
hash to authenticate the timestamp.
Specifically, the VerifyCommit
function will be updated to use this new timestamp.
Currently, a validator will prevote a proposal in one of three cases:
The only change we will make to the prevote step is to what a validator considers a valid proposal as detailed above.
The precommit step will not require much modification. Its proposal validation rules will change in the same ways that validation will change in the prevote step.
When a validator receives a valid proposed block and +2/3 prevotes for that block, it stores the block as its ‘locked block’ in the RoundState.ValidBlock field. In each subsequent round it will prevote that block. A validator will only change which block it has locked if it sees +2/3 prevotes for a different block.
This mechanism will remain largely unchanged.
The only difference is the addition of proposal timestamp validation.
A validator will prevote nil in a round if the proposal message it received is not timely
.
Prevoting nil in this case will not cause a validator to ‘unlock’ its locked block.
This difference is an incidental result of the changes to prevote validation.
It is included in this design for completeness and to clarify that no additional changes will be made to block locking.
voteTime is a mechanism for calculating the next BFTTime
given both the validator's current known Unix time and the previous block timestamp.
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.
This logic is used in multiple places and is no longer needed for proposer-based timestamps.
It should therefore be removed completely.
Precommit
messages, we are able to aggregate signatures.<2/3
of validators can no longer influence block timestamps.BFTTime
.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.
This skew will be bound by the PRECISION
value, so it is unlikely to be too large.
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.