Skip to content

Commit bec590a

Browse files
authored
pss: Use distance to determine single guaranteed recipient (ethersphere#1672)
1 parent 60d07de commit bec590a

File tree

1 file changed

+20
-17
lines changed

1 file changed

+20
-17
lines changed

pss/prox_test.go

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -136,22 +136,26 @@ func (td *testData) init(msgCount int) error {
136136
msgAddr := pot.RandomAddress() // we choose message addresses randomly
137137
td.recipientAddresses = append(td.recipientAddresses, msgAddr.Bytes())
138138
smallestPo := 256
139-
var targets []enode.ID
140-
var closestPO int
139+
var target enode.ID
140+
var closestPO int = -1
141141

142142
// loop through all nodes and find the required and allowed recipients of each message
143143
// (for more information, please see the comment to the main test function)
144144
for _, nod := range td.sim.Net.GetNodes() {
145145
po, _ := pof(td.recipientAddresses[i], td.nodeAddresses[nod.ID()], 0)
146146
depth := td.kademlias[nod.ID()].NeighbourhoodDepth()
147147

148-
// only nodes with closest IDs (wrt the msg address) will be required recipients
148+
// only the node that is the closest in terms of distance is the required recipient
149149
if po > closestPO {
150150
closestPO = po
151-
targets = nil
152-
targets = append(targets, nod.ID())
151+
target = nod.ID()
153152
} else if po == closestPO {
154-
targets = append(targets, nod.ID())
153+
compareResult, err := pot.DistanceCmp(msgAddr.Bytes(), td.nodeAddresses[nod.ID()], td.nodeAddresses[target])
154+
if err != nil {
155+
return err
156+
} else if compareResult == 1 {
157+
target = nod.ID()
158+
}
155159
}
156160

157161
if po >= depth {
@@ -163,15 +167,20 @@ func (td *testData) init(msgCount int) error {
163167
if po < smallestPo {
164168
smallestPo = po
165169
td.senders[i] = nod.ID()
170+
} else if po == smallestPo {
171+
compareResult, err := pot.DistanceCmp(msgAddr.Bytes(), td.nodeAddresses[nod.ID()], td.nodeAddresses[td.senders[i]])
172+
if err != nil {
173+
return err
174+
} else if compareResult == -1 {
175+
td.senders[i] = nod.ID()
176+
}
166177
}
167178
}
168179

169-
td.requiredMsgCount += len(targets)
170-
for _, id := range targets {
171-
td.requiredMsgs[id] = append(td.requiredMsgs[id], uint64(i))
172-
}
180+
td.requiredMsgCount += 1
181+
td.requiredMsgs[target] = append(td.requiredMsgs[target], uint64(i))
173182

174-
log.Debug("nn for msg", "targets", len(targets), "msgidx", i, "msg", common.Bytes2Hex(msgAddr[:8]), "sender", td.senders[i], "senderpo", smallestPo)
183+
log.Debug("nn for msg", "msgidx", i, "msg", common.Bytes2Hex(msgAddr[:8]), "sender", td.senders[i], "senderpo", smallestPo, "closestAddr", td.nodeAddresses[target])
175184
}
176185
log.Debug("recipientAddresses to receive", "count", td.requiredMsgCount)
177186
return nil
@@ -441,12 +450,6 @@ func newProxServices(td *testData, allowRaw bool, handlerContextFuncs map[Topic]
441450
deregisters = append(deregisters, ps.Register(&tpc, hndlrFunc(td, ctx.Config)))
442451
}
443452

444-
// if handshake mode is set, add the controller
445-
// TODO: This should be hooked to the handshake test file
446-
if useHandshake {
447-
SetHandshakeController(ps, NewHandshakeParams())
448-
}
449-
450453
// we expose some api calls for cheating
451454
ps.addAPI(rpc.API{
452455
Namespace: "psstest",

0 commit comments

Comments
 (0)