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.

173 lines
4.8 KiB

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