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