|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Copyright (c) 2015-2020 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 Taproot soft fork activation for Elements |
| 6 | +
|
| 7 | +Unlike Bitcoin, this (a) does not use Speedy Trial, and (b) does use |
| 8 | +a pair of new versionbits features which allows Taproot to have its |
| 9 | +own signalling period length (128 blocks on regtest) and activation |
| 10 | +threshold (100%). |
| 11 | +
|
| 12 | +The primary purpose of this test is to confirm that this configuration |
| 13 | +works; the actual activation (e.g. how are Taproot-enabled transactions |
| 14 | +treated) is covered by other tests and by manual testing. |
| 15 | +
|
| 16 | +There is an undocumented option `con_taproot_signal_start` which sets |
| 17 | +the block at which signalling starts; otherwise it is set to "always |
| 18 | +on" which means that signalling will not occur. |
| 19 | +""" |
| 20 | + |
| 21 | +from test_framework.test_framework import BitcoinTestFramework |
| 22 | +from test_framework.util import assert_equal |
| 23 | + |
| 24 | +class TaprootActivationTest(BitcoinTestFramework): |
| 25 | + def set_test_params(self): |
| 26 | + self.setup_clean_chain = True |
| 27 | + self.num_nodes = 1 |
| 28 | + |
| 29 | + def skip_test_if_missing_module(self): |
| 30 | + self.skip_if_no_wallet() |
| 31 | + |
| 32 | + def test_activation(self, rpc, activation_height): |
| 33 | + self.log.info("Testing activation at height %d" % activation_height) |
| 34 | + activation_height = 128 * ((activation_height + 127) // 128) |
| 35 | + |
| 36 | + assert_equal(rpc.getblockcount(), 0) |
| 37 | + |
| 38 | + blocks = rpc.generatetoaddress(activation_height - 2, rpc.getnewaddress()) |
| 39 | + assert_equal(rpc.getblockcount(), activation_height - 2) |
| 40 | + |
| 41 | + for n, block in enumerate(blocks): |
| 42 | + decode = rpc.getblockheader(block) |
| 43 | + if n < 143: |
| 44 | + assert_equal (decode["versionHex"], "20000000") |
| 45 | + elif n < 431: |
| 46 | + # TESTDUMMY deployment: 144 blocks active, 144 blocks locked in |
| 47 | + assert_equal (decode["versionHex"], "30000000") |
| 48 | + else: |
| 49 | + assert_equal (decode["versionHex"], "20000000") |
| 50 | + |
| 51 | + assert_equal(rpc.getblockchaininfo()["softforks"]["taproot"]["bip9"]["status"], "defined") |
| 52 | + # The 1023rd block does not signal, but changes the signalling state |
| 53 | + # to "started" from "defined" |
| 54 | + blocks = rpc.generatetoaddress(1, rpc.getnewaddress()) |
| 55 | + assert_equal(rpc.getblockchaininfo()["softforks"]["taproot"]["bip9"]["status"], "started") |
| 56 | + assert_equal(rpc.getblockheader(blocks[0])["versionHex"], "20000000") |
| 57 | + |
| 58 | + blocks = rpc.generatetoaddress(127, rpc.getnewaddress()) |
| 59 | + for n, block in enumerate(blocks): |
| 60 | + decode = rpc.getblockheader(block) |
| 61 | + assert_equal (decode["versionHex"], "20000004") |
| 62 | + assert_equal(rpc.getblockchaininfo()["softforks"]["taproot"]["bip9"]["status"], "started") |
| 63 | + |
| 64 | + # Fail to signal on the 128th block. Since the threshold for Taproot is |
| 65 | + # 100% this will prevent activation. Note that our period is 128, not |
| 66 | + # 144 (the default), as we have overridden the period for Taproot. On |
| 67 | + # the main Liquid chain it is overridden to be one week of signalling. |
| 68 | + block = rpc.getnewblockhex() |
| 69 | + block = block[:1] + "0" + block[2:] # turn off Taproot signal |
| 70 | + rpc.submitblock(block) |
| 71 | + assert_equal(rpc.getblockchaininfo()["softforks"]["taproot"]["bip9"]["status"], "started") |
| 72 | + |
| 73 | + # Run through another 128 blocks, without failing to signal |
| 74 | + blocks = rpc.generatetoaddress(127, rpc.getnewaddress()) |
| 75 | + for n, block in enumerate(blocks): |
| 76 | + decode = rpc.getblockheader(block) |
| 77 | + assert_equal (decode["versionHex"], "20000004") |
| 78 | + assert_equal(rpc.getblockchaininfo()["softforks"]["taproot"]["bip9"]["status"], "started") |
| 79 | + # The 128th block then switches from "started" to "locked_in" |
| 80 | + blocks = rpc.generatetoaddress(1, rpc.getnewaddress()) |
| 81 | + assert_equal(rpc.getblockchaininfo()["softforks"]["taproot"]["bip9"]["status"], "locked_in") |
| 82 | + assert_equal(rpc.getblockheader(blocks[0])["versionHex"], "20000004") |
| 83 | + |
| 84 | + # Run through another 128 blocks, which will go from "locked in" to "active" regardless of signalling |
| 85 | + blocks = rpc.generatetoaddress(127, rpc.getnewaddress()) |
| 86 | + for n, block in enumerate(blocks): |
| 87 | + decode = rpc.getblockheader(block) |
| 88 | + assert_equal (decode["versionHex"], "20000004") |
| 89 | + assert_equal(rpc.getblockchaininfo()["softforks"]["taproot"]["bip9"]["status"], "locked_in") |
| 90 | + block = rpc.getnewblockhex() |
| 91 | + block = block[:1] + "0" + block[2:] # turn off Taproot signal |
| 92 | + rpc.submitblock(block) |
| 93 | + assert_equal(rpc.getblockchaininfo()["softforks"]["taproot"]["bip9"]["status"], "active") |
| 94 | + |
| 95 | + # After the state is "active", signallng stops by default. |
| 96 | + blocks = rpc.generatetoaddress(1, self.nodes[0].getnewaddress()) |
| 97 | + assert_equal(rpc.getblockchaininfo()["softforks"]["taproot"]["bip9"]["status"], "active") |
| 98 | + assert_equal(rpc.getblockheader(blocks[0])["versionHex"], "20000000") |
| 99 | + |
| 100 | + def run_test(self): |
| 101 | + # Test that regtest nodes without -con_taproot_signal_start never signal |
| 102 | + self.log.info("Testing node not configured to activate taproot") |
| 103 | + blocks = self.nodes[0].generatetoaddress(2500, self.nodes[0].getnewaddress()) |
| 104 | + assert_equal(self.nodes[0].getblockcount(), 2500) |
| 105 | + for n, block in enumerate(blocks): |
| 106 | + decode = self.nodes[0].getblockheader(block) |
| 107 | + if n < 143: |
| 108 | + assert_equal (decode["versionHex"], "20000000") |
| 109 | + elif n < 431: |
| 110 | + # TESTDUMMY deployment: 144 blocks active, 144 blocks locked in |
| 111 | + assert_equal (decode["versionHex"], "30000000") |
| 112 | + else: |
| 113 | + assert_equal (decode["versionHex"], "20000000") |
| 114 | + |
| 115 | + # Test activation starting from height 1000 |
| 116 | + self.restart_node(0, ["-con_taproot_signal_start=500"]) |
| 117 | + self.nodes[0].invalidateblock(self.nodes[0].getblockhash(1)) |
| 118 | + self.test_activation(self.nodes[0], 500) |
| 119 | + |
| 120 | +if __name__ == '__main__': |
| 121 | + TaprootActivationTest().main() |
0 commit comments