diff --git a/rpc/apis.go b/rpc/apis.go index b38dcaa43..e2e006757 100644 --- a/rpc/apis.go +++ b/rpc/apis.go @@ -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 } diff --git a/rpc/cmd.go b/rpc/cmd.go index bfd4663b3..f2362dba2 100644 --- a/rpc/cmd.go +++ b/rpc/cmd.go @@ -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" @@ -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)") diff --git a/rpc/config.go b/rpc/config.go index f3fc92051..f701a1434 100644 --- a/rpc/config.go +++ b/rpc/config.go @@ -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 diff --git a/scripts/contract-test.sh b/scripts/contract-test.sh index 359bc5823..4c9ad194c 100644 --- a/scripts/contract-test.sh +++ b/scripts/contract-test.sh @@ -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 @@ -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 diff --git a/scripts/integration-test-all.sh b/scripts/integration-test-all.sh index c148ab545..ae3fb734c 100755 --- a/scripts/integration-test-all.sh +++ b/scripts/integration-test-all.sh @@ -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 diff --git a/scripts/run-solidity-tests.sh b/scripts/run-solidity-tests.sh index c56afd1b6..a6180b202 100755 --- a/scripts/run-solidity-tests.sh +++ b/scripts/run-solidity-tests.sh @@ -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 @@ -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 diff --git a/scripts/start.sh b/scripts/start.sh index da1320edc..484ab2f64 100644 --- a/scripts/start.sh +++ b/scripts/start.sh @@ -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 \ No newline at end of file