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

Commit 92dc7d9

Browse files
author
David Ansermino
authored
Basic RPC and CLI Queries (#77)
- Adds ethermint query command (`emintcli query ethermint <query>`) - Supports block number, storage, code, balance lookups - Implements RPC API methods `eth_blockNumber`, `eth_getStorageAt`, `eth_getBalance`, and `eth_getCode` - Adds tester utility for RPC calls - Adheres to go test format, but should not be run with regular suite - Requires daemon and RPC server to be running - Excluded from `make test`, available with `make test-rpc` - Implemented AppModule interface and added EVM module to app - Required for routing - Implements `InitGenesis` (`x/evm/genesis.go`) and stubs `ExportGenesis` - Modifies GenesisAccount to match expected format
1 parent d9d45b4 commit 92dc7d9

File tree

18 files changed

+545
-250
lines changed

18 files changed

+545
-250
lines changed

Makefile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
PACKAGES=$(shell go list ./... | grep -Ev 'vendor|importer')
15+
PACKAGES=$(shell go list ./... | grep -Ev 'vendor|importer|rpc/tester')
1616
COMMIT_HASH := $(shell git rev-parse --short HEAD)
1717
BUILD_FLAGS = -tags netgo -ldflags "-X github.com/cosmos/ethermint/version.GitCommit=${COMMIT_HASH}"
1818
DOCKER_TAG = unstable
@@ -146,6 +146,9 @@ test-import:
146146
--blockchain blockchain --timeout=5m
147147
# TODO: remove tmp directory after test run to avoid subsequent errors
148148

149+
test-rpc:
150+
@${GO_MOD} go test -v --vet=off ./rpc/tester
151+
149152
godocs:
150153
@echo "--> Wait a few seconds and visit http://localhost:6060/pkg/github.com/cosmos/ethermint"
151154
godoc -http=:6060

app/ethermint.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,7 @@ var (
5959
crisis.AppModuleBasic{},
6060
slashing.AppModuleBasic{},
6161
supply.AppModuleBasic{},
62-
// TODO: Enable EVM AppModuleBasic
63-
//evm.AppModuleBasic{},
62+
evm.AppModuleBasic{},
6463
)
6564
)
6665

