mirror of
https://github.com/bitcoin-s/bitcoin-s.git
synced 2025-01-19 05:43:51 +01:00
Rework TransactionWitness, add more tests for resizing (#267)
Turn down logging again
This commit is contained in:
parent
cc2067c88c
commit
a8e513fd0c
@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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 {
|
||||
|
Loading…
Reference in New Issue
Block a user