|
|
|
@ -102,7 +102,7 @@ class UTXO():
|
|
|
|
|
self.nValue = value
|
|
|
|
|
|
|
|
|
|
def sign_p2pk_witness_input(script, tx_to, in_idx, hashtype, value, key):
|
|
|
|
|
"""Add signature for a P2PK witness program."""
|
|
|
|
|
"""Add signature for a P2PK witness script."""
|
|
|
|
|
tx_hash = SegwitV0SignatureHash(script, tx_to, in_idx, hashtype, value)
|
|
|
|
|
signature = key.sign_ecdsa(tx_hash) + chr(hashtype).encode('latin-1')
|
|
|
|
|
tx_to.wit.vtxinwit[in_idx].scriptWitness.stack = [signature, script]
|
|
|
|
@ -281,7 +281,7 @@ class SegWitTest(BitcoinTestFramework):
|
|
|
|
|
self.test_submit_block()
|
|
|
|
|
self.test_extra_witness_data()
|
|
|
|
|
self.test_max_witness_push_length()
|
|
|
|
|
self.test_max_witness_program_length()
|
|
|
|
|
self.test_max_witness_script_length()
|
|
|
|
|
self.test_witness_input_length()
|
|
|
|
|
self.test_block_relay()
|
|
|
|
|
self.test_tx_relay_after_segwit_activation()
|
|
|
|
@ -488,8 +488,8 @@ class SegWitTest(BitcoinTestFramework):
|
|
|
|
|
self.disconnect_nodes(0, 2)
|
|
|
|
|
|
|
|
|
|
# Create two outputs, a p2wsh and p2sh-p2wsh
|
|
|
|
|
witness_program = CScript([OP_TRUE])
|
|
|
|
|
script_pubkey = script_to_p2wsh_script(witness_program)
|
|
|
|
|
witness_script = CScript([OP_TRUE])
|
|
|
|
|
script_pubkey = script_to_p2wsh_script(witness_script)
|
|
|
|
|
p2sh_script_pubkey = script_to_p2sh_script(script_pubkey)
|
|
|
|
|
|
|
|
|
|
value = self.utxo[0].nValue // 3
|
|
|
|
@ -624,9 +624,9 @@ class SegWitTest(BitcoinTestFramework):
|
|
|
|
|
V0 segwit outputs and inputs are always standard.
|
|
|
|
|
V0 segwit inputs may only be mined after activation, but not before."""
|
|
|
|
|
|
|
|
|
|
witness_program = CScript([OP_TRUE])
|
|
|
|
|
script_pubkey = script_to_p2wsh_script(witness_program)
|
|
|
|
|
p2sh_script_pubkey = script_to_p2sh_script(witness_program)
|
|
|
|
|
witness_script = CScript([OP_TRUE])
|
|
|
|
|
script_pubkey = script_to_p2wsh_script(witness_script)
|
|
|
|
|
p2sh_script_pubkey = script_to_p2sh_script(witness_script)
|
|
|
|
|
|
|
|
|
|
# First prepare a p2sh output (so that spending it will pass standardness)
|
|
|
|
|
p2sh_tx = CTransaction()
|
|
|
|
@ -642,7 +642,7 @@ class SegWitTest(BitcoinTestFramework):
|
|
|
|
|
# Now test standardness of v0 P2WSH outputs.
|
|
|
|
|
# Start by creating a transaction with two outputs.
|
|
|
|
|
tx = CTransaction()
|
|
|
|
|
tx.vin = [CTxIn(COutPoint(p2sh_tx.sha256, 0), CScript([witness_program]))]
|
|
|
|
|
tx.vin = [CTxIn(COutPoint(p2sh_tx.sha256, 0), CScript([witness_script]))]
|
|
|
|
|
tx.vout = [CTxOut(p2sh_tx.vout[0].nValue - 10000, script_pubkey)]
|
|
|
|
|
tx.vout.append(CTxOut(8000, script_pubkey)) # Might burn this later
|
|
|
|
|
tx.vin[0].nSequence = BIP125_SEQUENCE_NUMBER # Just to have the option to bump this tx from the mempool
|
|
|
|
@ -653,14 +653,14 @@ class SegWitTest(BitcoinTestFramework):
|
|
|
|
|
test_transaction_acceptance(self.nodes[1], self.std_node, tx, with_witness=True, accepted=True)
|
|
|
|
|
|
|
|
|
|
# Now create something that looks like a P2PKH output. This won't be spendable.
|
|
|
|
|
witness_hash = sha256(witness_program)
|
|
|
|
|
witness_hash = sha256(witness_script)
|
|
|
|
|
script_pubkey = CScript([OP_0, hash160(witness_hash)])
|
|
|
|
|
tx2 = CTransaction()
|
|
|
|
|
# tx was accepted, so we spend the second output.
|
|
|
|
|
tx2.vin = [CTxIn(COutPoint(tx.sha256, 1), b"")]
|
|
|
|
|
tx2.vout = [CTxOut(7000, script_pubkey)]
|
|
|
|
|
tx2.wit.vtxinwit.append(CTxInWitness())
|
|
|
|
|
tx2.wit.vtxinwit[0].scriptWitness.stack = [witness_program]
|
|
|
|
|
tx2.wit.vtxinwit[0].scriptWitness.stack = [witness_script]
|
|
|
|
|
tx2.rehash()
|
|
|
|
|
|
|
|
|
|
test_transaction_acceptance(self.nodes[1], self.std_node, tx2, with_witness=True, accepted=True)
|
|
|
|
@ -673,7 +673,7 @@ class SegWitTest(BitcoinTestFramework):
|
|
|
|
|
tx3.vin = [CTxIn(COutPoint(tx.sha256, 0), b"")]
|
|
|
|
|
tx3.vout = [CTxOut(tx.vout[0].nValue - 1000, CScript([OP_TRUE, OP_DROP] * 15 + [OP_TRUE]))]
|
|
|
|
|
tx3.wit.vtxinwit.append(CTxInWitness())
|
|
|
|
|
tx3.wit.vtxinwit[0].scriptWitness.stack = [witness_program]
|
|
|
|
|
tx3.wit.vtxinwit[0].scriptWitness.stack = [witness_script]
|
|
|
|
|
tx3.rehash()
|
|
|
|
|
if not self.segwit_active:
|
|
|
|
|
# Just check mempool acceptance, but don't add the transaction to the mempool, since witness is disallowed
|
|
|
|
@ -731,8 +731,8 @@ class SegWitTest(BitcoinTestFramework):
|
|
|
|
|
"""Test P2SH wrapped witness programs."""
|
|
|
|
|
|
|
|
|
|
# Prepare the p2sh-wrapped witness output
|
|
|
|
|
witness_program = CScript([OP_DROP, OP_TRUE])
|
|
|
|
|
p2wsh_pubkey = script_to_p2wsh_script(witness_program)
|
|
|
|
|
witness_script = CScript([OP_DROP, OP_TRUE])
|
|
|
|
|
p2wsh_pubkey = script_to_p2wsh_script(witness_script)
|
|
|
|
|
script_pubkey = script_to_p2sh_script(p2wsh_pubkey)
|
|
|
|
|
script_sig = CScript([p2wsh_pubkey]) # a push of the redeem script
|
|
|
|
|
|
|
|
|
@ -776,7 +776,7 @@ class SegWitTest(BitcoinTestFramework):
|
|
|
|
|
spend_tx.vin[0].scriptSig = script_sig
|
|
|
|
|
spend_tx.rehash()
|
|
|
|
|
spend_tx.wit.vtxinwit.append(CTxInWitness())
|
|
|
|
|
spend_tx.wit.vtxinwit[0].scriptWitness.stack = [b'a', witness_program]
|
|
|
|
|
spend_tx.wit.vtxinwit[0].scriptWitness.stack = [b'a', witness_script]
|
|
|
|
|
|
|
|
|
|
# Verify mempool acceptance
|
|
|
|
|
test_transaction_acceptance(self.nodes[0], self.test_node, spend_tx, with_witness=True, accepted=True)
|
|
|
|
@ -825,18 +825,18 @@ class SegWitTest(BitcoinTestFramework):
|
|
|
|
|
tx = CTransaction()
|
|
|
|
|
tx.vin.append(CTxIn(COutPoint(self.utxo[0].sha256, self.utxo[0].n), b""))
|
|
|
|
|
|
|
|
|
|
# Let's construct a witness program
|
|
|
|
|
witness_program = CScript([OP_TRUE])
|
|
|
|
|
script_pubkey = script_to_p2wsh_script(witness_program)
|
|
|
|
|
# Let's construct a witness script
|
|
|
|
|
witness_script = CScript([OP_TRUE])
|
|
|
|
|
script_pubkey = script_to_p2wsh_script(witness_script)
|
|
|
|
|
tx.vout.append(CTxOut(self.utxo[0].nValue - 1000, script_pubkey))
|
|
|
|
|
tx.rehash()
|
|
|
|
|
|
|
|
|
|
# tx2 will spend tx1, and send back to a regular anyone-can-spend address
|
|
|
|
|
tx2 = CTransaction()
|
|
|
|
|
tx2.vin.append(CTxIn(COutPoint(tx.sha256, 0), b""))
|
|
|
|
|
tx2.vout.append(CTxOut(tx.vout[0].nValue - 1000, witness_program))
|
|
|
|
|
tx2.vout.append(CTxOut(tx.vout[0].nValue - 1000, witness_script))
|
|
|
|
|
tx2.wit.vtxinwit.append(CTxInWitness())
|
|
|
|
|
tx2.wit.vtxinwit[0].scriptWitness.stack = [witness_program]
|
|
|
|
|
tx2.wit.vtxinwit[0].scriptWitness.stack = [witness_script]
|
|
|
|
|
tx2.rehash()
|
|
|
|
|
|
|
|
|
|
block_3 = self.build_next_block()
|
|
|
|
@ -871,7 +871,7 @@ class SegWitTest(BitcoinTestFramework):
|
|
|
|
|
block_4 = self.build_next_block()
|
|
|
|
|
tx3 = CTransaction()
|
|
|
|
|
tx3.vin.append(CTxIn(COutPoint(tx2.sha256, 0), b""))
|
|
|
|
|
tx3.vout.append(CTxOut(tx.vout[0].nValue - 1000, witness_program))
|
|
|
|
|
tx3.vout.append(CTxOut(tx.vout[0].nValue - 1000, witness_script))
|
|
|
|
|
tx3.rehash()
|
|
|
|
|
block_4.vtx.append(tx3)
|
|
|
|
|
block_4.hashMerkleRoot = block_4.calc_merkle_root()
|
|
|
|
@ -933,14 +933,14 @@ class SegWitTest(BitcoinTestFramework):
|
|
|
|
|
assert len(self.utxo) > 0
|
|
|
|
|
|
|
|
|
|
# Create a P2WSH transaction.
|
|
|
|
|
# The witness program will be a bunch of OP_2DROP's, followed by OP_TRUE.
|
|
|
|
|
# The witness script will be a bunch of OP_2DROP's, followed by OP_TRUE.
|
|
|
|
|
# This should give us plenty of room to tweak the spending tx's
|
|
|
|
|
# virtual size.
|
|
|
|
|
NUM_DROPS = 200 # 201 max ops per script!
|
|
|
|
|
NUM_OUTPUTS = 50
|
|
|
|
|
|
|
|
|
|
witness_program = CScript([OP_2DROP] * NUM_DROPS + [OP_TRUE])
|
|
|
|
|
script_pubkey = script_to_p2wsh_script(witness_program)
|
|
|
|
|
witness_script = CScript([OP_2DROP] * NUM_DROPS + [OP_TRUE])
|
|
|
|
|
script_pubkey = script_to_p2wsh_script(witness_script)
|
|
|
|
|
|
|
|
|
|
prevout = COutPoint(self.utxo[0].sha256, self.utxo[0].n)
|
|
|
|
|
value = self.utxo[0].nValue
|
|
|
|
@ -960,7 +960,7 @@ class SegWitTest(BitcoinTestFramework):
|
|
|
|
|
child_tx.vout = [CTxOut(value - 100000, CScript([OP_TRUE]))]
|
|
|
|
|
for _ in range(NUM_OUTPUTS):
|
|
|
|
|
child_tx.wit.vtxinwit.append(CTxInWitness())
|
|
|
|
|
child_tx.wit.vtxinwit[-1].scriptWitness.stack = [b'a' * 195] * (2 * NUM_DROPS) + [witness_program]
|
|
|
|
|
child_tx.wit.vtxinwit[-1].scriptWitness.stack = [b'a' * 195] * (2 * NUM_DROPS) + [witness_script]
|
|
|
|
|
child_tx.rehash()
|
|
|
|
|
self.update_witness_block_with_transactions(block, [parent_tx, child_tx])
|
|
|
|
|
|
|
|
|
@ -1041,8 +1041,8 @@ class SegWitTest(BitcoinTestFramework):
|
|
|
|
|
|
|
|
|
|
block = self.build_next_block()
|
|
|
|
|
|
|
|
|
|
witness_program = CScript([OP_DROP, OP_TRUE])
|
|
|
|
|
script_pubkey = script_to_p2wsh_script(witness_program)
|
|
|
|
|
witness_script = CScript([OP_DROP, OP_TRUE])
|
|
|
|
|
script_pubkey = script_to_p2wsh_script(witness_script)
|
|
|
|
|
|
|
|
|
|
# First try extra witness data on a tx that doesn't require a witness
|
|
|
|
|
tx = CTransaction()
|
|
|
|
@ -1073,7 +1073,7 @@ class SegWitTest(BitcoinTestFramework):
|
|
|
|
|
tx2.vin.append(CTxIn(COutPoint(tx.sha256, 1), b"")) # non-witness
|
|
|
|
|
tx2.vout.append(CTxOut(tx.vout[0].nValue, CScript([OP_TRUE])))
|
|
|
|
|
tx2.wit.vtxinwit.extend([CTxInWitness(), CTxInWitness()])
|
|
|
|
|
tx2.wit.vtxinwit[0].scriptWitness.stack = [CScript([CScriptNum(1)]), CScript([CScriptNum(1)]), witness_program]
|
|
|
|
|
tx2.wit.vtxinwit[0].scriptWitness.stack = [CScript([CScriptNum(1)]), CScript([CScriptNum(1)]), witness_script]
|
|
|
|
|
tx2.wit.vtxinwit[1].scriptWitness.stack = [CScript([OP_TRUE])]
|
|
|
|
|
|
|
|
|
|
block = self.build_next_block()
|
|
|
|
@ -1113,8 +1113,8 @@ class SegWitTest(BitcoinTestFramework):
|
|
|
|
|
|
|
|
|
|
block = self.build_next_block()
|
|
|
|
|
|
|
|
|
|
witness_program = CScript([OP_DROP, OP_TRUE])
|
|
|
|
|
script_pubkey = script_to_p2wsh_script(witness_program)
|
|
|
|
|
witness_script = CScript([OP_DROP, OP_TRUE])
|
|
|
|
|
script_pubkey = script_to_p2wsh_script(witness_script)
|
|
|
|
|
|
|
|
|
|
tx = CTransaction()
|
|
|
|
|
tx.vin.append(CTxIn(COutPoint(self.utxo[0].sha256, self.utxo[0].n), b""))
|
|
|
|
@ -1126,7 +1126,7 @@ class SegWitTest(BitcoinTestFramework):
|
|
|
|
|
tx2.vout.append(CTxOut(tx.vout[0].nValue - 1000, CScript([OP_TRUE])))
|
|
|
|
|
tx2.wit.vtxinwit.append(CTxInWitness())
|
|
|
|
|
# First try a 521-byte stack element
|
|
|
|
|
tx2.wit.vtxinwit[0].scriptWitness.stack = [b'a' * (MAX_SCRIPT_ELEMENT_SIZE + 1), witness_program]
|
|
|
|
|
tx2.wit.vtxinwit[0].scriptWitness.stack = [b'a' * (MAX_SCRIPT_ELEMENT_SIZE + 1), witness_script]
|
|
|
|
|
tx2.rehash()
|
|
|
|
|
|
|
|
|
|
self.update_witness_block_with_transactions(block, [tx, tx2])
|
|
|
|
@ -1144,15 +1144,15 @@ class SegWitTest(BitcoinTestFramework):
|
|
|
|
|
self.utxo.append(UTXO(tx2.sha256, 0, tx2.vout[0].nValue))
|
|
|
|
|
|
|
|
|
|
@subtest # type: ignore
|
|
|
|
|
def test_max_witness_program_length(self):
|
|
|
|
|
def test_max_witness_script_length(self):
|
|
|
|
|
"""Test that witness outputs greater than 10kB can't be spent."""
|
|
|
|
|
|
|
|
|
|
MAX_PROGRAM_LENGTH = 10000
|
|
|
|
|
MAX_WITNESS_SCRIPT_LENGTH = 10000
|
|
|
|
|
|
|
|
|
|
# This program is 19 max pushes (9937 bytes), then 64 more opcode-bytes.
|
|
|
|
|
long_witness_program = CScript([b'a' * MAX_SCRIPT_ELEMENT_SIZE] * 19 + [OP_DROP] * 63 + [OP_TRUE])
|
|
|
|
|
assert len(long_witness_program) == MAX_PROGRAM_LENGTH + 1
|
|
|
|
|
long_script_pubkey = script_to_p2wsh_script(long_witness_program)
|
|
|
|
|
# This script is 19 max pushes (9937 bytes), then 64 more opcode-bytes.
|
|
|
|
|
long_witness_script = CScript([b'a' * MAX_SCRIPT_ELEMENT_SIZE] * 19 + [OP_DROP] * 63 + [OP_TRUE])
|
|
|
|
|
assert len(long_witness_script) == MAX_WITNESS_SCRIPT_LENGTH + 1
|
|
|
|
|
long_script_pubkey = script_to_p2wsh_script(long_witness_script)
|
|
|
|
|
|
|
|
|
|
block = self.build_next_block()
|
|
|
|
|
|
|
|
|
@ -1165,22 +1165,22 @@ class SegWitTest(BitcoinTestFramework):
|
|
|
|
|
tx2.vin.append(CTxIn(COutPoint(tx.sha256, 0), b""))
|
|
|
|
|
tx2.vout.append(CTxOut(tx.vout[0].nValue - 1000, CScript([OP_TRUE])))
|
|
|
|
|
tx2.wit.vtxinwit.append(CTxInWitness())
|
|
|
|
|
tx2.wit.vtxinwit[0].scriptWitness.stack = [b'a'] * 44 + [long_witness_program]
|
|
|
|
|
tx2.wit.vtxinwit[0].scriptWitness.stack = [b'a'] * 44 + [long_witness_script]
|
|
|
|
|
tx2.rehash()
|
|
|
|
|
|
|
|
|
|
self.update_witness_block_with_transactions(block, [tx, tx2])
|
|
|
|
|
|
|
|
|
|
test_witness_block(self.nodes[0], self.test_node, block, accepted=False)
|
|
|
|
|
|
|
|
|
|
# Try again with one less byte in the witness program
|
|
|
|
|
witness_program = CScript([b'a' * MAX_SCRIPT_ELEMENT_SIZE] * 19 + [OP_DROP] * 62 + [OP_TRUE])
|
|
|
|
|
assert len(witness_program) == MAX_PROGRAM_LENGTH
|
|
|
|
|
script_pubkey = script_to_p2wsh_script(witness_program)
|
|
|
|
|
# Try again with one less byte in the witness script
|
|
|
|
|
witness_script = CScript([b'a' * MAX_SCRIPT_ELEMENT_SIZE] * 19 + [OP_DROP] * 62 + [OP_TRUE])
|
|
|
|
|
assert len(witness_script) == MAX_WITNESS_SCRIPT_LENGTH
|
|
|
|
|
script_pubkey = script_to_p2wsh_script(witness_script)
|
|
|
|
|
|
|
|
|
|
tx.vout[0] = CTxOut(tx.vout[0].nValue, script_pubkey)
|
|
|
|
|
tx.rehash()
|
|
|
|
|
tx2.vin[0].prevout.hash = tx.sha256
|
|
|
|
|
tx2.wit.vtxinwit[0].scriptWitness.stack = [b'a'] * 43 + [witness_program]
|
|
|
|
|
tx2.wit.vtxinwit[0].scriptWitness.stack = [b'a'] * 43 + [witness_script]
|
|
|
|
|
tx2.rehash()
|
|
|
|
|
block.vtx = [block.vtx[0]]
|
|
|
|
|
self.update_witness_block_with_transactions(block, [tx, tx2])
|
|
|
|
@ -1193,8 +1193,8 @@ class SegWitTest(BitcoinTestFramework):
|
|
|
|
|
def test_witness_input_length(self):
|
|
|
|
|
"""Test that vin length must match vtxinwit length."""
|
|
|
|
|
|
|
|
|
|
witness_program = CScript([OP_DROP, OP_TRUE])
|
|
|
|
|
script_pubkey = script_to_p2wsh_script(witness_program)
|
|
|
|
|
witness_script = CScript([OP_DROP, OP_TRUE])
|
|
|
|
|
script_pubkey = script_to_p2wsh_script(witness_script)
|
|
|
|
|
|
|
|
|
|
# Create a transaction that splits our utxo into many outputs
|
|
|
|
|
tx = CTransaction()
|
|
|
|
@ -1238,7 +1238,7 @@ class SegWitTest(BitcoinTestFramework):
|
|
|
|
|
# First try using a too long vtxinwit
|
|
|
|
|
for i in range(11):
|
|
|
|
|
tx2.wit.vtxinwit.append(CTxInWitness())
|
|
|
|
|
tx2.wit.vtxinwit[i].scriptWitness.stack = [b'a', witness_program]
|
|
|
|
|
tx2.wit.vtxinwit[i].scriptWitness.stack = [b'a', witness_script]
|
|
|
|
|
|
|
|
|
|
block = self.build_next_block()
|
|
|
|
|
self.update_witness_block_with_transactions(block, [tx2])
|
|
|
|
@ -1254,15 +1254,15 @@ class SegWitTest(BitcoinTestFramework):
|
|
|
|
|
|
|
|
|
|
# Now make one of the intermediate witnesses be incorrect
|
|
|
|
|
tx2.wit.vtxinwit.append(CTxInWitness())
|
|
|
|
|
tx2.wit.vtxinwit[-1].scriptWitness.stack = [b'a', witness_program]
|
|
|
|
|
tx2.wit.vtxinwit[5].scriptWitness.stack = [witness_program]
|
|
|
|
|
tx2.wit.vtxinwit[-1].scriptWitness.stack = [b'a', witness_script]
|
|
|
|
|
tx2.wit.vtxinwit[5].scriptWitness.stack = [witness_script]
|
|
|
|
|
|
|
|
|
|
block.vtx = [block.vtx[0]]
|
|
|
|
|
self.update_witness_block_with_transactions(block, [tx2])
|
|
|
|
|
test_witness_block(self.nodes[0], self.test_node, block, accepted=False)
|
|
|
|
|
|
|
|
|
|
# Fix the broken witness and the block should be accepted.
|
|
|
|
|
tx2.wit.vtxinwit[5].scriptWitness.stack = [b'a', witness_program]
|
|
|
|
|
tx2.wit.vtxinwit[5].scriptWitness.stack = [b'a', witness_script]
|
|
|
|
|
block.vtx = [block.vtx[0]]
|
|
|
|
|
self.update_witness_block_with_transactions(block, [tx2])
|
|
|
|
|
test_witness_block(self.nodes[0], self.test_node, block, accepted=True)
|
|
|
|
@ -1300,8 +1300,8 @@ class SegWitTest(BitcoinTestFramework):
|
|
|
|
|
test_transaction_acceptance(self.nodes[0], self.test_node, tx, with_witness=False, accepted=True)
|
|
|
|
|
|
|
|
|
|
# Now try to add extra witness data to a valid witness tx.
|
|
|
|
|
witness_program = CScript([OP_TRUE])
|
|
|
|
|
script_pubkey = script_to_p2wsh_script(witness_program)
|
|
|
|
|
witness_script = CScript([OP_TRUE])
|
|
|
|
|
script_pubkey = script_to_p2wsh_script(witness_script)
|
|
|
|
|
tx2 = CTransaction()
|
|
|
|
|
tx2.vin.append(CTxIn(COutPoint(tx_hash, 0), b""))
|
|
|
|
|
tx2.vout.append(CTxOut(tx.vout[0].nValue - 1000, script_pubkey))
|
|
|
|
@ -1312,10 +1312,10 @@ class SegWitTest(BitcoinTestFramework):
|
|
|
|
|
tx3.wit.vtxinwit.append(CTxInWitness())
|
|
|
|
|
|
|
|
|
|
# Add too-large for IsStandard witness and check that it does not enter reject filter
|
|
|
|
|
p2sh_program = CScript([OP_TRUE])
|
|
|
|
|
witness_program2 = CScript([b'a' * 400000])
|
|
|
|
|
tx3.vout.append(CTxOut(tx2.vout[0].nValue - 1000, script_to_p2sh_script(p2sh_program)))
|
|
|
|
|
tx3.wit.vtxinwit[0].scriptWitness.stack = [witness_program2]
|
|
|
|
|
p2sh_script = CScript([OP_TRUE])
|
|
|
|
|
witness_script2 = CScript([b'a' * 400000])
|
|
|
|
|
tx3.vout.append(CTxOut(tx2.vout[0].nValue - 1000, script_to_p2sh_script(p2sh_script)))
|
|
|
|
|
tx3.wit.vtxinwit[0].scriptWitness.stack = [witness_script2]
|
|
|
|
|
tx3.rehash()
|
|
|
|
|
|
|
|
|
|
# Node will not be blinded to the transaction, requesting it any number of times
|
|
|
|
@ -1329,14 +1329,14 @@ class SegWitTest(BitcoinTestFramework):
|
|
|
|
|
|
|
|
|
|
# Remove witness stuffing, instead add extra witness push on stack
|
|
|
|
|
tx3.vout[0] = CTxOut(tx2.vout[0].nValue - 1000, CScript([OP_TRUE, OP_DROP] * 15 + [OP_TRUE]))
|
|
|
|
|
tx3.wit.vtxinwit[0].scriptWitness.stack = [CScript([CScriptNum(1)]), witness_program]
|
|
|
|
|
tx3.wit.vtxinwit[0].scriptWitness.stack = [CScript([CScriptNum(1)]), witness_script]
|
|
|
|
|
tx3.rehash()
|
|
|
|
|
|
|
|
|
|
test_transaction_acceptance(self.nodes[0], self.test_node, tx2, with_witness=True, accepted=True)
|
|
|
|
|
test_transaction_acceptance(self.nodes[0], self.test_node, tx3, with_witness=True, accepted=False)
|
|
|
|
|
|
|
|
|
|
# Get rid of the extra witness, and verify acceptance.
|
|
|
|
|
tx3.wit.vtxinwit[0].scriptWitness.stack = [witness_program]
|
|
|
|
|
tx3.wit.vtxinwit[0].scriptWitness.stack = [witness_script]
|
|
|
|
|
# Also check that old_node gets a tx announcement, even though this is
|
|
|
|
|
# a witness transaction.
|
|
|
|
|
self.old_node.wait_for_inv([CInv(MSG_TX, tx2.sha256)]) # wait until tx2 was inv'ed
|
|
|
|
@ -1353,7 +1353,7 @@ class SegWitTest(BitcoinTestFramework):
|
|
|
|
|
assert_equal(raw_tx["vsize"], vsize)
|
|
|
|
|
assert_equal(raw_tx["weight"], weight)
|
|
|
|
|
assert_equal(len(raw_tx["vin"][0]["txinwitness"]), 1)
|
|
|
|
|
assert_equal(raw_tx["vin"][0]["txinwitness"][0], witness_program.hex())
|
|
|
|
|
assert_equal(raw_tx["vin"][0]["txinwitness"][0], witness_script.hex())
|
|
|
|
|
assert vsize != raw_tx["size"]
|
|
|
|
|
|
|
|
|
|
# Cleanup: mine the transactions and update utxo for next test
|
|
|
|
@ -1389,8 +1389,8 @@ class SegWitTest(BitcoinTestFramework):
|
|
|
|
|
self.sync_blocks()
|
|
|
|
|
temp_utxo = []
|
|
|
|
|
tx = CTransaction()
|
|
|
|
|
witness_program = CScript([OP_TRUE])
|
|
|
|
|
witness_hash = sha256(witness_program)
|
|
|
|
|
witness_script = CScript([OP_TRUE])
|
|
|
|
|
witness_hash = sha256(witness_script)
|
|
|
|
|
assert_equal(len(self.nodes[1].getrawmempool()), 0)
|
|
|
|
|
for version in list(range(OP_1, OP_16 + 1)) + [OP_0]:
|
|
|
|
|
# First try to spend to a future version segwit script_pubkey.
|
|
|
|
@ -1418,7 +1418,7 @@ class SegWitTest(BitcoinTestFramework):
|
|
|
|
|
tx2.vin = [CTxIn(COutPoint(tx.sha256, 0), b"")]
|
|
|
|
|
tx2.vout = [CTxOut(tx.vout[0].nValue - 1000, script_pubkey)]
|
|
|
|
|
tx2.wit.vtxinwit.append(CTxInWitness())
|
|
|
|
|
tx2.wit.vtxinwit[0].scriptWitness.stack = [witness_program]
|
|
|
|
|
tx2.wit.vtxinwit[0].scriptWitness.stack = [witness_script]
|
|
|
|
|
tx2.rehash()
|
|
|
|
|
# Gets accepted to both policy-enforcing nodes and others.
|
|
|
|
|
test_transaction_acceptance(self.nodes[0], self.test_node, tx2, with_witness=True, accepted=True)
|
|
|
|
@ -1433,7 +1433,7 @@ class SegWitTest(BitcoinTestFramework):
|
|
|
|
|
tx3.vin.append(CTxIn(COutPoint(i.sha256, i.n), b""))
|
|
|
|
|
tx3.wit.vtxinwit.append(CTxInWitness())
|
|
|
|
|
total_value += i.nValue
|
|
|
|
|
tx3.wit.vtxinwit[-1].scriptWitness.stack = [witness_program]
|
|
|
|
|
tx3.wit.vtxinwit[-1].scriptWitness.stack = [witness_script]
|
|
|
|
|
tx3.vout.append(CTxOut(total_value - 1000, script_pubkey))
|
|
|
|
|
tx3.rehash()
|
|
|
|
|
|
|
|
|
@ -1462,8 +1462,8 @@ class SegWitTest(BitcoinTestFramework):
|
|
|
|
|
|
|
|
|
|
block = self.build_next_block()
|
|
|
|
|
# Change the output of the block to be a witness output.
|
|
|
|
|
witness_program = CScript([OP_TRUE])
|
|
|
|
|
script_pubkey = script_to_p2wsh_script(witness_program)
|
|
|
|
|
witness_script = CScript([OP_TRUE])
|
|
|
|
|
script_pubkey = script_to_p2wsh_script(witness_script)
|
|
|
|
|
block.vtx[0].vout[0].scriptPubKey = script_pubkey
|
|
|
|
|
# This next line will rehash the coinbase and update the merkle
|
|
|
|
|
# root, and solve.
|
|
|
|
@ -1472,9 +1472,9 @@ class SegWitTest(BitcoinTestFramework):
|
|
|
|
|
|
|
|
|
|
spend_tx = CTransaction()
|
|
|
|
|
spend_tx.vin = [CTxIn(COutPoint(block.vtx[0].sha256, 0), b"")]
|
|
|
|
|
spend_tx.vout = [CTxOut(block.vtx[0].vout[0].nValue, witness_program)]
|
|
|
|
|
spend_tx.vout = [CTxOut(block.vtx[0].vout[0].nValue, witness_script)]
|
|
|
|
|
spend_tx.wit.vtxinwit.append(CTxInWitness())
|
|
|
|
|
spend_tx.wit.vtxinwit[0].scriptWitness.stack = [witness_program]
|
|
|
|
|
spend_tx.wit.vtxinwit[0].scriptWitness.stack = [witness_script]
|
|
|
|
|
spend_tx.rehash()
|
|
|
|
|
|
|
|
|
|
# Now test a premature spend.
|
|
|
|
@ -1523,8 +1523,8 @@ class SegWitTest(BitcoinTestFramework):
|
|
|
|
|
|
|
|
|
|
# Now try to spend it. Send it to a P2WSH output, which we'll
|
|
|
|
|
# use in the next test.
|
|
|
|
|
witness_program = CScript([pubkey, CScriptOp(OP_CHECKSIG)])
|
|
|
|
|
script_wsh = script_to_p2wsh_script(witness_program)
|
|
|
|
|
witness_script = CScript([pubkey, CScriptOp(OP_CHECKSIG)])
|
|
|
|
|
script_wsh = script_to_p2wsh_script(witness_script)
|
|
|
|
|
|
|
|
|
|
tx2 = CTransaction()
|
|
|
|
|
tx2.vin.append(CTxIn(COutPoint(tx.sha256, 0), b""))
|
|
|
|
@ -1553,7 +1553,7 @@ class SegWitTest(BitcoinTestFramework):
|
|
|
|
|
tx3.vin.append(CTxIn(COutPoint(tx2.sha256, 0), b""))
|
|
|
|
|
tx3.vout.append(CTxOut(tx2.vout[0].nValue - 1000, script_p2sh))
|
|
|
|
|
tx3.wit.vtxinwit.append(CTxInWitness())
|
|
|
|
|
sign_p2pk_witness_input(witness_program, tx3, 0, SIGHASH_ALL, tx2.vout[0].nValue, key)
|
|
|
|
|
sign_p2pk_witness_input(witness_script, tx3, 0, SIGHASH_ALL, tx2.vout[0].nValue, key)
|
|
|
|
|
|
|
|
|
|
# Should fail policy test.
|
|
|
|
|
test_transaction_acceptance(self.nodes[0], self.test_node, tx3, True, False, 'non-mandatory-script-verify-flag (Using non-compressed keys in segwit)')
|
|
|
|
@ -1570,7 +1570,7 @@ class SegWitTest(BitcoinTestFramework):
|
|
|
|
|
tx4.vin.append(CTxIn(COutPoint(tx3.sha256, 0), script_sig))
|
|
|
|
|
tx4.vout.append(CTxOut(tx3.vout[0].nValue - 1000, script_pubkey))
|
|
|
|
|
tx4.wit.vtxinwit.append(CTxInWitness())
|
|
|
|
|
sign_p2pk_witness_input(witness_program, tx4, 0, SIGHASH_ALL, tx3.vout[0].nValue, key)
|
|
|
|
|
sign_p2pk_witness_input(witness_script, tx4, 0, SIGHASH_ALL, tx3.vout[0].nValue, key)
|
|
|
|
|
|
|
|
|
|
# Should fail policy test.
|
|
|
|
|
test_transaction_acceptance(self.nodes[0], self.test_node, tx4, True, False, 'non-mandatory-script-verify-flag (Using non-compressed keys in segwit)')
|
|
|
|
@ -1601,8 +1601,8 @@ class SegWitTest(BitcoinTestFramework):
|
|
|
|
|
key.generate()
|
|
|
|
|
pubkey = key.get_pubkey().get_bytes()
|
|
|
|
|
|
|
|
|
|
witness_program = CScript([pubkey, CScriptOp(OP_CHECKSIG)])
|
|
|
|
|
script_pubkey = script_to_p2wsh_script(witness_program)
|
|
|
|
|
witness_script = CScript([pubkey, CScriptOp(OP_CHECKSIG)])
|
|
|
|
|
script_pubkey = script_to_p2wsh_script(witness_script)
|
|
|
|
|
|
|
|
|
|
# First create a witness output for use in the tests.
|
|
|
|
|
tx = CTransaction()
|
|
|
|
@ -1629,18 +1629,18 @@ class SegWitTest(BitcoinTestFramework):
|
|
|
|
|
tx.vout.append(CTxOut(prev_utxo.nValue - 1000, script_pubkey))
|
|
|
|
|
tx.wit.vtxinwit.append(CTxInWitness())
|
|
|
|
|
# Too-large input value
|
|
|
|
|
sign_p2pk_witness_input(witness_program, tx, 0, hashtype, prev_utxo.nValue + 1, key)
|
|
|
|
|
sign_p2pk_witness_input(witness_script, tx, 0, hashtype, prev_utxo.nValue + 1, key)
|
|
|
|
|
self.update_witness_block_with_transactions(block, [tx])
|
|
|
|
|
test_witness_block(self.nodes[0], self.test_node, block, accepted=False)
|
|
|
|
|
|
|
|
|
|
# Too-small input value
|
|
|
|
|
sign_p2pk_witness_input(witness_program, tx, 0, hashtype, prev_utxo.nValue - 1, key)
|
|
|
|
|
sign_p2pk_witness_input(witness_script, tx, 0, hashtype, prev_utxo.nValue - 1, key)
|
|
|
|
|
block.vtx.pop() # remove last tx
|
|
|
|
|
self.update_witness_block_with_transactions(block, [tx])
|
|
|
|
|
test_witness_block(self.nodes[0], self.test_node, block, accepted=False)
|
|
|
|
|
|
|
|
|
|
# Now try correct value
|
|
|
|
|
sign_p2pk_witness_input(witness_program, tx, 0, hashtype, prev_utxo.nValue, key)
|
|
|
|
|
sign_p2pk_witness_input(witness_script, tx, 0, hashtype, prev_utxo.nValue, key)
|
|
|
|
|
block.vtx.pop()
|
|
|
|
|
self.update_witness_block_with_transactions(block, [tx])
|
|
|
|
|
test_witness_block(self.nodes[0], self.test_node, block, accepted=True)
|
|
|
|
@ -1661,7 +1661,7 @@ class SegWitTest(BitcoinTestFramework):
|
|
|
|
|
for _ in range(NUM_SIGHASH_TESTS):
|
|
|
|
|
tx.vout.append(CTxOut(split_value, script_pubkey))
|
|
|
|
|
tx.wit.vtxinwit.append(CTxInWitness())
|
|
|
|
|
sign_p2pk_witness_input(witness_program, tx, 0, SIGHASH_ALL, prev_utxo.nValue, key)
|
|
|
|
|
sign_p2pk_witness_input(witness_script, tx, 0, SIGHASH_ALL, prev_utxo.nValue, key)
|
|
|
|
|
for i in range(NUM_SIGHASH_TESTS):
|
|
|
|
|
temp_utxos.append(UTXO(tx.sha256, i, split_value))
|
|
|
|
|
|
|
|
|
@ -1696,7 +1696,7 @@ class SegWitTest(BitcoinTestFramework):
|
|
|
|
|
if random.randint(0, 1):
|
|
|
|
|
anyonecanpay = SIGHASH_ANYONECANPAY
|
|
|
|
|
hashtype = random.randint(1, 3) | anyonecanpay
|
|
|
|
|
sign_p2pk_witness_input(witness_program, tx, i, hashtype, temp_utxos[i].nValue, key)
|
|
|
|
|
sign_p2pk_witness_input(witness_script, tx, i, hashtype, temp_utxos[i].nValue, key)
|
|
|
|
|
if (hashtype == SIGHASH_SINGLE and i >= num_outputs):
|
|
|
|
|
used_sighash_single_out_of_bounds = True
|
|
|
|
|
tx.rehash()
|
|
|
|
@ -1726,7 +1726,7 @@ class SegWitTest(BitcoinTestFramework):
|
|
|
|
|
tx.vin.append(CTxIn(COutPoint(temp_utxos[0].sha256, temp_utxos[0].n), b""))
|
|
|
|
|
tx.vout.append(CTxOut(temp_utxos[0].nValue, script_pkh))
|
|
|
|
|
tx.wit.vtxinwit.append(CTxInWitness())
|
|
|
|
|
sign_p2pk_witness_input(witness_program, tx, 0, SIGHASH_ALL, temp_utxos[0].nValue, key)
|
|
|
|
|
sign_p2pk_witness_input(witness_script, tx, 0, SIGHASH_ALL, temp_utxos[0].nValue, key)
|
|
|
|
|
tx2 = CTransaction()
|
|
|
|
|
tx2.vin.append(CTxIn(COutPoint(tx.sha256, 0), b""))
|
|
|
|
|
tx2.vout.append(CTxOut(tx.vout[0].nValue, CScript([OP_TRUE])))
|
|
|
|
@ -1766,7 +1766,7 @@ class SegWitTest(BitcoinTestFramework):
|
|
|
|
|
# the signatures as we go.
|
|
|
|
|
tx.vin.append(CTxIn(COutPoint(i.sha256, i.n), b""))
|
|
|
|
|
tx.wit.vtxinwit.append(CTxInWitness())
|
|
|
|
|
sign_p2pk_witness_input(witness_program, tx, index, SIGHASH_ALL | SIGHASH_ANYONECANPAY, i.nValue, key)
|
|
|
|
|
sign_p2pk_witness_input(witness_script, tx, index, SIGHASH_ALL | SIGHASH_ANYONECANPAY, i.nValue, key)
|
|
|
|
|
index += 1
|
|
|
|
|
block = self.build_next_block()
|
|
|
|
|
self.update_witness_block_with_transactions(block, [tx])
|
|
|
|
@ -1965,8 +1965,8 @@ class SegWitTest(BitcoinTestFramework):
|
|
|
|
|
"""Test sigop counting is correct inside witnesses."""
|
|
|
|
|
|
|
|
|
|
# Keep this under MAX_OPS_PER_SCRIPT (201)
|
|
|
|
|
witness_program = CScript([OP_TRUE, OP_IF, OP_TRUE, OP_ELSE] + [OP_CHECKMULTISIG] * 5 + [OP_CHECKSIG] * 193 + [OP_ENDIF])
|
|
|
|
|
script_pubkey = script_to_p2wsh_script(witness_program)
|
|
|
|
|
witness_script = CScript([OP_TRUE, OP_IF, OP_TRUE, OP_ELSE] + [OP_CHECKMULTISIG] * 5 + [OP_CHECKSIG] * 193 + [OP_ENDIF])
|
|
|
|
|
script_pubkey = script_to_p2wsh_script(witness_script)
|
|
|
|
|
|
|
|
|
|
sigops_per_script = 20 * 5 + 193 * 1
|
|
|
|
|
# We'll produce 2 extra outputs, one with a program that would take us
|
|
|
|
@ -1981,13 +1981,13 @@ class SegWitTest(BitcoinTestFramework):
|
|
|
|
|
# This script, when spent with the first
|
|
|
|
|
# N(=MAX_SIGOP_COST//sigops_per_script) outputs of our transaction,
|
|
|
|
|
# would push us just over the block sigop limit.
|
|
|
|
|
witness_program_toomany = CScript([OP_TRUE, OP_IF, OP_TRUE, OP_ELSE] + [OP_CHECKSIG] * (extra_sigops_available + 1) + [OP_ENDIF])
|
|
|
|
|
script_pubkey_toomany = script_to_p2wsh_script(witness_program_toomany)
|
|
|
|
|
witness_script_toomany = CScript([OP_TRUE, OP_IF, OP_TRUE, OP_ELSE] + [OP_CHECKSIG] * (extra_sigops_available + 1) + [OP_ENDIF])
|
|
|
|
|
script_pubkey_toomany = script_to_p2wsh_script(witness_script_toomany)
|
|
|
|
|
|
|
|
|
|
# If we spend this script instead, we would exactly reach our sigop
|
|
|
|
|
# limit (for witness sigops).
|
|
|
|
|
witness_program_justright = CScript([OP_TRUE, OP_IF, OP_TRUE, OP_ELSE] + [OP_CHECKSIG] * (extra_sigops_available) + [OP_ENDIF])
|
|
|
|
|
script_pubkey_justright = script_to_p2wsh_script(witness_program_justright)
|
|
|
|
|
witness_script_justright = CScript([OP_TRUE, OP_IF, OP_TRUE, OP_ELSE] + [OP_CHECKSIG] * (extra_sigops_available) + [OP_ENDIF])
|
|
|
|
|
script_pubkey_justright = script_to_p2wsh_script(witness_script_justright)
|
|
|
|
|
|
|
|
|
|
# First split our available utxo into a bunch of outputs
|
|
|
|
|
split_value = self.utxo[0].nValue // outputs
|
|
|
|
@ -2010,9 +2010,9 @@ class SegWitTest(BitcoinTestFramework):
|
|
|
|
|
for i in range(outputs - 1):
|
|
|
|
|
tx2.vin.append(CTxIn(COutPoint(tx.sha256, i), b""))
|
|
|
|
|
tx2.wit.vtxinwit.append(CTxInWitness())
|
|
|
|
|
tx2.wit.vtxinwit[-1].scriptWitness.stack = [witness_program]
|
|
|
|
|
tx2.wit.vtxinwit[-1].scriptWitness.stack = [witness_script]
|
|
|
|
|
total_value += tx.vout[i].nValue
|
|
|
|
|
tx2.wit.vtxinwit[-1].scriptWitness.stack = [witness_program_toomany]
|
|
|
|
|
tx2.wit.vtxinwit[-1].scriptWitness.stack = [witness_script_toomany]
|
|
|
|
|
tx2.vout.append(CTxOut(total_value, CScript([OP_TRUE])))
|
|
|
|
|
tx2.rehash()
|
|
|
|
|
|
|
|
|
@ -2051,7 +2051,7 @@ class SegWitTest(BitcoinTestFramework):
|
|
|
|
|
tx2.vout.pop()
|
|
|
|
|
tx2.vin.append(CTxIn(COutPoint(tx.sha256, outputs - 1), b""))
|
|
|
|
|
tx2.wit.vtxinwit.append(CTxInWitness())
|
|
|
|
|
tx2.wit.vtxinwit[-1].scriptWitness.stack = [witness_program_justright]
|
|
|
|
|
tx2.wit.vtxinwit[-1].scriptWitness.stack = [witness_script_justright]
|
|
|
|
|
tx2.rehash()
|
|
|
|
|
self.update_witness_block_with_transactions(block_5, [tx2])
|
|
|
|
|
test_witness_block(self.nodes[0], self.test_node, block_5, accepted=True)
|
|
|
|
@ -2119,8 +2119,8 @@ class SegWitTest(BitcoinTestFramework):
|
|
|
|
|
|
|
|
|
|
# Create a Segwit output from the latest UTXO
|
|
|
|
|
# and announce it to the network
|
|
|
|
|
witness_program = CScript([OP_TRUE])
|
|
|
|
|
script_pubkey = script_to_p2wsh_script(witness_program)
|
|
|
|
|
witness_script = CScript([OP_TRUE])
|
|
|
|
|
script_pubkey = script_to_p2wsh_script(witness_script)
|
|
|
|
|
|
|
|
|
|
tx = CTransaction()
|
|
|
|
|
tx.vin.append(CTxIn(COutPoint(self.utxo[0].sha256, self.utxo[0].n), b""))
|
|
|
|
@ -2132,7 +2132,7 @@ class SegWitTest(BitcoinTestFramework):
|
|
|
|
|
tx2.vin.append(CTxIn(COutPoint(tx.sha256, 0), b""))
|
|
|
|
|
tx2.vout.append(CTxOut(tx.vout[0].nValue - 1000, script_pubkey))
|
|
|
|
|
tx2.wit.vtxinwit.append(CTxInWitness())
|
|
|
|
|
tx2.wit.vtxinwit[0].scriptWitness.stack = [witness_program]
|
|
|
|
|
tx2.wit.vtxinwit[0].scriptWitness.stack = [witness_script]
|
|
|
|
|
tx2.rehash()
|
|
|
|
|
|
|
|
|
|
# Announce Segwit transaction with wtxid
|
|
|
|
|