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.

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