From c6f32a8c6088d575698b3b111ae052d98286a5e9 Mon Sep 17 00:00:00 2001 From: Vlad Date: Mon, 18 Dec 2017 17:37:08 +0200 Subject: [PATCH] whisper: respect the PoW requirement --- whisper/whisperv6/peer.go | 2 +- whisper/whisperv6/peer_test.go | 48 ++++++++++++++++++++++++---------- whisper/whisperv6/whisper.go | 5 +++- 3 files changed, 39 insertions(+), 16 deletions(-) diff --git a/whisper/whisperv6/peer.go b/whisper/whisperv6/peer.go index 29d13bc6ef..f4611b8a71 100644 --- a/whisper/whisperv6/peer.go +++ b/whisper/whisperv6/peer.go @@ -156,7 +156,7 @@ func (p *Peer) broadcast() error { var cnt int envelopes := p.host.Envelopes() for _, envelope := range envelopes { - if !p.marked(envelope) { + if !p.marked(envelope) && envelope.PoW() >= p.powRequirement { err := p2p.Send(p.ws, messagesCode, envelope) if err != nil { return err diff --git a/whisper/whisperv6/peer_test.go b/whisper/whisperv6/peer_test.go index 7dd93238c8..40b4cc5ead 100644 --- a/whisper/whisperv6/peer_test.go +++ b/whisper/whisperv6/peer_test.go @@ -88,23 +88,31 @@ var sharedKey []byte = []byte("some arbitrary data here") var sharedTopic TopicType = TopicType{0xF, 0x1, 0x2, 0} var expectedMessage []byte = []byte("per rectum ad astra") -// This test does the following: -// 1. creates a chain of whisper nodes, -// 2. installs the filters with shared (predefined) parameters, -// 3. each node sends a number of random (undecryptable) messages, -// 4. first node sends one expected (decryptable) message, -// 5. checks if each node have received and decrypted exactly one message, -// 6. sends protocol-level messages (powRequirementCode) and checks the new PoW requirement values. func TestSimulation(t *testing.T) { + // create a chain of whisper nodes, + // installs the filters with shared (predefined) parameters initialize(t) + // each node sends a number of random (undecryptable) messages for i := 0; i < NumNodes; i++ { sendMsg(t, false, i) } + // node #0 sends one expected (decryptable) message sendMsg(t, true, 0) - checkPropagation(t) + + // check if each node have received and decrypted exactly one message + checkPropagation(t, true) + + // send protocol-level messages (powRequirementCode) and check the new PoW requirement values powReqExchange(t) + + // node #1 sends one expected (decryptable) message + sendMsg(t, true, 1) + + // check if each node (except node #0) have received and decrypted exactly one message + checkPropagation(t, false) + stopServers() } @@ -181,18 +189,21 @@ func stopServers() { } } -func checkPropagation(t *testing.T) { +func checkPropagation(t *testing.T, includingNodeZero bool) { if t.Failed() { return } - const cycle = 100 - const iterations = 100 + const cycle = 50 + const iterations = 200 + + first := 0 + if !includingNodeZero { + first = 1 + } for j := 0; j < iterations; j++ { - time.Sleep(cycle * time.Millisecond) - - for i := 0; i < NumNodes; i++ { + for i := first; i < NumNodes; i++ { f := nodes[i].shh.GetFilter(nodes[i].filerId) if f == nil { t.Fatalf("failed to get filterId %s from node %d.", nodes[i].filerId, i) @@ -207,9 +218,18 @@ func checkPropagation(t *testing.T) { return } } + + time.Sleep(cycle * time.Millisecond) } t.Fatalf("Test was not complete: timeout %d seconds.", iterations*cycle/1000) + + if !includingNodeZero { + f := nodes[0].shh.GetFilter(nodes[0].filerId) + if f != nil { + t.Fatalf("node zero received a message with low PoW.") + } + } } func validateMail(t *testing.T, index int, mail []*ReceivedMessage) bool { diff --git a/whisper/whisperv6/whisper.go b/whisper/whisperv6/whisper.go index ad1897e802..1cdea2aba3 100644 --- a/whisper/whisperv6/whisper.go +++ b/whisper/whisperv6/whisper.go @@ -638,7 +638,10 @@ func (wh *Whisper) add(envelope *Envelope) (bool, error) { if envelope.PoW() < wh.MinPow() { log.Debug("envelope with low PoW dropped", "PoW", envelope.PoW(), "hash", envelope.Hash().Hex()) - return false, nil // drop envelope without error + return false, nil // drop envelope without error for now + + // after the Status message will include the PoW requirement, it should return an error here: + //return false, fmt.Errorf("envelope with low PoW dropped: PoW=%f, hash=[%v]", envelope.PoW(), envelope.Hash().Hex()) } hash := envelope.Hash()