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

Commit 1cac4fe

Browse files
author
David Ansermino
authored
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.
1 parent 28aaba0 commit 1cac4fe

File tree

6 files changed

+147
-154
lines changed

6 files changed

+147
-154
lines changed

rpc/eth_api.go

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ func NewPublicEthAPI(cliCtx context.CLIContext, nonceLock *AddrLocker,
4242
}
4343

4444
// ProtocolVersion returns the supported Ethereum protocol version.
45-
func (e *PublicEthAPI) ProtocolVersion() string {
46-
return version.ProtocolVersion
45+
func (e *PublicEthAPI) ProtocolVersion() hexutil.Uint {
46+
return hexutil.Uint(version.ProtocolVersion)
4747
}
4848

4949
// 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) {
9595
}
9696

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

105-
var out types.QueryResBlockNumber
106-
e.cliCtx.Codec.MustUnmarshalJSON(res, &out)
107-
return out.Number
104+
var qRes uint64
105+
e.cliCtx.Codec.MustUnmarshalJSON(res, &qRes)
106+
107+
return hexutil.Uint64(qRes), nil
108108
}
109109

110110
// 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
115115
return nil, err
116116
}
117117

118-
var out types.QueryResBalance
118+
var out *big.Int
119119
e.cliCtx.Codec.MustUnmarshalJSON(res, &out)
120-
return (*hexutil.Big)(out.Balance), nil
120+
121+
return (*hexutil.Big)(out), nil
121122
}
122123

123124
// 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
128129
return nil, err
129130
}
130131

131-
var out types.QueryResStorage
132+
var out []byte
132133
e.cliCtx.Codec.MustUnmarshalJSON(res, &out)
133-
return out.Value[:], nil
134+
return out, nil
134135
}
135136

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

144-
var out types.QueryResNonce
145-
e.cliCtx.Codec.MustUnmarshalJSON(res, &out)
146-
return hexutil.Uint64(out.Nonce), nil
145+
var out *hexutil.Uint64
146+
e.cliCtx.Codec.MustUnmarshalJSON(res, out)
147+
return out, nil
147148
}
148149

149150
// 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
185186
return nil, err
186187
}
187188

188-
var out types.QueryResCode
189+
var out []byte
189190
e.cliCtx.Codec.MustUnmarshalJSON(res, &out)
190-
return out.Code, nil
191+
return out, nil
191192
}
192193

193194
// Sign signs the provided data using the private key of address via Geth's signature standard.

rpc/tester/tester_test.go

Lines changed: 104 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,19 @@ package tester
1010
import (
1111
"bytes"
1212
"encoding/json"
13+
"errors"
1314
"fmt"
14-
"github.com/cosmos/ethermint/version"
15-
"github.com/cosmos/ethermint/x/evm/types"
16-
"io/ioutil"
1715
"math/big"
1816
"net/http"
1917
"testing"
18+
19+
"github.com/cosmos/ethermint/version"
20+
"github.com/ethereum/go-ethereum/common/hexutil"
2021
)
2122

2223
const (
23-
host = "127.0.0.1"
24-
port = 1317
24+
host = "localhost"
25+
port = 8545
2526
addrA = "0xc94770007dda54cF92009BFF0dE90c06F603a09f"
2627
addrAStoreKey = 0
2728
)
@@ -35,6 +36,18 @@ type Request struct {
3536
Id int `json:"id"`
3637
}
3738

