Skip to content

Commit

Permalink
Rework Standard Finalizer (into pieces) (#1473)
Browse files Browse the repository at this point in the history
* Made the standard non-interactive tx finalizer simple using composition of its (reusable) pieces

* Responded to code review and made minor improvements

* Fixed docs
  • Loading branch information
nkohen committed May 27, 2020
1 parent 1f4227c commit 13884f5
Show file tree
Hide file tree
Showing 9 changed files with 196 additions and 133 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,11 @@ class RawTxSignerTest extends BitcoinSAsyncTest {
)
val utxos = Vector(utxo)
val feeUnit = SatoshisPerVirtualByte(currencyUnit = Satoshis(1))
val utxF = NonInteractiveWithChangeFinalizer.txFrom(outputs = destinations,
utxos = utxos,
feeRate = feeUnit,
changeSPK =
EmptyScriptPubKey)
val utxF = StandardNonInteractiveFinalizer.txFrom(outputs = destinations,
utxos = utxos,
feeRate = feeUnit,
changeSPK =
EmptyScriptPubKey)
//trivially false
val f = (_: Seq[ScriptSignatureParams[InputInfo]], _: Transaction) => false

Expand Down Expand Up @@ -122,11 +122,11 @@ class RawTxSignerTest extends BitcoinSAsyncTest {
val utxos = Vector(utxo)

val feeUnit = SatoshisPerVirtualByte(Satoshis.one)
val utxF = NonInteractiveWithChangeFinalizer.txFrom(outputs = destinations,
utxos = utxos,
feeRate = feeUnit,
changeSPK =
EmptyScriptPubKey)
val utxF = StandardNonInteractiveFinalizer.txFrom(outputs = destinations,
utxos = utxos,
feeRate = feeUnit,
changeSPK =
EmptyScriptPubKey)

recoverToSucceededIf[IllegalArgumentException] {
utxF.flatMap(utx => RawTxSigner.sign(utx, utxos, feeUnit))
Expand Down Expand Up @@ -160,11 +160,11 @@ class RawTxSignerTest extends BitcoinSAsyncTest {
val utxos = Vector(utxo)

val feeUnit = SatoshisPerVirtualByte(Satoshis.one)
val utxF = NonInteractiveWithChangeFinalizer.txFrom(outputs = destinations,
utxos = utxos,
feeRate = feeUnit,
changeSPK =
EmptyScriptPubKey)
val utxF = StandardNonInteractiveFinalizer.txFrom(outputs = destinations,
utxos = utxos,
feeRate = feeUnit,
changeSPK =
EmptyScriptPubKey)

recoverToSucceededIf[IllegalArgumentException] {
utxF.flatMap(utx => RawTxSigner.sign(utx, utxos, feeUnit))
Expand Down Expand Up @@ -194,7 +194,7 @@ class RawTxSignerTest extends BitcoinSAsyncTest {
val feeUnit = SatoshisPerByte(Satoshis.one)

val utxF =
NonInteractiveWithChangeFinalizer.txFrom(
StandardNonInteractiveFinalizer.txFrom(
outputs = Vector(
TransactionOutput(Bitcoins.one - CurrencyUnits.oneMBTC,
EmptyScriptPubKey)),
Expand Down Expand Up @@ -231,7 +231,7 @@ class RawTxSignerTest extends BitcoinSAsyncTest {
val feeUnit = SatoshisPerByte(Satoshis.one)

val utxF =
NonInteractiveWithChangeFinalizer.txFrom(
StandardNonInteractiveFinalizer.txFrom(
outputs = Vector(
TransactionOutput(Bitcoins.one - CurrencyUnits.oneMBTC,
EmptyScriptPubKey)),
Expand Down Expand Up @@ -283,7 +283,7 @@ class RawTxSignerTest extends BitcoinSAsyncTest {
val feeRate = SatoshisPerByte(Satoshis.one)

val utxF =
NonInteractiveWithChangeFinalizer.txFrom(
StandardNonInteractiveFinalizer.txFrom(
Vector(
TransactionOutput(Bitcoins.one + Bitcoins.one - CurrencyUnits.oneMBTC,
EmptyScriptPubKey)),
Expand Down Expand Up @@ -361,10 +361,10 @@ class RawTxSignerTest extends BitcoinSAsyncTest {
case ((creditingTxsInfo, destinations), (changeSPK, _)) =>
val fee = SatoshisPerVirtualByte(Satoshis(1000))
val utxF =
NonInteractiveWithChangeFinalizer.txFrom(outputs = destinations,
utxos = creditingTxsInfo,
feeRate = fee,
changeSPK = changeSPK)
StandardNonInteractiveFinalizer.txFrom(outputs = destinations,
utxos = creditingTxsInfo,
feeRate = fee,
changeSPK = changeSPK)
val txF = utxF.flatMap(utx =>
RawTxSigner.sign(utx, creditingTxsInfo.toVector, fee))

Expand All @@ -380,10 +380,10 @@ class RawTxSignerTest extends BitcoinSAsyncTest {
case ((creditingTxsInfo, destinations), (changeSPK, _)) =>
val fee = SatoshisPerByte(Satoshis(1000))
val utxF =
NonInteractiveWithChangeFinalizer.txFrom(outputs = destinations,
utxos = creditingTxsInfo,
feeRate = fee,
changeSPK = changeSPK)
StandardNonInteractiveFinalizer.txFrom(outputs = destinations,
utxos = creditingTxsInfo,
feeRate = fee,
changeSPK = changeSPK)
val txF = utxF.flatMap(utx =>
RawTxSigner.sign(utx, creditingTxsInfo.toVector, fee))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ import org.bitcoins.testkit.Implicits._
import org.bitcoins.testkit.core.gen.ScriptGenerators
import org.bitcoins.testkit.util.BitcoinSAsyncTest

class NonInteractiveWithChangeFinalizerTest extends BitcoinSAsyncTest {
behavior of "NonInteractiveWithChangeFinalizerTest"
class StandardNonInteractiveFinalizerTest extends BitcoinSAsyncTest {
behavior of "StandardNonInteractiveFinalizer"

private val (spk, privKey) = ScriptGenerators.p2pkhScriptPubKey.sampleSome

Expand All @@ -54,26 +54,28 @@ class NonInteractiveWithChangeFinalizerTest extends BitcoinSAsyncTest {
tx.lockTime)

assert(
NonInteractiveWithChangeFinalizer
SanityCheckFinalizer
.sanityDestinationChecks(Vector(outPoint),
Vector(output),
changeSPK,
Vector(EmptyScriptPubKey),
Vector(changeSPK),
missingOutputTx)
.isFailure)
}

it should "detect extra outputs added" in {
val newOutput = TransactionOutput(Bitcoins.max, EmptyScriptPubKey)
val newOutput =
TransactionOutput(Bitcoins.one,
P2PKHScriptPubKey(ECPublicKey.freshPublicKey))
val extraOutputTx = BaseTransaction(tx.version,
tx.inputs,
Vector(output, newOutput),
tx.lockTime)

assert(
NonInteractiveWithChangeFinalizer
SanityCheckFinalizer
.sanityDestinationChecks(Vector(outPoint),
Vector(output),
changeSPK,
Vector(EmptyScriptPubKey),
Vector(changeSPK),
extraOutputTx)
.isFailure)
}
Expand All @@ -89,10 +91,10 @@ class NonInteractiveWithChangeFinalizerTest extends BitcoinSAsyncTest {
tx.lockTime)

assert(
NonInteractiveWithChangeFinalizer
SanityCheckFinalizer
.sanityDestinationChecks(Vector(outPoint),
Vector(output),
changeSPK,
Vector(EmptyScriptPubKey),
Vector(changeSPK),
extraOutPointTx)
.isFailure)
}
Expand Down Expand Up @@ -124,10 +126,10 @@ class NonInteractiveWithChangeFinalizerTest extends BitcoinSAsyncTest {
val feeUnit = SatoshisPerVirtualByte(Satoshis.one)

recoverToSucceededIf[IllegalArgumentException] {
NonInteractiveWithChangeFinalizer.txFrom(outputs = destinations,
utxos = utxos,
feeRate = feeUnit,
changeSPK = EmptyScriptPubKey)
StandardNonInteractiveFinalizer.txFrom(outputs = destinations,
utxos = utxos,
feeRate = feeUnit,
changeSPK = EmptyScriptPubKey)
}
}

Expand Down Expand Up @@ -157,10 +159,10 @@ class NonInteractiveWithChangeFinalizerTest extends BitcoinSAsyncTest {
val feeUnit = SatoshisPerVirtualByte(Satoshis(-1))

recoverToSucceededIf[IllegalArgumentException] {
NonInteractiveWithChangeFinalizer.txFrom(outputs = destinations,
utxos = utxos,
feeRate = feeUnit,
changeSPK = EmptyScriptPubKey)
StandardNonInteractiveFinalizer.txFrom(outputs = destinations,
utxos = utxos,
feeRate = feeUnit,
changeSPK = EmptyScriptPubKey)
}
}

Expand All @@ -184,7 +186,7 @@ class NonInteractiveWithChangeFinalizerTest extends BitcoinSAsyncTest {
)

recoverToSucceededIf[UnsupportedOperationException] {
NonInteractiveWithChangeFinalizer.txFrom(
StandardNonInteractiveFinalizer.txFrom(
Vector(TransactionOutput(Bitcoins.one, EmptyScriptPubKey)),
Vector(spendingInfo),
SatoshisPerVirtualByte(Satoshis.one),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ import org.bitcoins.core.psbt.PSBT
import org.bitcoins.core.script.PreExecutionScriptProgram
import org.bitcoins.core.script.interpreter.ScriptInterpreter
import org.bitcoins.core.wallet.builder.{
NonInteractiveWithChangeFinalizer,
RawTxSigner
RawTxSigner,
StandardNonInteractiveFinalizer
}
import org.bitcoins.core.wallet.fee.SatoshisPerVirtualByte
import org.bitcoins.core.wallet.utxo.{
Expand Down Expand Up @@ -128,7 +128,7 @@ class SignerTest extends BitcoinSAsyncTest {
val fee = SatoshisPerVirtualByte(Satoshis(1000))

for {
unsignedTx <- NonInteractiveWithChangeFinalizer.txFrom(
unsignedTx <- StandardNonInteractiveFinalizer.txFrom(
destinations,
creditingTxsInfos,
fee,
Expand Down Expand Up @@ -264,7 +264,7 @@ class SignerTest extends BitcoinSAsyncTest {
val fee = SatoshisPerVirtualByte(Satoshis(100))

for {
unsignedTx <- NonInteractiveWithChangeFinalizer.txFrom(
unsignedTx <- StandardNonInteractiveFinalizer.txFrom(
destinations,
creditingTxsInfos,
fee,
Expand Down
Loading

0 comments on commit 13884f5

Please sign in to comment.