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.

284 lines
11 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. func (evpool *Pool) verify(evidence types.Evidence) (*info, error) {
  17. var (
  18. state = evpool.State()
  19. height = state.LastBlockHeight
  20. evidenceParams = state.ConsensusParams.Evidence
  21. ageNumBlocks = height - evidence.Height()
  22. )
  23. // verify the time of the evidence
  24. blockMeta := evpool.blockStore.LoadBlockMeta(evidence.Height())
  25. if blockMeta == nil {
  26. return nil, fmt.Errorf("don't have header at height #%d", evidence.Height())
  27. }
  28. evTime := blockMeta.Header.Time
  29. ageDuration := state.LastBlockTime.Sub(evTime)
  30. // check that the evidence hasn't expired
  31. if ageDuration > evidenceParams.MaxAgeDuration && ageNumBlocks > evidenceParams.MaxAgeNumBlocks {
  32. return nil, fmt.Errorf(
  33. "evidence from height %d (created at: %v) is too old; min height is %d and evidence can not be older than %v",
  34. evidence.Height(),
  35. evTime,
  36. height-evidenceParams.MaxAgeNumBlocks,
  37. state.LastBlockTime.Add(evidenceParams.MaxAgeDuration),
  38. )
  39. }
  40. // apply the evidence-specific verification logic
  41. switch ev := evidence.(type) {
  42. case *types.DuplicateVoteEvidence:
  43. valSet, err := evpool.stateDB.LoadValidators(evidence.Height())
  44. if err != nil {
  45. return nil, err
  46. }
  47. err = VerifyDuplicateVote(ev, state.ChainID, valSet)
  48. if err != nil {
  49. return nil, fmt.Errorf("verifying duplicate vote evidence: %w", err)
  50. }
  51. _, val := valSet.GetByAddress(ev.VoteA.ValidatorAddress)
  52. return &info{
  53. Evidence: evidence,
  54. Time: evTime,
  55. Validators: []*types.Validator{val}, // just a single validator for duplicate vote evidence
  56. TotalVotingPower: valSet.TotalVotingPower(),
  57. }, nil
  58. case *types.LightClientAttackEvidence:
  59. commonHeader, err := getSignedHeader(evpool.blockStore, evidence.Height())
  60. if err != nil {
  61. return nil, err
  62. }
  63. commonVals, err := evpool.stateDB.LoadValidators(evidence.Height())
  64. if err != nil {
  65. return nil, err
  66. }
  67. trustedHeader := commonHeader
  68. // in the case of lunatic the trusted header is different to the common header
  69. if evidence.Height() != ev.ConflictingBlock.Height {
  70. trustedHeader, err = getSignedHeader(evpool.blockStore, ev.ConflictingBlock.Height)
  71. if err != nil {
  72. return nil, err
  73. }
  74. }
  75. err = VerifyLightClientAttack(ev, commonHeader, trustedHeader, commonVals, state.LastBlockTime,
  76. state.ConsensusParams.Evidence.MaxAgeDuration)
  77. if err != nil {
  78. return nil, err
  79. }
  80. // find out what type of attack this was and thus extract the malicious validators. Note in the case of an
  81. // Amnesia attack we don't have any malicious validators.
  82. validators, attackType := getMaliciousValidators(ev, commonVals, trustedHeader)
  83. totalVotingPower := ev.ConflictingBlock.ValidatorSet.TotalVotingPower()
  84. if attackType == lunaticType {
  85. totalVotingPower = commonVals.TotalVotingPower()
  86. }
  87. return &info{
  88. Evidence: evidence,
  89. Time: evTime,
  90. Validators: validators,
  91. TotalVotingPower: totalVotingPower,
  92. }, nil
  93. default:
  94. return nil, fmt.Errorf("unrecognized evidence type: %T", evidence)
  95. }
  96. }
  97. // VerifyLightClientAttack verifies LightClientAttackEvidence against the state of the full node. This involves
  98. // the following checks:
  99. // - the common header from the full node has at least 1/3 voting power which is also present in
  100. // the conflicting header's commit
  101. // - the nodes trusted header at the same height as the conflicting header has a different hash
  102. func VerifyLightClientAttack(e *types.LightClientAttackEvidence, commonHeader, trustedHeader *types.SignedHeader,
  103. commonVals *types.ValidatorSet, now time.Time, trustPeriod time.Duration) error {
  104. // In the case of lunatic attack we need to perform a single verification jump between the
  105. // common header and the conflicting one
  106. if commonHeader.Height != trustedHeader.Height {
  107. err := light.Verify(commonHeader, commonVals, e.ConflictingBlock.SignedHeader, e.ConflictingBlock.ValidatorSet,
  108. trustPeriod, now, 0*time.Second, light.DefaultTrustLevel)
  109. if err != nil {
  110. return fmt.Errorf("skipping verification from common to conflicting header failed: %w", err)
  111. }
  112. } else {
  113. // in the case of equivocation and amnesia we expect some header hashes to be correctly derived
  114. if isInvalidHeader(trustedHeader.Header, e.ConflictingBlock.Header) {
  115. return errors.New("common height is the same as conflicting block height so expected the conflicting" +
  116. " block to be correctly derived yet it wasn't")
  117. }
  118. // ensure that 2/3 of the validator set did vote for this block
  119. if err := e.ConflictingBlock.ValidatorSet.VerifyCommitLight(trustedHeader.ChainID, e.ConflictingBlock.Commit.BlockID,
  120. e.ConflictingBlock.Height, e.ConflictingBlock.Commit); err != nil {
  121. return fmt.Errorf("invalid commit from conflicting block: %w", err)
  122. }
  123. }
  124. if bytes.Equal(trustedHeader.Hash(), e.ConflictingBlock.Hash()) {
  125. return fmt.Errorf("trusted header hash matches the evidence conflicting header hash: %X",
  126. trustedHeader.Hash())
  127. }
  128. return nil
  129. }
  130. // VerifyDuplicateVote verifies DuplicateVoteEvidence against the state of full node. This involves the
  131. // following checks:
  132. // - the validator is in the validator set at the height of the evidence
  133. // - the height, round, type and validator address of the votes must be the same
  134. // - the block ID's must be different
  135. // - The signatures must both be valid
  136. func VerifyDuplicateVote(e *types.DuplicateVoteEvidence, chainID string, valSet *types.ValidatorSet) error {
  137. _, val := valSet.GetByAddress(e.VoteA.ValidatorAddress)
  138. if val == nil {
  139. return fmt.Errorf("address %X was not a validator at height %d", e.VoteA.ValidatorAddress, e.Height())
  140. }
  141. pubKey := val.PubKey
  142. // H/R/S must be the same
  143. if e.VoteA.Height != e.VoteB.Height ||
  144. e.VoteA.Round != e.VoteB.Round ||
  145. e.VoteA.Type != e.VoteB.Type {
  146. return fmt.Errorf("h/r/s does not match: %d/%d/%v vs %d/%d/%v",
  147. e.VoteA.Height, e.VoteA.Round, e.VoteA.Type,
  148. e.VoteB.Height, e.VoteB.Round, e.VoteB.Type)
  149. }
  150. // Address must be the same
  151. if !bytes.Equal(e.VoteA.ValidatorAddress, e.VoteB.ValidatorAddress) {
  152. return fmt.Errorf("validator addresses do not match: %X vs %X",
  153. e.VoteA.ValidatorAddress,
  154. e.VoteB.ValidatorAddress,
  155. )
  156. }
  157. // BlockIDs must be different
  158. if e.VoteA.BlockID.Equals(e.VoteB.BlockID) {
  159. return fmt.Errorf(
  160. "block IDs are the same (%v) - not a real duplicate vote",
  161. e.VoteA.BlockID,
  162. )
  163. }
  164. // pubkey must match address (this should already be true, sanity check)
  165. addr := e.VoteA.ValidatorAddress
  166. if !bytes.Equal(pubKey.Address(), addr) {
  167. return fmt.Errorf("address (%X) doesn't match pubkey (%v - %X)",
  168. addr, pubKey, pubKey.Address())
  169. }
  170. va := e.VoteA.ToProto()
  171. vb := e.VoteB.ToProto()
  172. // Signatures must be valid
  173. if !pubKey.VerifySignature(types.VoteSignBytes(chainID, va), e.VoteA.Signature) {
  174. return fmt.Errorf("verifying VoteA: %w", types.ErrVoteInvalidSignature)
  175. }
  176. if !pubKey.VerifySignature(types.VoteSignBytes(chainID, vb), e.VoteB.Signature) {
  177. return fmt.Errorf("verifying VoteB: %w", types.ErrVoteInvalidSignature)
  178. }
  179. return nil
  180. }
  181. func getSignedHeader(blockStore BlockStore, height int64) (*types.SignedHeader, error) {
  182. blockMeta := blockStore.LoadBlockMeta(height)
  183. if blockMeta == nil {
  184. return nil, fmt.Errorf("don't have header at height #%d", height)
  185. }
  186. commit := blockStore.LoadBlockCommit(height)
  187. if commit == nil {
  188. return nil, fmt.Errorf("don't have commit at height #%d", height)
  189. }
  190. return &types.SignedHeader{
  191. Header: &blockMeta.Header,
  192. Commit: commit,
  193. }, nil
  194. }
  195. // getMaliciousValidators finds out what style of attack LightClientAttackEvidence was and then works out who
  196. // the malicious validators were and returns them.
  197. func getMaliciousValidators(evidence *types.LightClientAttackEvidence, commonVals *types.ValidatorSet,
  198. trusted *types.SignedHeader) ([]*types.Validator, lightClientAttackType) {
  199. var validators []*types.Validator
  200. // First check if the header is invalid. This means that it is a lunatic attack and therefore we take the
  201. // validators who are in the commonVals and voted for the lunatic header
  202. if isInvalidHeader(trusted.Header, evidence.ConflictingBlock.Header) {
  203. for _, commitSig := range evidence.ConflictingBlock.Commit.Signatures {
  204. if !commitSig.ForBlock() {
  205. continue
  206. }
  207. _, val := commonVals.GetByAddress(commitSig.ValidatorAddress)
  208. if val == nil {
  209. // validator wasn't in the common validator set
  210. continue
  211. }
  212. validators = append(validators, val)
  213. }
  214. return validators, lunaticType
  215. // Next, check to see if it is an equivocation attack and both commits are in the same round. If this is the
  216. // case then we take the validators from the conflicting light block validator set that voted in both headers.
  217. } else if trusted.Commit.Round == evidence.ConflictingBlock.Commit.Round {
  218. // validator hashes are the same therefore the indexing order of validators are the same and thus we
  219. // only need a single loop to find the validators that voted twice.
  220. for i := 0; i < len(evidence.ConflictingBlock.Commit.Signatures); i++ {
  221. sigA := evidence.ConflictingBlock.Commit.Signatures[i]
  222. if sigA.Absent() {
  223. continue
  224. }
  225. sigB := trusted.Commit.Signatures[i]
  226. if sigB.Absent() {
  227. continue
  228. }
  229. _, val := evidence.ConflictingBlock.ValidatorSet.GetByAddress(sigA.ValidatorAddress)
  230. validators = append(validators, val)
  231. }
  232. return validators, equivocationType
  233. }
  234. // if the rounds are different then this is an amnesia attack. Unfortunately, given the nature of the attack,
  235. // we aren't able yet to deduce which are malicious validators and which are not hence we return an
  236. // empty validator set.
  237. return validators, amnesiaType
  238. }
  239. // isInvalidHeader takes a trusted header and matches it againt a conflicting header
  240. // to determine whether the conflicting header was the product of a valid state transition
  241. // or not. If it is then all the deterministic fields of the header should be the same.
  242. // If not, it is an invalid header and constitutes a lunatic attack.
  243. func isInvalidHeader(trusted, conflicting *types.Header) bool {
  244. return !bytes.Equal(trusted.ValidatorsHash, conflicting.ValidatorsHash) ||
  245. !bytes.Equal(trusted.NextValidatorsHash, conflicting.NextValidatorsHash) ||
  246. !bytes.Equal(trusted.ConsensusHash, conflicting.ConsensusHash) ||
  247. !bytes.Equal(trusted.AppHash, conflicting.AppHash) ||
  248. !bytes.Equal(trusted.LastResultsHash, conflicting.LastResultsHash)
  249. }
  250. type lightClientAttackType int
  251. const (
  252. lunaticType lightClientAttackType = iota + 1
  253. equivocationType
  254. amnesiaType
  255. )