Rework BitcoindRpcTestUtil.getBinary() to match major and minor versions of bitcoind binary when possible (#5569)

This commit is contained in:
Chris Stewart 2024-05-06 13:00:07 -05:00 committed by GitHub
parent 6dacfb071a
commit cb3fbe523a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 43 additions and 23 deletions

View file

@ -378,15 +378,15 @@ object BitcoindVersion
val known: Vector[BitcoindVersion] = standard
case object V25 extends BitcoindVersion {
override def toString: String = "v25"
override def toString: String = "v25.2"
}
case object V26 extends BitcoindVersion {
override def toString: String = "v26"
override def toString: String = "v26.1"
}
case object V27 extends BitcoindVersion {
override def toString: String = "v27"
override def toString: String = "v27.0"
}
case object Unknown extends BitcoindVersion {
@ -417,4 +417,17 @@ object BitcoindVersion
newest
}
}
def findVersion(version: String): Option[BitcoindVersion] = {
// first try to match the version exactly
BitcoindVersion.known
.find(v => version == v.toString) match {
case Some(r) => Some(r)
case None =>
// try to match the major version if we can't match it exactly
BitcoindVersion.known.find { v =>
version.startsWith(v.toString)
}
}
}
}

View file

@ -51,22 +51,18 @@ sealed trait BitcoindInstanceLocal extends BitcoindInstance {
def getVersion: BitcoindVersion = {
val binaryPath = binary.getAbsolutePath
val versionT = Try {
val foundVersion =
Seq(binaryPath, "--version").!!.split(Properties.lineSeparator).head
.split(" ")
.last
BitcoindVersion.known
.find(v => foundVersion.startsWith(v.toString))
.getOrElse {
println(
s"Unsupported Bitcoin Core version: $foundVersion. The latest supported version is ${BitcoindVersion.newest}")
logger.warn(
s"Unsupported Bitcoin Core version: $foundVersion. The latest supported version is ${BitcoindVersion.newest}"
)
BitcoindVersion.newest
}
BitcoindVersion.findVersion(foundVersion).getOrElse {
// else just default to the newest version of bitcoind
logger.warn(
s"Unsupported Bitcoin Core version: $foundVersion. The latest supported version is ${BitcoindVersion.newest}"
)
BitcoindVersion.newest
}
}
versionT match {

View file

@ -179,24 +179,35 @@ trait BitcoindRpcTestUtil extends BitcoinSLogger {
// default to newest version
case Unknown => getBinary(BitcoindVersion.newest, binaryDirectory)
case known @ (V25 | V26 | V27) =>
val fileList = Files
val fileList: List[(Path, String)] = Files
.list(binaryDirectory)
.iterator()
.asScala
.toList
.filter(f => Files.isDirectory(f))
.map(p => (p, p.toString.split("-").last))
// drop leading 'v'
val version = known.toString.drop(1)
val filtered = fileList.filter(f => f.toString.contains(version))
val exactMatchOpt = fileList.find { case (_, versionStr) =>
// try matching the version exactly
versionStr == version
}
if (filtered.isEmpty)
throw new RuntimeException(
s"bitcoind ${known.toString} is not installed in $binaryDirectory. Run `sbt downloadBitcoind`"
)
val versionFolder: Path = exactMatchOpt match {
case Some((p, _)) =>
p
case None =>
val filtered = fileList.filter(f => f.toString.contains(version))
if (filtered.isEmpty)
throw new RuntimeException(
s"bitcoind ${known.toString} is not installed in $binaryDirectory. Run `sbt downloadBitcoind`"
)
// might be multiple versions downloaded for
// each major version, i.e. 0.16.2 and 0.16.3
val versionFolder = filtered.max
// might be multiple versions downloaded for
// each major version, i.e. 0.16.2 and 0.16.3
val versionFolder = filtered.max
versionFolder._1
}
versionFolder
.resolve("bin")