mobile: Make sync mode configurable

Make sync mode configurable. Keep Light as the default sync mode to
maintain backward-compatibility.

The mode can be configured by calling `NodeConfig.setSyncMode()`
This commit is contained in:
Ashish Bhatia 2018-11-07 10:24:01 -08:00
parent 0bcff8f525
commit 821a9f2d13

View file

@ -38,6 +38,13 @@ import (
whisper "github.com/ethereum/go-ethereum/whisper/whisperv6" whisper "github.com/ethereum/go-ethereum/whisper/whisperv6"
) )
// I am intentionally duplicating these constants different from downloader.SyncMode integer values, to ensure the
// backwards compatibility where the mobile node defaults to LightSync.
const SyncModeUnset = 0 // will be treated as SyncModeLightSync
const SyncModeFullSync = 1
const SyncModeFastSync = 2
const SyncModeLightSync = 3
// 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
// entire API provided by go-ethereum to reduce the maintenance surface and dev // entire API provided by go-ethereum to reduce the maintenance surface and dev
@ -76,6 +83,10 @@ type NodeConfig struct {
// Listening address of pprof server. // Listening address of pprof server.
PprofAddress string PprofAddress string
// Sync mode for the node (eth/downloader/modes.go)
// This has to be integer since Enum exports to Java are not supported by "gomobile"
SyncMode int
} }
// defaultNodeConfig contains the default node configuration values to use if all // defaultNodeConfig contains the default node configuration values to use if all
@ -188,6 +199,23 @@ func NewNode(datadir string, config *NodeConfig) (stack *Node, _ error) {
return &Node{rawStack}, nil return &Node{rawStack}, nil
} }
func getSyncMode(syncMode int) downloader.SyncMode {
switch syncMode {
case SyncModeFullSync:
return downloader.FullSync
case SyncModeFastSync:
return downloader.FastSync
case SyncModeUnset:
fallthrough
// If unset, default to light sync.
// This maintains backward compatibility.
case SyncModeLightSync:
return downloader.LightSync
default:
panic(fmt.Sprintf("Unexpected sync mode value: %d", syncMode))
}
}
// Start creates a live P2P node and starts running it. // Start creates a live P2P node and starts running it.
func (n *Node) Start() error { func (n *Node) Start() error {
return n.node.Start() return n.node.Start()