Minor fixes (#94)

- 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.
This commit is contained in:
David Ansermino 2019-09-24 16:49:40 +02:00 committed by GitHub
parent 28aaba0695
commit 1cac4feb4d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 151 additions and 158 deletions

View File

@ -42,8 +42,8 @@ func NewPublicEthAPI(cliCtx context.CLIContext, nonceLock *AddrLocker,
} }
// ProtocolVersion returns the supported Ethereum protocol version. // ProtocolVersion returns the supported Ethereum protocol version.
func (e *PublicEthAPI) ProtocolVersion() string { func (e *PublicEthAPI) ProtocolVersion() hexutil.Uint {
return version.ProtocolVersion 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 // Syncing returns whether or not the current node is syncing with other peers. Returns false if not, or a struct
@ -95,16 +95,16 @@ func (e *PublicEthAPI) Accounts() ([]common.Address, error) {
} }
// BlockNumber returns the current block number. // 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) res, _, err := e.cliCtx.QueryWithData(fmt.Sprintf("custom/%s/blockNumber", types.ModuleName), nil)
if err != nil { if err != nil {
fmt.Printf("could not resolve: %s\n", err) return hexutil.Uint64(0), err
return nil
} }
var out types.QueryResBlockNumber var qRes uint64
e.cliCtx.Codec.MustUnmarshalJSON(res, &out) e.cliCtx.Codec.MustUnmarshalJSON(res, &qRes)
return out.Number
return hexutil.Uint64(qRes), nil
} }
// GetBalance returns the provided account's balance up to the provided block number. // GetBalance returns the provided account's balance up to the provided block number.
@ -115,9 +115,10 @@ func (e *PublicEthAPI) GetBalance(address common.Address, blockNum rpc.BlockNumb
return nil, err return nil, err
} }
var out types.QueryResBalance var out *big.Int
e.cliCtx.Codec.MustUnmarshalJSON(res, &out) 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. // GetStorageAt returns the contract storage at the given address, block number, and key.
@ -128,22 +129,22 @@ func (e *PublicEthAPI) GetStorageAt(address common.Address, key string, blockNum
return nil, err return nil, err
} }
var out types.QueryResStorage var out []byte
e.cliCtx.Codec.MustUnmarshalJSON(res, &out) 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. // 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()) ctx := e.cliCtx.WithHeight(blockNum.Int64())
res, _, err := ctx.QueryWithData(fmt.Sprintf("custom/%s/nonce/%s", types.ModuleName, address), nil) res, _, err := ctx.QueryWithData(fmt.Sprintf("custom/%s/nonce/%s", types.ModuleName, address), nil)
if err != nil { if err != nil {
return 0, err return nil, err
} }
var out types.QueryResNonce var out *hexutil.Uint64
e.cliCtx.Codec.MustUnmarshalJSON(res, &out) e.cliCtx.Codec.MustUnmarshalJSON(res, out)
return hexutil.Uint64(out.Nonce), nil return out, nil
} }
// GetBlockTransactionCountByHash returns the number of transactions in the block identified by hash. // GetBlockTransactionCountByHash returns the number of transactions in the block identified by hash.
@ -185,9 +186,9 @@ func (e *PublicEthAPI) GetCode(address common.Address, blockNumber rpc.BlockNumb
return nil, err return nil, err
} }
var out types.QueryResCode var out []byte
e.cliCtx.Codec.MustUnmarshalJSON(res, &out) 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. // Sign signs the provided data using the private key of address via Geth's signature standard.

View File

