github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/x/evm/types/params_test.go (about) 1 package types 2 3 import ( 4 "encoding/json" 5 "strings" 6 "testing" 7 8 gojson "github.com/goccy/go-json" 9 "github.com/json-iterator/go" 10 "github.com/pquerna/ffjson/ffjson" 11 "github.com/stretchr/testify/require" 12 ) 13 14 func TestParamsValidate(t *testing.T) { 15 testCases := []struct { 16 name string 17 params Params 18 expError bool 19 }{ 20 {"default", DefaultParams(), false}, 21 { 22 "valid", 23 NewParams(true, true, false, false, defaultMaxGasLimitPerTx, 2929, 1884, 1344), 24 false, 25 }, 26 { 27 "invalid eip", 28 Params{ 29 ExtraEIPs: []int{1}, 30 }, 31 true, 32 }, 33 } 34 35 for _, tc := range testCases { 36 err := tc.params.Validate() 37 38 if tc.expError { 39 require.Error(t, err, tc.name) 40 } else { 41 require.NoError(t, err, tc.name) 42 } 43 } 44 } 45 46 func TestParamsValidatePriv(t *testing.T) { 47 require.Error(t, validateBool("")) 48 require.NoError(t, validateBool(true)) 49 require.Error(t, validateEIPs("")) 50 require.NoError(t, validateEIPs([]int{1884})) 51 require.NoError(t, validateUint64(uint64(30000000))) 52 require.Error(t, validateUint64("test")) 53 } 54 55 func TestParams_String(t *testing.T) { 56 const expectedParamsStr = `enable_create: false 57 enable_call: false 58 extra_eips: [] 59 enable_contract_deployment_whitelist: false 60 enable_contract_blocked_list: false 61 max_gas_limit_per_tx: 30000000 62 ` 63 require.True(t, strings.EqualFold(expectedParamsStr, DefaultParams().String())) 64 } 65 66 func BenchmarkParamsUnmarshal(b *testing.B) { 67 s := `{"enable_create":true,"enable_call":false,"extra_eips":[1,1,1,1],"enable_contract_deployment_whitelist":true,"enable_contract_blocked_list":true,"max_gas_limit_per_tx":100}` 68 bz := []byte(s) 69 b.ResetTimer() 70 b.Run("json", func(b *testing.B) { 71 b.ResetTimer() 72 b.ReportAllocs() 73 for n := 0; n < b.N; n++ { 74 var params Params 75 _ = json.Unmarshal(bz, ¶ms) 76 } 77 }) 78 79 b.Run("jsoniter", func(b *testing.B) { 80 b.ResetTimer() 81 b.ReportAllocs() 82 for n := 0; n < b.N; n++ { 83 var params Params 84 _ = jsoniter.Unmarshal(bz, ¶ms) 85 } 86 }) 87 b.Run("ffjson", func(b *testing.B) { 88 b.ResetTimer() 89 b.ReportAllocs() 90 for n := 0; n < b.N; n++ { 91 var params Params 92 _ = ffjson.Unmarshal(bz, ¶ms) 93 } 94 }) 95 b.Run("go-json", func(b *testing.B) { 96 b.ResetTimer() 97 b.ReportAllocs() 98 for n := 0; n < b.N; n++ { 99 var params Params 100 _ = gojson.Unmarshal(bz, ¶ms) 101 } 102 }) 103 } 104 105 func BenchmarkParamsMarshal(b *testing.B) { 106 params := NewParams(true, false, true, true, 100) 107 b.ResetTimer() 108 b.Run("json", func(b *testing.B) { 109 b.ResetTimer() 110 b.ReportAllocs() 111 for n := 0; n < b.N; n++ { 112 _, _ = json.Marshal(¶ms) 113 114 } 115 }) 116 117 b.Run("jsoniter", func(b *testing.B) { 118 b.ResetTimer() 119 b.ReportAllocs() 120 for n := 0; n < b.N; n++ { 121 _, _ = jsoniter.Marshal(¶ms) 122 123 } 124 }) 125 126 b.Run("ffjson", func(b *testing.B) { 127 b.ResetTimer() 128 b.ReportAllocs() 129 for n := 0; n < b.N; n++ { 130 _, _ = ffjson.Marshal(¶ms) 131 } 132 }) 133 134 b.Run("go-json", func(b *testing.B) { 135 b.ResetTimer() 136 b.ReportAllocs() 137 for n := 0; n < b.N; n++ { 138 _, _ = gojson.Marshal(¶ms) 139 } 140 }) 141 //NOTE: fastjson is just a parser, it does not provide "Marshal" method. 142 }