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.

257 lines
8.4 KiB

  1. package light_test
  2. import (
  3. "testing"
  4. "time"
  5. "github.com/stretchr/testify/assert"
  6. "github.com/stretchr/testify/require"
  7. dbm "github.com/tendermint/tm-db"
  8. "github.com/tendermint/tendermint/libs/log"
  9. "github.com/tendermint/tendermint/light"
  10. "github.com/tendermint/tendermint/light/provider"
  11. mockp "github.com/tendermint/tendermint/light/provider/mock"
  12. dbs "github.com/tendermint/tendermint/light/store/db"
  13. "github.com/tendermint/tendermint/types"
  14. )
  15. func TestLightClientAttackEvidence_Lunatic(t *testing.T) {
  16. // primary performs a lunatic attack
  17. var (
  18. latestHeight = int64(10)
  19. valSize = 5
  20. divergenceHeight = int64(6)
  21. primaryHeaders = make(map[int64]*types.SignedHeader, latestHeight)
  22. primaryValidators = make(map[int64]*types.ValidatorSet, latestHeight)
  23. )
  24. witnessHeaders, witnessValidators, chainKeys := genMockNodeWithKeys(chainID, latestHeight, valSize, 2, bTime)
  25. witness := mockp.New(chainID, witnessHeaders, witnessValidators)
  26. forgedKeys := chainKeys[divergenceHeight-1].ChangeKeys(3) // we change 3 out of the 5 validators (still 2/5 remain)
  27. forgedVals := forgedKeys.ToValidators(2, 0)
  28. for height := int64(1); height <= latestHeight; height++ {
  29. if height < divergenceHeight {
  30. primaryHeaders[height] = witnessHeaders[height]
  31. primaryValidators[height] = witnessValidators[height]
  32. continue
  33. }
  34. primaryHeaders[height] = forgedKeys.GenSignedHeader(chainID, height, bTime.Add(time.Duration(height)*time.Minute),
  35. nil, forgedVals, forgedVals, hash("app_hash"), hash("cons_hash"), hash("results_hash"), 0, len(forgedKeys))
  36. primaryValidators[height] = forgedVals
  37. }
  38. primary := mockp.New(chainID, primaryHeaders, primaryValidators)
  39. c, err := light.NewClient(
  40. ctx,
  41. chainID,
  42. light.TrustOptions{
  43. Period: 4 * time.Hour,
  44. Height: 1,
  45. Hash: primaryHeaders[1].Hash(),
  46. },
  47. primary,
  48. []provider.Provider{witness},
  49. dbs.New(dbm.NewMemDB()),
  50. light.Logger(log.TestingLogger()),
  51. )
  52. require.NoError(t, err)
  53. // Check verification returns an error.
  54. _, err = c.VerifyLightBlockAtHeight(ctx, 10, bTime.Add(1*time.Hour))
  55. if assert.Error(t, err) {
  56. assert.Equal(t, light.ErrLightClientAttack, err)
  57. }
  58. // Check evidence was sent to both full nodes.
  59. evAgainstPrimary := &types.LightClientAttackEvidence{
  60. // after the divergence height the valset doesn't change so we expect the evidence to be for height 10
  61. ConflictingBlock: &types.LightBlock{
  62. SignedHeader: primaryHeaders[10],
  63. ValidatorSet: primaryValidators[10],
  64. },
  65. CommonHeight: 4,
  66. }
  67. assert.True(t, witness.HasEvidence(evAgainstPrimary))
  68. evAgainstWitness := &types.LightClientAttackEvidence{
  69. // when forming evidence against witness we learn that the canonical chain continued to change validator sets
  70. // hence the conflicting block is at 7
  71. ConflictingBlock: &types.LightBlock{
  72. SignedHeader: witnessHeaders[7],
  73. ValidatorSet: witnessValidators[7],
  74. },
  75. CommonHeight: 4,
  76. }
  77. assert.True(t, primary.HasEvidence(evAgainstWitness))
  78. }
  79. func TestLightClientAttackEvidence_Equivocation(t *testing.T) {
  80. verificationOptions := map[string]light.Option{
  81. "sequential": light.SequentialVerification(),
  82. "skipping": light.SkippingVerification(light.DefaultTrustLevel),
  83. }
  84. for s, verificationOption := range verificationOptions {
  85. t.Log("==> verification", s)
  86. // primary performs an equivocation attack
  87. var (
  88. latestHeight = int64(10)
  89. valSize = 5
  90. divergenceHeight = int64(6)
  91. primaryHeaders = make(map[int64]*types.SignedHeader, latestHeight)
  92. primaryValidators = make(map[int64]*types.ValidatorSet, latestHeight)
  93. )
  94. // validators don't change in this network (however we still use a map just for convenience)
  95. witnessHeaders, witnessValidators, chainKeys := genMockNodeWithKeys(chainID, latestHeight+2, valSize, 2, bTime)
  96. witness := mockp.New(chainID, witnessHeaders, witnessValidators)
  97. for height := int64(1); height <= latestHeight; height++ {
  98. if height < divergenceHeight {
  99. primaryHeaders[height] = witnessHeaders[height]
  100. primaryValidators[height] = witnessValidators[height]
  101. continue
  102. }
  103. // we don't have a network partition so we will make 4/5 (greater than 2/3) malicious and vote again for
  104. // a different block (which we do by adding txs)
  105. primaryHeaders[height] = chainKeys[height].GenSignedHeader(chainID, height,
  106. bTime.Add(time.Duration(height)*time.Minute), []types.Tx{[]byte("abcd")},
  107. witnessValidators[height], witnessValidators[height+1], hash("app_hash"),
  108. hash("cons_hash"), hash("results_hash"), 0, len(chainKeys[height])-1)
  109. primaryValidators[height] = witnessValidators[height]
  110. }
  111. primary := mockp.New(chainID, primaryHeaders, primaryValidators)
  112. c, err := light.NewClient(
  113. ctx,
  114. chainID,
  115. light.TrustOptions{
  116. Period: 4 * time.Hour,
  117. Height: 1,
  118. Hash: primaryHeaders[1].Hash(),
  119. },
  120. primary,
  121. []provider.Provider{witness},
  122. dbs.New(dbm.NewMemDB()),
  123. light.Logger(log.TestingLogger()),
  124. verificationOption,
  125. )
  126. require.NoError(t, err)
  127. // Check verification returns an error.
  128. _, err = c.VerifyLightBlockAtHeight(ctx, 10, bTime.Add(1*time.Hour))
  129. if assert.Error(t, err) {
  130. assert.Equal(t, light.ErrLightClientAttack, err)
  131. }
  132. // Check evidence was sent to both full nodes.
  133. // Common height should be set to the height of the divergent header in the instance
  134. // of an equivocation attack and the validator sets are the same as what the witness has
  135. evAgainstPrimary := &types.LightClientAttackEvidence{
  136. ConflictingBlock: &types.LightBlock{
  137. SignedHeader: primaryHeaders[divergenceHeight],
  138. ValidatorSet: primaryValidators[divergenceHeight],
  139. },
  140. CommonHeight: divergenceHeight,
  141. }
  142. assert.True(t, witness.HasEvidence(evAgainstPrimary))
  143. evAgainstWitness := &types.LightClientAttackEvidence{
  144. ConflictingBlock: &types.LightBlock{
  145. SignedHeader: witnessHeaders[divergenceHeight],
  146. ValidatorSet: witnessValidators[divergenceHeight],
  147. },
  148. CommonHeight: divergenceHeight,
  149. }
  150. assert.True(t, primary.HasEvidence(evAgainstWitness))
  151. }
  152. }
  153. // 1. Different nodes therefore a divergent header is produced.
  154. // => light client returns an error upon creation because primary and witness
  155. // have a different view.
  156. func TestClientDivergentTraces1(t *testing.T) {
  157. primary := mockp.New(genMockNode(chainID, 10, 5, 2, bTime))
  158. firstBlock, err := primary.LightBlock(ctx, 1)
  159. require.NoError(t, err)
  160. witness := mockp.New(genMockNode(chainID, 10, 5, 2, bTime))
  161. _, err = light.NewClient(
  162. ctx,
  163. chainID,
  164. light.TrustOptions{
  165. Height: 1,
  166. Hash: firstBlock.Hash(),
  167. Period: 4 * time.Hour,
  168. },
  169. primary,
  170. []provider.Provider{witness},
  171. dbs.New(dbm.NewMemDB()),
  172. light.Logger(log.TestingLogger()),
  173. )
  174. require.Error(t, err)
  175. assert.Contains(t, err.Error(), "does not match primary")
  176. }
  177. // 2. Two out of three nodes don't respond but the third has a header that matches
  178. // => verification should be successful and all the witnesses should remain
  179. func TestClientDivergentTraces2(t *testing.T) {
  180. primary := mockp.New(genMockNode(chainID, 10, 5, 2, bTime))
  181. firstBlock, err := primary.LightBlock(ctx, 1)
  182. require.NoError(t, err)
  183. c, err := light.NewClient(
  184. ctx,
  185. chainID,
  186. light.TrustOptions{
  187. Height: 1,
  188. Hash: firstBlock.Hash(),
  189. Period: 4 * time.Hour,
  190. },
  191. primary,
  192. []provider.Provider{deadNode, deadNode, primary},
  193. dbs.New(dbm.NewMemDB()),
  194. light.Logger(log.TestingLogger()),
  195. )
  196. require.NoError(t, err)
  197. _, err = c.VerifyLightBlockAtHeight(ctx, 10, bTime.Add(1*time.Hour))
  198. assert.NoError(t, err)
  199. assert.Equal(t, 3, len(c.Witnesses()))
  200. }
  201. // 3. witness has the same first header, but different second header
  202. // => creation should succeed, but the verification should fail
  203. func TestClientDivergentTraces3(t *testing.T) {
  204. _, primaryHeaders, primaryVals := genMockNode(chainID, 10, 5, 2, bTime)
  205. primary := mockp.New(chainID, primaryHeaders, primaryVals)
  206. firstBlock, err := primary.LightBlock(ctx, 1)
  207. require.NoError(t, err)
  208. _, mockHeaders, mockVals := genMockNode(chainID, 10, 5, 2, bTime)
  209. mockHeaders[1] = primaryHeaders[1]
  210. mockVals[1] = primaryVals[1]
  211. witness := mockp.New(chainID, mockHeaders, mockVals)
  212. c, err := light.NewClient(
  213. ctx,
  214. chainID,
  215. light.TrustOptions{
  216. Height: 1,
  217. Hash: firstBlock.Hash(),
  218. Period: 4 * time.Hour,
  219. },
  220. primary,
  221. []provider.Provider{witness},
  222. dbs.New(dbm.NewMemDB()),
  223. light.Logger(log.TestingLogger()),
  224. )
  225. require.NoError(t, err)
  226. _, err = c.VerifyLightBlockAtHeight(ctx, 10, bTime.Add(1*time.Hour))
  227. assert.Error(t, err)
  228. assert.Equal(t, 1, len(c.Witnesses()))
  229. }