Skip to content

Commit

Permalink
Add RPC for Boot Nodes (#4735)
Browse files Browse the repository at this point in the history
* initial commit for boot rpc feature
* add start rpc to kick off boot node rpc server
* fix boot node tests, goimports
* remove unused configs and functions from node/boot, cleanup boot metrics, improve boot node test
* fix the issue of get default harmony config for boot node
* set boot node version, fix dependencies name
* goimports, fix boot format
* remove downloader from hmy_boot
* remove tracer from hmy_boot
* remove unused RPCs from hmy_boot
* remove unused functions from hmy_boot
* fix the issue of nil config for new node/boot
* fix getBootAPIs config issue, update boot log tag
* fix boot node invalid address issue for local net
* fix version flag issue, move version meta data to main
* fix boot version info
* disable boot rpc
* disable Start HHTP and WS endpoints to find the build issue
* disable boot apis
* remove RpcMethodFilter for boot servers
* remove StartServers for boot servers
* remove StartServers for boot servers
* enable RpcMethodFilter
* check build issue by uncommenting a part of code
* uncomment startServers for boot host
* add http and ws ports flags for boot node RPCs
* enable http server for boot
* enable ws server for boot
* remove unused functions for boot services, add boot node descriptions, improve boot node comments
* update HTTP modules and ws modules array for boot rpc
* todo for enable services
* goimports for cmd/main
* update version year for boot node rpc
* remove unnecessary configs from boot node RPC configs
* remove unnecessary configs from boot node RPC configs
* remove group configs from boot node RPC configs
* fix the boot node RPC config test
* remove a few more unnecessary configs from boot node RPC configs
* remove shard configs from boot RPC configs
* remove network from boot config
* remove auth ports from boot api
* remove transaction error from bootnode api
* remove extra fields from boot node tests
* fix boot node test
* remove a few extra functions and tests from internal config for bootn ode
* separate structure for boot node meta data
* refactor peer info in boot api
* set peer id for node configs in boot node api
* remove shard-id from boot node metadata, add network to boot node meta data
* fix goimports
  • Loading branch information
GheisMohammadi authored Sep 11, 2024
1 parent 952814a commit 6d9b09d
Show file tree
Hide file tree
Showing 101 changed files with 1,478 additions and 251 deletions.
51 changes: 47 additions & 4 deletions cmd/bootnode/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ import (
"time"

"github.com/ethereum/go-ethereum/log"
//cmdharmony "github.com/harmony-one/harmony/cmd/harmony"
harmonyConfigs "github.com/harmony-one/harmony/cmd/config"
nodeConfigs "github.com/harmony-one/harmony/internal/configs/node"
"github.com/harmony-one/harmony/internal/utils"
bootnode "github.com/harmony-one/harmony/node/boot"
"github.com/harmony-one/harmony/p2p"
net "github.com/libp2p/go-libp2p/core/network"
ma "github.com/multiformats/go-multiaddr"
Expand Down Expand Up @@ -87,8 +91,7 @@ var (
)

func printVersion(me string) {
fmt.Fprintf(os.Stderr, "Harmony (C) 2019. %v, version %v-%v (%v %v)\n", path.Base(me), version, commit, builtBy, builtAt)
os.Exit(0)
fmt.Fprintf(os.Stderr, "Harmony (C) 2024. %v, version %v-%v (%v %v)\n", path.Base(me), version, commit, builtBy, builtAt)
}

func main() {
Expand All @@ -97,6 +100,8 @@ func main() {

ip := flag.String("ip", "127.0.0.1", "IP of the node")
port := flag.String("port", "9876", "port of the node.")
httpPort := flag.Int("rpc_http_port", 9500, "port of the rpc http")
wsPort := flag.Int("rpc_ws_port", 9800, "port of the rpc ws")
console := flag.Bool("console_only", false, "Output to console only")
logFolder := flag.String("log_folder", "latest", "the folder collecting the logs of this execution")
logMaxSize := flag.Int("log_max_size", 100, "the max size in megabytes of the log file before it gets rotated")
Expand All @@ -113,7 +118,7 @@ func main() {
muxer := flag.String("muxer", "mplex, yamux", "protocol muxer to mux per-protocol streams (mplex, yamux)")
userAgent := flag.String("user_agent", defUserAgent, "explicitly set the user-agent, so we can differentiate from other Go libp2p users")
noRelay := flag.Bool("no_relay", true, "no relay services, direct connections between peers only")

networkType := flag.String("network", "mainnet", "network type (mainnet, testnet, pangaea, partner, stressnet, devnet, localnet)")
pprof := flag.Bool("pprof", false, "enabled pprof")
pprofAddr := flag.String("pprof.addr", "127.0.0.1:6060", "http pprof address")
//keyFile := flag.String("pprof.profile.names", "", "the private key file of the bootnode")
Expand All @@ -124,12 +129,13 @@ func main() {

if *versionFlag {
printVersion(os.Args[0])
os.Exit(0)
}

// Logging setup
utils.SetLogContext(*port, *ip)
utils.SetLogVerbosity(log.Lvl(*verbosity))
if *console != true {
if !*console {
utils.AddLogFile(fmt.Sprintf("%v/bootnode-%v-%v.log", *logFolder, *ip, *port), *logMaxSize, *logRotateCount, *logRotateMaxAge)
}

Expand Down Expand Up @@ -165,8 +171,19 @@ func main() {
fmt.Sprintf("/ip4/%s/tcp/%s/p2p/%s\n", *ip, *port, host.GetID().String()),
)

nt := nodeConfigs.NetworkType(*networkType)
nodeConfigs.SetNetworkType(nt)
harmonyConfigs.VersionMetaData = append(harmonyConfigs.VersionMetaData, path.Base(os.Args[0]), version, commit, builtBy, builtAt)
nodeConfigs.SetVersion(harmonyConfigs.GetHarmonyVersion())
nodeConfigs.SetPeerID(host.GetID())
hc := harmonyConfigs.GetDefaultConfigCopy()

host.Start()

utils.Logger().Info().
Interface("network", nt).
Msg("boot node host started")

if *logConn {
host.GetP2PHost().Network().Notify(NewConnLogger(utils.GetLogInstance()))
}
Expand All @@ -176,5 +193,31 @@ func main() {
http.ListenAndServe(*pprofAddr, nil)
}

currentBootNode := bootnode.New(host, &hc)
rpcConfigs := currentBootNode.GetRPCServerConfig()
rpcConfigs.HTTPPort = *httpPort
rpcConfigs.WSPort = *wsPort

// TODO: enable boot services
/*
if err := currentBootNode.StartServices(); err != nil {
fmt.Fprint(os.Stderr, err.Error())
os.Exit(-1)
}
*/

if err := currentBootNode.StartRPC(); err != nil {
utils.Logger().Error().
Err(err).
Msg("StartRPC failed")
}

utils.Logger().Info().
Interface("network", nt).
Interface("ip", currentBootNode.SelfPeer.IP).
Interface("port", currentBootNode.SelfPeer.Port).
Interface("PeerID", currentBootNode.SelfPeer.PeerID).
Msg("boot node RPC started")

select {}
}
131 changes: 123 additions & 8 deletions cmd/harmony/config.go → cmd/config/config.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package main
package config

import (
"errors"
"fmt"
"math/rand"
"os"
"strings"
"time"
Expand All @@ -19,7 +20,7 @@ func validateHarmonyConfig(config harmonyconfig.HarmonyConfig) error {
var accepts []string

nodeType := config.General.NodeType
accepts = []string{nodeTypeValidator, nodeTypeExplorer}
accepts = []string{NodeTypeValidator, NodeTypeExplorer}
if err := checkStringAccepted("--run", nodeType, accepts); err != nil {
return err
}
Expand All @@ -42,7 +43,7 @@ func validateHarmonyConfig(config harmonyconfig.HarmonyConfig) error {
return err
}

if config.General.NodeType == nodeTypeExplorer && config.General.ShardID < 0 {
if config.General.NodeType == NodeTypeExplorer && config.General.ShardID < 0 {
return errors.New("flag --run.shard must be specified for explorer node")
}

Expand Down Expand Up @@ -76,7 +77,7 @@ func checkStringAccepted(flag string, val string, accepts []string) error {
return fmt.Errorf("unknown arg for %s: %s (%v)", flag, val, acceptsStr)
}

func getDefaultDNSSyncConfig(nt nodeconfig.NetworkType) harmonyconfig.DnsSync {
func GetDefaultDNSSyncConfig(nt nodeconfig.NetworkType) harmonyconfig.DnsSync {
zone := nodeconfig.GetDefaultDNSZone(nt)
port := nodeconfig.GetDefaultDNSPort(nt)
dnsSync := harmonyconfig.DnsSync{
Expand All @@ -101,7 +102,7 @@ func getDefaultDNSSyncConfig(nt nodeconfig.NetworkType) harmonyconfig.DnsSync {
return dnsSync
}

func getDefaultNetworkConfig(nt nodeconfig.NetworkType) harmonyconfig.NetworkConfig {
func GetDefaultNetworkConfig(nt nodeconfig.NetworkType) harmonyconfig.NetworkConfig {
bn := nodeconfig.GetDefaultBootNodes(nt)
return harmonyconfig.NetworkConfig{
NetworkType: string(nt),
Expand Down Expand Up @@ -130,7 +131,7 @@ func parseNetworkType(nt string) nodeconfig.NetworkType {
}
}

func getDefaultSyncConfig(nt nodeconfig.NetworkType) harmonyconfig.SyncConfig {
func GetDefaultSyncConfig(nt nodeconfig.NetworkType) harmonyconfig.SyncConfig {
switch nt {
case nodeconfig.Mainnet:
return defaultMainnetSyncConfig
Expand All @@ -145,7 +146,7 @@ func getDefaultSyncConfig(nt nodeconfig.NetworkType) harmonyconfig.SyncConfig {
}
}

func getDefaultCacheConfig(nt nodeconfig.NetworkType) harmonyconfig.CacheConfig {
func GetDefaultCacheConfig(nt nodeconfig.NetworkType) harmonyconfig.CacheConfig {
cacheConfig := harmonyconfig.CacheConfig{
Disabled: defaultCacheConfig.Disabled,
TrieNodeLimit: defaultCacheConfig.TrieNodeLimit,
Expand Down Expand Up @@ -203,7 +204,7 @@ var updateConfigCmd = &cobra.Command{

func dumpConfig(cmd *cobra.Command, args []string) {
nt := getNetworkType(cmd)
config := getDefaultHmyConfigCopy(nt)
config := GetDefaultHmyConfigCopy(nt)

if err := writeHarmonyConfigToFile(config, args[0]); err != nil {
fmt.Println(err)
Expand Down Expand Up @@ -296,3 +297,117 @@ func writeHarmonyConfigToFile(config harmonyconfig.HarmonyConfig, file string) e
}
return os.WriteFile(file, b, 0644)
}

var configFlag = cli.StringFlag{
Name: "config",
Usage: "load node config from the config toml file.",
Shorthand: "c",
DefValue: "",
}

func applyRootFlags(cmd *cobra.Command, config *harmonyconfig.HarmonyConfig) {
// Misc flags shall be applied first since legacy ip / port is overwritten
// by new ip / port flags
applyLegacyMiscFlags(cmd, config)
applyGeneralFlags(cmd, config)
applyNetworkFlags(cmd, config)
applyDNSSyncFlags(cmd, config)
applyP2PFlags(cmd, config)
applyHTTPFlags(cmd, config)
applyWSFlags(cmd, config)
applyRPCOptFlags(cmd, config)
applyBLSFlags(cmd, config)
applyConsensusFlags(cmd, config)
applyTxPoolFlags(cmd, config)
applyPprofFlags(cmd, config)
applyLogFlags(cmd, config)
applySysFlags(cmd, config)
applyDevnetFlags(cmd, config)
applyRevertFlags(cmd, config)
applyPreimageFlags(cmd, config)
applyPrometheusFlags(cmd, config)
applySyncFlags(cmd, config)
applyShardDataFlags(cmd, config)
applyGPOFlags(cmd, config)
applyCacheFlags(cmd, config)
}

func registerRootCmdFlags(rootCmd *cobra.Command) error {
flags := getRootFlags()

return cli.RegisterFlags(rootCmd, flags)
}

func GetHarmonyConfig(cmd *cobra.Command) (harmonyconfig.HarmonyConfig, error) {
var (
config harmonyconfig.HarmonyConfig
err error
migratedFrom string
configFile string
isUsingDefault bool
)
if cli.IsFlagChanged(cmd, configFlag) {
configFile = cli.GetStringFlagValue(cmd, configFlag)
config, migratedFrom, err = loadHarmonyConfig(configFile)
} else {
nt := getNetworkType(cmd)
config = GetDefaultHmyConfigCopy(nt)
isUsingDefault = true
}
if err != nil {
return harmonyconfig.HarmonyConfig{}, err
}
if migratedFrom != defaultConfig.Version && !isUsingDefault {
fmt.Printf("Old config version detected %s\n",
migratedFrom)
stat, _ := os.Stdin.Stat()
// Ask to update if only using terminal
if stat.Mode()&os.ModeCharDevice != 0 {
if promptConfigUpdate() {
err := updateConfigFile(configFile)
if err != nil {
fmt.Printf("Could not update config - %s", err.Error())
fmt.Println("Update config manually with `./harmony config update [config_file]`")
}
}

} else {
fmt.Println("Update saved config with `./harmony config update [config_file]`")
}
}

applyRootFlags(cmd, &config)

if err := validateHarmonyConfig(config); err != nil {
return harmonyconfig.HarmonyConfig{}, err
}
sanityFixHarmonyConfig(&config)
return config, nil
}

func Init(rootCmd *cobra.Command) {
rand.Seed(time.Now().UnixNano())
cli.SetParseErrorHandle(func(err error) {
os.Exit(128) // 128 - invalid command line arguments
})
configCmd.AddCommand(dumpConfigCmd)
configCmd.AddCommand(updateConfigCmd)
rootCmd.AddCommand(configCmd)
rootCmd.AddCommand(versionCmd)
rootCmd.AddCommand(dumpConfigLegacyCmd)
rootCmd.AddCommand(dumpDBCmd)
rootCmd.AddCommand(inspectDBCmd)

if err := registerRootCmdFlags(rootCmd); err != nil {
os.Exit(2)
}
if err := registerDumpConfigFlags(); err != nil {
os.Exit(2)
}
if err := registerDumpDBFlags(); err != nil {
os.Exit(2)
}
if err := registerInspectionFlags(); err != nil {
os.Exit(2)
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package config

import (
"errors"
Expand Down Expand Up @@ -83,8 +83,8 @@ func init() {
ntStr := confTree.Get("Network.NetworkType").(string)
nt := parseNetworkType(ntStr)

defDNSSyncConf := getDefaultDNSSyncConfig(nt)
defSyncConfig := getDefaultSyncConfig(nt)
defDNSSyncConf := GetDefaultDNSSyncConfig(nt)
defSyncConfig := GetDefaultSyncConfig(nt)

// Legacy conf missing fields
if confTree.Get("Sync") == nil {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package config

import (
"testing"
Expand Down Expand Up @@ -343,8 +343,8 @@ Version = "1.0.4"
)

func Test_migrateConf(t *testing.T) {
defConf := getDefaultHmyConfigCopy(nodeconfig.Mainnet)
legacyDefConf := getDefaultHmyConfigCopy(nodeconfig.Mainnet)
defConf := GetDefaultHmyConfigCopy(nodeconfig.Mainnet)
legacyDefConf := GetDefaultHmyConfigCopy(nodeconfig.Mainnet)
// Versions prior to 1.0.3 use different BootNodes
legacyDefConf.Network.BootNodes = []string{
"/ip4/100.26.90.187/tcp/9874/p2p/Qmdfjtk6hPoyrH1zVD9PEH4zfWLo38dP2mDvvKXfh3tnEv",
Expand Down
14 changes: 7 additions & 7 deletions cmd/harmony/config_test.go → cmd/config/config_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package config

import (
"fmt"
Expand All @@ -16,7 +16,7 @@ import (
type testCfgOpt func(config *harmonyconfig.HarmonyConfig)

func makeTestConfig(nt nodeconfig.NetworkType, opt testCfgOpt) harmonyconfig.HarmonyConfig {
cfg := getDefaultHmyConfigCopy(nt)
cfg := GetDefaultHmyConfigCopy(nt)
if opt != nil {
opt(&cfg)
}
Expand Down Expand Up @@ -127,7 +127,7 @@ Version = "1.0.4"
if err != nil {
t.Fatal(err)
}
defConf := getDefaultHmyConfigCopy(nodeconfig.Mainnet)
defConf := GetDefaultHmyConfigCopy(nodeconfig.Mainnet)
if config.HTTP.RosettaEnabled {
t.Errorf("Expected rosetta http server to be disabled when loading old config")
}
Expand Down Expand Up @@ -160,13 +160,13 @@ func TestPersistConfig(t *testing.T) {
},
{
config: makeTestConfig("mainnet", func(cfg *harmonyconfig.HarmonyConfig) {
consensus := getDefaultConsensusConfigCopy()
consensus := GetDefaultConsensusConfigCopy()
cfg.Consensus = &consensus

devnet := getDefaultDevnetConfigCopy()
devnet := GetDefaultDevnetConfigCopy()
cfg.Devnet = &devnet

revert := getDefaultRevertConfigCopy()
revert := GetDefaultRevertConfigCopy()
cfg.Revert = &revert

webHook := "web hook"
Expand All @@ -175,7 +175,7 @@ func TestPersistConfig(t *testing.T) {
TPBroadcastInvalidTxn: &trueBool,
}

logCtx := getDefaultLogContextCopy()
logCtx := GetDefaultLogContextCopy()
cfg.Log.Context = &logCtx
}),
},
Expand Down
Loading

0 comments on commit 6d9b09d

Please sign in to comment.