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.

169 lines
4.7 KiB

  1. // nolint: vetshadow
  2. package lite_test
  3. import (
  4. "fmt"
  5. "testing"
  6. "github.com/stretchr/testify/assert"
  7. "github.com/stretchr/testify/require"
  8. "github.com/tendermint/tendermint/lite"
  9. )
  10. func TestInquirerValidPath(t *testing.T) {
  11. assert, require := assert.New(t), require.New(t)
  12. trust := lite.NewMemStoreProvider()
  13. source := lite.NewMemStoreProvider()
  14. // set up the validators to generate test blocks
  15. var vote int64 = 10
  16. keys := lite.GenValKeys(5)
  17. // construct a bunch of commits, each with one more height than the last
  18. chainID := "inquiry-test"
  19. consHash := []byte("params")
  20. resHash := []byte("results")
  21. count := 50
  22. commits := make([]lite.FullCommit, count)
  23. for i := 0; i < count; i++ {
  24. // extend the keys by 1 each time
  25. keys = keys.Extend(1)
  26. vals := keys.ToValidators(vote, 0)
  27. h := int64(20 + 10*i)
  28. appHash := []byte(fmt.Sprintf("h=%d", h))
  29. commits[i] = keys.GenFullCommit(chainID, h, nil, vals, appHash, consHash, resHash, 0, len(keys))
  30. }
  31. // initialize a certifier with the initial state
  32. cert := lite.NewInquiring(chainID, commits[0], trust, source)
  33. // this should fail validation....
  34. commit := commits[count-1].Commit
  35. err := cert.Certify(commit)
  36. require.NotNil(err)
  37. // add a few seed in the middle should be insufficient
  38. for i := 10; i < 13; i++ {
  39. err := source.StoreCommit(commits[i])
  40. require.Nil(err)
  41. }
  42. err = cert.Certify(commit)
  43. assert.NotNil(err)
  44. // with more info, we succeed
  45. for i := 0; i < count; i++ {
  46. err := source.StoreCommit(commits[i])
  47. require.Nil(err)
  48. }
  49. err = cert.Certify(commit)
  50. assert.Nil(err, "%+v", err)
  51. }
  52. func TestInquirerMinimalPath(t *testing.T) {
  53. assert, require := assert.New(t), require.New(t)
  54. trust := lite.NewMemStoreProvider()
  55. source := lite.NewMemStoreProvider()
  56. // set up the validators to generate test blocks
  57. var vote int64 = 10
  58. keys := lite.GenValKeys(5)
  59. // construct a bunch of commits, each with one more height than the last
  60. chainID := "minimal-path"
  61. consHash := []byte("other-params")
  62. count := 12
  63. commits := make([]lite.FullCommit, count)
  64. for i := 0; i < count; i++ {
  65. // extend the validators, so we are just below 2/3
  66. keys = keys.Extend(len(keys)/2 - 1)
  67. vals := keys.ToValidators(vote, 0)
  68. h := int64(5 + 10*i)
  69. appHash := []byte(fmt.Sprintf("h=%d", h))
  70. resHash := []byte(fmt.Sprintf("res=%d", h))
  71. commits[i] = keys.GenFullCommit(chainID, h, nil, vals, appHash, consHash, resHash, 0, len(keys))
  72. }
  73. // initialize a certifier with the initial state
  74. cert := lite.NewInquiring(chainID, commits[0], trust, source)
  75. // this should fail validation....
  76. commit := commits[count-1].Commit
  77. err := cert.Certify(commit)
  78. require.NotNil(err)
  79. // add a few seed in the middle should be insufficient
  80. for i := 5; i < 8; i++ {
  81. err := source.StoreCommit(commits[i])
  82. require.Nil(err)
  83. }
  84. err = cert.Certify(commit)
  85. assert.NotNil(err)
  86. // with more info, we succeed
  87. for i := 0; i < count; i++ {
  88. err := source.StoreCommit(commits[i])
  89. require.Nil(err)
  90. }
  91. err = cert.Certify(commit)
  92. assert.Nil(err, "%+v", err)
  93. }
  94. func TestInquirerVerifyHistorical(t *testing.T) {
  95. assert, require := assert.New(t), require.New(t)
  96. trust := lite.NewMemStoreProvider()
  97. source := lite.NewMemStoreProvider()
  98. // set up the validators to generate test blocks
  99. var vote int64 = 10
  100. keys := lite.GenValKeys(5)
  101. // construct a bunch of commits, each with one more height than the last
  102. chainID := "inquiry-test"
  103. count := 10
  104. consHash := []byte("special-params")
  105. commits := make([]lite.FullCommit, count)
  106. for i := 0; i < count; i++ {
  107. // extend the keys by 1 each time
  108. keys = keys.Extend(1)
  109. vals := keys.ToValidators(vote, 0)
  110. h := int64(20 + 10*i)
  111. appHash := []byte(fmt.Sprintf("h=%d", h))
  112. resHash := []byte(fmt.Sprintf("res=%d", h))
  113. commits[i] = keys.GenFullCommit(chainID, h, nil, vals, appHash, consHash, resHash, 0, len(keys))
  114. }
  115. // initialize a certifier with the initial state
  116. cert := lite.NewInquiring(chainID, commits[0], trust, source)
  117. // store a few commits as trust
  118. for _, i := range []int{2, 5} {
  119. trust.StoreCommit(commits[i])
  120. }
  121. // let's see if we can jump forward using trusted commits
  122. err := source.StoreCommit(commits[7])
  123. require.Nil(err, "%+v", err)
  124. check := commits[7].Commit
  125. err = cert.Certify(check)
  126. require.Nil(err, "%+v", err)
  127. assert.Equal(check.Height(), cert.LastHeight())
  128. // add access to all commits via untrusted source
  129. for i := 0; i < count; i++ {
  130. err := source.StoreCommit(commits[i])
  131. require.Nil(err)
  132. }
  133. // try to check an unknown seed in the past
  134. mid := commits[3].Commit
  135. err = cert.Certify(mid)
  136. require.Nil(err, "%+v", err)
  137. assert.Equal(mid.Height(), cert.LastHeight())
  138. // and jump all the way forward again
  139. end := commits[count-1].Commit
  140. err = cert.Certify(end)
  141. require.Nil(err, "%+v", err)
  142. assert.Equal(end.Height(), cert.LastHeight())
  143. }