Skip to content

Commit

Permalink
Merge pull request #101 from input-output-hk/ETCM-168-discovery-part4
Browse files Browse the repository at this point in the history
ETCM-168: ENR content constructor and extra logging
  • Loading branch information
aakoshh authored Nov 6, 2020
2 parents 7672926 + 495ada3 commit feef9b2
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
}
}
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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}")"""
}
}

0 comments on commit feef9b2

Please sign in to comment.