swarm/pss: comments added

This commit is contained in:
Vlad 2018-12-14 13:31:46 +04:00
parent f1eff7bf92
commit bcf32d2f3a
2 changed files with 14 additions and 7 deletions

View file

@ -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 {

View file

@ -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