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.

178 lines
4.5 KiB

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