@@ -1706,6 +1706,116 @@ def test_getblocktemplate_before_lockin(self):
1706
1706
assert (block_version & (1 << VB_WITNESS_BIT ) != 0 )
1707
1707
self .nodes [0 ].setmocktime (0 ) # undo mocktime
1708
1708
1709
+ def test_non_standard_witness (self ):
1710
+ print ("\t Testing detection of non-standard P2WSH witness" )
1711
+ pad = chr (1 ).encode ('latin-1' )
1712
+
1713
+ # Create scripts for tests
1714
+ scripts = []
1715
+ scripts .append (CScript ([OP_DROP ] * 100 ))
1716
+ scripts .append (CScript ([OP_DROP ] * 99 ))
1717
+ scripts .append (CScript ([pad * 59 ] * 59 + [OP_DROP ] * 60 ))
1718
+ scripts .append (CScript ([pad * 59 ] * 59 + [OP_DROP ] * 61 ))
1719
+
1720
+ p2wsh_scripts = []
1721
+
1722
+ assert (len (self .utxo ))
1723
+ tx = CTransaction ()
1724
+ tx .vin .append (CTxIn (COutPoint (self .utxo [0 ].sha256 , self .utxo [0 ].n ), b"" ))
1725
+
1726
+ # For each script, generate a pair of P2WSH and P2SH-P2WSH output.
1727
+ outputvalue = (self .utxo [0 ].nValue - 1000 ) // (len (scripts ) * 2 )
1728
+ for i in scripts :
1729
+ p2wsh = CScript ([OP_0 , sha256 (i )])
1730
+ p2sh = hash160 (p2wsh )
1731
+ p2wsh_scripts .append (p2wsh )
1732
+ tx .vout .append (CTxOut (outputvalue , p2wsh ))
1733
+ tx .vout .append (CTxOut (outputvalue , CScript ([OP_HASH160 , p2sh , OP_EQUAL ])))
1734
+ tx .rehash ()
1735
+ txid = tx .sha256
1736
+ self .test_node .test_transaction_acceptance (tx , with_witness = False , accepted = True )
1737
+
1738
+ self .nodes [0 ].generate (1 )
1739
+ sync_blocks (self .nodes )
1740
+
1741
+ # Creating transactions for tests
1742
+ p2wsh_txs = []
1743
+ p2sh_txs = []
1744
+ for i in range (len (scripts )):
1745
+ p2wsh_tx = CTransaction ()
1746
+ p2wsh_tx .vin .append (CTxIn (COutPoint (txid ,i * 2 )))
1747
+ p2wsh_tx .vout .append (CTxOut (outputvalue - 5000 , CScript ([OP_0 , hash160 (hex_str_to_bytes ("" ))])))
1748
+ p2wsh_tx .wit .vtxinwit .append (CTxInWitness ())
1749
+ p2wsh_tx .rehash ()
1750
+ p2wsh_txs .append (p2wsh_tx )
1751
+ p2sh_tx = CTransaction ()
1752
+ p2sh_tx .vin .append (CTxIn (COutPoint (txid ,i * 2 + 1 ), CScript ([p2wsh_scripts [i ]])))
1753
+ p2sh_tx .vout .append (CTxOut (outputvalue - 5000 , CScript ([OP_0 , hash160 (hex_str_to_bytes ("" ))])))
1754
+ p2sh_tx .wit .vtxinwit .append (CTxInWitness ())
1755
+ p2sh_tx .rehash ()
1756
+ p2sh_txs .append (p2sh_tx )
1757
+
1758
+ # Testing native P2WSH
1759
+ # Witness stack size, excluding witnessScript, over 100 is non-standard
1760
+ p2wsh_txs [0 ].wit .vtxinwit [0 ].scriptWitness .stack = [pad ] * 101 + [scripts [0 ]]
1761
+ self .std_node .test_transaction_acceptance (p2wsh_txs [0 ], True , False , b'bad-witness-nonstandard' )
1762
+ # Non-standard nodes should accept
1763
+ self .test_node .test_transaction_acceptance (p2wsh_txs [0 ], True , True )
1764
+
1765
+ # Stack element size over 80 bytes is non-standard
1766
+ p2wsh_txs [1 ].wit .vtxinwit [0 ].scriptWitness .stack = [pad * 81 ] * 100 + [scripts [1 ]]
1767
+ # It can't be used to blind a node to the transaction
1768
+ self .std_node .announce_tx_and_wait_for_getdata (p2wsh_txs [1 ])
1769
+ self .std_node .test_transaction_acceptance (p2wsh_txs [1 ], True , False , b'bad-witness-nonstandard' )
1770
+ self .std_node .announce_tx_and_wait_for_getdata (p2wsh_txs [1 ])
1771
+ self .std_node .test_transaction_acceptance (p2wsh_txs [1 ], True , False , b'bad-witness-nonstandard' )
1772
+ # Non-standard nodes should accept
1773
+ self .test_node .test_transaction_acceptance (p2wsh_txs [1 ], True , True )
1774
+ # Standard nodes should accept if element size is not over 80 bytes
1775
+ p2wsh_txs [1 ].wit .vtxinwit [0 ].scriptWitness .stack = [pad * 80 ] * 100 + [scripts [1 ]]
1776
+ self .std_node .announce_tx_and_wait_for_getdata (p2wsh_txs [1 ])
1777
+ self .std_node .test_transaction_acceptance (p2wsh_txs [1 ], True , True )
1778
+
1779
+ # witnessScript size at 3600 bytes is standard
1780
+ p2wsh_txs [2 ].wit .vtxinwit [0 ].scriptWitness .stack = [pad , pad , scripts [2 ]]
1781
+ self .test_node .test_transaction_acceptance (p2wsh_txs [2 ], True , True )
1782
+ self .std_node .test_transaction_acceptance (p2wsh_txs [2 ], True , True )
1783
+
1784
+ # witnessScript size at 3601 bytes is non-standard
1785
+ p2wsh_txs [3 ].wit .vtxinwit [0 ].scriptWitness .stack = [pad , pad , pad , scripts [3 ]]
1786
+ self .std_node .test_transaction_acceptance (p2wsh_txs [3 ], True , False , b'bad-witness-nonstandard' )
1787
+ # Non-standard nodes should accept
1788
+ self .test_node .test_transaction_acceptance (p2wsh_txs [3 ], True , True )
1789
+
1790
+ # Repeating the same tests with P2SH-P2WSH
1791
+ p2sh_txs [0 ].wit .vtxinwit [0 ].scriptWitness .stack = [pad ] * 101 + [scripts [0 ]]
1792
+ self .std_node .test_transaction_acceptance (p2sh_txs [0 ], True , False , b'bad-witness-nonstandard' )
1793
+ self .test_node .test_transaction_acceptance (p2sh_txs [0 ], True , True )
1794
+ p2sh_txs [1 ].wit .vtxinwit [0 ].scriptWitness .stack = [pad * 81 ] * 100 + [scripts [1 ]]
1795
+ self .std_node .announce_tx_and_wait_for_getdata (p2sh_txs [1 ])
1796
+ self .std_node .test_transaction_acceptance (p2sh_txs [1 ], True , False , b'bad-witness-nonstandard' )
1797
+ self .std_node .announce_tx_and_wait_for_getdata (p2sh_txs [1 ])
1798
+ self .std_node .test_transaction_acceptance (p2sh_txs [1 ], True , False , b'bad-witness-nonstandard' )
1799
+ self .test_node .test_transaction_acceptance (p2sh_txs [1 ], True , True )
1800
+ p2sh_txs [1 ].wit .vtxinwit [0 ].scriptWitness .stack = [pad * 80 ] * 100 + [scripts [1 ]]
1801
+ self .std_node .announce_tx_and_wait_for_getdata (p2sh_txs [1 ])
1802
+ self .std_node .test_transaction_acceptance (p2sh_txs [1 ], True , True )
1803
+ p2sh_txs [2 ].wit .vtxinwit [0 ].scriptWitness .stack = [pad , pad , scripts [2 ]]
1804
+ self .test_node .test_transaction_acceptance (p2sh_txs [2 ], True , True )
1805
+ self .std_node .test_transaction_acceptance (p2sh_txs [2 ], True , True )
1806
+ p2sh_txs [3 ].wit .vtxinwit [0 ].scriptWitness .stack = [pad , pad , pad , scripts [3 ]]
1807
+ self .std_node .test_transaction_acceptance (p2sh_txs [3 ], True , False , b'bad-witness-nonstandard' )
1808
+ self .test_node .test_transaction_acceptance (p2sh_txs [3 ], True , True )
1809
+
1810
+ self .nodes [0 ].generate (1 ) # Mine and clean up the mempool of non-standard node
1811
+ # Valid but non-standard transactions in a block should be accepted by standard node
1812
+ sync_blocks (self .nodes )
1813
+ assert_equal (len (self .nodes [0 ].getrawmempool ()), 0 )
1814
+ assert_equal (len (self .nodes [1 ].getrawmempool ()), 0 )
1815
+
1816
+ self .utxo .pop (0 )
1817
+
1818
+
1709
1819
def run_test (self ):
1710
1820
# Setup the p2p connections and start up the network thread.
1711
1821
self .test_node = TestNode () # sets NODE_WITNESS|NODE_NETWORK
@@ -1778,6 +1888,7 @@ def run_test(self):
1778
1888
self .test_segwit_versions ()
1779
1889
self .test_premature_coinbase_witness_spend ()
1780
1890
self .test_signature_version_1 ()
1891
+ self .test_non_standard_witness ()
1781
1892
sync_blocks (self .nodes )
1782
1893
if self .test_upgrade :
1783
1894
self .test_upgrade_after_activation (self .nodes [2 ], 2 )
0 commit comments