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.

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