@ -101,7 +101,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 ]
@ -272,7 +272,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 ( )
@ -473,8 +473,8 @@ class SegWitTest(BitcoinTestFramework):
blocks are permitted to contain witnesses ) . """
# 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
@ -581,9 +581,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 ( )
@ -599,7 +599,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
@ -610,14 +610,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 )
@ -630,7 +630,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
@ -688,8 +688,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
@ -733,7 +733,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 )
@ -782,18 +782,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 ( )
@ -828,7 +828,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 ( )
@ -890,14 +890,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
@ -917,7 +917,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 ] )
@ -998,8 +998,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 ( )
@ -1030,7 +1030,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 ( )
@ -1070,8 +1070,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 " " ) )
@ -1083,7 +1083,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 ] )
@ -1101,15 +1101,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 ( )
@ -1122,22 +1122,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 ] )
@ -1150,8 +1150,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 ( )
@ -1195,7 +1195,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 ] )
@ -1211,15 +1211,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 )
@ -1257,8 +1257,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 ) )
@ -1269,10 +1269,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_ program 2 = 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_ program 2]
p2sh_ script = CScript ( [ OP_TRUE ] )
witness_ script 2 = 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_ script 2]
tx3 . rehash ( )
# Node will not be blinded to the transaction, requesting it any number of times
@ -1286,14 +1286,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
@ -1310,7 +1310,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
@ -1346,8 +1346,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.
@ -1375,7 +1375,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 )
@ -1390,7 +1390,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 ( )
@ -1419,8 +1419,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.
@ -1429,9 +1429,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.
@ -1480,8 +1480,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 " " ) )
@ -1510,7 +1510,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) ' )
@ -1527,7 +1527,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) ' )
@ -1558,8 +1558,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 ( )
@ -1586,18 +1586,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 )
@ -1618,7 +1618,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 ) )
@ -1653,7 +1653,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 ( )
@ -1683,7 +1683,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 ] ) ) )
@ -1723,7 +1723,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 ] )
@ -1889,8 +1889,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
@ -1905,13 +1905,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
@ -1934,9 +1934,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 ( )
@ -1975,7 +1975,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 )
@ -2043,8 +2043,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 " " ) )
@ -2056,7 +2056,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