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.

211 lines
5.8 KiB

  1. package lite
  2. import (
  3. "fmt"
  4. "sync"
  5. "testing"
  6. "github.com/stretchr/testify/assert"
  7. "github.com/stretchr/testify/require"
  8. dbm "github.com/tendermint/tendermint/libs/db"
  9. log "github.com/tendermint/tendermint/libs/log"
  10. )
  11. func TestInquirerValidPath(t *testing.T) {
  12. assert, require := assert.New(t), require.New(t)
  13. trust := NewDBProvider("trust", dbm.NewMemDB())
  14. source := NewDBProvider("source", dbm.NewMemDB())
  15. // Set up the validators to generate test blocks.
  16. var vote int64 = 10
  17. keys := genPrivKeys(5)
  18. nkeys := keys.Extend(1)
  19. // Construct a bunch of commits, each with one more height than the last.
  20. chainID := "inquiry-test"
  21. consHash := []byte("params")
  22. resHash := []byte("results")
  23. count := 50
  24. fcz := make([]FullCommit, count)
  25. for i := 0; i < count; i++ {
  26. vals := keys.ToValidators(vote, 0)
  27. nextVals := nkeys.ToValidators(vote, 0)
  28. h := int64(1 + i)
  29. appHash := []byte(fmt.Sprintf("h=%d", h))
  30. fcz[i] = keys.GenFullCommit(
  31. chainID, h, nil,
  32. vals, nextVals,
  33. appHash, consHash, resHash, 0, len(keys))
  34. // Extend the keys by 1 each time.
  35. keys = nkeys
  36. nkeys = nkeys.Extend(1)
  37. }
  38. // Initialize a Verifier with the initial state.
  39. err := trust.SaveFullCommit(fcz[0])
  40. require.Nil(err)
  41. cert := NewDynamicVerifier(chainID, trust, source)
  42. cert.SetLogger(log.TestingLogger())
  43. // This should fail validation:
  44. sh := fcz[count-1].SignedHeader
  45. err = cert.Verify(sh)
  46. require.NotNil(err)
  47. // Adding a few commits in the middle should be insufficient.
  48. for i := 10; i < 13; i++ {
  49. err := source.SaveFullCommit(fcz[i])
  50. require.Nil(err)
  51. }
  52. err = cert.Verify(sh)
  53. assert.NotNil(err)
  54. // With more info, we succeed.
  55. for i := 0; i < count; i++ {
  56. err := source.SaveFullCommit(fcz[i])
  57. require.Nil(err)
  58. }
  59. err = cert.Verify(sh)
  60. assert.Nil(err, "%+v", err)
  61. }
  62. func TestInquirerVerifyHistorical(t *testing.T) {
  63. assert, require := assert.New(t), require.New(t)
  64. trust := NewDBProvider("trust", dbm.NewMemDB())
  65. source := NewDBProvider("source", dbm.NewMemDB())
  66. // Set up the validators to generate test blocks.
  67. var vote int64 = 10
  68. keys := genPrivKeys(5)
  69. nkeys := keys.Extend(1)
  70. // Construct a bunch of commits, each with one more height than the last.
  71. chainID := "inquiry-test"
  72. count := 10
  73. consHash := []byte("special-params")
  74. fcz := make([]FullCommit, count)
  75. for i := 0; i < count; i++ {
  76. vals := keys.ToValidators(vote, 0)
  77. nextVals := nkeys.ToValidators(vote, 0)
  78. h := int64(1 + i)
  79. appHash := []byte(fmt.Sprintf("h=%d", h))
  80. resHash := []byte(fmt.Sprintf("res=%d", h))
  81. fcz[i] = keys.GenFullCommit(
  82. chainID, h, nil,
  83. vals, nextVals,
  84. appHash, consHash, resHash, 0, len(keys))
  85. // Extend the keys by 1 each time.
  86. keys = nkeys
  87. nkeys = nkeys.Extend(1)
  88. }
  89. // Initialize a Verifier with the initial state.
  90. err := trust.SaveFullCommit(fcz[0])
  91. require.Nil(err)
  92. cert := NewDynamicVerifier(chainID, trust, source)
  93. cert.SetLogger(log.TestingLogger())
  94. // Store a few full commits as trust.
  95. for _, i := range []int{2, 5} {
  96. trust.SaveFullCommit(fcz[i])
  97. }
  98. // See if we can jump forward using trusted full commits.
  99. // Souce doesn't have fcz[9] so cert.LastTrustedHeight wont' change.
  100. err = source.SaveFullCommit(fcz[7])
  101. require.Nil(err, "%+v", err)
  102. sh := fcz[8].SignedHeader
  103. err = cert.Verify(sh)
  104. require.Nil(err, "%+v", err)
  105. assert.Equal(fcz[7].Height(), cert.LastTrustedHeight())
  106. fc_, err := trust.LatestFullCommit(chainID, fcz[8].Height(), fcz[8].Height())
  107. require.NotNil(err, "%+v", err)
  108. assert.Equal(fc_, (FullCommit{}))
  109. // With fcz[9] Verify will update last trusted height.
  110. err = source.SaveFullCommit(fcz[9])
  111. require.Nil(err, "%+v", err)
  112. sh = fcz[8].SignedHeader
  113. err = cert.Verify(sh)
  114. require.Nil(err, "%+v", err)
  115. assert.Equal(fcz[8].Height(), cert.LastTrustedHeight())
  116. fc_, err = trust.LatestFullCommit(chainID, fcz[8].Height(), fcz[8].Height())
  117. require.Nil(err, "%+v", err)
  118. assert.Equal(fc_.Height(), fcz[8].Height())
  119. // Add access to all full commits via untrusted source.
  120. for i := 0; i < count; i++ {
  121. err := source.SaveFullCommit(fcz[i])
  122. require.Nil(err)
  123. }
  124. // Try to check an unknown seed in the past.
  125. sh = fcz[3].SignedHeader
  126. err = cert.Verify(sh)
  127. require.Nil(err, "%+v", err)
  128. assert.Equal(fcz[8].Height(), cert.LastTrustedHeight())
  129. // Jump all the way forward again.
  130. sh = fcz[count-1].SignedHeader
  131. err = cert.Verify(sh)
  132. require.Nil(err, "%+v", err)
  133. assert.Equal(fcz[9].Height(), cert.LastTrustedHeight())
  134. }
  135. func TestConcurrencyInquirerVerify(t *testing.T) {
  136. _, require := assert.New(t), require.New(t)
  137. trust := NewDBProvider("trust", dbm.NewMemDB()).SetLimit(10)
  138. source := NewDBProvider("source", dbm.NewMemDB())
  139. // Set up the validators to generate test blocks.
  140. var vote int64 = 10
  141. keys := genPrivKeys(5)
  142. nkeys := keys.Extend(1)
  143. // Construct a bunch of commits, each with one more height than the last.
  144. chainID := "inquiry-test"
  145. count := 10
  146. consHash := []byte("special-params")
  147. fcz := make([]FullCommit, count)
  148. for i := 0; i < count; i++ {
  149. vals := keys.ToValidators(vote, 0)
  150. nextVals := nkeys.ToValidators(vote, 0)
  151. h := int64(1 + i)
  152. appHash := []byte(fmt.Sprintf("h=%d", h))
  153. resHash := []byte(fmt.Sprintf("res=%d", h))
  154. fcz[i] = keys.GenFullCommit(
  155. chainID, h, nil,
  156. vals, nextVals,
  157. appHash, consHash, resHash, 0, len(keys))
  158. // Extend the keys by 1 each time.
  159. keys = nkeys
  160. nkeys = nkeys.Extend(1)
  161. }
  162. // Initialize a Verifier with the initial state.
  163. err := trust.SaveFullCommit(fcz[0])
  164. require.Nil(err)
  165. cert := NewDynamicVerifier(chainID, trust, source)
  166. cert.SetLogger(log.TestingLogger())
  167. err = source.SaveFullCommit(fcz[7])
  168. err = source.SaveFullCommit(fcz[8])
  169. require.Nil(err, "%+v", err)
  170. sh := fcz[8].SignedHeader
  171. var wg sync.WaitGroup
  172. count = 100
  173. errList := make([]error, count)
  174. for i := 0; i < count; i++ {
  175. wg.Add(1)
  176. go func(index int) {
  177. errList[index] = cert.Verify(sh)
  178. defer wg.Done()
  179. }(i)
  180. }
  181. wg.Wait()
  182. for _, err := range errList {
  183. require.Nil(err)
  184. }
  185. }