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.

153 lines
4.3 KiB

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