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.

165 lines
4.6 KiB

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