2016-05-06 11:23:48 +02:00
|
|
|
#!/usr/bin/env python3
|
2016-03-11 00:36:55 +01:00
|
|
|
# Copyright (c) 2015 The Bitcoin Core developers
|
2016-05-06 11:23:48 +02:00
|
|
|
# Distributed under the MIT software license, see the accompanying
|
2016-03-11 00:36:55 +01:00
|
|
|
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
2019-01-07 10:55:35 +01:00
|
|
|
"""Test activation of the first version bits soft fork.
|
2016-03-11 00:36:55 +01:00
|
|
|
|
|
|
|
This soft fork will activate the following BIPS:
|
|
|
|
BIP 68 - nSequence relative lock times
|
|
|
|
BIP 112 - CHECKSEQUENCEVERIFY
|
|
|
|
BIP 113 - MedianTimePast semantics for nLockTime
|
|
|
|
|
|
|
|
regtest lock-in with 108/144 block signalling
|
|
|
|
activation after a further 144 blocks
|
|
|
|
|
|
|
|
mine 82 blocks whose coinbases will be used to generate inputs for our tests
|
|
|
|
mine 61 blocks to transition from DEFINED to STARTED
|
|
|
|
mine 144 blocks only 100 of which are signaling readiness in order to fail to change state this period
|
|
|
|
mine 144 blocks with 108 signaling and verify STARTED->LOCKED_IN
|
|
|
|
mine 140 blocks and seed block chain with the 82 inputs will use for our tests at height 572
|
|
|
|
mine 3 blocks and verify still at LOCKED_IN and test that enforcement has not triggered
|
|
|
|
mine 1 block and test that enforcement has triggered (which triggers ACTIVE)
|
|
|
|
Test BIP 113 is enforced
|
|
|
|
Mine 4 blocks so next height is 580 and test BIP 68 is enforced for time and height
|
|
|
|
Mine 1 block so next height is 581 and test BIP 68 now passes time but not height
|
|
|
|
Mine 1 block so next height is 582 and test BIP 68 now passes time and height
|
|
|
|
Test that BIP 112 is enforced
|
|
|
|
|
|
|
|
Various transactions will be used to test that the BIPs rules are not enforced before the soft fork activates
|
|
|
|
And that after the soft fork activates transactions pass and fail as they should according to the rules.
|
|
|
|
For each BIP, transactions of versions 1 and 2 will be tested.
|
|
|
|
----------------
|
|
|
|
BIP 113:
|
|
|
|
bip113tx - modify the nLocktime variable
|
|
|
|
|
|
|
|
BIP 68:
|
|
|
|
bip68txs - 16 txs with nSequence relative locktime of 10 with various bits set as per the relative_locktimes below
|
|
|
|
|
|
|
|
BIP 112:
|
|
|
|
bip112txs_vary_nSequence - 16 txs with nSequence relative_locktimes of 10 evaluated against 10 OP_CSV OP_DROP
|
|
|
|
bip112txs_vary_nSequence_9 - 16 txs with nSequence relative_locktimes of 9 evaluated against 10 OP_CSV OP_DROP
|
|
|
|
bip112txs_vary_OP_CSV - 16 txs with nSequence = 10 evaluated against varying {relative_locktimes of 10} OP_CSV OP_DROP
|
|
|
|
bip112txs_vary_OP_CSV_9 - 16 txs with nSequence = 9 evaluated against varying {relative_locktimes of 10} OP_CSV OP_DROP
|
|
|
|
bip112tx_special - test negative argument to OP_CSV
|
2019-01-07 10:55:35 +01:00
|
|
|
"""
|
2020-01-11 02:31:44 +01:00
|
|
|
from decimal import Decimal
|
|
|
|
from itertools import product
|
|
|
|
from io import BytesIO
|
2019-01-07 10:55:35 +01:00
|
|
|
|
|
|
|
from test_framework.blocktools import create_coinbase, create_block
|
2020-01-11 02:31:44 +01:00
|
|
|
from test_framework.mininode import ToHex, CTransaction
|
|
|
|
from test_framework.mininode import network_thread_start, P2PDataStore
|
|
|
|
from test_framework.script import (
|
|
|
|
CScript,
|
|
|
|
OP_CHECKSEQUENCEVERIFY,
|
|
|
|
OP_DROP,
|
|
|
|
)
|
|
|
|
from test_framework.test_framework import (BitcoinTestFramework, GENESISTIME)
|
|
|
|
from test_framework.util import (
|
|
|
|
assert_equal,
|
|
|
|
get_bip9_status,
|
|
|
|
hex_str_to_bytes,
|
|
|
|
)
|
|
|
|
|
|
|
|
BASE_RELATIVE_LOCKTIME = 10
|
|
|
|
SEQ_DISABLE_FLAG = 1 << 31
|
|
|
|
SEQ_RANDOM_HIGH_BIT = 1 << 25
|
|
|
|
SEQ_TYPE_FLAG = 1 << 22
|
|
|
|
SEQ_RANDOM_LOW_BIT = 1 << 18
|
|
|
|
|
|
|
|
def relative_locktime(sdf, srhb, stf, srlb):
|
|
|
|
"""Returns a locktime with certain bits set."""
|
|
|
|
|
|
|
|
locktime = BASE_RELATIVE_LOCKTIME
|
|
|
|
if sdf:
|
|
|
|
locktime |= SEQ_DISABLE_FLAG
|
|
|
|
if srhb:
|
|
|
|
locktime |= SEQ_RANDOM_HIGH_BIT
|
|
|
|
if stf:
|
|
|
|
locktime |= SEQ_TYPE_FLAG
|
|
|
|
if srlb:
|
|
|
|
locktime |= SEQ_RANDOM_LOW_BIT
|
|
|
|
return locktime
|
|
|
|
|
|
|
|
def all_rlt_txs(txs):
|
|
|
|
return [tx['tx'] for tx in txs]
|
|
|
|
|
|
|
|
def create_transaction(node, txid, to_address, amount):
|
|
|
|
inputs = [{"txid": txid, "vout": 0}]
|
|
|
|
outputs = {to_address: amount}
|
|
|
|
rawtx = node.createrawtransaction(inputs, outputs)
|
|
|
|
tx = CTransaction()
|
|
|
|
f = BytesIO(hex_str_to_bytes(rawtx))
|
|
|
|
tx.deserialize(f)
|
|
|
|
return tx
|
|
|
|
|
|
|
|
def sign_transaction(node, unsignedtx):
|
|
|
|
rawtx = ToHex(unsignedtx)
|
|
|
|
signresult = node.signrawtransaction(rawtx)
|
|
|
|
tx = CTransaction()
|
|
|
|
f = BytesIO(hex_str_to_bytes(signresult['hex']))
|
|
|
|
tx.deserialize(f)
|
|
|
|
return tx
|
|
|
|
|
|
|
|
def create_bip112special(node, input, txversion, address):
|
|
|
|
tx = create_transaction(node, input, address, Decimal("499.98"))
|
|
|
|
tx.nVersion = txversion
|
|
|
|
signtx = sign_transaction(node, tx)
|
|
|
|
signtx.vin[0].scriptSig = CScript([-1, OP_CHECKSEQUENCEVERIFY, OP_DROP] + list(CScript(signtx.vin[0].scriptSig)))
|
|
|
|
return signtx
|
|
|
|
|
|
|
|
def send_generic_input_tx(node, coinbases, address):
|
|
|
|
amount = Decimal("499.99")
|
|
|
|
return node.sendrawtransaction(ToHex(sign_transaction(node, create_transaction(node, node.getblock(coinbases.pop())['tx'][0], address, amount))))
|
|
|
|
|
|
|
|
def create_bip68txs(node, bip68inputs, txversion, address, locktime_delta=0):
|
|
|
|
"""Returns a list of bip68 transactions with different bits set."""
|
2016-03-11 00:36:55 +01:00
|
|
|
txs = []
|
2020-01-11 02:31:44 +01:00
|
|
|
assert(len(bip68inputs) >= 16)
|
|
|
|
for i, (sdf, srhb, stf, srlb) in enumerate(product(*[[True, False]] * 4)):
|
|
|
|
locktime = relative_locktime(sdf, srhb, stf, srlb)
|
|
|
|
tx = create_transaction(node, bip68inputs[i], address, Decimal("499.98"))
|
|
|
|
tx.nVersion = txversion
|
|
|
|
tx.vin[0].nSequence = locktime + locktime_delta
|
|
|
|
tx = sign_transaction(node, tx)
|
|
|
|
tx.rehash()
|
|
|
|
txs.append({'tx': tx, 'sdf': sdf, 'stf': stf})
|
|
|
|
|
2016-03-11 00:36:55 +01:00
|
|
|
return txs
|
|
|
|
|
2020-01-11 02:31:44 +01:00
|
|
|
def create_bip112txs(node, bip112inputs, varyOP_CSV, txversion, address, locktime_delta=0):
|
|
|
|
"""Returns a list of bip68 transactions with different bits set."""
|
|
|
|
txs = []
|
|
|
|
assert(len(bip112inputs) >= 16)
|
|
|
|
for i, (sdf, srhb, stf, srlb) in enumerate(product(*[[True, False]] * 4)):
|
|
|
|
locktime = relative_locktime(sdf, srhb, stf, srlb)
|
|
|
|
tx = create_transaction(node, bip112inputs[i], address, Decimal("499.98"))
|
|
|
|
if (varyOP_CSV): # if varying OP_CSV, nSequence is fixed
|
|
|
|
tx.vin[0].nSequence = BASE_RELATIVE_LOCKTIME + locktime_delta
|
|
|
|
else: # vary nSequence instead, OP_CSV is fixed
|
|
|
|
tx.vin[0].nSequence = locktime + locktime_delta
|
|
|
|
tx.nVersion = txversion
|
|
|
|
signtx = sign_transaction(node, tx)
|
|
|
|
if (varyOP_CSV):
|
|
|
|
signtx.vin[0].scriptSig = CScript([locktime, OP_CHECKSEQUENCEVERIFY, OP_DROP] + list(CScript(signtx.vin[0].scriptSig)))
|
|
|
|
else:
|
|
|
|
signtx.vin[0].scriptSig = CScript([BASE_RELATIVE_LOCKTIME, OP_CHECKSEQUENCEVERIFY, OP_DROP] + list(CScript(signtx.vin[0].scriptSig)))
|
|
|
|
tx.rehash()
|
|
|
|
txs.append({'tx': signtx, 'sdf': sdf, 'stf': stf})
|
|
|
|
return txs
|
|
|
|
|
|
|
|
class BIP68_112_113Test(BitcoinTestFramework):
|
2017-09-01 18:47:13 +02:00
|
|
|
def set_test_params(self):
|
2016-03-11 00:36:55 +01:00
|
|
|
self.num_nodes = 1
|
2017-09-01 18:47:13 +02:00
|
|
|
self.setup_clean_chain = True
|
2019-06-21 15:44:29 +02:00
|
|
|
# Must also set '-maxtipage=600100' to allow syncing from very old blocks
|
|
|
|
# and '-dip3params=2000:2000' to create pre-dip3 blocks only
|
2017-05-02 20:02:55 +02:00
|
|
|
self.extra_args = [['-whitelist=127.0.0.1', '-blockversion=4', '-maxtipage=600100', '-dip3params=2000:2000']]
|
2016-03-11 00:36:55 +01:00
|
|
|
|
2019-06-21 15:44:29 +02:00
|
|
|
def setup_network(self):
|
|
|
|
self.setup_nodes()
|
|
|
|
|
2020-01-11 02:31:44 +01:00
|
|
|
def generate_blocks(self, number, version, test_blocks=None):
|
|
|
|
if test_blocks is None:
|
|
|
|
test_blocks = []
|
2016-05-06 11:23:48 +02:00
|
|
|
for i in range(number):
|
2016-03-11 00:36:55 +01:00
|
|
|
block = self.create_test_block([], version)
|
2020-01-11 02:31:44 +01:00
|
|
|
test_blocks.append(block)
|
2016-03-11 00:36:55 +01:00
|
|
|
self.last_block_time += 600
|
|
|
|
self.tip = block.sha256
|
|
|
|
self.tipheight += 1
|
|
|
|
return test_blocks
|
|
|
|
|
2020-01-11 02:31:44 +01:00
|
|
|
def create_test_block(self, txs, version=536870912):
|
2016-03-11 00:36:55 +01:00
|
|
|
block = create_block(self.tip, create_coinbase(self.tipheight + 1), self.last_block_time + 600)
|
|
|
|
block.nVersion = version
|
|
|
|
block.vtx.extend(txs)
|
|
|
|
block.hashMerkleRoot = block.calc_merkle_root()
|
|
|
|
block.rehash()
|
|
|
|
block.solve()
|
|
|
|
return block
|
|
|
|
|
2020-04-14 12:00:16 +02:00
|
|
|
def send_blocks(self, blocks, success=True, reject_code=None, reject_reason=None, request_block=True):
|
2020-01-11 02:31:44 +01:00
|
|
|
"""Sends blocks to test node. Syncs and verifies that tip has advanced to most recent block.
|
|
|
|
|
|
|
|
Call with success = False if the tip shouldn't advance to the most recent block."""
|
|
|
|
self.nodes[0].p2p.send_blocks_and_test(blocks, self.nodes[0], success=success, reject_code=reject_code, reject_reason=reject_reason, request_block=request_block)
|
|
|
|
|
|
|
|
def run_test(self):
|
|
|
|
self.nodes[0].add_p2p_connection(P2PDataStore())
|
|
|
|
network_thread_start()
|
|
|
|
self.nodes[0].p2p.wait_for_verack()
|
|
|
|
|
|
|
|
self.log.info("Generate blocks in the past for coinbase outputs.")
|
|
|
|
self.coinbase_blocks = self.nodes[0].generate(1 + 16 + 2 * 32 + 1) # 82 blocks generated for inputs
|
2018-04-18 13:48:59 +02:00
|
|
|
# set time so that there was enough time to build up to 1000 blocks 10 minutes apart on top of the last one
|
|
|
|
# without worrying about getting into the future
|
|
|
|
self.nodes[0].setmocktime(GENESISTIME + 600 * 1000 + 100)
|
2020-01-11 02:31:44 +01:00
|
|
|
self.tipheight = 82 # height of the next block to build
|
2018-04-18 13:48:59 +02:00
|
|
|
self.last_block_time = GENESISTIME
|
2020-01-11 02:31:44 +01:00
|
|
|
self.tip = int(self.nodes[0].getbestblockhash(), 16)
|
2016-03-11 00:36:55 +01:00
|
|
|
self.nodeaddress = self.nodes[0].getnewaddress()
|
|
|
|
|
2020-01-11 02:31:44 +01:00
|
|
|
self.log.info("Test that the csv softfork is DEFINED")
|
2016-04-07 20:33:08 +02:00
|
|
|
assert_equal(get_bip9_status(self.nodes[0], 'csv')['status'], 'defined')
|
2016-03-11 00:36:55 +01:00
|
|
|
test_blocks = self.generate_blocks(61, 4)
|
2020-04-14 12:00:16 +02:00
|
|
|
self.send_blocks(test_blocks)
|
2020-01-11 02:31:44 +01:00
|
|
|
|
|
|
|
self.log.info("Advance from DEFINED to STARTED, height = 143")
|
2016-04-07 20:33:08 +02:00
|
|
|
assert_equal(get_bip9_status(self.nodes[0], 'csv')['status'], 'started')
|
2016-03-11 00:36:55 +01:00
|
|
|
|
2020-01-11 02:31:44 +01:00
|
|
|
self.log.info("Fail to achieve LOCKED_IN")
|
|
|
|
# 100 out of 144 signal bit 0. Use a variety of bits to simulate multiple parallel softforks
|
|
|
|
|
|
|
|
test_blocks = self.generate_blocks(50, 536870913) # 0x20000001 (signalling ready)
|
|
|
|
test_blocks = self.generate_blocks(20, 4, test_blocks) # 0x00000004 (signalling not)
|
|
|
|
test_blocks = self.generate_blocks(50, 536871169, test_blocks) # 0x20000101 (signalling ready)
|
|
|
|
test_blocks = self.generate_blocks(24, 536936448, test_blocks) # 0x20010000 (signalling not)
|
2020-04-14 12:00:16 +02:00
|
|
|
self.send_blocks(test_blocks)
|
2020-01-11 02:31:44 +01:00
|
|
|
|
|
|
|
self.log.info("Failed to advance past STARTED, height = 287")
|
2016-04-07 20:33:08 +02:00
|
|
|
assert_equal(get_bip9_status(self.nodes[0], 'csv')['status'], 'started')
|
2016-03-11 00:36:55 +01:00
|
|
|
|
2020-01-11 02:31:44 +01:00
|
|
|
self.log.info("Generate blocks to achieve LOCK-IN")
|
2016-03-11 00:36:55 +01:00
|
|
|
# 108 out of 144 signal bit 0 to achieve lock-in
|
|
|
|
# using a variety of bits to simulate multiple parallel softforks
|
2020-01-11 02:31:44 +01:00
|
|
|
test_blocks = self.generate_blocks(58, 536870913) # 0x20000001 (signalling ready)
|
|
|
|
test_blocks = self.generate_blocks(26, 4, test_blocks) # 0x00000004 (signalling not)
|
|
|
|
test_blocks = self.generate_blocks(50, 536871169, test_blocks) # 0x20000101 (signalling ready)
|
|
|
|
test_blocks = self.generate_blocks(10, 536936448, test_blocks) # 0x20010000 (signalling not)
|
2020-04-14 12:00:16 +02:00
|
|
|
self.send_blocks(test_blocks)
|
2020-01-11 02:31:44 +01:00
|
|
|
|
|
|
|
self.log.info("Advanced from STARTED to LOCKED_IN, height = 431")
|
2016-04-07 20:33:08 +02:00
|
|
|
assert_equal(get_bip9_status(self.nodes[0], 'csv')['status'], 'locked_in')
|
2016-03-11 00:36:55 +01:00
|
|
|
|
2020-01-11 02:31:44 +01:00
|
|
|
# Generate 140 more version 4 blocks
|
2016-03-11 00:36:55 +01:00
|
|
|
test_blocks = self.generate_blocks(140, 4)
|
2020-04-14 12:00:16 +02:00
|
|
|
self.send_blocks(test_blocks)
|
2016-03-11 00:36:55 +01:00
|
|
|
|
2020-01-11 02:31:44 +01:00
|
|
|
# Inputs at height = 572
|
|
|
|
#
|
2016-03-11 00:36:55 +01:00
|
|
|
# Put inputs for all tests in the chain at height 572 (tip now = 571) (time increases by 600s per block)
|
|
|
|
# Note we reuse inputs for v1 and v2 txs so must test these separately
|
|
|
|
# 16 normal inputs
|
|
|
|
bip68inputs = []
|
2016-05-06 11:23:48 +02:00
|
|
|
for i in range(16):
|
2020-01-11 02:31:44 +01:00
|
|
|
bip68inputs.append(send_generic_input_tx(self.nodes[0], self.coinbase_blocks, self.nodeaddress))
|
|
|
|
|
2016-03-11 00:36:55 +01:00
|
|
|
# 2 sets of 16 inputs with 10 OP_CSV OP_DROP (actually will be prepended to spending scriptSig)
|
|
|
|
bip112basicinputs = []
|
2016-05-06 11:23:48 +02:00
|
|
|
for j in range(2):
|
2016-03-11 00:36:55 +01:00
|
|
|
inputs = []
|
2016-05-06 11:23:48 +02:00
|
|
|
for i in range(16):
|
2020-01-11 02:31:44 +01:00
|
|
|
inputs.append(send_generic_input_tx(self.nodes[0], self.coinbase_blocks, self.nodeaddress))
|
2016-03-11 00:36:55 +01:00
|
|
|
bip112basicinputs.append(inputs)
|
2020-01-11 02:31:44 +01:00
|
|
|
|
2016-03-11 00:36:55 +01:00
|
|
|
# 2 sets of 16 varied inputs with (relative_lock_time) OP_CSV OP_DROP (actually will be prepended to spending scriptSig)
|
|
|
|
bip112diverseinputs = []
|
2016-05-06 11:23:48 +02:00
|
|
|
for j in range(2):
|
2016-03-11 00:36:55 +01:00
|
|
|
inputs = []
|
2016-05-06 11:23:48 +02:00
|
|
|
for i in range(16):
|
2020-01-11 02:31:44 +01:00
|
|
|
inputs.append(send_generic_input_tx(self.nodes[0], self.coinbase_blocks, self.nodeaddress))
|
2016-03-11 00:36:55 +01:00
|
|
|
bip112diverseinputs.append(inputs)
|
2020-01-11 02:31:44 +01:00
|
|
|
|
2016-03-11 00:36:55 +01:00
|
|
|
# 1 special input with -1 OP_CSV OP_DROP (actually will be prepended to spending scriptSig)
|
2020-01-11 02:31:44 +01:00
|
|
|
bip112specialinput = send_generic_input_tx(self.nodes[0], self.coinbase_blocks, self.nodeaddress)
|
|
|
|
|
2016-03-11 00:36:55 +01:00
|
|
|
# 1 normal input
|
2020-01-11 02:31:44 +01:00
|
|
|
bip113input = send_generic_input_tx(self.nodes[0], self.coinbase_blocks, self.nodeaddress)
|
2016-03-11 00:36:55 +01:00
|
|
|
|
|
|
|
self.nodes[0].setmocktime(self.last_block_time + 600)
|
2020-01-11 02:31:44 +01:00
|
|
|
inputblockhash = self.nodes[0].generate(1)[0] # 1 block generated for inputs to be in chain at height 572
|
2018-04-18 13:48:59 +02:00
|
|
|
self.nodes[0].setmocktime(GENESISTIME + 600 * 1000 + 100)
|
2020-01-11 02:31:44 +01:00
|
|
|
self.tip = int(inputblockhash, 16)
|
2016-03-11 00:36:55 +01:00
|
|
|
self.tipheight += 1
|
|
|
|
self.last_block_time += 600
|
2020-01-11 02:31:44 +01:00
|
|
|
assert_equal(len(self.nodes[0].getblock(inputblockhash, True)["tx"]), 82 + 1)
|
2016-03-11 00:36:55 +01:00
|
|
|
|
|
|
|
# 2 more version 4 blocks
|
|
|
|
test_blocks = self.generate_blocks(2, 4)
|
2020-04-14 12:00:16 +02:00
|
|
|
self.send_blocks(test_blocks)
|
2020-01-11 02:31:44 +01:00
|
|
|
|
|
|
|
self.log.info("Not yet advanced to ACTIVE, height = 574 (will activate for block 576, not 575)")
|
2016-04-07 20:33:08 +02:00
|
|
|
assert_equal(get_bip9_status(self.nodes[0], 'csv')['status'], 'locked_in')
|
2016-03-11 00:36:55 +01:00
|
|
|
|
|
|
|
# Test both version 1 and version 2 transactions for all tests
|
|
|
|
# BIP113 test transaction will be modified before each use to put in appropriate block time
|
2020-01-11 02:31:44 +01:00
|
|
|
bip113tx_v1 = create_transaction(self.nodes[0], bip113input, self.nodeaddress, Decimal("499.98"))
|
2016-03-11 00:36:55 +01:00
|
|
|
bip113tx_v1.vin[0].nSequence = 0xFFFFFFFE
|
2016-12-15 17:03:23 +01:00
|
|
|
bip113tx_v1.nVersion = 1
|
2020-01-11 02:31:44 +01:00
|
|
|
bip113tx_v2 = create_transaction(self.nodes[0], bip113input, self.nodeaddress, Decimal("499.98"))
|
2016-03-11 00:36:55 +01:00
|
|
|
bip113tx_v2.vin[0].nSequence = 0xFFFFFFFE
|
|
|
|
bip113tx_v2.nVersion = 2
|
|
|
|
|
|
|
|
# For BIP68 test all 16 relative sequence locktimes
|
2020-01-11 02:31:44 +01:00
|
|
|
bip68txs_v1 = create_bip68txs(self.nodes[0], bip68inputs, 1, self.nodeaddress)
|
|
|
|
bip68txs_v2 = create_bip68txs(self.nodes[0], bip68inputs, 2, self.nodeaddress)
|
2016-03-11 00:36:55 +01:00
|
|
|
|
|
|
|
# For BIP112 test:
|
|
|
|
# 16 relative sequence locktimes of 10 against 10 OP_CSV OP_DROP inputs
|
2020-01-11 02:31:44 +01:00
|
|
|
bip112txs_vary_nSequence_v1 = create_bip112txs(self.nodes[0], bip112basicinputs[0], False, 1, self.nodeaddress)
|
|
|
|
bip112txs_vary_nSequence_v2 = create_bip112txs(self.nodes[0], bip112basicinputs[0], False, 2, self.nodeaddress)
|
2016-03-11 00:36:55 +01:00
|
|
|
# 16 relative sequence locktimes of 9 against 10 OP_CSV OP_DROP inputs
|
2020-01-11 02:31:44 +01:00
|
|
|
bip112txs_vary_nSequence_9_v1 = create_bip112txs(self.nodes[0], bip112basicinputs[1], False, 1, self.nodeaddress, -1)
|
|
|
|
bip112txs_vary_nSequence_9_v2 = create_bip112txs(self.nodes[0], bip112basicinputs[1], False, 2, self.nodeaddress, -1)
|
2016-03-11 00:36:55 +01:00
|
|
|
# sequence lock time of 10 against 16 (relative_lock_time) OP_CSV OP_DROP inputs
|
2020-01-11 02:31:44 +01:00
|
|
|
bip112txs_vary_OP_CSV_v1 = create_bip112txs(self.nodes[0], bip112diverseinputs[0], True, 1, self.nodeaddress)
|
|
|
|
bip112txs_vary_OP_CSV_v2 = create_bip112txs(self.nodes[0], bip112diverseinputs[0], True, 2, self.nodeaddress)
|
2016-03-11 00:36:55 +01:00
|
|
|
# sequence lock time of 9 against 16 (relative_lock_time) OP_CSV OP_DROP inputs
|
2020-01-11 02:31:44 +01:00
|
|
|
bip112txs_vary_OP_CSV_9_v1 = create_bip112txs(self.nodes[0], bip112diverseinputs[1], True, 1, self.nodeaddress, -1)
|
|
|
|
bip112txs_vary_OP_CSV_9_v2 = create_bip112txs(self.nodes[0], bip112diverseinputs[1], True, 2, self.nodeaddress, -1)
|
2016-03-11 00:36:55 +01:00
|
|
|
# -1 OP_CSV OP_DROP input
|
2020-01-11 02:31:44 +01:00
|
|
|
bip112tx_special_v1 = create_bip112special(self.nodes[0], bip112specialinput, 1, self.nodeaddress)
|
|
|
|
bip112tx_special_v2 = create_bip112special(self.nodes[0], bip112specialinput, 2, self.nodeaddress)
|
|
|
|
|
|
|
|
self.log.info("TESTING")
|
2016-03-11 00:36:55 +01:00
|
|
|
|
2020-01-11 02:31:44 +01:00
|
|
|
self.log.info("Pre-Soft Fork Tests. All txs should pass.")
|
|
|
|
self.log.info("Test version 1 txs")
|
2016-03-11 00:36:55 +01:00
|
|
|
|
|
|
|
success_txs = []
|
|
|
|
# add BIP113 tx and -1 CSV tx
|
2020-01-11 02:31:44 +01:00
|
|
|
bip113tx_v1.nLockTime = self.last_block_time - 600 * 5 # = MTP of prior block (not <) but < time put on current block
|
|
|
|
bip113signed1 = sign_transaction(self.nodes[0], bip113tx_v1)
|
2016-03-11 00:36:55 +01:00
|
|
|
success_txs.append(bip113signed1)
|
|
|
|
success_txs.append(bip112tx_special_v1)
|
|
|
|
# add BIP 68 txs
|
|
|
|
success_txs.extend(all_rlt_txs(bip68txs_v1))
|
|
|
|
# add BIP 112 with seq=10 txs
|
|
|
|
success_txs.extend(all_rlt_txs(bip112txs_vary_nSequence_v1))
|
|
|
|
success_txs.extend(all_rlt_txs(bip112txs_vary_OP_CSV_v1))
|
|
|
|
# try BIP 112 with seq=9 txs
|
|
|
|
success_txs.extend(all_rlt_txs(bip112txs_vary_nSequence_9_v1))
|
|
|
|
success_txs.extend(all_rlt_txs(bip112txs_vary_OP_CSV_9_v1))
|
2020-04-14 12:00:16 +02:00
|
|
|
self.send_blocks([self.create_test_block(success_txs)])
|
2016-03-11 00:36:55 +01:00
|
|
|
self.nodes[0].invalidateblock(self.nodes[0].getbestblockhash())
|
|
|
|
|
2020-01-11 02:31:44 +01:00
|
|
|
self.log.info("Test version 2 txs")
|
|
|
|
|
2016-03-11 00:36:55 +01:00
|
|
|
success_txs = []
|
|
|
|
# add BIP113 tx and -1 CSV tx
|
2020-01-11 02:31:44 +01:00
|
|
|
bip113tx_v2.nLockTime = self.last_block_time - 600 * 5 # = MTP of prior block (not <) but < time put on current block
|
|
|
|
bip113signed2 = sign_transaction(self.nodes[0], bip113tx_v2)
|
2016-03-11 00:36:55 +01:00
|
|
|
success_txs.append(bip113signed2)
|
|
|
|
success_txs.append(bip112tx_special_v2)
|
|
|
|
# add BIP 68 txs
|
|
|
|
success_txs.extend(all_rlt_txs(bip68txs_v2))
|
|
|
|
# add BIP 112 with seq=10 txs
|
|
|
|
success_txs.extend(all_rlt_txs(bip112txs_vary_nSequence_v2))
|
|
|
|
success_txs.extend(all_rlt_txs(bip112txs_vary_OP_CSV_v2))
|
|
|
|
# try BIP 112 with seq=9 txs
|
|
|
|
success_txs.extend(all_rlt_txs(bip112txs_vary_nSequence_9_v2))
|
|
|
|
success_txs.extend(all_rlt_txs(bip112txs_vary_OP_CSV_9_v2))
|
2020-04-14 12:00:16 +02:00
|
|
|
self.send_blocks([self.create_test_block(success_txs)])
|
2016-03-11 00:36:55 +01:00
|
|
|
self.nodes[0].invalidateblock(self.nodes[0].getbestblockhash())
|
|
|
|
|
|
|
|
# 1 more version 4 block to get us to height 575 so the fork should now be active for the next block
|
|
|
|
test_blocks = self.generate_blocks(1, 4)
|
2020-04-14 12:00:16 +02:00
|
|
|
self.send_blocks(test_blocks)
|
2016-04-07 20:33:08 +02:00
|
|
|
assert_equal(get_bip9_status(self.nodes[0], 'csv')['status'], 'active')
|
2016-03-11 00:36:55 +01:00
|
|
|
|
2020-01-11 02:31:44 +01:00
|
|
|
self.log.info("Post-Soft Fork Tests.")
|
2016-03-11 00:36:55 +01:00
|
|
|
|
2020-01-11 02:31:44 +01:00
|
|
|
self.log.info("BIP 113 tests")
|
2016-03-11 00:36:55 +01:00
|
|
|
# BIP 113 tests should now fail regardless of version number if nLockTime isn't satisfied by new rules
|
2020-01-11 02:31:44 +01:00
|
|
|
bip113tx_v1.nLockTime = self.last_block_time - 600 * 5 # = MTP of prior block (not <) but < time put on current block
|
|
|
|
bip113signed1 = sign_transaction(self.nodes[0], bip113tx_v1)
|
|
|
|
bip113tx_v2.nLockTime = self.last_block_time - 600 * 5 # = MTP of prior block (not <) but < time put on current block
|
|
|
|
bip113signed2 = sign_transaction(self.nodes[0], bip113tx_v2)
|
2016-03-11 00:36:55 +01:00
|
|
|
for bip113tx in [bip113signed1, bip113signed2]:
|
2020-04-14 12:00:16 +02:00
|
|
|
self.send_blocks([self.create_test_block([bip113tx])], success=False)
|
2016-03-11 00:36:55 +01:00
|
|
|
# BIP 113 tests should now pass if the locktime is < MTP
|
2020-01-11 02:31:44 +01:00
|
|
|
bip113tx_v1.nLockTime = self.last_block_time - 600 * 5 - 1 # < MTP of prior block
|
|
|
|
bip113signed1 = sign_transaction(self.nodes[0], bip113tx_v1)
|
|
|
|
bip113tx_v2.nLockTime = self.last_block_time - 600 * 5 - 1 # < MTP of prior block
|
|
|
|
bip113signed2 = sign_transaction(self.nodes[0], bip113tx_v2)
|
2016-03-11 00:36:55 +01:00
|
|
|
for bip113tx in [bip113signed1, bip113signed2]:
|
2020-04-14 12:00:16 +02:00
|
|
|
self.send_blocks([self.create_test_block([bip113tx])])
|
2016-03-11 00:36:55 +01:00
|
|
|
self.nodes[0].invalidateblock(self.nodes[0].getbestblockhash())
|
|
|
|
|
|
|
|
# Next block height = 580 after 4 blocks of random version
|
|
|
|
test_blocks = self.generate_blocks(4, 1234)
|
2020-04-14 12:00:16 +02:00
|
|
|
self.send_blocks(test_blocks)
|
2020-01-11 02:31:44 +01:00
|
|
|
|
|
|
|
self.log.info("BIP 68 tests")
|
|
|
|
self.log.info("Test version 1 txs - all should still pass")
|
2016-03-11 00:36:55 +01:00
|
|
|
|
|
|
|
success_txs = []
|
|
|
|
success_txs.extend(all_rlt_txs(bip68txs_v1))
|
2020-04-14 12:00:16 +02:00
|
|
|
self.send_blocks([self.create_test_block(success_txs)])
|
2016-03-11 00:36:55 +01:00
|
|
|
self.nodes[0].invalidateblock(self.nodes[0].getbestblockhash())
|
|
|
|
|
2020-01-11 02:31:44 +01:00
|
|
|
self.log.info("Test version 2 txs")
|
|
|
|
|
2016-03-11 00:36:55 +01:00
|
|
|
# All txs with SEQUENCE_LOCKTIME_DISABLE_FLAG set pass
|
2020-01-11 02:31:44 +01:00
|
|
|
bip68success_txs = [tx['tx'] for tx in bip68txs_v2 if tx['sdf']]
|
2020-04-14 12:00:16 +02:00
|
|
|
self.send_blocks([self.create_test_block(bip68success_txs)])
|
2016-03-11 00:36:55 +01:00
|
|
|
self.nodes[0].invalidateblock(self.nodes[0].getbestblockhash())
|
2020-01-11 02:31:44 +01:00
|
|
|
|
2016-03-11 00:36:55 +01:00
|
|
|
# All txs without flag fail as we are at delta height = 8 < 10 and delta time = 8 * 600 < 10 * 512
|
2020-01-11 02:31:44 +01:00
|
|
|
bip68timetxs = [tx['tx'] for tx in bip68txs_v2 if not tx['sdf'] and tx['stf']]
|
2016-03-11 00:36:55 +01:00
|
|
|
for tx in bip68timetxs:
|
2020-04-14 12:00:16 +02:00
|
|
|
self.send_blocks([self.create_test_block([tx])], success=False)
|
2020-01-11 02:31:44 +01:00
|
|
|
|
|
|
|
bip68heighttxs = [tx['tx'] for tx in bip68txs_v2 if not tx['sdf'] and not tx['stf']]
|
2016-03-11 00:36:55 +01:00
|
|
|
for tx in bip68heighttxs:
|
2020-04-14 12:00:16 +02:00
|
|
|
self.send_blocks([self.create_test_block([tx])], success=False)
|
2016-03-11 00:36:55 +01:00
|
|
|
|
|
|
|
# Advance one block to 581
|
|
|
|
test_blocks = self.generate_blocks(1, 1234)
|
2020-04-14 12:00:16 +02:00
|
|
|
self.send_blocks(test_blocks)
|
2016-03-11 00:36:55 +01:00
|
|
|
|
|
|
|
# Height txs should fail and time txs should now pass 9 * 600 > 10 * 512
|
|
|
|
bip68success_txs.extend(bip68timetxs)
|
2020-04-14 12:00:16 +02:00
|
|
|
self.send_blocks([self.create_test_block(bip68success_txs)])
|
2016-03-11 00:36:55 +01:00
|
|
|
self.nodes[0].invalidateblock(self.nodes[0].getbestblockhash())
|
|
|
|
for tx in bip68heighttxs:
|
2020-04-14 12:00:16 +02:00
|
|
|
self.send_blocks([self.create_test_block([tx])], success=False)
|
2016-03-11 00:36:55 +01:00
|
|
|
|
|
|
|
# Advance one block to 582
|
|
|
|
test_blocks = self.generate_blocks(1, 1234)
|
2020-04-14 12:00:16 +02:00
|
|
|
self.send_blocks(test_blocks)
|
2016-03-11 00:36:55 +01:00
|
|
|
|
|
|
|
# All BIP 68 txs should pass
|
|
|
|
bip68success_txs.extend(bip68heighttxs)
|
2020-04-14 12:00:16 +02:00
|
|
|
self.send_blocks([self.create_test_block(bip68success_txs)])
|
2016-03-11 00:36:55 +01:00
|
|
|
self.nodes[0].invalidateblock(self.nodes[0].getbestblockhash())
|
|
|
|
|
2020-01-11 02:31:44 +01:00
|
|
|
self.log.info("BIP 112 tests")
|
|
|
|
self.log.info("Test version 1 txs")
|
2016-03-11 00:36:55 +01:00
|
|
|
|
|
|
|
# -1 OP_CSV tx should fail
|
2020-04-14 12:00:16 +02:00
|
|
|
self.send_blocks([self.create_test_block([bip112tx_special_v1])], success=False)
|
2016-03-11 00:36:55 +01:00
|
|
|
# If SEQUENCE_LOCKTIME_DISABLE_FLAG is set in argument to OP_CSV, version 1 txs should still pass
|
2020-01-11 02:31:44 +01:00
|
|
|
|
|
|
|
success_txs = [tx['tx'] for tx in bip112txs_vary_OP_CSV_v1 if tx['sdf']]
|
|
|
|
success_txs += [tx['tx'] for tx in bip112txs_vary_OP_CSV_9_v1 if tx['sdf']]
|
2020-04-14 12:00:16 +02:00
|
|
|
self.send_blocks([self.create_test_block(success_txs)])
|
2016-03-11 00:36:55 +01:00
|
|
|
self.nodes[0].invalidateblock(self.nodes[0].getbestblockhash())
|
|
|
|
|
|
|
|
# If SEQUENCE_LOCKTIME_DISABLE_FLAG is unset in argument to OP_CSV, version 1 txs should now fail
|
2020-01-11 02:31:44 +01:00
|
|
|
fail_txs = all_rlt_txs(bip112txs_vary_nSequence_v1)
|
|
|
|
fail_txs += all_rlt_txs(bip112txs_vary_nSequence_9_v1)
|
|
|
|
fail_txs += [tx['tx'] for tx in bip112txs_vary_OP_CSV_9_v1 if not tx['sdf']]
|
|
|
|
fail_txs += [tx['tx'] for tx in bip112txs_vary_OP_CSV_9_v1 if not tx['sdf']]
|
2016-03-11 00:36:55 +01:00
|
|
|
for tx in fail_txs:
|
2020-04-14 12:00:16 +02:00
|
|
|
self.send_blocks([self.create_test_block([tx])], success=False)
|
2020-01-11 02:31:44 +01:00
|
|
|
|
|
|
|
self.log.info("Test version 2 txs")
|
2016-03-11 00:36:55 +01:00
|
|
|
|
|
|
|
# -1 OP_CSV tx should fail
|
2020-04-14 12:00:16 +02:00
|
|
|
self.send_blocks([self.create_test_block([bip112tx_special_v2])], success=False)
|
2016-03-11 00:36:55 +01:00
|
|
|
|
|
|
|
# If SEQUENCE_LOCKTIME_DISABLE_FLAG is set in argument to OP_CSV, version 2 txs should pass (all sequence locks are met)
|
2020-01-11 02:31:44 +01:00
|
|
|
success_txs = [tx['tx'] for tx in bip112txs_vary_OP_CSV_v2 if tx['sdf']]
|
|
|
|
success_txs += [tx['tx'] for tx in bip112txs_vary_OP_CSV_9_v2 if tx['sdf']]
|
2016-03-11 00:36:55 +01:00
|
|
|
|
2020-04-14 12:00:16 +02:00
|
|
|
self.send_blocks([self.create_test_block(success_txs)])
|
2016-03-11 00:36:55 +01:00
|
|
|
self.nodes[0].invalidateblock(self.nodes[0].getbestblockhash())
|
|
|
|
|
2020-01-11 02:31:44 +01:00
|
|
|
# SEQUENCE_LOCKTIME_DISABLE_FLAG is unset in argument to OP_CSV for all remaining txs ##
|
2016-03-11 00:36:55 +01:00
|
|
|
|
2020-01-11 02:31:44 +01:00
|
|
|
# All txs with nSequence 9 should fail either due to earlier mismatch or failing the CSV check
|
|
|
|
fail_txs = all_rlt_txs(bip112txs_vary_nSequence_9_v2)
|
|
|
|
fail_txs += [tx['tx'] for tx in bip112txs_vary_OP_CSV_9_v2 if not tx['sdf']]
|
2016-03-11 00:36:55 +01:00
|
|
|
for tx in fail_txs:
|
2020-04-14 12:00:16 +02:00
|
|
|
self.send_blocks([self.create_test_block([tx])], success=False)
|
2016-03-11 00:36:55 +01:00
|
|
|
|
|
|
|
# If SEQUENCE_LOCKTIME_DISABLE_FLAG is set in nSequence, tx should fail
|
2020-01-11 02:31:44 +01:00
|
|
|
fail_txs = [tx['tx'] for tx in bip112txs_vary_nSequence_v2 if tx['sdf']]
|
2016-03-11 00:36:55 +01:00
|
|
|
for tx in fail_txs:
|
2020-04-14 12:00:16 +02:00
|
|
|
self.send_blocks([self.create_test_block([tx])], success=False)
|
2016-03-11 00:36:55 +01:00
|
|
|
|
|
|
|
# If sequencelock types mismatch, tx should fail
|
2020-01-11 02:31:44 +01:00
|
|
|
fail_txs = [tx['tx'] for tx in bip112txs_vary_nSequence_v2 if not tx['sdf'] and tx['stf']]
|
|
|
|
fail_txs += [tx['tx'] for tx in bip112txs_vary_OP_CSV_v2 if not tx['sdf'] and tx['stf']]
|
2016-03-11 00:36:55 +01:00
|
|
|
for tx in fail_txs:
|
2020-04-14 12:00:16 +02:00
|
|
|
self.send_blocks([self.create_test_block([tx])], success=False)
|
2016-03-11 00:36:55 +01:00
|
|
|
|
|
|
|
# Remaining txs should pass, just test masking works properly
|
2020-01-11 02:31:44 +01:00
|
|
|
success_txs = [tx['tx'] for tx in bip112txs_vary_nSequence_v2 if not tx['sdf'] and not tx['stf']]
|
|
|
|
success_txs += [tx['tx'] for tx in bip112txs_vary_OP_CSV_v2 if not tx['sdf'] and not tx['stf']]
|
2020-04-14 12:00:16 +02:00
|
|
|
self.send_blocks([self.create_test_block(success_txs)])
|
2016-03-11 00:36:55 +01:00
|
|
|
self.nodes[0].invalidateblock(self.nodes[0].getbestblockhash())
|
|
|
|
|
|
|
|
# Additional test, of checking that comparison of two time types works properly
|
|
|
|
time_txs = []
|
2020-01-11 02:31:44 +01:00
|
|
|
for tx in [tx['tx'] for tx in bip112txs_vary_OP_CSV_v2 if not tx['sdf'] and tx['stf']]:
|
|
|
|
tx.vin[0].nSequence = BASE_RELATIVE_LOCKTIME | SEQ_TYPE_FLAG
|
|
|
|
signtx = sign_transaction(self.nodes[0], tx)
|
|
|
|
time_txs.append(signtx)
|
2016-03-11 00:36:55 +01:00
|
|
|
|
2020-04-14 12:00:16 +02:00
|
|
|
self.send_blocks([self.create_test_block(time_txs)])
|
2020-01-11 02:31:44 +01:00
|
|
|
self.nodes[0].invalidateblock(self.nodes[0].getbestblockhash())
|
2016-03-11 00:36:55 +01:00
|
|
|
|
2020-01-11 02:31:44 +01:00
|
|
|
# TODO: Test empty stack fails
|
2016-03-11 00:36:55 +01:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
BIP68_112_113Test().main()
|