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.8 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 db
  2. import (
  3. "testing"
  4. "github.com/stretchr/testify/assert"
  5. "github.com/stretchr/testify/require"
  6. dbm "github.com/tendermint/tm-db"
  7. "github.com/tendermint/tendermint/types"
  8. )
  9. func TestLast_FirstSignedHeaderHeight(t *testing.T) {
  10. dbStore := New(dbm.NewMemDB(), "TestLast_FirstSignedHeaderHeight")
  11. // Empty store
  12. height, err := dbStore.LastSignedHeaderHeight()
  13. require.NoError(t, err)
  14. assert.EqualValues(t, -1, height)
  15. height, err = dbStore.FirstSignedHeaderHeight()
  16. require.NoError(t, err)
  17. assert.EqualValues(t, -1, height)
  18. // 1 key
  19. err = dbStore.SaveSignedHeaderAndNextValidatorSet(
  20. &types.SignedHeader{Header: &types.Header{Height: 1}}, &types.ValidatorSet{})
  21. require.NoError(t, err)
  22. height, err = dbStore.LastSignedHeaderHeight()
  23. require.NoError(t, err)
  24. assert.EqualValues(t, 1, height)
  25. height, err = dbStore.FirstSignedHeaderHeight()
  26. require.NoError(t, err)
  27. assert.EqualValues(t, 1, height)
  28. }
  29. func Test_SaveSignedHeaderAndNextValidatorSet(t *testing.T) {
  30. dbStore := New(dbm.NewMemDB(), "Test_SaveSignedHeaderAndNextValidatorSet")
  31. // Empty store
  32. h, err := dbStore.SignedHeader(1)
  33. require.NoError(t, err)
  34. assert.Nil(t, h)
  35. valSet, err := dbStore.ValidatorSet(2)
  36. require.NoError(t, err)
  37. assert.Nil(t, valSet)
  38. // 1 key
  39. err = dbStore.SaveSignedHeaderAndNextValidatorSet(
  40. &types.SignedHeader{Header: &types.Header{Height: 1}}, &types.ValidatorSet{})
  41. require.NoError(t, err)
  42. h, err = dbStore.SignedHeader(1)
  43. require.NoError(t, err)
  44. assert.NotNil(t, h)
  45. valSet, err = dbStore.ValidatorSet(2)
  46. require.NoError(t, err)
  47. assert.NotNil(t, valSet)
  48. // Empty store
  49. err = dbStore.DeleteSignedHeaderAndNextValidatorSet(1)
  50. require.NoError(t, err)
  51. h, err = dbStore.SignedHeader(1)
  52. require.NoError(t, err)
  53. assert.Nil(t, h)
  54. valSet, err = dbStore.ValidatorSet(2)
  55. require.NoError(t, err)
  56. assert.Nil(t, valSet)
  57. }