Blockchain

Here we describe the data structures in the Tendermint blockchain and the rules for validating them.

Data Structures

The Tendermint blockchains consists of a short list of basic data types:

  • Block
  • Header
  • Version
  • BlockID
  • Time
  • Data (for transactions)
  • Commit and Vote
  • EvidenceData and Evidence

Block

A block consists of a header, transactions, votes (the commit), and a list of evidence of malfeasance (ie. signing conflicting votes).

  1. type Block struct {
  2. Header Header
  3. Txs Data
  4. Evidence EvidenceData
  5. LastCommit Commit
  6. }

Note the LastCommit is the set of votes that committed the last block.

Header

A block header contains metadata about the block and about the consensus, as well as commitments to the data in the current block, the previous block, and the results returned by the application:

  1. type Header struct {
  2. // basic block info
  3. Version Version
  4. ChainID string
  5. Height int64
  6. Time Time
  7. NumTxs int64
  8. TotalTxs int64
  9. // prev block info
  10. LastBlockID BlockID
  11. // hashes of block data
  12. LastCommitHash []byte // commit from validators from the last block
  13. DataHash []byte // MerkleRoot of transaction hashes
  14. // hashes from the app output from the prev block
  15. ValidatorsHash []byte // validators for the current block
  16. NextValidatorsHash []byte // validators for the next block
  17. ConsensusHash []byte // consensus params for current block
  18. AppHash []byte // state after txs from the previous block
  19. LastResultsHash []byte // root hash of all results from the txs from the previous block
  20. // consensus info
  21. EvidenceHash []byte // evidence included in the block
  22. ProposerAddress []byte // original proposer of the block

Further details on each of these fields is described below.

Version

The Version contains the protocol version for the blockchain and the application as two uint64 values:

  1. type Version struct {
  2. Block uint64
  3. App uint64
  4. }

BlockID

The BlockID contains two distinct Merkle roots of the block. The first, used as the block’s main hash, is the MerkleRoot of all the fields in the header (ie. MerkleRoot(header). The second, used for secure gossipping of the block during consensus, is the MerkleRoot of the complete serialized block cut into parts (ie. MerkleRoot(MakeParts(block))). The BlockID includes these two hashes, as well as the number of parts (ie. len(MakeParts(block)))

  1. type BlockID struct {
  2. Hash []byte
  3. PartsHeader PartSetHeader
  4. }
  5. type PartSetHeader struct {
  6. Total int32
  7. Hash []byte
  8. }

See MerkleRoot for details.

Time

Tendermint uses the Google.Protobuf.WellKnownTypes.Timestamp format, which uses two integers, one for Seconds and for Nanoseconds.

Data

Data is just a wrapper for a list of transactions, where transactions are arbitrary byte arrays:

  1. type Data struct {
  2. Txs [][]byte
  3. }

Commit

Commit is a simple wrapper for a list of votes, with one vote for each validator. It also contains the relevant BlockID:

  1. type Commit struct {
  2. BlockID BlockID
  3. Precommits []Vote
  4. }

NOTE: this will likely change to reduce the commit size by eliminating redundant information - see issue #1648.

Vote

A vote is a signed message from a validator for a particular block. The vote includes information about the validator signing it.

  1. type Vote struct {
  2. Type byte
  3. Height int64
  4. Round int
  5. BlockID BlockID
  6. Timestamp Time
  7. ValidatorAddress []byte
  8. ValidatorIndex int
  9. Signature []byte
  10. }

There are two types of votes: a prevote has vote.Type == 1 and a precommit has vote.Type == 2.

Signature

Signatures in Tendermint are raw bytes representing the underlying signature.

See the signature spec for more.

EvidenceData

EvidenceData is a simple wrapper for a list of evidence:

  1. type EvidenceData struct {
  2. Evidence []Evidence
  3. }

Evidence

Evidence in Tendermint is implemented as an interface. This means any evidence is encoded using its Amino prefix. There is currently only a single type, the DuplicateVoteEvidence.

  1. // amino name: "tendermint/DuplicateVoteEvidence"
  2. type DuplicateVoteEvidence struct {
  3. PubKey PubKey
  4. VoteA Vote
  5. VoteB Vote
  6. }

See the pubkey spec for more.

Validation

Here we describe the validation rules for every element in a block. Blocks which do not satisfy these rules are considered invalid.

We abuse notation by using something that looks like Go, supplemented with English. A statement such as x == y is an assertion - if it fails, the item is invalid.

We refer to certain globally available objects: block is the block under consideration, prevBlock is the block at the previous height, and state keeps track of the validator set, the consensus parameters and other results from the application. At the point when block is the block under consideration, the current version of the state corresponds to the state after executing transactions from the prevBlock. Elements of an object are accessed as expected, ie. block.Header. See the definition of State.

Header

A Header is valid if its corresponding fields are valid.

Version

  1. block.Version.Block == state.Version.Block
  2. block.Version.App == state.Version.App

The block version must match the state version.

ChainID

  1. len(block.ChainID) < 50

ChainID must be less than 50 bytes.

Height

  1. block.Header.Height > 0
  2. block.Header.Height == prevBlock.Header.Height + 1

The height is an incrementing integer. The first block has block.Header.Height == 1.

Time

  1. block.Header.Timestamp >= prevBlock.Header.Timestamp + state.consensusParams.Block.TimeIotaMs
  2. block.Header.Timestamp == MedianTime(block.LastCommit, state.LastValidators)

The block timestamp must be monotonic. It must equal the weighted median of the timestamps of the valid votes in the block.LastCommit.

Note: the timestamp of a vote must be greater by at least one millisecond than that of the block being voted on.

The timestamp of the first block must be equal to the genesis time (since there’s no votes to compute the median).

  1. if block.Header.Height == 1 {
  2. block.Header.Timestamp == genesisTime
  3. }

See the section on BFT time for more details.

NumTxs

  1. block.Header.NumTxs == len(block.Txs.Txs)

Number of transactions included in the block.

TotalTxs

  1. block.Header.TotalTxs == prevBlock.Header.TotalTxs + block.Header.NumTxs

The cumulative sum of all transactions included in this blockchain.

The first block has block.Header.TotalTxs = block.Header.NumberTxs.

LastBlockID

LastBlockID is the previous block’s BlockID:

  1. prevBlockParts := MakeParts(prevBlock)
  2. block.Header.LastBlockID == BlockID {
  3. Hash: MerkleRoot(prevBlock.Header),
  4. PartsHeader{
  5. Hash: MerkleRoot(prevBlockParts),
  6. Total: len(prevBlockParts),
  7. },
  8. }

The first block has block.Header.LastBlockID == BlockID{}.

LastCommitHash

  1. block.Header.LastCommitHash == MerkleRoot(block.LastCommit.Precommits)

MerkleRoot of the votes included in the block. These are the votes that committed the previous block.

The first block has block.Header.LastCommitHash == []byte{}

DataHash

  1. block.Header.DataHash == MerkleRoot(Hashes(block.Txs.Txs))

MerkleRoot of the hashes of transactions included in the block.

Note the transactions are hashed before being included in the Merkle tree, so the leaves of the Merkle tree are the hashes, not the transactions themselves. This is because transaction hashes are regularly used as identifiers for transactions.

ValidatorsHash

  1. block.ValidatorsHash == MerkleRoot(state.Validators)

MerkleRoot of the current validator set that is committing the block. This can be used to validate the LastCommit included in the next block. Note the validators are sorted by their address before computing the MerkleRoot.

NextValidatorsHash

  1. block.NextValidatorsHash == MerkleRoot(state.NextValidators)

MerkleRoot of the next validator set that will be the validator set that commits the next block. This is included so that the current validator set gets a chance to sign the next validator sets Merkle root. Note the validators are sorted by their address before computing the MerkleRoot.

ConsensusHash

  1. block.ConsensusHash == state.ConsensusParams.Hash()

Hash of the amino-encoding of a subset of the consensus parameters.

AppHash

  1. block.AppHash == state.AppHash

Arbitrary byte array returned by the application after executing and commiting the previous block. It serves as the basis for validating any merkle proofs that comes from the ABCI application and represents the state of the actual application rather than the state of the blockchain itself.

The first block has block.Header.AppHash == []byte{}.

LastResultsHash

  1. block.ResultsHash == MerkleRoot(state.LastResults)

MerkleRoot of the results of the transactions in the previous block.

The first block has block.Header.ResultsHash == []byte{}.

EvidenceHash

  1. block.EvidenceHash == MerkleRoot(block.Evidence)

MerkleRoot of the evidence of Byzantine behaviour included in this block.

ProposerAddress

  1. block.Header.ProposerAddress in state.Validators

Address of the original proposer of the block. Must be a current validator.

Txs

Arbitrary length array of arbitrary length byte-arrays.

LastCommit

The first height is an exception - it requires the LastCommit to be empty:

  1. if block.Header.Height == 1 {
  2. len(b.LastCommit) == 0
  3. }

Otherwise, we require:

  1. len(block.LastCommit) == len(state.LastValidators)
  2. talliedVotingPower := 0
  3. for i, vote := range block.LastCommit{
  4. if vote == nil{
  5. continue
  6. }
  7. vote.Type == 2
  8. vote.Height == block.LastCommit.Height()
  9. vote.Round == block.LastCommit.Round()
  10. vote.BlockID == block.LastBlockID
  11. val := state.LastValidators[i]
  12. vote.Verify(block.ChainID, val.PubKey) == true
  13. talliedVotingPower += val.VotingPower
  14. }
  15. talliedVotingPower > (2/3) * TotalVotingPower(state.LastValidators)

Includes one (possibly nil) vote for every current validator. Non-nil votes must be Precommits. All votes must be for the same height and round. All votes must be for the previous block. All votes must have a valid signature from the corresponding validator. The sum total of the voting power of the validators that voted must be greater than 2/3 of the total voting power of the complete validator set.

Vote

A vote is a signed message broadcast in the consensus for a particular block at a particular height and round. When stored in the blockchain or propagated over the network, votes are encoded in Amino. For signing, votes are represented via CanonicalVote and also encoded using amino (protobuf compatible) via Vote.SignBytes which includes the ChainID, and uses a different ordering of the fields.

We define a method Verify that returns true if the signature verifies against the pubkey for the SignBytes using the given ChainID:

  1. func (vote *Vote) Verify(chainID string, pubKey crypto.PubKey) error {
  2. if !bytes.Equal(pubKey.Address(), vote.ValidatorAddress) {
  3. return ErrVoteInvalidValidatorAddress
  4. }
  5. if !pubKey.VerifyBytes(vote.SignBytes(chainID), vote.Signature) {
  6. return ErrVoteInvalidSignature
  7. }
  8. return nil
  9. }

where pubKey.Verify performs the appropriate digital signature verification of the pubKey against the given signature and message bytes.

Evidence

There is currently only one kind of evidence, DuplicateVoteEvidence.

DuplicateVoteEvidence ev is valid if

  • ev.VoteA and ev.VoteB can be verified with ev.PubKey
  • ev.VoteA and ev.VoteB have the same Height, Round, Address, Index, Type
  • ev.VoteA.BlockID != ev.VoteB.BlockID
  • (block.Height - ev.VoteA.Height) < MAX_EVIDENCE_AGE

Execution

Once a block is validated, it can be executed against the state.

The state follows this recursive equation:

  1. state(1) = InitialState
  2. state(h+1) <- Execute(state(h), ABCIApp, block(h))

where InitialState includes the initial consensus parameters and validator set, and ABCIApp is an ABCI application that can return results and changes to the validator set (TODO). Execute is defined as:

  1. Execute(s State, app ABCIApp, block Block) State {
  2. // Fuction ApplyBlock executes block of transactions against the app and returns the new root hash of the app state,
  3. // modifications to the validator set and the changes of the consensus parameters.
  4. AppHash, ValidatorChanges, ConsensusParamChanges := app.ApplyBlock(block)
  5. return State{
  6. LastResults: abciResponses.DeliverTxResults,
  7. AppHash: AppHash,
  8. LastValidators: state.Validators,
  9. Validators: state.NextValidators,
  10. NextValidators: UpdateValidators(state.NextValidators, ValidatorChanges),
  11. ConsensusParams: UpdateConsensusParams(state.ConsensusParams, ConsensusParamChanges),
  12. }
  13. }