github.com/number571/tendermint@v0.34.11-gost/docs/architecture/adr-059-evidence-composition-and-lifecycle.md (about)

     1  # ADR 059: Evidence Composition and Lifecycle
     2  
     3  ## Changelog
     4  
     5  - 04/09/2020: Initial Draft (Unabridged)
     6  - 07/09/2020: First Version
     7  - 13/03/2021: Ammendment to accomodate forward lunatic attack
     8  - 29/06/2021: Add information about ABCI specific fields
     9  
    10  ## Scope
    11  
    12  This document is designed to collate together and surface some predicaments involving evidence in Tendermint: both its composition and lifecycle. It then aims to find a solution to these. The scope does not extend to the verification nor detection of certain types of evidence but concerns itself mainly with the general form of evidence and how it moves from inception to application.
    13  
    14  ## Background
    15  
    16  For a long time `DuplicateVoteEvidence`, formed in the consensus reactor, was the only evidence Tendermint had. It was produced whenever two votes from the same validator in the same round
    17  was observed and thus it was designed that each evidence was for a single validator. It was predicted that there may come more forms of evidence and thus `DuplicateVoteEvidence` was used as the model for the `Evidence` interface and also for the form of the evidence data sent to the application. It is important to note that Tendermint concerns itself just with the detection and reporting of evidence and it is the responsibility of the application to exercise punishment.
    18  
    19  ```go
    20  type Evidence interface { //existing
    21    Height() int64                                     // height of the offense
    22    Time() time.Time                                   // time of the offense
    23    Address() []byte                                   // address of the offending validator
    24    Bytes() []byte                                     // bytes which comprise the evidence
    25    Hash() []byte                                      // hash of the evidence
    26    Verify(chainID string, pubKey crypto.PubKey) error // verify the evidence
    27    Equal(Evidence) bool                               // check equality of evidence
    28  
    29    ValidateBasic() error
    30    String() string
    31  }
    32  ```
    33  
    34  ```go
    35  type DuplicateVoteEvidence struct {
    36    VoteA *Vote
    37    VoteB *Vote
    38  
    39    timestamp time.Time // taken from the block time
    40  }
    41  ```
    42  
    43  Tendermint has now introduced a new type of evidence to protect light clients from being attacked. This `LightClientAttackEvidence` (see [here](https://github.com/informalsystems/tendermint-rs/blob/31ca3e64ce90786c1734caf186e30595832297a4/docs/spec/lightclient/attacks/evidence-handling.md) for more information) is vastly different to `DuplicateVoteEvidence` in that it is physically a much different size containing a complete signed header and validator set. It is formed within the light client, not the consensus reactor and requires a lot more information from state to verify (`VerifyLightClientAttack(commonHeader, trustedHeader *SignedHeader, commonVals *ValidatorSet)`  vs `VerifyDuplicateVote(chainID string, pubKey PubKey)`). Finally it batches validators together (a single piece of evidence that implicates multiple malicious validators at a height) as opposed to having individual evidence (each piece of evidence is per validator per height). This evidence stretches the existing mould that was used to accommodate new types of evidence and has thus caused us to reconsider how evidence should be formatted and processed.
    44  
    45  ```go
    46  type LightClientAttackEvidence struct { // proposed struct in spec
    47    ConflictingBlock *LightBlock
    48    CommonHeight int64
    49    Type  AttackType     // enum: {Lunatic|Equivocation|Amnesia}
    50  
    51    timestamp time.Time // taken from the block time at the common height
    52  }
    53  ```
    54  *Note: These three attack types have been proven by the research team to be exhaustive*
    55  
    56  ## Possible Approaches for Evidence Composition
    57  
    58  ### Individual framework
    59  
    60  Evidence remains on a per validator basis. This causes the least disruption to the current processes but requires that we break `LightClientAttackEvidence` into several pieces of evidence for each malicious validator. This not only has performance consequences in that there are n times as many database operations and that the gossiping of evidence will require more bandwidth then necessary (by requiring a header for each piece) but it potentially impacts our ability to validate it. In batch form, the full node can run the same process the light client did to see that 1/3 validating power was present in both the common block and the conflicting block whereas this becomes more difficult to verify individually without opening the possibility that malicious validators forge evidence against innocent . Not only that, but `LightClientAttackEvidence` also deals with amnesia attacks which unfortunately have the characteristic where we know the set of validators involved but not the subset that were actually malicious (more to be said about this later). And finally splitting the evidence into individual pieces makes it difficult to understand the severity of the attack (i.e. the total voting power involved in the attack)
    61  
    62  #### An example of a possible implementation path
    63  
    64  We would ignore amnesia evidence (as individually it's hard to make) and revert to the initial split we had before where `DuplicateVoteEvidence` is also used for light client equivocation attacks and thus we only need `LunaticEvidence`. We would also most likely need to remove `Verify` from the interface as this isn't really something that can be used.
    65  
    66  ``` go
    67  type LunaticEvidence struct { // individual lunatic attack
    68    header *Header
    69    commonHeight int64
    70    vote *Vote
    71  
    72    timestamp time.Time // once again taken from the block time at the height of the common header
    73  }
    74  ```
    75  
    76  ### Batch Framework
    77  
    78  The last approach of this category would be to consider batch only evidence. This works fine with `LightClientAttackEvidence` but would require alterations to `DuplicateVoteEvidence` which would most likely mean that the consensus would send conflicting votes to a buffer in the evidence module which would then wrap all the votes together per height before gossiping them to other nodes and trying to commit it on chain. At a glance this may improve IO and verification speed and perhaps more importantly grouping validators gives the application and Tendermint a better overview of the severity of the attack.
    79  
    80  However individual evidence has the advantage that it is easy to check if a node already has that evidence meaning we just need to check hashes to know that we've already verified this evidence before. Batching evidence would imply that each node may have a different combination of duplicate votes which may complicate things.
    81  
    82  #### An example of a possible implementation path
    83  
    84  `LightClientAttackEvidence` won't change but the evidence interface will need to look like the proposed one above and `DuplicateVoteEvidence` will need to change to encompass multiple double votes. A problem with batch evidence is that it needs to be unique to avoid people from submitting different permutations.
    85  
    86  ## Decision
    87  
    88  The decision is to adopt a hybrid design.
    89  
    90  We allow individual and batch evidence to coexist together, meaning that verification is done depending on the evidence type and that  the bulk of the work is done in the evidence pool itself (including forming the evidence to be sent to the application).
    91  
    92  
    93  ## Detailed Design
    94  
    95  Evidence has the following simple interface:
    96  
    97  ```go
    98  type Evidence interface {  //proposed
    99    Height() int64                                     // height of the offense
   100    Bytes() []byte                                     // bytes which comprise the evidence
   101    Hash() []byte                                      // hash of the evidence
   102    ValidateBasic() error
   103    String() string
   104  }
   105  ```
   106  
   107  The changing of the interface is backwards compatible as these methods are all present in the previous version of the interface. However, networks will need to upgrade to be able to process the new evidence as verification has changed.
   108  
   109  We have two concrete types of evidence that fulfil this interface
   110  
   111  ```go
   112  type LightClientAttackEvidence struct {
   113    ConflictingBlock *LightBlock
   114    CommonHeight int64 // the last height at which the primary provider and witness provider had the same header
   115  
   116    // abci specific information
   117  	ByzantineValidators []*Validator // validators in the validator set that misbehaved in creating the conflicting block
   118  	TotalVotingPower    int64        // total voting power of the validator set at the common height
   119  	Timestamp           time.Time    // timestamp of the block at the common height
   120  }
   121  ```
   122  where the `Hash()` is the hash of the header and commonHeight.
   123  
   124  Note: It was also discussed whether to include the commit hash which captures the validators that signed the header. However this would open the opportunity for someone to propose multiple permutations of the same evidence (through different commit signatures) hence it was omitted. Consequentially, when it comes to verifying evidence in a block, for `LightClientAttackEvidence` we can't just check the hashes because someone could have the same hash as us but a different commit where less than 1/3 validators voted which would be an invalid version of the evidence. (see `fastCheck` for more details)
   125  
   126  ```go
   127  type DuplicateVoteEvidence {
   128    VoteA *Vote
   129    VoteB *Vote
   130  
   131    // abci specific information
   132  	TotalVotingPower int64
   133  	ValidatorPower   int64
   134  	Timestamp        time.Time
   135  }
   136  ```
   137  where the `Hash()` is the hash of the two votes
   138  
   139  For both of these types of evidence, `Bytes()` represents the proto-encoded byte array format of the evidence and `ValidateBasic` is
   140  an initial consistency check to make sure the evidence has a valid structure.
   141  
   142  ### The Evidence Pool
   143  
   144  `LightClientAttackEvidence` is generated in the light client and `DuplicateVoteEvidence` in consensus. Both are sent to the evidence pool through `AddEvidence(ev Evidence) error`. The evidence pool's primary purpose is to verify evidence. It also gossips evidence to other peers' evidence pool and serves it to consensus so it can be committed on chain and the relevant information can be sent to the application in order to exercise punishment. When evidence is added, the pool first runs `Has(ev Evidence)` to check if it has already received it (by comparing hashes) and then  `Verify(ev Evidence) error`.  Once verified the evidence pool stores it it's pending database. There are two databases: one for pending evidence that is not yet committed and another of the committed evidence (to avoid committing evidence twice)
   145  
   146  #### Verification
   147  
   148  `Verify()` does the following:
   149  
   150  - Use the hash to see if we already have this evidence in our committed database.
   151  
   152  - Use the height to check if the evidence hasn't expired.
   153  
   154  - If it has expired then use the height to find the block header and check if the time has also expired in which case we drop the evidence
   155  
   156  - Then proceed with switch statement for each of the two evidence:
   157  
   158  For `DuplicateVote`:
   159  
   160  - Check that height, round, type and validator address are the same
   161  
   162  - Check that the Block ID is different
   163  
   164  - Check the look up table for addresses to make sure there already isn't evidence against this validator
   165  
   166  - Fetch the validator set and confirm that the address is in the set at the height of the attack
   167  
   168  - Check that the chain ID and signature is valid.
   169  
   170  For `LightClientAttack`
   171  
   172  - Fetch the common signed header and val set from the common height and use skipping verification to verify the conflicting header
   173  
   174  - Fetch the trusted signed header at the same height as the conflicting header and compare with the conflicting header to work out which type of attack it is and in doing so return the malicious validators. NOTE: If the node doesn't have the signed header at the height of the conflicting header, it instead fetches the latest header it has and checks to see if it can prove the evidence based on a violation of header time. This is known as forward lunatic attack.
   175  
   176    - If equivocation, return the validators that signed for the commits of both the trusted and signed header
   177  
   178    - If lunatic, return the validators from the common val set that signed in the conflicting block
   179  
   180    - If amnesia, return no validators (since we can't know which validators are malicious). This also means that we don't currently send amnesia evidence to the application, although we will introduce more robust amnesia evidence handling in future Tendermint Core releases
   181  
   182  - Check that the hashes of the conflicting header and the trusted header are different
   183  
   184  - In the case of a forward lunatic attack, where the trusted header height is less than the conflicting header height, the node checks that the time of the trusted header is later than the time of conflicting header. This proves that the conflicting header breaks monotonically increasing time. If the node doesn't have a trusted header with a later time then it is unable to validate the evidence for now. 
   185  
   186  - Lastly, for each validator, check the look up table to make sure there already isn't evidence against this validator
   187  
   188  After verification we persist the evidence with the key `height/hash` to the pending evidence database in the evidence pool.
   189  
   190  #### ABCI Evidence
   191  
   192  Both evidence structures contain data (such as timestamp) that are necessary to be passed to the application but do not strictly constitute evidence of misbehaviour. As such, these fields are verified last. If any of these fields are invalid to a node i.e. they don't correspond with their state, nodes will reconstruct a new evidence struct from the existing fields and repopulate the abci specific fields with their own state data.
   193  
   194  #### Broadcasting and receiving evidence
   195  
   196  The evidence pool also runs a reactor that broadcasts the newly validated
   197  evidence to all connected peers.
   198  
   199  Receiving evidence from other evidence reactors works in the same manner as receiving evidence from the consensus reactor or a light client.
   200  
   201  
   202  #### Proposing evidence on the block
   203  
   204  When it comes to prevoting and precomitting a proposal that contains evidence, the full node will once again
   205  call upon the evidence pool to verify the evidence using `CheckEvidence(ev []Evidence)`:
   206  
   207  This performs the following actions:
   208  
   209  1. Loops through all the evidence to check that nothing has been duplicated
   210  
   211  2. For each evidence, run `fastCheck(ev evidence)` which works similar to `Has` but instead for `LightClientAttackEvidence` if it has the
   212  same hash it then goes on to check that the validators it has are all signers in the commit of the conflicting header. If it doesn't pass fast check (because it hasn't seen the evidence before) then it will have to verify the evidence.
   213  
   214  3. runs `Verify(ev Evidence)` - Note: this also saves the evidence to the db as mentioned before.
   215  
   216  
   217  #### Updating application and pool
   218  
   219  The final part of the lifecycle is when the block is committed and the `BlockExecutor` then updates state. As part of this process, the `BlockExecutor` gets the evidence pool to create a simplified format for the evidence to be sent to the application. This happens in `ApplyBlock` where the executor calls `Update(Block, State) []abci.Evidence`.
   220  
   221  ```go
   222  abciResponses.BeginBlock.ByzantineValidators = evpool.Update(block, state)
   223  ```
   224  
   225  Here is the format of the evidence that the application will receive. As seen above, this is stored as an array within `BeginBlock`.
   226  The changes to the application are minimal (it is still formed one for each malicious validator) with the exception of using an enum instead of a string for the evidence type.
   227  
   228  ```go
   229  type Evidence struct {
   230    // either LightClientAttackEvidence or DuplicateVoteEvidence as an enum (abci.EvidenceType)
   231  	Type EvidenceType `protobuf:"varint,1,opt,name=type,proto3,enum=tendermint.abci.EvidenceType" json:"type,omitempty"`
   232  	// The offending validator
   233  	Validator Validator `protobuf:"bytes,2,opt,name=validator,proto3" json:"validator"`
   234  	// The height when the offense occurred
   235  	Height int64 `protobuf:"varint,3,opt,name=height,proto3" json:"height,omitempty"`
   236  	// The corresponding time where the offense occurred
   237  	Time time.Time `protobuf:"bytes,4,opt,name=time,proto3,stdtime" json:"time"`
   238  	// Total voting power of the validator set in case the ABCI application does
   239  	// not store historical validators.
   240  	// https://github.com/number571/tendermint/issues/4581
   241  	TotalVotingPower int64 `protobuf:"varint,5,opt,name=total_voting_power,json=totalVotingPower,proto3" json:"total_voting_power,omitempty"`
   242  }
   243  ```
   244  
   245  
   246  This `Update()` function does the following:
   247  
   248  - Increments state which keeps track of both the current time and height used for measuring expiry
   249  
   250  - Marks evidence as committed and saves to db. This prevents validators from proposing committed evidence in the future
   251    Note: the db just saves the height and the hash. There is no need to save the entire committed evidence
   252  
   253  - Forms ABCI evidence as such:  (note for `DuplicateVoteEvidence` the validators array size is 1)
   254    ```go
   255    for _, val := range evInfo.Validators {
   256      abciEv = append(abciEv, &abci.Evidence{
   257        Type: evType,   // either DuplicateVote or LightClientAttack
   258        Validator: val,   // the offending validator (which includes the address, pubkey and power)
   259        Height: evInfo.ev.Height(),    // the height when the offense happened
   260        Time: evInfo.time,      // the time when the offense happened
   261        TotalVotingPower: evInfo.totalVotingPower   // the total voting power of the validator set
   262      })
   263    }
   264    ```
   265  
   266  - Removes expired evidence from both pending and committed databases
   267  
   268  The ABCI evidence is then sent via the `BlockExecutor` to the application.
   269  
   270  #### Summary
   271  
   272  To summarize, we can see the lifecycle of evidence as such:
   273  
   274  ![evidence_lifecycle](../imgs/evidence_lifecycle.png)
   275  
   276  Evidence is first detected and created in the light client and consensus reactor. It is verified and stored as `EvidenceInfo` and gossiped to the evidence pools in other nodes. The consensus reactor later communicates with the evidence pool to either retrieve evidence to be put into a block, or verify the evidence the consensus reactor has retrieved in a block. Lastly when a block is added to the chain, the block executor sends the committed evidence back to the evidence pool so a pointer to the evidence can be stored in the evidence pool and it can update it's height and time. Finally, it turns the committed evidence into ABCI evidence and through the block executor passes the evidence to the application so the application can handle it.
   277  
   278  ## Status
   279  
   280  Implemented
   281  
   282  ## Consequences
   283  
   284  <!-- > This section describes the consequences, after applying the decision. All consequences should be summarized here, not just the "positive" ones. -->
   285  
   286  ### Positive
   287  
   288  - Evidence is better contained to the evidence pool / module
   289  - LightClientAttack is kept together (easier for verification and bandwidth)
   290  - Variations on commit sigs in LightClientAttack doesn't lead to multiple permutations and multiple evidence
   291  - Address to evidence map prevents DOS attacks, where a single validator could DOS the network by flooding it with evidence submissions
   292  
   293  ### Negative
   294  
   295  - Changes the `Evidence` interface and thus is a block breaking change
   296  - Changes the ABCI `Evidence` and is thus a ABCI breaking change
   297  - Unable to query evidence for address / time without evidence pool
   298  
   299  ### Neutral
   300  
   301  
   302  ## References
   303  
   304  <!-- > Are there any relevant PR comments, issues that led up to this, or articles referenced for why we made the given design choice? If so link them here! -->
   305  
   306  - [LightClientAttackEvidence](https://github.com/informalsystems/tendermint-rs/blob/31ca3e64ce90786c1734caf186e30595832297a4/docs/spec/lightclient/attacks/evidence-handling.md)