2016-05-06 11:23:48 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# Copyright (c) 2014-2016 The Bitcoin Core developers
|
2015-12-08 23:25:28 +01:00
|
|
|
# Distributed under the MIT software license, see the accompanying
|
|
|
|
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
2019-01-07 10:55:35 +01:00
|
|
|
"""Test BIP68 implementation."""
|
2015-12-08 23:25:28 +01:00
|
|
|
|
Merge #13054: tests: Enable automatic detection of undefined names in Python tests scripts. Remove wildcard imports.
68400d8b96 tests: Use explicit imports (practicalswift)
Pull request description:
Enable automatic detection of undefined names in Python tests scripts. Remove wildcard imports.
Wildcard imports make it unclear which names are present in the namespace, confusing both readers and many automated tools.
An additional benefit of not using wildcard imports in tests scripts is that readers of a test script then can infer the rough testing scope just by looking at the imports.
Before this commit:
```
$ contrib/devtools/lint-python.sh | head -10
./test/functional/feature_rbf.py:8:1: F403 'from test_framework.util import *' used; unable to detect undefined names
./test/functional/feature_rbf.py:9:1: F403 'from test_framework.script import *' used; unable to detect undefined names
./test/functional/feature_rbf.py:10:1: F403 'from test_framework.mininode import *' used; unable to detect undefined names
./test/functional/feature_rbf.py:15:12: F405 bytes_to_hex_str may be undefined, or defined from star imports: test_framework.mininode, test_framework.script, test_framework.util
./test/functional/feature_rbf.py:17:58: F405 CScript may be undefined, or defined from star imports: test_framework.mininode, test_framework.script, test_framework.util
./test/functional/feature_rbf.py:25:13: F405 COIN may be undefined, or defined from star imports: test_framework.mininode, test_framework.script, test_framework.util
./test/functional/feature_rbf.py:26:31: F405 satoshi_round may be undefined, or defined from star imports: test_framework.mininode, test_framework.script, test_framework.util
./test/functional/feature_rbf.py:26:60: F405 COIN may be undefined, or defined from star imports: test_framework.mininode, test_framework.script, test_framework.util
./test/functional/feature_rbf.py:30:41: F405 satoshi_round may be undefined, or defined from star imports: test_framework.mininode, test_framework.script, test_framework.util
./test/functional/feature_rbf.py:30:68: F405 COIN may be undefined, or defined from star imports: test_framework.mininode, test_framework.script, test_framework.util
$
```
After this commit:
```
$ contrib/devtools/lint-python.sh | head -10
$
```
Tree-SHA512: 3f826d39cffb6438388e5efcb20a9622ff8238247e882d68f7b38609877421b2a8e10e9229575f8eb6a8fa42dec4256986692e92922c86171f750a0e887438d9
2018-08-13 14:24:43 +02:00
|
|
|
from test_framework.blocktools import create_block, create_coinbase
|
|
|
|
from test_framework.messages import COIN, COutPoint, CTransaction, CTxIn, CTxOut, FromHex, ToHex
|
|
|
|
from test_framework.script import CScript
|
2015-12-08 23:25:28 +01:00
|
|
|
from test_framework.test_framework import BitcoinTestFramework
|
Merge #13054: tests: Enable automatic detection of undefined names in Python tests scripts. Remove wildcard imports.
68400d8b96 tests: Use explicit imports (practicalswift)
Pull request description:
Enable automatic detection of undefined names in Python tests scripts. Remove wildcard imports.
Wildcard imports make it unclear which names are present in the namespace, confusing both readers and many automated tools.
An additional benefit of not using wildcard imports in tests scripts is that readers of a test script then can infer the rough testing scope just by looking at the imports.
Before this commit:
```
$ contrib/devtools/lint-python.sh | head -10
./test/functional/feature_rbf.py:8:1: F403 'from test_framework.util import *' used; unable to detect undefined names
./test/functional/feature_rbf.py:9:1: F403 'from test_framework.script import *' used; unable to detect undefined names
./test/functional/feature_rbf.py:10:1: F403 'from test_framework.mininode import *' used; unable to detect undefined names
./test/functional/feature_rbf.py:15:12: F405 bytes_to_hex_str may be undefined, or defined from star imports: test_framework.mininode, test_framework.script, test_framework.util
./test/functional/feature_rbf.py:17:58: F405 CScript may be undefined, or defined from star imports: test_framework.mininode, test_framework.script, test_framework.util
./test/functional/feature_rbf.py:25:13: F405 COIN may be undefined, or defined from star imports: test_framework.mininode, test_framework.script, test_framework.util
./test/functional/feature_rbf.py:26:31: F405 satoshi_round may be undefined, or defined from star imports: test_framework.mininode, test_framework.script, test_framework.util
./test/functional/feature_rbf.py:26:60: F405 COIN may be undefined, or defined from star imports: test_framework.mininode, test_framework.script, test_framework.util
./test/functional/feature_rbf.py:30:41: F405 satoshi_round may be undefined, or defined from star imports: test_framework.mininode, test_framework.script, test_framework.util
./test/functional/feature_rbf.py:30:68: F405 COIN may be undefined, or defined from star imports: test_framework.mininode, test_framework.script, test_framework.util
$
```
After this commit:
```
$ contrib/devtools/lint-python.sh | head -10
$
```
Tree-SHA512: 3f826d39cffb6438388e5efcb20a9622ff8238247e882d68f7b38609877421b2a8e10e9229575f8eb6a8fa42dec4256986692e92922c86171f750a0e887438d9
2018-08-13 14:24:43 +02:00
|
|
|
from test_framework.util import assert_equal, assert_greater_than, assert_raises_rpc_error, get_bip9_status, satoshi_round
|
2015-12-08 23:25:28 +01:00
|
|
|
|
|
|
|
SEQUENCE_LOCKTIME_DISABLE_FLAG = (1<<31)
|
|
|
|
SEQUENCE_LOCKTIME_TYPE_FLAG = (1<<22) # this means use time (0 means height)
|
2017-09-21 15:58:23 +02:00
|
|
|
SEQUENCE_LOCKTIME_GRANULARITY = 9 # this is a bit-shift
|
|
|
|
SEQUENCE_LOCKTIME_MASK = 0x0000ffff
|
2015-12-08 23:25:28 +01:00
|
|
|
|
|
|
|
# RPC error for non-BIP68 final transactions
|
2018-02-16 22:20:21 +01:00
|
|
|
NOT_FINAL_ERROR = "non-BIP68-final (code 64)"
|
2015-12-08 23:25:28 +01:00
|
|
|
|
|
|
|
class BIP68Test(BitcoinTestFramework):
|
2017-09-01 18:47:13 +02:00
|
|
|
def set_test_params(self):
|
2016-05-20 15:16:51 +02:00
|
|
|
self.num_nodes = 2
|
2017-05-02 20:02:55 +02:00
|
|
|
self.extra_args = [[], ["-acceptnonstdtxn=0"]]
|
2015-12-08 23:25:28 +01:00
|
|
|
|
2017-05-02 20:02:55 +02:00
|
|
|
def run_test(self):
|
2015-12-08 23:25:28 +01:00
|
|
|
self.relayfee = self.nodes[0].getnetworkinfo()["relayfee"]
|
|
|
|
|
|
|
|
# Generate some coins
|
|
|
|
self.nodes[0].generate(110)
|
|
|
|
|
2017-03-09 21:16:20 +01:00
|
|
|
self.log.info("Running test disable flag")
|
2015-12-08 23:25:28 +01:00
|
|
|
self.test_disable_flag()
|
|
|
|
|
2017-03-09 21:16:20 +01:00
|
|
|
self.log.info("Running test sequence-lock-confirmed-inputs")
|
2015-12-08 23:25:28 +01:00
|
|
|
self.test_sequence_lock_confirmed_inputs()
|
|
|
|
|
2017-03-09 21:16:20 +01:00
|
|
|
self.log.info("Running test sequence-lock-unconfirmed-inputs")
|
2015-12-08 23:25:28 +01:00
|
|
|
self.test_sequence_lock_unconfirmed_inputs()
|
|
|
|
|
2017-03-09 21:16:20 +01:00
|
|
|
self.log.info("Running test BIP68 not consensus before versionbits activation")
|
2015-12-08 23:25:28 +01:00
|
|
|
self.test_bip68_not_consensus()
|
|
|
|
|
2017-03-09 21:16:20 +01:00
|
|
|
self.log.info("Activating BIP68 (and 112/113)")
|
2016-04-07 20:33:08 +02:00
|
|
|
self.activateCSV()
|
|
|
|
|
2017-03-09 21:16:20 +01:00
|
|
|
self.log.info("Verifying nVersion=2 transactions are standard.")
|
|
|
|
self.log.info("Note that nVersion=2 transactions are always standard (independent of BIP68 activation status).")
|
2017-03-06 10:05:17 +01:00
|
|
|
self.test_version2_relay()
|
2016-04-07 20:33:08 +02:00
|
|
|
|
2017-03-09 21:16:20 +01:00
|
|
|
self.log.info("Passed")
|
2015-12-08 23:25:28 +01:00
|
|
|
|
|
|
|
# Test that BIP68 is not in effect if tx version is 1, or if
|
|
|
|
# the first sequence bit is set.
|
|
|
|
def test_disable_flag(self):
|
|
|
|
# Create some unconfirmed inputs
|
|
|
|
new_addr = self.nodes[0].getnewaddress()
|
|
|
|
self.nodes[0].sendtoaddress(new_addr, 2) # send 2 BTC
|
|
|
|
|
|
|
|
utxos = self.nodes[0].listunspent(0, 0)
|
|
|
|
assert(len(utxos) > 0)
|
|
|
|
|
|
|
|
utxo = utxos[0]
|
|
|
|
|
|
|
|
tx1 = CTransaction()
|
2016-04-03 19:03:53 +02:00
|
|
|
value = int(satoshi_round(utxo["amount"] - self.relayfee)*COIN)
|
2015-12-08 23:25:28 +01:00
|
|
|
|
|
|
|
# Check that the disable flag disables relative locktime.
|
|
|
|
# If sequence locks were used, this would require 1 block for the
|
|
|
|
# input to mature.
|
|
|
|
sequence_value = SEQUENCE_LOCKTIME_DISABLE_FLAG | 1
|
2020-07-29 03:23:12 +02:00
|
|
|
tx1.vin = [CTxIn(COutPoint(int(utxo["txid"], 16), utxo["vout"]), nSequence=sequence_value)]
|
2015-12-08 23:25:28 +01:00
|
|
|
tx1.vout = [CTxOut(value, CScript([b'a']))]
|
|
|
|
|
2018-02-20 03:29:22 +01:00
|
|
|
tx1_signed = self.nodes[0].signrawtransactionwithwallet(ToHex(tx1))["hex"]
|
2015-12-08 23:25:28 +01:00
|
|
|
tx1_id = self.nodes[0].sendrawtransaction(tx1_signed)
|
|
|
|
tx1_id = int(tx1_id, 16)
|
|
|
|
|
|
|
|
# This transaction will enable sequence-locks, so this transaction should
|
|
|
|
# fail
|
|
|
|
tx2 = CTransaction()
|
|
|
|
tx2.nVersion = 2
|
|
|
|
sequence_value = sequence_value & 0x7fffffff
|
|
|
|
tx2.vin = [CTxIn(COutPoint(tx1_id, 0), nSequence=sequence_value)]
|
2018-05-12 16:33:07 +02:00
|
|
|
tx2.vout = [CTxOut(int(value - self.relayfee * COIN), CScript([b'a' * 35]))]
|
2015-12-08 23:25:28 +01:00
|
|
|
tx2.rehash()
|
|
|
|
|
2019-09-25 11:34:51 +02:00
|
|
|
assert_raises_rpc_error(-26, NOT_FINAL_ERROR, self.nodes[0].sendrawtransaction, ToHex(tx2))
|
2015-12-08 23:25:28 +01:00
|
|
|
|
|
|
|
# Setting the version back down to 1 should disable the sequence lock,
|
|
|
|
# so this should be accepted.
|
|
|
|
tx2.nVersion = 1
|
|
|
|
|
|
|
|
self.nodes[0].sendrawtransaction(ToHex(tx2))
|
|
|
|
|
|
|
|
# Calculate the median time past of a prior block ("confirmations" before
|
|
|
|
# the current tip).
|
|
|
|
def get_median_time_past(self, confirmations):
|
|
|
|
block_hash = self.nodes[0].getblockhash(self.nodes[0].getblockcount()-confirmations)
|
|
|
|
return self.nodes[0].getblockheader(block_hash)["mediantime"]
|
|
|
|
|
|
|
|
# Test that sequence locks are respected for transactions spending confirmed inputs.
|
|
|
|
def test_sequence_lock_confirmed_inputs(self):
|
|
|
|
# Create lots of confirmed utxos, and use them to generate lots of random
|
|
|
|
# transactions.
|
|
|
|
max_outputs = 50
|
|
|
|
addresses = []
|
|
|
|
while len(addresses) < max_outputs:
|
|
|
|
addresses.append(self.nodes[0].getnewaddress())
|
|
|
|
while len(self.nodes[0].listunspent()) < 200:
|
|
|
|
import random
|
|
|
|
random.shuffle(addresses)
|
|
|
|
num_outputs = random.randint(1, max_outputs)
|
|
|
|
outputs = {}
|
2016-05-06 11:23:48 +02:00
|
|
|
for i in range(num_outputs):
|
2015-12-08 23:25:28 +01:00
|
|
|
outputs[addresses[i]] = random.randint(1, 20)*0.01
|
|
|
|
self.nodes[0].sendmany("", outputs)
|
|
|
|
self.nodes[0].generate(1)
|
|
|
|
|
|
|
|
utxos = self.nodes[0].listunspent()
|
|
|
|
|
|
|
|
# Try creating a lot of random transactions.
|
|
|
|
# Each time, choose a random number of inputs, and randomly set
|
|
|
|
# some of those inputs to be sequence locked (and randomly choose
|
|
|
|
# between height/time locking). Small random chance of making the locks
|
|
|
|
# all pass.
|
2016-05-06 11:23:48 +02:00
|
|
|
for i in range(400):
|
2015-12-08 23:25:28 +01:00
|
|
|
# Randomly choose up to 10 inputs
|
|
|
|
num_inputs = random.randint(1, 10)
|
|
|
|
random.shuffle(utxos)
|
|
|
|
|
|
|
|
# Track whether any sequence locks used should fail
|
|
|
|
should_pass = True
|
Merge #12987: tests/tools: Enable additional Python flake8 rules for automatic linting via Travis
643aad17fa Enable additional flake8 rules (practicalswift)
f020aca297 Minor Python cleanups to make flake8 pass with the new rules enabled (practicalswift)
Pull request description:
Enabled rules:
```
* E242: tab after ','
* E266: too many leading '#' for block comment
* E401: multiple imports on one line
* E402: module level import not at top of file
* E701: multiple statements on one line (colon)
* E901: SyntaxError: invalid syntax
* E902: TokenError: EOF in multi-line string
* F821: undefined name 'Foo'
* W293: blank line contains whitespace
* W606: 'async' and 'await' are reserved keywords starting with Python 3.7
```
Note to reviewers:
* In general we don't allow whitespace cleanups to existing code, but in order to allow for enabling Travis checking for these rules a few smaller whitespace cleanups had to made as part of this PR.
* Use [this `?w=1` link](https://github.com/bitcoin/bitcoin/pull/12987/files?w=1) to show a diff without whitespace changes.
Before this commit:
```
$ flake8 -qq --statistics --ignore=B,C,E,F,I,N,W --select=E112,E113,E115,E116,E125,E131,E133,E223,E224,E242,E266,E271,E272,E273,E274,E275,E304,E306,E401,E402,E502,E701,E702,E703,E714,E721,E741,E742,E743,F401,E901,E902,F402,F404,F406,F407,F601,F602,F621,F622,F631,F701,F702,F703,F704,F705,F706,F707,F811,F812,F821,F822,F823,F831,F841,W292,W293,W504,W601,W602,W603,W604,W605,W606 .
5 E266 too many leading '#' for block comment
4 E401 multiple imports on one line
6 E402 module level import not at top of file
5 E701 multiple statements on one line (colon)
1 F812 list comprehension redefines 'n' from line 159
4 F821 undefined name 'ConnectionRefusedError'
28 W293 blank line contains whitespace
```
After this commit:
```
$ flake8 -qq --statistics --ignore=B,C,E,F,I,N,W --select=E112,E113,E115,E116,E125,E131,E133,E223,E224,E242,E266,E271,E272,E273,E274,E275,E304,E306,E401,E402,E502,E701,E702,E703,E714,E721,E741,E742,E743,F401,E901,E902,F402,F404,F406,F407,F601,F602,F621,F622,F631,F701,F702,F703,F704,F705,F706,F707,F811,F812,F821,F822,F823,F831,F841,W292,W293,W504,W601,W602,W603,W604,W605,W606 .
$
```
Tree-SHA512: fc7d5e752298a50d4248afc620ee2c173135b4ca008e48e02913ac968e5a24a5fd5396926047ec62f1d580d537434ccae01f249bb2f3338fa59dc630bf97ca7a
Signed-off-by: pasta <pasta@dashboost.org>
2018-04-16 17:49:49 +02:00
|
|
|
|
2015-12-08 23:25:28 +01:00
|
|
|
# Track whether this transaction was built with sequence locks
|
|
|
|
using_sequence_locks = False
|
|
|
|
|
|
|
|
tx = CTransaction()
|
|
|
|
tx.nVersion = 2
|
|
|
|
value = 0
|
2016-05-06 11:23:48 +02:00
|
|
|
for j in range(num_inputs):
|
2015-12-08 23:25:28 +01:00
|
|
|
sequence_value = 0xfffffffe # this disables sequence locks
|
|
|
|
|
|
|
|
# 50% chance we enable sequence locks
|
|
|
|
if random.randint(0,1):
|
|
|
|
using_sequence_locks = True
|
|
|
|
|
|
|
|
# 10% of the time, make the input sequence value pass
|
|
|
|
input_will_pass = (random.randint(1,10) == 1)
|
|
|
|
sequence_value = utxos[j]["confirmations"]
|
|
|
|
if not input_will_pass:
|
|
|
|
sequence_value += 1
|
|
|
|
should_pass = False
|
|
|
|
|
|
|
|
# Figure out what the median-time-past was for the confirmed input
|
|
|
|
# Note that if an input has N confirmations, we're going back N blocks
|
|
|
|
# from the tip so that we're looking up MTP of the block
|
|
|
|
# PRIOR to the one the input appears in, as per the BIP68 spec.
|
|
|
|
orig_time = self.get_median_time_past(utxos[j]["confirmations"])
|
|
|
|
cur_time = self.get_median_time_past(0) # MTP of the tip
|
|
|
|
|
|
|
|
# can only timelock this input if it's not too old -- otherwise use height
|
|
|
|
can_time_lock = True
|
|
|
|
if ((cur_time - orig_time) >> SEQUENCE_LOCKTIME_GRANULARITY) >= SEQUENCE_LOCKTIME_MASK:
|
|
|
|
can_time_lock = False
|
|
|
|
|
|
|
|
# if time-lockable, then 50% chance we make this a time lock
|
|
|
|
if random.randint(0,1) and can_time_lock:
|
|
|
|
# Find first time-lock value that fails, or latest one that succeeds
|
|
|
|
time_delta = sequence_value << SEQUENCE_LOCKTIME_GRANULARITY
|
|
|
|
if input_will_pass and time_delta > cur_time - orig_time:
|
|
|
|
sequence_value = ((cur_time - orig_time) >> SEQUENCE_LOCKTIME_GRANULARITY)
|
|
|
|
elif (not input_will_pass and time_delta <= cur_time - orig_time):
|
|
|
|
sequence_value = ((cur_time - orig_time) >> SEQUENCE_LOCKTIME_GRANULARITY)+1
|
|
|
|
sequence_value |= SEQUENCE_LOCKTIME_TYPE_FLAG
|
|
|
|
tx.vin.append(CTxIn(COutPoint(int(utxos[j]["txid"], 16), utxos[j]["vout"]), nSequence=sequence_value))
|
|
|
|
value += utxos[j]["amount"]*COIN
|
|
|
|
# Overestimate the size of the tx - signatures should be less than 120 bytes, and leave 50 for the output
|
2016-04-03 19:03:53 +02:00
|
|
|
tx_size = len(ToHex(tx))//2 + 120*num_inputs + 50
|
|
|
|
tx.vout.append(CTxOut(int(value-self.relayfee*tx_size*COIN/1000), CScript([b'a'])))
|
2018-02-20 03:29:22 +01:00
|
|
|
rawtx = self.nodes[0].signrawtransactionwithwallet(ToHex(tx))["hex"]
|
2015-12-08 23:25:28 +01:00
|
|
|
|
2017-03-16 11:57:09 +01:00
|
|
|
if (using_sequence_locks and not should_pass):
|
|
|
|
# This transaction should be rejected
|
2019-09-25 11:34:51 +02:00
|
|
|
assert_raises_rpc_error(-26, NOT_FINAL_ERROR, self.nodes[0].sendrawtransaction, rawtx)
|
2015-12-08 23:25:28 +01:00
|
|
|
else:
|
2017-03-16 11:57:09 +01:00
|
|
|
# This raw transaction should be accepted
|
|
|
|
self.nodes[0].sendrawtransaction(rawtx)
|
2015-12-08 23:25:28 +01:00
|
|
|
utxos = self.nodes[0].listunspent()
|
|
|
|
|
|
|
|
# Test that sequence locks on unconfirmed inputs must have nSequence
|
|
|
|
# height or time of 0 to be accepted.
|
|
|
|
# Then test that BIP68-invalid transactions are removed from the mempool
|
|
|
|
# after a reorg.
|
|
|
|
def test_sequence_lock_unconfirmed_inputs(self):
|
|
|
|
# Store height so we can easily reset the chain at the end of the test
|
|
|
|
cur_height = self.nodes[0].getblockcount()
|
|
|
|
|
|
|
|
# Create a mempool tx.
|
|
|
|
txid = self.nodes[0].sendtoaddress(self.nodes[0].getnewaddress(), 2)
|
|
|
|
tx1 = FromHex(CTransaction(), self.nodes[0].getrawtransaction(txid))
|
|
|
|
tx1.rehash()
|
|
|
|
|
|
|
|
# Anyone-can-spend mempool tx.
|
|
|
|
# Sequence lock of 0 should pass.
|
|
|
|
tx2 = CTransaction()
|
|
|
|
tx2.nVersion = 2
|
|
|
|
tx2.vin = [CTxIn(COutPoint(tx1.sha256, 0), nSequence=0)]
|
|
|
|
tx2.vout = [CTxOut(int(tx1.vout[0].nValue - self.relayfee*COIN), CScript([b'a']))]
|
2018-02-20 03:29:22 +01:00
|
|
|
tx2_raw = self.nodes[0].signrawtransactionwithwallet(ToHex(tx2))["hex"]
|
2015-12-08 23:25:28 +01:00
|
|
|
tx2 = FromHex(tx2, tx2_raw)
|
|
|
|
tx2.rehash()
|
|
|
|
|
|
|
|
self.nodes[0].sendrawtransaction(tx2_raw)
|
|
|
|
|
|
|
|
# Create a spend of the 0th output of orig_tx with a sequence lock
|
|
|
|
# of 1, and test what happens when submitting.
|
|
|
|
# orig_tx.vout[0] must be an anyone-can-spend output
|
|
|
|
def test_nonzero_locks(orig_tx, node, relayfee, use_height_lock):
|
|
|
|
sequence_value = 1
|
|
|
|
if not use_height_lock:
|
|
|
|
sequence_value |= SEQUENCE_LOCKTIME_TYPE_FLAG
|
|
|
|
|
|
|
|
tx = CTransaction()
|
|
|
|
tx.nVersion = 2
|
|
|
|
tx.vin = [CTxIn(COutPoint(orig_tx.sha256, 0), nSequence=sequence_value)]
|
2018-05-12 16:33:07 +02:00
|
|
|
tx.vout = [CTxOut(int(orig_tx.vout[0].nValue - relayfee * COIN), CScript([b'a' * 35]))]
|
2015-12-08 23:25:28 +01:00
|
|
|
tx.rehash()
|
|
|
|
|
2017-03-16 11:57:09 +01:00
|
|
|
if (orig_tx.hash in node.getrawmempool()):
|
|
|
|
# sendrawtransaction should fail if the tx is in the mempool
|
2019-09-25 11:34:51 +02:00
|
|
|
assert_raises_rpc_error(-26, NOT_FINAL_ERROR, node.sendrawtransaction, ToHex(tx))
|
2015-12-08 23:25:28 +01:00
|
|
|
else:
|
2017-03-16 11:57:09 +01:00
|
|
|
# sendrawtransaction should succeed if the tx is not in the mempool
|
|
|
|
node.sendrawtransaction(ToHex(tx))
|
|
|
|
|
2015-12-08 23:25:28 +01:00
|
|
|
return tx
|
|
|
|
|
|
|
|
test_nonzero_locks(tx2, self.nodes[0], self.relayfee, use_height_lock=True)
|
|
|
|
test_nonzero_locks(tx2, self.nodes[0], self.relayfee, use_height_lock=False)
|
|
|
|
|
|
|
|
# Now mine some blocks, but make sure tx2 doesn't get mined.
|
|
|
|
# Use prioritisetransaction to lower the effective feerate to 0
|
2019-03-14 15:44:42 +01:00
|
|
|
self.nodes[0].prioritisetransaction(tx2.hash, int(-self.relayfee*COIN))
|
2019-08-09 01:14:11 +02:00
|
|
|
cur_time = self.mocktime
|
2016-05-06 11:23:48 +02:00
|
|
|
for i in range(10):
|
2015-12-08 23:25:28 +01:00
|
|
|
self.nodes[0].setmocktime(cur_time + 600)
|
|
|
|
self.nodes[0].generate(1)
|
|
|
|
cur_time += 600
|
|
|
|
|
|
|
|
assert(tx2.hash in self.nodes[0].getrawmempool())
|
|
|
|
|
|
|
|
test_nonzero_locks(tx2, self.nodes[0], self.relayfee, use_height_lock=True)
|
|
|
|
test_nonzero_locks(tx2, self.nodes[0], self.relayfee, use_height_lock=False)
|
|
|
|
|
|
|
|
# Mine tx2, and then try again
|
2019-03-14 15:44:42 +01:00
|
|
|
self.nodes[0].prioritisetransaction(tx2.hash, int(self.relayfee*COIN))
|
2015-12-08 23:25:28 +01:00
|
|
|
|
|
|
|
# Advance the time on the node so that we can test timelocks
|
|
|
|
self.nodes[0].setmocktime(cur_time+600)
|
|
|
|
self.nodes[0].generate(1)
|
|
|
|
assert(tx2.hash not in self.nodes[0].getrawmempool())
|
|
|
|
|
|
|
|
# Now that tx2 is not in the mempool, a sequence locked spend should
|
|
|
|
# succeed
|
|
|
|
tx3 = test_nonzero_locks(tx2, self.nodes[0], self.relayfee, use_height_lock=False)
|
|
|
|
assert(tx3.hash in self.nodes[0].getrawmempool())
|
|
|
|
|
|
|
|
self.nodes[0].generate(1)
|
|
|
|
assert(tx3.hash not in self.nodes[0].getrawmempool())
|
|
|
|
|
|
|
|
# One more test, this time using height locks
|
|
|
|
tx4 = test_nonzero_locks(tx3, self.nodes[0], self.relayfee, use_height_lock=True)
|
|
|
|
assert(tx4.hash in self.nodes[0].getrawmempool())
|
|
|
|
|
|
|
|
# Now try combining confirmed and unconfirmed inputs
|
|
|
|
tx5 = test_nonzero_locks(tx4, self.nodes[0], self.relayfee, use_height_lock=True)
|
|
|
|
assert(tx5.hash not in self.nodes[0].getrawmempool())
|
|
|
|
|
2016-02-10 22:01:04 +01:00
|
|
|
utxos = self.nodes[0].listunspent()
|
2015-12-08 23:25:28 +01:00
|
|
|
tx5.vin.append(CTxIn(COutPoint(int(utxos[0]["txid"], 16), utxos[0]["vout"]), nSequence=1))
|
|
|
|
tx5.vout[0].nValue += int(utxos[0]["amount"]*COIN)
|
2018-02-20 03:29:22 +01:00
|
|
|
raw_tx5 = self.nodes[0].signrawtransactionwithwallet(ToHex(tx5))["hex"]
|
2015-12-08 23:25:28 +01:00
|
|
|
|
2019-09-25 11:34:51 +02:00
|
|
|
assert_raises_rpc_error(-26, NOT_FINAL_ERROR, self.nodes[0].sendrawtransaction, raw_tx5)
|
2015-12-08 23:25:28 +01:00
|
|
|
|
|
|
|
# Test mempool-BIP68 consistency after reorg
|
|
|
|
#
|
|
|
|
# State of the transactions in the last blocks:
|
|
|
|
# ... -> [ tx2 ] -> [ tx3 ]
|
|
|
|
# tip-1 tip
|
|
|
|
# And currently tx4 is in the mempool.
|
|
|
|
#
|
|
|
|
# If we invalidate the tip, tx3 should get added to the mempool, causing
|
|
|
|
# tx4 to be removed (fails sequence-lock).
|
|
|
|
self.nodes[0].invalidateblock(self.nodes[0].getbestblockhash())
|
|
|
|
assert(tx4.hash not in self.nodes[0].getrawmempool())
|
|
|
|
assert(tx3.hash in self.nodes[0].getrawmempool())
|
|
|
|
|
|
|
|
# Now mine 2 empty blocks to reorg out the current tip (labeled tip-1 in
|
|
|
|
# diagram above).
|
|
|
|
# This would cause tx2 to be added back to the mempool, which in turn causes
|
|
|
|
# tx3 to be removed.
|
|
|
|
tip = int(self.nodes[0].getblockhash(self.nodes[0].getblockcount()-1), 16)
|
|
|
|
height = self.nodes[0].getblockcount()
|
2016-05-06 11:23:48 +02:00
|
|
|
for i in range(2):
|
2015-12-08 23:25:28 +01:00
|
|
|
block = create_block(tip, create_coinbase(height), cur_time)
|
|
|
|
block.nVersion = 3
|
|
|
|
block.rehash()
|
|
|
|
block.solve()
|
|
|
|
tip = block.sha256
|
|
|
|
height += 1
|
|
|
|
self.nodes[0].submitblock(ToHex(block))
|
|
|
|
cur_time += 1
|
|
|
|
|
|
|
|
mempool = self.nodes[0].getrawmempool()
|
|
|
|
assert(tx3.hash not in mempool)
|
|
|
|
assert(tx2.hash in mempool)
|
|
|
|
|
|
|
|
# Reset the chain and get rid of the mocktimed-blocks
|
2019-08-09 01:14:11 +02:00
|
|
|
self.nodes[0].setmocktime(self.mocktime)
|
2015-12-08 23:25:28 +01:00
|
|
|
self.nodes[0].invalidateblock(self.nodes[0].getblockhash(cur_height+1))
|
|
|
|
self.nodes[0].generate(10)
|
|
|
|
|
2016-04-07 20:33:08 +02:00
|
|
|
# Make sure that BIP68 isn't being used to validate blocks, prior to
|
|
|
|
# versionbits activation. If more blocks are mined prior to this test
|
|
|
|
# being run, then it's possible the test has activated the soft fork, and
|
|
|
|
# this test should be moved to run earlier, or deleted.
|
2015-12-08 23:25:28 +01:00
|
|
|
def test_bip68_not_consensus(self):
|
2016-04-07 20:33:08 +02:00
|
|
|
assert(get_bip9_status(self.nodes[0], 'csv')['status'] != 'active')
|
2015-12-08 23:25:28 +01:00
|
|
|
txid = self.nodes[0].sendtoaddress(self.nodes[0].getnewaddress(), 2)
|
|
|
|
|
|
|
|
tx1 = FromHex(CTransaction(), self.nodes[0].getrawtransaction(txid))
|
|
|
|
tx1.rehash()
|
|
|
|
|
|
|
|
# Make an anyone-can-spend transaction
|
|
|
|
tx2 = CTransaction()
|
|
|
|
tx2.nVersion = 1
|
|
|
|
tx2.vin = [CTxIn(COutPoint(tx1.sha256, 0), nSequence=0)]
|
|
|
|
tx2.vout = [CTxOut(int(tx1.vout[0].nValue - self.relayfee*COIN), CScript([b'a']))]
|
|
|
|
|
|
|
|
# sign tx2
|
2018-02-20 03:29:22 +01:00
|
|
|
tx2_raw = self.nodes[0].signrawtransactionwithwallet(ToHex(tx2))["hex"]
|
2015-12-08 23:25:28 +01:00
|
|
|
tx2 = FromHex(tx2, tx2_raw)
|
|
|
|
tx2.rehash()
|
|
|
|
|
|
|
|
self.nodes[0].sendrawtransaction(ToHex(tx2))
|
Merge #12987: tests/tools: Enable additional Python flake8 rules for automatic linting via Travis
643aad17fa Enable additional flake8 rules (practicalswift)
f020aca297 Minor Python cleanups to make flake8 pass with the new rules enabled (practicalswift)
Pull request description:
Enabled rules:
```
* E242: tab after ','
* E266: too many leading '#' for block comment
* E401: multiple imports on one line
* E402: module level import not at top of file
* E701: multiple statements on one line (colon)
* E901: SyntaxError: invalid syntax
* E902: TokenError: EOF in multi-line string
* F821: undefined name 'Foo'
* W293: blank line contains whitespace
* W606: 'async' and 'await' are reserved keywords starting with Python 3.7
```
Note to reviewers:
* In general we don't allow whitespace cleanups to existing code, but in order to allow for enabling Travis checking for these rules a few smaller whitespace cleanups had to made as part of this PR.
* Use [this `?w=1` link](https://github.com/bitcoin/bitcoin/pull/12987/files?w=1) to show a diff without whitespace changes.
Before this commit:
```
$ flake8 -qq --statistics --ignore=B,C,E,F,I,N,W --select=E112,E113,E115,E116,E125,E131,E133,E223,E224,E242,E266,E271,E272,E273,E274,E275,E304,E306,E401,E402,E502,E701,E702,E703,E714,E721,E741,E742,E743,F401,E901,E902,F402,F404,F406,F407,F601,F602,F621,F622,F631,F701,F702,F703,F704,F705,F706,F707,F811,F812,F821,F822,F823,F831,F841,W292,W293,W504,W601,W602,W603,W604,W605,W606 .
5 E266 too many leading '#' for block comment
4 E401 multiple imports on one line
6 E402 module level import not at top of file
5 E701 multiple statements on one line (colon)
1 F812 list comprehension redefines 'n' from line 159
4 F821 undefined name 'ConnectionRefusedError'
28 W293 blank line contains whitespace
```
After this commit:
```
$ flake8 -qq --statistics --ignore=B,C,E,F,I,N,W --select=E112,E113,E115,E116,E125,E131,E133,E223,E224,E242,E266,E271,E272,E273,E274,E275,E304,E306,E401,E402,E502,E701,E702,E703,E714,E721,E741,E742,E743,F401,E901,E902,F402,F404,F406,F407,F601,F602,F621,F622,F631,F701,F702,F703,F704,F705,F706,F707,F811,F812,F821,F822,F823,F831,F841,W292,W293,W504,W601,W602,W603,W604,W605,W606 .
$
```
Tree-SHA512: fc7d5e752298a50d4248afc620ee2c173135b4ca008e48e02913ac968e5a24a5fd5396926047ec62f1d580d537434ccae01f249bb2f3338fa59dc630bf97ca7a
Signed-off-by: pasta <pasta@dashboost.org>
2018-04-16 17:49:49 +02:00
|
|
|
|
2015-12-08 23:25:28 +01:00
|
|
|
# Now make an invalid spend of tx2 according to BIP68
|
|
|
|
sequence_value = 100 # 100 block relative locktime
|
|
|
|
|
|
|
|
tx3 = CTransaction()
|
|
|
|
tx3.nVersion = 2
|
|
|
|
tx3.vin = [CTxIn(COutPoint(tx2.sha256, 0), nSequence=sequence_value)]
|
2018-05-12 16:33:07 +02:00
|
|
|
tx3.vout = [CTxOut(int(tx2.vout[0].nValue - self.relayfee * COIN), CScript([b'a' * 35]))]
|
2015-12-08 23:25:28 +01:00
|
|
|
tx3.rehash()
|
|
|
|
|
2019-09-25 11:34:51 +02:00
|
|
|
assert_raises_rpc_error(-26, NOT_FINAL_ERROR, self.nodes[0].sendrawtransaction, ToHex(tx3))
|
2015-12-08 23:25:28 +01:00
|
|
|
|
|
|
|
# make a block that violates bip68; ensure that the tip updates
|
|
|
|
tip = int(self.nodes[0].getbestblockhash(), 16)
|
2019-08-09 01:14:11 +02:00
|
|
|
block = create_block(tip, create_coinbase(self.nodes[0].getblockcount()+1), self.mocktime + 600)
|
2015-12-08 23:25:28 +01:00
|
|
|
block.nVersion = 3
|
|
|
|
block.vtx.extend([tx1, tx2, tx3])
|
|
|
|
block.hashMerkleRoot = block.calc_merkle_root()
|
|
|
|
block.rehash()
|
|
|
|
block.solve()
|
|
|
|
|
|
|
|
self.nodes[0].submitblock(ToHex(block))
|
|
|
|
assert_equal(self.nodes[0].getbestblockhash(), block.hash)
|
|
|
|
|
2016-04-07 20:33:08 +02:00
|
|
|
def activateCSV(self):
|
|
|
|
# activation should happen at block height 432 (3 periods)
|
2017-10-02 14:48:37 +02:00
|
|
|
# getblockchaininfo will show CSV as active at block 431 (144 * 3 -1) since it's returning whether CSV is active for the next block.
|
2016-04-07 20:33:08 +02:00
|
|
|
min_activation_height = 432
|
|
|
|
height = self.nodes[0].getblockcount()
|
2017-10-02 14:48:37 +02:00
|
|
|
assert_greater_than(min_activation_height - height, 2)
|
|
|
|
self.nodes[0].generate(min_activation_height - height - 2)
|
|
|
|
assert_equal(get_bip9_status(self.nodes[0], 'csv')['status'], "locked_in")
|
|
|
|
self.nodes[0].generate(1)
|
|
|
|
assert_equal(get_bip9_status(self.nodes[0], 'csv')['status'], "active")
|
2020-04-14 12:00:16 +02:00
|
|
|
self.sync_blocks()
|
2016-04-07 20:33:08 +02:00
|
|
|
|
2017-03-06 10:05:17 +01:00
|
|
|
# Use self.nodes[1] to test that version 2 transactions are standard.
|
|
|
|
def test_version2_relay(self):
|
2016-04-07 20:33:08 +02:00
|
|
|
inputs = [ ]
|
|
|
|
outputs = { self.nodes[1].getnewaddress() : 1.0 }
|
|
|
|
rawtx = self.nodes[1].createrawtransaction(inputs, outputs)
|
|
|
|
rawtxfund = self.nodes[1].fundrawtransaction(rawtx)['hex']
|
|
|
|
tx = FromHex(CTransaction(), rawtxfund)
|
|
|
|
tx.nVersion = 2
|
2018-02-20 03:29:22 +01:00
|
|
|
tx_signed = self.nodes[1].signrawtransactionwithwallet(ToHex(tx))["hex"]
|
2017-08-28 22:53:34 +02:00
|
|
|
self.nodes[1].sendrawtransaction(tx_signed)
|
2015-12-08 23:25:28 +01:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
BIP68Test().main()
|