whisper: respect the PoW requirement

This commit is contained in:
Vlad 2017-12-18 17:37:08 +02:00
parent 0fec3e7f2e
commit c6f32a8c60
3 changed files with 39 additions and 16 deletions

View file

@ -156,7 +156,7 @@ func (p *Peer) broadcast() error {
var cnt int var cnt int
envelopes := p.host.Envelopes() envelopes := p.host.Envelopes()
for _, envelope := range envelopes { for _, envelope := range envelopes {
if !p.marked(envelope) { if !p.marked(envelope) && envelope.PoW() >= p.powRequirement {
err := p2p.Send(p.ws, messagesCode, envelope) err := p2p.Send(p.ws, messagesCode, envelope)
if err != nil { if err != nil {
return err return err

View file

@ -88,23 +88,31 @@ var sharedKey []byte = []byte("some arbitrary data here")
var sharedTopic TopicType = TopicType{0xF, 0x1, 0x2, 0} var sharedTopic TopicType = TopicType{0xF, 0x1, 0x2, 0}
var expectedMessage []byte = []byte("per rectum ad astra") 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) { func TestSimulation(t *testing.T) {
// create a chain of whisper nodes,
// installs the filters with shared (predefined) parameters
initialize(t) initialize(t)
// each node sends a number of random (undecryptable) messages
for i := 0; i < NumNodes; i++ { for i := 0; i < NumNodes; i++ {
sendMsg(t, false, i) sendMsg(t, false, i)
} }
// node #0 sends one expected (decryptable) message
sendMsg(t, true, 0) 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) 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() stopServers()
} }
@ -181,18 +189,21 @@ func stopServers() {
} }
} }
func checkPropagation(t *testing.T) { func checkPropagation(t *testing.T, includingNodeZero bool) {
if t.Failed() { if t.Failed() {
return return
} }
const cycle = 100 const cycle = 50
const iterations = 100 const iterations = 200
first := 0
if !includingNodeZero {
first = 1
}
for j := 0; j < iterations; j++ { for j := 0; j < iterations; j++ {
time.Sleep(cycle * time.Millisecond) for i := first; i < NumNodes; i++ {
for i := 0; i < NumNodes; i++ {
f := nodes[i].shh.GetFilter(nodes[i].filerId) f := nodes[i].shh.GetFilter(nodes[i].filerId)
if f == nil { if f == nil {
t.Fatalf("failed to get filterId %s from node %d.", nodes[i].filerId, i) t.Fatalf("failed to get filterId %s from node %d.", nodes[i].filerId, i)
@ -207,9 +218,18 @@ func checkPropagation(t *testing.T) {
return return
} }
} }
time.Sleep(cycle * time.Millisecond)
} }
t.Fatalf("Test was not complete: timeout %d seconds.", iterations*cycle/1000) 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 { func validateMail(t *testing.T, index int, mail []*ReceivedMessage) bool {

View file

@ -638,7 +638,10 @@ func (wh *Whisper) add(envelope *Envelope) (bool, error) {
if envelope.PoW() < wh.MinPow() { if envelope.PoW() < wh.MinPow() {
log.Debug("envelope with low PoW dropped", "PoW", envelope.PoW(), "hash", envelope.Hash().Hex()) 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() hash := envelope.Hash()