39+
type RPCError struct {
40+
Code int `json:"code"`
41+
Message string `json:"message"`
42+
Data interface{} `json:"data,omitempty"`
43+
}
44+
45+
type Response struct {
46+
Error *RPCError `json:"error"`
47+
Id int `json:"id"`
48+
Result json.RawMessage `json:"result,omitempty"`
49+
}
50+
3851
func createRequest(method string, params []string) Request {
3952
return Request{
4053
Version: "2.0",
@@ -44,86 +57,133 @@ func createRequest(method string, params []string) Request {
4457
}
4558
}
4659

47-
func call(t *testing.T, method string, params []string, resp interface{}) {
60+
func call(method string, params []string) (*Response, error) {
4861
req, err := json.Marshal(createRequest(method, params))
4962
if err != nil {
50-
t.Error(err)
63+
return nil, err
5164
}
5265

5366
res, err := http.Post(addr, "application/json", bytes.NewBuffer(req))
5467
if err != nil {
55-
t.Error(err)
68+
return nil, err
5669
}
57-
defer res.Body.Close()
5870

59-
body, err := ioutil.ReadAll(res.Body)
71+
decoder := json.NewDecoder(res.Body)
72+
var rpcRes *Response
73+
err = decoder.Decode(&rpcRes)
6074
if err != nil {
61-
t.Error(err)
75+
return nil, err
76+
}
77+
78+
if rpcRes.Error != nil {
79+
return nil, errors.New(rpcRes.Error.Message)
6280
}
6381

64-
err = json.Unmarshal(body, resp)
82+
err = res.Body.Close()
6583
if err != nil {
66-
t.Error(err)
84+
return nil, err
6785
}
86+
87+
return rpcRes, nil
6888
}
6989

7090
func TestEth_protocolVersion(t *testing.T) {
71-
expectedRes := version.ProtocolVersion
91+
expectedRes := hexutil.Uint(version.ProtocolVersion)
7292

73-
res := &types.QueryResProtocolVersion{}
74-
call(t, "eth_protocolVersion", []string{}, res)
93+
rpcRes, err := call("eth_protocolVersion", []string{})
94+
if err != nil {
95+
t.Fatal(err)
96+
}
7597

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

78-
if res.Version != expectedRes {
79-
t.Errorf("expected: %s got: %s\n", expectedRes, res)
101+
if err != nil {
102+
t.Fatal(err)
103+
}
104+
105+
t.Logf("Got protocol version: %s\n", res.String())
106+
107+
if res != expectedRes {
108+
t.Fatalf("expected: %s got: %s\n", expectedRes.String(), rpcRes.Result)
80109
}
81110
}
82111

83112
func TestEth_blockNumber(t *testing.T) {
84-
res := &types.QueryResBlockNumber{}
85-
call(t, "eth_blockNumber", []string{}, res)
86-
87-
t.Logf("Got block number: %s\n", res.Number.String())
113+
rpcRes, err := call("eth_blockNumber", []string{})
114+
if err != nil {
115+
t.Fatal(err)
116+
}
117+
var res hexutil.Uint64
118+
err = res.UnmarshalJSON(rpcRes.Result)
88119

89-
// -1 if x < y, 0 if x == y; where x is res, y is 0
90-
if res.Number.Cmp(big.NewInt(0)) < 1 {
91-
t.Errorf("Invalid block number got: %v", res)
120+
if err != nil {
121+
t.Fatal(err)
92122
}
123+
124+
t.Logf("Got block number: %s\n", res.String())
125+
93126
}
94127

95128
func TestEth_GetBalance(t *testing.T) {
96-
//expectedRes := types.QueryResBalance{Balance:}
97-
res := &types.QueryResBalance{}
98-
call(t, "eth_getBalance", []string{addrA, "latest"}, res)
129+
rpcRes, err := call("eth_getBalance", []string{addrA, "0x0"})
130+
if err != nil {
131+
t.Fatal(err)
132+
return
133+
}
99134

100-
t.Logf("Got balance %s for %s\n", res.Balance.String(), addrA)
135+
var res hexutil.Big
136+
err = res.UnmarshalJSON(rpcRes.Result)
137+
if err != nil {
138+
t.Fatal(err)
139+
}
140+
141+
t.Logf("Got balance %s for %s\n", res.String(), addrA)
101142

102143
// 0 if x == y; where x is res, y is 0
103-
if res.Balance.ToInt().Cmp(big.NewInt(0)) != 0 {
104-
t.Errorf("expected balance: %d, got: %s", 0, res.Balance.String())
144+
if res.ToInt().Cmp(big.NewInt(0)) != 0 {
145+
t.Errorf("expected balance: %d, got: %s", 0, res.String())
105146
}
147+
106148
}
107149

108150
func TestEth_GetStorageAt(t *testing.T) {
109-
expectedRes := types.QueryResStorage{Value: []byte{}}
110-
res := &types.QueryResStorage{}
111-
call(t, "eth_getStorageAt", []string{addrA, string(addrAStoreKey), "latest"}, res)
151+
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}
152+
rpcRes, err := call("eth_getStorageAt", []string{addrA, string(addrAStoreKey), "0x0"})
153+
if err != nil {
154+
t.Fatal(err)
155+
}
112156

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

115-
if !bytes.Equal(res.Value, expectedRes.Value) {
116-
t.Errorf("expected: %X got: %X", expectedRes.Value, res.Value)
160+
if err != nil {
161+
t.Fatal(err)
162+
}
163+
164+
t.Logf("Got value [%X] for %s with key %X\n", storage, addrA, addrAStoreKey)
165+
166+
if !bytes.Equal(storage, expectedRes) {
167+
t.Errorf("expected: %d (%d bytes) got: %d (%d bytes)", expectedRes, len(expectedRes), storage, len(storage))
117168
}
118169
}
119170

120171
func TestEth_GetCode(t *testing.T) {
121-
expectedRes := types.QueryResCode{Code: []byte{}}
122-
res := &types.QueryResCode{}
123-
call(t, "eth_getCode", []string{addrA, "latest"}, res)
172+
expectedRes := hexutil.Bytes{}
173+
rpcRes, err := call("eth_getCode", []string{addrA, "0x0"})
174+
if err != nil {
175+
t.Error(err)
176+
}
177+
178+
var code hexutil.Bytes
179+
err = code.UnmarshalJSON(rpcRes.Result)
180+
181+
if err != nil {
182+
t.Fatal(err)
183+
}
124184

125-
t.Logf("Got code [%X] for %s\n", res.Code, addrA)
126-
if !bytes.Equal(expectedRes.Code, res.Code) {
127-
t.Errorf("expected: %X got: %X", expectedRes.Code, res.Code)
185+
t.Logf("Got code [%X] for %s\n", code, addrA)
186+
if !bytes.Equal(expectedRes, code) {
187+
t.Errorf("expected: %X got: %X", expectedRes, code)
128188
}
129189
}

version/version.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const AppName = "Ethermint"
1515
const Version = "0.0.0"
1616

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

2020
// GitCommit contains the git SHA1 short hash set by build flags.
2121
var GitCommit = ""

x/evm/client/cli/query.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"github.com/cosmos/cosmos-sdk/client/context"
88
"github.com/cosmos/cosmos-sdk/codec"
99
"github.com/cosmos/ethermint/x/evm/types"
10+
"github.com/ethereum/go-ethereum/common/hexutil"
1011
"github.com/spf13/cobra"
1112
)
1213

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

45-
var out types.QueryResBlockNumber
46+
var out *hexutil.Big
4647
cdc.MustUnmarshalJSON(res, &out)
4748
return cliCtx.PrintOutput(out)
4849
},
@@ -67,7 +68,7 @@ func GetCmdGetStorageAt(queryRoute string, cdc *codec.Codec) *cobra.Command {
6768
fmt.Printf("could not resolve: %s\n", err)
6869
return nil
6970
}
70-
var out types.QueryResStorage
71+
var out hexutil.Bytes
7172
cdc.MustUnmarshalJSON(res, &out)
7273
return cliCtx.PrintOutput(out)
7374
},
@@ -91,9 +92,9 @@ func GetCmdGetCode(queryRoute string, cdc *codec.Codec) *cobra.Command {
9192
fmt.Printf("could not resolve: %s\n", err)
9293
return nil
9394
}
94-
var out types.QueryResCode
95+
var out []byte
9596
cdc.MustUnmarshalJSON(res, &out)
96-
return cliCtx.PrintOutput(out)
97+
return cliCtx.PrintOutput(hexutil.Bytes(out))
9798
},
9899
}
99100
}
@@ -115,7 +116,7 @@ func GetCmdGetNonce(queryRoute string, cdc *codec.Codec) *cobra.Command {
115116
fmt.Printf("could not resolve: %s\n", err)
116117
return nil
117118
}
118-
var out types.QueryResNonce
119+
var out hexutil.Uint64
119120
cdc.MustUnmarshalJSON(res, &out)
120121
return cliCtx.PrintOutput(out)
121122
},

0 commit comments

Comments
 (0)