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:
		
							parent
							
								
									99112e1cbd
								
							
						
					
					
						commit
						d1e22e1544
					
				
							
								
								
									
										35
									
								
								rpc/apis.go
									
									
									
									
									
								
							
							
						
						
									
										35
									
								
								rpc/apis.go
									
									
									
									
									
								
							| @ -21,46 +21,67 @@ const ( | |||||||
| 	EthNamespace      = "eth" | 	EthNamespace      = "eth" | ||||||
| 	PersonalNamespace = "personal" | 	PersonalNamespace = "personal" | ||||||
| 	NetNamespace      = "net" | 	NetNamespace      = "net" | ||||||
|  | 	flagRPCAPI        = "rpc-api" | ||||||
| 
 | 
 | ||||||
| 	apiVersion = "1.0" | 	apiVersion = "1.0" | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
| // GetAPIs returns the list of all APIs from the Ethereum namespaces
 | // 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) | 	nonceLock := new(rpctypes.AddrLocker) | ||||||
| 	backend := backend.New(clientCtx) | 	backend := backend.New(clientCtx) | ||||||
| 	ethAPI := eth.NewAPI(clientCtx, backend, nonceLock, keys...) | 	ethAPI := eth.NewAPI(clientCtx, backend, nonceLock, keys...) | ||||||
| 
 | 
 | ||||||
| 	return []rpc.API{ | 	var apis []rpc.API | ||||||
| 		{ | 
 | ||||||
|  | 	for _, api := range selectedApis { | ||||||
|  | 		switch api { | ||||||
|  | 		case Web3Namespace: | ||||||
|  | 			apis = append(apis, | ||||||
|  | 				rpc.API{ | ||||||
| 					Namespace: Web3Namespace, | 					Namespace: Web3Namespace, | ||||||
| 					Version:   apiVersion, | 					Version:   apiVersion, | ||||||
| 					Service:   web3.NewAPI(), | 					Service:   web3.NewAPI(), | ||||||
| 					Public:    true, | 					Public:    true, | ||||||
| 				}, | 				}, | ||||||
| 		{ | 			) | ||||||
|  | 		case EthNamespace: | ||||||
|  | 			apis = append(apis, | ||||||
|  | 				rpc.API{ | ||||||
| 					Namespace: EthNamespace, | 					Namespace: EthNamespace, | ||||||
| 					Version:   apiVersion, | 					Version:   apiVersion, | ||||||
| 					Service:   ethAPI, | 					Service:   ethAPI, | ||||||
| 					Public:    true, | 					Public:    true, | ||||||
| 				}, | 				}, | ||||||
| 		{ | 			) | ||||||
|  | 			apis = append(apis, | ||||||
|  | 				rpc.API{ | ||||||
| 					Namespace: EthNamespace, | 					Namespace: EthNamespace, | ||||||
| 					Version:   apiVersion, | 					Version:   apiVersion, | ||||||
| 					Service:   filters.NewAPI(clientCtx, backend), | 					Service:   filters.NewAPI(clientCtx, backend), | ||||||
| 					Public:    true, | 					Public:    true, | ||||||
| 				}, | 				}, | ||||||
| 		{ | 			) | ||||||
|  | 		case PersonalNamespace: | ||||||
|  | 			apis = append(apis, | ||||||
|  | 				rpc.API{ | ||||||
| 					Namespace: PersonalNamespace, | 					Namespace: PersonalNamespace, | ||||||
| 					Version:   apiVersion, | 					Version:   apiVersion, | ||||||
| 					Service:   personal.NewAPI(ethAPI), | 					Service:   personal.NewAPI(ethAPI), | ||||||
| 					Public:    false, | 					Public:    false, | ||||||
| 				}, | 				}, | ||||||
| 		{ | 			) | ||||||
|  | 		case NetNamespace: | ||||||
|  | 			apis = append(apis, | ||||||
|  | 				rpc.API{ | ||||||
| 					Namespace: NetNamespace, | 					Namespace: NetNamespace, | ||||||
| 					Version:   apiVersion, | 					Version:   apiVersion, | ||||||
| 					Service:   net.NewAPI(clientCtx), | 					Service:   net.NewAPI(clientCtx), | ||||||
| 					Public:    true, | 					Public:    true, | ||||||
| 				}, | 				}, | ||||||
|  | 			) | ||||||
| 		} | 		} | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	return apis | ||||||
| } | } | ||||||
|  | |||||||
| @ -1,6 +1,8 @@ | |||||||
| package rpc | package rpc | ||||||
| 
 | 
 | ||||||
