github.com/DFWallet/tendermint-cosmos@v0.0.2/types/light_test.go (about)

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