Skip to content

Commit

Permalink
Uniform jsonrpc type return (0xPolygon#873)
Browse files Browse the repository at this point in the history
* Use uniform type return in jsonrpc

* Revert

* Fix error convetion

* Fix tests
  • Loading branch information
ferranbt authored Nov 8, 2022
1 parent b774553 commit 8e732ad
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 38 deletions.
14 changes: 6 additions & 8 deletions jsonrpc/eth_blockchain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -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))
})
}

Expand All @@ -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) {
Expand Down
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) {
// 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
2 changes: 1 addition & 1 deletion jsonrpc/eth_state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
})
}
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
}

0 comments on commit 8e732ad

Please sign in to comment.