2018-05-08 10:38:54 +00:00
|
|
|
diff --git a/whisper/whisperv6/api.go b/whisper/whisperv6/api.go
|
2018-05-04 08:23:38 +00:00
|
|
|
index 2de99f293..e0c3284b6 100644
|
2018-05-08 10:38:54 +00:00
|
|
|
--- a/whisper/whisperv6/api.go
|
|
|
|
+++ b/whisper/whisperv6/api.go
|
2018-05-04 08:23:38 +00:00
|
|
|
@@ -285,7 +285,7 @@ func (api *PublicWhisperAPI) Post(ctx context.Context, req NewMessage) (hexutil.
|
|
|
|
}
|
2018-08-31 08:09:38 +00:00
|
|
|
|
2018-05-04 08:23:38 +00:00
|
|
|
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-08-31 08:09:38 +00:00
|
|
|
|
2018-05-08 10:38:54 +00:00
|
|
|
package whisperv6
|
2018-08-31 08:09:38 +00:00
|
|
|
|
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-08-31 08:09:38 +00:00
|
|
|
|
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
|
2018-05-04 08:23:38 +00:00
|
|
|
index c42d1fa8a..3b65fdba0 100644
|
2018-05-08 10:38:54 +00:00
|
|
|
--- a/whisper/whisperv6/envelope.go
|
|
|
|
+++ b/whisper/whisperv6/envelope.go
|
2018-05-04 08:23:38 +00:00
|
|
|
@@ -62,9 +62,9 @@ func (e *Envelope) rlpWithoutNonce() []byte {
|
2018-08-31 08:09:38 +00:00
|
|
|
|
2018-05-04 08:23:38 +00:00
|
|
|
// 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
|
2018-05-04 08:23:38 +00:00
|
|
|
index 2d4e86244..a12b445e2 100644
|
2018-05-08 10:38:54 +00:00
|
|
|
--- a/whisper/whisperv6/message.go
|
|
|
|
+++ b/whisper/whisperv6/message.go
|
2018-05-04 08:23:38 +00:00
|
|
|
@@ -27,6 +27,7 @@ import (
|
|
|
|
"errors"
|
|
|
|
mrand "math/rand"
|
|
|
|
"strconv"
|
|
|
|
+ "time"
|
2018-08-31 08:09:38 +00:00
|
|
|
|
2018-05-04 08:23:38 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
|
|
|
@@ -234,7 +235,7 @@ func generateSecureRandomData(length int) ([]byte, error) {
|
|
|
|
}
|
2018-08-31 08:09:38 +00:00
|
|
|
|
2018-05-04 08:23:38 +00:00
|
|
|
// 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
|
|
|
|
}
|
2018-08-31 08:09:38 +00:00
|
|
|
|
2018-05-04 08:23:38 +00:00
|
|
|
- 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
|
2018-08-31 08:09:38 +00:00
|
|
|
@@ -93,4 +92,6 @@ type Whisper struct {
|
2018-05-04 08:23:38 +00:00
|
|
|
envelopeFeed event.Feed
|
|
|
|
+
|
|
|
|
+ timeSource func() time.Time // source of time for whisper
|
|
|
|
}
|
2018-08-31 08:09:38 +00:00
|
|
|
|
2018-05-04 08:23:38 +00:00
|
|
|
// 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,
|
2018-05-04 08:23:38 +00:00
|
|
|
}
|
2018-08-31 08:09:38 +00:00
|
|
|
|
2018-05-04 08:23:38 +00:00
|
|
|
whisper.filters = NewFilters(whisper)
|
2018-05-08 10:38:54 +00:00
|
|
|
@@ -215,6 +218,11 @@ func (whisper *Whisper) APIs() []rpc.API {
|
2018-05-04 08:23:38 +00:00
|
|
|
}
|
|
|
|
}
|
2018-08-31 08:09:38 +00:00
|
|
|
|
2018-05-04 08:23:38 +00:00
|
|
|
+// 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 {
|
2018-05-04 08:23:38 +00:00
|
|
|
// 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
|
2018-08-31 08:09:38 +00:00
|
|
|
|
2018-05-04 08:23:38 +00:00
|
|
|
if sent > now {
|
2018-05-08 10:38:54 +00:00
|
|
|
@@ -988,7 +996,7 @@ func (whisper *Whisper) expire() {
|
2018-05-04 08:23:38 +00:00
|
|
|
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
|