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.

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