2016-03-19 20:58:06 +01:00
|
|
|
#!/usr/bin/env python3
|
2016-12-31 19:01:21 +01:00
|
|
|
# Copyright (c) 2015-2016 The Bitcoin Core developers
|
2016-03-19 20:58:06 +01:00
|
|
|
# Distributed under the MIT software license, see the accompanying
|
2016-03-16 06:30:04 +01:00
|
|
|
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
2017-01-18 00:34:40 +01:00
|
|
|
"""Test BIP 9 soft forks.
|
2016-03-16 06:30:04 +01:00
|
|
|
|
|
|
|
Connect to a single node.
|
|
|
|
regtest lock-in with 108/144 block signalling
|
|
|
|
activation after a further 144 blocks
|
|
|
|
mine 2 block and save coinbases for later use
|
|
|
|
mine 141 blocks to transition from DEFINED to STARTED
|
|
|
|
mine 100 blocks signalling readiness and 44 not in order to fail to change state this period
|
|
|
|
mine 108 blocks signalling readiness and 36 blocks not signalling readiness (STARTED->LOCKED_IN)
|
|
|
|
mine a further 143 blocks (LOCKED_IN)
|
|
|
|
test that enforcement has not triggered (which triggers ACTIVE)
|
|
|
|
test that enforcement has triggered
|
2017-01-18 00:34:40 +01:00
|
|
|
"""
|
2017-06-07 17:54:39 +02:00
|
|
|
from io import BytesIO
|
|
|
|
import shutil
|
|
|
|
import time
|
|
|
|
import itertools
|
2016-03-16 06:30:04 +01:00
|
|
|
|
2017-01-18 00:34:40 +01:00
|
|
|
from test_framework.test_framework import ComparisonTestFramework
|
|
|
|
from test_framework.util import *
|
|
|
|
from test_framework.mininode import CTransaction, NetworkThread
|
|
|
|
from test_framework.blocktools import create_coinbase, create_block
|
|
|
|
from test_framework.comptool import TestInstance, TestManager
|
|
|
|
from test_framework.script import CScript, OP_1NEGATE, OP_CHECKSEQUENCEVERIFY, OP_DROP
|
2016-03-16 06:30:04 +01:00
|
|
|
|
|
|
|
class BIP9SoftForksTest(ComparisonTestFramework):
|
2017-06-10 00:21:21 +02:00
|
|
|
def set_test_params(self):
|
2016-03-16 06:30:04 +01:00
|
|
|
self.num_nodes = 1
|
2017-04-03 15:34:04 +02:00
|
|
|
self.extra_args = [['-whitelist=127.0.0.1']]
|
2017-06-10 00:21:21 +02:00
|
|
|
self.setup_clean_chain = True
|
2016-03-16 06:30:04 +01:00
|
|
|
|
|
|
|
def run_test(self):
|
|
|
|
self.test = TestManager(self, self.options.tmpdir)
|
|
|
|
self.test.add_all_connections(self.nodes)
|
|
|
|
NetworkThread().start() # Start up network handling in another thread
|
|
|
|
self.test.run()
|
|
|
|
|
|
|
|
def create_transaction(self, node, coinbase, to_address, amount):
|
|
|
|
from_txid = node.getblock(coinbase)['tx'][0]
|
|
|
|
inputs = [{ "txid" : from_txid, "vout" : 0}]
|
|
|
|
outputs = { to_address : amount }
|
|
|
|
rawtx = node.createrawtransaction(inputs, outputs)
|
|
|
|
tx = CTransaction()
|
2016-04-10 16:54:28 +02:00
|
|
|
f = BytesIO(hex_str_to_bytes(rawtx))
|
2016-03-16 06:30:04 +01:00
|
|
|
tx.deserialize(f)
|
|
|
|
tx.nVersion = 2
|
|
|
|
return tx
|
|
|
|
|
|
|
|
def sign_transaction(self, node, tx):
|
2016-04-10 16:54:28 +02:00
|
|
|
signresult = node.signrawtransaction(bytes_to_hex_str(tx.serialize()))
|
2016-03-16 06:30:04 +01:00
|
|
|
tx = CTransaction()
|
2016-04-10 16:54:28 +02:00
|
|
|
f = BytesIO(hex_str_to_bytes(signresult['hex']))
|
2016-03-16 06:30:04 +01:00
|
|
|
tx.deserialize(f)
|
|
|
|
return tx
|
|
|
|
|
|
|
|
def generate_blocks(self, number, version, test_blocks = []):
|
2016-03-19 20:58:06 +01:00
|
|
|
for i in range(number):
|
2016-03-16 06:30:04 +01:00
|
|
|
block = create_block(self.tip, create_coinbase(self.height), self.last_block_time + 1)
|
|
|
|
block.nVersion = version
|
|
|
|
block.rehash()
|
|
|
|
block.solve()
|
|
|
|
test_blocks.append([block, True])
|
|
|
|
self.last_block_time += 1
|
|
|
|
self.tip = block.sha256
|
|
|
|
self.height += 1
|
|
|
|
return test_blocks
|
|
|
|
|
|
|
|
def get_bip9_status(self, key):
|
|
|
|
info = self.nodes[0].getblockchaininfo()
|
2016-04-12 08:14:18 +02:00
|
|
|
return info['bip9_softforks'][key]
|
2016-03-16 06:30:04 +01:00
|
|
|
|
2016-05-21 10:39:34 +02:00
|
|
|
def test_BIP(self, bipName, activated_version, invalidate, invalidatePostSignature, bitno):
|
2016-05-07 00:08:39 +02:00
|
|
|
assert_equal(self.get_bip9_status(bipName)['status'], 'defined')
|
|
|
|
assert_equal(self.get_bip9_status(bipName)['since'], 0)
|
|
|
|
|
2016-03-16 06:30:04 +01:00
|
|
|
# generate some coins for later
|
|
|
|
self.coinbase_blocks = self.nodes[0].generate(2)
|
|
|
|
self.height = 3 # height of the next block to build
|
2016-03-19 20:58:06 +01:00
|
|
|
self.tip = int("0x" + self.nodes[0].getbestblockhash(), 0)
|
2016-03-16 06:30:04 +01:00
|
|
|
self.nodeaddress = self.nodes[0].getnewaddress()
|
2016-03-20 18:18:32 +01:00
|
|
|
self.last_block_time = int(time.time())
|
2016-03-16 06:30:04 +01:00
|
|
|
|
|
|
|
assert_equal(self.get_bip9_status(bipName)['status'], 'defined')
|
2016-05-07 00:08:39 +02:00
|
|
|
assert_equal(self.get_bip9_status(bipName)['since'], 0)
|
2016-05-21 10:39:34 +02:00
|
|
|
tmpl = self.nodes[0].getblocktemplate({})
|
|
|
|
assert(bipName not in tmpl['rules'])
|
|
|
|
assert(bipName not in tmpl['vbavailable'])
|
|
|
|
assert_equal(tmpl['vbrequired'], 0)
|
|
|
|
assert_equal(tmpl['version'], 0x20000000)
|
2016-03-16 06:30:04 +01:00
|
|
|
|
|
|
|
# Test 1
|
|
|
|
# Advance from DEFINED to STARTED
|
|
|
|
test_blocks = self.generate_blocks(141, 4)
|
|
|
|
yield TestInstance(test_blocks, sync_every_block=False)
|
|
|
|
|
|
|
|
assert_equal(self.get_bip9_status(bipName)['status'], 'started')
|
2016-05-07 00:08:39 +02:00
|
|
|
assert_equal(self.get_bip9_status(bipName)['since'], 144)
|
2017-01-14 02:06:50 +01:00
|
|
|
assert_equal(self.get_bip9_status(bipName)['statistics']['elapsed'], 0)
|
|
|
|
assert_equal(self.get_bip9_status(bipName)['statistics']['count'], 0)
|
2016-05-21 10:39:34 +02:00
|
|
|
tmpl = self.nodes[0].getblocktemplate({})
|
|
|
|
assert(bipName not in tmpl['rules'])
|
|
|
|
assert_equal(tmpl['vbavailable'][bipName], bitno)
|
|
|
|
assert_equal(tmpl['vbrequired'], 0)
|
|
|
|
assert(tmpl['version'] & activated_version)
|
2016-03-16 06:30:04 +01:00
|
|
|
|
2017-01-14 02:06:50 +01:00
|
|
|
# Test 1-A
|
|
|
|
# check stats after max number of "signalling not" blocks such that LOCKED_IN still possible this period
|
|
|
|
test_blocks = self.generate_blocks(36, 4, test_blocks) # 0x00000004 (signalling not)
|
|
|
|
test_blocks = self.generate_blocks(10, activated_version) # 0x20000001 (signalling ready)
|
|
|
|
yield TestInstance(test_blocks, sync_every_block=False)
|
|
|
|
|
|
|
|
assert_equal(self.get_bip9_status(bipName)['statistics']['elapsed'], 46)
|
|
|
|
assert_equal(self.get_bip9_status(bipName)['statistics']['count'], 10)
|
|
|
|
assert_equal(self.get_bip9_status(bipName)['statistics']['possible'], True)
|
|
|
|
|
|
|
|
# Test 1-B
|
|
|
|
# check stats after one additional "signalling not" block -- LOCKED_IN no longer possible this period
|
|
|
|
test_blocks = self.generate_blocks(1, 4, test_blocks) # 0x00000004 (signalling not)
|
|
|
|
yield TestInstance(test_blocks, sync_every_block=False)
|
|
|
|
|
|
|
|
assert_equal(self.get_bip9_status(bipName)['statistics']['elapsed'], 47)
|
|
|
|
assert_equal(self.get_bip9_status(bipName)['statistics']['count'], 10)
|
|
|
|
assert_equal(self.get_bip9_status(bipName)['statistics']['possible'], False)
|
|
|
|
|
|
|
|
# Test 1-C
|
|
|
|
# finish period with "ready" blocks, but soft fork will still fail to advance to LOCKED_IN
|
|
|
|
test_blocks = self.generate_blocks(97, activated_version) # 0x20000001 (signalling ready)
|
|
|
|
yield TestInstance(test_blocks, sync_every_block=False)
|
|
|
|
|
|
|
|
assert_equal(self.get_bip9_status(bipName)['statistics']['elapsed'], 0)
|
|
|
|
assert_equal(self.get_bip9_status(bipName)['statistics']['count'], 0)
|
|
|
|
assert_equal(self.get_bip9_status(bipName)['statistics']['possible'], True)
|
|
|
|
assert_equal(self.get_bip9_status(bipName)['status'], 'started')
|
|
|
|
|
2016-03-16 06:30:04 +01:00
|
|
|
# Test 2
|
|
|
|
# Fail to achieve LOCKED_IN 100 out of 144 signal bit 1
|
|
|
|
# using a variety of bits to simulate multiple parallel softforks
|
|
|
|
test_blocks = self.generate_blocks(50, activated_version) # 0x20000001 (signalling ready)
|
|
|
|
test_blocks = self.generate_blocks(20, 4, test_blocks) # 0x00000004 (signalling not)
|
|
|
|
test_blocks = self.generate_blocks(50, activated_version, test_blocks) # 0x20000101 (signalling ready)
|
|
|
|
test_blocks = self.generate_blocks(24, 4, test_blocks) # 0x20010000 (signalling not)
|
|
|
|
yield TestInstance(test_blocks, sync_every_block=False)
|
|
|
|
|
|
|
|
assert_equal(self.get_bip9_status(bipName)['status'], 'started')
|
2016-05-07 00:08:39 +02:00
|
|
|
assert_equal(self.get_bip9_status(bipName)['since'], 144)
|
2017-01-14 02:06:50 +01:00
|
|
|
assert_equal(self.get_bip9_status(bipName)['statistics']['elapsed'], 0)
|
|
|
|
assert_equal(self.get_bip9_status(bipName)['statistics']['count'], 0)
|
2016-05-21 10:39:34 +02:00
|
|
|
tmpl = self.nodes[0].getblocktemplate({})
|
|
|
|
assert(bipName not in tmpl['rules'])
|
|
|
|
assert_equal(tmpl['vbavailable'][bipName], bitno)
|
|
|
|
assert_equal(tmpl['vbrequired'], 0)
|
|
|
|
assert(tmpl['version'] & activated_version)
|
2016-03-16 06:30:04 +01:00
|
|
|
|
|
|
|
# Test 3
|
|
|
|
# 108 out of 144 signal bit 1 to achieve LOCKED_IN
|
|
|
|
# using a variety of bits to simulate multiple parallel softforks
|
2017-01-14 02:06:50 +01:00
|
|
|
test_blocks = self.generate_blocks(57, activated_version) # 0x20000001 (signalling ready)
|
2016-03-16 06:30:04 +01:00
|
|
|
test_blocks = self.generate_blocks(26, 4, test_blocks) # 0x00000004 (signalling not)
|
|
|
|
test_blocks = self.generate_blocks(50, activated_version, test_blocks) # 0x20000101 (signalling ready)
|
|
|
|
test_blocks = self.generate_blocks(10, 4, test_blocks) # 0x20010000 (signalling not)
|
|
|
|
yield TestInstance(test_blocks, sync_every_block=False)
|
|
|
|
|
2017-01-14 02:06:50 +01:00
|
|
|
# check counting stats and "possible" flag before last block of this period achieves LOCKED_IN...
|
|
|
|
assert_equal(self.get_bip9_status(bipName)['statistics']['elapsed'], 143)
|
|
|
|
assert_equal(self.get_bip9_status(bipName)['statistics']['count'], 107)
|
|
|
|
assert_equal(self.get_bip9_status(bipName)['statistics']['possible'], True)
|
|
|
|
assert_equal(self.get_bip9_status(bipName)['status'], 'started')
|
|
|
|
|
|
|
|
# ...continue with Test 3
|
|
|
|
test_blocks = self.generate_blocks(1, activated_version) # 0x20000001 (signalling ready)
|
|
|
|
yield TestInstance(test_blocks, sync_every_block=False)
|
|
|
|
|
2016-03-16 06:30:04 +01:00
|
|
|
assert_equal(self.get_bip9_status(bipName)['status'], 'locked_in')
|
2017-01-14 02:06:50 +01:00
|
|
|
assert_equal(self.get_bip9_status(bipName)['since'], 576)
|
2016-05-21 10:39:34 +02:00
|
|
|
tmpl = self.nodes[0].getblocktemplate({})
|
|
|
|
assert(bipName not in tmpl['rules'])
|
2016-03-16 06:30:04 +01:00
|
|
|
|
|
|
|
# Test 4
|
|
|
|
# 143 more version 536870913 blocks (waiting period-1)
|
|
|
|
test_blocks = self.generate_blocks(143, 4)
|
|
|
|
yield TestInstance(test_blocks, sync_every_block=False)
|
|
|
|
|
|
|
|
assert_equal(self.get_bip9_status(bipName)['status'], 'locked_in')
|
2017-01-14 02:06:50 +01:00
|
|
|
assert_equal(self.get_bip9_status(bipName)['since'], 576)
|
2016-05-21 10:39:34 +02:00
|
|
|
tmpl = self.nodes[0].getblocktemplate({})
|
|
|
|
assert(bipName not in tmpl['rules'])
|
2016-03-16 06:30:04 +01:00
|
|
|
|
|
|
|
# Test 5
|
|
|
|
# Check that the new rule is enforced
|
|
|
|
spendtx = self.create_transaction(self.nodes[0],
|
|
|
|
self.coinbase_blocks[0], self.nodeaddress, 1.0)
|
|
|
|
invalidate(spendtx)
|
|
|
|
spendtx = self.sign_transaction(self.nodes[0], spendtx)
|
|
|
|
spendtx.rehash()
|
|
|
|
invalidatePostSignature(spendtx)
|
|
|
|
spendtx.rehash()
|
|
|
|
block = create_block(self.tip, create_coinbase(self.height), self.last_block_time + 1)
|
|
|
|
block.nVersion = activated_version
|
|
|
|
block.vtx.append(spendtx)
|
|
|
|
block.hashMerkleRoot = block.calc_merkle_root()
|
|
|
|
block.rehash()
|
|
|
|
block.solve()
|
|
|
|
|
|
|
|
self.last_block_time += 1
|
|
|
|
self.tip = block.sha256
|
|
|
|
self.height += 1
|
|
|
|
yield TestInstance([[block, True]])
|
|
|
|
|
|
|
|
assert_equal(self.get_bip9_status(bipName)['status'], 'active')
|
2017-01-14 02:06:50 +01:00
|
|
|
assert_equal(self.get_bip9_status(bipName)['since'], 720)
|
2016-05-21 10:39:34 +02:00
|
|
|
tmpl = self.nodes[0].getblocktemplate({})
|
|
|
|
assert(bipName in tmpl['rules'])
|
|
|
|
assert(bipName not in tmpl['vbavailable'])
|
|
|
|
assert_equal(tmpl['vbrequired'], 0)
|
|
|
|
assert(not (tmpl['version'] & (1 << bitno)))
|
2016-03-16 06:30:04 +01:00
|
|
|
|
|
|
|
# Test 6
|
|
|
|
# Check that the new sequence lock rules are enforced
|
|
|
|
spendtx = self.create_transaction(self.nodes[0],
|
|
|
|
self.coinbase_blocks[1], self.nodeaddress, 1.0)
|
|
|
|
invalidate(spendtx)
|
|
|
|
spendtx = self.sign_transaction(self.nodes[0], spendtx)
|
|
|
|
spendtx.rehash()
|
|
|
|
invalidatePostSignature(spendtx)
|
|
|
|
spendtx.rehash()
|
|
|
|
|
|
|
|
block = create_block(self.tip, create_coinbase(self.height), self.last_block_time + 1)
|
|
|
|
block.nVersion = 5
|
|
|
|
block.vtx.append(spendtx)
|
|
|
|
block.hashMerkleRoot = block.calc_merkle_root()
|
|
|
|
block.rehash()
|
|
|
|
block.solve()
|
|
|
|
self.last_block_time += 1
|
|
|
|
yield TestInstance([[block, False]])
|
|
|
|
|
|
|
|
# Restart all
|
2017-03-24 21:14:13 +01:00
|
|
|
self.test.clear_all_connections()
|
2017-03-24 04:56:31 +01:00
|
|
|
self.stop_nodes()
|
2017-06-09 22:35:17 +02:00
|
|
|
self.nodes = []
|
2017-03-24 21:14:13 +01:00
|
|
|
shutil.rmtree(self.options.tmpdir + "/node0")
|
2016-03-16 06:30:04 +01:00
|
|
|
self.setup_chain()
|
|
|
|
self.setup_network()
|
|
|
|
self.test.add_all_connections(self.nodes)
|
2017-03-24 21:14:13 +01:00
|
|
|
NetworkThread().start()
|
2017-11-17 21:01:24 +01:00
|
|
|
self.test.p2p_connections[0].wait_for_verack()
|
2016-03-16 06:30:04 +01:00
|
|
|
|
|
|
|
def get_tests(self):
|
|
|
|
for test in itertools.chain(
|
2016-05-21 10:39:34 +02:00
|
|
|
self.test_BIP('csv', 0x20000001, self.sequence_lock_invalidate, self.donothing, 0),
|
|
|
|
self.test_BIP('csv', 0x20000001, self.mtp_invalidate, self.donothing, 0),
|
|
|
|
self.test_BIP('csv', 0x20000001, self.donothing, self.csv_invalidate, 0)
|
2016-03-16 06:30:04 +01:00
|
|
|
):
|
|
|
|
yield test
|
|
|
|
|
|
|
|
def donothing(self, tx):
|
|
|
|
return
|
|
|
|
|
|
|
|
def csv_invalidate(self, tx):
|
2017-01-18 00:34:40 +01:00
|
|
|
"""Modify the signature in vin 0 of the tx to fail CSV
|
2016-03-16 06:30:04 +01:00
|
|
|
Prepends -1 CSV DROP in the scriptSig itself.
|
2017-01-18 00:34:40 +01:00
|
|
|
"""
|
2016-06-28 18:23:32 +02:00
|
|
|
tx.vin[0].scriptSig = CScript([OP_1NEGATE, OP_CHECKSEQUENCEVERIFY, OP_DROP] +
|
2016-03-16 06:30:04 +01:00
|
|
|
list(CScript(tx.vin[0].scriptSig)))
|
|
|
|
|
|
|
|
def sequence_lock_invalidate(self, tx):
|
2017-01-18 00:34:40 +01:00
|
|
|
"""Modify the nSequence to make it fails once sequence lock rule is
|
|
|
|
activated (high timespan).
|
|
|
|
"""
|
2016-03-16 06:30:04 +01:00
|
|
|
tx.vin[0].nSequence = 0x00FFFFFF
|
|
|
|
tx.nLockTime = 0
|
|
|
|
|
|
|
|
def mtp_invalidate(self, tx):
|
2017-01-18 00:34:40 +01:00
|
|
|
"""Modify the nLockTime to make it fails once MTP rule is activated."""
|
2016-03-16 06:30:04 +01:00
|
|
|
# Disable Sequence lock, Activate nLockTime
|
|
|
|
tx.vin[0].nSequence = 0x90FFFFFF
|
|
|
|
tx.nLockTime = self.last_block_time
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2016-03-19 21:36:32 +01:00
|
|
|
BIP9SoftForksTest().main()
|