Skip to content

Commit

Permalink
Expand boostrap node list
Browse files Browse the repository at this point in the history
  • Loading branch information
lavrov committed Aug 30, 2024
1 parent 653f495 commit 613b9a0
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 15 deletions.
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ lazy val root = project

inThisBuild(
List(
scalaVersion := "3.3.0",
scalaVersion := "3.5.0",
scalacOptions ++= List(
"-source:future",
"-Ykind-projector:underscores"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ package com.github.torrentdam.bittorrent.dht
import cats.effect.kernel.Temporal
import cats.implicits.*
import cats.MonadError
import cats.effect.implicits.*
import com.comcast.ip4s.*
import org.legogroup.woof.given
import org.legogroup.woof.Logger
import fs2.Stream

import scala.concurrent.duration.*

object RoutingTableBootstrap {
Expand All @@ -20,33 +23,46 @@ object RoutingTableBootstrap {
): F[Unit] =
for {
_ <- logger.info("Bootstrapping")
seedInfo <- resolveSeedNode(client)
seedInfo <- resolveBootstrapNode(client)
response <- client.findNodes(seedInfo, seedInfo.id)
_ <- response.nodes.traverse(table.insert)
_ <- logger.info(s"Bootstrap completed with ${response.nodes.size} nodes")
} yield {}

def resolveSeedNode[F[_]](
def resolveBootstrapNode[F[_]](
client: Client[F]
)(using
F: Temporal[F],
dns: Dns[F],
logger: Logger[F]
): F[NodeInfo] =
SeedNodeAddress.resolve[F].flatMap { seedAddress =>
def loop: F[NodeInfo] =
def tryThis(hostname: SocketAddress[Hostname]): F[Option[NodeInfo]] =
logger.info(s"Trying $hostname") >>
hostname.resolve[F].flatMap { seedAddress =>
client
.ping(seedAddress)
.map(pong => NodeInfo(pong.id, seedAddress))
.timeout(5.seconds)
.map(pong => NodeInfo(pong.id, seedAddress).some)
.recoverWith { case e =>
val msg = e.getMessage
logger.info(s"Bootstrap failed $msg $e") >> F.sleep(5.seconds) >> loop
logger.info(s"Bootstrap failed $hostname $msg $e").as(none)
}
logger.info(s"Resolve seed node $seedAddress") >>
loop.flatTap { nodeInfo =>
logger.info(s"Resolved as ${nodeInfo.id}")
}
}
Stream.emits(SeedNodeAddressList)
.covary[F]
.evalMap(tryThis)
.collectFirst {
case Some(node) => node
}
.compile
.lastOrError
.flatTap(node => logger.info(s"Bootstrap node resolved: $node"))

private val SeedNodeAddress = SocketAddress(host"router.bittorrent.com", port"6881")
private val SeedNodeAddressList = List(
SocketAddress(host"router.bittorrent.com", port"6881"),
SocketAddress(host"router.utorrent.com", port"6881"),
SocketAddress(host"dht.transmissionbt.com", port"6881"),
SocketAddress(host"router.bitcomet.com", port"6881"),
SocketAddress(host"dht.aelitis.com", port"6881"),
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ object Message {

val PeersResponseFormat: BencodeFormat[Response.Peers] = (
field[NodeId]("id"),
field[List[PeerInfo]]("values")(BencodeFormat.listFormat(encodedString(CompactPeerInfoCodec)))
field[List[PeerInfo]]("values")(using BencodeFormat.listFormat(using encodedString(CompactPeerInfoCodec)))
).imapN[Response.Peers](Response.Peers.apply)(v => (v.id, v.peers))

val SampleInfoHashesResponseFormat: BencodeFormat[Response.SampleInfoHashes] = (
Expand Down
2 changes: 1 addition & 1 deletion project/build.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
sbt.version=1.8.2
sbt.version=1.10.1
2 changes: 1 addition & 1 deletion project/plugins.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ addSbtPlugin("com.github.sbt" % "sbt-pgp" % "2.2.1")
addSbtPlugin("com.github.sbt" % "sbt-native-packager" % "1.9.9")

// Multi-platform support
addSbtPlugin("org.scala-js" % "sbt-scalajs" % "1.13.2")
addSbtPlugin("org.scala-js" % "sbt-scalajs" % "1.16.0")
addSbtPlugin("org.portable-scala" % "sbt-scalajs-crossproject" % "1.2.0")
addSbtPlugin("org.portable-scala" % "sbt-scala-native-crossproject" % "1.2.0")
addSbtPlugin("org.scala-native" % "sbt-scala-native" % "0.4.17")

0 comments on commit 613b9a0

Please sign in to comment.