Skip to content

Commit

Permalink
ETCM-207: Test findNode.
Browse files Browse the repository at this point in the history
  • Loading branch information
aakoshh committed Oct 15, 2020
1 parent ba33d33 commit 6c6af63
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 4 deletions.
Expand Up @@ -125,7 +125,8 @@ object DiscoveryService {
def withEnrAndAddress(peer: Peer[A], enr: EthereumNodeRecord, address: Node.Address): State[A] =
copy(
enrMap = enrMap.updated(peer.id, enr),
nodeMap = nodeMap.updated(peer.id, Node(peer.id, address))
nodeMap = nodeMap.updated(peer.id, Node(peer.id, address)),
kBuckets = kBuckets.add(peer.id)
)

def clearBondingResults(peer: Peer[A]): State[A] =
Expand Down Expand Up @@ -179,7 +180,7 @@ object DiscoveryService {
override def updateExternalAddress(address: InetAddress): Task[Unit] = ???
override def localNode: Task[Node] = ???

override def ping =
override def ping: Call[Peer[A], Proc.Ping] =
caller =>
maybeRemoteEnrSeq =>
for {
Expand All @@ -192,9 +193,22 @@ object DiscoveryService {
enrSeq <- stateRef.get.map(_.enr.content.seq)
} yield Some(Some(enrSeq))

override def findNode: Call[Peer[A], Proc.FindNode] = ???
override def findNode: Call[Peer[A], Proc.FindNode] =
caller =>
target =>
ifBonded(caller) {
for {
state <- stateRef.get
closestNodeIds = state.kBuckets.closestNodes(target, config.kademliaBucketSize)
closestNodes = closestNodeIds.map(id => state.nodeMap.get(PublicKey(id))).flatten
} yield closestNodes
}

override def enrRequest: Call[Peer[A], Proc.ENRRequest] = ???

private def ifBonded[T](caller: Peer[A])(thunk: Task[T]): Task[Option[T]] =
isBonded(caller).ifM(thunk.map(Some(_)), Task.pure(None))

def enroll(): Task[Unit] = ???

def startPeriodicRefresh(): Task[Fiber[Task, Unit]] = ???
Expand Down
Expand Up @@ -16,6 +16,7 @@ import scala.concurrent.duration._

class DiscoveryServiceSpec extends AsyncFlatSpec with Matchers {
import DiscoveryService.{State, BondingResults}
import DiscoveryNetworkSpec.{randomKeyPair}
import DiscoveryServiceSpec._

def test(fixture: Fixture) =
Expand Down Expand Up @@ -346,7 +347,7 @@ class DiscoveryServiceSpec extends AsyncFlatSpec with Matchers {
}
}

it should "update the ENR and node maps" in test {
it should "update the ENR, node maps and the k-buckets" in test {
new Fixture {
override lazy val rpc = unimplementedRPC.copy(
enrRequest = _ => _ => Task(Some(remoteENR))
Expand All @@ -358,6 +359,7 @@ class DiscoveryServiceSpec extends AsyncFlatSpec with Matchers {
state.fetchEnrMap should not contain key(remotePeer)
state.nodeMap should contain key (remotePeer.id)
state.enrMap(remotePeer.id) shouldBe remoteENR
state.kBuckets.contains(remotePeer.id) shouldBe true
}
}
}
Expand Down Expand Up @@ -415,6 +417,40 @@ class DiscoveryServiceSpec extends AsyncFlatSpec with Matchers {
}
}

behavior of "findNode"

it should "not respond to unbonded peers" in test {
new Fixture {
override val test = for {
maybeResponse <- service.findNode(remotePeer)(remotePublicKey)
} yield {
maybeResponse shouldBe empty
}
}
}

it should "return peers for who we have an ENR record" in test {
new Fixture {
val caller = {
val (callerPublicKey, _) = randomKeyPair
val callerAddress = aRandomAddress
Peer(callerPublicKey, callerAddress)
}

override val test = for {
// Pretend we are bonded with the caller and know about the remote node.
_ <- stateRef.update {
_.withLastPongTimestamp(caller, System.currentTimeMillis)
.withEnrAndAddress(remotePeer, remoteENR, remoteNode.address)
}
maybeNodes <- service.findNode(caller)(remotePublicKey)
} yield {
maybeNodes should not be empty
maybeNodes.get should have size 2
}
}
}

behavior of "getNode"
it should "return the local node" in (pending)
it should "not return a nodes which is not bonded" in (pending)
Expand Down

0 comments on commit 6c6af63

Please sign in to comment.