diff --git a/cmd/geth/config.go b/cmd/geth/config.go index 27490c4048..bc5d54b30a 100644 --- a/cmd/geth/config.go +++ b/cmd/geth/config.go @@ -126,6 +126,7 @@ func makeConfigNode(ctx *cli.Context) (*node.Node, gethConfig) { } // Apply flags. + utils.SetULC(ctx, &cfg.Eth, &cfg.Node.P2P) utils.SetNodeConfig(ctx, &cfg.Node) stack, err := node.New(&cfg.Node) if err != nil { diff --git a/cmd/geth/main.go b/cmd/geth/main.go index bdb7fad62a..16c899f064 100644 --- a/cmd/geth/main.go +++ b/cmd/geth/main.go @@ -84,6 +84,7 @@ var ( utils.TxPoolLifetimeFlag, utils.FastSyncFlag, utils.LightModeFlag, + utils.ULCModeConfigFlag, utils.SyncModeFlag, utils.LightServFlag, utils.LightPeersFlag, diff --git a/cmd/utils/flags.go b/cmd/utils/flags.go index 30edf199c9..e643a85fe3 100644 --- a/cmd/utils/flags.go +++ b/cmd/utils/flags.go @@ -56,6 +56,7 @@ import ( "github.com/ethereum/go-ethereum/params" whisper "github.com/ethereum/go-ethereum/whisper/whisperv5" "gopkg.in/urfave/cli.v1" + "encoding/json" ) var ( @@ -163,6 +164,10 @@ var ( Name: "light", Usage: "Enable light client mode", } + ULCModeConfigFlag = cli.StringFlag{ + Name: "ulcconfig", + Usage: "Config file to use for ULC mode", + } defaultSyncMode = eth.DefaultConfig.SyncMode SyncModeFlag = TextMarshalerFlag{ Name: "syncmode", @@ -718,6 +723,42 @@ func setIPC(ctx *cli.Context, cfg *node.Config) { } } +// SetULC setup ULC config from file if given. +func SetULC(ctx *cli.Context, cfg *eth.Config, p2pCfg *p2p.Config) { + path := ctx.GlobalString(ULCModeConfigFlag.Name) + if path == "" { + return + } + + cfgData, err := ioutil.ReadFile(path) + if err != nil { + Fatalf("Failed to read ULC config file: %v", err) + } + + err = json.Unmarshal(cfgData, &cfg.ULC) + if err != nil { + Fatalf(err.Error()) + } + + if len(cfg.ULC.TrustedNodes) == 0 { + return + } + + if cfg.ULC.MinTrustedFraction <= 0 || cfg.ULC.MinTrustedFraction > 100 { + cfg.ULC.MinTrustedFraction = eth.DefaultConfig.ULC.MinTrustedFraction + } + + p2pCfg.TrustedNodes = make([]*discover.Node, 0, len(cfg.ULC.TrustedNodes)) + for _, url := range cfg.ULC.TrustedNodes { + node, err := discover.ParseNode(url) + if err != nil { + log.Error("Trusted node URL invalid", "enode", url, "err", err) + continue + } + p2pCfg.TrustedNodes = append(p2pCfg.TrustedNodes, node) + } +} + // makeDatabaseHandles raises out the number of allowed file handles per process // for Geth and returns half of the allowance to assign to the database. func makeDatabaseHandles() int { diff --git a/eth/config.go b/eth/config.go index 383cd6783c..545191a490 100644 --- a/eth/config.go +++ b/eth/config.go @@ -42,6 +42,7 @@ var DefaultConfig = Config{ DatasetsInMem: 1, DatasetsOnDisk: 2, }, + ULC: &ULCConfig{MinTrustedFraction: 75}, NetworkId: 1, LightPeers: 20, DatabaseCache: 128, @@ -83,6 +84,9 @@ type Config struct { LightServ int `toml:",omitempty"` // Maximum percentage of time allowed for serving LES requests LightPeers int `toml:",omitempty"` // Maximum number of LES client peers + // Ultra Light client options + ULC *ULCConfig `toml:",omitempty"` + // Database options SkipBcVersionCheck bool `toml:"-"` DatabaseHandles int `toml:"-"` diff --git a/eth/gen_config.go b/eth/gen_config.go index e2d50e1f66..fb27d7cad6 100644 --- a/eth/gen_config.go +++ b/eth/gen_config.go @@ -13,32 +13,28 @@ import ( "github.com/ethereum/go-ethereum/eth/gasprice" ) +var _ = (*configMarshaling)(nil) + func (c Config) MarshalTOML() (interface{}, error) { type Config struct { Genesis *core.Genesis `toml:",omitempty"` NetworkId uint64 SyncMode downloader.SyncMode - LightServ int `toml:",omitempty"` - LightPeers int `toml:",omitempty"` - MaxPeers int `toml:"-"` - SkipBcVersionCheck bool `toml:"-"` - DatabaseHandles int `toml:"-"` + LightServ int `toml:",omitempty"` + LightPeers int `toml:",omitempty"` + ULC *ULCConfig `toml:",omitempty"` + SkipBcVersionCheck bool `toml:"-"` + DatabaseHandles int `toml:"-"` DatabaseCache int Etherbase common.Address `toml:",omitempty"` MinerThreads int `toml:",omitempty"` ExtraData hexutil.Bytes `toml:",omitempty"` GasPrice *big.Int - EthashCacheDir string - EthashCachesInMem int - EthashCachesOnDisk int - EthashDatasetDir string - EthashDatasetsInMem int - EthashDatasetsOnDisk int + Ethash ethash.Config TxPool core.TxPoolConfig GPO gasprice.Config EnablePreimageRecording bool - DocRoot string `toml:"-"` - PowMode ethash.Mode `toml:"-"` + DocRoot string `toml:"-"` } var enc Config enc.Genesis = c.Genesis @@ -46,6 +42,7 @@ func (c Config) MarshalTOML() (interface{}, error) { enc.SyncMode = c.SyncMode enc.LightServ = c.LightServ enc.LightPeers = c.LightPeers + enc.ULC = c.ULC enc.SkipBcVersionCheck = c.SkipBcVersionCheck enc.DatabaseHandles = c.DatabaseHandles enc.DatabaseCache = c.DatabaseCache @@ -53,17 +50,11 @@ func (c Config) MarshalTOML() (interface{}, error) { enc.MinerThreads = c.MinerThreads enc.ExtraData = c.ExtraData enc.GasPrice = c.GasPrice - enc.EthashCacheDir = c.Ethash.CacheDir - enc.EthashCachesInMem = c.Ethash.CachesInMem - enc.EthashCachesOnDisk = c.Ethash.CachesOnDisk - enc.EthashDatasetDir = c.Ethash.DatasetDir - enc.EthashDatasetsInMem = c.Ethash.DatasetsInMem - enc.EthashDatasetsOnDisk = c.Ethash.DatasetsOnDisk + enc.Ethash = c.Ethash enc.TxPool = c.TxPool enc.GPO = c.GPO enc.EnablePreimageRecording = c.EnablePreimageRecording enc.DocRoot = c.DocRoot - enc.PowMode = c.Ethash.PowMode return &enc, nil } @@ -72,27 +63,21 @@ func (c *Config) UnmarshalTOML(unmarshal func(interface{}) error) error { Genesis *core.Genesis `toml:",omitempty"` NetworkId *uint64 SyncMode *downloader.SyncMode - LightServ *int `toml:",omitempty"` - LightPeers *int `toml:",omitempty"` - MaxPeers *int `toml:"-"` - SkipBcVersionCheck *bool `toml:"-"` - DatabaseHandles *int `toml:"-"` + LightServ *int `toml:",omitempty"` + LightPeers *int `toml:",omitempty"` + ULC *ULCConfig `toml:",omitempty"` + SkipBcVersionCheck *bool `toml:"-"` + DatabaseHandles *int `toml:"-"` DatabaseCache *int Etherbase *common.Address `toml:",omitempty"` MinerThreads *int `toml:",omitempty"` ExtraData hexutil.Bytes `toml:",omitempty"` GasPrice *big.Int - EthashCacheDir *string - EthashCachesInMem *int - EthashCachesOnDisk *int - EthashDatasetDir *string - EthashDatasetsInMem *int - EthashDatasetsOnDisk *int + Ethash *ethash.Config TxPool *core.TxPoolConfig GPO *gasprice.Config EnablePreimageRecording *bool - DocRoot *string `toml:"-"` - PowMode *ethash.Mode `toml:"-"` + DocRoot *string `toml:"-"` } var dec Config if err := unmarshal(&dec); err != nil { @@ -113,6 +98,9 @@ func (c *Config) UnmarshalTOML(unmarshal func(interface{}) error) error { if dec.LightPeers != nil { c.LightPeers = *dec.LightPeers } + if dec.ULC != nil { + c.ULC = dec.ULC + } if dec.SkipBcVersionCheck != nil { c.SkipBcVersionCheck = *dec.SkipBcVersionCheck } @@ -134,23 +122,8 @@ func (c *Config) UnmarshalTOML(unmarshal func(interface{}) error) error { if dec.GasPrice != nil { c.GasPrice = dec.GasPrice } - if dec.EthashCacheDir != nil { - c.Ethash.CacheDir = *dec.EthashCacheDir - } - if dec.EthashCachesInMem != nil { - c.Ethash.CachesInMem = *dec.EthashCachesInMem - } - if dec.EthashCachesOnDisk != nil { - c.Ethash.CachesOnDisk = *dec.EthashCachesOnDisk - } - if dec.EthashDatasetDir != nil { - c.Ethash.DatasetDir = *dec.EthashDatasetDir - } - if dec.EthashDatasetsInMem != nil { - c.Ethash.DatasetsInMem = *dec.EthashDatasetsInMem - } - if dec.EthashDatasetsOnDisk != nil { - c.Ethash.DatasetsOnDisk = *dec.EthashDatasetsOnDisk + if dec.Ethash != nil { + c.Ethash = *dec.Ethash } if dec.TxPool != nil { c.TxPool = *dec.TxPool @@ -164,8 +137,5 @@ func (c *Config) UnmarshalTOML(unmarshal func(interface{}) error) error { if dec.DocRoot != nil { c.DocRoot = *dec.DocRoot } - if dec.PowMode != nil { - c.Ethash.PowMode = *dec.PowMode - } return nil } diff --git a/eth/ulc_config.go b/eth/ulc_config.go new file mode 100644 index 0000000000..b86d191239 --- /dev/null +++ b/eth/ulc_config.go @@ -0,0 +1,7 @@ +package eth + +// Ultra Light client options +type ULCConfig struct { + TrustedNodes []string `toml:",omitempty"` // A list of trusted servers + MinTrustedFraction int `toml:",omitempty"` // Minimum percentage of connected trusted servers to validate trusted (1-100) +}