diff --git a/scalanet/discovery/src/io/iohk/scalanet/discovery/ethereum/EthereumNodeRecord.scala b/scalanet/discovery/src/io/iohk/scalanet/discovery/ethereum/EthereumNodeRecord.scala index 0f7e7106..1e3b38e7 100644 --- a/scalanet/discovery/src/io/iohk/scalanet/discovery/ethereum/EthereumNodeRecord.scala +++ b/scalanet/discovery/src/io/iohk/scalanet/discovery/ethereum/EthereumNodeRecord.scala @@ -25,6 +25,10 @@ object EthereumNodeRecord { // Normally clients treat the values as RLP, however we don't have access to the RLP types here, hence it's just bytes. attrs: SortedMap[ByteVector, ByteVector] ) + object Content { + def apply(seq: Long, attrs: (ByteVector, ByteVector)*): Content = + Content(seq, SortedMap(attrs: _*)) + } object Keys { private def key(k: String): ByteVector = @@ -61,14 +65,14 @@ object EthereumNodeRecord { def apply(signature: Signature, seq: Long, attrs: (ByteVector, ByteVector)*): EthereumNodeRecord = EthereumNodeRecord( signature, - EthereumNodeRecord.Content(seq, SortedMap(attrs: _*)) + EthereumNodeRecord.Content(seq, attrs: _*) ) def apply(privateKey: PrivateKey, seq: Long, attrs: (ByteVector, ByteVector)*)( implicit sigalg: SigAlg, codec: Codec[Content] ): Attempt[EthereumNodeRecord] = { - val content = EthereumNodeRecord.Content(seq, SortedMap(attrs: _*)) + val content = EthereumNodeRecord.Content(seq, attrs: _*) codec.encode(content).map { data => val sig = sigalg.removeRecoveryId(sigalg.sign(privateKey, data)) EthereumNodeRecord(sig, content) diff --git a/scalanet/discovery/src/io/iohk/scalanet/discovery/ethereum/v4/DiscoveryNetwork.scala b/scalanet/discovery/src/io/iohk/scalanet/discovery/ethereum/v4/DiscoveryNetwork.scala index 9f411414..016862ab 100644 --- a/scalanet/discovery/src/io/iohk/scalanet/discovery/ethereum/v4/DiscoveryNetwork.scala +++ b/scalanet/discovery/src/io/iohk/scalanet/discovery/ethereum/v4/DiscoveryNetwork.scala @@ -134,9 +134,8 @@ object DiscoveryNetwork { } case Attempt.Failure(err) => - Task.raiseError( - new PacketException(s"Failed to unpack message: $err") - ) + Task(logger.debug(s"Failed to unpack packet: $err; ${packet.show}")) >> + Task.raiseError(new PacketException(s"Failed to unpack message: $err")) } } diff --git a/scalanet/discovery/src/io/iohk/scalanet/discovery/ethereum/v4/DiscoveryService.scala b/scalanet/discovery/src/io/iohk/scalanet/discovery/ethereum/v4/DiscoveryService.scala index f45a693f..77bb70f5 100644 --- a/scalanet/discovery/src/io/iohk/scalanet/discovery/ethereum/v4/DiscoveryService.scala +++ b/scalanet/discovery/src/io/iohk/scalanet/discovery/ethereum/v4/DiscoveryService.scala @@ -798,7 +798,8 @@ object DiscoveryService { /** Look up a random node ID to discover new peers. */ protected[v4] def lookupRandom(): Task[Unit] = - lookup(target = sigalg.newKeyPair._1).void + Task(logger.info("Looking up a random target...")) >> + lookup(target = sigalg.newKeyPair._1).void /** Look up self with the bootstrap nodes. First we have to fetch their ENR * records to verify they are reachable and so that they can participate @@ -812,23 +813,26 @@ object DiscoveryService { if (config.knownPeers.isEmpty) Task.pure(false) else { - val tryEnroll = for { + for { nodeId <- stateRef.get.map(_.node.id) bootstrapPeers = config.knownPeers.toList.map(toPeer).filterNot(_.id == nodeId) + _ <- Task(logger.info(s"Enrolling with ${bootstrapPeers.size} bootstrap nodes.")) maybeBootstrapEnrs <- Task.parTraverseN(config.kademliaAlpha)(bootstrapPeers)(fetchEnr(_, delay = true)) - result <- if (maybeBootstrapEnrs.exists(_.isDefined)) { - lookup(nodeId).as(true) + enrolled = maybeBootstrapEnrs.count(_.isDefined) + succeeded = enrolled > 0 + _ <- if (succeeded) { + for { + _ <- Task( + logger.info(s"Successfully enrolled with $enrolled bootstrap nodes. Performing initial lookup...") + ) + _ <- lookup(nodeId) + nodeCount <- stateRef.get.map(_.nodeMap.size) + _ <- Task(logger.info(s"Discovered $nodeCount nodes by the end of the lookup.")) + } yield () } else { - Task.pure(false) - } - } yield result - - tryEnroll.flatTap { - case true => - Task(logger.info("Successfully enrolled with some of the bootstrap nodes.")) - case false => Task(logger.warn("Failed to enroll with any of the the bootstrap nodes.")) - } + } + } yield succeeded } } } diff --git a/scalanet/discovery/src/io/iohk/scalanet/discovery/ethereum/v4/Packet.scala b/scalanet/discovery/src/io/iohk/scalanet/discovery/ethereum/v4/Packet.scala index 35f2382c..e6645a5a 100644 --- a/scalanet/discovery/src/io/iohk/scalanet/discovery/ethereum/v4/Packet.scala +++ b/scalanet/discovery/src/io/iohk/scalanet/discovery/ethereum/v4/Packet.scala @@ -1,5 +1,6 @@ package io.iohk.scalanet.discovery.ethereum.v4 +import cats.Show import io.iohk.scalanet.discovery.hash.{Hash, Keccak256} import io.iohk.scalanet.discovery.crypto.{SigAlg, PrivateKey, PublicKey, Signature} import scodec.bits.BitVector @@ -86,4 +87,8 @@ object Packet { publicKey <- sigalg.recoverPublicKey(packet.signature, packet.data) payload <- codec.decodeValue(packet.data) } yield (payload, publicKey) + + implicit val show: Show[Packet] = Show.show[Packet] { p => + s"""Packet(hash = hex"${p.hash.toHex}", signature = hex"${p.signature.toHex}", data = hex"${p.data.toHex}")""" + } }