| import ( | import ( | ||||||
|  | 	"fmt" | ||||||
|  | 
 | ||||||
| 	"github.com/cosmos/cosmos-sdk/client/flags" | 	"github.com/cosmos/cosmos-sdk/client/flags" | ||||||
| 	"github.com/cosmos/cosmos-sdk/client/lcd" | 	"github.com/cosmos/cosmos-sdk/client/lcd" | ||||||
| 	"github.com/cosmos/cosmos-sdk/codec" | 	"github.com/cosmos/cosmos-sdk/codec" | ||||||
| @ -11,6 +13,7 @@ import ( | |||||||
| // Cosmos rest-server endpoints
 | // Cosmos rest-server endpoints
 | ||||||
| func ServeCmd(cdc *codec.Codec) *cobra.Command { | func ServeCmd(cdc *codec.Codec) *cobra.Command { | ||||||
| 	cmd := lcd.ServeCommand(cdc, RegisterRoutes) | 	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(flagUnlockKey, "", "Select a key to unlock on the RPC server") | ||||||
| 	cmd.Flags().String(flagWebsocket, "8546", "websocket port to listen to") | 	cmd.Flags().String(flagWebsocket, "8546", "websocket port to listen to") | ||||||
| 	cmd.Flags().StringP(flags.FlagBroadcastMode, "b", flags.BroadcastSync, "Transaction broadcasting mode (sync|async|block)") | 	cmd.Flags().StringP(flags.FlagBroadcastMode, "b", flags.BroadcastSync, "Transaction broadcasting mode (sync|async|block)") | ||||||
|  | |||||||
| @ -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
 | 	// Register all the APIs exposed by the namespace services
 | ||||||
| 	// TODO: handle allowlist and private APIs
 | 	// TODO: handle allowlist and private APIs
 | ||||||
|  | |||||||
| @ -49,13 +49,13 @@ $PWD/build/ethermintd start --pruning=nothing --rpc.unsafe --log_level "main:inf | |||||||
| sleep 1 | sleep 1 | ||||||
| 
 | 
 | ||||||
| # Start the rest server with unlocked key in background and log to file | # 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 | 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/*.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 | 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 | echo $ACCT | ||||||
| 
 | 
 | ||||||
| @ -66,4 +66,4 @@ PRIVKEY="$("$PWD"/build/ethermintcli keys unsafe-export-eth-key $KEY)" | |||||||
| echo $PRIVKEY | echo $PRIVKEY | ||||||
| 
 | 
 | ||||||
| ## need to get the private key from the account in order to check this functionality. | ## 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 | ||||||
|  | |||||||
| @ -121,7 +121,7 @@ start_func() { | |||||||
| 
 | 
 | ||||||
| start_cli_func() { | start_cli_func() { | ||||||
|     echo "starting ethermint node $i in background ..." |     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" \ |     --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 \ |     --home "$DATA_CLI_DIR$i" --read-timeout 30 --write-timeout 30 \ | ||||||
|     >"$DATA_CLI_DIR"/cli"$i".log 2>&1 & disown |     >"$DATA_CLI_DIR"/cli"$i".log 2>&1 & disown | ||||||
|  | |||||||
| @ -24,7 +24,7 @@ fi | |||||||
| chmod +x ./init-test-node.sh | chmod +x ./init-test-node.sh | ||||||
| ./init-test-node.sh > ethermintd.log & | ./init-test-node.sh > ethermintd.log & | ||||||
| sleep 5 | 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 | cd suites/initializable | ||||||
| yarn test-ethermint | yarn test-ethermint | ||||||
| @ -45,7 +45,7 @@ exit $ok | |||||||
| 
 | 
 | ||||||
| ./../../init-test-node.sh > ethermintd.log & | ./../../init-test-node.sh > ethermintd.log & | ||||||
| sleep 5 | 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 | cd ../initializable-buidler | ||||||
| yarn test-ethermint | yarn test-ethermint | ||||||
|  | |||||||
| @ -1,5 +1,5 @@ | |||||||
| #!/bin/sh | #!/bin/sh | ||||||
| ethermintd --home /ethermint/node$ID/ethermintd/ start > ethermintd.log & | ethermintd --home /ethermint/node$ID/ethermintd/ start > ethermintd.log & | ||||||
| sleep 5 | 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 | tail -f /dev/null | ||||||
		Loading…
	
		Reference in New Issue
	
	Block a user