|
| 1 | +#!/usr/bin/env python2 |
| 2 | +# Copyright (c) 2014 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 | + |
| 6 | +# |
| 7 | +# Test the SegWit changeover logic |
| 8 | +# |
| 9 | + |
| 10 | +from test_framework.test_framework import BitcoinTestFramework |
| 11 | +from test_framework.util import * |
| 12 | +import os |
| 13 | +import shutil |
| 14 | +import hashlib |
| 15 | +from binascii import hexlify |
| 16 | + |
| 17 | +NODE_0 = 0 |
| 18 | +NODE_1 = 1 |
| 19 | +NODE_2 = 2 |
| 20 | +WIT_V0 = 0 |
| 21 | +WIT_V1 = 1 |
| 22 | + |
| 23 | +def sha256(s): |
| 24 | + return hashlib.new('sha256', s).digest() |
| 25 | + |
| 26 | +def ripemd160(s): |
| 27 | + return hashlib.new('ripemd160', s).digest() |
| 28 | + |
| 29 | +def witness_script(version, pubkey): |
| 30 | + if (version == 0): |
| 31 | + pubkeyhash = hexlify(ripemd160(sha256(pubkey.decode("hex")))) |
| 32 | + pkscript = "001976a914" + pubkeyhash + "88ac" |
| 33 | + elif (version == 1): |
| 34 | + witnessprogram = "21"+pubkey+"ac" |
| 35 | + hashwitnessprogram = hexlify(sha256(witnessprogram.decode("hex"))) |
| 36 | + pkscript = "5120" + hashwitnessprogram |
| 37 | + else: |
| 38 | + assert("Wrong version" == "0 or 1") |
| 39 | + return pkscript |
| 40 | + |
| 41 | +def addlength(script): |
| 42 | + scriptlen = format(len(script)/2, 'x') |
| 43 | + assert(len(scriptlen) == 2) |
| 44 | + return scriptlen + script |
| 45 | + |
| 46 | +def create_witnessprogram(version, node, utxo, pubkey, encode_p2sh, amount): |
| 47 | + pkscript = witness_script(version, pubkey); |
| 48 | + if (encode_p2sh): |
| 49 | + p2sh_hash = hexlify(ripemd160(sha256(pkscript.decode("hex")))) |
| 50 | + pkscript = "a914"+p2sh_hash+"87" |
| 51 | + inputs = [] |
| 52 | + outputs = {} |
| 53 | + inputs.append({ "txid" : utxo["txid"], "vout" : utxo["vout"]} ) |
| 54 | + DUMMY_P2SH = "2MySexEGVzZpRgNQ1JdjdP5bRETznm3roQ2" # P2SH of "OP_1 OP_DROP" |
| 55 | + outputs[DUMMY_P2SH] = amount |
| 56 | + tx_to_witness = node.createrawtransaction(inputs,outputs) |
| 57 | + #replace dummy output with our own |
| 58 | + tx_to_witness = tx_to_witness[0:110] + addlength(pkscript) + tx_to_witness[-8:] |
| 59 | + return tx_to_witness |
| 60 | + |
| 61 | +def send_to_witness(version, node, utxo, pubkey, encode_p2sh, amount, sign=True, insert_redeem_script=""): |
| 62 | + tx_to_witness = create_witnessprogram(version, node, utxo, pubkey, encode_p2sh, amount) |
| 63 | + if (sign): |
| 64 | + signed = node.signrawtransaction(tx_to_witness) |
| 65 | + return node.sendrawtransaction(signed["hex"]) |
| 66 | + else: |
| 67 | + if (insert_redeem_script): |
| 68 | + tx_to_witness = tx_to_witness[0:82] + addlength(insert_redeem_script) + tx_to_witness[84:] |
| 69 | + |
| 70 | + return node.sendrawtransaction(tx_to_witness) |
| 71 | + |
| 72 | +def getutxo(txid): |
| 73 | + utxo = {} |
| 74 | + utxo["vout"] = 0 |
| 75 | + utxo["txid"] = txid |
| 76 | + return utxo |
| 77 | + |
| 78 | +class SegWitTest(BitcoinTestFramework): |
| 79 | + |
| 80 | + def setup_chain(self): |
| 81 | + print("Initializing test directory "+self.options.tmpdir) |
| 82 | + initialize_chain_clean(self.options.tmpdir, 3) |
| 83 | + |
| 84 | + def setup_network(self): |
| 85 | + self.nodes = [] |
| 86 | + self.nodes.append(start_node(0, self.options.tmpdir, ["-logtimemicros", "-debug"])) |
| 87 | + self.nodes.append(start_node(1, self.options.tmpdir, ["-logtimemicros", "-debug", "-blockversion=4", "-promiscuousmempoolflags=517", "-prematurewitness"])) |
| 88 | + self.nodes.append(start_node(2, self.options.tmpdir, ["-logtimemicros", "-debug", "-blockversion=5", "-promiscuousmempoolflags=517", "-prematurewitness"])) |
| 89 | + connect_nodes(self.nodes[1], 0) |
| 90 | + connect_nodes(self.nodes[2], 1) |
| 91 | + connect_nodes(self.nodes[0], 2) |
| 92 | + self.is_network_split = False |
| 93 | + self.sync_all() |
| 94 | + |
| 95 | + def success_mine(self, node, txid, sign, redeem_script=""): |
| 96 | + send_to_witness(1, node, getutxo(txid), self.pubkey[0], False, Decimal("49.998"), sign, redeem_script) |
| 97 | + block = node.generate(1) |
| 98 | + assert_equal(len(node.getblock(block[0])["tx"]), 2) |
| 99 | + sync_blocks(self.nodes) |
| 100 | + |
| 101 | + def skip_mine(self, node, txid, sign, redeem_script=""): |
| 102 | + send_to_witness(1, node, getutxo(txid), self.pubkey[0], False, Decimal("49.998"), sign, redeem_script) |
| 103 | + block = node.generate(1) |
| 104 | + assert_equal(len(node.getblock(block[0])["tx"]), 1) |
| 105 | + sync_blocks(self.nodes) |
| 106 | + |
| 107 | + def fail_accept(self, node, txid, sign, redeem_script=""): |
| 108 | + try: |
| 109 | + send_to_witness(1, node, getutxo(txid), self.pubkey[0], False, Decimal("49.998"), sign, redeem_script) |
| 110 | + except JSONRPCException as exp: |
| 111 | + assert(exp.error["code"] == -26) |
| 112 | + else: |
| 113 | + raise AssertionError("Tx should not have been accepted") |
| 114 | + |
| 115 | + def fail_mine(self, node, txid, sign, redeem_script=""): |
| 116 | + send_to_witness(1, node, getutxo(txid), self.pubkey[0], False, Decimal("49.998"), sign, redeem_script) |
| 117 | + try: |
| 118 | + node.generate(1) |
| 119 | + except JSONRPCException as exp: |
| 120 | + assert(exp.error["code"] == -1) |
| 121 | + else: |
| 122 | + raise AssertionError("Created valid block when TestBlockValidity should have failed") |
| 123 | + sync_blocks(self.nodes) |
| 124 | + |
| 125 | + def run_test(self): |
| 126 | + self.nodes[0].generate(160) #block 160 |
| 127 | + |
| 128 | + self.pubkey = [] |
| 129 | + p2sh_ids = [] # p2sh_ids[NODE][VER] is an array of txids that spend to a witness version VER pkscript to an address for NODE embedded in p2sh |
| 130 | + wit_ids = [] # wit_ids[NODE][VER] is an array of txids that spend to a witness version VER pkscript to an address for NODE via bare witness |
| 131 | + for i in xrange(3): |
| 132 | + newaddress = self.nodes[i].getnewaddress() |
| 133 | + self.nodes[i].addwitnessaddress(newaddress) |
| 134 | + self.pubkey.append(self.nodes[i].validateaddress(newaddress)["pubkey"]) |
| 135 | + p2sh_ids.append([]) |
| 136 | + wit_ids.append([]) |
| 137 | + for v in xrange(2): |
| 138 | + p2sh_ids[i].append([]) |
| 139 | + wit_ids[i].append([]) |
| 140 | + |
| 141 | + for i in xrange(5): |
| 142 | + for n in xrange(3): |
| 143 | + for v in xrange(2): |
| 144 | + wit_ids[n][v].append(send_to_witness(v, self.nodes[0], self.nodes[0].listunspent()[0], self.pubkey[n], False, Decimal("49.999"))) |
| 145 | + p2sh_ids[n][v].append(send_to_witness(v, self.nodes[0], self.nodes[0].listunspent()[0], self.pubkey[n], True, Decimal("49.999"))) |
| 146 | + |
| 147 | + self.nodes[0].generate(1) #block 161 |
| 148 | + sync_blocks(self.nodes) |
| 149 | + |
| 150 | + # Make sure all nodes recognize the transactions as theirs |
| 151 | + assert_equal(self.nodes[0].getbalance(), 60*50 - 60*50 + 20*Decimal("49.999") + 50) |
| 152 | + assert_equal(self.nodes[1].getbalance(), 20*Decimal("49.999")) |
| 153 | + assert_equal(self.nodes[2].getbalance(), 20*Decimal("49.999")) |
| 154 | + |
| 155 | + self.nodes[0].generate(581) #block 742 |
| 156 | + sync_blocks(self.nodes) |
| 157 | + |
| 158 | + print "Verify default node can't accept any witness format txs before fork" |
| 159 | + # unsigned, no scriptsig |
| 160 | + self.fail_accept(self.nodes[0], wit_ids[NODE_0][WIT_V0][0], False) |
| 161 | + self.fail_accept(self.nodes[0], wit_ids[NODE_0][WIT_V1][0], False) |
| 162 | + self.fail_accept(self.nodes[0], p2sh_ids[NODE_0][WIT_V0][0], False) |
| 163 | + self.fail_accept(self.nodes[0], p2sh_ids[NODE_0][WIT_V1][0], False) |
| 164 | + # unsigned with redeem script |
| 165 | + self.fail_accept(self.nodes[0], p2sh_ids[NODE_0][WIT_V0][0], False, addlength(witness_script(0, self.pubkey[0]))) |
| 166 | + self.fail_accept(self.nodes[0], p2sh_ids[NODE_0][WIT_V1][0], False, addlength(witness_script(1, self.pubkey[0]))) |
| 167 | + # signed |
| 168 | + self.fail_accept(self.nodes[0], wit_ids[NODE_0][WIT_V0][0], True) |
| 169 | + self.fail_accept(self.nodes[0], wit_ids[NODE_0][WIT_V1][0], True) |
| 170 | + self.fail_accept(self.nodes[0], p2sh_ids[NODE_0][WIT_V0][0], True) |
| 171 | + self.fail_accept(self.nodes[0], p2sh_ids[NODE_0][WIT_V1][0], True) |
| 172 | + |
| 173 | + print "Verify witness txs are skipped for mining before the fork" |
| 174 | + self.skip_mine(self.nodes[2], wit_ids[NODE_2][WIT_V0][0], True) #block 743 |
| 175 | + self.skip_mine(self.nodes[2], wit_ids[NODE_2][WIT_V1][0], True) #block 744 |
| 176 | + self.skip_mine(self.nodes[2], p2sh_ids[NODE_2][WIT_V0][0], True) #block 745 |
| 177 | + self.skip_mine(self.nodes[2], p2sh_ids[NODE_2][WIT_V1][0], True) #block 746 |
| 178 | + |
| 179 | + # TODO: An old node would see these txs without witnesses and be able to mine them |
| 180 | + |
| 181 | + print "Verify unsigned bare witness txs in version 5 blocks are valid before the fork" |
| 182 | + self.success_mine(self.nodes[2], wit_ids[NODE_2][WIT_V0][1], False) #block 747 |
| 183 | + self.success_mine(self.nodes[2], wit_ids[NODE_2][WIT_V1][1], False) #block 748 |
| 184 | + |
| 185 | + print "Verify unsigned p2sh witness txs without a redeem script are invalid" |
| 186 | + self.fail_accept(self.nodes[2], p2sh_ids[NODE_2][WIT_V0][1], False) |
| 187 | + self.fail_accept(self.nodes[2], p2sh_ids[NODE_2][WIT_V1][1], False) |
| 188 | + |
| 189 | + print "Verify unsigned p2sh witness txs with a redeem script in version 5 blocks are valid before the fork" |
| 190 | + self.success_mine(self.nodes[2], p2sh_ids[NODE_2][WIT_V0][1], False, addlength(witness_script(0, self.pubkey[2]))) #block 749 |
| 191 | + self.success_mine(self.nodes[2], p2sh_ids[NODE_2][WIT_V1][1], False, addlength(witness_script(1, self.pubkey[2]))) #block 750 |
| 192 | + |
| 193 | + print "Verify previous witness txs skipped for mining can now be mined" |
| 194 | + assert_equal(len(self.nodes[2].getrawmempool()), 4) |
| 195 | + block = self.nodes[2].generate(1) #block 751 |
| 196 | + sync_blocks(self.nodes) |
| 197 | + assert_equal(len(self.nodes[2].getrawmempool()), 0) |
| 198 | + assert_equal(len(self.nodes[2].getblock(block[0])["tx"]), 5) |
| 199 | + |
| 200 | + print "Verify witness txs without witness data in version 5 blocks are invalid after the fork" |
| 201 | + self.fail_mine(self.nodes[2], wit_ids[NODE_2][WIT_V0][2], False) |
| 202 | + self.fail_mine(self.nodes[2], wit_ids[NODE_2][WIT_V1][2], False) |
| 203 | + self.fail_mine(self.nodes[2], p2sh_ids[NODE_2][WIT_V0][2], False, addlength(witness_script(0, self.pubkey[2]))) |
| 204 | + self.fail_mine(self.nodes[2], p2sh_ids[NODE_2][WIT_V1][2], False, addlength(witness_script(1, self.pubkey[2]))) |
| 205 | + |
| 206 | + |
| 207 | + print "Verify that a version 4 block can still mine those unsigned txs" |
| 208 | + assert_equal(len(self.nodes[2].getrawmempool()), 4) |
| 209 | + sync_mempools(self.nodes[1:3]) |
| 210 | + block = self.nodes[1].generate(1) #block 752 |
| 211 | + sync_blocks(self.nodes) |
| 212 | + assert_equal(len(self.nodes[2].getrawmempool()), 0) |
| 213 | + assert_equal(len(self.nodes[1].getblock(block[0])["tx"]), 5) |
| 214 | + |
| 215 | + print "Verify all types of witness txs can be submitted signed after the fork to node with -prematurewitness" |
| 216 | + self.success_mine(self.nodes[2], wit_ids[NODE_2][WIT_V0][3], True) #block 753 |
| 217 | + self.success_mine(self.nodes[2], wit_ids[NODE_2][WIT_V1][3], True) #block 754 |
| 218 | + self.success_mine(self.nodes[2], p2sh_ids[NODE_2][WIT_V0][3], True) #block 755 |
| 219 | + self.success_mine(self.nodes[2], p2sh_ids[NODE_2][WIT_V1][3], True) #block 756 |
| 220 | + |
| 221 | + print "Verify default node can't accept any witness format txs between enforce and reject points of fork" |
| 222 | + # unsigned, no scriptsig |
| 223 | + self.fail_accept(self.nodes[0], wit_ids[NODE_0][WIT_V0][0], False) |
| 224 | + self.fail_accept(self.nodes[0], wit_ids[NODE_0][WIT_V1][0], False) |
| 225 | + self.fail_accept(self.nodes[0], p2sh_ids[NODE_0][WIT_V0][0], False) |
| 226 | + self.fail_accept(self.nodes[0], p2sh_ids[NODE_0][WIT_V1][0], False) |
| 227 | + # unsigned with redeem script |
| 228 | + self.fail_accept(self.nodes[0], p2sh_ids[NODE_0][WIT_V0][0], False, addlength(witness_script(0, self.pubkey[0]))) |
| 229 | + self.fail_accept(self.nodes[0], p2sh_ids[NODE_0][WIT_V1][0], False, addlength(witness_script(1, self.pubkey[0]))) |
| 230 | + # signed |
| 231 | + self.fail_accept(self.nodes[0], wit_ids[NODE_0][WIT_V0][0], True) |
| 232 | + self.fail_accept(self.nodes[0], wit_ids[NODE_0][WIT_V1][0], True) |
| 233 | + self.fail_accept(self.nodes[0], p2sh_ids[NODE_0][WIT_V0][0], True) |
| 234 | + self.fail_accept(self.nodes[0], p2sh_ids[NODE_0][WIT_V1][0], True) |
| 235 | + |
| 236 | + # TODO: verify witness txs are invalid if in a v4 block |
| 237 | + print "Verify witness txs aren't mined in a v4 block" |
| 238 | + self.skip_mine(self.nodes[1], wit_ids[NODE_1][WIT_V0][0], True) #block 757 |
| 239 | + self.skip_mine(self.nodes[1], wit_ids[NODE_1][WIT_V1][0], True) #block 758 |
| 240 | + self.skip_mine(self.nodes[1], p2sh_ids[NODE_1][WIT_V0][0], True) #block 759 |
| 241 | + self.skip_mine(self.nodes[1], p2sh_ids[NODE_1][WIT_V1][0], True) #block 760 |
| 242 | + |
| 243 | + # Mine them from ver 5 node |
| 244 | + sync_mempools(self.nodes[1:3]) |
| 245 | + assert_equal(len(self.nodes[2].getrawmempool()), 4) |
| 246 | + block = self.nodes[2].generate(1) #block 761 |
| 247 | + sync_blocks(self.nodes) |
| 248 | + assert_equal(len(self.nodes[2].getrawmempool()), 0) |
| 249 | + assert_equal(len(self.nodes[2].getblock(block[0])["tx"]), 5) |
| 250 | + |
| 251 | + self.nodes[0].generate(195) #block 956 (5 of which are v4 blocks) |
| 252 | + sync_blocks(self.nodes) |
| 253 | + |
| 254 | + print "Verify that version 4 blocks are invalid period after reject point" |
| 255 | + try: |
| 256 | + self.nodes[1].generate(1) |
| 257 | + except JSONRPCException as exp: |
| 258 | + assert(exp.error["code"] == -1) |
| 259 | + else: |
| 260 | + raise AssertionError("Created valid block when TestBlockValidity should have failed") |
| 261 | + |
| 262 | + print "Verify default node can now use witness txs" |
| 263 | + self.success_mine(self.nodes[0], wit_ids[NODE_0][WIT_V0][0], True) #block 957 |
| 264 | + self.success_mine(self.nodes[0], wit_ids[NODE_0][WIT_V1][0], True) #block 958 |
| 265 | + self.success_mine(self.nodes[0], p2sh_ids[NODE_0][WIT_V0][0], True) #block 959 |
| 266 | + self.success_mine(self.nodes[0], p2sh_ids[NODE_0][WIT_V1][0], True) #block 960 |
| 267 | + |
| 268 | +if __name__ == '__main__': |
| 269 | + SegWitTest().main() |
0 commit comments