Skip to content

Commit

Permalink
Merge pull request #3853 from LuttyYang/add_auth_api
Browse files Browse the repository at this point in the history
Add RPC auth port for some auth api
  • Loading branch information
jhd2best authored Aug 29, 2021
2 parents f741ca8 + 52d55e9 commit dece072
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 6 deletions.
9 changes: 9 additions & 0 deletions cmd/harmony/config_migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,4 +192,13 @@ func init() {
confTree.Set("Version", "2.2.0")
return confTree
}

migrations["2.2.0"] = func(confTree *toml.Tree) *toml.Tree {
if confTree.Get("HTTP.AuthPort") == nil {
confTree.Set("HTTP.AuthPort", defaultConfig.HTTP.AuthPort)
}

confTree.Set("Version", "2.3.0")
return confTree
}
}
3 changes: 2 additions & 1 deletion cmd/harmony/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
nodeconfig "github.com/harmony-one/harmony/internal/configs/node"
)

const tomlConfigVersion = "2.2.0"
const tomlConfigVersion = "2.3.0"

const (
defNetworkType = nodeconfig.Mainnet
Expand Down Expand Up @@ -34,6 +34,7 @@ var defaultConfig = harmonyconfig.HarmonyConfig{
RosettaEnabled: false,
IP: "127.0.0.1",
Port: nodeconfig.DefaultRPCPort,
AuthPort: nodeconfig.DefaultAuthRPCPort,
RosettaPort: nodeconfig.DefaultRosettaPort,
},
WS: harmonyconfig.WsConfig{
Expand Down
12 changes: 12 additions & 0 deletions cmd/harmony/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ var (
httpRosettaEnabledFlag,
httpIPFlag,
httpPortFlag,
httpAuthPortFlag,
httpRosettaPortFlag,
}

Expand Down Expand Up @@ -568,6 +569,11 @@ var (
Usage: "rpc port to listen for HTTP requests",
DefValue: defaultConfig.HTTP.Port,
}
httpAuthPortFlag = cli.IntFlag{
Name: "http.auth-port",
Usage: "rpc port to listen for auth HTTP requests",
DefValue: defaultConfig.HTTP.AuthPort,
}
httpRosettaEnabledFlag = cli.BoolFlag{
Name: "http.rosetta",
Usage: "enable HTTP / Rosetta requests",
Expand All @@ -593,6 +599,11 @@ func applyHTTPFlags(cmd *cobra.Command, config *harmonyconfig.HarmonyConfig) {
isRPCSpecified = true
}

if cli.IsFlagChanged(cmd, httpAuthPortFlag) {
config.HTTP.AuthPort = cli.GetIntFlagValue(cmd, httpAuthPortFlag)
isRPCSpecified = true
}

if cli.IsFlagChanged(cmd, httpRosettaPortFlag) {
config.HTTP.RosettaPort = cli.GetIntFlagValue(cmd, httpRosettaPortFlag)
isRosettaSpecified = true
Expand Down Expand Up @@ -1336,6 +1347,7 @@ func applyLegacyMiscFlags(cmd *cobra.Command, config *harmonyconfig.HarmonyConfi
legacyPort := cli.GetIntFlagValue(cmd, legacyPortFlag)
config.P2P.Port = legacyPort
config.HTTP.Port = nodeconfig.GetRPCHTTPPortFromBase(legacyPort)
config.HTTP.AuthPort = nodeconfig.GetRPCAuthHTTPPortFromBase(legacyPort)
config.HTTP.RosettaPort = nodeconfig.GetRosettaHTTPPortFromBase(legacyPort)
config.WS.Port = nodeconfig.GetWSPortFromBase(legacyPort)

Expand Down
17 changes: 17 additions & 0 deletions cmd/harmony/flags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ func TestHarmonyFlags(t *testing.T) {
Enabled: true,
IP: "127.0.0.1",
Port: 9500,
AuthPort: 9501,
RosettaEnabled: false,
RosettaPort: 9700,
},
Expand Down Expand Up @@ -424,6 +425,7 @@ func TestRPCFlags(t *testing.T) {
RosettaEnabled: false,
IP: defaultConfig.HTTP.IP,
Port: defaultConfig.HTTP.Port,
AuthPort: defaultConfig.HTTP.AuthPort,
RosettaPort: defaultConfig.HTTP.RosettaPort,
},
},
Expand All @@ -434,6 +436,18 @@ func TestRPCFlags(t *testing.T) {
RosettaEnabled: false,
IP: "8.8.8.8",
Port: 9001,
AuthPort: defaultConfig.HTTP.AuthPort,
RosettaPort: defaultConfig.HTTP.RosettaPort,
},
},
{
args: []string{"--http.ip", "8.8.8.8", "--http.auth-port", "9001"},
expConfig: harmonyconfig.HttpConfig{
Enabled: true,
RosettaEnabled: false,
IP: "8.8.8.8",
Port: defaultConfig.HTTP.Port,
AuthPort: 9001,
RosettaPort: defaultConfig.HTTP.RosettaPort,
},
},
Expand All @@ -444,6 +458,7 @@ func TestRPCFlags(t *testing.T) {
RosettaEnabled: true,
IP: "8.8.8.8",
Port: 9001,
AuthPort: defaultConfig.HTTP.AuthPort,
RosettaPort: 10001,
},
},
Expand All @@ -454,6 +469,7 @@ func TestRPCFlags(t *testing.T) {
RosettaEnabled: true,
IP: "8.8.8.8",
Port: defaultConfig.HTTP.Port,
AuthPort: defaultConfig.HTTP.AuthPort,
RosettaPort: 10001,
},
},
Expand All @@ -464,6 +480,7 @@ func TestRPCFlags(t *testing.T) {
RosettaEnabled: false,
IP: nodeconfig.DefaultPublicListenIP,
Port: 9501,
AuthPort: 9502,
RosettaPort: 9701,
},
},
Expand Down
1 change: 1 addition & 0 deletions cmd/harmony/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,7 @@ func setupNodeAndRun(hc harmonyconfig.HarmonyConfig) {
HTTPEnabled: hc.HTTP.Enabled,
HTTPIp: hc.HTTP.IP,
HTTPPort: hc.HTTP.Port,
HTTPAuthPort: hc.HTTP.AuthPort,
WSEnabled: hc.WS.Enabled,
WSIp: hc.WS.IP,
WSPort: hc.WS.Port,
Expand Down
1 change: 1 addition & 0 deletions internal/configs/harmony/harmony.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ type HttpConfig struct {
Enabled bool
IP string
Port int
AuthPort int
RosettaEnabled bool
RosettaPort int
}
Expand Down
7 changes: 4 additions & 3 deletions internal/configs/node/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,10 @@ type ConfigType struct {

// RPCServerConfig is the config for rpc listen addresses
type RPCServerConfig struct {
HTTPEnabled bool
HTTPIp string
HTTPPort int
HTTPEnabled bool
HTTPIp string
HTTPPort int
HTTPAuthPort int

WSEnabled bool
WSIp string
Expand Down
10 changes: 10 additions & 0 deletions internal/configs/node/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ const (
DefaultDNSPort = 6000
// DefaultRPCPort is the default rpc port. The actual port used is 9000+500
DefaultRPCPort = 9500
// DefaultAuthRPCPort is the default rpc auth port. The actual port used is 9000+501
DefaultAuthRPCPort = 9501
// DefaultRosettaPort is the default rosetta port. The actual port used is 9000+700
DefaultRosettaPort = 9700
// DefaultWSPort is the default port for web socket endpoint. The actual port used is
Expand All @@ -67,6 +69,9 @@ const (
// rpcHTTPPortOffset is the port offset for RPC HTTP requests
rpcHTTPPortOffset = 500

// rpcHTTPAuthPortOffset is the port offset for RPC Auth HTTP requests
rpcHTTPAuthPortOffset = 501

// rpcHTTPPortOffset is the port offset for rosetta HTTP requests
rosettaHTTPPortOffset = 700

Expand Down Expand Up @@ -123,6 +128,11 @@ func GetRPCHTTPPortFromBase(basePort int) int {
return basePort + rpcHTTPPortOffset
}

// GetRPCAuthHTTPPortFromBase return the rpc HTTP port from base port
func GetRPCAuthHTTPPortFromBase(basePort int) int {
return basePort + rpcHTTPAuthPortOffset
}

// GetRosettaHTTPPortFromBase return the rosetta HTTP port from base port
func GetRosettaHTTPPortFromBase(basePort int) int {
return basePort + rosettaHTTPPortOffset
Expand Down
35 changes: 33 additions & 2 deletions rpc/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ var (
wsListener net.Listener
wsHandler *rpc.Server
httpEndpoint = ""
httpAuthEndpoint = ""
wsEndpoint = ""
httpVirtualHosts = []string{"*"}
httpTimeouts = rpc.DefaultHTTPTimeouts
Expand All @@ -71,12 +72,18 @@ func (n Version) Namespace() string {
// StartServers starts the http & ws servers
func StartServers(hmy *hmy.Harmony, apis []rpc.API, config nodeconfig.RPCServerConfig) error {
apis = append(apis, getAPIs(hmy, config.DebugEnabled, config.RateLimiterEnabled, config.RequestsPerSecond)...)
authApis := getAuthAPIs(hmy, config.DebugEnabled, config.RateLimiterEnabled, config.RequestsPerSecond)

if config.HTTPEnabled {
httpEndpoint = fmt.Sprintf("%v:%v", config.HTTPIp, config.HTTPPort)
if err := startHTTP(apis); err != nil {
return err
}

httpAuthEndpoint = fmt.Sprintf("%v:%v", config.HTTPIp, config.HTTPAuthPort)
if err := startAuthHTTP(authApis); err != nil {
return err
}
}

if config.WSEnabled {
Expand Down Expand Up @@ -120,6 +127,13 @@ func StopServers() error {
return nil
}

func getAuthAPIs(hmy *hmy.Harmony, debugEnable bool, rateLimiterEnable bool, ratelimit int) []rpc.API {
return []rpc.API{
NewPublicTraceAPI(hmy, Debug), // Debug version means geth trace rpc
NewPublicTraceAPI(hmy, Trace), // Trace version means parity trace rpc
}
}

// getAPIs returns all the API methods for the RPC interface
func getAPIs(hmy *hmy.Harmony, debugEnable bool, rateLimiterEnable bool, ratelimit int) []rpc.API {
publicAPIs := []rpc.API{
Expand All @@ -141,10 +155,10 @@ func getAPIs(hmy *hmy.Harmony, debugEnable bool, rateLimiterEnable bool, ratelim
NewPublicPoolAPI(hmy, Eth),
NewPublicStakingAPI(hmy, V1),
NewPublicStakingAPI(hmy, V2),
NewPublicTraceAPI(hmy, Debug), // Debug version means geth trace rpc
NewPublicTraceAPI(hmy, Trace), // Trace version means parity trace rpc
NewPublicDebugAPI(hmy, V1),
NewPublicDebugAPI(hmy, V2),
NewPublicTraceAPI(hmy, Debug), // Debug version means geth trace rpc
NewPublicTraceAPI(hmy, Trace), // Trace version means parity trace rpc
// Legacy methods (subject to removal)
v1.NewPublicLegacyAPI(hmy, "hmy"),
eth.NewPublicEthService(hmy, "eth"),
Expand Down Expand Up @@ -179,6 +193,23 @@ func startHTTP(apis []rpc.API) (err error) {
return nil
}

func startAuthHTTP(apis []rpc.API) (err error) {
httpListener, httpHandler, err = rpc.StartHTTPEndpoint(
httpAuthEndpoint, apis, HTTPModules, httpOrigins, httpVirtualHosts, httpTimeouts,
)
if err != nil {
return err
}

utils.Logger().Info().
Str("url", fmt.Sprintf("http://%s", httpAuthEndpoint)).
Str("cors", strings.Join(httpOrigins, ",")).
Str("vhosts", strings.Join(httpVirtualHosts, ",")).
Msg("HTTP endpoint opened")
fmt.Printf("Started Auth-RPC server at: %v\n", httpAuthEndpoint)
return nil
}

func startWS(apis []rpc.API) (err error) {
wsListener, wsHandler, err = rpc.StartWSEndpoint(wsEndpoint, apis, WSModules, wsOrigins, true)
if err != nil {
Expand Down

0 comments on commit dece072

Please sign in to comment.