status-go/_assets/patches/geth/0027-whisper-time-source.patch

132 lines
4.7 KiB
Diff
Raw Normal View History

2018-05-08 10:38:54 +00:00
diff --git a/whisper/whisperv6/api.go b/whisper/whisperv6/api.go
index 2de99f293..e0c3284b6 100644
2018-05-08 10:38:54 +00:00
--- a/whisper/whisperv6/api.go
+++ b/whisper/whisperv6/api.go
@@ -285,7 +285,7 @@ func (api *PublicWhisperAPI) Post(ctx context.Context, req NewMessage) (hexutil.
}
var result []byte
- env, err := whisperMsg.Wrap(params)
+ env, err := whisperMsg.Wrap(params, api.w.GetCurrentTime())
if err != nil {
return nil, err
}
2018-05-08 10:38:54 +00:00
diff --git a/whisper/whisperv6/config.go b/whisper/whisperv6/config.go
index 61419de00..3c28263e5 100644
--- a/whisper/whisperv6/config.go
+++ b/whisper/whisperv6/config.go
@@ -16,14 +16,18 @@
2018-05-08 10:38:54 +00:00
package whisperv6
2018-05-08 10:38:54 +00:00
+import "time"
+
// Config represents the configuration state of a whisper node.
type Config struct {
MaxMessageSize uint32 `toml:",omitempty"`
MinimumAcceptedPOW float64 `toml:",omitempty"`
+ TimeSource func() time.Time
}
2018-05-08 10:38:54 +00:00
// DefaultConfig represents (shocker!) the default configuration.
var DefaultConfig = Config{
MaxMessageSize: DefaultMaxMessageSize,
MinimumAcceptedPOW: DefaultMinimumPoW,
+ TimeSource: time.Now,
}
diff --git a/whisper/whisperv6/envelope.go b/whisper/whisperv6/envelope.go
index c42d1fa8a..3b65fdba0 100644
2018-05-08 10:38:54 +00:00
--- a/whisper/whisperv6/envelope.go
+++ b/whisper/whisperv6/envelope.go
@@ -62,9 +62,9 @@ func (e *Envelope) rlpWithoutNonce() []byte {
// NewEnvelope wraps a Whisper message with expiration and destination data
// included into an envelope for network forwarding.
-func NewEnvelope(ttl uint32, topic TopicType, msg *sentMessage) *Envelope {
+func NewEnvelope(ttl uint32, topic TopicType, msg *sentMessage, now time.Time) *Envelope {
env := Envelope{
- Expiry: uint32(time.Now().Add(time.Second * time.Duration(ttl)).Unix()),
+ Expiry: uint32(now.Add(time.Second * time.Duration(ttl)).Unix()),
TTL: ttl,
Topic: topic,
Data: msg.Raw,
2018-05-08 10:38:54 +00:00
diff --git a/whisper/whisperv6/message.go b/whisper/whisperv6/message.go
index 2d4e86244..a12b445e2 100644
2018-05-08 10:38:54 +00:00
--- a/whisper/whisperv6/message.go
+++ b/whisper/whisperv6/message.go
@@ -27,6 +27,7 @@ import (
"errors"
mrand "math/rand"
"strconv"
+ "time"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
@@ -234,7 +235,7 @@ func generateSecureRandomData(length int) ([]byte, error) {
}
// Wrap bundles the message into an Envelope to transmit over the network.
-func (msg *sentMessage) Wrap(options *MessageParams) (envelope *Envelope, err error) {
+func (msg *sentMessage) Wrap(options *MessageParams, now time.Time) (envelope *Envelope, err error) {
if options.TTL == 0 {
options.TTL = DefaultTTL
}
@@ -254,7 +255,7 @@ func (msg *sentMessage) Wrap(options *MessageParams) (envelope *Envelope, err er
return nil, err
}
- envelope = NewEnvelope(options.TTL, options.Topic, msg)
+ envelope = NewEnvelope(options.TTL, options.Topic, msg, now)
if err = envelope.Seal(options); err != nil {
return nil, err
}
2018-05-08 10:38:54 +00:00
diff --git a/whisper/whisperv6/whisper.go b/whisper/whisperv6/whisper.go
index 8d56ece64..697f0ecb8 100644
--- a/whisper/whisperv6/whisper.go
+++ b/whisper/whisperv6/whisper.go
@@ -93,4 +92,6 @@ type Whisper struct {
envelopeFeed event.Feed
+
+ timeSource func() time.Time // source of time for whisper
}
// New creates a Whisper client ready to communicate through the Ethereum P2P network.
@@ -110,6 +112,7 @@ func New(cfg *Config) *Whisper {
p2pMsgQueue: make(chan *Envelope, messageQueueLimit),
quit: make(chan struct{}),
syncAllowance: DefaultSyncAllowance,
2018-05-08 10:38:54 +00:00
+ timeSource: cfg.TimeSource,
}
whisper.filters = NewFilters(whisper)
2018-05-08 10:38:54 +00:00
@@ -215,6 +218,11 @@ func (whisper *Whisper) APIs() []rpc.API {
}
}
+// GetCurrentTime returns current time.
+func (whisper *Whisper) GetCurrentTime() time.Time {
+ return whisper.timeSource()
+}
+
// RegisterServer registers MailServer interface.
// MailServer will process all the incoming messages with p2pRequestCode.
func (whisper *Whisper) RegisterServer(server MailServer) {
2018-05-08 10:38:54 +00:00
@@ -829,7 +837,7 @@ func (whisper *Whisper) runMessageLoop(p *Peer, rw p2p.MsgReadWriter) error {
// appropriate time-stamp. In case of error, connection should be dropped.
// param isP2P indicates whether the message is peer-to-peer (should not be forwarded).
func (whisper *Whisper) add(envelope *Envelope, isP2P bool) (bool, error) {
- now := uint32(time.Now().Unix())
+ now := uint32(whisper.timeSource().Unix())
sent := envelope.Expiry - envelope.TTL
if sent > now {
2018-05-08 10:38:54 +00:00
@@ -988,7 +996,7 @@ func (whisper *Whisper) expire() {
whisper.statsMu.Lock()
defer whisper.statsMu.Unlock()
whisper.stats.reset()
- now := uint32(time.Now().Unix())
+ now := uint32(whisper.timeSource().Unix())
for expiry, hashSet := range whisper.expirations {
if expiry < now {
// Dump all expired messages and remove timestamp