github.com/iotexproject/iotex-core@v1.14.1-rc1/api/web3server_utils_test.go (about) 1 package api 2 3 import ( 4 "math/big" 5 "testing" 6 7 "github.com/golang/mock/gomock" 8 "github.com/iotexproject/iotex-address/address" 9 "github.com/iotexproject/iotex-core/test/mock/mock_apicoreservice" 10 "github.com/stretchr/testify/require" 11 "github.com/tidwall/gjson" 12 ) 13 14 func TestParseCallObject(t *testing.T) { 15 require := require.New(t) 16 17 testData := []struct { 18 name string 19 input string 20 21 from string 22 to string 23 gasLimit uint64 24 gasPrice *big.Int 25 value *big.Int 26 data []byte 27 err error 28 }{ 29 { 30 name: "legacy", 31 input: `{"params":[{ 32 "from": "", 33 "to": "0x7c13866F9253DEf79e20034eDD011e1d69E67fe5", 34 "gas": "0x4e20", 35 "gasPrice": "0xe8d4a51000", 36 "value": "0x1", 37 "data": "0x6d4ce63c" 38 }, 39 1]}`, 40 from: address.ZeroAddress, 41 to: "io10sfcvmuj2000083qqd8d6qg7r457vll9gly090", 42 gasLimit: 20000, 43 gasPrice: new(big.Int).SetInt64(1000000000000), 44 value: new(big.Int).SetInt64(1), 45 data: []byte{0x6d, 0x4c, 0xe6, 0x3c}, 46 }, 47 { 48 name: "input instead of data", 49 input: `{"params":[{ 50 "from": "", 51 "to": "0x7c13866F9253DEf79e20034eDD011e1d69E67fe5", 52 "gas": "0x4e20", 53 "gasPrice": "0xe8d4a51000", 54 "value": "0x1", 55 "input": "0x6d4ce63c" 56 }, 57 1]}`, 58 from: address.ZeroAddress, 59 to: "io10sfcvmuj2000083qqd8d6qg7r457vll9gly090", 60 gasLimit: 20000, 61 gasPrice: new(big.Int).SetInt64(1000000000000), 62 value: new(big.Int).SetInt64(1), 63 data: []byte{0x6d, 0x4c, 0xe6, 0x3c}, 64 }, 65 } 66 67 for _, test := range testData { 68 t.Run(test.name, func(t *testing.T) { 69 in := gjson.Parse(test.input) 70 from, to, gasLimit, gasPrice, value, data, err := parseCallObject(&in) 71 require.Equal(test.from, from.String()) 72 require.Equal(test.to, to) 73 require.Equal(test.gasLimit, gasLimit) 74 require.Equal(test.gasPrice, gasPrice) 75 require.Equal(test.value, value) 76 require.Equal(test.data, data) 77 require.Equal(test.err, err) 78 }) 79 } 80 81 t.Run("parse gas price", func(t *testing.T) { 82 input := `{"params":[{ 83 "from": "", 84 "to": "0x7c13866F9253DEf79e20034eDD011e1d69E67fe5", 85 "gas": "0x4e20", 86 "gasPrice": "unknown", 87 "value": "0x1", 88 "input": "0x6d4ce63c" 89 }, 90 1]}` 91 in := gjson.Parse(input) 92 _, _, _, _, _, _, err := parseCallObject(&in) 93 require.EqualError(err, "gasPrice: unknown: wrong type of params") 94 }) 95 96 t.Run("parse value", func(t *testing.T) { 97 input := `{"params":[{ 98 "from": "", 99 "to": "0x7c13866F9253DEf79e20034eDD011e1d69E67fe5", 100 "gas": "0x4e20", 101 "gasPrice": "0xe8d4a51000", 102 "value": "unknown", 103 "input": "0x6d4ce63c" 104 }, 105 1]}` 106 in := gjson.Parse(input) 107 _, _, _, _, _, _, err := parseCallObject(&in) 108 require.EqualError(err, "value: unknown: wrong type of params") 109 }) 110 111 } 112 113 func TestParseBlockNumber(t *testing.T) { 114 require := require.New(t) 115 ctrl := gomock.NewController(t) 116 defer ctrl.Finish() 117 core := mock_apicoreservice.NewMockCoreService(ctrl) 118 web3svr := &web3Handler{core, nil, _defaultBatchRequestLimit} 119 120 t.Run("earliest block number", func(t *testing.T) { 121 num, _ := web3svr.parseBlockNumber("earliest") 122 require.Equal(num, uint64(0x1)) 123 }) 124 125 t.Run("pending block number", func(t *testing.T) { 126 core.EXPECT().TipHeight().Return(uint64(0x1)) 127 num, _ := web3svr.parseBlockNumber("pending") 128 require.Equal(num, uint64(0x1)) 129 }) 130 131 t.Run("latest block number", func(t *testing.T) { 132 core.EXPECT().TipHeight().Return(uint64(0x1)) 133 num, _ := web3svr.parseBlockNumber("latest") 134 require.Equal(num, uint64(0x1)) 135 }) 136 137 t.Run("nil block number", func(t *testing.T) { 138 core.EXPECT().TipHeight().Return(uint64(0x1)) 139 num, _ := web3svr.parseBlockNumber("") 140 require.Equal(num, uint64(0x1)) 141 }) 142 }