File tree Expand file tree Collapse file tree 3 files changed +34
-1
lines changed
src/io/iohk/scalanet/discovery/ethereum/v4 Expand file tree Collapse file tree 3 files changed +34
-1
lines changed Original file line number Diff line number Diff line change @@ -188,8 +188,13 @@ object DiscoveryService {
188
188
)
189
189
}
190
190
191
+ /** Update the timestamp of the peer in the K-table, if it's still part of it. */
191
192
def withTouch (peer : Peer [A ]): State [A ] =
192
- copy(kBuckets = kBuckets.touch(peer.kademliaId))
193
+ if (kBuckets.contains(peer.kademliaId))
194
+ copy(kBuckets = kBuckets.touch(peer.kademliaId))
195
+ else
196
+ // Not adding because `kademliaIdToNodeId` and `nodeMap` may no longer have this peer.
197
+ this
193
198
194
199
def clearBondingResults (peer : Peer [A ]): State [A ] =
195
200
copy(bondingResultsMap = bondingResultsMap - peer)
Original file line number Diff line number Diff line change @@ -1003,6 +1003,30 @@ class DiscoveryServiceSpec extends AsyncFlatSpec with Matchers {
1003
1003
}
1004
1004
}
1005
1005
}
1006
+
1007
+ behavior of " withTouch"
1008
+
1009
+ it should " not touch a peer not already in the k-table" in test {
1010
+ new Fixture {
1011
+ override val test = for {
1012
+ state0 <- stateRef.get
1013
+ state1 = state0.withTouch(remotePeer)
1014
+ state2 = state1.withEnrAndAddress(remotePeer, remoteENR, remoteNode.address)
1015
+ _ <- Task .sleep(1 .milli) // If we're too quick then TimeSet will assign the same timestamp.
1016
+ state3 = state2.withTouch(remotePeer)
1017
+ } yield {
1018
+ val peerId = remotePeer.kademliaId
1019
+
1020
+ state0.kBuckets.contains(peerId) shouldBe false
1021
+ state1.kBuckets.contains(peerId) shouldBe false
1022
+ state2.kBuckets.contains(peerId) shouldBe true
1023
+
1024
+ val (_, bucket2) = state2.kBuckets.getBucket(peerId)
1025
+ val (_, bucket3) = state3.kBuckets.getBucket(peerId)
1026
+ bucket2.timestamps should not equal bucket3.timestamps
1027
+ }
1028
+ }
1029
+ }
1006
1030
}
1007
1031
1008
1032
object DiscoveryServiceSpec {
Original file line number Diff line number Diff line change @@ -36,6 +36,10 @@ class KBucketsSpec extends FlatSpec {
36
36
kb.add(v).contains(v) shouldBe true
37
37
}
38
38
39
+ they should " retrieve any node added via touch" in forAll(genBitVector()) { v =>
40
+ kb.touch(v).contains(v) shouldBe true
41
+ }
42
+
39
43
they should " not retrieve any node removed via remove" in forAll(genBitVector()) { v =>
40
44
kb.add(v).remove(v).contains(v) shouldBe false
41
45
}
You can’t perform that action at this time.
0 commit comments