diff --git a/jsonrpc/eth_blockchain_test.go b/jsonrpc/eth_blockchain_test.go index 4beb1fd27a..eb5e88ef29 100644 --- a/jsonrpc/eth_blockchain_test.go +++ b/jsonrpc/eth_blockchain_test.go @@ -2,12 +2,10 @@ package jsonrpc import ( "errors" - "fmt" "math/big" "testing" "github.com/0xPolygon/polygon-edge/blockchain" - "github.com/0xPolygon/polygon-edge/helper/hex" "github.com/0xPolygon/polygon-edge/helper/progress" "github.com/0xPolygon/polygon-edge/state/runtime" "github.com/0xPolygon/polygon-edge/types" @@ -232,9 +230,9 @@ func TestEth_Syncing(t *testing.T) { //nolint:forcetypeassert response := res.(progression) assert.NotEqual(t, progress.ChainSyncBulk, response.Type) - assert.Equal(t, fmt.Sprintf("0x%x", 1), response.StartingBlock) - assert.Equal(t, fmt.Sprintf("0x%x", 10), response.CurrentBlock) - assert.Equal(t, fmt.Sprintf("0x%x", 100), response.HighestBlock) + assert.Equal(t, argUint64(1), response.StartingBlock) + assert.Equal(t, argUint64(10), response.CurrentBlock) + assert.Equal(t, argUint64(100), response.HighestBlock) }) t.Run("returns \"false\" if sync is not progress", func(t *testing.T) { @@ -261,7 +259,7 @@ func TestEth_GetPrice_PriceLimitSet(t *testing.T) { assert.NoError(t, err) assert.NotNil(t, res) - assert.Equal(t, hex.EncodeUint64(priceLimit), res) + assert.Equal(t, argUint64(priceLimit), res) }) t.Run("returns average gas price when it is larger than set price limit flag", func(t *testing.T) { @@ -270,7 +268,7 @@ func TestEth_GetPrice_PriceLimitSet(t *testing.T) { assert.NoError(t, err) assert.NotNil(t, res) - assert.GreaterOrEqual(t, res, hex.EncodeUint64(priceLimit)) + assert.GreaterOrEqual(t, res, argUint64(priceLimit)) }) } @@ -283,7 +281,7 @@ func TestEth_GasPrice(t *testing.T) { assert.NoError(t, err) assert.NotNil(t, res) - assert.Equal(t, fmt.Sprintf("0x%x", store.averageGasPrice), res) + assert.Equal(t, argUint64(store.averageGasPrice), res) } func TestEth_Call(t *testing.T) { diff --git a/jsonrpc/eth_endpoint.go b/jsonrpc/eth_endpoint.go index 791e4be471..916bbe99f7 100644 --- a/jsonrpc/eth_endpoint.go +++ b/jsonrpc/eth_endpoint.go @@ -10,7 +10,6 @@ import ( "github.com/0xPolygon/polygon-edge/chain" "github.com/0xPolygon/polygon-edge/helper/common" - "github.com/0xPolygon/polygon-edge/helper/hex" "github.com/0xPolygon/polygon-edge/helper/progress" "github.com/0xPolygon/polygon-edge/state" "github.com/0xPolygon/polygon-edge/state/runtime" @@ -119,9 +118,9 @@ func (e *Eth) Syncing() (interface{}, error) { // Node is bulk syncing, return the status return progression{ Type: string(syncProgression.SyncType), - StartingBlock: hex.EncodeUint64(syncProgression.StartingBlock), - CurrentBlock: hex.EncodeUint64(syncProgression.CurrentBlock), - HighestBlock: hex.EncodeUint64(syncProgression.HighestBlock), + StartingBlock: argUint64(syncProgression.StartingBlock), + CurrentBlock: argUint64(syncProgression.CurrentBlock), + HighestBlock: argUint64(syncProgression.HighestBlock), }, nil } @@ -201,12 +200,7 @@ func (e *Eth) BlockNumber() (interface{}, error) { } // SendRawTransaction sends a raw transaction -func (e *Eth) SendRawTransaction(input string) (interface{}, error) { - buf, decodeErr := hex.DecodeHex(input) - if decodeErr != nil { - return nil, fmt.Errorf("unable to decode input, %w", decodeErr) - } - +func (e *Eth) SendRawTransaction(buf argBytes) (interface{}, error) { tx := &types.Transaction{} if err := tx.UnmarshalRLP(buf); err != nil { return nil, err @@ -433,12 +427,12 @@ func (e *Eth) GetStorageAt( // GasPrice returns the average gas price based on the last x blocks // taking into consideration operator defined price limit -func (e *Eth) GasPrice() (string, error) { +func (e *Eth) GasPrice() (interface{}, error) { // Fetch average gas price in uint64 avgGasPrice := e.store.GetAvgGasPrice().Uint64() // Return --price-limit flag defined value if it is greater than avgGasPrice - return hex.EncodeUint64(common.Max(e.priceLimit, avgGasPrice)), nil + return argUint64(common.Max(e.priceLimit, avgGasPrice)), nil } // Call executes a smart contract call using the transaction object data @@ -674,7 +668,7 @@ func (e *Eth) EstimateGas(arg *txnArgs, rawNum *BlockNumber) (interface{}, error ) } - return hex.EncodeUint64(highEnd), nil + return argUint64(highEnd), nil } // GetFilterLogs returns an array of logs for the specified filter diff --git a/jsonrpc/eth_state_test.go b/jsonrpc/eth_state_test.go index 6de758165b..0714005c8e 100644 --- a/jsonrpc/eth_state_test.go +++ b/jsonrpc/eth_state_test.go @@ -700,7 +700,7 @@ func TestEth_EstimateGas_GasLimit(t *testing.T) { assert.NoError(t, estimateErr) // Make sure the estimate is correct - assert.Equal(t, fmt.Sprintf("0x%x", testCase.intrinsicGasCost), estimate) + assert.Equal(t, argUint64(testCase.intrinsicGasCost), estimate) } }) } diff --git a/jsonrpc/eth_txpool_test.go b/jsonrpc/eth_txpool_test.go index 4a134c0658..9b07dd1410 100644 --- a/jsonrpc/eth_txpool_test.go +++ b/jsonrpc/eth_txpool_test.go @@ -4,7 +4,6 @@ import ( "math/big" "testing" - "github.com/0xPolygon/polygon-edge/helper/hex" "github.com/0xPolygon/polygon-edge/state" "github.com/0xPolygon/polygon-edge/types" "github.com/stretchr/testify/assert" @@ -21,7 +20,7 @@ func TestEth_TxnPool_SendRawTransaction(t *testing.T) { txn.ComputeHash() data := txn.MarshalRLP() - _, err := eth.SendRawTransaction(hex.EncodeToHex(data)) + _, err := eth.SendRawTransaction(data) assert.NoError(t, err) assert.NotEqual(t, store.txn.Hash, types.ZeroHash) @@ -43,7 +42,7 @@ func TestEth_TxnPool_SendTransaction(t *testing.T) { GasPrice: big.NewInt(int64(1)), } - _, err := eth.SendRawTransaction(hex.EncodeToHex(txToSend.MarshalRLP())) + _, err := eth.SendRawTransaction(txToSend.MarshalRLP()) assert.NoError(t, err) assert.NotEqual(t, store.txn.Hash, types.ZeroHash) } diff --git a/jsonrpc/mocks_test.go b/jsonrpc/mocks_test.go index 704b197449..757f43eb09 100644 --- a/jsonrpc/mocks_test.go +++ b/jsonrpc/mocks_test.go @@ -133,3 +133,7 @@ func (m *mockStore) GetTxs(inclQueued bool) ( func (m *mockStore) GetCapacity() (uint64, uint64) { return 0, 0 } + +func (m *mockStore) GetPeers() int { + return 20 +} diff --git a/jsonrpc/net_endpoint.go b/jsonrpc/net_endpoint.go index 84b878351d..7a43bcbb0e 100644 --- a/jsonrpc/net_endpoint.go +++ b/jsonrpc/net_endpoint.go @@ -27,5 +27,5 @@ func (n *Net) Listening() (interface{}, error) { func (n *Net) PeerCount() (interface{}, error) { peers := n.store.GetPeers() - return strconv.FormatInt(int64(peers), 10), nil + return argUint64(peers), nil } diff --git a/jsonrpc/net_endpoint_test.go b/jsonrpc/net_endpoint_test.go new file mode 100644 index 0000000000..d03d32c856 --- /dev/null +++ b/jsonrpc/net_endpoint_test.go @@ -0,0 +1,28 @@ +package jsonrpc + +import ( + "testing" + + "github.com/hashicorp/go-hclog" + "github.com/stretchr/testify/assert" +) + +func TestNetEndpoint_PeerCount(t *testing.T) { + dispatcher := newDispatcher( + hclog.NewNullLogger(), + newMockStore(), + &dispatcherParams{ + chainID: 1, + }) + + resp, err := dispatcher.Handle([]byte(`{ + "method": "net_peerCount", + "params": [""] + }`)) + assert.NoError(t, err) + + var res string + + assert.NoError(t, expectJSONResult(resp, &res)) + assert.Equal(t, "0x14", res) +} diff --git a/jsonrpc/types.go b/jsonrpc/types.go index 9755c61d6e..3270be92ca 100644 --- a/jsonrpc/types.go +++ b/jsonrpc/types.go @@ -305,8 +305,8 @@ type txnArgs struct { } type progression struct { - Type string `json:"type"` - StartingBlock string `json:"startingBlock"` - CurrentBlock string `json:"currentBlock"` - HighestBlock string `json:"highestBlock"` + Type string `json:"type"` + StartingBlock argUint64 `json:"startingBlock"` + CurrentBlock argUint64 `json:"currentBlock"` + HighestBlock argUint64 `json:"highestBlock"` } diff --git a/jsonrpc/web3_endpoint.go b/jsonrpc/web3_endpoint.go index 5ebcf9e5f1..b2ee9792aa 100644 --- a/jsonrpc/web3_endpoint.go +++ b/jsonrpc/web3_endpoint.go @@ -3,7 +3,6 @@ package jsonrpc import ( "fmt" - "github.com/0xPolygon/polygon-edge/helper/hex" "github.com/0xPolygon/polygon-edge/helper/keccak" "github.com/0xPolygon/polygon-edge/versioning" ) @@ -27,13 +26,8 @@ func (w *Web3) ClientVersion() (interface{}, error) { } // Sha3 returns Keccak-256 (not the standardized SHA3-256) of the given data -func (w *Web3) Sha3(val string) (interface{}, error) { - v, err := hex.DecodeHex(val) - if err != nil { - return nil, NewInvalidRequestError("Invalid hex string") - } - +func (w *Web3) Sha3(v argBytes) (interface{}, error) { dst := keccak.Keccak256(nil, v) - return hex.EncodeToHex(dst), nil + return argBytes(dst), nil }