mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-19 18:32:23 +00:00
swarm/pss: comments added
This commit is contained in:
parent
f1eff7bf92
commit
bcf32d2f3a
2 changed files with 14 additions and 7 deletions
|
|
@ -14,12 +14,16 @@ import (
|
||||||
whisper "github.com/ethereum/go-ethereum/whisper/whisperv5"
|
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) {
|
func TestForwardBasic(t *testing.T) {
|
||||||
base := newBaseAddress() // 0xFFFFFF.......
|
base := newBaseAddress() // 0xFFFFFF.......
|
||||||
var peerAddresses []pot.Address
|
var peerAddresses []pot.Address
|
||||||
var dst pot.Address
|
var dst pot.Address
|
||||||
const depth = 9
|
const depth = 9
|
||||||
for i := 0; i <= depth; i++ {
|
for i := 0; i <= depth; i++ {
|
||||||
|
// add two peers for each proximity order (same as in live system)
|
||||||
a := pot.RandomAddressAt(base, i)
|
a := pot.RandomAddressAt(base, i)
|
||||||
peerAddresses = append(peerAddresses, a)
|
peerAddresses = append(peerAddresses, a)
|
||||||
a = pot.RandomAddressAt(base, i)
|
a = pot.RandomAddressAt(base, i)
|
||||||
|
|
@ -87,10 +91,13 @@ func TestForwardBasic(t *testing.T) {
|
||||||
testForwardMsg(900, t, ps, peerAddresses[19][:1], peerAddresses, all[16:])
|
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)
|
testResMap := make(map[pot.Address]int)
|
||||||
msg := newTestMsg(addr)
|
msg := newTestMsg(addr)
|
||||||
ps.forward(msg, func(p *Pss, sp *network.Peer, msg *PssMsg) bool {
|
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())
|
a := pot.NewAddressFromBytes(sp.Address())
|
||||||
testResMap[a]++
|
testResMap[a]++
|
||||||
return true
|
return true
|
||||||
|
|
@ -98,7 +105,7 @@ func testForwardMsg(num int, t *testing.T, ps *Pss, addr []byte, addresses []pot
|
||||||
|
|
||||||
// check test results
|
// check test results
|
||||||
var fail bool
|
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
|
// false negatives
|
||||||
for _, i := range expected {
|
for _, i := range expected {
|
||||||
|
|
|
||||||
|
|
@ -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
|
// 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
|
var isPssEnabled bool
|
||||||
info := sp.Info()
|
info := sp.Info()
|
||||||
for _, capability := range info.Caps {
|
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
|
// 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
|
// 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.
|
// successfully forwarded to at least one peer.
|
||||||
func (p *Pss) forward(msg *PssMsg, trySend func(p *Pss, sp *network.Peer, msg *PssMsg) bool) error {
|
func (p *Pss) forward(msg *PssMsg, sendMsg func(p *Pss, sp *network.Peer, msg *PssMsg) bool) error {
|
||||||
if trySend == nil {
|
if sendMsg == nil {
|
||||||
trySend = trySendMsg
|
sendMsg = sendMessage
|
||||||
}
|
}
|
||||||
|
|
||||||
metrics.GetOrRegisterCounter("pss.forward", nil).Inc(1)
|
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 {
|
if po < depth && sent > 0 {
|
||||||
return false // stop iterating
|
return false // stop iterating
|
||||||
}
|
}
|
||||||
if trySend(p, sp, msg) {
|
if sendMsg(p, sp, msg) {
|
||||||
sent++
|
sent++
|
||||||
}
|
}
|
||||||
return po < addressLength*8 // stop iterating in case of exact match of full address
|
return po < addressLength*8 // stop iterating in case of exact match of full address
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue