mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-19 18:32:23 +00:00
Address comments
1. Use `iota` for constants as recommended by @holiman. 2. Create full ethereum service for full and fast mode.
This commit is contained in:
parent
821a9f2d13
commit
40a69b521e
1 changed files with 55 additions and 16 deletions
|
|
@ -40,10 +40,12 @@ import (
|
||||||
|
|
||||||
// I am intentionally duplicating these constants different from downloader.SyncMode integer values, to ensure the
|
// I am intentionally duplicating these constants different from downloader.SyncMode integer values, to ensure the
|
||||||
// backwards compatibility where the mobile node defaults to LightSync.
|
// backwards compatibility where the mobile node defaults to LightSync.
|
||||||
const SyncModeUnset = 0 // will be treated as SyncModeLightSync
|
const (
|
||||||
const SyncModeFullSync = 1
|
SyncModeUnset = iota // will be treated as SyncModeLightSync
|
||||||
const SyncModeFastSync = 2
|
SyncModeFullSync
|
||||||
const SyncModeLightSync = 3
|
SyncModeFastSync
|
||||||
|
SyncModeLightSync
|
||||||
|
)
|
||||||
|
|
||||||
// NodeConfig represents the collection of configuration values to fine tune the Geth
|
// NodeConfig represents the collection of configuration values to fine tune the Geth
|
||||||
// node embedded into a mobile process. The available values are a subset of the
|
// node embedded into a mobile process. The available values are a subset of the
|
||||||
|
|
@ -166,24 +168,18 @@ func NewNode(datadir string, config *NodeConfig) (stack *Node, _ error) {
|
||||||
}
|
}
|
||||||
// Register the Ethereum protocol if requested
|
// Register the Ethereum protocol if requested
|
||||||
if config.EthereumEnabled {
|
if config.EthereumEnabled {
|
||||||
|
syncMode := getSyncMode(config.SyncMode)
|
||||||
ethConf := eth.DefaultConfig
|
ethConf := eth.DefaultConfig
|
||||||
ethConf.Genesis = genesis
|
ethConf.Genesis = genesis
|
||||||
ethConf.SyncMode = downloader.LightSync
|
ethConf.SyncMode = syncMode
|
||||||
ethConf.NetworkId = uint64(config.EthereumNetworkID)
|
ethConf.NetworkId = uint64(config.EthereumNetworkID)
|
||||||
ethConf.DatabaseCache = config.EthereumDatabaseCache
|
ethConf.DatabaseCache = config.EthereumDatabaseCache
|
||||||
if err := rawStack.Register(func(ctx *node.ServiceContext) (node.Service, error) {
|
if err := rawStack.Register(getEthereumServiceConstructor(syncMode, ðConf)); err != nil {
|
||||||
return les.New(ctx, ðConf)
|
|
||||||
}); err != nil {
|
|
||||||
return nil, fmt.Errorf("ethereum init: %v", err)
|
return nil, fmt.Errorf("ethereum init: %v", err)
|
||||||
}
|
}
|
||||||
// If netstats reporting is requested, do it
|
// If netstats reporting is requested, do it
|
||||||
if config.EthereumNetStats != "" {
|
if config.EthereumNetStats != "" {
|
||||||
if err := rawStack.Register(func(ctx *node.ServiceContext) (node.Service, error) {
|
if err := rawStack.Register(getEthStatsServiceConstructor(syncMode, config)); err != nil {
|
||||||
var lesServ *les.LightEthereum
|
|
||||||
ctx.Service(&lesServ)
|
|
||||||
|
|
||||||
return ethstats.New(config.EthereumNetStats, nil, lesServ)
|
|
||||||
}); err != nil {
|
|
||||||
return nil, fmt.Errorf("netstats init: %v", err)
|
return nil, fmt.Errorf("netstats init: %v", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -199,6 +195,49 @@ func NewNode(datadir string, config *NodeConfig) (stack *Node, _ error) {
|
||||||
return &Node{rawStack}, nil
|
return &Node{rawStack}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getEthereumServiceConstructor(syncMode downloader.SyncMode, ethConf *eth.Config) node.ServiceConstructor {
|
||||||
|
return func(ctx *node.ServiceContext) (node.Service, error) {
|
||||||
|
switch syncMode {
|
||||||
|
// Full-node for full sync and fast sync
|
||||||
|
case downloader.FullSync:
|
||||||
|
fallthrough
|
||||||
|
case downloader.FastSync:
|
||||||
|
return eth.New(ctx, ethConf)
|
||||||
|
// Light node for light sync
|
||||||
|
case downloader.LightSync:
|
||||||
|
return les.New(ctx, ethConf)
|
||||||
|
default:
|
||||||
|
panic(fmt.Sprintf("Unexpected sync mode: %v", syncMode))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func getEthStatsServiceConstructor(syncMode downloader.SyncMode, config *NodeConfig) node.ServiceConstructor {
|
||||||
|
return func(ctx *node.ServiceContext) (node.Service, error) {
|
||||||
|
switch syncMode {
|
||||||
|
case downloader.FullSync:
|
||||||
|
fallthrough
|
||||||
|
case downloader.FastSync:
|
||||||
|
var ethServ *eth.Ethereum
|
||||||
|
ctx.Service(ðServ)
|
||||||
|
err := ctx.Service(ðServ)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("netstats init: %v", err)
|
||||||
|
}
|
||||||
|
return ethstats.New(config.EthereumNetStats, ethServ, nil)
|
||||||
|
case downloader.LightSync:
|
||||||
|
var lesServ *les.LightEthereum
|
||||||
|
err := ctx.Service(&lesServ)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("netstats init: %v", err)
|
||||||
|
}
|
||||||
|
return ethstats.New(config.EthereumNetStats, nil, lesServ)
|
||||||
|
default:
|
||||||
|
panic(fmt.Sprintf("Unexpected sync mode: %v", syncMode))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func getSyncMode(syncMode int) downloader.SyncMode {
|
func getSyncMode(syncMode int) downloader.SyncMode {
|
||||||
switch syncMode {
|
switch syncMode {
|
||||||
case SyncModeFullSync:
|
case SyncModeFullSync:
|
||||||
|
|
@ -207,8 +246,8 @@ func getSyncMode(syncMode int) downloader.SyncMode {
|
||||||
return downloader.FastSync
|
return downloader.FastSync
|
||||||
case SyncModeUnset:
|
case SyncModeUnset:
|
||||||
fallthrough
|
fallthrough
|
||||||
// If unset, default to light sync.
|
// If unset, default to light sync.
|
||||||
// This maintains backward compatibility.
|
// This maintains backward compatibility.
|
||||||
case SyncModeLightSync:
|
case SyncModeLightSync:
|
||||||
return downloader.LightSync
|
return downloader.LightSync
|
||||||
default:
|
default:
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue