@@ -10,18 +10,19 @@ package tester
1010import (
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
2223const (
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+
3851func 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
7090func 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
83112func 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
95128func 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
108150func 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
120171func 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}
0 commit comments