github.com/pokt-network/tendermint@v0.32.11-0.20230426215212-59310158d3e9/types/params_test.go (about)

     1  package types
     2  
     3  import (
     4  	"bytes"
     5  	"github.com/stretchr/testify/assert"
     6  	"sort"
     7  	"testing"
     8  
     9  	abci "github.com/tendermint/tendermint/abci/types"
    10  )
    11  
    12  var (
    13  	valEd25519   = []string{ABCIPubKeyTypeEd25519}
    14  	valSecp256k1 = []string{ABCIPubKeyTypeSecp256k1}
    15  )
    16  
    17  func TestConsensusParamsValidation(t *testing.T) {
    18  	testCases := []struct {
    19  		params ConsensusParams
    20  		valid  bool
    21  	}{
    22  		// test block params
    23  		0: {makeParams(1, 0, 10, 1, valEd25519), true},
    24  		1: {makeParams(0, 0, 10, 1, valEd25519), false},
    25  		2: {makeParams(47*1024*1024, 0, 10, 1, valEd25519), true},
    26  		3: {makeParams(10, 0, 10, 1, valEd25519), true},
    27  		4: {makeParams(100*1024*1024, 0, 10, 1, valEd25519), true},
    28  		5: {makeParams(101*1024*1024, 0, 10, 1, valEd25519), false},
    29  		6: {makeParams(1024*1024*1024, 0, 10, 1, valEd25519), false},
    30  		7: {makeParams(1024*1024*1024, 0, 10, -1, valEd25519), false},
    31  		8: {makeParams(1, 0, -10, 1, valEd25519), false},
    32  		// test evidence params
    33  		9:  {makeParams(1, 0, 10, 0, valEd25519), false},
    34  		10: {makeParams(1, 0, 10, -1, valEd25519), false},
    35  		// test no pubkey type provided
    36  		11: {makeParams(1, 0, 10, 1, []string{}), false},
    37  		// test invalid pubkey type provided
    38  		12: {makeParams(1, 0, 10, 1, []string{"potatoes make good pubkeys"}), false},
    39  	}
    40  	for i, tc := range testCases {
    41  		if tc.valid {
    42  			assert.NoErrorf(t, tc.params.Validate(), "expected no error for valid params (#%d)", i)
    43  		} else {
    44  			assert.Errorf(t, tc.params.Validate(), "expected error for non valid params (#%d)", i)
    45  		}
    46  	}
    47  }
    48  
    49  func makeParams(
    50  	blockBytes, blockGas int64,
    51  	blockTimeIotaMs int64,
    52  	evidenceAge int64,
    53  	pubkeyTypes []string,
    54  ) ConsensusParams {
    55  	return ConsensusParams{
    56  		Block: BlockParams{
    57  			MaxBytes:   blockBytes,
    58  			MaxGas:     blockGas,
    59  			TimeIotaMs: blockTimeIotaMs,
    60  		},
    61  		Evidence: EvidenceParams{
    62  			MaxAge: evidenceAge,
    63  		},
    64  		Validator: ValidatorParams{
    65  			PubKeyTypes: pubkeyTypes,
    66  		},
    67  	}
    68  }
    69  
    70  func TestConsensusParamsHash(t *testing.T) {
    71  	params := []ConsensusParams{
    72  		makeParams(4, 2, 10, 3, valEd25519),
    73  		makeParams(1, 4, 10, 3, valEd25519),
    74  		makeParams(1, 2, 10, 4, valEd25519),
    75  		makeParams(2, 5, 10, 7, valEd25519),
    76  		makeParams(1, 7, 10, 6, valEd25519),
    77  		makeParams(9, 5, 10, 4, valEd25519),
    78  		makeParams(7, 8, 10, 9, valEd25519),
    79  		makeParams(4, 6, 10, 5, valEd25519),
    80  	}
    81  
    82  	hashes := make([][]byte, len(params))
    83  	for i := range params {
    84  		hashes[i] = params[i].Hash()
    85  	}
    86  
    87  	// make sure there are no duplicates...
    88  	// sort, then check in order for matches
    89  	sort.Slice(hashes, func(i, j int) bool {
    90  		return bytes.Compare(hashes[i], hashes[j]) < 0
    91  	})
    92  	for i := 0; i < len(hashes)-1; i++ {
    93  		assert.NotEqual(t, hashes[i], hashes[i+1])
    94  	}
    95  }
    96  
    97  func TestConsensusParamsUpdate(t *testing.T) {
    98  	testCases := []struct {
    99  		params        ConsensusParams
   100  		updates       *abci.ConsensusParams
   101  		updatedParams ConsensusParams
   102  	}{
   103  		// empty updates
   104  		{
   105  			makeParams(1, 2, 10, 3, valEd25519),
   106  			&abci.ConsensusParams{},
   107  			makeParams(1, 2, 10, 3, valEd25519),
   108  		},
   109  		// fine updates
   110  		{
   111  			makeParams(1, 2, 10, 3, valEd25519),
   112  			&abci.ConsensusParams{
   113  				Block: &abci.BlockParams{
   114  					MaxBytes: 100,
   115  					MaxGas:   200,
   116  				},
   117  				Evidence: &abci.EvidenceParams{
   118  					MaxAge: 300,
   119  				},
   120  				Validator: &abci.ValidatorParams{
   121  					PubKeyTypes: valSecp256k1,
   122  				},
   123  			},
   124  			makeParams(100, 200, 10, 300, valSecp256k1),
   125  		},
   126  	}
   127  	for _, tc := range testCases {
   128  		assert.Equal(t, tc.updatedParams, tc.params.Update(tc.updates))
   129  	}
   130  }