Skip to content

Commit

Permalink
Make sync mode of "Geth for mobile" configurable (ethereum#77)
Browse files Browse the repository at this point in the history
* Make Geth for mobile's sync mode configurable

1. Default to LightSync to maintain backward compatibility.
2. Gomobile, the tool for generating Android bindings does not handle
enums, so, a new set of integer constants have to be created.
  • Loading branch information
ashishb authored Oct 26, 2018
1 parent 24549c8 commit 592315a
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
Binary file modified geth-sources.jar
Binary file not shown.
34 changes: 33 additions & 1 deletion mobile/geth.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ import (
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
const SyncModeCeloLatestSync = 4

// 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
// entire API provided by go-ethereum to reduce the maintenance surface and dev
Expand Down Expand Up @@ -76,6 +84,11 @@ type NodeConfig struct {

// Listening address of pprof server.
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"
// See getSyncMode(syncMode int)
SyncMode int
}

// defaultNodeConfig contains the default node configuration values to use if all
Expand Down Expand Up @@ -162,7 +175,7 @@ func NewNode(datadir string, config *NodeConfig) (stack *Node, _ error) {
// Once https:/celo-org/geth/pull/62 is landed,
// change the ethConf.SyncMode to downloader.CeloLatestSync.
// It might be better to make this configurable.
ethConf.SyncMode = downloader.LightSync
ethConf.SyncMode = getSyncMode(config.SyncMode)
ethConf.NetworkId = uint64(config.EthereumNetworkID)
ethConf.DatabaseCache = config.EthereumDatabaseCache
if err := rawStack.Register(func(ctx *node.ServiceContext) (node.Service, error) {
Expand Down Expand Up @@ -193,6 +206,25 @@ func NewNode(datadir string, config *NodeConfig) (stack *Node, _ error) {
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
case SyncModeCeloLatestSync:
return downloader.CeloLatestSync
default:
panic(fmt.Sprintf("Unexpected sync mode value: %d", syncMode))
}
}

// Start creates a live P2P node and starts running it.
func (n *Node) Start() error {
return n.node.Start()
Expand Down

0 comments on commit 592315a

Please sign in to comment.