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.

161 lines
4.1 KiB

  1. package types
  2. import (
  3. "math"
  4. "testing"
  5. "time"
  6. "github.com/stretchr/testify/assert"
  7. "github.com/tendermint/tendermint/crypto"
  8. "github.com/tendermint/tendermint/proto/tendermint/version"
  9. )
  10. func TestLightBlockValidateBasic(t *testing.T) {
  11. header := makeRandHeader()
  12. commit := randCommit(time.Now())
  13. vals, _ := RandValidatorSet(5, 1)
  14. header.Height = commit.Height
  15. header.LastBlockID = commit.BlockID
  16. header.ValidatorsHash = vals.Hash()
  17. vals2, _ := RandValidatorSet(3, 1)
  18. vals3 := vals.Copy()
  19. vals3.Proposer = &Validator{}
  20. commit.BlockID.Hash = header.Hash()
  21. sh := &SignedHeader{
  22. Header: &header,
  23. Commit: commit,
  24. }
  25. testCases := []struct {
  26. name string
  27. sh *SignedHeader
  28. vals *ValidatorSet
  29. expectErr bool
  30. }{
  31. {"valid light block", sh, vals, false},
  32. {"hashes don't match", sh, vals2, true},
  33. {"invalid validator set", sh, vals3, true},
  34. {"invalid signed header", &SignedHeader{Header: &header, Commit: randCommit(time.Now())}, vals, true},
  35. }
  36. for _, tc := range testCases {
  37. lightBlock := LightBlock{
  38. SignedHeader: tc.sh,
  39. ValidatorSet: tc.vals,
  40. }
  41. err := lightBlock.ValidateBasic(header.ChainID)
  42. if tc.expectErr {
  43. assert.Error(t, err, tc.name)
  44. } else {
  45. assert.NoError(t, err, tc.name)
  46. }
  47. }
  48. }
  49. func TestLightBlockProtobuf(t *testing.T) {
  50. header := makeRandHeader()
  51. commit := randCommit(time.Now())
  52. vals, _ := RandValidatorSet(5, 1)
  53. header.Height = commit.Height
  54. header.LastBlockID = commit.BlockID
  55. header.ValidatorsHash = vals.Hash()
  56. vals3 := vals.Copy()
  57. vals3.Proposer = &Validator{}
  58. commit.BlockID.Hash = header.Hash()
  59. sh := &SignedHeader{
  60. Header: &header,
  61. Commit: commit,
  62. }
  63. testCases := []struct {
  64. name string
  65. sh *SignedHeader
  66. vals *ValidatorSet
  67. toProtoErr bool
  68. toBlockErr bool
  69. }{
  70. {"valid light block", sh, vals, false, false},
  71. {"empty signed header", &SignedHeader{}, vals, false, false},
  72. {"empty validator set", sh, &ValidatorSet{}, false, true},
  73. {"empty light block", &SignedHeader{}, &ValidatorSet{}, false, true},
  74. }
  75. for _, tc := range testCases {
  76. lightBlock := &LightBlock{
  77. SignedHeader: tc.sh,
  78. ValidatorSet: tc.vals,
  79. }
  80. lbp, err := lightBlock.ToProto()
  81. if tc.toProtoErr {
  82. assert.Error(t, err, tc.name)
  83. } else {
  84. assert.NoError(t, err, tc.name)
  85. }
  86. lb, err := LightBlockFromProto(lbp)
  87. if tc.toBlockErr {
  88. assert.Error(t, err, tc.name)
  89. } else {
  90. assert.NoError(t, err, tc.name)
  91. assert.Equal(t, lightBlock, lb)
  92. }
  93. }
  94. }
  95. func TestSignedHeaderValidateBasic(t *testing.T) {
  96. commit := randCommit(time.Now())
  97. chainID := "𠜎"
  98. timestamp := time.Date(math.MaxInt64, 0, 0, 0, 0, 0, math.MaxInt64, time.UTC)
  99. h := Header{
  100. Version: version.Consensus{Block: math.MaxInt64, App: math.MaxInt64},
  101. ChainID: chainID,
  102. Height: commit.Height,
  103. Time: timestamp,
  104. LastBlockID: commit.BlockID,
  105. LastCommitHash: commit.Hash(),
  106. DataHash: commit.Hash(),
  107. ValidatorsHash: commit.Hash(),
  108. NextValidatorsHash: commit.Hash(),
  109. ConsensusHash: commit.Hash(),
  110. AppHash: commit.Hash(),
  111. LastResultsHash: commit.Hash(),
  112. EvidenceHash: commit.Hash(),
  113. ProposerAddress: crypto.AddressHash([]byte("proposer_address")),
  114. }
  115. validSignedHeader := SignedHeader{Header: &h, Commit: commit}
  116. validSignedHeader.Commit.BlockID.Hash = validSignedHeader.Hash()
  117. invalidSignedHeader := SignedHeader{}
  118. testCases := []struct {
  119. testName string
  120. shHeader *Header
  121. shCommit *Commit
  122. expectErr bool
  123. }{
  124. {"Valid Signed Header", validSignedHeader.Header, validSignedHeader.Commit, false},
  125. {"Invalid Signed Header", invalidSignedHeader.Header, validSignedHeader.Commit, true},
  126. {"Invalid Signed Header", validSignedHeader.Header, invalidSignedHeader.Commit, true},
  127. }
  128. for _, tc := range testCases {
  129. tc := tc
  130. t.Run(tc.testName, func(t *testing.T) {
  131. sh := SignedHeader{
  132. Header: tc.shHeader,
  133. Commit: tc.shCommit,
  134. }
  135. assert.Equal(
  136. t,
  137. tc.expectErr,
  138. sh.ValidateBasic(validSignedHeader.Header.ChainID) != nil,
  139. "Validate Basic had an unexpected result",
  140. )
  141. })
  142. }
  143. }