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