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.

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