Use correct timesource in waku-2

This commit is contained in:
Andrea Maria Piana 2023-03-24 12:05:42 +00:00
parent 696e061861
commit 7c2b5a39b3
4 changed files with 9 additions and 27 deletions

View File

@ -121,11 +121,6 @@ func (w *gethWakuWrapper) GetCurrentTime() time.Time {
return w.waku.CurrentTime()
}
// SetTimeSource assigns a particular source of time to a whisper object.
func (w *gethWakuWrapper) SetTimeSource(timesource func() time.Time) {
w.waku.SetTimeSource(timesource)
}
func (w *gethWakuWrapper) SubscribeEnvelopeEvents(eventsProxy chan<- types.EnvelopeEvent) types.Subscription {
events := make(chan wakucommon.EnvelopeEvent, 100) // must be buffered to prevent blocking whisper
go func() {

View File

@ -69,11 +69,6 @@ func (w *gethWakuV2Wrapper) GetCurrentTime() time.Time {
return w.waku.CurrentTime()
}
// SetTimeSource assigns a particular source of time to a whisper object.
func (w *gethWakuV2Wrapper) SetTimeSource(timesource func() time.Time) {
w.waku.SetTimeSource(timesource)
}
func (w *gethWakuV2Wrapper) SubscribeEnvelopeEvents(eventsProxy chan<- types.EnvelopeEvent) types.Subscription {
events := make(chan wakucommon.EnvelopeEvent, 100) // must be buffered to prevent blocking whisper
go func() {

View File

@ -107,8 +107,7 @@ type Waku interface {
// If a message does not match the bloom, it will tantamount to spam, and the peer will
// be disconnected.
BloomFilter() []byte
// SetTimeSource assigns a particular source of time to a whisper object.
SetTimeSource(timesource func() time.Time)
// GetCurrentTime returns current time.
GetCurrentTime() time.Time

View File

@ -61,7 +61,6 @@ import (
"github.com/waku-org/go-waku/waku/v2/protocol/filter"
"github.com/waku-org/go-waku/waku/v2/protocol/peer_exchange"
"github.com/waku-org/go-waku/waku/v2/protocol/relay"
"github.com/waku-org/go-waku/waku/v2/utils"
"github.com/status-im/status-go/connection"
"github.com/status-im/status-go/eth-node/types"
@ -132,8 +131,6 @@ type Waku struct {
connStatusSubscriptions map[string]*types.ConnStatusSubscription
connStatusMu sync.Mutex
timeSource func() time.Time // source of time for waku
logger *zap.Logger
// NTP Synced timesource
@ -166,7 +163,7 @@ func getUsableUDPPort() (int, error) {
}
// New creates a WakuV2 client ready to communicate through the LibP2P network.
func New(nodeKey string, fleet string, cfg *Config, logger *zap.Logger, appDB *sql.DB, timesource *timesource.NTPTimeSource) (*Waku, error) {
func New(nodeKey string, fleet string, cfg *Config, logger *zap.Logger, appDB *sql.DB, ts *timesource.NTPTimeSource) (*Waku, error) {
var err error
if logger == nil {
logger, err = zap.NewDevelopment()
@ -175,6 +172,10 @@ func New(nodeKey string, fleet string, cfg *Config, logger *zap.Logger, appDB *s
}
}
if ts == nil {
ts = timesource.Default()
}
cfg = setDefaults(cfg)
if cfg.UDPPort == 0 {
@ -201,8 +202,8 @@ func New(nodeKey string, fleet string, cfg *Config, logger *zap.Logger, appDB *s
dnsAddressCache: make(map[string][]dnsdisc.DiscoveredNode),
dnsAddressCacheLock: &sync.RWMutex{},
storeMsgIDs: make(map[gethcommon.Hash]bool),
timesource: ts,
storeMsgIDsMu: sync.RWMutex{},
timeSource: time.Now,
logger: logger,
discV5BootstrapNodes: cfg.DiscV5BootstrapNodes,
}
@ -764,12 +765,7 @@ func (w *Waku) ConfirmationsEnabled() bool {
// CurrentTime returns current time.
func (w *Waku) CurrentTime() time.Time {
return w.timeSource()
}
// SetTimeSource assigns a particular source of time to a waku object.
func (w *Waku) SetTimeSource(timesource func() time.Time) {
w.timeSource = timesource
return w.timesource.Now()
}
// APIs returns the RPC descriptors the Waku implementation offers
@ -1489,11 +1485,8 @@ func (w *Waku) AddStorePeer(address string) (peer.ID, error) {
}
func (w *Waku) timestamp() int64 {
if w.timesource != nil {
return w.timesource.Now().UnixNano()
}
return utils.GetUnixEpoch()
}
func (w *Waku) autoRelayPeerSource(ctx context.Context, numPeers int) <-chan peer.AddrInfo {