[#1022] Make ntp synchronization configuratble (#1026)

This commit is contained in:
Adrià Cidre 2018-06-13 13:24:04 +02:00 committed by GitHub
parent a339d7ed33
commit 6173d3ee06
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 30 additions and 9 deletions

View File

@ -7,6 +7,7 @@ import (
"os"
"path"
"path/filepath"
"time"
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/crypto"
@ -194,22 +195,27 @@ func activateShhService(stack *node.Node, config *params.NodeConfig, db *leveldb
logger.Info("SHH protocol is disabled")
return nil
}
if config.WhisperConfig.EnableNTPSync {
if err := stack.Register(func(*node.ServiceContext) (node.Service, error) {
return timesource.Default(), nil
}); err != nil {
return err
}
}
err = stack.Register(func(ctx *node.ServiceContext) (node.Service, error) {
var timeSource *timesource.NTPTimeSource
if err := ctx.Service(&timeSource); err != nil {
return nil, err
}
whisperServiceConfig := &whisper.Config{
MaxMessageSize: whisper.DefaultMaxMessageSize,
MinimumAcceptedPOW: 0.001,
TimeSource: timeSource.Now,
TimeSource: time.Now,
}
if config.WhisperConfig.EnableNTPSync {
if whisperServiceConfig.TimeSource, err = whisperTimeSource(ctx); err != nil {
return nil, err
}
}
whisperService := whisper.New(whisperServiceConfig)
// enable metrics
@ -296,3 +302,12 @@ func parseNodesV5(enodes []string) []*discv5.Node {
}
return nodes
}
// whisperTimeSource get timeSource to be used by whisper
func whisperTimeSource(ctx *node.ServiceContext) (func() time.Time, error) {
var timeSource *timesource.NTPTimeSource
if err := ctx.Service(&timeSource); err != nil {
return nil, err
}
return timeSource.Now, nil
}

View File

@ -116,6 +116,9 @@ type WhisperConfig struct {
// FirebaseConfig extra configuration for Firebase Cloud Messaging
FirebaseConfig *FirebaseConfig `json:"FirebaseConfig,"`
// EnableNTPSync enables NTP synchronizations
EnableNTPSync bool
}
// ReadPasswordFile reads and returns content of the password file
@ -353,6 +356,7 @@ func NewNodeConfig(dataDir string, clstrCfgFile string, networkID uint64) (*Node
FirebaseConfig: &FirebaseConfig{
NotificationTriggerURL: FirebaseNotificationTriggerURL,
},
EnableNTPSync: true,
},
SwarmConfig: &SwarmConfig{},
RegisterTopics: []discv5.Topic{},

View File

@ -255,6 +255,8 @@ func MakeTestNodeConfig(networkID int) (*params.NodeConfig, error) {
if err != nil {
return nil, err
}
nodeConfig.WhisperConfig.EnableNTPSync = false
return nodeConfig, nil
}