feature_segwit.py
1 #!/usr/bin/env python3 2 # Copyright (c) 2016-present The Bitcoin Core developers 3 # Distributed under the MIT software license, see the accompanying 4 # file COPYING or http://www.opensource.org/licenses/mit-license.php. 5 """Test the SegWit changeover logic.""" 6 7 from decimal import Decimal 8 9 from test_framework.address import ( 10 script_to_p2sh_p2wsh, 11 script_to_p2wsh, 12 ) 13 from test_framework.blocktools import ( 14 send_to_witness, 15 witness_script, 16 ) 17 from test_framework.descriptors import descsum_create 18 from test_framework.messages import ( 19 COIN, 20 COutPoint, 21 CTransaction, 22 CTxIn, 23 CTxOut, 24 tx_from_hex, 25 ) 26 from test_framework.script import ( 27 CScript, 28 OP_DROP, 29 OP_TRUE, 30 ) 31 from test_framework.script_util import ( 32 keys_to_multisig_script, 33 ) 34 from test_framework.test_framework import BitcoinTestFramework 35 from test_framework.util import ( 36 assert_equal, 37 assert_greater_than_or_equal, 38 assert_is_hex_string, 39 assert_raises_rpc_error, 40 ) 41 from test_framework.wallet_util import ( 42 get_generate_key, 43 ) 44 45 NODE_0 = 0 46 NODE_2 = 2 47 P2WPKH = 0 48 P2WSH = 1 49 50 51 def getutxo(txid): 52 utxo = {} 53 utxo["vout"] = 0 54 utxo["txid"] = txid 55 return utxo 56 57 58 def find_spendable_utxo(node, min_value): 59 for utxo in node.listunspent(query_options={'minimumAmount': min_value}): 60 if utxo['spendable']: 61 return utxo 62 63 raise AssertionError(f"Unspent output equal or higher than {min_value} not found") 64 65 66 class SegWitTest(BitcoinTestFramework): 67 def set_test_params(self): 68 self.setup_clean_chain = True 69 self.num_nodes = 3 70 # This test tests SegWit both pre and post-activation, so use the normal BIP9 activation. 71 self.extra_args = [ 72 [ 73 "-acceptnonstdtxn=1", 74 "-testactivationheight=segwit@165", 75 "-addresstype=legacy", 76 ], 77 [ 78 "-acceptnonstdtxn=1", 79 "-testactivationheight=segwit@165", 80 "-addresstype=legacy", 81 ], 82 [ 83 "-acceptnonstdtxn=1", 84 "-testactivationheight=segwit@165", 85 "-addresstype=legacy", 86 ], 87 ] 88 self.rpc_timeout = 120 89 90 def skip_test_if_missing_module(self): 91 self.skip_if_no_wallet() 92 93 def setup_network(self): 94 super().setup_network() 95 self.connect_nodes(0, 2) 96 self.sync_all() 97 98 def success_mine(self, node, txid, sign, redeem_script=""): 99 send_to_witness(1, node, getutxo(txid), self.pubkey[0], False, Decimal("49.998"), sign, redeem_script) 100 block = self.generate(node, 1) 101 assert_equal(len(node.getblock(block[0])["tx"]), 2) 102 self.sync_blocks() 103 104 def fail_accept(self, node, error_msg, txid, sign, redeem_script=""): 105 assert_raises_rpc_error(-26, error_msg, send_to_witness, use_p2wsh=1, node=node, utxo=getutxo(txid), pubkey=self.pubkey[0], encode_p2sh=False, amount=Decimal("49.998"), sign=sign, insert_redeem_script=redeem_script) 106 107 def run_test(self): 108 self.generate(self.nodes[0], 161) # block 161 109 110 self.log.info("Verify sigops are counted in GBT with pre-BIP141 rules before the fork") 111 txid = self.nodes[0].sendtoaddress(self.nodes[0].getnewaddress(), 1) 112 tmpl = self.nodes[0].getblocktemplate({'rules': ['segwit']}) 113 assert_equal(tmpl['sizelimit'], 1000000) 114 assert 'weightlimit' not in tmpl 115 assert_equal(tmpl['sigoplimit'], 20000) 116 assert_equal(tmpl['transactions'][0]['hash'], txid) 117 assert_equal(tmpl['transactions'][0]['sigops'], 2) 118 assert '!segwit' not in tmpl['rules'] 119 self.generate(self.nodes[0], 1) # block 162 120 121 balance_presetup = self.nodes[0].getbalance() 122 self.pubkey = [] 123 p2sh_ids = [] # p2sh_ids[NODE][TYPE] is an array of txids that spend to P2WPKH (TYPE=0) or P2WSH (TYPE=1) scripts to an address for NODE embedded in p2sh 124 wit_ids = [] # wit_ids[NODE][TYPE] is an array of txids that spend to P2WPKH (TYPE=0) or P2WSH (TYPE=1) scripts to an address for NODE via bare witness 125 for i in range(3): 126 key = get_generate_key() 127 self.pubkey.append(key.pubkey) 128 129 multiscript = keys_to_multisig_script([self.pubkey[-1]]) 130 p2sh_ms_addr = self.nodes[i].createmultisig(1, [self.pubkey[-1]], 'p2sh-segwit')['address'] 131 bip173_ms_addr = self.nodes[i].createmultisig(1, [self.pubkey[-1]], 'bech32')['address'] 132 assert_equal(p2sh_ms_addr, script_to_p2sh_p2wsh(multiscript)) 133 assert_equal(bip173_ms_addr, script_to_p2wsh(multiscript)) 134 135 p2sh_ms_desc = descsum_create(f"sh(wsh(multi(1,{key.privkey})))") 136 bip173_ms_desc = descsum_create(f"wsh(multi(1,{key.privkey}))") 137 assert_equal(self.nodes[i].deriveaddresses(p2sh_ms_desc)[0], p2sh_ms_addr) 138 assert_equal(self.nodes[i].deriveaddresses(bip173_ms_desc)[0], bip173_ms_addr) 139 140 sh_wpkh_desc = descsum_create(f"sh(wpkh({key.privkey}))") 141 wpkh_desc = descsum_create(f"wpkh({key.privkey})") 142 assert_equal(self.nodes[i].deriveaddresses(sh_wpkh_desc)[0], key.p2sh_p2wpkh_addr) 143 assert_equal(self.nodes[i].deriveaddresses(wpkh_desc)[0], key.p2wpkh_addr) 144 145 res = self.nodes[i].importdescriptors([ 146 {"desc": p2sh_ms_desc, "timestamp": "now"}, 147 {"desc": bip173_ms_desc, "timestamp": "now"}, 148 {"desc": sh_wpkh_desc, "timestamp": "now"}, 149 {"desc": wpkh_desc, "timestamp": "now"}, 150 ]) 151 assert all([r["success"] for r in res]) 152 153 p2sh_ids.append([]) 154 wit_ids.append([]) 155 for _ in range(2): 156 p2sh_ids[i].append([]) 157 wit_ids[i].append([]) 158 159 for _ in range(5): 160 for n in range(3): 161 for v in range(2): 162 wit_ids[n][v].append(send_to_witness(v, self.nodes[0], find_spendable_utxo(self.nodes[0], 50), self.pubkey[n], False, Decimal("49.999"))) 163 p2sh_ids[n][v].append(send_to_witness(v, self.nodes[0], find_spendable_utxo(self.nodes[0], 50), self.pubkey[n], True, Decimal("49.999"))) 164 165 self.generate(self.nodes[0], 1) # block 163 166 167 # Make sure all nodes recognize the transactions as theirs 168 assert_equal(self.nodes[0].getbalance(), balance_presetup - 60 * 50 + 20 * Decimal("49.999") + 50) 169 assert_equal(self.nodes[1].getbalance(), 20 * Decimal("49.999")) 170 assert_equal(self.nodes[2].getbalance(), 20 * Decimal("49.999")) 171 172 self.log.info("Verify unsigned p2sh witness txs without a redeem script are invalid") 173 self.fail_accept(self.nodes[2], "mempool-script-verify-flag-failed (Operation not valid with the current stack size)", p2sh_ids[NODE_2][P2WPKH][1], sign=False) 174 self.fail_accept(self.nodes[2], "mempool-script-verify-flag-failed (Operation not valid with the current stack size)", p2sh_ids[NODE_2][P2WSH][1], sign=False) 175 176 self.generate(self.nodes[0], 1) # block 164 177 178 self.log.info("Verify witness txs are mined as soon as segwit activates") 179 180 send_to_witness(1, self.nodes[2], getutxo(wit_ids[NODE_2][P2WPKH][0]), self.pubkey[0], encode_p2sh=False, amount=Decimal("49.998"), sign=True) 181 send_to_witness(1, self.nodes[2], getutxo(wit_ids[NODE_2][P2WSH][0]), self.pubkey[0], encode_p2sh=False, amount=Decimal("49.998"), sign=True) 182 send_to_witness(1, self.nodes[2], getutxo(p2sh_ids[NODE_2][P2WPKH][0]), self.pubkey[0], encode_p2sh=False, amount=Decimal("49.998"), sign=True) 183 send_to_witness(1, self.nodes[2], getutxo(p2sh_ids[NODE_2][P2WSH][0]), self.pubkey[0], encode_p2sh=False, amount=Decimal("49.998"), sign=True) 184 185 assert_equal(len(self.nodes[2].getrawmempool()), 4) 186 blockhash = self.generate(self.nodes[2], 1)[0] # block 165 (first block with new rules) 187 assert_equal(len(self.nodes[2].getrawmempool()), 0) 188 segwit_tx_list = self.nodes[2].getblock(blockhash)["tx"] 189 assert_equal(len(segwit_tx_list), 5) 190 191 self.log.info("Verify default node can't accept txs with missing witness") 192 # unsigned, no scriptsig 193 self.fail_accept(self.nodes[0], "mempool-script-verify-flag-failed (Witness program hash mismatch)", wit_ids[NODE_0][P2WPKH][0], sign=False) 194 self.fail_accept(self.nodes[0], "mempool-script-verify-flag-failed (Witness program was passed an empty witness)", wit_ids[NODE_0][P2WSH][0], sign=False) 195 self.fail_accept(self.nodes[0], "mempool-script-verify-flag-failed (Operation not valid with the current stack size)", p2sh_ids[NODE_0][P2WPKH][0], sign=False) 196 self.fail_accept(self.nodes[0], "mempool-script-verify-flag-failed (Operation not valid with the current stack size)", p2sh_ids[NODE_0][P2WSH][0], sign=False) 197 # unsigned with redeem script 198 self.fail_accept(self.nodes[0], "mempool-script-verify-flag-failed (Witness program hash mismatch)", p2sh_ids[NODE_0][P2WPKH][0], sign=False, redeem_script=witness_script(False, self.pubkey[0])) 199 self.fail_accept(self.nodes[0], "mempool-script-verify-flag-failed (Witness program was passed an empty witness)", p2sh_ids[NODE_0][P2WSH][0], sign=False, redeem_script=witness_script(True, self.pubkey[0])) 200 201 # Coinbase contains the witness commitment nonce, check that RPC shows us 202 coinbase_txid = self.nodes[2].getblock(blockhash)['tx'][0] 203 coinbase_tx = self.nodes[2].gettransaction(txid=coinbase_txid, verbose=True) 204 witnesses = coinbase_tx["decoded"]["vin"][0]["txinwitness"] 205 assert_equal(len(witnesses), 1) 206 assert_is_hex_string(witnesses[0]) 207 assert_equal(witnesses[0], '00' * 32) 208 209 self.log.info("Verify witness txs without witness data are invalid after the fork") 210 self.fail_accept(self.nodes[2], 'mempool-script-verify-flag-failed (Witness program hash mismatch)', wit_ids[NODE_2][P2WPKH][2], sign=False) 211 self.fail_accept(self.nodes[2], 'mempool-script-verify-flag-failed (Witness program was passed an empty witness)', wit_ids[NODE_2][P2WSH][2], sign=False) 212 self.fail_accept(self.nodes[2], 'mempool-script-verify-flag-failed (Witness program hash mismatch)', p2sh_ids[NODE_2][P2WPKH][2], sign=False, redeem_script=witness_script(False, self.pubkey[2])) 213 self.fail_accept(self.nodes[2], 'mempool-script-verify-flag-failed (Witness program was passed an empty witness)', p2sh_ids[NODE_2][P2WSH][2], sign=False, redeem_script=witness_script(True, self.pubkey[2])) 214 215 self.log.info("Verify default node can now use witness txs") 216 self.success_mine(self.nodes[0], wit_ids[NODE_0][P2WPKH][0], True) 217 self.success_mine(self.nodes[0], wit_ids[NODE_0][P2WSH][0], True) 218 self.success_mine(self.nodes[0], p2sh_ids[NODE_0][P2WPKH][0], True) 219 self.success_mine(self.nodes[0], p2sh_ids[NODE_0][P2WSH][0], True) 220 221 self.log.info("Verify sigops are counted in GBT with BIP141 rules after the fork") 222 txid = self.nodes[0].sendtoaddress(self.nodes[0].getnewaddress(), 1) 223 raw_tx = self.nodes[0].getrawtransaction(txid, True) 224 tmpl = self.nodes[0].getblocktemplate({'rules': ['segwit']}) 225 assert_greater_than_or_equal(tmpl['sizelimit'], 3999577) # actual maximum size is lower due to minimum mandatory non-witness data 226 assert_equal(tmpl['weightlimit'], 4000000) 227 assert_equal(tmpl['sigoplimit'], 80000) 228 assert_equal(tmpl['transactions'][0]['txid'], txid) 229 expected_sigops = 9 if 'txinwitness' in raw_tx["vin"][0] else 8 230 assert_equal(tmpl['transactions'][0]['sigops'], expected_sigops) 231 assert '!segwit' in tmpl['rules'] 232 233 self.generate(self.nodes[0], 1) # Mine a block to clear the gbt cache 234 235 self.log.info("Non-segwit miners are able to use GBT response after activation.") 236 # Create a 3-tx chain: tx1 (non-segwit input, paying to a segwit output) -> 237 # tx2 (segwit input, paying to a non-segwit output) -> 238 # tx3 (non-segwit input, paying to a non-segwit output). 239 # tx1 is allowed to appear in the block, but no others. 240 txid1 = send_to_witness(1, self.nodes[0], find_spendable_utxo(self.nodes[0], 50), self.pubkey[0], False, Decimal("49.996")) 241 assert txid1 in self.nodes[0].getrawmempool() 242 243 tx1_hex = self.nodes[0].gettransaction(txid1)['hex'] 244 tx1 = tx_from_hex(tx1_hex) 245 246 # Check that wtxid is properly reported in mempool entry (txid1) 247 assert_equal(self.nodes[0].getmempoolentry(txid1)["wtxid"], tx1.wtxid_hex) 248 249 # Check that weight and vsize are properly reported in mempool entry (txid1) 250 assert_equal(self.nodes[0].getmempoolentry(txid1)["vsize"], tx1.get_vsize()) 251 assert_equal(self.nodes[0].getmempoolentry(txid1)["weight"], tx1.get_weight()) 252 253 # Now create tx2, which will spend from txid1. 254 tx = CTransaction() 255 tx.vin.append(CTxIn(COutPoint(int(txid1, 16), 0), b'')) 256 tx.vout.append(CTxOut(int(49.99 * COIN), CScript([OP_TRUE, OP_DROP] * 15 + [OP_TRUE]))) 257 tx2_hex = self.nodes[0].signrawtransactionwithwallet(tx.serialize().hex())['hex'] 258 txid2 = self.nodes[0].sendrawtransaction(tx2_hex) 259 tx = tx_from_hex(tx2_hex) 260 assert not tx.wit.is_null() 261 262 # Check that wtxid is properly reported in mempool entry (txid2) 263 assert_equal(self.nodes[0].getmempoolentry(txid2)["wtxid"], tx.wtxid_hex) 264 265 # Check that weight and vsize are properly reported in mempool entry (txid2) 266 assert_equal(self.nodes[0].getmempoolentry(txid2)["vsize"], tx.get_vsize()) 267 assert_equal(self.nodes[0].getmempoolentry(txid2)["weight"], tx.get_weight()) 268 269 # Now create tx3, which will spend from txid2 270 tx = CTransaction() 271 tx.vin.append(CTxIn(COutPoint(int(txid2, 16), 0), b"")) 272 tx.vout.append(CTxOut(int(49.95 * COIN), CScript([OP_TRUE, OP_DROP] * 15 + [OP_TRUE]))) # Huge fee 273 txid3 = self.nodes[0].sendrawtransaction(hexstring=tx.serialize().hex(), maxfeerate=0) 274 assert tx.wit.is_null() 275 assert txid3 in self.nodes[0].getrawmempool() 276 277 # Check that getblocktemplate includes all transactions. 278 template = self.nodes[0].getblocktemplate({"rules": ["segwit"]}) 279 template_txids = [t['txid'] for t in template['transactions']] 280 assert txid1 in template_txids 281 assert txid2 in template_txids 282 assert txid3 in template_txids 283 284 # Check that wtxid is properly reported in mempool entry (txid3) 285 assert_equal(self.nodes[0].getmempoolentry(txid3)["wtxid"], tx.wtxid_hex) 286 287 # Check that weight and vsize are properly reported in mempool entry (txid3) 288 assert_equal(self.nodes[0].getmempoolentry(txid3)["vsize"], tx.get_vsize()) 289 assert_equal(self.nodes[0].getmempoolentry(txid3)["weight"], tx.get_weight()) 290 291 # Mine a block to clear the gbt cache again. 292 self.generate(self.nodes[0], 1) 293 294 295 if __name__ == '__main__': 296 SegWitTest(__file__).main()