bitcoin-s/bitcoind-rpc/bitcoind-rpc.sbt
Chris Stewart a18bd70369
Upgrade to bitcoind 24.2 (#5284)
* Upgrade to bitcoind 24.2

* update m1 24.2 bitcoind hash

* update windows, mac x86 bitcoind v24.1 hashes
2023-11-06 14:03:10 -06:00

150 lines
5.6 KiB
Scala

import java.nio.file.Files
import java.security.MessageDigest
import scala.collection.JavaConverters._
import scala.concurrent.duration.DurationInt
import scala.concurrent.{Await, Future, Promise}
import scala.util.Properties
name := "bitcoin-s-bitcoind-rpc"
libraryDependencies ++= Deps.bitcoindRpc
CommonSettings.prodSettings
TaskKeys.downloadBitcoind := {
val logger = streams.value.log
import scala.sys.process._
val binaryDir = CommonSettings.binariesPath.resolve("bitcoind")
if (Files.notExists(binaryDir)) {
logger.info(s"Creating directory for bitcoind binaries: $binaryDir")
Files.createDirectories(binaryDir)
}
val versions =
List("24.2", "23.2", "22.0", "0.21.1")
logger.debug(
s"(Maybe) downloading Bitcoin Core binaries for versions: ${versions.mkString(",")}")
val arm64MacVersions = List("24.2", "23.2")
def getPlatformAndSuffix(version: String): (String, String) = {
if (Properties.isLinux) ("x86_64-linux-gnu", "tar.gz")
else if (Properties.isMac) {
if (arm64MacVersions.contains(version)) {
if (System.getProperty("os.arch") == "aarch64")
("arm64-apple-darwin", "tar.gz")
else ("x86_64-apple-darwin", "tar.gz")
} else ("osx64", "tar.gz")
} else if (Properties.isWin) ("win64", "zip")
else sys.error(s"Unsupported OS: ${Properties.osName}")
}
implicit val ec = scala.concurrent.ExecutionContext.global
val downloads = versions.map { version =>
val (platform, suffix) = getPlatformAndSuffix(version)
val archiveLocation = binaryDir resolve s"$version.$suffix"
val location =
if (version.init.endsWith("rc")) { // if it is a release candidate
val (base, rc) = version.splitAt(version.length - 3)
s"https://bitcoincore.org/bin/bitcoin-core-$base/test.$rc/bitcoin-$version-$platform.$suffix"
} else
s"https://bitcoincore.org/bin/bitcoin-core-$version/bitcoin-$version-$platform.$suffix"
val expectedEndLocation = binaryDir resolve s"bitcoin-$version"
if (
Files
.list(binaryDir)
.iterator
.asScala
.map(_.toString)
.exists(expectedEndLocation.toString.startsWith(_))
) {
logger.debug(
s"Directory $expectedEndLocation already exists, skipping download of version $version")
Future.unit
} else {
// 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 { () =>
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.!!
val bytes = Files.readAllBytes(archiveLocation)
val hash = MessageDigest
.getInstance("SHA-256")
.digest(bytes)
.map("%02x" format _)
.mkString
val expectedHash =
if (Properties.isLinux)
Map(
"24.2" -> "7540d6e34c311e355af2fd76e5eee853b76c291978d6b5ebb555c7877e9de38d",
"23.2" -> "853b9b0a50800a5b355df7a39dbdd6d7a2339f765e2d31a36d14bd705e7dbce1",
"22.0" -> "59ebd25dd82a51638b7a6bb914586201e67db67b919b2a1ff08925a7936d1b16",
"0.21.1" -> "366eb44a7a0aa5bd342deea215ec19a184a11f2ca22220304ebb20b9c8917e2b",
)
else if (Properties.isMac)
Map(
"24.2" -> (if (System.getProperty("os.arch") == "aarch64")
"ae6f5f0cb4079005c32695711ef78b26a26c4c547ceb593b3626059626530a5d"
else
"b1b21455c339b2daf0998bfad17d0741d967c3c81db040bb5f73234168526d29"),
"23.2" -> (if (System.getProperty("os.arch") == "aarch64")
"33f8680f820327d5f9e64c02e476d03a5b2e5bd403e1b1cb0a82bd7ec2a8dc5e"
else
"f56f2e21718c3ef13b962775f1e9985eed962ae6237684deebf6b59f799a912f"),
"22.0" -> "2744d199c3343b2d94faffdfb2c94d75a630ba27301a70e47b0ad30a7e0155e9",
"0.21.1" -> "1ea5cedb64318e9868a66d3ab65de14516f9ada53143e460d50af428b5aec3c7"
)
else if (Properties.isWin)
Map(
"24.2" -> "544436bc9d5ce017e679bbccfe8a4928fbc840b414ee0240db8c3523ba54340a",
"23.2" -> "29dd4c94de8b292fd19fd9475f2f31f891d04f16238bd7defa48eef3f2f8546a",
"22.0" -> "9485e4b52ed6cebfe474ab4d7d0c1be6d0bb879ba7246a8239326b2230a77eb1",
"0.21.1" -> "94c80f90184cdc7e7e75988a55b38384de262336abd80b1b30121c6e965dc74e"
)
else sys.error(s"Unsupported OS: ${Properties.osName}")
val success = hash.equalsIgnoreCase(expectedHash(version))
if (success) {
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 {
logger.error(
s"Downloaded invalid version of bitcoind v$version, got $hash, expected ${expectedHash(version)}")
}
logger.info(s"Deleting archive")
Files.delete(archiveLocation)
if (!success)
throw new RuntimeException(s"Failed to download bitcoind v$version")
}
}
}
//timeout if we cannot download in 5 minutes
Await.result(Future.sequence(downloads), 5.minutes)
}