@@ -185,7 +184,7 @@ func NewEthermintApp(logger tmlog.Logger, db dbm.DB, loadLatest bool,
185184
app.slashingKeeper = slashing.NewKeeper(app.cdc, app.keySlashing, &stakingKeeper,
186185
slashingSubspace, slashing.DefaultCodespace)
187186
app.crisisKeeper = crisis.NewKeeper(crisisSubspace, invCheckPeriod, app.supplyKeeper, auth.FeeCollectorName)
188-
app.evmKeeper = evm.NewKeeper(app.accountKeeper, app.evmStoreKey, app.evmCodeKey)
187+
app.evmKeeper = evm.NewKeeper(app.accountKeeper, app.evmStoreKey, app.evmCodeKey, cdc)
189188

190189
// register the proposal types
191190
govRouter := gov.NewRouter()
@@ -212,6 +211,7 @@ func NewEthermintApp(logger tmlog.Logger, db dbm.DB, loadLatest bool,
212211
mint.NewAppModule(app.mintKeeper),
213212
slashing.NewAppModule(app.slashingKeeper, app.stakingKeeper),
214213
staking.NewAppModule(app.stakingKeeper, app.distrKeeper, app.accountKeeper, app.supplyKeeper),
214+
evm.NewAppModule(app.evmKeeper),
215215
)
216216

217217
// During begin block slashing happens after distr.BeginBlocker so that

cmd/emintcli/main.go

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package main
22

33
import (
4+
"github.com/cosmos/ethermint/rpc"
5+
"github.com/tendermint/go-amino"
46
"os"
57
"path"
68

@@ -10,7 +12,6 @@ import (
1012
sdk "github.com/cosmos/cosmos-sdk/types"
1113

1214
emintapp "github.com/cosmos/ethermint/app"
13-
"github.com/cosmos/ethermint/rpc"
1415
"github.com/spf13/cobra"
1516
"github.com/spf13/viper"
1617
"github.com/tendermint/tendermint/libs/cli"
@@ -43,7 +44,7 @@ func main() {
4344
rootCmd.AddCommand(
4445
sdkrpc.StatusCommand(),
4546
client.ConfigCmd(emintapp.DefaultCLIHome),
46-
// TODO: Set up query command
47+
queryCmd(cdc),
4748
// TODO: Set up tx command
4849
// TODO: Set up rest routes (if included, different from web3 api)
4950
rpc.Web3RpcCmd(cdc),
@@ -59,6 +60,24 @@ func main() {
5960
}
6061
}
6162

63+
func queryCmd(cdc *amino.Codec) *cobra.Command {
64+
queryCmd := &cobra.Command{
65+
Use: "query",
66+
Aliases: []string{"q"},
67+
Short: "Querying subcommands",
68+
}
69+
70+
// TODO: Possibly add these query commands from other modules
71+
//queryCmd.AddCommand(
72+
// ...
73+
//)
74+
75+
// add modules' query commands
76+
emintapp.ModuleBasics.AddQueryCommands(queryCmd, cdc)
77+
78+
return queryCmd
79+
}
80+
6281
func initConfig(cmd *cobra.Command) error {
6382
home, err := cmd.PersistentFlags().GetString(cli.HomeFlag)
6483
if err != nil {

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ require (
4646
github.com/steakknife/hamming v0.0.0-20180906055917-c99c65617cd3 // indirect
4747
github.com/stretchr/testify v1.3.0
4848
github.com/syndtr/goleveldb v1.0.0 // indirect
49+
github.com/tendermint/go-amino v0.15.0
4950
github.com/tendermint/tendermint v0.32.0
5051
github.com/tyler-smith/go-bip39 v1.0.0 // indirect
5152
github.com/wsddn/go-ecdh v0.0.0-20161211032359-48726bab9208 // indirect

go.sum

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,11 @@ github.com/cespare/cp v1.1.1/go.mod h1:SOGHArjBr4JWaSDEVpWpo/hNg6RoKrls6Oh40hiwW
3939
github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=
4040
github.com/client9/misspell v0.3.4 h1:ta993UF76GwbvJcIo3Y68y/M3WxlpEHPWIGDkJYwzJI=
4141
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
42-
github.com/coreos/bbolt v1.3.2/go.mod h1:iRUV2dpdMOn7Bo10OQBFzIJO9kkE559Wcmn+qkEiiKk=
4342
github.com/coreos/etcd v3.3.10+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE=
4443
github.com/coreos/go-etcd v2.0.0+incompatible/go.mod h1:Jez6KQU2B/sWsbdaef3ED8NzMklzPG4d5KIOhIy30Tk=
4544
github.com/coreos/go-semver v0.2.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk=
46-
github.com/cosmos/cosmos-sdk v0.28.2-0.20190709220430-3f519832a7a5 h1:gakqjbZrqlUB1/rx8r/s86SVcRatOafVDfJF99yBcng=
47-
github.com/cosmos/cosmos-sdk v0.28.2-0.20190709220430-3f519832a7a5/go.mod h1:qzvnGkt2+ynMpjmf9/dws/94/qM87awRbuyvF7r2R8Q=
4845
github.com/cosmos/cosmos-sdk v0.28.2-0.20190711105643-280734d0e37f h1:jmVM19bsHZRVVe8rugzfILuL3VPgCj5b6941I20Naw0=
4946
github.com/cosmos/cosmos-sdk v0.28.2-0.20190711105643-280734d0e37f/go.mod h1:qzvnGkt2+ynMpjmf9/dws/94/qM87awRbuyvF7r2R8Q=
50-
github.com/cosmos/cosmos-sdk v0.35.0 h1:EPeie1aKHwnXtTzKggvabG7aAPN+DDmju2xquvjFwao=
5147
github.com/cosmos/go-bip39 v0.0.0-20180618194314-52158e4697b8 h1:Iwin12wRQtyZhH6FV3ykFcdGNlYEzoeR0jN8Vn+JWsI=
5248
github.com/cosmos/go-bip39 v0.0.0-20180618194314-52158e4697b8/go.mod h1:tSxLoYXyBmiFeKpvmq4dzayMdCjCnu8uqmCysIGBT2Y=
5349
github.com/cosmos/go-bip39 v0.0.0-20180819234021-555e2067c45d h1:49RLWk1j44Xu4fjHb6JFYmeUnDORVwHNkDxaQ0ctCVU=
@@ -124,8 +120,6 @@ github.com/google/uuid v1.0.0 h1:b4Gk+7WdP/d3HZH8EJsZpvV7EtDOgaZLtnaNGIu1adA=
124120
github.com/google/uuid v1.0.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
125121
github.com/gorilla/mux v1.7.0 h1:tOSd0UKHQd6urX6ApfOn4XdBMY6Sh1MfxV3kmaazO+U=
126122
github.com/gorilla/mux v1.7.0/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=
127-
github.com/gorilla/mux v1.7.3 h1:gnP5JzjVOuiZD07fKKToCAOjS0yOpj/qPETTXCCS6hw=
128-
github.com/gorilla/mux v1.7.3/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=
129123
github.com/gorilla/websocket v1.2.0 h1:VJtLvh6VQym50czpZzx07z/kw9EgAxI3x1ZB8taTMQQ=
130124
github.com/gorilla/websocket v1.2.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
131125
github.com/gorilla/websocket v1.4.0 h1:WDFjx/TMzVgy9VdMMQi2K2Emtwi2QcUQsztZ/zLaH/Q=
@@ -146,7 +140,6 @@ github.com/jackpal/go-nat-pmp v1.0.1/go.mod h1:QPH045xvCAeXUZOxsnwmrtiCoxIr9eob+
146140
github.com/jessevdk/go-flags v0.0.0-20141203071132-1679536dcc89/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
147141
github.com/jmhodges/levigo v1.0.0 h1:q5EC36kV79HWeTBWsod3mG11EgStG3qArTKcvlksN1U=
148142
github.com/jmhodges/levigo v1.0.0/go.mod h1:Q6Qx+uH3RAqyK4rFQroq9RL7mdkABMcfhEI+nNuzMJQ=
149-
github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo=
150143
github.com/jrick/logrotate v1.0.0/go.mod h1:LNinyqDIJnpAur+b8yyulnQw/wDuN1+BYKlTRt3OuAQ=
151144
github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
152145
github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=
@@ -212,7 +205,6 @@ github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910/go.mod h1:
212205
github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90 h1:S/YWwWx/RA8rT8tKFRuGUZhuA90OyIBpPCXkcbwU8DE=
213206
github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
214207
github.com/prometheus/common v0.0.0-20181020173914-7e9e6cabbd39/go.mod h1:daVV7qP5qjZbuso7PdcryaAu0sAZbrN9i7WWcTMWvro=
215-
github.com/prometheus/common v0.0.0-20181113130724-41aa239b4cce/go.mod h1:daVV7qP5qjZbuso7PdcryaAu0sAZbrN9i7WWcTMWvro=
216208
github.com/prometheus/common v0.0.0-20181126121408-4724e9255275/go.mod h1:daVV7qP5qjZbuso7PdcryaAu0sAZbrN9i7WWcTMWvro=
217209
github.com/prometheus/common v0.2.0 h1:kUZDBDTdBVBYBj5Tmh2NZLlF60mfjA27rM34b+cVwNU=
218210
github.com/prometheus/common v0.2.0/go.mod h1:TNfzLD0ON7rHzMJeJkieUDPYmFC7Snx/y86RQel1bk4=
@@ -301,7 +293,6 @@ github.com/wsddn/go-ecdh v0.0.0-20161211032359-48726bab9208/go.mod h1:IotVbo4F+m
301293
github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q=
302294
github.com/zondax/hid v0.9.0 h1:eiT3P6vNxAEVxXMw66eZUAAnU2zD33JBkfG/EnfAKl8=
303295
github.com/zondax/hid v0.9.0/go.mod h1:l5wttcP0jwtdLjqjMMWFVEE7d1zO0jvSPA9OPZxWpEM=
304-
go.etcd.io/bbolt v1.3.2/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU=
305296
go.etcd.io/bbolt v1.3.3 h1:MUGmc65QhB3pIlaQ5bB4LwqSj6GIonVJXpZiaKNyaKk=
306297
go.etcd.io/bbolt v1.3.3/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU=
307298
golang.org/x/crypto v0.0.0-20170930174604-9419663f5a44/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
@@ -340,7 +331,6 @@ golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5h
340331
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
341332
golang.org/x/sys v0.0.0-20180926160741-c2ed4eda69e7 h1:bit1t3mgdR35yN0cX0G8orgLtOuyL9Wqxa1mccLB0ig=
342333
golang.org/x/sys v0.0.0-20180926160741-c2ed4eda69e7/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
343-
golang.org/x/sys v0.0.0-20181107165924-66b7b1311ac8/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
344334
golang.org/x/sys v0.0.0-20181116152217-5ac8a444bdc5/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
345335
golang.org/x/sys v0.0.0-20181205085412-a5c9d58dba9a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
346336
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=

rpc/apis_test.go

Lines changed: 0 additions & 77 deletions
This file was deleted.

rpc/eth_api.go

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package rpc
22

33
import (
4+
"fmt"
45
"github.com/cosmos/cosmos-sdk/client/context"
56
"github.com/cosmos/ethermint/version"
7+
"github.com/cosmos/ethermint/x/evm/types"
68
"github.com/ethereum/go-ethereum/common"
79
"github.com/ethereum/go-ethereum/common/hexutil"
810
"github.com/ethereum/go-ethereum/rpc"
@@ -11,7 +13,7 @@ import (
1113
)
1214

1315
// PublicEthAPI is the eth_ prefixed set of APIs in the Web3 JSON-RPC spec.
14-
type PublicEthAPI struct{
16+
type PublicEthAPI struct {
1517
cliCtx context.CLIContext
1618
}
1719

@@ -61,18 +63,41 @@ func (e *PublicEthAPI) Accounts() []common.Address {
6163

6264
// BlockNumber returns the current block number.
6365
func (e *PublicEthAPI) BlockNumber() *big.Int {
64-
return big.NewInt(0)
66+
res, _, err := e.cliCtx.QueryWithData(fmt.Sprintf("custom/%s/blockNumber", types.ModuleName), nil)
67+
if err != nil {
68+
fmt.Printf("could not resolve: %s\n", err)
69+
return nil
70+
}
71+
72+
var out types.QueryResBlockNumber
73+
e.cliCtx.Codec.MustUnmarshalJSON(res, &out)
74+
return out.Number
6575
}
6676

6777
// GetBalance returns the provided account's balance up to the provided block number.
6878
func (e *PublicEthAPI) GetBalance(address common.Address, blockNum rpc.BlockNumber) *hexutil.Big {
69-
out := big.NewInt(0)
70-
return (*hexutil.Big)(out)
79+
res, _, err := e.cliCtx.QueryWithData(fmt.Sprintf("custom/%s/balance/%s", types.ModuleName, address), nil)
80+
if err != nil {
81+
fmt.Printf("could not resolve: %s\n", err)
82+
return nil
83+
}
84+
85+
var out types.QueryResBalance
86+
e.cliCtx.Codec.MustUnmarshalJSON(res, &out)
87+
return (*hexutil.Big)(out.Balance)
7188
}
7289

7390
// GetStorageAt returns the contract storage at the given address, block number, and key.
7491
func (e *PublicEthAPI) GetStorageAt(address common.Address, key string, blockNum rpc.BlockNumber) hexutil.Bytes {
75-
return nil
92+
res, _, err := e.cliCtx.QueryWithData(fmt.Sprintf("custom/%s/storage/%s/%s", types.ModuleName, address, key), nil)
93+
if err != nil {
94+
fmt.Printf("could not resolve: %s\n", err)
95+
return nil
96+
}
97+
98+
var out types.QueryResStorage
99+
e.cliCtx.Codec.MustUnmarshalJSON(res, &out)
100+
return out.Value[:]
76101
}
77102

78103
// GetTransactionCount returns the number of transactions at the given address up to the given block number.
@@ -102,7 +127,15 @@ func (e *PublicEthAPI) GetUncleCountByBlockNumber(blockNum rpc.BlockNumber) hexu
102127

103128
// GetCode returns the contract code at the given address and block number.
104129
func (e *PublicEthAPI) GetCode(address common.Address, blockNumber rpc.BlockNumber) hexutil.Bytes {
105-
return nil
130+
res, _, err := e.cliCtx.QueryWithData(fmt.Sprintf("custom/%s/code/%s", types.ModuleName, address), nil)
131+
if err != nil {
132+
fmt.Printf("could not resolve: %s\n", err)
133+
return nil
134+
}
135+
136+
var out types.QueryResCode
137+
e.cliCtx.Codec.MustUnmarshalJSON(res, &out)
138+
return out.Code
106139
}
107140

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

rpc/rpc.go

Lines changed: 0 additions & 38 deletions
This file was deleted.

0 commit comments

Comments
 (0)