From a8e513fd0ca4c482e8adf52ed53b50151844a40f Mon Sep 17 00:00:00 2001 From: Chris Stewart Date: Mon, 10 Dec 2018 10:53:45 -0500 Subject: [PATCH] Rework TransactionWitness, add more tests for resizing (#267) Turn down logging again --- .../transaction/TransactionWitnessSpec.scala | 29 +++++++++++++++++-- .../transaction/TransactionWitness.scala | 16 +++++----- 2 files changed, 33 insertions(+), 12 deletions(-) diff --git a/core-test/src/test/scala/org/bitcoins/core/protocol/transaction/TransactionWitnessSpec.scala b/core-test/src/test/scala/org/bitcoins/core/protocol/transaction/TransactionWitnessSpec.scala index df87f602a2..bf9f94abcd 100644 --- a/core-test/src/test/scala/org/bitcoins/core/protocol/transaction/TransactionWitnessSpec.scala +++ b/core-test/src/test/scala/org/bitcoins/core/protocol/transaction/TransactionWitnessSpec.scala @@ -1,16 +1,39 @@ package org.bitcoins.core.protocol.transaction +import org.bitcoins.core.crypto.{ECPrivateKey, EmptyDigitalSignature} import org.bitcoins.core.gen.WitnessGenerators -import org.scalacheck.{Prop, Properties} +import org.bitcoins.core.protocol.script._ +import org.scalacheck.Prop +import org.scalatest.{FlatSpec, MustMatchers} /** * Created by chris on 11/28/16. */ -class TransactionWitnessSpec extends Properties("TransactionWitnessSpec") { +class TransactionWitnessSpec extends FlatSpec with MustMatchers { - property("serialization symmetry") = { + behavior of "TransactionWitness" + + it must "have serialization symmetry" in { Prop.forAll(WitnessGenerators.transactionWitness) { witness => TransactionWitness(witness.hex, witness.witnesses.size) == witness } } + + it must "be able to resize a witness to the given index" in { + val empty = EmptyWitness + val pubKey = ECPrivateKey.freshPrivateKey.publicKey + val p2pkh = P2PKHScriptSignature(EmptyDigitalSignature,pubKey) + val scriptWit = P2WPKHWitnessV0.fromP2PKHScriptSig(p2pkh) + val updated = empty.updated(2,scriptWit) + + updated.witnesses.length must be (3) + } + + + it must "fail to update a negative index witness" in { + intercept[IndexOutOfBoundsException] { + EmptyWitness.updated(-1,EmptyScriptWitness) + } + } + } diff --git a/core/src/main/scala/org/bitcoins/core/protocol/transaction/TransactionWitness.scala b/core/src/main/scala/org/bitcoins/core/protocol/transaction/TransactionWitness.scala index 27613b188b..81f73527e0 100644 --- a/core/src/main/scala/org/bitcoins/core/protocol/transaction/TransactionWitness.scala +++ b/core/src/main/scala/org/bitcoins/core/protocol/transaction/TransactionWitness.scala @@ -12,24 +12,23 @@ import scodec.bits.ByteVector * [[https://github.com/bitcoin/bitcoin/blob/b4e4ba475a5679e09f279aaf2a83dcf93c632bdb/src/primitives/transaction.h#L232-L268]] */ sealed abstract class TransactionWitness extends NetworkElement { - def witnesses: Vector[ScriptWitness] + val witnesses: Vector[ScriptWitness] - override def bytes = RawTransactionWitnessParser.write(this) + override def bytes: ByteVector = { + RawTransactionWitnessParser.write(this) + } /** * Update the [[witnesses]] index to the given witness * Pads the witnesses vector if needed to accomodate the new witness */ def updated(index: Int, witness: ScriptWitness): TransactionWitness = { - val scriptWits = { + val scriptWits: Vector[ScriptWitness] = { if (index < 0) { throw new IndexOutOfBoundsException(index.toString) } else if (index >= witnesses.size) { - val padded = padScriptWitness(index) - padded.updated(index, witness) - } else { witnesses.updated(index, witness) } @@ -58,9 +57,8 @@ sealed abstract class TransactionWitness extends NetworkElement { /** Used to represent a transaction witness pre segwit, see BIP141 for details */ case object EmptyWitness extends TransactionWitness { - override def bytes = ByteVector.low(1) - override def witnesses: Vector[ScriptWitness] = Vector.empty - + override val bytes: ByteVector = ByteVector.low(1) + override val witnesses: Vector[ScriptWitness] = Vector.empty } object TransactionWitness {