mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-11-27 14:30:24 +01:00
test: use script_util helpers for creating P2W{PKH,SH} scripts
This commit is contained in:
@@ -41,7 +41,6 @@ from test_framework.messages import (
|
||||
ser_vector,
|
||||
sha256,
|
||||
tx_from_hex,
|
||||
uint256_from_str,
|
||||
)
|
||||
from test_framework.p2p import (
|
||||
P2PInterface,
|
||||
@@ -74,8 +73,10 @@ from test_framework.script import (
|
||||
hash160,
|
||||
)
|
||||
from test_framework.script_util import (
|
||||
key_to_p2wpkh_script,
|
||||
keyhash_to_p2pkh_script,
|
||||
script_to_p2sh_script,
|
||||
script_to_p2wsh_script,
|
||||
)
|
||||
from test_framework.test_framework import BitcoinTestFramework
|
||||
from test_framework.util import (
|
||||
@@ -488,8 +489,7 @@ class SegWitTest(BitcoinTestFramework):
|
||||
|
||||
# Create two outputs, a p2wsh and p2sh-p2wsh
|
||||
witness_program = CScript([OP_TRUE])
|
||||
witness_hash = sha256(witness_program)
|
||||
script_pubkey = CScript([OP_0, witness_hash])
|
||||
script_pubkey = script_to_p2wsh_script(witness_program)
|
||||
p2sh_script_pubkey = script_to_p2sh_script(script_pubkey)
|
||||
|
||||
value = self.utxo[0].nValue // 3
|
||||
@@ -625,8 +625,7 @@ class SegWitTest(BitcoinTestFramework):
|
||||
V0 segwit inputs may only be mined after activation, but not before."""
|
||||
|
||||
witness_program = CScript([OP_TRUE])
|
||||
witness_hash = sha256(witness_program)
|
||||
script_pubkey = CScript([OP_0, witness_hash])
|
||||
script_pubkey = script_to_p2wsh_script(witness_program)
|
||||
p2sh_script_pubkey = script_to_p2sh_script(witness_program)
|
||||
|
||||
# First prepare a p2sh output (so that spending it will pass standardness)
|
||||
@@ -654,6 +653,7 @@ 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)
|
||||
script_pubkey = CScript([OP_0, hash160(witness_hash)])
|
||||
tx2 = CTransaction()
|
||||
# tx was accepted, so we spend the second output.
|
||||
@@ -732,8 +732,7 @@ class SegWitTest(BitcoinTestFramework):
|
||||
|
||||
# Prepare the p2sh-wrapped witness output
|
||||
witness_program = CScript([OP_DROP, OP_TRUE])
|
||||
witness_hash = sha256(witness_program)
|
||||
p2wsh_pubkey = CScript([OP_0, witness_hash])
|
||||
p2wsh_pubkey = script_to_p2wsh_script(witness_program)
|
||||
script_pubkey = script_to_p2sh_script(p2wsh_pubkey)
|
||||
script_sig = CScript([p2wsh_pubkey]) # a push of the redeem script
|
||||
|
||||
@@ -828,8 +827,7 @@ class SegWitTest(BitcoinTestFramework):
|
||||
|
||||
# Let's construct a witness program
|
||||
witness_program = CScript([OP_TRUE])
|
||||
witness_hash = sha256(witness_program)
|
||||
script_pubkey = CScript([OP_0, witness_hash])
|
||||
script_pubkey = script_to_p2wsh_script(witness_program)
|
||||
tx.vout.append(CTxOut(self.utxo[0].nValue - 1000, script_pubkey))
|
||||
tx.rehash()
|
||||
|
||||
@@ -942,8 +940,7 @@ class SegWitTest(BitcoinTestFramework):
|
||||
NUM_OUTPUTS = 50
|
||||
|
||||
witness_program = CScript([OP_2DROP] * NUM_DROPS + [OP_TRUE])
|
||||
witness_hash = uint256_from_str(sha256(witness_program))
|
||||
script_pubkey = CScript([OP_0, ser_uint256(witness_hash)])
|
||||
script_pubkey = script_to_p2wsh_script(witness_program)
|
||||
|
||||
prevout = COutPoint(self.utxo[0].sha256, self.utxo[0].n)
|
||||
value = self.utxo[0].nValue
|
||||
@@ -1045,8 +1042,7 @@ class SegWitTest(BitcoinTestFramework):
|
||||
block = self.build_next_block()
|
||||
|
||||
witness_program = CScript([OP_DROP, OP_TRUE])
|
||||
witness_hash = sha256(witness_program)
|
||||
script_pubkey = CScript([OP_0, witness_hash])
|
||||
script_pubkey = script_to_p2wsh_script(witness_program)
|
||||
|
||||
# First try extra witness data on a tx that doesn't require a witness
|
||||
tx = CTransaction()
|
||||
@@ -1118,8 +1114,7 @@ class SegWitTest(BitcoinTestFramework):
|
||||
block = self.build_next_block()
|
||||
|
||||
witness_program = CScript([OP_DROP, OP_TRUE])
|
||||
witness_hash = sha256(witness_program)
|
||||
script_pubkey = CScript([OP_0, witness_hash])
|
||||
script_pubkey = script_to_p2wsh_script(witness_program)
|
||||
|
||||
tx = CTransaction()
|
||||
tx.vin.append(CTxIn(COutPoint(self.utxo[0].sha256, self.utxo[0].n), b""))
|
||||
@@ -1157,8 +1152,7 @@ class SegWitTest(BitcoinTestFramework):
|
||||
# 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_witness_hash = sha256(long_witness_program)
|
||||
long_script_pubkey = CScript([OP_0, long_witness_hash])
|
||||
long_script_pubkey = script_to_p2wsh_script(long_witness_program)
|
||||
|
||||
block = self.build_next_block()
|
||||
|
||||
@@ -1181,8 +1175,7 @@ class SegWitTest(BitcoinTestFramework):
|
||||
# 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
|
||||
witness_hash = sha256(witness_program)
|
||||
script_pubkey = CScript([OP_0, witness_hash])
|
||||
script_pubkey = script_to_p2wsh_script(witness_program)
|
||||
|
||||
tx.vout[0] = CTxOut(tx.vout[0].nValue, script_pubkey)
|
||||
tx.rehash()
|
||||
@@ -1201,8 +1194,7 @@ class SegWitTest(BitcoinTestFramework):
|
||||
"""Test that vin length must match vtxinwit length."""
|
||||
|
||||
witness_program = CScript([OP_DROP, OP_TRUE])
|
||||
witness_hash = sha256(witness_program)
|
||||
script_pubkey = CScript([OP_0, witness_hash])
|
||||
script_pubkey = script_to_p2wsh_script(witness_program)
|
||||
|
||||
# Create a transaction that splits our utxo into many outputs
|
||||
tx = CTransaction()
|
||||
@@ -1309,8 +1301,7 @@ class SegWitTest(BitcoinTestFramework):
|
||||
|
||||
# Now try to add extra witness data to a valid witness tx.
|
||||
witness_program = CScript([OP_TRUE])
|
||||
witness_hash = sha256(witness_program)
|
||||
script_pubkey = CScript([OP_0, witness_hash])
|
||||
script_pubkey = script_to_p2wsh_script(witness_program)
|
||||
tx2 = CTransaction()
|
||||
tx2.vin.append(CTxIn(COutPoint(tx_hash, 0), b""))
|
||||
tx2.vout.append(CTxOut(tx.vout[0].nValue - 1000, script_pubkey))
|
||||
@@ -1472,8 +1463,7 @@ class SegWitTest(BitcoinTestFramework):
|
||||
block = self.build_next_block()
|
||||
# Change the output of the block to be a witness output.
|
||||
witness_program = CScript([OP_TRUE])
|
||||
witness_hash = sha256(witness_program)
|
||||
script_pubkey = CScript([OP_0, witness_hash])
|
||||
script_pubkey = script_to_p2wsh_script(witness_program)
|
||||
block.vtx[0].vout[0].scriptPubKey = script_pubkey
|
||||
# This next line will rehash the coinbase and update the merkle
|
||||
# root, and solve.
|
||||
@@ -1520,7 +1510,7 @@ class SegWitTest(BitcoinTestFramework):
|
||||
# Test 1: P2WPKH
|
||||
# First create a P2WPKH output that uses an uncompressed pubkey
|
||||
pubkeyhash = hash160(pubkey)
|
||||
script_pkh = CScript([OP_0, pubkeyhash])
|
||||
script_pkh = key_to_p2wpkh_script(pubkey)
|
||||
tx = CTransaction()
|
||||
tx.vin.append(CTxIn(COutPoint(utxo.sha256, utxo.n), b""))
|
||||
tx.vout.append(CTxOut(utxo.nValue - 1000, script_pkh))
|
||||
@@ -1534,8 +1524,7 @@ 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)])
|
||||
witness_hash = sha256(witness_program)
|
||||
script_wsh = CScript([OP_0, witness_hash])
|
||||
script_wsh = script_to_p2wsh_script(witness_program)
|
||||
|
||||
tx2 = CTransaction()
|
||||
tx2.vin.append(CTxIn(COutPoint(tx.sha256, 0), b""))
|
||||
@@ -1613,8 +1602,7 @@ class SegWitTest(BitcoinTestFramework):
|
||||
pubkey = key.get_pubkey().get_bytes()
|
||||
|
||||
witness_program = CScript([pubkey, CScriptOp(OP_CHECKSIG)])
|
||||
witness_hash = sha256(witness_program)
|
||||
script_pubkey = CScript([OP_0, witness_hash])
|
||||
script_pubkey = script_to_p2wsh_script(witness_program)
|
||||
|
||||
# First create a witness output for use in the tests.
|
||||
tx = CTransaction()
|
||||
@@ -1733,7 +1721,7 @@ class SegWitTest(BitcoinTestFramework):
|
||||
|
||||
# Now test witness version 0 P2PKH transactions
|
||||
pubkeyhash = hash160(pubkey)
|
||||
script_pkh = CScript([OP_0, pubkeyhash])
|
||||
script_pkh = key_to_p2wpkh_script(pubkey)
|
||||
tx = CTransaction()
|
||||
tx.vin.append(CTxIn(COutPoint(temp_utxos[0].sha256, temp_utxos[0].n), b""))
|
||||
tx.vout.append(CTxOut(temp_utxos[0].nValue, script_pkh))
|
||||
@@ -1860,7 +1848,7 @@ class SegWitTest(BitcoinTestFramework):
|
||||
# For each script, generate a pair of P2WSH and P2SH-P2WSH output.
|
||||
outputvalue = (self.utxo[0].nValue - 1000) // (len(scripts) * 2)
|
||||
for i in scripts:
|
||||
p2wsh = CScript([OP_0, sha256(i)])
|
||||
p2wsh = script_to_p2wsh_script(i)
|
||||
p2wsh_scripts.append(p2wsh)
|
||||
tx.vout.append(CTxOut(outputvalue, p2wsh))
|
||||
tx.vout.append(CTxOut(outputvalue, script_to_p2sh_script(p2wsh)))
|
||||
@@ -1877,13 +1865,13 @@ class SegWitTest(BitcoinTestFramework):
|
||||
for i in range(len(scripts)):
|
||||
p2wsh_tx = CTransaction()
|
||||
p2wsh_tx.vin.append(CTxIn(COutPoint(txid, i * 2)))
|
||||
p2wsh_tx.vout.append(CTxOut(outputvalue - 5000, CScript([OP_0, hash160(hex_str_to_bytes(""))])))
|
||||
p2wsh_tx.vout.append(CTxOut(outputvalue - 5000, CScript([OP_0, hash160(b"")])))
|
||||
p2wsh_tx.wit.vtxinwit.append(CTxInWitness())
|
||||
p2wsh_tx.rehash()
|
||||
p2wsh_txs.append(p2wsh_tx)
|
||||
p2sh_tx = CTransaction()
|
||||
p2sh_tx.vin.append(CTxIn(COutPoint(txid, i * 2 + 1), CScript([p2wsh_scripts[i]])))
|
||||
p2sh_tx.vout.append(CTxOut(outputvalue - 5000, CScript([OP_0, hash160(hex_str_to_bytes(""))])))
|
||||
p2sh_tx.vout.append(CTxOut(outputvalue - 5000, CScript([OP_0, hash160(b"")])))
|
||||
p2sh_tx.wit.vtxinwit.append(CTxInWitness())
|
||||
p2sh_tx.rehash()
|
||||
p2sh_txs.append(p2sh_tx)
|
||||
@@ -1978,8 +1966,7 @@ class SegWitTest(BitcoinTestFramework):
|
||||
|
||||
# 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])
|
||||
witness_hash = sha256(witness_program)
|
||||
script_pubkey = CScript([OP_0, witness_hash])
|
||||
script_pubkey = script_to_p2wsh_script(witness_program)
|
||||
|
||||
sigops_per_script = 20 * 5 + 193 * 1
|
||||
# We'll produce 2 extra outputs, one with a program that would take us
|
||||
@@ -1995,14 +1982,12 @@ class SegWitTest(BitcoinTestFramework):
|
||||
# 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])
|
||||
witness_hash_toomany = sha256(witness_program_toomany)
|
||||
script_pubkey_toomany = CScript([OP_0, witness_hash_toomany])
|
||||
script_pubkey_toomany = script_to_p2wsh_script(witness_program_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])
|
||||
witness_hash_justright = sha256(witness_program_justright)
|
||||
script_pubkey_justright = CScript([OP_0, witness_hash_justright])
|
||||
script_pubkey_justright = script_to_p2wsh_script(witness_program_justright)
|
||||
|
||||
# First split our available utxo into a bunch of outputs
|
||||
split_value = self.utxo[0].nValue // outputs
|
||||
@@ -2135,8 +2120,7 @@ class SegWitTest(BitcoinTestFramework):
|
||||
# Create a Segwit output from the latest UTXO
|
||||
# and announce it to the network
|
||||
witness_program = CScript([OP_TRUE])
|
||||
witness_hash = sha256(witness_program)
|
||||
script_pubkey = CScript([OP_0, witness_hash])
|
||||
script_pubkey = script_to_p2wsh_script(witness_program)
|
||||
|
||||
tx = CTransaction()
|
||||
tx.vin.append(CTxIn(COutPoint(self.utxo[0].sha256, self.utxo[0].n), b""))
|
||||
|
||||
Reference in New Issue
Block a user