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