Skip to content

Commit feef9b2

Browse files
authored
Merge pull request #101 from input-output-hk/ETCM-168-discovery-part4
ETCM-168: ENR content constructor and extra logging
2 parents 7672926 + 495ada3 commit feef9b2

File tree

4 files changed

+30
-18
lines changed

4 files changed

+30
-18
lines changed

scalanet/discovery/src/io/iohk/scalanet/discovery/ethereum/EthereumNodeRecord.scala

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ object EthereumNodeRecord {
2525
// Normally clients treat the values as RLP, however we don't have access to the RLP types here, hence it's just bytes.
2626
attrs: SortedMap[ByteVector, ByteVector]
2727
)
28+
object Content {
29+
def apply(seq: Long, attrs: (ByteVector, ByteVector)*): Content =
30+
Content(seq, SortedMap(attrs: _*))
31+
}
2832

2933
object Keys {
3034
private def key(k: String): ByteVector =
@@ -61,14 +65,14 @@ object EthereumNodeRecord {
6165
def apply(signature: Signature, seq: Long, attrs: (ByteVector, ByteVector)*): EthereumNodeRecord =
6266
EthereumNodeRecord(
6367
signature,
64-
EthereumNodeRecord.Content(seq, SortedMap(attrs: _*))
68+
EthereumNodeRecord.Content(seq, attrs: _*)
6569
)
6670

6771
def apply(privateKey: PrivateKey, seq: Long, attrs: (ByteVector, ByteVector)*)(
6872
implicit sigalg: SigAlg,
6973
codec: Codec[Content]
7074
): Attempt[EthereumNodeRecord] = {
71-
val content = EthereumNodeRecord.Content(seq, SortedMap(attrs: _*))
75+
val content = EthereumNodeRecord.Content(seq, attrs: _*)
7276
codec.encode(content).map { data =>
7377
val sig = sigalg.removeRecoveryId(sigalg.sign(privateKey, data))
7478
EthereumNodeRecord(sig, content)

scalanet/discovery/src/io/iohk/scalanet/discovery/ethereum/v4/DiscoveryNetwork.scala

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,8 @@ object DiscoveryNetwork {
134134
}
135135

136136
case Attempt.Failure(err) =>
137-
Task.raiseError(
138-
new PacketException(s"Failed to unpack message: $err")
139-
)
137+
Task(logger.debug(s"Failed to unpack packet: $err; ${packet.show}")) >>
138+
Task.raiseError(new PacketException(s"Failed to unpack message: $err"))
140139
}
141140
}
142141

scalanet/discovery/src/io/iohk/scalanet/discovery/ethereum/v4/DiscoveryService.scala

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -798,7 +798,8 @@ object DiscoveryService {
798798

799799
/** Look up a random node ID to discover new peers. */
800800
protected[v4] def lookupRandom(): Task[Unit] =
801-
lookup(target = sigalg.newKeyPair._1).void
801+
Task(logger.info("Looking up a random target...")) >>
802+
lookup(target = sigalg.newKeyPair._1).void
802803

803804
/** Look up self with the bootstrap nodes. First we have to fetch their ENR
804805
* records to verify they are reachable and so that they can participate
@@ -812,23 +813,26 @@ object DiscoveryService {
812813
if (config.knownPeers.isEmpty)
813814
Task.pure(false)
814815
else {
815-
val tryEnroll = for {
816+
for {
816817
nodeId <- stateRef.get.map(_.node.id)
817818
bootstrapPeers = config.knownPeers.toList.map(toPeer).filterNot(_.id == nodeId)
819+
_ <- Task(logger.info(s"Enrolling with ${bootstrapPeers.size} bootstrap nodes."))
818820
maybeBootstrapEnrs <- Task.parTraverseN(config.kademliaAlpha)(bootstrapPeers)(fetchEnr(_, delay = true))
819-
result <- if (maybeBootstrapEnrs.exists(_.isDefined)) {
820-
lookup(nodeId).as(true)
821+
enrolled = maybeBootstrapEnrs.count(_.isDefined)
822+
succeeded = enrolled > 0
823+
_ <- if (succeeded) {
824+
for {
825+
_ <- Task(
826+
logger.info(s"Successfully enrolled with $enrolled bootstrap nodes. Performing initial lookup...")
827+
)
828+
_ <- lookup(nodeId)
829+
nodeCount <- stateRef.get.map(_.nodeMap.size)
830+
_ <- Task(logger.info(s"Discovered $nodeCount nodes by the end of the lookup."))
831+
} yield ()
821832
} else {
822-
Task.pure(false)
823-
}
824-
} yield result
825-
826-
tryEnroll.flatTap {
827-
case true =>
828-
Task(logger.info("Successfully enrolled with some of the bootstrap nodes."))
829-
case false =>
830833
Task(logger.warn("Failed to enroll with any of the the bootstrap nodes."))
831-
}
834+
}
835+
} yield succeeded
832836
}
833837
}
834838
}

scalanet/discovery/src/io/iohk/scalanet/discovery/ethereum/v4/Packet.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.iohk.scalanet.discovery.ethereum.v4
22

3+
import cats.Show
34
import io.iohk.scalanet.discovery.hash.{Hash, Keccak256}
45
import io.iohk.scalanet.discovery.crypto.{SigAlg, PrivateKey, PublicKey, Signature}
56
import scodec.bits.BitVector
@@ -86,4 +87,8 @@ object Packet {
8687
publicKey <- sigalg.recoverPublicKey(packet.signature, packet.data)
8788
payload <- codec.decodeValue(packet.data)
8889
} yield (payload, publicKey)
90+
91+
implicit val show: Show[Packet] = Show.show[Packet] { p =>
92+
s"""Packet(hash = hex"${p.hash.toHex}", signature = hex"${p.signature.toHex}", data = hex"${p.data.toHex}")"""
93+
}
8994
}

0 commit comments

Comments
 (0)