mirror of
https://github.com/bitcoin-s/bitcoin-s.git
synced 2025-01-18 21:34:39 +01:00
Get Scala 2.13.4 compiling (#2294)
* Get Scala 2.13.4 compiling * Fix broken test case, in general use if() else() statements for Vectors rather than pattern match as I am unsure of how to correctly pattern match on Vector0,Vector1,...,Vector6() * Bump CI to 2.13.4 * OVerride CurrencyUnit.equals() so that it works on Scala 2.13.4, not sure why there was a regression here between Scala versions 2.13.4/2.13.3 * Add explicit unit tests making sure bitcoin/satoshi currency units are equivalent
This commit is contained in:
parent
cf5f206856
commit
f586e25699
@ -284,17 +284,18 @@ private[blockchain] trait BaseBlockChainCompObject
|
||||
val orderedHeaders =
|
||||
connectWalkBackwards(current = childHeader, ancestors = ancestors)
|
||||
|
||||
val initBlockchainOpt = orderedHeaders match {
|
||||
case Vector() | _ +: Vector() =>
|
||||
val initBlockchainOpt = {
|
||||
if (orderedHeaders.isEmpty || orderedHeaders.length == 1) {
|
||||
//for the case of _ +: Vector() this means only our
|
||||
//child header is in the chain, which means we
|
||||
//weren't able to form a blockchain
|
||||
None
|
||||
case h +: _ =>
|
||||
} else {
|
||||
//find our first header as we need it's Db representation
|
||||
//rather than just the raw header
|
||||
val dbOpt = ancestors.find(_.hashBE == h.hashBE)
|
||||
val dbOpt = ancestors.find(_.hashBE == orderedHeaders.head.hashBE)
|
||||
Some(Blockchain.fromHeaders(Vector(dbOpt.get)))
|
||||
}
|
||||
}
|
||||
|
||||
//now let's connect headers
|
||||
|
@ -63,23 +63,25 @@ class ChainHandler(
|
||||
val maxWork = groupedChains.keys.max
|
||||
val chainsByWork = groupedChains(maxWork)
|
||||
|
||||
val bestHeader: BlockHeaderDb = chainsByWork match {
|
||||
case Vector() =>
|
||||
val bestHeader: BlockHeaderDb = {
|
||||
if (chainsByWork.isEmpty) {
|
||||
// This should never happen
|
||||
val errMsg = s"Did not find blockchain with work $maxWork"
|
||||
logger.error(errMsg)
|
||||
throw new RuntimeException(errMsg)
|
||||
case chain +: Vector() =>
|
||||
chain.tip
|
||||
case chain +: rest =>
|
||||
} else if (chainsByWork.length == 1) {
|
||||
chainsByWork.head.tip
|
||||
} else {
|
||||
val tips = chainsByWork
|
||||
.map(_.tip.hashBE.hex)
|
||||
.mkString(", ")
|
||||
logger.warn(
|
||||
s"We have multiple competing blockchains with same work, selecting by time: ${(chain +: rest)
|
||||
.map(_.tip.hashBE.hex)
|
||||
.mkString(", ")}")
|
||||
s"We have multiple competing blockchains with same work, selecting by time: $tips")
|
||||
//since we have same chainwork, just take the oldest tip
|
||||
//as that's "more likely" to have been propagated first
|
||||
//and had more miners building on top of it
|
||||
chainsByWork.sortBy(_.tip.time).head.tip
|
||||
}
|
||||
}
|
||||
bestHeader
|
||||
}
|
||||
|
@ -11,6 +11,13 @@ class CurrencyUnitTest extends BitcoinSUnitTest {
|
||||
|
||||
behavior of "Satoshis"
|
||||
|
||||
it must "have 1BTC == 100,000,000 satoshis" in {
|
||||
assert(Bitcoins.one == Satoshis(100000000))
|
||||
assert(Satoshis(100000000) == Bitcoins.one)
|
||||
assert(Satoshis.zero == Bitcoins.zero)
|
||||
assert(Bitcoins.zero == Satoshis.zero)
|
||||
}
|
||||
|
||||
it must "have symmetry serialization" in {
|
||||
forAll(CurrencyUnitGenerator.satoshis) { satoshis =>
|
||||
assert(Satoshis(satoshis.hex) == satoshis)
|
||||
|
@ -74,6 +74,19 @@ sealed abstract class CurrencyUnit
|
||||
def toBigDecimal: BigDecimal
|
||||
|
||||
protected def underlying: A
|
||||
|
||||
override def equals(obj: Any): Boolean = {
|
||||
//needed for cases like
|
||||
//1BTC == 100,000,000 satoshis should be true
|
||||
//weirdly enough, this worked in scala version < 2.13.4
|
||||
//but seems to be broken in 2.13.4 :/
|
||||
//try removing this and running code, you should see
|
||||
//failures in the 'walletTest' module
|
||||
obj match {
|
||||
case cu: CurrencyUnit => cu.satoshis == satoshis
|
||||
case _ => false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
sealed abstract class Satoshis extends CurrencyUnit {
|
||||
|
@ -157,11 +157,11 @@ object LnTaggedFields {
|
||||
val newRemaining = t.slice(payload.size, t.size)
|
||||
|
||||
loop(newRemaining, fields :+ tag)
|
||||
case IndexedSeq() =>
|
||||
fields
|
||||
case _ +: _ | _ +: _ +: _ =>
|
||||
throw new IllegalArgumentException(
|
||||
"Failed to parse LnTaggedFields, needs 15bits of meta data to be able to parse")
|
||||
case _: Vector[_] =>
|
||||
fields
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -119,7 +119,8 @@ sealed abstract class ScriptParser
|
||||
.decodeHex(
|
||||
BytesUtil.flipEndianness(
|
||||
h)) ++ bytesToPushOntoStack.bytes ++ accum)
|
||||
case Vector() => accum
|
||||
case _: Vector[_] =>
|
||||
accum
|
||||
}
|
||||
}
|
||||
if (tryParsingLong(str) && str.size > 1 && str.substring(0, 2) != "0x") {
|
||||
|
@ -3,7 +3,7 @@ import sbt.Keys.excludeLintKeys
|
||||
import scala.util.Properties
|
||||
|
||||
val scala2_12 = "2.12.12"
|
||||
val scala2_13 = "2.13.3"
|
||||
val scala2_13 = "2.13.4"
|
||||
|
||||
scalafmtOnCompile in ThisBuild := !Properties.envOrNone("CI").contains("true")
|
||||
|
||||
|
@ -99,7 +99,9 @@ object CommonSettings {
|
||||
"-Ywarn-unused",
|
||||
"-unchecked",
|
||||
"-deprecation",
|
||||
"-feature"
|
||||
"-feature",
|
||||
"-Ypatmat-exhaust-depth",
|
||||
"off"
|
||||
) ++ commonCompilerOpts ++ {
|
||||
if (scalaVersion.startsWith("2.13")) scala2_13CompilerOpts
|
||||
else nonScala2_13CompilerOpts
|
||||
|
Loading…
Reference in New Issue
Block a user