Skip to content

Commit

Permalink
Set global node location at startup
Browse files Browse the repository at this point in the history
  • Loading branch information
wizeguyy committed Jan 25, 2023
1 parent 290b672 commit c76a76f
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 26 deletions.
3 changes: 2 additions & 1 deletion cmd/go-quai/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (

"gopkg.in/urfave/cli.v1"

"github.com/naoina/toml"
"github.com/spruce-solutions/go-quai/cmd/utils"
"github.com/spruce-solutions/go-quai/eth/catalyst"
"github.com/spruce-solutions/go-quai/eth/ethconfig"
Expand All @@ -35,7 +36,6 @@ import (
"github.com/spruce-solutions/go-quai/metrics"
"github.com/spruce-solutions/go-quai/node"
"github.com/spruce-solutions/go-quai/params"
"github.com/naoina/toml"
)

var (
Expand Down Expand Up @@ -130,6 +130,7 @@ func makeConfigNode(ctx *cli.Context) (*node.Node, quaiConfig) {
}

// Apply flags.
utils.SetGlobalVars(ctx)
utils.SetNodeConfig(ctx, &cfg.Node)
stack, err := node.New(&cfg.Node)
if err != nil {
Expand Down
2 changes: 2 additions & 0 deletions cmd/go-quai/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@ var (
utils.MinerNotifyFullFlag,
configFileFlag,
utils.CatalystFlag,
utils.RegionFlag,
utils.ZoneFlag,
}

rpcFlags = []cli.Flag{
Expand Down
7 changes: 7 additions & 0 deletions cmd/go-quai/usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,13 @@ var AppHelpFlagGroups = []flags.FlagGroup{
utils.CatalystFlag,
},
},
{
Name: "QUAI NODE LOCATION",
Flags: []cli.Flag{
utils.RegionFlag,
utils.ZoneFlag,
},
},
}

func init() {
Expand Down
80 changes: 59 additions & 21 deletions cmd/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ import (
"text/template"
"time"

pcsclite "github.com/gballet/go-libpcsclite"
gopsutil "github.com/shirou/gopsutil/mem"
"github.com/spruce-solutions/go-quai/accounts"
"github.com/spruce-solutions/go-quai/accounts/keystore"
"github.com/spruce-solutions/go-quai/common"
Expand Down Expand Up @@ -66,8 +68,6 @@ import (
"github.com/spruce-solutions/go-quai/p2p/nat"
"github.com/spruce-solutions/go-quai/p2p/netutil"
"github.com/spruce-solutions/go-quai/params"
pcsclite "github.com/gballet/go-libpcsclite"
gopsutil "github.com/shirou/gopsutil/mem"
"gopkg.in/urfave/cli.v1"
)

Expand Down Expand Up @@ -760,6 +760,17 @@ var (
Name: "catalyst",
Usage: "Catalyst mode (eth2 integration testing)",
}

RegionFlag = cli.IntFlag{
Name: "region",
Usage: "Quai Region flag",
Value: ethconfig.Defaults.Region,
}
ZoneFlag = cli.IntFlag{
Name: "zone",
Usage: "Quai Zone flag",
Value: ethconfig.Defaults.Zone,
}
)

// MakeDataDir retrieves the currently requested data directory, terminating
Expand All @@ -770,16 +781,25 @@ func MakeDataDir(ctx *cli.Context) string {
if ctx.GlobalBool(RopstenFlag.Name) {
// Maintain compatibility with older Geth configurations storing the
// Ropsten database in `testnet` instead of `ropsten`.
return filepath.Join(path, "ropsten")
}
if ctx.GlobalBool(RinkebyFlag.Name) {
return filepath.Join(path, "rinkeby")
path = filepath.Join(path, "ropsten")
} else if ctx.GlobalBool(RinkebyFlag.Name) {
path = filepath.Join(path, "rinkeby")
} else if ctx.GlobalBool(GoerliFlag.Name) {
path = filepath.Join(path, "goerli")
} else if ctx.GlobalBool(CalaverasFlag.Name) {
path = filepath.Join(path, "calaveras")
}
if ctx.GlobalBool(GoerliFlag.Name) {
return filepath.Join(path, "goerli")
}
if ctx.GlobalBool(CalaverasFlag.Name) {
return filepath.Join(path, "calaveras")
// Set specific directory for node location within the hierarchy
switch common.NodeLocation.Context() {
case common.PRIME_CTX:
path = filepath.Join(path, "prime")
case common.REGION_CTX:
regionNum := strconv.Itoa(common.NodeLocation.Region())
path = filepath.Join(path, "region-"+regionNum)
case common.ZONE_CTX:
regionNum := strconv.Itoa(common.NodeLocation.Region())
zoneNum := strconv.Itoa(common.NodeLocation.Zone())
path = filepath.Join(path, "zone-"+regionNum+"-"+zoneNum)
}
return path
}
Expand Down Expand Up @@ -1263,16 +1283,6 @@ func setDataDir(ctx *cli.Context, cfg *node.Config) {
case ctx.GlobalBool(DeveloperFlag.Name):
cfg.DataDir = "" // unless explicitly requested, use memory databases
case ctx.GlobalBool(RopstenFlag.Name) && cfg.DataDir == node.DefaultDataDir():
// Maintain compatibility with older Geth configurations storing the
// Ropsten database in `testnet` instead of `ropsten`.
legacyPath := filepath.Join(node.DefaultDataDir(), "testnet")
if _, err := os.Stat(legacyPath); !os.IsNotExist(err) {
log.Warn("Using the deprecated `testnet` datadir. Future versions will store the Ropsten chain in `ropsten`.")
cfg.DataDir = legacyPath
} else {
cfg.DataDir = filepath.Join(node.DefaultDataDir(), "ropsten")
}

cfg.DataDir = filepath.Join(node.DefaultDataDir(), "ropsten")
case ctx.GlobalBool(RinkebyFlag.Name) && cfg.DataDir == node.DefaultDataDir():
cfg.DataDir = filepath.Join(node.DefaultDataDir(), "rinkeby")
Expand All @@ -1281,6 +1291,18 @@ func setDataDir(ctx *cli.Context, cfg *node.Config) {
case ctx.GlobalBool(CalaverasFlag.Name) && cfg.DataDir == node.DefaultDataDir():
cfg.DataDir = filepath.Join(node.DefaultDataDir(), "calaveras")
}
// Set specific directory for node location within the hierarchy
switch common.NodeLocation.Context() {
case common.PRIME_CTX:
cfg.DataDir = filepath.Join(cfg.DataDir, "prime")
case common.REGION_CTX:
regionNum := strconv.Itoa(common.NodeLocation.Region())
cfg.DataDir = filepath.Join(cfg.DataDir, "region-"+regionNum)
case common.ZONE_CTX:
regionNum := strconv.Itoa(common.NodeLocation.Region())
zoneNum := strconv.Itoa(common.NodeLocation.Zone())
cfg.DataDir = filepath.Join(cfg.DataDir, "zone-"+regionNum+"-"+zoneNum)
}
}

func setGPO(ctx *cli.Context, cfg *gasprice.Config, light bool) {
Expand Down Expand Up @@ -1462,12 +1484,28 @@ func CheckExclusive(ctx *cli.Context, args ...interface{}) {
}
}

func SetGlobalVars(ctx *cli.Context) {
// Configure global NodeLocation
if !ctx.GlobalIsSet(RegionFlag.Name) && ctx.GlobalIsSet(ZoneFlag.Name) {
log.Crit("zone idx given, but missing region idx!")
}
if ctx.GlobalIsSet(RegionFlag.Name) {
region := byte(ctx.GlobalInt(RegionFlag.Name))
common.NodeLocation[common.REGION_CTX-1] = &region
}
if ctx.GlobalIsSet(ZoneFlag.Name) {
zone := byte(ctx.GlobalInt(ZoneFlag.Name))
common.NodeLocation[common.ZONE_CTX-1] = &zone
}
}

// SetEthConfig applies eth-related command line flags to the config.
func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *ethconfig.Config) {
// Avoid conflicting network flags
CheckExclusive(ctx, MainnetFlag, DeveloperFlag, RopstenFlag, RinkebyFlag, GoerliFlag, CalaverasFlag)
CheckExclusive(ctx, LightServeFlag, SyncModeFlag, "light")
CheckExclusive(ctx, DeveloperFlag, ExternalSignerFlag) // Can't use both ephemeral unlocked and external signer

if ctx.GlobalString(GCModeFlag.Name) == "archive" && ctx.GlobalUint64(TxLookupLimitFlag.Name) != 0 {
ctx.GlobalSet(TxLookupLimitFlag.Name, "0")
log.Warn("Disable transaction unindexing for archive node")
Expand Down
6 changes: 6 additions & 0 deletions eth/ethconfig/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,12 @@ type Config struct {

// Berlin block override (TODO: remove after the fork)
OverrideLondon *big.Int `toml:",omitempty"`

// Region location options
Region int

// Zone location options
Zone int
}

// CreateConsensusEngine creates a consensus engine for the given chain configuration.
Expand Down
8 changes: 4 additions & 4 deletions node/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,19 +61,19 @@ func DefaultDataDir() string {
if home != "" {
switch runtime.GOOS {
case "darwin":
return filepath.Join(home, "Library", "Ethereum")
return filepath.Join(home, "Library", "Quai")
case "windows":
// We used to put everything in %HOME%\AppData\Roaming, but this caused
// problems with non-typical setups. If this fallback location exists and
// is non-empty, use it, otherwise DTRT and check %LOCALAPPDATA%.
fallback := filepath.Join(home, "AppData", "Roaming", "Ethereum")
fallback := filepath.Join(home, "AppData", "Roaming", "Quai")
appdata := windowsAppData()
if appdata == "" || isNonEmptyDir(fallback) {
return fallback
}
return filepath.Join(appdata, "Ethereum")
return filepath.Join(appdata, "Quai")
default:
return filepath.Join(home, ".ethereum")
return filepath.Join(home, ".quai")
}
}
// As we cannot guess a stable location, return empty and handle later
Expand Down

0 comments on commit c76a76f

Please sign in to comment.