@ -10,18 +10,19 @@ package tester
import ( import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"errors"
"fmt" "fmt"
"github.com/cosmos/ethermint/version"
"github.com/cosmos/ethermint/x/evm/types"
"io/ioutil"
"math/big" "math/big"
"net/http" "net/http"
"testing" "testing"
"github.com/cosmos/ethermint/version"
"github.com/ethereum/go-ethereum/common/hexutil"
) )
const ( const (
host = "127.0.0.1" host = "localhost"
port = 1317 port = 8545
addrA = "0xc94770007dda54cF92009BFF0dE90c06F603a09f" addrA = "0xc94770007dda54cF92009BFF0dE90c06F603a09f"
addrAStoreKey = 0 addrAStoreKey = 0
) )
@ -35,6 +36,18 @@ type Request struct {
Id int `json:"id"` 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 { func createRequest(method string, params []string) Request {
return Request{ return Request{
Version: "2.0", Version: "2.0",
@ -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)) req, err := json.Marshal(createRequest(method, params))
if err != nil { if err != nil {
t.Error(err) return nil, err
} }
res, err := http.Post(addr, "application/json", bytes.NewBuffer(req)) res, err := http.Post(addr, "application/json", bytes.NewBuffer(req))
if err != nil { if err != nil {
t.Error(err) return nil, err
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
t.Error(err)
} }
err = json.Unmarshal(body, resp) decoder := json.NewDecoder(res.Body)
var rpcRes *Response
err = decoder.Decode(&rpcRes)
if err != nil { if err != nil {
t.Error(err) return nil, err
} }
if rpcRes.Error != nil {
return nil, errors.New(rpcRes.Error.Message)
}
err = res.Body.Close()
if err != nil {
return nil, err
}
return rpcRes, nil
} }
func TestEth_protocolVersion(t *testing.T) { func TestEth_protocolVersion(t *testing.T) {
expectedRes := version.ProtocolVersion expectedRes := hexutil.Uint(version.ProtocolVersion)
res := &types.QueryResProtocolVersion{} rpcRes, err := call("eth_protocolVersion", []string{})
call(t, "eth_protocolVersion", []string{}, res) 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 { if err != nil {
t.Errorf("expected: %s got: %s\n", expectedRes, res) 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) { func TestEth_blockNumber(t *testing.T) {
res := &types.QueryResBlockNumber{} rpcRes, err := call("eth_blockNumber", []string{})
call(t, "eth_blockNumber", []string{}, res) if err != nil {
t.Fatal(err)
t.Logf("Got block number: %s\n", res.Number.String())
// -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)
} }
var res hexutil.Uint64
err = res.UnmarshalJSON(rpcRes.Result)
if err != nil {
t.Fatal(err)
}
t.Logf("Got block number: %s\n", res.String())
} }
func TestEth_GetBalance(t *testing.T) { func TestEth_GetBalance(t *testing.T) {
//expectedRes := types.QueryResBalance{Balance:} rpcRes, err := call("eth_getBalance", []string{addrA, "0x0"})
res := &types.QueryResBalance{} if err != nil {
call(t, "eth_getBalance", []string{addrA, "latest"}, res) 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 // 0 if x == y; where x is res, y is 0
if res.Balance.ToInt().Cmp(big.NewInt(0)) != 0 { if res.ToInt().Cmp(big.NewInt(0)) != 0 {
t.Errorf("expected balance: %d, got: %s", 0, res.Balance.String()) t.Errorf("expected balance: %d, got: %s", 0, res.String())
} }
} }
func TestEth_GetStorageAt(t *testing.T) { func TestEth_GetStorageAt(t *testing.T) {
expectedRes := types.QueryResStorage{Value: []byte{}} 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}
res := &types.QueryResStorage{} rpcRes, err := call("eth_getStorageAt", []string{addrA, string(addrAStoreKey), "0x0"})
call(t, "eth_getStorageAt", []string{addrA, string(addrAStoreKey), "latest"}, res) 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) { if err != nil {
t.Errorf("expected: %X got: %X", expectedRes.Value, res.Value) 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) { func TestEth_GetCode(t *testing.T) {
expectedRes := types.QueryResCode{Code: []byte{}} expectedRes := hexutil.Bytes{}
res := &types.QueryResCode{} rpcRes, err := call("eth_getCode", []string{addrA, "0x0"})
call(t, "eth_getCode", []string{addrA, "latest"}, res) if err != nil {
t.Error(err)
}
t.Logf("Got code [%X] for %s\n", res.Code, addrA) var code hexutil.Bytes
if !bytes.Equal(expectedRes.Code, res.Code) { err = code.UnmarshalJSON(rpcRes.Result)
t.Errorf("expected: %X got: %X", expectedRes.Code, res.Code)
if err != nil {
t.Fatal(err)
}
t.Logf("Got code [%X] for %s\n", code, addrA)
if !bytes.Equal(expectedRes, code) {
t.Errorf("expected: %X got: %X", expectedRes, code)
} }
} }

View File

@ -15,7 +15,7 @@ const AppName = "Ethermint"
const Version = "0.0.0" const Version = "0.0.0"
// ProtocolVersion is the supported Ethereum protocol version (e.g., Homestead, Olympic, etc.) // 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. // GitCommit contains the git SHA1 short hash set by build flags.
var GitCommit = "" var GitCommit = ""

View File

@ -7,6 +7,7 @@ import (
"github.com/cosmos/cosmos-sdk/client/context" "github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/codec" "github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/ethermint/x/evm/types" "github.com/cosmos/ethermint/x/evm/types"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@ -42,7 +43,7 @@ func GetCmdGetBlockNumber(queryRoute string, cdc *codec.Codec) *cobra.Command {
return nil return nil
} }
var out types.QueryResBlockNumber var out *hexutil.Big
cdc.MustUnmarshalJSON(res, &out) cdc.MustUnmarshalJSON(res, &out)
return cliCtx.PrintOutput(out) return cliCtx.PrintOutput(out)
}, },
@ -67,7 +68,7 @@ func GetCmdGetStorageAt(queryRoute string, cdc *codec.Codec) *cobra.Command {
fmt.Printf("could not resolve: %s\n", err) fmt.Printf("could not resolve: %s\n", err)
return nil return nil
} }
var out types.QueryResStorage var out hexutil.Bytes
cdc.MustUnmarshalJSON(res, &out) cdc.MustUnmarshalJSON(res, &out)
return cliCtx.PrintOutput(out) return cliCtx.PrintOutput(out)
}, },
@ -91,9 +92,9 @@ func GetCmdGetCode(queryRoute string, cdc *codec.Codec) *cobra.Command {
fmt.Printf("could not resolve: %s\n", err) fmt.Printf("could not resolve: %s\n", err)
return nil return nil
} }
var out types.QueryResCode var out []byte
cdc.MustUnmarshalJSON(res, &out) cdc.MustUnmarshalJSON(res, &out)
return cliCtx.PrintOutput(out) return cliCtx.PrintOutput(hexutil.Bytes(out))
}, },
} }
} }
@ -115,7 +116,7 @@ func GetCmdGetNonce(queryRoute string, cdc *codec.Codec) *cobra.Command {
fmt.Printf("could not resolve: %s\n", err) fmt.Printf("could not resolve: %s\n", err)
return nil return nil
} }
var out types.QueryResNonce var out hexutil.Uint64
cdc.MustUnmarshalJSON(res, &out) cdc.MustUnmarshalJSON(res, &out)
return cliCtx.PrintOutput(out) return cliCtx.PrintOutput(out)
}, },

