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.

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