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

Commit d1e22e1

Browse files
authored
add cmd flag for rpc api modules (#821)
* add cmd flag for rpc api modules * fix test-contract race condition by sleeping
1 parent 99112e1 commit d1e22e1

File tree

7 files changed

+68
-40
lines changed

7 files changed

+68
-40
lines changed

rpc/apis.go

Lines changed: 53 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -21,46 +21,67 @@ const (
2121
EthNamespace = "eth"
2222
PersonalNamespace = "personal"
2323
NetNamespace = "net"
24+
flagRPCAPI = "rpc-api"
2425

2526
apiVersion = "1.0"
2627
)
2728

2829
// GetAPIs returns the list of all APIs from the Ethereum namespaces
29-
func GetAPIs(clientCtx context.CLIContext, keys ...ethsecp256k1.PrivKey) []rpc.API {
30+
func GetAPIs(clientCtx context.CLIContext, selectedApis []string, keys ...ethsecp256k1.PrivKey) []rpc.API {
3031
nonceLock := new(rpctypes.AddrLocker)
3132
backend := backend.New(clientCtx)
3233
ethAPI := eth.NewAPI(clientCtx, backend, nonceLock, keys...)
3334

34-
return []rpc.API{
35-
{
36-
Namespace: Web3Namespace,
37-
Version: apiVersion,
38-
Service: web3.NewAPI(),
39-
Public: true,
40-
},
41-
{
42-
Namespace: EthNamespace,
43-
Version: apiVersion,
44-
Service: ethAPI,
45-
Public: true,
46-
},
47-
{
48-
Namespace: EthNamespace,
49-
Version: apiVersion,
50-
Service: filters.NewAPI(clientCtx, backend),
51-
Public: true,
52-
},
53-
{
54-
Namespace: PersonalNamespace,
55-
Version: apiVersion,
56-
Service: personal.NewAPI(ethAPI),
57-
Public: false,
58-
},
59-
{
60-
Namespace: NetNamespace,
61-
Version: apiVersion,
62-
Service: net.NewAPI(clientCtx),
63-
Public: true,
64-
},
35+
var apis []rpc.API
36+
37+
for _, api := range selectedApis {
38+
switch api {
39+
case Web3Namespace:
40+
apis = append(apis,
41+
rpc.API{
42+
Namespace: Web3Namespace,
43+
Version: apiVersion,
44+
Service: web3.NewAPI(),
45+
Public: true,
46+
},
47+
)
48+
case EthNamespace:
49+
apis = append(apis,
50+
rpc.API{
51+
Namespace: EthNamespace,
52+
Version: apiVersion,
53+
Service: ethAPI,
54+
Public: true,
55+
},
56+
)
57+
apis = append(apis,
58+
rpc.API{
59+
Namespace: EthNamespace,
60+
Version: apiVersion,
61+
Service: filters.NewAPI(clientCtx, backend),
62+
Public: true,
63+
},
64+
)
65+
case PersonalNamespace:
66+
apis = append(apis,
67+
rpc.API{
68+
Namespace: PersonalNamespace,
69+
Version: apiVersion,
70+
Service: personal.NewAPI(ethAPI),
71+
Public: false,
72+
},
73+
)
74+
case NetNamespace:
75+
apis = append(apis,
76+
rpc.API{
77+
Namespace: NetNamespace,
78+
Version: apiVersion,
79+
Service: net.NewAPI(clientCtx),
80+
Public: true,
81+
},
82+
)
83+
}
6584
}
85+
86+
return apis
6687
}

rpc/cmd.go

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

33
import (
4+
"fmt"
5+
46
"github.com/cosmos/cosmos-sdk/client/flags"
57
"github.com/cosmos/cosmos-sdk/client/lcd"
68
"github.com/cosmos/cosmos-sdk/codec"
@@ -11,6 +13,7 @@ import (
1113
// Cosmos rest-server endpoints
1214
func ServeCmd(cdc *codec.Codec) *cobra.Command {
1315
cmd := lcd.ServeCommand(cdc, RegisterRoutes)
16+
cmd.Flags().String(flagRPCAPI, "", fmt.Sprintf("Comma separated list of RPC API modules to enable: %s, %s, %s, %s", Web3Namespace, EthNamespace, PersonalNamespace, NetNamespace))
1417
cmd.Flags().String(flagUnlockKey, "", "Select a key to unlock on the RPC server")
1518
cmd.Flags().String(flagWebsocket, "8546", "websocket port to listen to")
1619
cmd.Flags().StringP(flags.FlagBroadcastMode, "b", flags.BroadcastSync, "Transaction broadcasting mode (sync|async|block)")

rpc/config.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,11 @@ func RegisterRoutes(rs *lcd.RestServer) {
5858
}
5959
}
6060

61-
apis := GetAPIs(rs.CliCtx, privkeys...)
61+
rpcapi := viper.GetString(flagRPCAPI)
62+
rpcapi = strings.ReplaceAll(rpcapi, " ", "")
63+
rpcapiArr := strings.Split(rpcapi, ",")
64+
65+
apis := GetAPIs(rs.CliCtx, rpcapiArr, privkeys...)
6266

6367
// Register all the APIs exposed by the namespace services
6468
// TODO: handle allowlist and private APIs

scripts/contract-test.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,13 @@ $PWD/build/ethermintd start --pruning=nothing --rpc.unsafe --log_level "main:inf
4949
sleep 1
5050

5151
# Start the rest server with unlocked key in background and log to file
52-
$PWD/build/ethermintcli rest-server --laddr "tcp://localhost:8545" --unlock-key $KEY --chain-id $CHAINID --trace > ethermintcli.log &
52+
$PWD/build/ethermintcli rest-server --laddr "tcp://localhost:8545" --unlock-key $KEY --chain-id $CHAINID --trace --rpc-api="web3,eth,net,personal" > ethermintcli.log &
5353

5454
solcjs --abi $PWD/tests-solidity/suites/basic/contracts/Counter.sol --bin -o $PWD/tests-solidity/suites/basic/counter
5555
mv $PWD/tests-solidity/suites/basic/counter/*.abi $PWD/tests-solidity/suites/basic/counter/counter_sol.abi 2> /dev/null
5656
mv $PWD/tests-solidity/suites/basic/counter/*.bin $PWD/tests-solidity/suites/basic/counter/counter_sol.bin 2> /dev/null
5757

58-
ACCT=$(curl --fail --silent -X POST --data '{"jsonrpc":"2.0","method":"eth_accounts","params":[],"id":1}' -H "Content-Type: application/json" http://localhost:8545 | grep -o '\0x[^"]*' 2>&1)
58+
ACCT=$(curl --fail --silent -X POST --data '{"jsonrpc":"2.0","method":"eth_accounts","params":[],"id":1}' -H "Content-Type: application/json" http://localhost:8545 | grep -o '\0x[^"]*' | head -1 2>&1)
5959

6060
echo $ACCT
6161

@@ -66,4 +66,4 @@ PRIVKEY="$("$PWD"/build/ethermintcli keys unsafe-export-eth-key $KEY)"
6666
echo $PRIVKEY
6767

6868
## need to get the private key from the account in order to check this functionality.
69-
cd tests-solidity/suites/basic/ && go get && go run main.go $ACCT
69+
cd tests-solidity/suites/basic/ && go get && sleep 5 && go run main.go $ACCT

scripts/integration-test-all.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ start_func() {
121121

122122
start_cli_func() {
123123
echo "starting ethermint node $i in background ..."
124-
"$PWD"/build/ethermintcli rest-server --unlock-key $KEY"$i" --chain-id $CHAINID --trace \
124+
"$PWD"/build/ethermintcli rest-server --unlock-key $KEY"$i" --chain-id $CHAINID --trace --rpc-api="web3,eth,net,personal" \
125125
--laddr "tcp://localhost:$RPC_PORT$i" --node tcp://$IP_ADDR:$NODE_RPC_PORT"$i" \
126126
--home "$DATA_CLI_DIR$i" --read-timeout 30 --write-timeout 30 \
127127
>"$DATA_CLI_DIR"/cli"$i".log 2>&1 & disown

scripts/run-solidity-tests.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ fi
2424
chmod +x ./init-test-node.sh
2525
./init-test-node.sh > ethermintd.log &
2626
sleep 5
27-
ethermintcli rest-server --laddr "tcp://localhost:8545" --unlock-key localkey,user1,user2 --chain-id $CHAINID --trace --wsport 8546 > ethermintcli.log &
27+
ethermintcli rest-server --laddr "tcp://localhost:8545" --unlock-key localkey,user1,user2 --chain-id $CHAINID --trace --wsport 8546 --rpc-api="web3,eth,net,personal" > ethermintcli.log &
2828

2929
cd suites/initializable
3030
yarn test-ethermint
@@ -45,7 +45,7 @@ exit $ok
4545

4646
./../../init-test-node.sh > ethermintd.log &
4747
sleep 5
48-
ethermintcli rest-server --laddr "tcp://localhost:8545" --unlock-key localkey,user1,user2 --chain-id $CHAINID --trace --wsport 8546 > ethermintcli.log &
48+
ethermintcli rest-server --laddr "tcp://localhost:8545" --unlock-key localkey,user1,user2 --chain-id $CHAINID --trace --wsport 8546 --rpc-api="web3,eth,net,personal" > ethermintcli.log &
4949

5050
cd ../initializable-buidler
5151
yarn test-ethermint

scripts/start.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/bin/sh
22
ethermintd --home /ethermint/node$ID/ethermintd/ start > ethermintd.log &
33
sleep 5
4-
ethermintcli rest-server --laddr "tcp://localhost:8545" --chain-id "ethermint-7305661614933169792" --trace > ethermintcli.log &
4+
ethermintcli rest-server --laddr "tcp://localhost:8545" --chain-id "ethermint-7305661614933169792" --trace --rpc-api="web3,eth,net,personal" > ethermintcli.log &
55
tail -f /dev/null

0 commit comments

Comments
 (0)