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.

267 lines
10 KiB

  1. package evidence
  2. import (
  3. "bytes"
  4. "errors"
  5. "fmt"
  6. "time"
  7. "github.com/tendermint/tendermint/light"
  8. "github.com/tendermint/tendermint/types"
  9. )
  10. // verify verifies the evidence fully by checking:
  11. // - It has not already been committed
  12. // - it is sufficiently recent (MaxAge)
  13. // - it is from a key who was a validator at the given height
  14. // - it is internally consistent with state
  15. // - it was properly signed by the alleged equivocator and meets the individual evidence verification requirements
  16. //
  17. // NOTE: Evidence may be provided that we do not have the block or validator
  18. // set for. In these cases, we do not return a ErrInvalidEvidence as not to have
  19. // the sending peer disconnect. All other errors are treated as invalid evidence
  20. // (i.e. ErrInvalidEvidence).
  21. func (evpool *Pool) verify(evidence types.Evidence) error {
  22. var (
  23. state = evpool.State()
  24. height = state.LastBlockHeight
  25. evidenceParams = state.ConsensusParams.Evidence
  26. ageNumBlocks = height - evidence.Height()
  27. )
  28. // ensure we have the block for the evidence height
  29. //
  30. // NOTE: It is currently possible for a peer to send us evidence we're not
  31. // able to process because we're too far behind (e.g. syncing), so we DO NOT
  32. // return an invalid evidence error because we do not want the peer to
  33. // disconnect or signal an error in this particular case.
  34. blockMeta := evpool.blockStore.LoadBlockMeta(evidence.Height())
  35. if blockMeta == nil {
  36. return fmt.Errorf("failed to verify evidence; missing block for height %d", evidence.Height())
  37. }
  38. // verify the time of the evidence
  39. evTime := blockMeta.Header.Time
  40. ageDuration := state.LastBlockTime.Sub(evTime)
  41. // check that the evidence hasn't expired
  42. if ageDuration > evidenceParams.MaxAgeDuration && ageNumBlocks > evidenceParams.MaxAgeNumBlocks {
  43. return types.NewErrInvalidEvidence(
  44. evidence,
  45. fmt.Errorf(
  46. "evidence from height %d (created at: %v) is too old; min height is %d and evidence can not be older than %v",
  47. evidence.Height(),
  48. evTime,
  49. height-evidenceParams.MaxAgeNumBlocks,
  50. state.LastBlockTime.Add(evidenceParams.MaxAgeDuration),
  51. ),
  52. )
  53. }
  54. // apply the evidence-specific verification logic
  55. switch ev := evidence.(type) {
  56. case *types.DuplicateVoteEvidence:
  57. valSet, err := evpool.stateDB.LoadValidators(evidence.Height())
  58. if err != nil {
  59. return err
  60. }
  61. if err := VerifyDuplicateVote(ev, state.ChainID, valSet); err != nil {
  62. return types.NewErrInvalidEvidence(evidence, err)
  63. }
  64. _, val := valSet.GetByAddress(ev.VoteA.ValidatorAddress)
  65. if err := ev.ValidateABCI(val, valSet, evTime); err != nil {
  66. ev.GenerateABCI(val, valSet, evTime)
  67. if addErr := evpool.addPendingEvidence(ev); addErr != nil {
  68. evpool.logger.Error("adding pending duplicate vote evidence failed", "err", addErr)
  69. }
  70. return err
  71. }
  72. return nil
  73. case *types.LightClientAttackEvidence:
  74. commonHeader, err := getSignedHeader(evpool.blockStore, evidence.Height())
  75. if err != nil {
  76. return err
  77. }
  78. commonVals, err := evpool.stateDB.LoadValidators(evidence.Height())
  79. if err != nil {
  80. return err
  81. }
  82. trustedHeader := commonHeader
  83. // in the case of lunatic the trusted header is different to the common header
  84. if evidence.Height() != ev.ConflictingBlock.Height {
  85. trustedHeader, err = getSignedHeader(evpool.blockStore, ev.ConflictingBlock.Height)
  86. if err != nil {
  87. // FIXME: This multi step process is a bit unergonomic. We may want to consider a more efficient process
  88. // that doesn't require as much io and is atomic.
  89. // If the node doesn't have a block at the height of the conflicting block, then this could be
  90. // a forward lunatic attack. Thus the node must get the latest height it has
  91. latestHeight := evpool.blockStore.Height()
  92. trustedHeader, err = getSignedHeader(evpool.blockStore, latestHeight)
  93. if err != nil {
  94. return err
  95. }
  96. if trustedHeader.Time.Before(ev.ConflictingBlock.Time) {
  97. return fmt.Errorf("latest block time (%v) is before conflicting block time (%v)",
  98. trustedHeader.Time, ev.ConflictingBlock.Time,
  99. )
  100. }
  101. }
  102. }
  103. err = VerifyLightClientAttack(
  104. ev,
  105. commonHeader,
  106. trustedHeader,
  107. commonVals,
  108. state.LastBlockTime,
  109. state.ConsensusParams.Evidence.MaxAgeDuration,
  110. )
  111. if err != nil {
  112. return types.NewErrInvalidEvidence(evidence, err)
  113. }
  114. // validate the ABCI component of evidence. If this fails but the rest
  115. // is valid then we regenerate the ABCI component, save the rectified
  116. // evidence and return an error
  117. if err := ev.ValidateABCI(commonVals, trustedHeader, evTime); err != nil {
  118. ev.GenerateABCI(commonVals, trustedHeader, evTime)
  119. if addErr := evpool.addPendingEvidence(ev); addErr != nil {
  120. evpool.logger.Error("adding pending light client attack evidence failed", "err", addErr)
  121. }
  122. return err
  123. }
  124. return nil
  125. default:
  126. return types.NewErrInvalidEvidence(evidence, fmt.Errorf("unrecognized evidence type: %T", evidence))
  127. }
  128. }
  129. // VerifyLightClientAttack verifies LightClientAttackEvidence against the state of the full node. This involves
  130. // the following checks:
  131. // - the common header from the full node has at least 1/3 voting power which is also present in
  132. // the conflicting header's commit
  133. // - 2/3+ of the conflicting validator set correctly signed the conflicting block
  134. // - the nodes trusted header at the same height as the conflicting header has a different hash
  135. //
  136. // CONTRACT: must run ValidateBasic() on the evidence before verifying
  137. // must check that the evidence has not expired (i.e. is outside the maximum age threshold)
  138. func VerifyLightClientAttack(e *types.LightClientAttackEvidence, commonHeader, trustedHeader *types.SignedHeader,
  139. commonVals *types.ValidatorSet, now time.Time, trustPeriod time.Duration) error {
  140. // In the case of lunatic attack there will be a different commonHeader height. Therefore the node perform a single
  141. // verification jump between the common header and the conflicting one
  142. if commonHeader.Height != e.ConflictingBlock.Height {
  143. err := commonVals.VerifyCommitLightTrusting(trustedHeader.ChainID, e.ConflictingBlock.Commit, light.DefaultTrustLevel)
  144. if err != nil {
  145. return fmt.Errorf("skipping verification of conflicting block failed: %w", err)
  146. }
  147. // In the case of equivocation and amnesia we expect all header hashes to be correctly derived
  148. } else if e.ConflictingHeaderIsInvalid(trustedHeader.Header) {
  149. return errors.New("common height is the same as conflicting block height so expected the conflicting" +
  150. " block to be correctly derived yet it wasn't")
  151. }
  152. // Verify that the 2/3+ commits from the conflicting validator set were for the conflicting header
  153. if err := e.ConflictingBlock.ValidatorSet.VerifyCommitLight(trustedHeader.ChainID, e.ConflictingBlock.Commit.BlockID,
  154. e.ConflictingBlock.Height, e.ConflictingBlock.Commit); err != nil {
  155. return fmt.Errorf("invalid commit from conflicting block: %w", err)
  156. }
  157. // check in the case of a forward lunatic attack that monotonically increasing time has been violated
  158. if e.ConflictingBlock.Height > trustedHeader.Height && e.ConflictingBlock.Time.After(trustedHeader.Time) {
  159. return fmt.Errorf("conflicting block doesn't violate monotonically increasing time (%v is after %v)",
  160. e.ConflictingBlock.Time, trustedHeader.Time,
  161. )
  162. // In all other cases check that the hashes of the conflicting header and the trusted header are different
  163. } else if bytes.Equal(trustedHeader.Hash(), e.ConflictingBlock.Hash()) {
  164. return fmt.Errorf("trusted header hash matches the evidence's conflicting header hash: %X",
  165. trustedHeader.Hash())
  166. }
  167. return nil
  168. }
  169. // VerifyDuplicateVote verifies DuplicateVoteEvidence against the state of full node. This involves the
  170. // following checks:
  171. // - the validator is in the validator set at the height of the evidence
  172. // - the height, round, type and validator address of the votes must be the same
  173. // - the block ID's must be different
  174. // - The signatures must both be valid
  175. func VerifyDuplicateVote(e *types.DuplicateVoteEvidence, chainID string, valSet *types.ValidatorSet) error {
  176. _, val := valSet.GetByAddress(e.VoteA.ValidatorAddress)
  177. if val == nil {
  178. return fmt.Errorf("address %X was not a validator at height %d", e.VoteA.ValidatorAddress, e.Height())
  179. }
  180. pubKey := val.PubKey
  181. // H/R/S must be the same
  182. if e.VoteA.Height != e.VoteB.Height ||
  183. e.VoteA.Round != e.VoteB.Round ||
  184. e.VoteA.Type != e.VoteB.Type {
  185. return fmt.Errorf("h/r/s does not match: %d/%d/%v vs %d/%d/%v",
  186. e.VoteA.Height, e.VoteA.Round, e.VoteA.Type,
  187. e.VoteB.Height, e.VoteB.Round, e.VoteB.Type)
  188. }
  189. // Address must be the same
  190. if !bytes.Equal(e.VoteA.ValidatorAddress, e.VoteB.ValidatorAddress) {
  191. return fmt.Errorf("validator addresses do not match: %X vs %X",
  192. e.VoteA.ValidatorAddress,
  193. e.VoteB.ValidatorAddress,
  194. )
  195. }
  196. // BlockIDs must be different
  197. if e.VoteA.BlockID.Equals(e.VoteB.BlockID) {
  198. return fmt.Errorf(
  199. "block IDs are the same (%v) - not a real duplicate vote",
  200. e.VoteA.BlockID,
  201. )
  202. }
  203. // pubkey must match address (this should already be true, sanity check)
  204. addr := e.VoteA.ValidatorAddress
  205. if !bytes.Equal(pubKey.Address(), addr) {
  206. return fmt.Errorf("address (%X) doesn't match pubkey (%v - %X)",
  207. addr, pubKey, pubKey.Address())
  208. }
  209. va := e.VoteA.ToProto()
  210. vb := e.VoteB.ToProto()
  211. // Signatures must be valid
  212. if !pubKey.VerifySignature(types.VoteSignBytes(chainID, va), e.VoteA.Signature) {
  213. return fmt.Errorf("verifying VoteA: %w", types.ErrVoteInvalidSignature)
  214. }
  215. if !pubKey.VerifySignature(types.VoteSignBytes(chainID, vb), e.VoteB.Signature) {
  216. return fmt.Errorf("verifying VoteB: %w", types.ErrVoteInvalidSignature)
  217. }
  218. return nil
  219. }
  220. func getSignedHeader(blockStore BlockStore, height int64) (*types.SignedHeader, error) {
  221. blockMeta := blockStore.LoadBlockMeta(height)
  222. if blockMeta == nil {
  223. return nil, fmt.Errorf("don't have header at height #%d", height)
  224. }
  225. commit := blockStore.LoadBlockCommit(height)
  226. if commit == nil {
  227. return nil, fmt.Errorf("don't have commit at height #%d", height)
  228. }
  229. return &types.SignedHeader{
  230. Header: &blockMeta.Header,
  231. Commit: commit,
  232. }, nil
  233. }