From bcf32d2f3aa9e5c514edab2b42af4c291f2377d5 Mon Sep 17 00:00:00 2001 From: Vlad Date: Fri, 14 Dec 2018 13:31:46 +0400 Subject: [PATCH] swarm/pss: comments added --- swarm/pss/forwarding_test.go | 11 +++++++++-- swarm/pss/pss.go | 10 +++++----- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/swarm/pss/forwarding_test.go b/swarm/pss/forwarding_test.go index 5706185e11..3b67733e9e 100644 --- a/swarm/pss/forwarding_test.go +++ b/swarm/pss/forwarding_test.go @@ -14,12 +14,16 @@ import ( whisper "github.com/ethereum/go-ethereum/whisper/whisperv5" ) +// the purpose of this test is to see that pss.forward() function correctly +// selects the peers for message forwarding, depending on the message address +// and kademlia constellation. func TestForwardBasic(t *testing.T) { base := newBaseAddress() // 0xFFFFFF....... var peerAddresses []pot.Address var dst pot.Address const depth = 9 for i := 0; i <= depth; i++ { + // add two peers for each proximity order (same as in live system) a := pot.RandomAddressAt(base, i) peerAddresses = append(peerAddresses, a) a = pot.RandomAddressAt(base, i) @@ -87,10 +91,13 @@ func TestForwardBasic(t *testing.T) { testForwardMsg(900, t, ps, peerAddresses[19][:1], peerAddresses, all[16:]) } -func testForwardMsg(num int, t *testing.T, ps *Pss, addr []byte, addresses []pot.Address, expected []int) { +// this function tests the forwarding of a single message. the recipient address (addr) is passed as param, +// along with addreses of all peers, and indexes of those peers which are expected to receive the message. +func testForwardMsg(testID int, t *testing.T, ps *Pss, addr []byte, addresses []pot.Address, expected []int) { testResMap := make(map[pot.Address]int) msg := newTestMsg(addr) ps.forward(msg, func(p *Pss, sp *network.Peer, msg *PssMsg) bool { + // this function substitutes the real send function, since we only want to test the peer selection functionality a := pot.NewAddressFromBytes(sp.Address()) testResMap[a]++ return true @@ -98,7 +105,7 @@ func testForwardMsg(num int, t *testing.T, ps *Pss, addr []byte, addresses []pot // check test results var fail bool - s := fmt.Sprintf("test id: %d, msg address: %x..., radius: %d", num, addr[:len(addr)%4], 8*len(addr)) + s := fmt.Sprintf("test id: %d, msg address: %x..., radius: %d", testID, addr[:len(addr)%4], 8*len(addr)) // false negatives for _, i := range expected { diff --git a/swarm/pss/pss.go b/swarm/pss/pss.go index f7fd0785c4..3be6aa3227 100644 --- a/swarm/pss/pss.go +++ b/swarm/pss/pss.go @@ -887,7 +887,7 @@ func (p *Pss) send(to []byte, topic Topic, msg []byte, asymmetric bool, key []by } // tries to send a message, returns true if successful -func trySendMsg(p *Pss, sp *network.Peer, msg *PssMsg) bool { +func sendMessage(p *Pss, sp *network.Peer, msg *PssMsg) bool { var isPssEnabled bool info := sp.Info() for _, capability := range info.Caps { @@ -925,9 +925,9 @@ func trySendMsg(p *Pss, sp *network.Peer, msg *PssMsg) bool { // are any; otherwise only to one peer, closest to the recipient address. In any case, if the message // forwarding fails, the node should try to forward it to the next best peer, until the message is // successfully forwarded to at least one peer. -func (p *Pss) forward(msg *PssMsg, trySend func(p *Pss, sp *network.Peer, msg *PssMsg) bool) error { - if trySend == nil { - trySend = trySendMsg +func (p *Pss) forward(msg *PssMsg, sendMsg func(p *Pss, sp *network.Peer, msg *PssMsg) bool) error { + if sendMsg == nil { + sendMsg = sendMessage } metrics.GetOrRegisterCounter("pss.forward", nil).Inc(1) @@ -956,7 +956,7 @@ func (p *Pss) forward(msg *PssMsg, trySend func(p *Pss, sp *network.Peer, msg *P if po < depth && sent > 0 { return false // stop iterating } - if trySend(p, sp, msg) { + if sendMsg(p, sp, msg) { sent++ } return po < addressLength*8 // stop iterating in case of exact match of full address