diff --git a/whisper/peer_test.go b/whisper/peer_test.go index 594671ee14c80a26eac49be83c598b3e82f45684..b3d2031c194694c7eff7d56bb382d67a703083e1 100644 --- a/whisper/peer_test.go +++ b/whisper/peer_test.go @@ -239,14 +239,17 @@ func TestPeerMessageExpiration(t *testing.T) { } payload := []interface{}{envelope} if err := p2p.ExpectMsg(tester.stream, messagesCode, payload); err != nil { - t.Fatalf("message mismatch: %v", err) + // A premature empty message may have been broadcast, check the next too + if err := p2p.ExpectMsg(tester.stream, messagesCode, payload); err != nil { + t.Fatalf("message mismatch: %v", err) + } } // Check that the message is inside the cache if !peer.known.Has(envelope.Hash()) { t.Fatalf("message not found in cache") } // Discard messages until expiration and check cache again - exp := time.Now().Add(time.Second + expirationCycle) + exp := time.Now().Add(time.Second + 2*expirationCycle + 100*time.Millisecond) for time.Now().Before(exp) { if err := p2p.ExpectMsg(tester.stream, messagesCode, []interface{}{}); err != nil { t.Fatalf("message mismatch: %v", err) diff --git a/whisper/whisper.go b/whisper/whisper.go index 676d8ae7a3c5eccf37ab40ec9482df3b7f4aaa0b..a341f23e48b63ae8ef24e1229437e20de21ad9c4 100644 --- a/whisper/whisper.go +++ b/whisper/whisper.go @@ -234,6 +234,11 @@ func (self *Whisper) add(envelope *Envelope) error { self.poolMu.Lock() defer self.poolMu.Unlock() + // short circuit when a received envelope has already expired + if envelope.Expiry <= uint32(time.Now().Unix()) { + return nil + } + // Insert the message into the tracked pool hash := envelope.Hash() if _, ok := self.messages[hash]; ok { diff --git a/whisper/whisper_test.go b/whisper/whisper_test.go index 1a9a8667ada53687cfb9e58e8670b6f9740c8265..b83ce0fe7026c332d6d61cd6c329f7d57472c2c3 100644 --- a/whisper/whisper_test.go +++ b/whisper/whisper_test.go @@ -207,4 +207,13 @@ func TestMessageExpiration(t *testing.T) { if found { t.Fatalf("message not expired from cache") } + + node.add(envelope) + node.poolMu.RLock() + _, found = node.messages[envelope.Hash()] + node.poolMu.RUnlock() + if found { + t.Fatalf("message was added to cache") + } + }