[cmd] seperate port between different usages

pull/3278/head
Jacky Wang 4 years ago
parent 8850e38dc9
commit 8a250e6dd6
No known key found for this signature in database
GPG Key ID: 1085CE5F4FF5842C
  1. 4
      cmd/harmony/config.go
  2. 6
      cmd/harmony/default.go
  3. 43
      cmd/harmony/flags.go
  4. 28
      cmd/harmony/flags_test.go
  5. 19
      cmd/harmony/main.go
  6. 42
      internal/configs/node/config.go
  7. 24
      internal/configs/node/network.go
  8. 8
      node/rpc.go
  9. 25
      rpc/rpc.go
  10. 4
      scripts/node.sh

@ -20,7 +20,7 @@ type harmonyConfig struct {
General generalConfig
Network networkConfig
P2P p2pConfig
RPC rpcConfig
HTTP httpConfig
WS wsConfig
Consensus consensusConfig
BLSKeys blsConfig
@ -99,7 +99,7 @@ type logContext struct {
Port int
}
type rpcConfig struct {
type httpConfig struct {
Enabled bool
IP string
Port int

@ -23,7 +23,7 @@ var defaultConfig = harmonyConfig{
Port: nodeconfig.DefaultP2PPort,
KeyFile: "./.hmykey",
},
RPC: rpcConfig{
HTTP: httpConfig{
Enabled: true,
IP: "127.0.0.1",
Port: nodeconfig.DefaultRPCPort,
@ -136,3 +136,7 @@ const (
legacyBLSKmsTypeFile = "file"
legacyBLSKmsTypeNone = "none"
)
const (
localEndpoint = "127.0.0.1"
)

@ -278,13 +278,10 @@ var (
Name: "bootnodes",
Usage: "a list of bootnode multiaddress (delimited by ,)",
}
// TODO: separate dns.client from dns.host. Add --dns.host to determine whether start
// dns host, --dns.port / --dns.ip for serving endpoint
dnsZoneFlag = cli.StringFlag{
Name: "dns.zone",
Usage: "use customized peers from the zone for state syncing",
}
// TODO: 9500 as default
dnsPortFlag = cli.IntFlag{
Name: "dns.port",
DefValue: nodeconfig.DefaultDNSPort,
@ -395,23 +392,23 @@ var (
rpcEnabledFlag = cli.BoolFlag{
Name: "http",
Usage: "enable HTTP / RPC requests",
DefValue: defaultConfig.RPC.Enabled,
DefValue: defaultConfig.HTTP.Enabled,
}
rpcIPFlag = cli.StringFlag{
Name: "http.ip",
Usage: "ip address to listen for RPC calls",
DefValue: defaultConfig.RPC.IP,
DefValue: defaultConfig.HTTP.IP,
}
rpcPortFlag = cli.IntFlag{
Name: "http.port",
Usage: "rpc port to listen for RPC calls",
DefValue: defaultConfig.RPC.Port,
Usage: "rpc port to listen for HTTP requests",
DefValue: defaultConfig.HTTP.Port,
}
legacyPublicRPCFlag = cli.BoolFlag{
Name: "public_rpc",
Usage: "Enable Public RPC Access (default: false)",
DefValue: defaultConfig.RPC.Enabled,
Deprecated: "please use --http.ip to specify the ip address to listen",
Usage: "Enable Public HTTP Access (default: false)",
DefValue: defaultConfig.HTTP.Enabled,
Deprecated: "use --http.ip and --ws.ip to specify the ip address to listen. Use 127.0.0.1 to listen local requests.",
}
)
@ -419,19 +416,26 @@ func applyRPCFlags(cmd *cobra.Command, config *harmonyConfig) {
var isRPCSpecified bool
if cli.IsFlagChanged(cmd, rpcIPFlag) {
config.RPC.IP = cli.GetStringFlagValue(cmd, rpcIPFlag)
config.HTTP.IP = cli.GetStringFlagValue(cmd, rpcIPFlag)
isRPCSpecified = true
}
if cli.IsFlagChanged(cmd, rpcPortFlag) {
config.RPC.Port = cli.GetIntFlagValue(cmd, rpcPortFlag)
config.HTTP.Port = cli.GetIntFlagValue(cmd, rpcPortFlag)
isRPCSpecified = true
}
if cli.IsFlagChanged(cmd, rpcEnabledFlag) {
config.RPC.Enabled = cli.GetBoolFlagValue(cmd, rpcEnabledFlag)
config.HTTP.Enabled = cli.GetBoolFlagValue(cmd, rpcEnabledFlag)
} else if isRPCSpecified {
config.RPC.Enabled = true
config.HTTP.Enabled = true
}
if cli.IsFlagChanged(cmd, legacyPublicRPCFlag) {
if !cli.GetBoolFlagValue(cmd, legacyPublicRPCFlag) {
config.HTTP.IP = localEndpoint
config.WS.IP = localEndpoint
}
}
}
@ -1057,7 +1061,7 @@ var (
legacyIPFlag = cli.StringFlag{
Name: "ip",
Usage: "ip of the node",
DefValue: defaultConfig.RPC.IP,
DefValue: defaultConfig.HTTP.IP,
Deprecated: "use --http.ip",
}
legacyWebHookConfigFlag = cli.StringFlag{
@ -1070,18 +1074,17 @@ var (
// Note: this function need to be called before parse other flags
func applyLegacyMiscFlags(cmd *cobra.Command, config *harmonyConfig) {
// TODO: move all port manipulation +500 -3000 logic here
if cli.IsFlagChanged(cmd, legacyPortFlag) {
legacyPort := cli.GetIntFlagValue(cmd, legacyPortFlag)
config.P2P.Port = legacyPort
config.RPC.Port = legacyPort
config.WS.Port = legacyPort
config.HTTP.Port = nodeconfig.GetHTTPPortFromBase(legacyPort)
config.WS.Port = nodeconfig.GetWSPortFromBase(legacyPort)
}
if cli.IsFlagChanged(cmd, legacyIPFlag) {
legacyIP := cli.GetStringFlagValue(cmd, legacyIPFlag)
config.RPC.IP = legacyIP
config.RPC.Enabled = true
config.HTTP.IP = legacyIP
config.HTTP.Enabled = true
config.P2P.IP = legacyIP
config.WS.IP = legacyIP
}

@ -50,15 +50,15 @@ func TestHarmonyFlags(t *testing.T) {
Port: 9000,
KeyFile: defaultConfig.P2P.KeyFile,
},
RPC: rpcConfig{
HTTP: httpConfig{
Enabled: true,
IP: "8.8.8.8",
Port: 9000,
Port: 9500,
},
WS: wsConfig{
Enabled: true,
IP: "8.8.8.8",
Port: 9000,
Port: 9800,
},
Consensus: consensusConfig{
DelayCommit: "0ms",
@ -321,24 +321,24 @@ func TestP2PFlags(t *testing.T) {
func TestRPCFlags(t *testing.T) {
tests := []struct {
args []string
expConfig rpcConfig
expConfig httpConfig
expErr error
}{
{
args: []string{},
expConfig: defaultConfig.RPC,
expConfig: defaultConfig.HTTP,
},
{
args: []string{"--http=false"},
expConfig: rpcConfig{
expConfig: httpConfig{
Enabled: false,
IP: defaultConfig.RPC.IP,
Port: defaultConfig.RPC.Port,
IP: defaultConfig.HTTP.IP,
Port: defaultConfig.HTTP.Port,
},
},
{
args: []string{"--http.ip", "8.8.8.8", "--http.port", "9001"},
expConfig: rpcConfig{
expConfig: httpConfig{
Enabled: true,
IP: "8.8.8.8",
Port: 9001,
@ -346,10 +346,10 @@ func TestRPCFlags(t *testing.T) {
},
{
args: []string{"--ip", "8.8.8.8", "--port", "9001", "--public_rpc"},
expConfig: rpcConfig{
expConfig: httpConfig{
Enabled: true,
IP: "8.8.8.8",
Port: 9001,
Port: 9501,
},
},
}
@ -370,8 +370,8 @@ func TestRPCFlags(t *testing.T) {
continue
}
if !reflect.DeepEqual(hc.RPC, test.expConfig) {
t.Errorf("Test %v: unexpected config: \n\t%+v\n\t%+v", i, hc.RPC, test.expConfig)
if !reflect.DeepEqual(hc.HTTP, test.expConfig) {
t.Errorf("Test %v: unexpected config: \n\t%+v\n\t%+v", i, hc.HTTP, test.expConfig)
}
ts.tearDown()
}
@ -408,7 +408,7 @@ func TestWSFlags(t *testing.T) {
expConfig: wsConfig{
Enabled: true,
IP: "8.8.8.8",
Port: 9001,
Port: 9801,
},
},
}

@ -188,12 +188,8 @@ func setupNodeAndRun(hc harmonyConfig) {
utils.FatalErrMsg(err, "cannot parse bootnode list %#v",
bootNodes)
}
// TODO: seperate use of port rpc / p2p
nodeconfig.GetDefaultConfig().Port = strconv.Itoa(hc.RPC.Port)
nodeconfig.GetDefaultConfig().IP = hc.RPC.IP
nodeconfig.SetShardingSchedule(shard.Schedule)
nodeconfig.SetPublicRPC(true)
nodeconfig.SetVersion(getHarmonyVersion())
nodeconfigSetShardSchedule(hc)
@ -247,6 +243,15 @@ func setupNodeAndRun(hc harmonyConfig) {
}
}()
// Parse RPC config
nodeConfig.RPCServer = nodeconfig.RPCServerConfig{
HTTPEnabled: hc.HTTP.Enabled,
HTTPIp: hc.HTTP.IP,
HTTPPort: hc.HTTP.Port,
WSEnabled: hc.WS.Enabled,
WSIp: hc.WS.IP,
WSPort: hc.WS.Port,
}
if nodeConfig.ShardID != shard.BeaconChainShardID {
utils.Logger().Info().
Uint32("shardID", currentNode.Blockchain().ShardID()).
@ -286,7 +291,7 @@ func setupNodeAndRun(hc harmonyConfig) {
Str("ClientGroupID", nodeConfig.GetClientGroupID().String()).
Str("Role", currentNode.NodeConfig.Role().String()).
Str("multiaddress",
fmt.Sprintf("/ip4/%s/tcp/%d/p2p/%s", hc.RPC.IP, hc.P2P.Port, myHost.GetID().Pretty()),
fmt.Sprintf("/ip4/%s/tcp/%d/p2p/%s", hc.P2P.IP, hc.P2P.Port, myHost.GetID().Pretty()),
).
Msg(startMsg)
@ -296,7 +301,7 @@ func setupNodeAndRun(hc harmonyConfig) {
currentNode.ServiceManagerSetup()
currentNode.RunServices()
if err := currentNode.StartRPC(strconv.Itoa(hc.RPC.Port)); err != nil {
if err := currentNode.StartRPC(); err != nil {
utils.Logger().Warn().
Err(err).
Msg("StartRPC failed")
@ -505,7 +510,7 @@ func setupConsensusAndNode(hc harmonyConfig, nodeConfig *nodeconfig.ConfigType)
} else {
if hc.Network.NetworkType == nodeconfig.Localnet {
epochConfig := shard.Schedule.InstanceForEpoch(ethCommon.Big0)
selfPort := hc.Network.DNSPort
selfPort := hc.P2P.Port
currentNode.SyncingPeerProvider = node.NewLocalSyncingPeerProvider(
6000, uint16(selfPort), epochConfig.NumShards(), uint32(epochConfig.NumNodesPerShard()))
} else {

@ -9,9 +9,8 @@ import (
"strings"
"sync"
"github.com/harmony-one/harmony/crypto/bls"
bls_core "github.com/harmony-one/bls/ffi/go/bls"
"github.com/harmony-one/harmony/crypto/bls"
shardingconfig "github.com/harmony-one/harmony/internal/configs/sharding"
"github.com/harmony-one/harmony/internal/params"
"github.com/harmony-one/harmony/multibls"
@ -64,20 +63,20 @@ const (
)
var version string
var publicRPC bool // enable public RPC access
var peerID peer.ID // PeerID of the node
// ConfigType is the structure of all node related configuration variables
type ConfigType struct {
// The three groupID design, please refer to https://github.com/harmony-one/harmony/blob/master/node/node.md#libp2p-integration
beacon GroupID // the beacon group ID
group GroupID // the group ID of the shard (note: for beacon chain node, the beacon and shard group are the same)
client GroupID // the client group ID of the shard
isClient bool // whether this node is a client node, such as wallet
ShardID uint32 // ShardID of this node; TODO ek – revisit when resharding
role Role // Role of the node
Port string // Port of the node.
IP string // IP of the node.
beacon GroupID // the beacon group ID
group GroupID // the group ID of the shard (note: for beacon chain node, the beacon and shard group are the same)
client GroupID // the client group ID of the shard
isClient bool // whether this node is a client node, such as wallet
ShardID uint32 // ShardID of this node; TODO ek – revisit when resharding
role Role // Role of the node
Port string // Port of the node.
IP string // IP of the node.
RPCServer RPCServerConfig // RPC server port and ip
StringRole string
P2PPriKey p2p_crypto.PrivKey
ConsensusPriKey multibls.PrivateKeys
@ -92,6 +91,17 @@ type ConfigType struct {
}
}
// RPCServerConfig is the config for rpc listen addresses
type RPCServerConfig struct {
HTTPEnabled bool
HTTPIp string
HTTPPort int
WSEnabled bool
WSIp string
WSPort int
}
// configs is a list of node configuration.
// It has at least one configuration.
// The first one is the default, global node configuration
@ -225,16 +235,6 @@ func GetPeerID() peer.ID {
return peerID
}
// SetPublicRPC set the boolean value of public RPC access
func SetPublicRPC(v bool) {
publicRPC = v
}
// GetPublicRPC get the boolean value of public RPC access
func GetPublicRPC() bool {
return publicRPC
}
// ShardingSchedule returns the sharding schedule for this node config.
func (conf *ConfigType) ShardingSchedule() shardingconfig.Schedule {
return conf.shardingSchedule

@ -44,11 +44,17 @@ const (
// TODO: refactor all 9000-3000 = 6000 stuff
DefaultDNSPort = 9000
// DefaultRPCPort is the default rpc port. The actual port used is 9000+500
// TODO: modify this
DefaultRPCPort = 9000
DefaultRPCPort = 9500
// DefaultWSPort is the default port for web socket endpoint. The actual port used is
// TODO: modify this
DefaultWSPort = 9000
DefaultWSPort = 9800
)
const (
// rpcHTTPPortOffset is the port offset for RPC HTTP requests
rpcHTTPPortOffset = 500
// rpcWSPortOffSet is the port offset for RPC websocket requests
rpcWSPortOffSet = 800
)
// GetDefaultBootNodes get the default bootnode with the given network type
@ -89,3 +95,13 @@ func GetDefaultDNSZone(networkType NetworkType) string {
func GetDefaultDNSPort(NetworkType) int {
return DefaultDNSPort
}
// GetHTTPPortFromBase return the HTTP port from base port
func GetHTTPPortFromBase(basePort int) int {
return basePort + rpcHTTPPortOffset
}
// GetWSPortFromBase return the Websocket port from the base port
func GetWSPortFromBase(basePort int) int {
return basePort + rpcWSPortOffSet
}

@ -1,8 +1,6 @@
package node
import (
"strconv"
"github.com/ethereum/go-ethereum/rpc"
"github.com/harmony-one/harmony/core/types"
"github.com/harmony-one/harmony/hmy"
@ -63,7 +61,7 @@ func (node *Node) ReportPlainErrorSink() types.TransactionErrorReports {
}
// StartRPC start RPC service
func (node *Node) StartRPC(nodePort string) error {
func (node *Node) StartRPC() error {
harmony := hmy.New(node, node.TxPool, node.CxPool, node.Consensus.ShardID)
// Gather all the possible APIs to surface
@ -73,9 +71,7 @@ func (node *Node) StartRPC(nodePort string) error {
apis = append(apis, service.APIs()...)
}
port, _ := strconv.Atoi(nodePort)
return hmy_rpc.StartServers(harmony, port, apis)
return hmy_rpc.StartServers(harmony, apis, node.NodeConfig.RPCServer)
}
// StopRPC stop RPC service

@ -63,22 +63,23 @@ func (n Version) Namespace() string {
}
// StartServers starts the http & ws servers
func StartServers(hmy *hmy.Harmony, port int, apis []rpc.API) error {
ip := ""
if !nodeconfig.GetPublicRPC() {
ip = "127.0.0.1"
}
func StartServers(hmy *hmy.Harmony, apis []rpc.API, config nodeconfig.RPCServerConfig) error {
apis = append(apis, getAPIs(hmy)...)
httpEndpoint = fmt.Sprintf("%v:%v", ip, port+HTTPPortOffset)
if err := startHTTP(apis); err != nil {
return err
if config.HTTPEnabled {
httpEndpoint = fmt.Sprintf("%v:%v", config.HTTPIp, config.HTTPPort)
if err := startHTTP(apis); err != nil {
return err
}
}
wsEndpoint = fmt.Sprintf("%v:%v", ip, port+WSPortOffset)
if err := startWS(apis); err != nil {
return err
if config.WSEnabled {
wsEndpoint = fmt.Sprintf("%v:%v", config.WSIp, config.WSPort)
if err := startWS(apis); err != nil {
return err
}
}
return nil
}

@ -782,6 +782,10 @@ do
args+=(
--public_rpc
)
else
args+=(
--public_rpc=false
)
fi
if [ ! -z "${pprof}" ]; then
args+=(

Loading…
Cancel
Save