Skip to content
This repository has been archived by the owner on Nov 30, 2021. It is now read-only.

Commit

Permalink
Minor fixes (#94)
Browse files Browse the repository at this point in the history
- Updates RPC return types
- Removes custom query types in favour of default eth 
    - This is largely to allow for proper hexadecimal formatting (provided by `hexutil`), as the API is very specific about formatting.
  • Loading branch information
ansermino authored Sep 24, 2019
1 parent 28aaba0 commit 1cac4fe
Show file tree
Hide file tree
Showing 6 changed files with 147 additions and 154 deletions.
39 changes: 20 additions & 19 deletions rpc/eth_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ func NewPublicEthAPI(cliCtx context.CLIContext, nonceLock *AddrLocker,
}

// ProtocolVersion returns the supported Ethereum protocol version.
func (e *PublicEthAPI) ProtocolVersion() string {
return version.ProtocolVersion
func (e *PublicEthAPI) ProtocolVersion() hexutil.Uint {
return hexutil.Uint(version.ProtocolVersion)
}

// Syncing returns whether or not the current node is syncing with other peers. Returns false if not, or a struct
Expand Down Expand Up @@ -95,16 +95,16 @@ func (e *PublicEthAPI) Accounts() ([]common.Address, error) {
}

// BlockNumber returns the current block number.
func (e *PublicEthAPI) BlockNumber() *big.Int {
func (e *PublicEthAPI) BlockNumber() (hexutil.Uint64, error) {
res, _, err := e.cliCtx.QueryWithData(fmt.Sprintf("custom/%s/blockNumber", types.ModuleName), nil)
if err != nil {
fmt.Printf("could not resolve: %s\n", err)
return nil
return hexutil.Uint64(0), err
}

var out types.QueryResBlockNumber
e.cliCtx.Codec.MustUnmarshalJSON(res, &out)
return out.Number
var qRes uint64
e.cliCtx.Codec.MustUnmarshalJSON(res, &qRes)

return hexutil.Uint64(qRes), nil
}

// GetBalance returns the provided account's balance up to the provided block number.
Expand All @@ -115,9 +115,10 @@ func (e *PublicEthAPI) GetBalance(address common.Address, blockNum rpc.BlockNumb
return nil, err
}

var out types.QueryResBalance
var out *big.Int
e.cliCtx.Codec.MustUnmarshalJSON(res, &out)
return (*hexutil.Big)(out.Balance), nil

return (*hexutil.Big)(out), nil
}

// GetStorageAt returns the contract storage at the given address, block number, and key.
Expand All @@ -128,22 +129,22 @@ func (e *PublicEthAPI) GetStorageAt(address common.Address, key string, blockNum
return nil, err
}

var out types.QueryResStorage
var out []byte
e.cliCtx.Codec.MustUnmarshalJSON(res, &out)
return out.Value[:], nil
return out, nil
}

// GetTransactionCount returns the number of transactions at the given address up to the given block number.
func (e *PublicEthAPI) GetTransactionCount(address common.Address, blockNum rpc.BlockNumber) (hexutil.Uint64, error) {
func (e *PublicEthAPI) GetTransactionCount(address common.Address, blockNum rpc.BlockNumber) (*hexutil.Uint64, error) {
ctx := e.cliCtx.WithHeight(blockNum.Int64())
res, _, err := ctx.QueryWithData(fmt.Sprintf("custom/%s/nonce/%s", types.ModuleName, address), nil)
if err != nil {
return 0, err
return nil, err
}

var out types.QueryResNonce
e.cliCtx.Codec.MustUnmarshalJSON(res, &out)
return hexutil.Uint64(out.Nonce), nil
var out *hexutil.Uint64
e.cliCtx.Codec.MustUnmarshalJSON(res, out)
return out, nil
}

// GetBlockTransactionCountByHash returns the number of transactions in the block identified by hash.
Expand Down Expand Up @@ -185,9 +186,9 @@ func (e *PublicEthAPI) GetCode(address common.Address, blockNumber rpc.BlockNumb
return nil, err
}

var out types.QueryResCode
var out []byte
e.cliCtx.Codec.MustUnmarshalJSON(res, &out)
return out.Code, nil
return out, nil
}

// Sign signs the provided data using the private key of address via Geth's signature standard.
Expand Down
148 changes: 104 additions & 44 deletions rpc/tester/tester_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,19 @@ package tester
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"github.com/cosmos/ethermint/version"
"github.com/cosmos/ethermint/x/evm/types"
"io/ioutil"
"math/big"
"net/http"
"testing"

"github.com/cosmos/ethermint/version"
"github.com/ethereum/go-ethereum/common/hexutil"
)

