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

Commit

Permalink
add cmd flag for rpc api modules (#821)
Browse files Browse the repository at this point in the history
* add cmd flag for rpc api modules
* fix test-contract race condition by sleeping
  • Loading branch information
araskachoi authored Feb 26, 2021
1 parent 99112e1 commit d1e22e1
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 40 deletions.
85 changes: 53 additions & 32 deletions rpc/apis.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,46 +21,67 @@ const (
EthNamespace = "eth"
PersonalNamespace = "personal"
NetNamespace = "net"
flagRPCAPI = "rpc-api"

apiVersion = "1.0"
)

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

return []rpc.API{
{
Namespace: Web3Namespace,
Version: apiVersion,
Service: web3.NewAPI(),
Public: true,
},
{
Namespace: EthNamespace,
Version: apiVersion,
Service: ethAPI,
Public: true,
},
{
Namespace: EthNamespace,
Version: apiVersion,
Service: filters.NewAPI(clientCtx, backend),
Public: true,
},
{
Namespace: PersonalNamespace,
Version: apiVersion,
Service: personal.NewAPI(ethAPI),
Public: false,
},
{
Namespace: NetNamespace,
Version: apiVersion,
Service: net.NewAPI(clientCtx),
Public: true,
},
var apis []rpc.API

for _, api := range selectedApis {
switch api {
case Web3Namespace:
apis = append(apis,
rpc.API{
Namespace: Web3Namespace,
Version: apiVersion,
Service: web3.NewAPI(),
Public: true,
},
)
case EthNamespace:
apis = append(apis,
rpc.API{
Namespace: EthNamespace,
Version: apiVersion,
Service: ethAPI,
Public: true,
},
)
apis = append(apis,
rpc.API{
Namespace: EthNamespace,
Version: apiVersion,
Service: filters.NewAPI(clientCtx, backend),
Public: true,
},
)
case PersonalNamespace:
apis = append(apis,
rpc.API{
Namespace: PersonalNamespace,
Version: apiVersion,
Service: personal.NewAPI(ethAPI),
Public: false,
},
)
case NetNamespace:
apis = append(apis,
rpc.API{
Namespace: NetNamespace,
Version: apiVersion,
Service: net.NewAPI(clientCtx),
Public: true,
},
)
}
}

return apis
}
3 changes: 3 additions & 0 deletions rpc/cmd.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package rpc

import (
"fmt"

"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/client/lcd"
"github.com/cosmos/cosmos-sdk/codec"
Expand All @@ -11,6 +13,7 @@ import (
// Cosmos rest-server endpoints
func ServeCmd(cdc *codec.Codec) *cobra.Command {
cmd := lcd.ServeCommand(cdc, RegisterRoutes)
cmd.Flags().String(flagRPCAPI, "", fmt.Sprintf("Comma separated list of RPC API modules to enable: %s, %s, %s, %s", Web3Namespace, EthNamespace, PersonalNamespace, NetNamespace))
cmd.Flags().String(flagUnlockKey, "", "Select a key to unlock on the RPC server")
cmd.Flags().String(flagWebsocket, "8546", "websocket port to listen to")
cmd.Flags().StringP(flags.FlagBroadcastMode, "b", flags.BroadcastSync, "Transaction broadcasting mode (sync|async|block)")
Expand Down
6 changes: 5 additions & 1 deletion rpc/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,11 @@ func RegisterRoutes(rs *lcd.RestServer) {
}
}

apis := GetAPIs(rs.CliCtx, privkeys...)
rpcapi := viper.GetString(flagRPCAPI)
rpcapi = strings.ReplaceAll(rpcapi, " ", "")
rpcapiArr := strings.Split(rpcapi, ",")

apis := GetAPIs(rs.CliCtx, rpcapiArr, privkeys...)

// Register all the APIs exposed by the namespace services
// TODO: handle allowlist and private APIs
Expand Down
6 changes: 3 additions & 3 deletions scripts/contract-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,13 @@ $PWD/build/ethermintd start --pruning=nothing --rpc.unsafe --log_level "main:inf
sleep 1

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

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

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)
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)

echo $ACCT

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

## need to get the private key from the account in order to check this functionality.
cd tests-solidity/suites/basic/ && go get && go run main.go $ACCT
cd tests-solidity/suites/basic/ && go get && sleep 5 && go run main.go $ACCT
2 changes: 1 addition & 1 deletion scripts/integration-test-all.sh
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ start_func() {

start_cli_func() {
echo "starting ethermint node $i in background ..."
"$PWD"/build/ethermintcli rest-server --unlock-key $KEY"$i" --chain-id $CHAINID --trace \
"$PWD"/build/ethermintcli rest-server --unlock-key $KEY"$i" --chain-id $CHAINID --trace --rpc-api="web3,eth,net,personal" \
--laddr "tcp://localhost:$RPC_PORT$i" --node tcp://$IP_ADDR:$NODE_RPC_PORT"$i" \
--home "$DATA_CLI_DIR$i" --read-timeout 30 --write-timeout 30 \
>"$DATA_CLI_DIR"/cli"$i".log 2>&1 & disown
Expand Down
4 changes: 2 additions & 2 deletions scripts/run-solidity-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ fi
chmod +x ./init-test-node.sh
./init-test-node.sh > ethermintd.log &
sleep 5
ethermintcli rest-server --laddr "tcp://localhost:8545" --unlock-key localkey,user1,user2 --chain-id $CHAINID --trace --wsport 8546 > ethermintcli.log &
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 &

cd suites/initializable
yarn test-ethermint
Expand All @@ -45,7 +45,7 @@ exit $ok

./../../init-test-node.sh > ethermintd.log &
sleep 5
ethermintcli rest-server --laddr "tcp://localhost:8545" --unlock-key localkey,user1,user2 --chain-id $CHAINID --trace --wsport 8546 > ethermintcli.log &
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 &

cd ../initializable-buidler
yarn test-ethermint
Expand Down
2 changes: 1 addition & 1 deletion scripts/start.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/bin/sh
ethermintd --home /ethermint/node$ID/ethermintd/ start > ethermintd.log &
sleep 5
ethermintcli rest-server --laddr "tcp://localhost:8545" --chain-id "ethermint-7305661614933169792" --trace > ethermintcli.log &
ethermintcli rest-server --laddr "tcp://localhost:8545" --chain-id "ethermint-7305661614933169792" --trace --rpc-api="web3,eth,net,personal" > ethermintcli.log &
tail -f /dev/null

0 comments on commit d1e22e1

Please sign in to comment.