add cmd flag for rpc api modules (#821)

* add cmd flag for rpc api modules
* fix test-contract race condition by sleeping
This commit is contained in:
Daniel Choi 2021-02-26 13:52:33 -08:00 committed by GitHub
parent 99112e1cbd
commit d1e22e1544
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 68 additions and 40 deletions

View File

@ -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
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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