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.

76 lines
1.9 KiB

lite: follow up from #3989 (#4209) * rename adjusted to adjacent Refs https://github.com/tendermint/tendermint/pull/3989#discussion_r352140829 * rename ErrTooMuchChange to ErrNotEnoughVotingPowerSigned Refs https://github.com/tendermint/tendermint/pull/3989#discussion_r352142785 * verify commit is properly signed * remove no longer trusted headers * restore trustedHeader and trustedNextVals * check trustedHeader using options Refs https://github.com/tendermint/tendermint/pull/4209#issuecomment-562462165 * use correct var when checking if headers are adjacent in bisection func + replace TODO with a comment https://github.com/tendermint/tendermint/pull/3989#discussion_r352125455 * return header in VerifyHeaderAtHeight because that way we avoid DB call + add godoc comments + check if there are no headers yet in AutoClient https://github.com/tendermint/tendermint/pull/3989#pullrequestreview-315454506 * TestVerifyAdjacentHeaders: add 2 more test-cases + add TestVerifyReturnsErrorIfTrustLevelIsInvalid * lite: avoid overflow when parsing key in db store! * lite: rename AutoClient#Err to Errs * lite: add a test for AutoClient * lite: fix keyPattern and call itr.Next in db store * lite: add two tests for db store * lite: add TestClientRemovesNoLongerTrustedHeaders * lite: test Client#Cleanup * lite: test restoring trustedHeader https://github.com/tendermint/tendermint/pull/4209#issuecomment-562462165 * lite: comment out unused code in test_helpers * fix TestVerifyReturnsErrorIfTrustLevelIsInvalid after merge * change defaultRemoveNoLongerTrustedHeadersPeriod and add docs * write more doc * lite: uncomment testable examples * use stdlog.Fatal to stop AutoClient tests * make lll linter happy * separate errors for 2 cases - the validator set of a skipped header cannot be trusted, i.e. <1/3rd of h1 validator set has signed (new error, something like ErrNewValSetCantBeTrusted) - the validator set is trusted but < 2/3rds has signed (ErrNewHeaderCantBeTrusted) https://github.com/tendermint/tendermint/pull/4209#discussion_r360331253 * remove all headers (even the last one) that are outside of the trusting period. By doing this, we avoid checking the trustedHeader's hash in checkTrustedHeaderUsingOptions (case #1). https://github.com/tendermint/tendermint/pull/4209#discussion_r360332460 * explain restoreTrustedHeaderAndNextVals better https://github.com/tendermint/tendermint/pull/4209#discussion_r360602328 * add ConfirmationFunction option for optionally prompting for user input Y/n before removing headers Refs https://github.com/tendermint/tendermint/pull/4209#discussion_r360602945 * make cleaning optional https://github.com/tendermint/tendermint/pull/4209#discussion_r364838189 * return error when user refused to remove headers * check for double votes in VerifyCommitTrusting * leave only ErrNewValSetCantBeTrusted error to differenciate between h2Vals.VerifyCommit and h1NextVals.VerifyCommitTrusting * fix example tests * remove unnecessary if condition https://github.com/tendermint/tendermint/pull/4209#discussion_r365171847 It will be handled by the above switch. * verifyCommitBasic does not depend on vals Co-authored-by: Marko <marbar3778@yahoo.com>
5 years ago
  1. package lite
  2. import (
  3. "testing"
  4. "time"
  5. "github.com/stretchr/testify/assert"
  6. "github.com/stretchr/testify/require"
  7. dbm "github.com/tendermint/tm-db"
  8. "github.com/tendermint/tendermint/libs/log"
  9. mockp "github.com/tendermint/tendermint/lite2/provider/mock"
  10. dbs "github.com/tendermint/tendermint/lite2/store/db"
  11. "github.com/tendermint/tendermint/types"
  12. )
  13. func TestAutoClient(t *testing.T) {
  14. const (
  15. chainID = "TestAutoClient"
  16. )
  17. var (
  18. keys = genPrivKeys(4)
  19. // 20, 30, 40, 50 - the first 3 don't have 2/3, the last 3 do!
  20. vals = keys.ToValidators(20, 10)
  21. bTime = time.Now().Add(-1 * time.Hour)
  22. header = keys.GenSignedHeader(chainID, 1, bTime, nil, vals, vals,
  23. []byte("app_hash"), []byte("cons_hash"), []byte("results_hash"), 0, len(keys))
  24. )
  25. base, err := NewClient(
  26. chainID,
  27. TrustOptions{
  28. Period: 4 * time.Hour,
  29. Height: 1,
  30. Hash: header.Hash(),
  31. },
  32. mockp.New(
  33. chainID,
  34. map[int64]*types.SignedHeader{
  35. // trusted header
  36. 1: header,
  37. // interim header (3/3 signed)
  38. 2: keys.GenSignedHeader(chainID, 2, bTime.Add(30*time.Minute), nil, vals, vals,
  39. []byte("app_hash"), []byte("cons_hash"), []byte("results_hash"), 0, len(keys)),
  40. // last header (3/3 signed)
  41. 3: keys.GenSignedHeader(chainID, 3, bTime.Add(1*time.Hour), nil, vals, vals,
  42. []byte("app_hash"), []byte("cons_hash"), []byte("results_hash"), 0, len(keys)),
  43. },
  44. map[int64]*types.ValidatorSet{
  45. 1: vals,
  46. 2: vals,
  47. 3: vals,
  48. 4: vals,
  49. },
  50. ),
  51. dbs.New(dbm.NewMemDB(), chainID),
  52. )
  53. require.NoError(t, err)
  54. base.SetLogger(log.TestingLogger())
  55. c := NewAutoClient(base, 1*time.Second)
  56. defer c.Stop()
  57. for i := 2; i <= 3; i++ {
  58. select {
  59. case h := <-c.TrustedHeaders():
  60. assert.EqualValues(t, i, h.Height)
  61. case err := <-c.Errs():
  62. require.NoError(t, err)
  63. case <-time.After(2 * time.Second):
  64. t.Fatal("no headers/errors received in 2 sec")
  65. }
  66. }
  67. }