View File

@ -1,12 +1,9 @@
package evm package evm
import ( import (
"math/big"
"github.com/cosmos/cosmos-sdk/codec" "github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types" sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/ethermint/version" "github.com/cosmos/ethermint/version"
"github.com/cosmos/ethermint/x/evm/types"
ethcmn "github.com/ethereum/go-ethereum/common" ethcmn "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil" "github.com/ethereum/go-ethereum/common/hexutil"
abci "github.com/tendermint/tendermint/abci/types" abci "github.com/tendermint/tendermint/abci/types"
@ -47,7 +44,8 @@ func NewQuerier(keeper Keeper) sdk.Querier {
func queryProtocolVersion(keeper Keeper) ([]byte, sdk.Error) { func queryProtocolVersion(keeper Keeper) ([]byte, sdk.Error) {
vers := version.ProtocolVersion vers := version.ProtocolVersion
res, err := codec.MarshalJSONIndent(keeper.cdc, vers) bigRes := hexutil.Uint(vers)
res, err := codec.MarshalJSONIndent(keeper.cdc, bigRes)
if err != nil { if err != nil {
panic("could not marshal result to JSON") panic("could not marshal result to JSON")
} }
@ -58,16 +56,9 @@ func queryProtocolVersion(keeper Keeper) ([]byte, sdk.Error) {
func queryBalance(ctx sdk.Context, path []string, keeper Keeper) ([]byte, sdk.Error) { func queryBalance(ctx sdk.Context, path []string, keeper Keeper) ([]byte, sdk.Error) {
addr := ethcmn.BytesToAddress([]byte(path[1])) addr := ethcmn.BytesToAddress([]byte(path[1]))
balance := keeper.GetBalance(ctx, addr) balance := keeper.GetBalance(ctx, addr)
hBalance := &hexutil.Big{} res, err := codec.MarshalJSONIndent(keeper.cdc, balance)
err := hBalance.UnmarshalText(balance.Bytes())
if err != nil { if err != nil {
panic("could not marshal big.Int to hexutil.Big") panic("could not marshal result to JSON: ")
}
bRes := types.QueryResBalance{Balance: hBalance}
res, err := codec.MarshalJSONIndent(keeper.cdc, bRes)
if err != nil {
panic("could not marshal result to JSON")
} }
return res, nil return res, nil
@ -75,10 +66,12 @@ func queryBalance(ctx sdk.Context, path []string, keeper Keeper) ([]byte, sdk.Er
func queryBlockNumber(ctx sdk.Context, keeper Keeper) ([]byte, sdk.Error) { func queryBlockNumber(ctx sdk.Context, keeper Keeper) ([]byte, sdk.Error) {
num := ctx.BlockHeight() num := ctx.BlockHeight()
bnRes := types.QueryResBlockNumber{Number: big.NewInt(num)} hexUint := hexutil.Uint64(num)
res, err := codec.MarshalJSONIndent(keeper.cdc, bnRes)
res, err := codec.MarshalJSONIndent(keeper.cdc, hexUint)
if err != nil { if err != nil {
panic("could not marshal result to JSON") panic("could not marshal result to JSON: " + err.Error())
} }
return res, nil return res, nil
@ -88,10 +81,10 @@ func queryStorage(ctx sdk.Context, path []string, keeper Keeper) ([]byte, sdk.Er
addr := ethcmn.BytesToAddress([]byte(path[1])) addr := ethcmn.BytesToAddress([]byte(path[1]))
key := ethcmn.BytesToHash([]byte(path[2])) key := ethcmn.BytesToHash([]byte(path[2]))
val := keeper.GetState(ctx, addr, key) val := keeper.GetState(ctx, addr, key)
bRes := types.QueryResStorage{Value: val.Bytes()} bRes := hexutil.Bytes(val.Bytes())
res, err := codec.MarshalJSONIndent(keeper.cdc, bRes) res, err := codec.MarshalJSONIndent(keeper.cdc, &bRes)
if err != nil { if err != nil {
panic("could not marshal result to JSON") panic("could not marshal result to JSON: " + err.Error())
} }
return res, nil return res, nil
} }
@ -99,10 +92,9 @@ func queryStorage(ctx sdk.Context, path []string, keeper Keeper) ([]byte, sdk.Er
func queryCode(ctx sdk.Context, path []string, keeper Keeper) ([]byte, sdk.Error) { func queryCode(ctx sdk.Context, path []string, keeper Keeper) ([]byte, sdk.Error) {
addr := ethcmn.BytesToAddress([]byte(path[1])) addr := ethcmn.BytesToAddress([]byte(path[1]))
code := keeper.GetCode(ctx, addr) code := keeper.GetCode(ctx, addr)
cRes := types.QueryResCode{Code: code} res, err := codec.MarshalJSONIndent(keeper.cdc, code)
res, err := codec.MarshalJSONIndent(keeper.cdc, cRes)
if err != nil { if err != nil {
panic("could not marshal result to JSON") panic("could not marshal result to JSON: " + err.Error())
} }
return res, nil return res, nil
@ -111,10 +103,10 @@ func queryCode(ctx sdk.Context, path []string, keeper Keeper) ([]byte, sdk.Error
func queryNonce(ctx sdk.Context, path []string, keeper Keeper) ([]byte, sdk.Error) { func queryNonce(ctx sdk.Context, path []string, keeper Keeper) ([]byte, sdk.Error) {
addr := ethcmn.BytesToAddress([]byte(path[1])) addr := ethcmn.BytesToAddress([]byte(path[1]))
nonce := keeper.GetNonce(ctx, addr) nonce := keeper.GetNonce(ctx, addr)
nRes := types.QueryResNonce{Nonce: nonce} nRes := hexutil.Uint64(nonce)
res, err := codec.MarshalJSONIndent(keeper.cdc, nRes) res, err := codec.MarshalJSONIndent(keeper.cdc, nRes)
if err != nil { if err != nil {
panic("could not marshal result to JSON") panic("could not marshal result to JSON: " + err.Error())
} }
return res, nil return res, nil

View File

@ -1,61 +0,0 @@
package types
import (
"math/big"
"github.com/ethereum/go-ethereum/common/hexutil"
)
// QueryResProtocolVersion is response type for protocol version query
type QueryResProtocolVersion struct {
Version string `json:"result"`
}
func (q QueryResProtocolVersion) String() string {
return q.Version
}
// QueryResBalance is response type for balance query
type QueryResBalance struct {
Balance *hexutil.Big `json:"result"`
}
func (q QueryResBalance) String() string {
return q.Balance.String()
}
// QueryResBlockNumber is response type for block number query
type QueryResBlockNumber struct {
Number *big.Int `json:"result"`
}
func (q QueryResBlockNumber) String() string {
return q.Number.String()
}
// QueryResStorage is response type for storage query
type QueryResStorage struct {
Value []byte `json:"value"`
}
func (q QueryResStorage) String() string {
return string(q.Value)
}
// QueryResCode is response type for code query
type QueryResCode struct {
Code []byte
}
func (q QueryResCode) String() string {
return string(q.Code)
}
// QueryResNonce is response type for Nonce query
type QueryResNonce struct {
Nonce uint64 `json:"result"`
}
func (q QueryResNonce) String() string {
return string(q.Nonce)
}