2016-07-12 22:05:02 +02:00
|
|
|
#!/usr/bin/env python3
|
2019-02-21 02:03:13 +01:00
|
|
|
# Copyright (c) 2016-2019 The Bitcoin Core developers
|
2016-07-12 22:05:02 +02:00
|
|
|
# Distributed under the MIT software license, see the accompanying
|
|
|
|
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
2017-01-18 00:34:40 +01:00
|
|
|
"""Test compact blocks (BIP 152).
|
|
|
|
|
|
|
|
Version 1 compact blocks are pre-segwit (txids)
|
|
|
|
Version 2 compact blocks are post-segwit (wtxids)
|
|
|
|
"""
|
2018-07-07 00:10:35 +02:00
|
|
|
import random
|
|
|
|
|
2016-10-14 18:54:15 +02:00
|
|
|
from test_framework.blocktools import create_block, create_coinbase, add_witness_commitment
|
tests: Make msg_block a witness block
This diff has been generated with the following script, but is better
reviewed without looking at the script.
# -BEGIN VERIFY SCRIPT-
echo "Use msg_witness_block everywhere, except for tests that require msg_block"
# This could be a separate commit, but it is combined with the
# following scripts to reduce the overall diff
sed -i -e 's/msg_block/msg_witness_block/g' ./test/functional/{feature_assumevalid,feature_cltv,feature_dersig,feature_versionbits_warning,p2p_fingerprint,p2p_sendheaders,p2p_unrequested_blocks,example_test,rpc_blockchain}.py
echo "Rename msg_block to msg_no_witness_block"
# Rename msg_block to msg_no_witness_block in all tests (not the
# framework)
sed -i -e 's/msg_block/msg_no_witness_block/g' $(git grep -l msg_block ./test/functional/*.py)
# Derive msg_no_witness_block from msg_block
# Make msg_block a witness block in messages.py
patch -p1 --fuzz 0 << EOF
diff --git a/test/functional/test_framework/messages.py b/test/functional/test_framework/messages.py
index 00190e4cbd..e454ed5987 100755
--- a/test/functional/test_framework/messages.py
+++ b/test/functional/test_framework/messages.py
@@ -1133 +1133 @@ class msg_block:
- return self.block.serialize(with_witness=False)
+ return self.block.serialize()
@@ -1155 +1155 @@ class msg_generic:
-class msg_witness_block(msg_block):
+class msg_no_witness_block(msg_block):
@@ -1158,2 +1158 @@ class msg_witness_block(msg_block):
- r = self.block.serialize()
- return r
+ return self.block.serialize(with_witness=False)
@@ -1445 +1444 @@ class msg_blocktxn:
- r += self.block_transactions.serialize(with_witness=False)
+ r += self.block_transactions.serialize()
@@ -1452 +1451 @@ class msg_blocktxn:
-class msg_witness_blocktxn(msg_blocktxn):
+class msg_no_witness_blocktxn(msg_blocktxn):
@@ -1456,3 +1455 @@ class msg_witness_blocktxn(msg_blocktxn):
- r = b""
- r += self.block_transactions.serialize()
- return r
+ return self.block_transactions.serialize(with_witness=False)
EOF
# Conclude rename of msg_block to msg_no_witness_block
sed -i -e 's/msg_witness_block/msg_block/g' $(git grep -l msg_witness_block)
# -END VERIFY SCRIPT-
2019-05-08 16:21:25 +02:00
|
|
|
from test_framework.messages import BlockTransactions, BlockTransactionsRequest, calculate_shortid, CBlock, CBlockHeader, CInv, COutPoint, CTransaction, CTxIn, CTxInWitness, CTxOut, FromHex, HeaderAndShortIDs, msg_no_witness_block, msg_no_witness_blocktxn, msg_cmpctblock, msg_getblocktxn, msg_getdata, msg_getheaders, msg_headers, msg_inv, msg_sendcmpct, msg_sendheaders, msg_tx, msg_block, msg_blocktxn, MSG_WITNESS_FLAG, NODE_NETWORK, P2PHeaderAndShortIDs, PrefilledTransaction, ser_uint256, ToHex
|
2018-07-07 00:10:35 +02:00
|
|
|
from test_framework.mininode import mininode_lock, P2PInterface
|
2018-05-03 22:50:25 +02:00
|
|
|
from test_framework.script import CScript, OP_TRUE, OP_DROP
|
2018-07-07 00:10:35 +02:00
|
|
|
from test_framework.test_framework import BitcoinTestFramework
|
2019-05-20 20:59:07 +02:00
|
|
|
from test_framework.util import assert_equal, wait_until, softfork_active
|
2016-07-12 22:05:02 +02:00
|
|
|
|
2019-08-30 17:28:43 +02:00
|
|
|
# TestP2PConn: A peer we use to send messages to lbrycrdd, and store responses.
|
2018-02-07 15:22:58 +01:00
|
|
|
class TestP2PConn(P2PInterface):
|
2019-03-23 19:55:51 +01:00
|
|
|
def __init__(self, cmpct_version):
|
2017-03-28 19:41:22 +02:00
|
|
|
super().__init__()
|
2016-09-17 02:48:23 +02:00
|
|
|
self.last_sendcmpct = []
|
2016-07-12 22:05:02 +02:00
|
|
|
self.block_announced = False
|
2016-10-04 21:17:19 +02:00
|
|
|
# Store the hashes of blocks we've seen announced.
|
|
|
|
# This is for synchronizing the p2p message traffic,
|
|
|
|
# so we can eg wait until a particular block is announced.
|
2017-03-30 14:38:46 +02:00
|
|
|
self.announced_blockhashes = set()
|
2019-03-23 19:55:51 +01:00
|
|
|
self.cmpct_version = cmpct_version
|
2016-07-12 22:05:02 +02:00
|
|
|
|
2017-11-17 21:01:24 +01:00
|
|
|
def on_sendcmpct(self, message):
|
2016-09-17 02:48:23 +02:00
|
|
|
self.last_sendcmpct.append(message)
|
2016-07-12 22:05:02 +02:00
|
|
|
|
2017-11-17 21:01:24 +01:00
|
|
|
def on_cmpctblock(self, message):
|
2016-07-12 22:05:02 +02:00
|
|
|
self.block_announced = True
|
2017-03-30 14:38:46 +02:00
|
|
|
self.last_message["cmpctblock"].header_and_shortids.header.calc_sha256()
|
|
|
|
self.announced_blockhashes.add(self.last_message["cmpctblock"].header_and_shortids.header.sha256)
|
2016-07-12 22:05:02 +02:00
|
|
|
|
2017-11-17 21:01:24 +01:00
|
|
|
def on_headers(self, message):
|
2016-07-12 22:05:02 +02:00
|
|
|
self.block_announced = True
|
2017-03-30 14:38:46 +02:00
|
|
|
for x in self.last_message["headers"].headers:
|
2016-10-04 21:17:19 +02:00
|
|
|
x.calc_sha256()
|
2017-03-30 14:38:46 +02:00
|
|
|
self.announced_blockhashes.add(x.sha256)
|
2016-07-12 22:05:02 +02:00
|
|
|
|
2017-11-17 21:01:24 +01:00
|
|
|
def on_inv(self, message):
|
2017-03-30 14:38:46 +02:00
|
|
|
for x in self.last_message["inv"].inv:
|
2016-10-04 21:17:19 +02:00
|
|
|
if x.type == 2:
|
|
|
|
self.block_announced = True
|
2017-03-30 14:38:46 +02:00
|
|
|
self.announced_blockhashes.add(x.hash)
|
2016-07-12 22:05:02 +02:00
|
|
|
|
|
|
|
# Requires caller to hold mininode_lock
|
|
|
|
def received_block_announcement(self):
|
|
|
|
return self.block_announced
|
|
|
|
|
|
|
|
def clear_block_announcement(self):
|
|
|
|
with mininode_lock:
|
|
|
|
self.block_announced = False
|
2017-03-30 14:38:46 +02:00
|
|
|
self.last_message.pop("inv", None)
|
|
|
|
self.last_message.pop("headers", None)
|
|
|
|
self.last_message.pop("cmpctblock", None)
|
2016-07-12 22:05:02 +02:00
|
|
|
|
|
|
|
def get_headers(self, locator, hashstop):
|
|
|
|
msg = msg_getheaders()
|
|
|
|
msg.locator.vHave = locator
|
|
|
|
msg.hashstop = hashstop
|
2017-11-17 21:01:24 +01:00
|
|
|
self.send_message(msg)
|
2016-07-12 22:05:02 +02:00
|
|
|
|
|
|
|
def send_header_for_blocks(self, new_blocks):
|
|
|
|
headers_message = msg_headers()
|
|
|
|
headers_message.headers = [CBlockHeader(b) for b in new_blocks]
|
|
|
|
self.send_message(headers_message)
|
|
|
|
|
2016-09-15 03:00:53 +02:00
|
|
|
def request_headers_and_sync(self, locator, hashstop=0):
|
|
|
|
self.clear_block_announcement()
|
|
|
|
self.get_headers(locator, hashstop)
|
2017-08-16 18:17:34 +02:00
|
|
|
wait_until(self.received_block_announcement, timeout=30, lock=mininode_lock)
|
2016-09-15 03:00:53 +02:00
|
|
|
self.clear_block_announcement()
|
|
|
|
|
2016-10-04 21:17:19 +02:00
|
|
|
# Block until a block announcement for a particular block hash is
|
|
|
|
# received.
|
|
|
|
def wait_for_block_announcement(self, block_hash, timeout=30):
|
|
|
|
def received_hash():
|
2017-03-30 14:38:46 +02:00
|
|
|
return (block_hash in self.announced_blockhashes)
|
2017-08-16 18:17:34 +02:00
|
|
|
wait_until(received_hash, timeout=timeout, lock=mininode_lock)
|
2016-07-12 22:05:02 +02:00
|
|
|
|
2017-03-29 20:16:42 +02:00
|
|
|
def send_await_disconnect(self, message, timeout=30):
|
|
|
|
"""Sends a message to the node and wait for disconnect.
|
|
|
|
|
|
|
|
This is used when we want to send a message into the node that we expect
|
|
|
|
will get us disconnected, eg an invalid block."""
|
|
|
|
self.send_message(message)
|
2018-06-21 03:24:29 +02:00
|
|
|
wait_until(lambda: not self.is_connected, timeout=timeout, lock=mininode_lock)
|
2017-03-29 20:16:42 +02:00
|
|
|
|
2016-07-12 22:05:02 +02:00
|
|
|
class CompactBlocksTest(BitcoinTestFramework):
|
2017-06-10 00:21:21 +02:00
|
|
|
def set_test_params(self):
|
2020-02-07 17:59:29 +01:00
|
|
|
self.setup_clean_chain = False
|
2019-03-23 19:55:51 +01:00
|
|
|
self.num_nodes = 1
|
2019-04-24 23:55:58 +02:00
|
|
|
self.extra_args = [[
|
|
|
|
"-acceptnonstdtxn=1",
|
|
|
|
]]
|
2016-07-12 22:05:02 +02:00
|
|
|
self.utxos = []
|
|
|
|
|
2018-09-09 19:32:37 +02:00
|
|
|
def skip_test_if_missing_module(self):
|
|
|
|
self.skip_if_no_wallet()
|
|
|
|
|
2016-10-14 18:54:15 +02:00
|
|
|
def build_block_on_tip(self, node, segwit=False):
|
2016-09-17 02:48:23 +02:00
|
|
|
height = node.getblockcount()
|
|
|
|
tip = node.getbestblockhash()
|
|
|
|
mtp = node.getblockheader(tip)['mediantime']
|
2016-07-12 22:05:02 +02:00
|
|
|
block = create_block(int(tip, 16), create_coinbase(height + 1), mtp + 1)
|
2016-10-15 23:51:05 +02:00
|
|
|
block.nVersion = 4
|
2016-10-14 18:54:15 +02:00
|
|
|
if segwit:
|
|
|
|
add_witness_commitment(block)
|
2016-07-12 22:05:02 +02:00
|
|
|
block.solve()
|
|
|
|
return block
|
|
|
|
|
|
|
|
# Create 10 more anyone-can-spend utxo's for testing.
|
|
|
|
def make_utxos(self):
|
2016-09-17 02:48:23 +02:00
|
|
|
block = self.build_block_on_tip(self.nodes[0])
|
tests: Make msg_block a witness block
This diff has been generated with the following script, but is better
reviewed without looking at the script.
# -BEGIN VERIFY SCRIPT-
echo "Use msg_witness_block everywhere, except for tests that require msg_block"
# This could be a separate commit, but it is combined with the
# following scripts to reduce the overall diff
sed -i -e 's/msg_block/msg_witness_block/g' ./test/functional/{feature_assumevalid,feature_cltv,feature_dersig,feature_versionbits_warning,p2p_fingerprint,p2p_sendheaders,p2p_unrequested_blocks,example_test,rpc_blockchain}.py
echo "Rename msg_block to msg_no_witness_block"
# Rename msg_block to msg_no_witness_block in all tests (not the
# framework)
sed -i -e 's/msg_block/msg_no_witness_block/g' $(git grep -l msg_block ./test/functional/*.py)
# Derive msg_no_witness_block from msg_block
# Make msg_block a witness block in messages.py
patch -p1 --fuzz 0 << EOF
diff --git a/test/functional/test_framework/messages.py b/test/functional/test_framework/messages.py
index 00190e4cbd..e454ed5987 100755
--- a/test/functional/test_framework/messages.py
+++ b/test/functional/test_framework/messages.py
@@ -1133 +1133 @@ class msg_block:
- return self.block.serialize(with_witness=False)
+ return self.block.serialize()
@@ -1155 +1155 @@ class msg_generic:
-class msg_witness_block(msg_block):
+class msg_no_witness_block(msg_block):
@@ -1158,2 +1158 @@ class msg_witness_block(msg_block):
- r = self.block.serialize()
- return r
+ return self.block.serialize(with_witness=False)
@@ -1445 +1444 @@ class msg_blocktxn:
- r += self.block_transactions.serialize(with_witness=False)
+ r += self.block_transactions.serialize()
@@ -1452 +1451 @@ class msg_blocktxn:
-class msg_witness_blocktxn(msg_blocktxn):
+class msg_no_witness_blocktxn(msg_blocktxn):
@@ -1456,3 +1455 @@ class msg_witness_blocktxn(msg_blocktxn):
- r = b""
- r += self.block_transactions.serialize()
- return r
+ return self.block_transactions.serialize(with_witness=False)
EOF
# Conclude rename of msg_block to msg_no_witness_block
sed -i -e 's/msg_witness_block/msg_block/g' $(git grep -l msg_witness_block)
# -END VERIFY SCRIPT-
2019-05-08 16:21:25 +02:00
|
|
|
self.segwit_node.send_and_ping(msg_no_witness_block(block))
|
2019-02-19 23:43:44 +01:00
|
|
|
assert int(self.nodes[0].getbestblockhash(), 16) == block.sha256
|
2019-03-23 19:55:51 +01:00
|
|
|
self.nodes[0].generatetoaddress(100, self.nodes[0].getnewaddress(address_type="bech32"))
|
2016-07-12 22:05:02 +02:00
|
|
|
|
|
|
|
total_value = block.vtx[0].vout[0].nValue
|
|
|
|
out_value = total_value // 10
|
|
|
|
tx = CTransaction()
|
|
|
|
tx.vin.append(CTxIn(COutPoint(block.vtx[0].sha256, 0), b''))
|
|
|
|
for i in range(10):
|
|
|
|
tx.vout.append(CTxOut(out_value, CScript([OP_TRUE])))
|
|
|
|
tx.rehash()
|
|
|
|
|
2016-09-17 02:48:23 +02:00
|
|
|
block2 = self.build_block_on_tip(self.nodes[0])
|
2016-07-12 22:05:02 +02:00
|
|
|
block2.vtx.append(tx)
|
|
|
|
block2.hashMerkleRoot = block2.calc_merkle_root()
|
|
|
|
block2.solve()
|
tests: Make msg_block a witness block
This diff has been generated with the following script, but is better
reviewed without looking at the script.
# -BEGIN VERIFY SCRIPT-
echo "Use msg_witness_block everywhere, except for tests that require msg_block"
# This could be a separate commit, but it is combined with the
# following scripts to reduce the overall diff
sed -i -e 's/msg_block/msg_witness_block/g' ./test/functional/{feature_assumevalid,feature_cltv,feature_dersig,feature_versionbits_warning,p2p_fingerprint,p2p_sendheaders,p2p_unrequested_blocks,example_test,rpc_blockchain}.py
echo "Rename msg_block to msg_no_witness_block"
# Rename msg_block to msg_no_witness_block in all tests (not the
# framework)
sed -i -e 's/msg_block/msg_no_witness_block/g' $(git grep -l msg_block ./test/functional/*.py)
# Derive msg_no_witness_block from msg_block
# Make msg_block a witness block in messages.py
patch -p1 --fuzz 0 << EOF
diff --git a/test/functional/test_framework/messages.py b/test/functional/test_framework/messages.py
index 00190e4cbd..e454ed5987 100755
--- a/test/functional/test_framework/messages.py
+++ b/test/functional/test_framework/messages.py
@@ -1133 +1133 @@ class msg_block:
- return self.block.serialize(with_witness=False)
+ return self.block.serialize()
@@ -1155 +1155 @@ class msg_generic:
-class msg_witness_block(msg_block):
+class msg_no_witness_block(msg_block):
@@ -1158,2 +1158 @@ class msg_witness_block(msg_block):
- r = self.block.serialize()
- return r
+ return self.block.serialize(with_witness=False)
@@ -1445 +1444 @@ class msg_blocktxn:
- r += self.block_transactions.serialize(with_witness=False)
+ r += self.block_transactions.serialize()
@@ -1452 +1451 @@ class msg_blocktxn:
-class msg_witness_blocktxn(msg_blocktxn):
+class msg_no_witness_blocktxn(msg_blocktxn):
@@ -1456,3 +1455 @@ class msg_witness_blocktxn(msg_blocktxn):
- r = b""
- r += self.block_transactions.serialize()
- return r
+ return self.block_transactions.serialize(with_witness=False)
EOF
# Conclude rename of msg_block to msg_no_witness_block
sed -i -e 's/msg_witness_block/msg_block/g' $(git grep -l msg_witness_block)
# -END VERIFY SCRIPT-
2019-05-08 16:21:25 +02:00
|
|
|
self.segwit_node.send_and_ping(msg_no_witness_block(block2))
|
2016-07-12 22:05:02 +02:00
|
|
|
assert_equal(int(self.nodes[0].getbestblockhash(), 16), block2.sha256)
|
|
|
|
self.utxos.extend([[tx.sha256, i, out_value] for i in range(10)])
|
2019-03-23 19:55:51 +01:00
|
|
|
|
2016-07-12 22:05:02 +02:00
|
|
|
|
2016-09-17 02:48:23 +02:00
|
|
|
# Test "sendcmpct" (between peers preferring the same version):
|
|
|
|
# - No compact block announcements unless sendcmpct is sent.
|
|
|
|
# - If sendcmpct is sent with version > preferred_version, the message is ignored.
|
2016-07-12 22:05:02 +02:00
|
|
|
# - If sendcmpct is sent with boolean 0, then block announcements are not
|
|
|
|
# made with compact blocks.
|
|
|
|
# - If sendcmpct is then sent with boolean 1, then new block announcements
|
|
|
|
# are made with compact blocks.
|
2016-09-17 02:48:23 +02:00
|
|
|
# If old_node is passed in, request compact blocks with version=preferred-1
|
|
|
|
# and verify that it receives block announcements via compact block.
|
2019-03-23 19:55:51 +01:00
|
|
|
def test_sendcmpct(self, test_node, old_node=None):
|
|
|
|
preferred_version = test_node.cmpct_version
|
|
|
|
node = self.nodes[0]
|
|
|
|
|
2016-09-17 02:48:23 +02:00
|
|
|
# Make sure we get a SENDCMPCT message from our peer
|
2016-07-12 22:05:02 +02:00
|
|
|
def received_sendcmpct():
|
2016-09-17 02:48:23 +02:00
|
|
|
return (len(test_node.last_sendcmpct) > 0)
|
2017-08-16 18:17:34 +02:00
|
|
|
wait_until(received_sendcmpct, timeout=30, lock=mininode_lock)
|
2016-09-17 02:48:23 +02:00
|
|
|
with mininode_lock:
|
|
|
|
# Check that the first version received is the preferred one
|
|
|
|
assert_equal(test_node.last_sendcmpct[0].version, preferred_version)
|
|
|
|
# And that we receive versions down to 1.
|
|
|
|
assert_equal(test_node.last_sendcmpct[-1].version, 1)
|
|
|
|
test_node.last_sendcmpct = []
|
2016-07-12 22:05:02 +02:00
|
|
|
|
2016-09-17 02:48:23 +02:00
|
|
|
tip = int(node.getbestblockhash(), 16)
|
2016-07-12 22:05:02 +02:00
|
|
|
|
|
|
|
def check_announcement_of_new_block(node, peer, predicate):
|
2016-09-15 03:00:53 +02:00
|
|
|
peer.clear_block_announcement()
|
[qa] Wait for specific block announcement in p2p-compactblocks
Change check_announcement_of_new_block() to wait specifically for the
announcement of the newly created block, instead of waiting for any
announcement at all. A difficult to reproduce failure in
check_announcement_of_new_block() that happened in a travis build
(https://travis-ci.org/bitcoin/bitcoin/jobs/175198367) might have happened
because an older announcement was mistaken for the expected one. The error
looked like:
Assertion failed: Failed
File ".../bitcoin/qa/rpc-tests/test_framework/test_framework.py", line 145, in main
self.run_test()
File ".../bitcoin/build/../qa/rpc-tests/p2p-compactblocks.py", line 787, in run_test
self.test_sendcmpct(self.nodes[1], self.segwit_node, 2, old_node=self.old_node)
File ".../bitcoin/build/../qa/rpc-tests/p2p-compactblocks.py", line 201, in test_sendcmpct
check_announcement_of_new_block(node, test_node, lambda p: p.last_cmpctblock is None and p.last_inv is not None)
File ".../bitcoin/build/../qa/rpc-tests/p2p-compactblocks.py", line 194, in check_announcement_of_new_block
assert(predicate(peer))
This commit also changes the assertion failed message above to include more
detailed information for debug.
2016-11-14 15:58:30 +01:00
|
|
|
block_hash = int(node.generate(1)[0], 16)
|
|
|
|
peer.wait_for_block_announcement(block_hash, timeout=30)
|
2019-02-19 23:43:44 +01:00
|
|
|
assert peer.block_announced
|
[qa] Wait for specific block announcement in p2p-compactblocks
Change check_announcement_of_new_block() to wait specifically for the
announcement of the newly created block, instead of waiting for any
announcement at all. A difficult to reproduce failure in
check_announcement_of_new_block() that happened in a travis build
(https://travis-ci.org/bitcoin/bitcoin/jobs/175198367) might have happened
because an older announcement was mistaken for the expected one. The error
looked like:
Assertion failed: Failed
File ".../bitcoin/qa/rpc-tests/test_framework/test_framework.py", line 145, in main
self.run_test()
File ".../bitcoin/build/../qa/rpc-tests/p2p-compactblocks.py", line 787, in run_test
self.test_sendcmpct(self.nodes[1], self.segwit_node, 2, old_node=self.old_node)
File ".../bitcoin/build/../qa/rpc-tests/p2p-compactblocks.py", line 201, in test_sendcmpct
check_announcement_of_new_block(node, test_node, lambda p: p.last_cmpctblock is None and p.last_inv is not None)
File ".../bitcoin/build/../qa/rpc-tests/p2p-compactblocks.py", line 194, in check_announcement_of_new_block
assert(predicate(peer))
This commit also changes the assertion failed message above to include more
detailed information for debug.
2016-11-14 15:58:30 +01:00
|
|
|
|
2016-07-12 22:05:02 +02:00
|
|
|
with mininode_lock:
|
[qa] Wait for specific block announcement in p2p-compactblocks
Change check_announcement_of_new_block() to wait specifically for the
announcement of the newly created block, instead of waiting for any
announcement at all. A difficult to reproduce failure in
check_announcement_of_new_block() that happened in a travis build
(https://travis-ci.org/bitcoin/bitcoin/jobs/175198367) might have happened
because an older announcement was mistaken for the expected one. The error
looked like:
Assertion failed: Failed
File ".../bitcoin/qa/rpc-tests/test_framework/test_framework.py", line 145, in main
self.run_test()
File ".../bitcoin/build/../qa/rpc-tests/p2p-compactblocks.py", line 787, in run_test
self.test_sendcmpct(self.nodes[1], self.segwit_node, 2, old_node=self.old_node)
File ".../bitcoin/build/../qa/rpc-tests/p2p-compactblocks.py", line 201, in test_sendcmpct
check_announcement_of_new_block(node, test_node, lambda p: p.last_cmpctblock is None and p.last_inv is not None)
File ".../bitcoin/build/../qa/rpc-tests/p2p-compactblocks.py", line 194, in check_announcement_of_new_block
assert(predicate(peer))
This commit also changes the assertion failed message above to include more
detailed information for debug.
2016-11-14 15:58:30 +01:00
|
|
|
assert predicate(peer), (
|
|
|
|
"block_hash={!r}, cmpctblock={!r}, inv={!r}".format(
|
2017-03-30 14:38:46 +02:00
|
|
|
block_hash, peer.last_message.get("cmpctblock", None), peer.last_message.get("inv", None)))
|
2016-07-12 22:05:02 +02:00
|
|
|
|
|
|
|
# We shouldn't get any block announcements via cmpctblock yet.
|
2017-03-30 14:38:46 +02:00
|
|
|
check_announcement_of_new_block(node, test_node, lambda p: "cmpctblock" not in p.last_message)
|
2016-07-12 22:05:02 +02:00
|
|
|
|
|
|
|
# Try one more time, this time after requesting headers.
|
2016-09-17 02:48:23 +02:00
|
|
|
test_node.request_headers_and_sync(locator=[tip])
|
2017-03-30 14:38:46 +02:00
|
|
|
check_announcement_of_new_block(node, test_node, lambda p: "cmpctblock" not in p.last_message and "inv" in p.last_message)
|
2016-07-12 22:05:02 +02:00
|
|
|
|
2016-09-15 03:00:53 +02:00
|
|
|
# Test a few ways of using sendcmpct that should NOT
|
|
|
|
# result in compact block announcements.
|
|
|
|
# Before each test, sync the headers chain.
|
2016-09-17 02:48:23 +02:00
|
|
|
test_node.request_headers_and_sync(locator=[tip])
|
2016-07-12 22:05:02 +02:00
|
|
|
|
|
|
|
# Now try a SENDCMPCT message with too-high version
|
|
|
|
sendcmpct = msg_sendcmpct()
|
2018-09-23 14:50:47 +02:00
|
|
|
sendcmpct.version = preferred_version + 1
|
2016-09-17 02:48:23 +02:00
|
|
|
sendcmpct.announce = True
|
|
|
|
test_node.send_and_ping(sendcmpct)
|
2017-03-30 14:38:46 +02:00
|
|
|
check_announcement_of_new_block(node, test_node, lambda p: "cmpctblock" not in p.last_message)
|
2016-07-12 22:05:02 +02:00
|
|
|
|
2016-09-15 03:00:53 +02:00
|
|
|
# Headers sync before next test.
|
2016-09-17 02:48:23 +02:00
|
|
|
test_node.request_headers_and_sync(locator=[tip])
|
2016-07-12 22:05:02 +02:00
|
|
|
|
|
|
|
# Now try a SENDCMPCT message with valid version, but announce=False
|
2016-09-17 02:48:23 +02:00
|
|
|
sendcmpct.version = preferred_version
|
|
|
|
sendcmpct.announce = False
|
|
|
|
test_node.send_and_ping(sendcmpct)
|
2017-03-30 14:38:46 +02:00
|
|
|
check_announcement_of_new_block(node, test_node, lambda p: "cmpctblock" not in p.last_message)
|
2016-09-15 03:00:53 +02:00
|
|
|
|
|
|
|
# Headers sync before next test.
|
2016-09-17 02:48:23 +02:00
|
|
|
test_node.request_headers_and_sync(locator=[tip])
|
2016-07-12 22:05:02 +02:00
|
|
|
|
|
|
|
# Finally, try a SENDCMPCT message with announce=True
|
2016-09-17 02:48:23 +02:00
|
|
|
sendcmpct.version = preferred_version
|
2016-07-12 22:05:02 +02:00
|
|
|
sendcmpct.announce = True
|
2016-09-17 02:48:23 +02:00
|
|
|
test_node.send_and_ping(sendcmpct)
|
2017-03-30 14:38:46 +02:00
|
|
|
check_announcement_of_new_block(node, test_node, lambda p: "cmpctblock" in p.last_message)
|
2016-07-12 22:05:02 +02:00
|
|
|
|
2016-09-15 03:00:53 +02:00
|
|
|
# Try one more time (no headers sync should be needed!)
|
2017-03-30 14:38:46 +02:00
|
|
|
check_announcement_of_new_block(node, test_node, lambda p: "cmpctblock" in p.last_message)
|
2016-07-12 22:05:02 +02:00
|
|
|
|
|
|
|
# Try one more time, after turning on sendheaders
|
2016-09-17 02:48:23 +02:00
|
|
|
test_node.send_and_ping(msg_sendheaders())
|
2017-03-30 14:38:46 +02:00
|
|
|
check_announcement_of_new_block(node, test_node, lambda p: "cmpctblock" in p.last_message)
|
2016-09-17 02:48:23 +02:00
|
|
|
|
|
|
|
# Try one more time, after sending a version-1, announce=false message.
|
2018-09-23 14:50:47 +02:00
|
|
|
sendcmpct.version = preferred_version - 1
|
2016-09-17 02:48:23 +02:00
|
|
|
sendcmpct.announce = False
|
|
|
|
test_node.send_and_ping(sendcmpct)
|
2017-03-30 14:38:46 +02:00
|
|
|
check_announcement_of_new_block(node, test_node, lambda p: "cmpctblock" in p.last_message)
|
2016-07-12 22:05:02 +02:00
|
|
|
|
|
|
|
# Now turn off announcements
|
2016-09-17 02:48:23 +02:00
|
|
|
sendcmpct.version = preferred_version
|
2016-07-12 22:05:02 +02:00
|
|
|
sendcmpct.announce = False
|
2016-09-17 02:48:23 +02:00
|
|
|
test_node.send_and_ping(sendcmpct)
|
2017-03-30 14:38:46 +02:00
|
|
|
check_announcement_of_new_block(node, test_node, lambda p: "cmpctblock" not in p.last_message and "headers" in p.last_message)
|
2016-09-17 02:48:23 +02:00
|
|
|
|
|
|
|
if old_node is not None:
|
|
|
|
# Verify that a peer using an older protocol version can receive
|
|
|
|
# announcements from this node.
|
2018-09-23 14:50:47 +02:00
|
|
|
sendcmpct.version = preferred_version - 1
|
2016-09-17 02:48:23 +02:00
|
|
|
sendcmpct.announce = True
|
|
|
|
old_node.send_and_ping(sendcmpct)
|
|
|
|
# Header sync
|
|
|
|
old_node.request_headers_and_sync(locator=[tip])
|
2017-03-30 14:38:46 +02:00
|
|
|
check_announcement_of_new_block(node, old_node, lambda p: "cmpctblock" in p.last_message)
|
2016-07-12 22:05:02 +02:00
|
|
|
|
2019-08-30 17:28:43 +02:00
|
|
|
# This test actually causes lbrycrdd to (reasonably!) disconnect us, so do this last.
|
2016-07-12 22:05:02 +02:00
|
|
|
def test_invalid_cmpctblock_message(self):
|
|
|
|
self.nodes[0].generate(101)
|
2016-09-17 02:48:23 +02:00
|
|
|
block = self.build_block_on_tip(self.nodes[0])
|
2016-07-12 22:05:02 +02:00
|
|
|
|
|
|
|
cmpct_block = P2PHeaderAndShortIDs()
|
|
|
|
cmpct_block.header = CBlockHeader(block)
|
|
|
|
cmpct_block.prefilled_txn_length = 1
|
|
|
|
# This index will be too high
|
|
|
|
prefilled_txn = PrefilledTransaction(1, block.vtx[0])
|
|
|
|
cmpct_block.prefilled_txn = [prefilled_txn]
|
2019-03-23 19:55:51 +01:00
|
|
|
self.segwit_node.send_await_disconnect(msg_cmpctblock(cmpct_block))
|
2017-03-29 20:16:42 +02:00
|
|
|
assert_equal(int(self.nodes[0].getbestblockhash(), 16), block.hashPrevBlock)
|
2016-07-12 22:05:02 +02:00
|
|
|
|
|
|
|
# Compare the generated shortids to what we expect based on BIP 152, given
|
2019-08-30 17:28:43 +02:00
|
|
|
# lbrycrdd's choice of nonce.
|
2019-03-23 19:55:51 +01:00
|
|
|
def test_compactblock_construction(self, test_node, use_witness_address=True):
|
|
|
|
version = test_node.cmpct_version
|
|
|
|
node = self.nodes[0]
|
2016-07-12 22:05:02 +02:00
|
|
|
# Generate a bunch of transactions.
|
2020-02-07 17:59:29 +01:00
|
|
|
node.generate(200)
|
2016-07-12 22:05:02 +02:00
|
|
|
num_transactions = 25
|
2016-09-17 02:48:23 +02:00
|
|
|
address = node.getnewaddress()
|
|
|
|
|
|
|
|
segwit_tx_generated = False
|
2016-07-12 22:05:02 +02:00
|
|
|
for i in range(num_transactions):
|
2016-09-17 02:48:23 +02:00
|
|
|
txid = node.sendtoaddress(address, 0.1)
|
|
|
|
hex_tx = node.gettransaction(txid)["hex"]
|
|
|
|
tx = FromHex(CTransaction(), hex_tx)
|
|
|
|
if not tx.wit.is_null():
|
|
|
|
segwit_tx_generated = True
|
|
|
|
|
2020-02-13 16:41:28 +01:00
|
|
|
# if use_witness_address:
|
|
|
|
# assert segwit_tx_generated # check that our test is not broken
|
2016-07-12 22:05:02 +02:00
|
|
|
|
2016-10-04 21:17:19 +02:00
|
|
|
# Wait until we've seen the block announcement for the resulting tip
|
2016-10-24 21:33:14 +02:00
|
|
|
tip = int(node.getbestblockhash(), 16)
|
2017-08-16 18:17:34 +02:00
|
|
|
test_node.wait_for_block_announcement(tip)
|
2016-10-01 02:54:30 +02:00
|
|
|
|
2017-01-04 01:28:28 +01:00
|
|
|
# Make sure we will receive a fast-announce compact block
|
2019-03-23 19:55:51 +01:00
|
|
|
self.request_cb_announcements(test_node)
|
2017-01-04 01:28:28 +01:00
|
|
|
|
2016-07-12 22:05:02 +02:00
|
|
|
# Now mine a block, and look at the resulting compact block.
|
2016-09-17 02:48:23 +02:00
|
|
|
test_node.clear_block_announcement()
|
|
|
|
block_hash = int(node.generate(1)[0], 16)
|
2016-07-12 22:05:02 +02:00
|
|
|
|
|
|
|
# Store the raw block in our internal format.
|
2018-06-08 20:16:07 +02:00
|
|
|
block = FromHex(CBlock(), node.getblock("%064x" % block_hash, False))
|
2017-07-09 22:51:47 +02:00
|
|
|
for tx in block.vtx:
|
|
|
|
tx.calc_sha256()
|
2016-07-12 22:05:02 +02:00
|
|
|
block.rehash()
|
|
|
|
|
2017-01-04 01:28:28 +01:00
|
|
|
# Wait until the block was announced (via compact blocks)
|
2017-08-16 18:17:34 +02:00
|
|
|
wait_until(test_node.received_block_announcement, timeout=30, lock=mininode_lock)
|
2016-07-12 22:05:02 +02:00
|
|
|
|
2017-01-04 01:28:28 +01:00
|
|
|
# Now fetch and check the compact block
|
|
|
|
header_and_shortids = None
|
|
|
|
with mininode_lock:
|
2019-02-19 23:43:44 +01:00
|
|
|
assert "cmpctblock" in test_node.last_message
|
2017-01-04 01:28:28 +01:00
|
|
|
# Convert the on-the-wire representation to absolute indexes
|
2017-03-30 14:38:46 +02:00
|
|
|
header_and_shortids = HeaderAndShortIDs(test_node.last_message["cmpctblock"].header_and_shortids)
|
2017-01-04 01:28:28 +01:00
|
|
|
self.check_compactblock_construction_from_block(version, header_and_shortids, block_hash, block)
|
|
|
|
|
|
|
|
# Now fetch the compact block using a normal non-announce getdata
|
2016-07-12 22:05:02 +02:00
|
|
|
with mininode_lock:
|
2017-01-04 01:28:28 +01:00
|
|
|
test_node.clear_block_announcement()
|
|
|
|
inv = CInv(4, block_hash) # 4 == "CompactBlock"
|
|
|
|
test_node.send_message(msg_getdata([inv]))
|
2016-07-12 22:05:02 +02:00
|
|
|
|
2017-08-16 18:17:34 +02:00
|
|
|
wait_until(test_node.received_block_announcement, timeout=30, lock=mininode_lock)
|
2016-07-12 22:05:02 +02:00
|
|
|
|
2017-01-04 01:28:28 +01:00
|
|
|
# Now fetch and check the compact block
|
2016-07-12 22:05:02 +02:00
|
|
|
header_and_shortids = None
|
|
|
|
with mininode_lock:
|
2019-02-19 23:43:44 +01:00
|
|
|
assert "cmpctblock" in test_node.last_message
|
2016-07-12 22:05:02 +02:00
|
|
|
# Convert the on-the-wire representation to absolute indexes
|
2017-03-30 14:38:46 +02:00
|
|
|
header_and_shortids = HeaderAndShortIDs(test_node.last_message["cmpctblock"].header_and_shortids)
|
2017-01-04 01:28:28 +01:00
|
|
|
self.check_compactblock_construction_from_block(version, header_and_shortids, block_hash, block)
|
2016-07-12 22:05:02 +02:00
|
|
|
|
2017-01-04 01:28:28 +01:00
|
|
|
def check_compactblock_construction_from_block(self, version, header_and_shortids, block_hash, block):
|
2016-07-12 22:05:02 +02:00
|
|
|
# Check that we got the right block!
|
|
|
|
header_and_shortids.header.calc_sha256()
|
|
|
|
assert_equal(header_and_shortids.header.sha256, block_hash)
|
|
|
|
|
|
|
|
# Make sure the prefilled_txn appears to have included the coinbase
|
2019-02-19 23:43:44 +01:00
|
|
|
assert len(header_and_shortids.prefilled_txn) >= 1
|
2016-07-12 22:05:02 +02:00
|
|
|
assert_equal(header_and_shortids.prefilled_txn[0].index, 0)
|
|
|
|
|
|
|
|
# Check that all prefilled_txn entries match what's in the block.
|
|
|
|
for entry in header_and_shortids.prefilled_txn:
|
|
|
|
entry.tx.calc_sha256()
|
2016-09-17 02:48:23 +02:00
|
|
|
# This checks the non-witness parts of the tx agree
|
2016-07-12 22:05:02 +02:00
|
|
|
assert_equal(entry.tx.sha256, block.vtx[entry.index].sha256)
|
|
|
|
|
2016-09-17 02:48:23 +02:00
|
|
|
# And this checks the witness
|
|
|
|
wtxid = entry.tx.calc_sha256(True)
|
|
|
|
if version == 2:
|
|
|
|
assert_equal(wtxid, block.vtx[entry.index].calc_sha256(True))
|
|
|
|
else:
|
|
|
|
# Shouldn't have received a witness
|
2019-02-19 23:43:44 +01:00
|
|
|
assert entry.tx.wit.is_null()
|
2016-09-17 02:48:23 +02:00
|
|
|
|
2016-07-12 22:05:02 +02:00
|
|
|
# Check that the cmpctblock message announced all the transactions.
|
|
|
|
assert_equal(len(header_and_shortids.prefilled_txn) + len(header_and_shortids.shortids), len(block.vtx))
|
|
|
|
|
|
|
|
# And now check that all the shortids are as expected as well.
|
|
|
|
# Determine the siphash keys to use.
|
|
|
|
[k0, k1] = header_and_shortids.get_siphash_keys()
|
|
|
|
|
|
|
|
index = 0
|
|
|
|
while index < len(block.vtx):
|
|
|
|
if (len(header_and_shortids.prefilled_txn) > 0 and
|
|
|
|
header_and_shortids.prefilled_txn[0].index == index):
|
|
|
|
# Already checked prefilled transactions above
|
|
|
|
header_and_shortids.prefilled_txn.pop(0)
|
|
|
|
else:
|
2016-09-17 02:48:23 +02:00
|
|
|
tx_hash = block.vtx[index].sha256
|
|
|
|
if version == 2:
|
|
|
|
tx_hash = block.vtx[index].calc_sha256(True)
|
|
|
|
shortid = calculate_shortid(k0, k1, tx_hash)
|
2016-07-12 22:05:02 +02:00
|
|
|
assert_equal(shortid, header_and_shortids.shortids[0])
|
|
|
|
header_and_shortids.shortids.pop(0)
|
|
|
|
index += 1
|
|
|
|
|
2019-08-30 17:28:43 +02:00
|
|
|
# Test that lbrycrdd requests compact blocks when we announce new blocks
|
2016-07-12 22:05:02 +02:00
|
|
|
# via header or inv, and that responding to getblocktxn causes the block
|
|
|
|
# to be successfully reconstructed.
|
2016-09-17 02:48:23 +02:00
|
|
|
# Post-segwit: upgraded nodes would only make this request of cb-version-2,
|
|
|
|
# NODE_WITNESS peers. Unupgraded nodes would still make this request of
|
|
|
|
# any cb-version-1-supporting peer.
|
2019-03-23 19:55:51 +01:00
|
|
|
def test_compactblock_requests(self, test_node, segwit=True):
|
|
|
|
version = test_node.cmpct_version
|
|
|
|
node = self.nodes[0]
|
2016-07-12 22:05:02 +02:00
|
|
|
# Try announcing a block with an inv or header, expect a compactblock
|
|
|
|
# request
|
|
|
|
for announce in ["inv", "header"]:
|
2016-10-14 18:54:15 +02:00
|
|
|
block = self.build_block_on_tip(node, segwit=segwit)
|
2016-07-12 22:05:02 +02:00
|
|
|
with mininode_lock:
|
2017-03-30 14:38:46 +02:00
|
|
|
test_node.last_message.pop("getdata", None)
|
2016-07-12 22:05:02 +02:00
|
|
|
|
|
|
|
if announce == "inv":
|
2016-09-17 02:48:23 +02:00
|
|
|
test_node.send_message(msg_inv([CInv(2, block.sha256)]))
|
2017-08-16 18:17:34 +02:00
|
|
|
wait_until(lambda: "getheaders" in test_node.last_message, timeout=30, lock=mininode_lock)
|
2016-10-04 00:30:52 +02:00
|
|
|
test_node.send_header_for_blocks([block])
|
2016-07-12 22:05:02 +02:00
|
|
|
else:
|
2016-09-17 02:48:23 +02:00
|
|
|
test_node.send_header_for_blocks([block])
|
2017-08-16 18:17:34 +02:00
|
|
|
wait_until(lambda: "getdata" in test_node.last_message, timeout=30, lock=mininode_lock)
|
2017-03-30 14:38:46 +02:00
|
|
|
assert_equal(len(test_node.last_message["getdata"].inv), 1)
|
|
|
|
assert_equal(test_node.last_message["getdata"].inv[0].type, 4)
|
|
|
|
assert_equal(test_node.last_message["getdata"].inv[0].hash, block.sha256)
|
2016-07-12 22:05:02 +02:00
|
|
|
|
|
|
|
# Send back a compactblock message that omits the coinbase
|
|
|
|
comp_block = HeaderAndShortIDs()
|
|
|
|
comp_block.header = CBlockHeader(block)
|
|
|
|
comp_block.nonce = 0
|
2016-10-09 21:47:55 +02:00
|
|
|
[k0, k1] = comp_block.get_siphash_keys()
|
2016-10-14 18:54:15 +02:00
|
|
|
coinbase_hash = block.vtx[0].sha256
|
|
|
|
if version == 2:
|
|
|
|
coinbase_hash = block.vtx[0].calc_sha256(True)
|
2018-09-23 14:50:47 +02:00
|
|
|
comp_block.shortids = [calculate_shortid(k0, k1, coinbase_hash)]
|
2016-09-17 02:48:23 +02:00
|
|
|
test_node.send_and_ping(msg_cmpctblock(comp_block.to_p2p()))
|
|
|
|
assert_equal(int(node.getbestblockhash(), 16), block.hashPrevBlock)
|
2016-07-12 22:05:02 +02:00
|
|
|
# Expect a getblocktxn message.
|
|
|
|
with mininode_lock:
|
2019-02-19 23:43:44 +01:00
|
|
|
assert "getblocktxn" in test_node.last_message
|
2017-03-30 14:38:46 +02:00
|
|
|
absolute_indexes = test_node.last_message["getblocktxn"].block_txn_request.to_absolute()
|
2016-07-12 22:05:02 +02:00
|
|
|
assert_equal(absolute_indexes, [0]) # should be a coinbase request
|
|
|
|
|
|
|
|
# Send the coinbase, and verify that the tip advances.
|
2016-10-14 18:54:15 +02:00
|
|
|
if version == 2:
|
|
|
|
msg = msg_blocktxn()
|
tests: Make msg_block a witness block
This diff has been generated with the following script, but is better
reviewed without looking at the script.
# -BEGIN VERIFY SCRIPT-
echo "Use msg_witness_block everywhere, except for tests that require msg_block"
# This could be a separate commit, but it is combined with the
# following scripts to reduce the overall diff
sed -i -e 's/msg_block/msg_witness_block/g' ./test/functional/{feature_assumevalid,feature_cltv,feature_dersig,feature_versionbits_warning,p2p_fingerprint,p2p_sendheaders,p2p_unrequested_blocks,example_test,rpc_blockchain}.py
echo "Rename msg_block to msg_no_witness_block"
# Rename msg_block to msg_no_witness_block in all tests (not the
# framework)
sed -i -e 's/msg_block/msg_no_witness_block/g' $(git grep -l msg_block ./test/functional/*.py)
# Derive msg_no_witness_block from msg_block
# Make msg_block a witness block in messages.py
patch -p1 --fuzz 0 << EOF
diff --git a/test/functional/test_framework/messages.py b/test/functional/test_framework/messages.py
index 00190e4cbd..e454ed5987 100755
--- a/test/functional/test_framework/messages.py
+++ b/test/functional/test_framework/messages.py
@@ -1133 +1133 @@ class msg_block:
- return self.block.serialize(with_witness=False)
+ return self.block.serialize()
@@ -1155 +1155 @@ class msg_generic:
-class msg_witness_block(msg_block):
+class msg_no_witness_block(msg_block):
@@ -1158,2 +1158 @@ class msg_witness_block(msg_block):
- r = self.block.serialize()
- return r
+ return self.block.serialize(with_witness=False)
@@ -1445 +1444 @@ class msg_blocktxn:
- r += self.block_transactions.serialize(with_witness=False)
+ r += self.block_transactions.serialize()
@@ -1452 +1451 @@ class msg_blocktxn:
-class msg_witness_blocktxn(msg_blocktxn):
+class msg_no_witness_blocktxn(msg_blocktxn):
@@ -1456,3 +1455 @@ class msg_witness_blocktxn(msg_blocktxn):
- r = b""
- r += self.block_transactions.serialize()
- return r
+ return self.block_transactions.serialize(with_witness=False)
EOF
# Conclude rename of msg_block to msg_no_witness_block
sed -i -e 's/msg_witness_block/msg_block/g' $(git grep -l msg_witness_block)
# -END VERIFY SCRIPT-
2019-05-08 16:21:25 +02:00
|
|
|
else:
|
|
|
|
msg = msg_no_witness_blocktxn()
|
2016-07-12 22:05:02 +02:00
|
|
|
msg.block_transactions.blockhash = block.sha256
|
|
|
|
msg.block_transactions.transactions = [block.vtx[0]]
|
2016-09-17 02:48:23 +02:00
|
|
|
test_node.send_and_ping(msg)
|
|
|
|
assert_equal(int(node.getbestblockhash(), 16), block.sha256)
|
2016-07-12 22:05:02 +02:00
|
|
|
|
|
|
|
# Create a chain of transactions from given utxo, and add to a new block.
|
2016-09-17 02:48:23 +02:00
|
|
|
def build_block_with_transactions(self, node, utxo, num_transactions):
|
|
|
|
block = self.build_block_on_tip(node)
|
2016-07-12 22:05:02 +02:00
|
|
|
|
|
|
|
for i in range(num_transactions):
|
|
|
|
tx = CTransaction()
|
|
|
|
tx.vin.append(CTxIn(COutPoint(utxo[0], utxo[1]), b''))
|
2018-05-03 22:50:25 +02:00
|
|
|
tx.vout.append(CTxOut(utxo[2] - 1000, CScript([OP_TRUE, OP_DROP] * 15 + [OP_TRUE])))
|
2016-07-12 22:05:02 +02:00
|
|
|
tx.rehash()
|
|
|
|
utxo = [tx.sha256, 0, tx.vout[0].nValue]
|
|
|
|
block.vtx.append(tx)
|
|
|
|
|
|
|
|
block.hashMerkleRoot = block.calc_merkle_root()
|
|
|
|
block.solve()
|
|
|
|
return block
|
|
|
|
|
|
|
|
# Test that we only receive getblocktxn requests for transactions that the
|
|
|
|
# node needs, and that responding to them causes the block to be
|
|
|
|
# reconstructed.
|
2019-03-23 19:55:51 +01:00
|
|
|
def test_getblocktxn_requests(self, test_node):
|
|
|
|
version = test_node.cmpct_version
|
|
|
|
node = self.nodes[0]
|
2018-09-23 14:50:47 +02:00
|
|
|
with_witness = (version == 2)
|
2016-09-17 02:48:23 +02:00
|
|
|
|
|
|
|
def test_getblocktxn_response(compact_block, peer, expected_result):
|
|
|
|
msg = msg_cmpctblock(compact_block.to_p2p())
|
|
|
|
peer.send_and_ping(msg)
|
|
|
|
with mininode_lock:
|
2019-02-19 23:43:44 +01:00
|
|
|
assert "getblocktxn" in peer.last_message
|
2017-03-30 14:38:46 +02:00
|
|
|
absolute_indexes = peer.last_message["getblocktxn"].block_txn_request.to_absolute()
|
2016-09-17 02:48:23 +02:00
|
|
|
assert_equal(absolute_indexes, expected_result)
|
|
|
|
|
|
|
|
def test_tip_after_message(node, peer, msg, tip):
|
|
|
|
peer.send_and_ping(msg)
|
|
|
|
assert_equal(int(node.getbestblockhash(), 16), tip)
|
2016-07-12 22:05:02 +02:00
|
|
|
|
|
|
|
# First try announcing compactblocks that won't reconstruct, and verify
|
|
|
|
# that we receive getblocktxn messages back.
|
|
|
|
utxo = self.utxos.pop(0)
|
|
|
|
|
2016-09-17 02:48:23 +02:00
|
|
|
block = self.build_block_with_transactions(node, utxo, 5)
|
2016-07-12 22:05:02 +02:00
|
|
|
self.utxos.append([block.vtx[-1].sha256, 0, block.vtx[-1].vout[0].nValue])
|
|
|
|
comp_block = HeaderAndShortIDs()
|
2016-09-17 02:48:23 +02:00
|
|
|
comp_block.initialize_from_block(block, use_witness=with_witness)
|
2016-07-12 22:05:02 +02:00
|
|
|
|
2016-09-17 02:48:23 +02:00
|
|
|
test_getblocktxn_response(comp_block, test_node, [1, 2, 3, 4, 5])
|
|
|
|
|
tests: Make msg_block a witness block
This diff has been generated with the following script, but is better
reviewed without looking at the script.
# -BEGIN VERIFY SCRIPT-
echo "Use msg_witness_block everywhere, except for tests that require msg_block"
# This could be a separate commit, but it is combined with the
# following scripts to reduce the overall diff
sed -i -e 's/msg_block/msg_witness_block/g' ./test/functional/{feature_assumevalid,feature_cltv,feature_dersig,feature_versionbits_warning,p2p_fingerprint,p2p_sendheaders,p2p_unrequested_blocks,example_test,rpc_blockchain}.py
echo "Rename msg_block to msg_no_witness_block"
# Rename msg_block to msg_no_witness_block in all tests (not the
# framework)
sed -i -e 's/msg_block/msg_no_witness_block/g' $(git grep -l msg_block ./test/functional/*.py)
# Derive msg_no_witness_block from msg_block
# Make msg_block a witness block in messages.py
patch -p1 --fuzz 0 << EOF
diff --git a/test/functional/test_framework/messages.py b/test/functional/test_framework/messages.py
index 00190e4cbd..e454ed5987 100755
--- a/test/functional/test_framework/messages.py
+++ b/test/functional/test_framework/messages.py
@@ -1133 +1133 @@ class msg_block:
- return self.block.serialize(with_witness=False)
+ return self.block.serialize()
@@ -1155 +1155 @@ class msg_generic:
-class msg_witness_block(msg_block):
+class msg_no_witness_block(msg_block):
@@ -1158,2 +1158 @@ class msg_witness_block(msg_block):
- r = self.block.serialize()
- return r
+ return self.block.serialize(with_witness=False)
@@ -1445 +1444 @@ class msg_blocktxn:
- r += self.block_transactions.serialize(with_witness=False)
+ r += self.block_transactions.serialize()
@@ -1452 +1451 @@ class msg_blocktxn:
-class msg_witness_blocktxn(msg_blocktxn):
+class msg_no_witness_blocktxn(msg_blocktxn):
@@ -1456,3 +1455 @@ class msg_witness_blocktxn(msg_blocktxn):
- r = b""
- r += self.block_transactions.serialize()
- return r
+ return self.block_transactions.serialize(with_witness=False)
EOF
# Conclude rename of msg_block to msg_no_witness_block
sed -i -e 's/msg_witness_block/msg_block/g' $(git grep -l msg_witness_block)
# -END VERIFY SCRIPT-
2019-05-08 16:21:25 +02:00
|
|
|
msg_bt = msg_no_witness_blocktxn()
|
2016-09-17 02:48:23 +02:00
|
|
|
if with_witness:
|
tests: Make msg_block a witness block
This diff has been generated with the following script, but is better
reviewed without looking at the script.
# -BEGIN VERIFY SCRIPT-
echo "Use msg_witness_block everywhere, except for tests that require msg_block"
# This could be a separate commit, but it is combined with the
# following scripts to reduce the overall diff
sed -i -e 's/msg_block/msg_witness_block/g' ./test/functional/{feature_assumevalid,feature_cltv,feature_dersig,feature_versionbits_warning,p2p_fingerprint,p2p_sendheaders,p2p_unrequested_blocks,example_test,rpc_blockchain}.py
echo "Rename msg_block to msg_no_witness_block"
# Rename msg_block to msg_no_witness_block in all tests (not the
# framework)
sed -i -e 's/msg_block/msg_no_witness_block/g' $(git grep -l msg_block ./test/functional/*.py)
# Derive msg_no_witness_block from msg_block
# Make msg_block a witness block in messages.py
patch -p1 --fuzz 0 << EOF
diff --git a/test/functional/test_framework/messages.py b/test/functional/test_framework/messages.py
index 00190e4cbd..e454ed5987 100755
--- a/test/functional/test_framework/messages.py
+++ b/test/functional/test_framework/messages.py
@@ -1133 +1133 @@ class msg_block:
- return self.block.serialize(with_witness=False)
+ return self.block.serialize()
@@ -1155 +1155 @@ class msg_generic:
-class msg_witness_block(msg_block):
+class msg_no_witness_block(msg_block):
@@ -1158,2 +1158 @@ class msg_witness_block(msg_block):
- r = self.block.serialize()
- return r
+ return self.block.serialize(with_witness=False)
@@ -1445 +1444 @@ class msg_blocktxn:
- r += self.block_transactions.serialize(with_witness=False)
+ r += self.block_transactions.serialize()
@@ -1452 +1451 @@ class msg_blocktxn:
-class msg_witness_blocktxn(msg_blocktxn):
+class msg_no_witness_blocktxn(msg_blocktxn):
@@ -1456,3 +1455 @@ class msg_witness_blocktxn(msg_blocktxn):
- r = b""
- r += self.block_transactions.serialize()
- return r
+ return self.block_transactions.serialize(with_witness=False)
EOF
# Conclude rename of msg_block to msg_no_witness_block
sed -i -e 's/msg_witness_block/msg_block/g' $(git grep -l msg_witness_block)
# -END VERIFY SCRIPT-
2019-05-08 16:21:25 +02:00
|
|
|
msg_bt = msg_blocktxn() # serialize with witnesses
|
2016-09-17 02:48:23 +02:00
|
|
|
msg_bt.block_transactions = BlockTransactions(block.sha256, block.vtx[1:])
|
|
|
|
test_tip_after_message(node, test_node, msg_bt, block.sha256)
|
2016-07-12 22:05:02 +02:00
|
|
|
|
|
|
|
utxo = self.utxos.pop(0)
|
2016-09-17 02:48:23 +02:00
|
|
|
block = self.build_block_with_transactions(node, utxo, 5)
|
2016-07-12 22:05:02 +02:00
|
|
|
self.utxos.append([block.vtx[-1].sha256, 0, block.vtx[-1].vout[0].nValue])
|
|
|
|
|
|
|
|
# Now try interspersing the prefilled transactions
|
2016-09-17 02:48:23 +02:00
|
|
|
comp_block.initialize_from_block(block, prefill_list=[0, 1, 5], use_witness=with_witness)
|
|
|
|
test_getblocktxn_response(comp_block, test_node, [2, 3, 4])
|
|
|
|
msg_bt.block_transactions = BlockTransactions(block.sha256, block.vtx[2:5])
|
|
|
|
test_tip_after_message(node, test_node, msg_bt, block.sha256)
|
2016-07-12 22:05:02 +02:00
|
|
|
|
|
|
|
# Now try giving one transaction ahead of time.
|
|
|
|
utxo = self.utxos.pop(0)
|
2016-09-17 02:48:23 +02:00
|
|
|
block = self.build_block_with_transactions(node, utxo, 5)
|
2016-07-12 22:05:02 +02:00
|
|
|
self.utxos.append([block.vtx[-1].sha256, 0, block.vtx[-1].vout[0].nValue])
|
2016-09-17 02:48:23 +02:00
|
|
|
test_node.send_and_ping(msg_tx(block.vtx[1]))
|
2019-02-19 23:43:44 +01:00
|
|
|
assert block.vtx[1].hash in node.getrawmempool()
|
2016-07-12 22:05:02 +02:00
|
|
|
|
|
|
|
# Prefill 4 out of the 6 transactions, and verify that only the one
|
|
|
|
# that was not in the mempool is requested.
|
2016-09-17 02:48:23 +02:00
|
|
|
comp_block.initialize_from_block(block, prefill_list=[0, 2, 3, 4], use_witness=with_witness)
|
|
|
|
test_getblocktxn_response(comp_block, test_node, [5])
|
2016-07-12 22:05:02 +02:00
|
|
|
|
2016-09-17 02:48:23 +02:00
|
|
|
msg_bt.block_transactions = BlockTransactions(block.sha256, [block.vtx[5]])
|
|
|
|
test_tip_after_message(node, test_node, msg_bt, block.sha256)
|
2016-07-12 22:05:02 +02:00
|
|
|
|
|
|
|
# Now provide all transactions to the node before the block is
|
|
|
|
# announced and verify reconstruction happens immediately.
|
|
|
|
utxo = self.utxos.pop(0)
|
2016-09-17 02:48:23 +02:00
|
|
|
block = self.build_block_with_transactions(node, utxo, 10)
|
2016-07-12 22:05:02 +02:00
|
|
|
self.utxos.append([block.vtx[-1].sha256, 0, block.vtx[-1].vout[0].nValue])
|
|
|
|
for tx in block.vtx[1:]:
|
2016-09-17 02:48:23 +02:00
|
|
|
test_node.send_message(msg_tx(tx))
|
|
|
|
test_node.sync_with_ping()
|
2016-07-12 22:05:02 +02:00
|
|
|
# Make sure all transactions were accepted.
|
2016-09-17 02:48:23 +02:00
|
|
|
mempool = node.getrawmempool()
|
2016-07-12 22:05:02 +02:00
|
|
|
for tx in block.vtx[1:]:
|
2019-02-19 23:43:44 +01:00
|
|
|
assert tx.hash in mempool
|
2016-07-12 22:05:02 +02:00
|
|
|
|
|
|
|
# Clear out last request.
|
|
|
|
with mininode_lock:
|
2017-03-30 14:38:46 +02:00
|
|
|
test_node.last_message.pop("getblocktxn", None)
|
2016-07-12 22:05:02 +02:00
|
|
|
|
|
|
|
# Send compact block
|
2016-09-17 02:48:23 +02:00
|
|
|
comp_block.initialize_from_block(block, prefill_list=[0], use_witness=with_witness)
|
|
|
|
test_tip_after_message(node, test_node, msg_cmpctblock(comp_block.to_p2p()), block.sha256)
|
2016-07-12 22:05:02 +02:00
|
|
|
with mininode_lock:
|
|
|
|
# Shouldn't have gotten a request for any transaction
|
2019-02-19 23:43:44 +01:00
|
|
|
assert "getblocktxn" not in test_node.last_message
|
2016-07-12 22:05:02 +02:00
|
|
|
|
|
|
|
# Incorrectly responding to a getblocktxn shouldn't cause the block to be
|
|
|
|
# permanently failed.
|
2019-03-23 19:55:51 +01:00
|
|
|
def test_incorrect_blocktxn_response(self, test_node):
|
|
|
|
version = test_node.cmpct_version
|
|
|
|
node = self.nodes[0]
|
2016-07-12 22:05:02 +02:00
|
|
|
utxo = self.utxos.pop(0)
|
|
|
|
|
2016-09-17 02:48:23 +02:00
|
|
|
block = self.build_block_with_transactions(node, utxo, 10)
|
2016-07-12 22:05:02 +02:00
|
|
|
self.utxos.append([block.vtx[-1].sha256, 0, block.vtx[-1].vout[0].nValue])
|
|
|
|
# Relay the first 5 transactions from the block in advance
|
|
|
|
for tx in block.vtx[1:6]:
|
2016-09-17 02:48:23 +02:00
|
|
|
test_node.send_message(msg_tx(tx))
|
|
|
|
test_node.sync_with_ping()
|
2016-07-12 22:05:02 +02:00
|
|
|
# Make sure all transactions were accepted.
|
2016-09-17 02:48:23 +02:00
|
|
|
mempool = node.getrawmempool()
|
2016-07-12 22:05:02 +02:00
|
|
|
for tx in block.vtx[1:6]:
|
2019-02-19 23:43:44 +01:00
|
|
|
assert tx.hash in mempool
|
2016-07-12 22:05:02 +02:00
|
|
|
|
|
|
|
# Send compact block
|
|
|
|
comp_block = HeaderAndShortIDs()
|
2016-09-17 02:48:23 +02:00
|
|
|
comp_block.initialize_from_block(block, prefill_list=[0], use_witness=(version == 2))
|
|
|
|
test_node.send_and_ping(msg_cmpctblock(comp_block.to_p2p()))
|
2016-07-12 22:05:02 +02:00
|
|
|
absolute_indexes = []
|
|
|
|
with mininode_lock:
|
2019-02-19 23:43:44 +01:00
|
|
|
assert "getblocktxn" in test_node.last_message
|
2017-03-30 14:38:46 +02:00
|
|
|
absolute_indexes = test_node.last_message["getblocktxn"].block_txn_request.to_absolute()
|
2016-07-12 22:05:02 +02:00
|
|
|
assert_equal(absolute_indexes, [6, 7, 8, 9, 10])
|
|
|
|
|
|
|
|
# Now give an incorrect response.
|
2019-08-30 17:28:43 +02:00
|
|
|
# Note that it's possible for lbrycrdd to be smart enough to know we're
|
2016-07-12 22:05:02 +02:00
|
|
|
# lying, since it could check to see if the shortid matches what we're
|
|
|
|
# sending, and eg disconnect us for misbehavior. If that behavior
|
2018-03-18 15:26:45 +01:00
|
|
|
# change was made, we could just modify this test by having a
|
2016-07-12 22:05:02 +02:00
|
|
|
# different peer provide the block further down, so that we're still
|
|
|
|
# verifying that the block isn't marked bad permanently. This is good
|
|
|
|
# enough for now.
|
tests: Make msg_block a witness block
This diff has been generated with the following script, but is better
reviewed without looking at the script.
# -BEGIN VERIFY SCRIPT-
echo "Use msg_witness_block everywhere, except for tests that require msg_block"
# This could be a separate commit, but it is combined with the
# following scripts to reduce the overall diff
sed -i -e 's/msg_block/msg_witness_block/g' ./test/functional/{feature_assumevalid,feature_cltv,feature_dersig,feature_versionbits_warning,p2p_fingerprint,p2p_sendheaders,p2p_unrequested_blocks,example_test,rpc_blockchain}.py
echo "Rename msg_block to msg_no_witness_block"
# Rename msg_block to msg_no_witness_block in all tests (not the
# framework)
sed -i -e 's/msg_block/msg_no_witness_block/g' $(git grep -l msg_block ./test/functional/*.py)
# Derive msg_no_witness_block from msg_block
# Make msg_block a witness block in messages.py
patch -p1 --fuzz 0 << EOF
diff --git a/test/functional/test_framework/messages.py b/test/functional/test_framework/messages.py
index 00190e4cbd..e454ed5987 100755
--- a/test/functional/test_framework/messages.py
+++ b/test/functional/test_framework/messages.py
@@ -1133 +1133 @@ class msg_block:
- return self.block.serialize(with_witness=False)
+ return self.block.serialize()
@@ -1155 +1155 @@ class msg_generic:
-class msg_witness_block(msg_block):
+class msg_no_witness_block(msg_block):
@@ -1158,2 +1158 @@ class msg_witness_block(msg_block):
- r = self.block.serialize()
- return r
+ return self.block.serialize(with_witness=False)
@@ -1445 +1444 @@ class msg_blocktxn:
- r += self.block_transactions.serialize(with_witness=False)
+ r += self.block_transactions.serialize()
@@ -1452 +1451 @@ class msg_blocktxn:
-class msg_witness_blocktxn(msg_blocktxn):
+class msg_no_witness_blocktxn(msg_blocktxn):
@@ -1456,3 +1455 @@ class msg_witness_blocktxn(msg_blocktxn):
- r = b""
- r += self.block_transactions.serialize()
- return r
+ return self.block_transactions.serialize(with_witness=False)
EOF
# Conclude rename of msg_block to msg_no_witness_block
sed -i -e 's/msg_witness_block/msg_block/g' $(git grep -l msg_witness_block)
# -END VERIFY SCRIPT-
2019-05-08 16:21:25 +02:00
|
|
|
msg = msg_no_witness_blocktxn()
|
2018-09-23 14:50:47 +02:00
|
|
|
if version == 2:
|
tests: Make msg_block a witness block
This diff has been generated with the following script, but is better
reviewed without looking at the script.
# -BEGIN VERIFY SCRIPT-
echo "Use msg_witness_block everywhere, except for tests that require msg_block"
# This could be a separate commit, but it is combined with the
# following scripts to reduce the overall diff
sed -i -e 's/msg_block/msg_witness_block/g' ./test/functional/{feature_assumevalid,feature_cltv,feature_dersig,feature_versionbits_warning,p2p_fingerprint,p2p_sendheaders,p2p_unrequested_blocks,example_test,rpc_blockchain}.py
echo "Rename msg_block to msg_no_witness_block"
# Rename msg_block to msg_no_witness_block in all tests (not the
# framework)
sed -i -e 's/msg_block/msg_no_witness_block/g' $(git grep -l msg_block ./test/functional/*.py)
# Derive msg_no_witness_block from msg_block
# Make msg_block a witness block in messages.py
patch -p1 --fuzz 0 << EOF
diff --git a/test/functional/test_framework/messages.py b/test/functional/test_framework/messages.py
index 00190e4cbd..e454ed5987 100755
--- a/test/functional/test_framework/messages.py
+++ b/test/functional/test_framework/messages.py
@@ -1133 +1133 @@ class msg_block:
- return self.block.serialize(with_witness=False)
+ return self.block.serialize()
@@ -1155 +1155 @@ class msg_generic:
-class msg_witness_block(msg_block):
+class msg_no_witness_block(msg_block):
@@ -1158,2 +1158 @@ class msg_witness_block(msg_block):
- r = self.block.serialize()
- return r
+ return self.block.serialize(with_witness=False)
@@ -1445 +1444 @@ class msg_blocktxn:
- r += self.block_transactions.serialize(with_witness=False)
+ r += self.block_transactions.serialize()
@@ -1452 +1451 @@ class msg_blocktxn:
-class msg_witness_blocktxn(msg_blocktxn):
+class msg_no_witness_blocktxn(msg_blocktxn):
@@ -1456,3 +1455 @@ class msg_witness_blocktxn(msg_blocktxn):
- r = b""
- r += self.block_transactions.serialize()
- return r
+ return self.block_transactions.serialize(with_witness=False)
EOF
# Conclude rename of msg_block to msg_no_witness_block
sed -i -e 's/msg_witness_block/msg_block/g' $(git grep -l msg_witness_block)
# -END VERIFY SCRIPT-
2019-05-08 16:21:25 +02:00
|
|
|
msg = msg_blocktxn()
|
2016-07-12 22:05:02 +02:00
|
|
|
msg.block_transactions = BlockTransactions(block.sha256, [block.vtx[5]] + block.vtx[7:])
|
2016-09-17 02:48:23 +02:00
|
|
|
test_node.send_and_ping(msg)
|
2016-07-12 22:05:02 +02:00
|
|
|
|
|
|
|
# Tip should not have updated
|
2016-09-17 02:48:23 +02:00
|
|
|
assert_equal(int(node.getbestblockhash(), 16), block.hashPrevBlock)
|
2016-07-12 22:05:02 +02:00
|
|
|
|
|
|
|
# We should receive a getdata request
|
2017-08-16 18:17:34 +02:00
|
|
|
wait_until(lambda: "getdata" in test_node.last_message, timeout=10, lock=mininode_lock)
|
2017-03-30 14:38:46 +02:00
|
|
|
assert_equal(len(test_node.last_message["getdata"].inv), 1)
|
2019-02-19 23:43:44 +01:00
|
|
|
assert test_node.last_message["getdata"].inv[0].type == 2 or test_node.last_message["getdata"].inv[0].type == 2 | MSG_WITNESS_FLAG
|
2017-03-30 14:38:46 +02:00
|
|
|
assert_equal(test_node.last_message["getdata"].inv[0].hash, block.sha256)
|
2016-07-12 22:05:02 +02:00
|
|
|
|
|
|
|
# Deliver the block
|
2018-09-23 14:50:47 +02:00
|
|
|
if version == 2:
|
2016-09-17 02:48:23 +02:00
|
|
|
test_node.send_and_ping(msg_block(block))
|
tests: Make msg_block a witness block
This diff has been generated with the following script, but is better
reviewed without looking at the script.
# -BEGIN VERIFY SCRIPT-
echo "Use msg_witness_block everywhere, except for tests that require msg_block"
# This could be a separate commit, but it is combined with the
# following scripts to reduce the overall diff
sed -i -e 's/msg_block/msg_witness_block/g' ./test/functional/{feature_assumevalid,feature_cltv,feature_dersig,feature_versionbits_warning,p2p_fingerprint,p2p_sendheaders,p2p_unrequested_blocks,example_test,rpc_blockchain}.py
echo "Rename msg_block to msg_no_witness_block"
# Rename msg_block to msg_no_witness_block in all tests (not the
# framework)
sed -i -e 's/msg_block/msg_no_witness_block/g' $(git grep -l msg_block ./test/functional/*.py)
# Derive msg_no_witness_block from msg_block
# Make msg_block a witness block in messages.py
patch -p1 --fuzz 0 << EOF
diff --git a/test/functional/test_framework/messages.py b/test/functional/test_framework/messages.py
index 00190e4cbd..e454ed5987 100755
--- a/test/functional/test_framework/messages.py
+++ b/test/functional/test_framework/messages.py
@@ -1133 +1133 @@ class msg_block:
- return self.block.serialize(with_witness=False)
+ return self.block.serialize()
@@ -1155 +1155 @@ class msg_generic:
-class msg_witness_block(msg_block):
+class msg_no_witness_block(msg_block):
@@ -1158,2 +1158 @@ class msg_witness_block(msg_block):
- r = self.block.serialize()
- return r
+ return self.block.serialize(with_witness=False)
@@ -1445 +1444 @@ class msg_blocktxn:
- r += self.block_transactions.serialize(with_witness=False)
+ r += self.block_transactions.serialize()
@@ -1452 +1451 @@ class msg_blocktxn:
-class msg_witness_blocktxn(msg_blocktxn):
+class msg_no_witness_blocktxn(msg_blocktxn):
@@ -1456,3 +1455 @@ class msg_witness_blocktxn(msg_blocktxn):
- r = b""
- r += self.block_transactions.serialize()
- return r
+ return self.block_transactions.serialize(with_witness=False)
EOF
# Conclude rename of msg_block to msg_no_witness_block
sed -i -e 's/msg_witness_block/msg_block/g' $(git grep -l msg_witness_block)
# -END VERIFY SCRIPT-
2019-05-08 16:21:25 +02:00
|
|
|
else:
|
|
|
|
test_node.send_and_ping(msg_no_witness_block(block))
|
2016-09-17 02:48:23 +02:00
|
|
|
assert_equal(int(node.getbestblockhash(), 16), block.sha256)
|
2016-07-12 22:05:02 +02:00
|
|
|
|
2019-03-23 19:55:51 +01:00
|
|
|
def test_getblocktxn_handler(self, test_node):
|
2019-08-30 17:28:43 +02:00
|
|
|
# lbrycrdd will not send blocktxn responses for blocks whose height is
|
2019-03-23 19:55:51 +01:00
|
|
|
version = test_node.cmpct_version
|
|
|
|
node = self.nodes[0]
|
2016-10-26 20:53:18 +02:00
|
|
|
# more than 10 blocks deep.
|
2016-06-22 01:28:38 +02:00
|
|
|
MAX_GETBLOCKTXN_DEPTH = 10
|
2016-09-17 02:48:23 +02:00
|
|
|
chain_height = node.getblockcount()
|
2016-07-12 22:05:02 +02:00
|
|
|
current_height = chain_height
|
|
|
|
while (current_height >= chain_height - MAX_GETBLOCKTXN_DEPTH):
|
2016-09-17 02:48:23 +02:00
|
|
|
block_hash = node.getblockhash(current_height)
|
|
|
|
block = FromHex(CBlock(), node.getblock(block_hash, False))
|
2016-07-12 22:05:02 +02:00
|
|
|
|
|
|
|
msg = msg_getblocktxn()
|
|
|
|
msg.block_txn_request = BlockTransactionsRequest(int(block_hash, 16), [])
|
|
|
|
num_to_request = random.randint(1, len(block.vtx))
|
|
|
|
msg.block_txn_request.from_absolute(sorted(random.sample(range(len(block.vtx)), num_to_request)))
|
2016-09-17 02:48:23 +02:00
|
|
|
test_node.send_message(msg)
|
2017-08-16 18:17:34 +02:00
|
|
|
wait_until(lambda: "blocktxn" in test_node.last_message, timeout=10, lock=mininode_lock)
|
2016-07-12 22:05:02 +02:00
|
|
|
|
|
|
|
[tx.calc_sha256() for tx in block.vtx]
|
|
|
|
with mininode_lock:
|
2017-03-30 14:38:46 +02:00
|
|
|
assert_equal(test_node.last_message["blocktxn"].block_transactions.blockhash, int(block_hash, 16))
|
2016-07-12 22:05:02 +02:00
|
|
|
all_indices = msg.block_txn_request.to_absolute()
|
|
|
|
for index in all_indices:
|
2017-03-30 14:38:46 +02:00
|
|
|
tx = test_node.last_message["blocktxn"].block_transactions.transactions.pop(0)
|
2016-07-12 22:05:02 +02:00
|
|
|
tx.calc_sha256()
|
|
|
|
assert_equal(tx.sha256, block.vtx[index].sha256)
|
2016-09-17 02:48:23 +02:00
|
|
|
if version == 1:
|
|
|
|
# Witnesses should have been stripped
|
2019-02-19 23:43:44 +01:00
|
|
|
assert tx.wit.is_null()
|
2016-09-17 02:48:23 +02:00
|
|
|
else:
|
|
|
|
# Check that the witness matches
|
|
|
|
assert_equal(tx.calc_sha256(True), block.vtx[index].calc_sha256(True))
|
2017-03-30 14:38:46 +02:00
|
|
|
test_node.last_message.pop("blocktxn", None)
|
2016-07-12 22:05:02 +02:00
|
|
|
current_height -= 1
|
|
|
|
|
2016-10-26 20:53:18 +02:00
|
|
|
# Next request should send a full block response, as we're past the
|
|
|
|
# allowed depth for a blocktxn response.
|
2016-09-17 02:48:23 +02:00
|
|
|
block_hash = node.getblockhash(current_height)
|
2016-07-12 22:05:02 +02:00
|
|
|
msg.block_txn_request = BlockTransactionsRequest(int(block_hash, 16), [0])
|
2016-10-26 20:53:18 +02:00
|
|
|
with mininode_lock:
|
2017-03-30 14:38:46 +02:00
|
|
|
test_node.last_message.pop("block", None)
|
|
|
|
test_node.last_message.pop("blocktxn", None)
|
2016-09-17 02:48:23 +02:00
|
|
|
test_node.send_and_ping(msg)
|
2016-07-12 22:05:02 +02:00
|
|
|
with mininode_lock:
|
2017-03-30 14:38:46 +02:00
|
|
|
test_node.last_message["block"].block.calc_sha256()
|
|
|
|
assert_equal(test_node.last_message["block"].block.sha256, int(block_hash, 16))
|
|
|
|
assert "blocktxn" not in test_node.last_message
|
2016-07-12 22:05:02 +02:00
|
|
|
|
2019-03-23 19:55:51 +01:00
|
|
|
def test_compactblocks_not_at_tip(self, test_node):
|
|
|
|
node = self.nodes[0]
|
2016-07-12 22:05:02 +02:00
|
|
|
# Test that requesting old compactblocks doesn't work.
|
2016-08-31 17:35:59 +02:00
|
|
|
MAX_CMPCTBLOCK_DEPTH = 5
|
2016-07-12 22:05:02 +02:00
|
|
|
new_blocks = []
|
2016-08-31 17:35:59 +02:00
|
|
|
for i in range(MAX_CMPCTBLOCK_DEPTH + 1):
|
2016-09-17 02:48:23 +02:00
|
|
|
test_node.clear_block_announcement()
|
|
|
|
new_blocks.append(node.generate(1)[0])
|
2017-08-16 18:17:34 +02:00
|
|
|
wait_until(test_node.received_block_announcement, timeout=30, lock=mininode_lock)
|
2016-07-12 22:05:02 +02:00
|
|
|
|
2016-09-17 02:48:23 +02:00
|
|
|
test_node.clear_block_announcement()
|
|
|
|
test_node.send_message(msg_getdata([CInv(4, int(new_blocks[0], 16))]))
|
2017-08-16 18:17:34 +02:00
|
|
|
wait_until(lambda: "cmpctblock" in test_node.last_message, timeout=30, lock=mininode_lock)
|
2016-07-12 22:05:02 +02:00
|
|
|
|
2016-09-17 02:48:23 +02:00
|
|
|
test_node.clear_block_announcement()
|
|
|
|
node.generate(1)
|
2017-08-16 18:17:34 +02:00
|
|
|
wait_until(test_node.received_block_announcement, timeout=30, lock=mininode_lock)
|
2016-09-17 02:48:23 +02:00
|
|
|
test_node.clear_block_announcement()
|
2016-10-26 20:15:16 +02:00
|
|
|
with mininode_lock:
|
2017-03-30 14:38:46 +02:00
|
|
|
test_node.last_message.pop("block", None)
|
2016-09-17 02:48:23 +02:00
|
|
|
test_node.send_message(msg_getdata([CInv(4, int(new_blocks[0], 16))]))
|
2017-08-16 18:17:34 +02:00
|
|
|
wait_until(lambda: "block" in test_node.last_message, timeout=30, lock=mininode_lock)
|
2016-07-12 22:05:02 +02:00
|
|
|
with mininode_lock:
|
2017-03-30 14:38:46 +02:00
|
|
|
test_node.last_message["block"].block.calc_sha256()
|
|
|
|
assert_equal(test_node.last_message["block"].block.sha256, int(new_blocks[0], 16))
|
2016-07-12 22:05:02 +02:00
|
|
|
|
|
|
|
# Generate an old compactblock, and verify that it's not accepted.
|
2016-09-17 02:48:23 +02:00
|
|
|
cur_height = node.getblockcount()
|
2018-09-23 14:50:47 +02:00
|
|
|
hashPrevBlock = int(node.getblockhash(cur_height - 5), 16)
|
2016-09-17 02:48:23 +02:00
|
|
|
block = self.build_block_on_tip(node)
|
2016-07-12 22:05:02 +02:00
|
|
|
block.hashPrevBlock = hashPrevBlock
|
|
|
|
block.solve()
|
|
|
|
|
|
|
|
comp_block = HeaderAndShortIDs()
|
|
|
|
comp_block.initialize_from_block(block)
|
2016-09-17 02:48:23 +02:00
|
|
|
test_node.send_and_ping(msg_cmpctblock(comp_block.to_p2p()))
|
2016-07-12 22:05:02 +02:00
|
|
|
|
2016-09-17 02:48:23 +02:00
|
|
|
tips = node.getchaintips()
|
2016-07-12 22:05:02 +02:00
|
|
|
found = False
|
|
|
|
for x in tips:
|
|
|
|
if x["hash"] == block.hash:
|
|
|
|
assert_equal(x["status"], "headers-only")
|
|
|
|
found = True
|
|
|
|
break
|
2019-02-19 23:43:44 +01:00
|
|
|
assert found
|
2016-07-12 22:05:02 +02:00
|
|
|
|
|
|
|
# Requesting this block via getblocktxn should silently fail
|
|
|
|
# (to avoid fingerprinting attacks).
|
|
|
|
msg = msg_getblocktxn()
|
|
|
|
msg.block_txn_request = BlockTransactionsRequest(block.sha256, [0])
|
|
|
|
with mininode_lock:
|
2017-03-30 14:38:46 +02:00
|
|
|
test_node.last_message.pop("blocktxn", None)
|
2016-09-17 02:48:23 +02:00
|
|
|
test_node.send_and_ping(msg)
|
2016-07-12 22:05:02 +02:00
|
|
|
with mininode_lock:
|
2017-03-30 14:38:46 +02:00
|
|
|
assert "blocktxn" not in test_node.last_message
|
2016-09-17 02:48:23 +02:00
|
|
|
|
2019-03-23 19:55:51 +01:00
|
|
|
def test_end_to_end_block_relay(self, listeners):
|
|
|
|
node = self.nodes[0]
|
2016-09-17 02:48:23 +02:00
|
|
|
utxo = self.utxos.pop(0)
|
|
|
|
|
|
|
|
block = self.build_block_with_transactions(node, utxo, 10)
|
|
|
|
|
|
|
|
[l.clear_block_announcement() for l in listeners]
|
|
|
|
|
|
|
|
# ToHex() won't serialize with witness, but this block has no witnesses
|
|
|
|
# anyway. TODO: repeat this test with witness tx's to a segwit node.
|
|
|
|
node.submitblock(ToHex(block))
|
|
|
|
|
|
|
|
for l in listeners:
|
2017-08-16 18:17:34 +02:00
|
|
|
wait_until(lambda: l.received_block_announcement(), timeout=30, lock=mininode_lock)
|
2016-09-17 02:48:23 +02:00
|
|
|
with mininode_lock:
|
|
|
|
for l in listeners:
|
2017-03-30 14:38:46 +02:00
|
|
|
assert "cmpctblock" in l.last_message
|
|
|
|
l.last_message["cmpctblock"].header_and_shortids.header.calc_sha256()
|
|
|
|
assert_equal(l.last_message["cmpctblock"].header_and_shortids.header.sha256, block.sha256)
|
2016-09-17 02:48:23 +02:00
|
|
|
|
2016-10-26 19:03:30 +02:00
|
|
|
# Test that we don't get disconnected if we relay a compact block with valid header,
|
|
|
|
# but invalid transactions.
|
2019-03-23 19:55:51 +01:00
|
|
|
def test_invalid_tx_in_compactblock(self, test_node, use_segwit=True):
|
|
|
|
node = self.nodes[0]
|
2019-02-19 23:43:44 +01:00
|
|
|
assert len(self.utxos)
|
2016-10-26 19:03:30 +02:00
|
|
|
utxo = self.utxos[0]
|
|
|
|
|
|
|
|
block = self.build_block_with_transactions(node, utxo, 5)
|
|
|
|
del block.vtx[3]
|
|
|
|
block.hashMerkleRoot = block.calc_merkle_root()
|
|
|
|
if use_segwit:
|
|
|
|
# If we're testing with segwit, also drop the coinbase witness,
|
|
|
|
# but include the witness commitment.
|
|
|
|
add_witness_commitment(block)
|
|
|
|
block.vtx[0].wit.vtxinwit = []
|
|
|
|
block.solve()
|
|
|
|
|
|
|
|
# Now send the compact block with all transactions prefilled, and
|
|
|
|
# verify that we don't get disconnected.
|
|
|
|
comp_block = HeaderAndShortIDs()
|
|
|
|
comp_block.initialize_from_block(block, prefill_list=[0, 1, 2, 3, 4], use_witness=use_segwit)
|
|
|
|
msg = msg_cmpctblock(comp_block.to_p2p())
|
|
|
|
test_node.send_and_ping(msg)
|
|
|
|
|
|
|
|
# Check that the tip didn't advance
|
2019-02-19 23:43:44 +01:00
|
|
|
assert int(node.getbestblockhash(), 16) is not block.sha256
|
2016-10-26 19:03:30 +02:00
|
|
|
test_node.sync_with_ping()
|
|
|
|
|
2016-09-17 02:48:23 +02:00
|
|
|
# Helper for enabling cb announcements
|
|
|
|
# Send the sendcmpct request and sync headers
|
2019-03-23 19:55:51 +01:00
|
|
|
def request_cb_announcements(self, peer):
|
|
|
|
node = self.nodes[0]
|
2016-09-17 02:48:23 +02:00
|
|
|
tip = node.getbestblockhash()
|
|
|
|
peer.get_headers(locator=[int(tip, 16)], hashstop=0)
|
|
|
|
|
|
|
|
msg = msg_sendcmpct()
|
2019-03-23 19:55:51 +01:00
|
|
|
msg.version = peer.cmpct_version
|
2016-09-17 02:48:23 +02:00
|
|
|
msg.announce = True
|
|
|
|
peer.send_and_ping(msg)
|
|
|
|
|
2019-03-23 19:55:51 +01:00
|
|
|
def test_compactblock_reconstruction_multiple_peers(self, stalling_peer, delivery_peer):
|
|
|
|
node = self.nodes[0]
|
2019-02-19 23:43:44 +01:00
|
|
|
assert len(self.utxos)
|
2016-12-01 18:59:10 +01:00
|
|
|
|
|
|
|
def announce_cmpct_block(node, peer):
|
|
|
|
utxo = self.utxos.pop(0)
|
|
|
|
block = self.build_block_with_transactions(node, utxo, 5)
|
|
|
|
|
|
|
|
cmpct_block = HeaderAndShortIDs()
|
|
|
|
cmpct_block.initialize_from_block(block)
|
|
|
|
msg = msg_cmpctblock(cmpct_block.to_p2p())
|
|
|
|
peer.send_and_ping(msg)
|
|
|
|
with mininode_lock:
|
2017-03-30 14:38:46 +02:00
|
|
|
assert "getblocktxn" in peer.last_message
|
2016-12-01 18:59:10 +01:00
|
|
|
return block, cmpct_block
|
|
|
|
|
|
|
|
block, cmpct_block = announce_cmpct_block(node, stalling_peer)
|
|
|
|
|
|
|
|
for tx in block.vtx[1:]:
|
|
|
|
delivery_peer.send_message(msg_tx(tx))
|
|
|
|
delivery_peer.sync_with_ping()
|
|
|
|
mempool = node.getrawmempool()
|
|
|
|
for tx in block.vtx[1:]:
|
2019-02-19 23:43:44 +01:00
|
|
|
assert tx.hash in mempool
|
2016-12-01 18:59:10 +01:00
|
|
|
|
|
|
|
delivery_peer.send_and_ping(msg_cmpctblock(cmpct_block.to_p2p()))
|
|
|
|
assert_equal(int(node.getbestblockhash(), 16), block.sha256)
|
|
|
|
|
|
|
|
self.utxos.append([block.vtx[-1].sha256, 0, block.vtx[-1].vout[0].nValue])
|
|
|
|
|
|
|
|
# Now test that delivering an invalid compact block won't break relay
|
|
|
|
|
|
|
|
block, cmpct_block = announce_cmpct_block(node, stalling_peer)
|
|
|
|
for tx in block.vtx[1:]:
|
|
|
|
delivery_peer.send_message(msg_tx(tx))
|
|
|
|
delivery_peer.sync_with_ping()
|
|
|
|
|
2018-09-23 14:50:47 +02:00
|
|
|
cmpct_block.prefilled_txn[0].tx.wit.vtxinwit = [CTxInWitness()]
|
2016-12-01 18:59:10 +01:00
|
|
|
cmpct_block.prefilled_txn[0].tx.wit.vtxinwit[0].scriptWitness.stack = [ser_uint256(0)]
|
|
|
|
|
|
|
|
cmpct_block.use_witness = True
|
|
|
|
delivery_peer.send_and_ping(msg_cmpctblock(cmpct_block.to_p2p()))
|
2019-02-19 23:43:44 +01:00
|
|
|
assert int(node.getbestblockhash(), 16) != block.sha256
|
2016-12-01 18:59:10 +01:00
|
|
|
|
tests: Make msg_block a witness block
This diff has been generated with the following script, but is better
reviewed without looking at the script.
# -BEGIN VERIFY SCRIPT-
echo "Use msg_witness_block everywhere, except for tests that require msg_block"
# This could be a separate commit, but it is combined with the
# following scripts to reduce the overall diff
sed -i -e 's/msg_block/msg_witness_block/g' ./test/functional/{feature_assumevalid,feature_cltv,feature_dersig,feature_versionbits_warning,p2p_fingerprint,p2p_sendheaders,p2p_unrequested_blocks,example_test,rpc_blockchain}.py
echo "Rename msg_block to msg_no_witness_block"
# Rename msg_block to msg_no_witness_block in all tests (not the
# framework)
sed -i -e 's/msg_block/msg_no_witness_block/g' $(git grep -l msg_block ./test/functional/*.py)
# Derive msg_no_witness_block from msg_block
# Make msg_block a witness block in messages.py
patch -p1 --fuzz 0 << EOF
diff --git a/test/functional/test_framework/messages.py b/test/functional/test_framework/messages.py
index 00190e4cbd..e454ed5987 100755
--- a/test/functional/test_framework/messages.py
+++ b/test/functional/test_framework/messages.py
@@ -1133 +1133 @@ class msg_block:
- return self.block.serialize(with_witness=False)
+ return self.block.serialize()
@@ -1155 +1155 @@ class msg_generic:
-class msg_witness_block(msg_block):
+class msg_no_witness_block(msg_block):
@@ -1158,2 +1158 @@ class msg_witness_block(msg_block):
- r = self.block.serialize()
- return r
+ return self.block.serialize(with_witness=False)
@@ -1445 +1444 @@ class msg_blocktxn:
- r += self.block_transactions.serialize(with_witness=False)
+ r += self.block_transactions.serialize()
@@ -1452 +1451 @@ class msg_blocktxn:
-class msg_witness_blocktxn(msg_blocktxn):
+class msg_no_witness_blocktxn(msg_blocktxn):
@@ -1456,3 +1455 @@ class msg_witness_blocktxn(msg_blocktxn):
- r = b""
- r += self.block_transactions.serialize()
- return r
+ return self.block_transactions.serialize(with_witness=False)
EOF
# Conclude rename of msg_block to msg_no_witness_block
sed -i -e 's/msg_witness_block/msg_block/g' $(git grep -l msg_witness_block)
# -END VERIFY SCRIPT-
2019-05-08 16:21:25 +02:00
|
|
|
msg = msg_no_witness_blocktxn()
|
2016-12-01 18:59:10 +01:00
|
|
|
msg.block_transactions.blockhash = block.sha256
|
|
|
|
msg.block_transactions.transactions = block.vtx[1:]
|
|
|
|
stalling_peer.send_and_ping(msg)
|
|
|
|
assert_equal(int(node.getbestblockhash(), 16), block.sha256)
|
2016-07-12 22:05:02 +02:00
|
|
|
|
|
|
|
def run_test(self):
|
2018-06-18 23:28:37 +02:00
|
|
|
# Setup the p2p connections
|
2019-03-23 19:55:51 +01:00
|
|
|
self.segwit_node = self.nodes[0].add_p2p_connection(TestP2PConn(cmpct_version=2))
|
|
|
|
self.old_node = self.nodes[0].add_p2p_connection(TestP2PConn(cmpct_version=1), services=NODE_NETWORK)
|
|
|
|
self.additional_segwit_node = self.nodes[0].add_p2p_connection(TestP2PConn(cmpct_version=2))
|
2016-07-12 22:05:02 +02:00
|
|
|
|
|
|
|
# We will need UTXOs to construct transactions in later tests.
|
|
|
|
self.make_utxos()
|
|
|
|
|
2019-05-20 20:59:07 +02:00
|
|
|
assert softfork_active(self.nodes[0], "segwit")
|
2016-09-17 02:48:23 +02:00
|
|
|
|
2017-03-08 00:46:17 +01:00
|
|
|
self.log.info("Testing SENDCMPCT p2p message... ")
|
2019-03-23 19:55:51 +01:00
|
|
|
self.test_sendcmpct(self.segwit_node, old_node=self.old_node)
|
|
|
|
self.test_sendcmpct(self.additional_segwit_node)
|
2016-09-17 02:48:23 +02:00
|
|
|
|
2017-03-08 00:46:17 +01:00
|
|
|
self.log.info("Testing compactblock construction...")
|
2019-03-23 19:55:51 +01:00
|
|
|
self.test_compactblock_construction(self.old_node)
|
|
|
|
self.test_compactblock_construction(self.segwit_node)
|
2016-09-17 02:48:23 +02:00
|
|
|
|
2017-03-08 00:46:17 +01:00
|
|
|
self.log.info("Testing compactblock requests (segwit node)... ")
|
2019-03-23 19:55:51 +01:00
|
|
|
self.test_compactblock_requests(self.segwit_node)
|
2016-09-17 02:48:23 +02:00
|
|
|
|
2017-03-08 00:46:17 +01:00
|
|
|
self.log.info("Testing getblocktxn requests (segwit node)...")
|
2019-03-23 19:55:51 +01:00
|
|
|
self.test_getblocktxn_requests(self.segwit_node)
|
2016-09-17 02:48:23 +02:00
|
|
|
|
2017-03-08 00:46:17 +01:00
|
|
|
self.log.info("Testing getblocktxn handler (segwit node should return witnesses)...")
|
2019-03-23 19:55:51 +01:00
|
|
|
self.test_getblocktxn_handler(self.segwit_node)
|
|
|
|
self.test_getblocktxn_handler(self.old_node)
|
|
|
|
|
|
|
|
self.log.info("Testing compactblock requests/announcements not at chain tip...")
|
|
|
|
self.test_compactblocks_not_at_tip(self.segwit_node)
|
|
|
|
self.test_compactblocks_not_at_tip(self.old_node)
|
|
|
|
|
|
|
|
self.log.info("Testing handling of incorrect blocktxn responses...")
|
|
|
|
self.test_incorrect_blocktxn_response(self.segwit_node)
|
|
|
|
|
|
|
|
self.log.info("Testing reconstructing compact blocks from all peers...")
|
|
|
|
self.test_compactblock_reconstruction_multiple_peers(self.segwit_node, self.additional_segwit_node)
|
2016-09-17 02:48:23 +02:00
|
|
|
|
|
|
|
# Test that if we submitblock to node1, we'll get a compact block
|
|
|
|
# announcement to all peers.
|
|
|
|
# (Post-segwit activation, blocks won't propagate from node0 to node1
|
|
|
|
# automatically, so don't bother testing a block announced to node0.)
|
2017-03-08 00:46:17 +01:00
|
|
|
self.log.info("Testing end-to-end block relay...")
|
2019-03-23 19:55:51 +01:00
|
|
|
self.request_cb_announcements(self.old_node)
|
|
|
|
self.request_cb_announcements(self.segwit_node)
|
|
|
|
self.test_end_to_end_block_relay([self.segwit_node, self.old_node])
|
2016-09-17 02:48:23 +02:00
|
|
|
|
2017-03-08 00:46:17 +01:00
|
|
|
self.log.info("Testing handling of invalid compact blocks...")
|
2019-03-23 19:55:51 +01:00
|
|
|
self.test_invalid_tx_in_compactblock(self.segwit_node)
|
|
|
|
self.test_invalid_tx_in_compactblock(self.old_node)
|
2016-10-26 19:03:30 +02:00
|
|
|
|
2017-03-08 00:46:17 +01:00
|
|
|
self.log.info("Testing invalid index in cmpctblock message...")
|
2016-07-12 22:05:02 +02:00
|
|
|
self.test_invalid_cmpctblock_message()
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
CompactBlocksTest().main()
|