Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Uniform jsonrpc type return #873

Merged
merged 4 commits into from
Nov 8, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 7 additions & 13 deletions jsonrpc/eth_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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) {
ferranbt marked this conversation as resolved.
Show resolved Hide resolved
// 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
Expand Down Expand Up @@ -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
Expand Down
5 changes: 2 additions & 3 deletions jsonrpc/eth_txpool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)

Expand All @@ -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)
}
Expand Down
4 changes: 4 additions & 0 deletions jsonrpc/mocks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion jsonrpc/net_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
28 changes: 28 additions & 0 deletions jsonrpc/net_endpoint_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
8 changes: 4 additions & 4 deletions jsonrpc/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
10 changes: 2 additions & 8 deletions jsonrpc/web3_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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
}