const (
host = "127.0.0.1"
port = 1317
host = "localhost"
port = 8545
addrA = "0xc94770007dda54cF92009BFF0dE90c06F603a09f"
addrAStoreKey = 0
)
Expand All @@ -35,6 +36,18 @@ type Request struct {
Id int `json:"id"`
}

type RPCError struct {
Code int `json:"code"`
Message string `json:"message"`
Data interface{} `json:"data,omitempty"`
}

type Response struct {
Error *RPCError `json:"error"`
Id int `json:"id"`
Result json.RawMessage `json:"result,omitempty"`
}

func createRequest(method string, params []string) Request {
return Request{
Version: "2.0",
Expand All @@ -44,86 +57,133 @@ func createRequest(method string, params []string) Request {
}
}

func call(t *testing.T, method string, params []string, resp interface{}) {
func call(method string, params []string) (*Response, error) {
req, err := json.Marshal(createRequest(method, params))
if err != nil {
t.Error(err)
return nil, err
}

res, err := http.Post(addr, "application/json", bytes.NewBuffer(req))
if err != nil {
t.Error(err)
return nil, err
}
defer res.Body.Close()

body, err := ioutil.ReadAll(res.Body)
decoder := json.NewDecoder(res.Body)
var rpcRes *Response
err = decoder.Decode(&rpcRes)
if err != nil {
t.Error(err)
return nil, err
}

if rpcRes.Error != nil {
return nil, errors.New(rpcRes.Error.Message)
}

err = json.Unmarshal(body, resp)
err = res.Body.Close()
if err != nil {
t.Error(err)
return nil, err
}

return rpcRes, nil
}

func TestEth_protocolVersion(t *testing.T) {
expectedRes := version.ProtocolVersion
expectedRes := hexutil.Uint(version.ProtocolVersion)

res := &types.QueryResProtocolVersion{}
call(t, "eth_protocolVersion", []string{}, res)
rpcRes, err := call("eth_protocolVersion", []string{})
if err != nil {
t.Fatal(err)
}

t.Logf("Got protocol version: %s\n", res.Version)
var res hexutil.Uint
err = res.UnmarshalJSON(rpcRes.Result)

if res.Version != expectedRes {
t.Errorf("expected: %s got: %s\n", expectedRes, res)
if err != nil {
t.Fatal(err)
}

t.Logf("Got protocol version: %s\n", res.String())

if res != expectedRes {
t.Fatalf("expected: %s got: %s\n", expectedRes.String(), rpcRes.Result)
}
}

func TestEth_blockNumber(t *testing.T) {
res := &types.QueryResBlockNumber{}
call(t, "eth_blockNumber", []string{}, res)

t.Logf("Got block number: %s\n", res.Number.String())
rpcRes, err := call("eth_blockNumber", []string{})
if err != nil {
t.Fatal(err)
}
var res hexutil.Uint64
err = res.UnmarshalJSON(rpcRes.Result)

// -1 if x < y, 0 if x == y; where x is res, y is 0
if res.Number.Cmp(big.NewInt(0)) < 1 {
t.Errorf("Invalid block number got: %v", res)
if err != nil {
t.Fatal(err)
}

t.Logf("Got block number: %s\n", res.String())

}

func TestEth_GetBalance(t *testing.T) {
//expectedRes := types.QueryResBalance{Balance:}
res := &types.QueryResBalance{}
call(t, "eth_getBalance", []string{addrA, "latest"}, res)
rpcRes, err := call("eth_getBalance", []string{addrA, "0x0"})
if err != nil {
t.Fatal(err)
return
}

t.Logf("Got balance %s for %s\n", res.Balance.String(), addrA)
var res hexutil.Big
err = res.UnmarshalJSON(rpcRes.Result)
if err != nil {
t.Fatal(err)
}

t.Logf("Got balance %s for %s\n", res.String(), addrA)

// 0 if x == y; where x is res, y is 0
if res.Balance.ToInt().Cmp(big.NewInt(0)) != 0 {
t.Errorf("expected balance: %d, got: %s", 0, res.Balance.String())
if res.ToInt().Cmp(big.NewInt(0)) != 0 {
t.Errorf("expected balance: %d, got: %s", 0, res.String())
}

}

func TestEth_GetStorageAt(t *testing.T) {
expectedRes := types.QueryResStorage{Value: []byte{}}
res := &types.QueryResStorage{}
call(t, "eth_getStorageAt", []string{addrA, string(addrAStoreKey), "latest"}, res)
expectedRes := hexutil.Bytes{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
rpcRes, err := call("eth_getStorageAt", []string{addrA, string(addrAStoreKey), "0x0"})
if err != nil {
t.Fatal(err)
}

t.Logf("Got value [%X] for %s with key %X\n", res.Value, addrA, addrAStoreKey)
var storage hexutil.Bytes
err = storage.UnmarshalJSON(rpcRes.Result)

if !bytes.Equal(res.Value, expectedRes.Value) {
t.Errorf("expected: %X got: %X", expectedRes.Value, res.Value)
if err != nil {
t.Fatal(err)
}

t.Logf("Got value [%X] for %s with key %X\n", storage, addrA, addrAStoreKey)

if !bytes.Equal(storage, expectedRes) {
t.Errorf("expected: %d (%d bytes) got: %d (%d bytes)", expectedRes, len(expectedRes), storage, len(storage))
}
}

func TestEth_GetCode(t *testing.T) {
expectedRes := types.QueryResCode{Code: []byte{}}
res := &types.QueryResCode{}
call(t, "eth_getCode", []string{addrA, "latest"}, res)
expectedRes := hexutil.Bytes{}
rpcRes, err := call("eth_getCode", []string{addrA, "0x0"})
if err != nil {
t.Error(err)
}

var code hexutil.Bytes
err = code.UnmarshalJSON(rpcRes.Result)

if err != nil {
t.Fatal(err)
}

t.Logf("Got code [%X] for %s\n", res.Code, addrA)
if !bytes.Equal(expectedRes.Code, res.Code) {
t.Errorf("expected: %X got: %X", expectedRes.Code, res.Code)
t.Logf("Got code [%X] for %s\n", code, addrA)
if !bytes.Equal(expectedRes, code) {
t.Errorf("expected: %X got: %X", expectedRes, code)
}
}
2 changes: 1 addition & 1 deletion version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const AppName = "Ethermint"
const Version = "0.0.0"

// ProtocolVersion is the supported Ethereum protocol version (e.g., Homestead, Olympic, etc.)
const ProtocolVersion = "63"
const ProtocolVersion uint = 63

// GitCommit contains the git SHA1 short hash set by build flags.
var GitCommit = ""
Expand Down
11 changes: 6 additions & 5 deletions x/evm/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/ethermint/x/evm/types"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -42,7 +43,7 @@ func GetCmdGetBlockNumber(queryRoute string, cdc *codec.Codec) *cobra.Command {
return nil
}

var out types.QueryResBlockNumber
var out *hexutil.Big
cdc.MustUnmarshalJSON(res, &out)
return cliCtx.PrintOutput(out)
},
Expand All @@ -67,7 +68,7 @@ func GetCmdGetStorageAt(queryRoute string, cdc *codec.Codec) *cobra.Command {
fmt.Printf("could not resolve: %s\n", err)
return nil
}
var out types.QueryResStorage
var out hexutil.Bytes
cdc.MustUnmarshalJSON(res, &out)
return cliCtx.PrintOutput(out)
},
Expand All @@ -91,9 +92,9 @@ func GetCmdGetCode(queryRoute string, cdc *codec.Codec) *cobra.Command {
fmt.Printf("could not resolve: %s\n", err)
return nil
}
var out types.QueryResCode
var out []byte
cdc.MustUnmarshalJSON(res, &out)
return cliCtx.PrintOutput(out)
return cliCtx.PrintOutput(hexutil.Bytes(out))
},
}
}
Expand All @@ -115,7 +116,7 @@ func GetCmdGetNonce(queryRoute string, cdc *codec.Codec) *cobra.Command {
fmt.Printf("could not resolve: %s\n", err)
return nil
}
var out types.QueryResNonce
var out hexutil.Uint64
cdc.MustUnmarshalJSON(res, &out)
return cliCtx.PrintOutput(out)
},
Expand Down
Loading

0 comments on commit 1cac4fe

Please sign in to comment.