Skip to content

Commit

Permalink
Scala 3 compatible
Browse files Browse the repository at this point in the history
  • Loading branch information
h0ngcha0 committed Jul 3, 2021
1 parent e1fb40a commit 865a8a4
Show file tree
Hide file tree
Showing 15 changed files with 63 additions and 41 deletions.
39 changes: 33 additions & 6 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,48 @@ version := "0.1.0"

ThisBuild / scalaVersion := "2.13.6"

scalacOptions := Seq(
"-unchecked",
"-feature",
ThisBuild / scalacOptions := Seq(
"-deprecation",
"-Ywarn-dead-code",
"-Ywarn-extra-implicit",
"-encoding",
"utf-8",
"-explaintypes",
"-feature",
"-unchecked",
"-Xsource:3",
"-Xlint:adapted-args",
"-Xlint:constant",
"-Xlint:delayedinit-select",
"-Xlint:doc-detached",
"-Xlint:inaccessible",
"-Xlint:infer-any",
"-Xlint:missing-interpolator",
"-Xlint:nullary-unit",
"-Xlint:option-implicit",
"-Xlint:package-object-classes",
"-Xlint:poly-implicit-overload",
"-Xlint:private-shadow",
"-Xlint:stars-align",
"-Xlint:type-parameter-shadow",
"-Xlint:nonlocal-return",
"-Xfatal-warnings",
"-Ywarn-dead-code",
"-Ywarn-extra-implicit",
"-Ywarn-numeric-widen",
"-Ywarn-unused:implicits",
"-Ywarn-unused:imports",
"-Ywarn-unused:locals",
"-Ywarn-unused:params",
"-Ywarn-unused:patvars",
"-Ywarn-unused:privates",
"-Ywarn-unused:imports",
"-Ywarn-value-discard",
"-Ymacro-annotations"
)

Compile / console / scalacOptions --= Seq(
"-Ywarn-unused:imports",
"-Xfatal-warnings"
)

val akkaHttpVersion = "10.2.4"
val akkaVersion = "2.6.15"

Expand Down
2 changes: 1 addition & 1 deletion src/main/scala/it/softfork/bitcoin4s/MnemonicCodes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,6 @@ class MnemonicCodes(config: MnemonicCodesConfig) {
}

private def byteToPaddedBinaryString(byte: Byte): String = {
f"${byte.toBinaryString}8s".replace(' ', '0')
f"${byte.toInt.toBinaryString}8s".replace(' ', '0')
}
}
4 changes: 1 addition & 3 deletions src/main/scala/it/softfork/bitcoin4s/Routes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ import it.softfork.bitcoin4s.external.blockcypher.{Service => BlockCypherService
import it.softfork.bitcoin4s.transaction.TxId
import play.api.libs.json.{Json, Writes}

import scala.concurrent.ExecutionContext

class Routes(blockcypherService: BlockCypherService)(implicit ec: ExecutionContext) extends PlayJsonSupport {
class Routes(blockcypherService: BlockCypherService) extends PlayJsonSupport {

val clientRoute = {
pathSingleSlash {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ object Signature {
bytesIn match {
case head +: tail =>
if ((head & 0x80) == 0) {
Some((head, tail))
Some((head.toInt, tail))
} else {
val lengthBytesNumber: Int = head - 0x80

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ package it.softfork.bitcoin4s.external
import akka.actor.ActorSystem
import akka.http.scaladsl.Http
import akka.http.scaladsl.model.{HttpRequest, HttpResponse}
import akka.stream.Materializer

import scala.concurrent.Future

class HttpSender(implicit system: ActorSystem, materializer: Materializer) {
class HttpSender(implicit system: ActorSystem) {

def apply(request: HttpRequest): Future[HttpResponse] = {
Http().singleRequest(request)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import it.softfork.bitcoin4s.transaction.TxId

import scala.collection.immutable.HashMap
import scala.concurrent.duration._
import scala.concurrent.{ExecutionContext, Future}
import scala.concurrent.Future

object TransactionCacheActor {

Expand All @@ -29,14 +29,12 @@ object TransactionCacheActor {
implicit val timeout: Timeout = Timeout(5.seconds)

def getTransaction(txId: TxId)(implicit
ec: ExecutionContext,
transactionCacheActor: ActorRef
): Future[Option[Transaction]] = {
(transactionCacheActor ? Get(txId)).mapTo[Option[Transaction]]
}

def setTransaction(txId: TxId, tx: Transaction)(implicit
ec: ExecutionContext,
transactionCacheActor: ActorRef
): Unit = {
transactionCacheActor ! Set(txId, tx)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package it.softfork.bitcoin4s.external.blockcypher

import akka.actor.Cancellable
import akka.stream.Materializer
import akka.stream.scaladsl.Source
import com.typesafe.scalalogging.StrictLogging
import it.softfork.bitcoin4s.script.SigVersion.{SIGVERSION_BASE, SIGVERSION_WITNESS_V0}
Expand All @@ -21,8 +20,7 @@ import it.softfork.bitcoin4s.external.ApiInterface
import scala.collection.immutable.ArraySeq

class Service(api: ApiInterface)(implicit
ec: ExecutionContext,
materializer: Materializer
ec: ExecutionContext
) extends StrictLogging {

def getTransaction(txId: TxId): Future[Option[Transaction]] = {
Expand Down
4 changes: 2 additions & 2 deletions src/main/scala/it/softfork/bitcoin4s/script/ConstantOp.scala
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ object ConstantOp {
case class OP_PUSHDATA(val value: Long) extends ConstantOp {
override def toString: String = s"OP_PUSHDATA($value)"
}
val ops_pushdata = for (i <- 1 to 75) yield OP_PUSHDATA(i)
val ops_pushdata = for (i <- 1 to 75) yield OP_PUSHDATA(i.toLong)

val all = Seq(
OP_0,
Expand Down Expand Up @@ -146,7 +146,7 @@ object ConstantOp {
case 1 if constant.bytes(0) >= 1 && constant.bytes(0) <= 16 =>
all.find(_.value == (OP_1.value + constant.bytes(0) - 1)).exists(_ == opCode)
case s if s <= 75 =>
opCode == OP_PUSHDATA(s)
opCode == OP_PUSHDATA(s.toLong)
case s if s <= 255 =>
opCode == OP_PUSHDATA1
case s if s <= 65535 =>
Expand Down
2 changes: 2 additions & 0 deletions src/main/scala/it/softfork/bitcoin4s/script/Interpreter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ object InterpreterState {
amount: Long,
sigVersion: SigVersion
): InterpreterState = {
println(s"sigVersion: $sigVersion")

InterpreterState(
scriptPubKey = scriptPubKey,
scriptSig = scriptSig,
Expand Down
2 changes: 1 addition & 1 deletion src/main/scala/it/softfork/bitcoin4s/script/Parser.scala
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ object Parser {
}

private def bytesAndLength(dataBytes: Seq[Byte]): Seq[Byte] = {
val dataBytesLength = dataBytes.length
val dataBytesLength = dataBytes.length.toLong

val lengthBytes: Seq[Byte] = if (dataBytesLength <= 75) {
OP_PUSHDATA(dataBytesLength).bytes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ object RichTransaction extends StrictLogging {
implicit class RichTx(tx: Tx) {

def serialize(): ByteVector = {
Tx.codec(transactionVersion).encode(tx).toEither match {
Tx.codec().encode(tx).toEither match {
case Left(error) =>
throw new RuntimeException(error.messageWithContext)
case Right(v) =>
Expand Down Expand Up @@ -51,14 +51,14 @@ object RichTransaction extends StrictLogging {
)
.getOrElse(updatedTx1)

val serialisedTx = Tx.codec(transactionVersion).compact.encode(updatedTx).toEither match {
val serialisedTx = Tx.codec().compact.encode(updatedTx).toEither match {
case Left(error) =>
throw new RuntimeException(error.messageWithContext)
case Right(v) =>
v.toByteArray
}

val transactionPreImage: Array[Byte] = serialisedTx ++ uint32ToBytes(sigHashType.value)
val transactionPreImage: Array[Byte] = serialisedTx ++ uint32ToBytes(sigHashType.value.toLong)
Hash.Hash256(transactionPreImage)
}

Expand Down Expand Up @@ -110,7 +110,7 @@ object RichTransaction extends StrictLogging {
val versionBytes = uint32ToBytes(tx.version)

val locktimeBytes = uint32ToBytes(tx.lock_time)
val sigHashTypeBytes = uint32ToBytes(sigHashType.value)
val sigHashTypeBytes = uint32ToBytes(sigHashType.value.toLong)

val preImage: Array[Byte] =
versionBytes ++
Expand Down
2 changes: 1 addition & 1 deletion src/main/scala/it/softfork/bitcoin4s/script/SpliceOp.scala
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ object SpliceOp {
case head :: _ =>
val newState = state.copy(
currentScript = state.currentScript,
stack = ScriptNum(head.bytes.length) +: state.stack,
stack = ScriptNum(head.bytes.length.toLong) +: state.stack,
opCount = state.opCount + 1
)

Expand Down
2 changes: 1 addition & 1 deletion src/main/scala/it/softfork/bitcoin4s/script/StackOp.scala
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ object StackOp {

case OP_DEPTH =>
getState.flatMap { state =>
val newStack = ScriptNum(state.stack.length) +: state.stack
val newStack = ScriptNum(state.stack.length.toLong) +: state.stack
setStateAndContinue(state.copy(stack = newStack, opCount = state.opCount + 1))
}

Expand Down
22 changes: 11 additions & 11 deletions src/main/scala/it/softfork/bitcoin4s/transaction/Tx.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,27 +24,27 @@ case class Tx(

object Tx {

def codec(version: Int): Codec[Tx] = {
def codec(): Codec[Tx] = {
def encode(tx: Tx): Attempt[BitVector] = {
val txCodec: Codec[Tx] =
if (tx.flag) codecWithWitness(version, tx.tx_in.length)
else codecWithoutWitness(version)
if (tx.flag) codecWithWitness(tx.tx_in.length)
else codecWithoutWitness()
txCodec.encode(tx)
}

def decode(bits: BitVector): Attempt[DecodeResult[Tx]] = {
for {
witnessFlag <- decodeWitnessFlag(bits)
isWitness = witnessFlag.value.isWitness
tx <- decodeTx(bits, isWitness, version)
tx <- decodeTx(bits, isWitness)
} yield tx
}

Codec[Tx](encode _, decode _)
}.as[Tx]

def fromHex(hex: String): Tx = {
Tx.codec(1).decodeValue(BitVector(hexToBytes(hex))) match {
Tx.codec().decodeValue(BitVector(hexToBytes(hex))) match {
case Attempt.Successful(tx) =>
tx
case Attempt.Failure(err) =>
Expand All @@ -53,7 +53,7 @@ object Tx {
}

def toHex(tx: Tx): String = {
Tx.codec(1).encode(tx) match {
Tx.codec().encode(tx) match {
case Attempt.Successful(tx) =>
tx.toHex
case Attempt.Failure(err) =>
Expand All @@ -75,7 +75,7 @@ object Tx {

// ==== private ====

private def codecWithoutWitness(version: Int) = {
private def codecWithoutWitness() = {
("version" | uint32L) ::
("flag" | provide(false)) ::
("tx_in" | VarList.varList(Codec[TxIn])) ::
Expand All @@ -84,7 +84,7 @@ object Tx {
("lock_time" | uint32L)
}.as[Tx]

private def codecWithWitness(version: Int, txInsCount: Int): Codec[Tx] = {
private def codecWithWitness(txInsCount: Int): Codec[Tx] = {
("version" | uint32L) ::
("flag" | mappedEnum(WitnessFlag.codec, false -> WitnessFlag(0, 0), true -> WitnessFlag(0, 1))) ::
("tx_in" | VarList.varList(Codec[TxIn])) ::
Expand All @@ -100,16 +100,16 @@ object Tx {
} yield witnessFlag
}

private def decodeTx(bits: BitVector, isWitness: Boolean, version: Int): Attempt[DecodeResult[Tx]] = {
private def decodeTx(bits: BitVector, isWitness: Boolean): Attempt[DecodeResult[Tx]] = {
if (isWitness) {
for {
vsn <- uint32L.decode(bits)
witnessFlag <- Codec[WitnessFlag].decode(vsn.remainder)
txIns <- VarList.varList(Codec[TxIn]).decode(witnessFlag.remainder)
tx <- codecWithWitness(version, txIns.value.length).decode(bits)
tx <- codecWithWitness(txIns.value.length).decode(bits)
} yield tx
} else {
codecWithoutWitness(version).decode(bits)
codecWithoutWitness().decode(bits)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ trait BitcoinCoreScriptTestRunner extends StrictLogging { self: Spec =>
def creditingTransaction(scriptPubKey: Seq[Byte], maybeAmount: Option[Long]) = {
// val emptyTxId = Array.fill[Byte](32)(0)
val emptyOutpoint = OutPoint(Hash.NULL, -1)
val maxSequence = 0xffffffff
val maxSequence: Long = 0xffffffff
val txIn = TxIn(
previous_output = emptyOutpoint,
sig_script = Script(Seq(OP_0, OP_0).flatMap(_.bytes)),
Expand Down Expand Up @@ -316,7 +316,7 @@ trait BitcoinCoreScriptTestRunner extends StrictLogging { self: Spec =>
}

def spendingTransaction(creditingTransaction: Tx, scriptSig: Seq[Byte], maybeWitnessScript: Option[Seq[ScriptConstant]]) = {
val maxSequence = 0xffffffff
val maxSequence: Long = 0xffffffff

import scodec.bits._

Expand Down

0 comments on commit 865a8a4

Please sign in to comment.