2021-01-14 21:01:44 +01:00
|
|
|
import java.nio.file.Files
|
2021-11-27 18:51:34 +01:00
|
|
|
import java.security.MessageDigest
|
2019-08-27 17:48:48 +02:00
|
|
|
import scala.collection.JavaConverters._
|
2019-12-02 16:01:21 +01:00
|
|
|
import scala.concurrent.duration.DurationInt
|
2021-11-27 18:51:34 +01:00
|
|
|
import scala.concurrent.{Await, Future, Promise}
|
2021-01-14 21:01:44 +01:00
|
|
|
import scala.util.Properties
|
2019-08-27 17:48:48 +02:00
|
|
|
|
|
|
|
name := "bitcoin-s-bitcoind-rpc"
|
|
|
|
|
|
|
|
libraryDependencies ++= Deps.bitcoindRpc
|
|
|
|
|
2019-08-30 22:11:52 +02:00
|
|
|
CommonSettings.prodSettings
|
2019-08-27 17:48:48 +02:00
|
|
|
|
2019-08-30 22:11:52 +02:00
|
|
|
TaskKeys.downloadBitcoind := {
|
2019-08-27 17:48:48 +02:00
|
|
|
val logger = streams.value.log
|
|
|
|
import scala.sys.process._
|
|
|
|
|
2020-02-17 19:07:04 +01:00
|
|
|
val binaryDir = CommonSettings.binariesPath.resolve("bitcoind")
|
|
|
|
|
2019-08-27 17:48:48 +02:00
|
|
|
if (Files.notExists(binaryDir)) {
|
|
|
|
logger.info(s"Creating directory for bitcoind binaries: $binaryDir")
|
|
|
|
Files.createDirectories(binaryDir)
|
|
|
|
}
|
|
|
|
|
2019-12-04 14:44:44 +01:00
|
|
|
val versions =
|
2024-06-18 22:40:13 +02:00
|
|
|
List("27.1", "26.1", "25.2")
|
2019-08-27 17:48:48 +02:00
|
|
|
|
|
|
|
logger.debug(
|
|
|
|
s"(Maybe) downloading Bitcoin Core binaries for versions: ${versions.mkString(",")}")
|
|
|
|
|
2022-06-21 20:20:47 +02:00
|
|
|
def getPlatformAndSuffix(version: String): (String, String) = {
|
2019-09-05 16:15:00 +02:00
|
|
|
if (Properties.isLinux) ("x86_64-linux-gnu", "tar.gz")
|
2022-11-28 15:43:49 +01:00
|
|
|
else if (Properties.isMac) {
|
2024-04-24 00:26:23 +02:00
|
|
|
if (versions.contains(version)) {
|
2022-11-28 15:43:49 +01:00
|
|
|
if (System.getProperty("os.arch") == "aarch64")
|
2022-09-23 13:19:25 +02:00
|
|
|
("arm64-apple-darwin", "tar.gz")
|
2022-11-28 15:43:49 +01:00
|
|
|
else ("x86_64-apple-darwin", "tar.gz")
|
|
|
|
} else ("osx64", "tar.gz")
|
|
|
|
} else if (Properties.isWin) ("win64", "zip")
|
2019-08-27 17:48:48 +02:00
|
|
|
else sys.error(s"Unsupported OS: ${Properties.osName}")
|
2022-06-21 20:20:47 +02:00
|
|
|
}
|
2019-08-27 17:48:48 +02:00
|
|
|
|
2019-12-02 16:01:21 +01:00
|
|
|
implicit val ec = scala.concurrent.ExecutionContext.global
|
|
|
|
val downloads = versions.map { version =>
|
2022-06-21 20:20:47 +02:00
|
|
|
val (platform, suffix) = getPlatformAndSuffix(version)
|
2019-09-05 16:15:00 +02:00
|
|
|
val archiveLocation = binaryDir resolve s"$version.$suffix"
|
2019-08-27 17:48:48 +02:00
|
|
|
val location =
|
2022-08-08 21:48:33 +02:00
|
|
|
if (version.init.endsWith("rc")) { // if it is a release candidate
|
2021-01-14 21:01:44 +01:00
|
|
|
val (base, rc) = version.splitAt(version.length - 3)
|
|
|
|
s"https://bitcoincore.org/bin/bitcoin-core-$base/test.$rc/bitcoin-$version-$platform.$suffix"
|
|
|
|
} else
|
2019-09-25 20:18:51 +02:00
|
|
|
s"https://bitcoincore.org/bin/bitcoin-core-$version/bitcoin-$version-$platform.$suffix"
|
2019-08-27 17:48:48 +02:00
|
|
|
|
|
|
|
val expectedEndLocation = binaryDir resolve s"bitcoin-$version"
|
2019-12-02 16:01:21 +01:00
|
|
|
|
2020-09-25 20:29:22 +02:00
|
|
|
if (
|
|
|
|
Files
|
|
|
|
.list(binaryDir)
|
|
|
|
.iterator
|
|
|
|
.asScala
|
|
|
|
.map(_.toString)
|
|
|
|
.exists(expectedEndLocation.toString.startsWith(_))
|
|
|
|
) {
|
2019-08-27 17:48:48 +02:00
|
|
|
logger.debug(
|
|
|
|
s"Directory $expectedEndLocation already exists, skipping download of version $version")
|
2019-12-02 16:01:21 +01:00
|
|
|
Future.unit
|
2019-08-27 17:48:48 +02:00
|
|
|
} else {
|
2021-11-27 18:51:34 +01:00
|
|
|
// copy of FutureUtil.makeAsync
|
|
|
|
def makeAsync(func: () => Unit): Future[Unit] = {
|
|
|
|
val resultP = Promise[Unit]()
|
|
|
|
|
|
|
|
ec.execute { () =>
|
|
|
|
val result: Unit = func()
|
|
|
|
resultP.success(result)
|
|
|
|
}
|
|
|
|
|
|
|
|
resultP.future
|
|
|
|
}
|
|
|
|
|
|
|
|
makeAsync { () =>
|
2019-12-02 16:01:21 +01:00
|
|
|
logger.info(
|
|
|
|
s"Downloading bitcoind version $version from location: $location")
|
|
|
|
logger.info(s"Placing the file in $archiveLocation")
|
|
|
|
val downloadCommand = url(location) #> archiveLocation.toFile
|
|
|
|
downloadCommand.!!
|
|
|
|
|
2021-11-27 18:51:34 +01:00
|
|
|
val bytes = Files.readAllBytes(archiveLocation)
|
|
|
|
val hash = MessageDigest
|
|
|
|
.getInstance("SHA-256")
|
|
|
|
.digest(bytes)
|
|
|
|
.map("%02x" format _)
|
|
|
|
.mkString
|
|
|
|
|
|
|
|
val expectedHash =
|
|
|
|
if (Properties.isLinux)
|
|
|
|
Map(
|
2024-04-27 21:51:45 +02:00
|
|
|
"25.2" -> "8d8c387e597e0edfc256f0bbace1dac3ad1ebf4a3c06da3e2975fda333817dea",
|
2024-04-28 20:16:39 +02:00
|
|
|
"26.1" -> "a5b7d206384a8100058d3f2e2f02123a8e49e83f523499e70e86e121a4897d5b",
|
2024-06-18 22:40:13 +02:00
|
|
|
"27.1" -> "c9840607d230d65f6938b81deaec0b98fe9cb14c3a41a5b13b2c05d044a48422"
|
2021-11-27 18:51:34 +01:00
|
|
|
)
|
|
|
|
else if (Properties.isMac)
|
|
|
|
Map(
|
2024-04-24 00:26:23 +02:00
|
|
|
"25.2" -> (if (System.getProperty("os.arch") == "aarch64")
|
|
|
|
"f55b394eebaa11d4b717d68aad9f75b824aaf3a7841dac7c26b1ef3d6d2915f5"
|
|
|
|
else
|
2024-04-27 21:51:45 +02:00
|
|
|
"e06ba379f6039ca99bc32d3e7974d420a31363498936f88aac7bab6f239de0f5"),
|
|
|
|
"26.1" -> (if (System.getProperty("os.arch") == "aarch64")
|
|
|
|
"8a8e415763b7ffd5988153cf03967d812eca629016dd3b0ddf6da3ab6f4a3621"
|
2024-04-28 20:16:39 +02:00
|
|
|
else
|
2024-04-29 17:01:59 +02:00
|
|
|
"acb50edd20692a9d023de12da573b64ca0fd9b4e9a2b88d1251020a3022b0f27"),
|
2024-06-18 22:40:13 +02:00
|
|
|
"27.1" -> (if (System.getProperty("os.arch") == "aarch64")
|
|
|
|
"ad4a3fd484077224a82dd56d194efb6e614467f413ab1dfb8776da4d08a4c227"
|
2024-04-27 21:51:45 +02:00
|
|
|
else
|
2024-04-29 17:01:59 +02:00
|
|
|
"e1efd8c4605b2aabc876da93b6eee2bedd868ce7d1f02b0220c1001f903b3e2c")
|
2021-11-27 18:51:34 +01:00
|
|
|
)
|
|
|
|
else if (Properties.isWin)
|
|
|
|
Map(
|
2024-04-27 21:51:45 +02:00
|
|
|
"25.2" -> "c2ac84f55ee879caefd4414868d318a741c52a7286da190bf7233d86a2ffca69",
|
2024-04-28 20:16:39 +02:00
|
|
|
"26.1" -> "7bd0849e47472aeff99a0ea2c0cefd98f5be829e5a2d3b0168b5a54456cc638a",
|
2024-06-18 22:40:13 +02:00
|
|
|
"27.1" -> "9719871a2c9a45c741e33d670d2319dcd3f8f52a6059e9c435a9a2841188b932"
|
2021-11-27 18:51:34 +01:00
|
|
|
)
|
|
|
|
else sys.error(s"Unsupported OS: ${Properties.osName}")
|
|
|
|
|
2022-09-23 13:19:25 +02:00
|
|
|
val success = hash.equalsIgnoreCase(expectedHash(version))
|
|
|
|
if (success) {
|
2021-11-27 18:51:34 +01:00
|
|
|
logger.info(s"Download complete and verified, unzipping result")
|
|
|
|
|
|
|
|
val extractCommand =
|
|
|
|
s"tar -xzf $archiveLocation --directory $binaryDir"
|
|
|
|
logger.info(s"Extracting archive with command: $extractCommand")
|
|
|
|
extractCommand.!!
|
|
|
|
} else {
|
2024-06-18 22:40:13 +02:00
|
|
|
Files.deleteIfExists(expectedEndLocation)
|
2021-11-27 18:51:34 +01:00
|
|
|
logger.error(
|
|
|
|
s"Downloaded invalid version of bitcoind v$version, got $hash, expected ${expectedHash(version)}")
|
|
|
|
}
|
2019-08-27 17:48:48 +02:00
|
|
|
|
2019-12-02 16:01:21 +01:00
|
|
|
logger.info(s"Deleting archive")
|
|
|
|
Files.delete(archiveLocation)
|
2022-09-23 13:19:25 +02:00
|
|
|
|
|
|
|
if (!success)
|
|
|
|
throw new RuntimeException(s"Failed to download bitcoind v$version")
|
2019-12-02 16:01:21 +01:00
|
|
|
}
|
2019-08-27 17:48:48 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2019-12-02 16:01:21 +01:00
|
|
|
|
2020-02-19 16:48:45 +01:00
|
|
|
//timeout if we cannot download in 5 minutes
|
|
|
|
Await.result(Future.sequence(downloads), 5.minutes)
|
2019-08-27 17:48:48 +02:00
|
|
|
}
|