test: replace inv type magic numbers by constants

This commit is contained in:
Sebastian Falbesoner 2020-04-25 13:44:44 +02:00
parent c1cd2b5a97
commit eeaaa58d2c
12 changed files with 40 additions and 36 deletions

View File

@ -15,7 +15,7 @@ from collections import defaultdict
# Avoid wildcard * imports # Avoid wildcard * imports
from test_framework.blocktools import (create_block, create_coinbase) from test_framework.blocktools import (create_block, create_coinbase)
from test_framework.messages import CInv from test_framework.messages import CInv, MSG_BLOCK
from test_framework.mininode import ( from test_framework.mininode import (
P2PInterface, P2PInterface,
mininode_lock, mininode_lock,
@ -198,7 +198,7 @@ class ExampleTest(BitcoinTestFramework):
getdata_request = msg_getdata() getdata_request = msg_getdata()
for block in blocks: for block in blocks:
getdata_request.inv.append(CInv(2, block)) getdata_request.inv.append(CInv(MSG_BLOCK, block))
self.nodes[2].p2p.send_message(getdata_request) self.nodes[2].p2p.send_message(getdata_request)
# wait_until() will loop until a predicate condition is met. Use it to test properties of the # wait_until() will loop until a predicate condition is met. Use it to test properties of the

View File

@ -13,7 +13,7 @@ if uploadtarget has been reached.
from collections import defaultdict from collections import defaultdict
import time import time
from test_framework.messages import CInv, msg_getdata from test_framework.messages import CInv, MSG_BLOCK, msg_getdata
from test_framework.mininode import P2PInterface from test_framework.mininode import P2PInterface
from test_framework.test_framework import BitcoinTestFramework from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import assert_equal, mine_large_block from test_framework.util import assert_equal, mine_large_block
@ -84,7 +84,7 @@ class MaxUploadTest(BitcoinTestFramework):
# the same big old block too many times (expect: disconnect) # the same big old block too many times (expect: disconnect)
getdata_request = msg_getdata() getdata_request = msg_getdata()
getdata_request.inv.append(CInv(2, big_old_block)) getdata_request.inv.append(CInv(MSG_BLOCK, big_old_block))
max_bytes_per_day = 800*1024*1024 max_bytes_per_day = 800*1024*1024
daily_buffer = 144 * 4000000 daily_buffer = 144 * 4000000
@ -109,7 +109,7 @@ class MaxUploadTest(BitcoinTestFramework):
# Requesting the current block on p2p_conns[1] should succeed indefinitely, # Requesting the current block on p2p_conns[1] should succeed indefinitely,
# even when over the max upload target. # even when over the max upload target.
# We'll try 800 times # We'll try 800 times
getdata_request.inv = [CInv(2, big_new_block)] getdata_request.inv = [CInv(MSG_BLOCK, big_new_block)]
for i in range(800): for i in range(800):
p2p_conns[1].send_and_ping(getdata_request) p2p_conns[1].send_and_ping(getdata_request)
assert_equal(p2p_conns[1].block_receive_map[big_new_block], i+1) assert_equal(p2p_conns[1].block_receive_map[big_new_block], i+1)
@ -117,7 +117,7 @@ class MaxUploadTest(BitcoinTestFramework):
self.log.info("Peer 1 able to repeatedly download new block") self.log.info("Peer 1 able to repeatedly download new block")
# But if p2p_conns[1] tries for an old block, it gets disconnected too. # But if p2p_conns[1] tries for an old block, it gets disconnected too.
getdata_request.inv = [CInv(2, big_old_block)] getdata_request.inv = [CInv(MSG_BLOCK, big_old_block)]
p2p_conns[1].send_message(getdata_request) p2p_conns[1].send_message(getdata_request)
p2p_conns[1].wait_for_disconnect() p2p_conns[1].wait_for_disconnect()
assert_equal(len(self.nodes[0].getpeerinfo()), 1) assert_equal(len(self.nodes[0].getpeerinfo()), 1)
@ -145,12 +145,12 @@ class MaxUploadTest(BitcoinTestFramework):
self.nodes[0].add_p2p_connection(TestP2PConn()) self.nodes[0].add_p2p_connection(TestP2PConn())
#retrieve 20 blocks which should be enough to break the 1MB limit #retrieve 20 blocks which should be enough to break the 1MB limit
getdata_request.inv = [CInv(2, big_new_block)] getdata_request.inv = [CInv(MSG_BLOCK, big_new_block)]
for i in range(20): for i in range(20):
self.nodes[0].p2p.send_and_ping(getdata_request) self.nodes[0].p2p.send_and_ping(getdata_request)
assert_equal(self.nodes[0].p2p.block_receive_map[big_new_block], i+1) assert_equal(self.nodes[0].p2p.block_receive_map[big_new_block], i+1)
getdata_request.inv = [CInv(2, big_old_block)] getdata_request.inv = [CInv(MSG_BLOCK, big_old_block)]
self.nodes[0].p2p.send_and_ping(getdata_request) self.nodes[0].p2p.send_and_ping(getdata_request)
assert_equal(len(self.nodes[0].getpeerinfo()), 1) #node is still connected because of the whitelist assert_equal(len(self.nodes[0].getpeerinfo()), 1) #node is still connected because of the whitelist

View File

@ -10,7 +10,7 @@ Version 2 compact blocks are post-segwit (wtxids)
import random import random
from test_framework.blocktools import create_block, create_coinbase, add_witness_commitment from test_framework.blocktools import create_block, create_coinbase, add_witness_commitment
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 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_BLOCK, MSG_WITNESS_FLAG, NODE_NETWORK, P2PHeaderAndShortIDs, PrefilledTransaction, ser_uint256, ToHex
from test_framework.mininode import mininode_lock, P2PInterface from test_framework.mininode import mininode_lock, P2PInterface
from test_framework.script import CScript, OP_TRUE, OP_DROP from test_framework.script import CScript, OP_TRUE, OP_DROP
from test_framework.test_framework import BitcoinTestFramework from test_framework.test_framework import BitcoinTestFramework
@ -44,7 +44,7 @@ class TestP2PConn(P2PInterface):
def on_inv(self, message): def on_inv(self, message):
for x in self.last_message["inv"].inv: for x in self.last_message["inv"].inv:
if x.type == 2: if x.type == MSG_BLOCK:
self.block_announced = True self.block_announced = True
self.announced_blockhashes.add(x.hash) self.announced_blockhashes.add(x.hash)
@ -380,7 +380,7 @@ class CompactBlocksTest(BitcoinTestFramework):
block = self.build_block_on_tip(node, segwit=segwit) block = self.build_block_on_tip(node, segwit=segwit)
if announce == "inv": if announce == "inv":
test_node.send_message(msg_inv([CInv(2, block.sha256)])) test_node.send_message(msg_inv([CInv(MSG_BLOCK, block.sha256)]))
wait_until(lambda: "getheaders" in test_node.last_message, timeout=30, lock=mininode_lock) wait_until(lambda: "getheaders" in test_node.last_message, timeout=30, lock=mininode_lock)
test_node.send_header_for_blocks([block]) test_node.send_header_for_blocks([block])
else: else:
@ -564,7 +564,8 @@ class CompactBlocksTest(BitcoinTestFramework):
# We should receive a getdata request # We should receive a getdata request
test_node.wait_for_getdata([block.sha256], timeout=10) test_node.wait_for_getdata([block.sha256], timeout=10)
assert test_node.last_message["getdata"].inv[0].type == 2 or test_node.last_message["getdata"].inv[0].type == 2 | MSG_WITNESS_FLAG assert test_node.last_message["getdata"].inv[0].type == MSG_BLOCK or \
test_node.last_message["getdata"].inv[0].type == MSG_BLOCK | MSG_WITNESS_FLAG
# Deliver the block # Deliver the block
if version == 2: if version == 2:

View File

@ -7,7 +7,7 @@
from decimal import Decimal from decimal import Decimal
import time import time
from test_framework.messages import msg_feefilter from test_framework.messages import MSG_TX, msg_feefilter
from test_framework.mininode import mininode_lock, P2PInterface from test_framework.mininode import mininode_lock, P2PInterface
from test_framework.test_framework import BitcoinTestFramework from test_framework.test_framework import BitcoinTestFramework
@ -31,7 +31,7 @@ class TestP2PConn(P2PInterface):
def on_inv(self, message): def on_inv(self, message):
for i in message.inv: for i in message.inv:
if (i.type == 1): if (i.type == MSG_TX):
self.txinvs.append(hashToHex(i.hash)) self.txinvs.append(hashToHex(i.hash))
def clear_invs(self): def clear_invs(self):

View File

@ -11,7 +11,7 @@ the node should pretend that it does not have it to avoid fingerprinting.
import time import time
from test_framework.blocktools import (create_block, create_coinbase) from test_framework.blocktools import (create_block, create_coinbase)
from test_framework.messages import CInv from test_framework.messages import CInv, MSG_BLOCK
from test_framework.mininode import ( from test_framework.mininode import (
P2PInterface, P2PInterface,
msg_headers, msg_headers,
@ -48,7 +48,7 @@ class P2PFingerprintTest(BitcoinTestFramework):
# Send a getdata request for a given block hash # Send a getdata request for a given block hash
def send_block_request(self, block_hash, node): def send_block_request(self, block_hash, node):
msg = msg_getdata() msg = msg_getdata()
msg.inv.append(CInv(2, block_hash)) # 2 == "Block" msg.inv.append(CInv(MSG_BLOCK, block_hash))
node.send_message(msg) node.send_message(msg)
# Send a getheaders request for a given single block hash # Send a getheaders request for a given single block hash

View File

@ -206,10 +206,10 @@ class InvalidMessagesTest(BitcoinTestFramework):
def test_large_inv(self): def test_large_inv(self):
conn = self.nodes[0].add_p2p_connection(P2PInterface()) conn = self.nodes[0].add_p2p_connection(P2PInterface())
with self.nodes[0].assert_debug_log(['Misbehaving', 'peer=4 (0 -> 20): message inv size() = 50001']): with self.nodes[0].assert_debug_log(['Misbehaving', 'peer=4 (0 -> 20): message inv size() = 50001']):
msg = messages.msg_inv([messages.CInv(1, 1)] * 50001) msg = messages.msg_inv([messages.CInv(messages.MSG_TX, 1)] * 50001)
conn.send_and_ping(msg) conn.send_and_ping(msg)
with self.nodes[0].assert_debug_log(['Misbehaving', 'peer=4 (20 -> 40): message getdata size() = 50001']): with self.nodes[0].assert_debug_log(['Misbehaving', 'peer=4 (20 -> 40): message getdata size() = 50001']):
msg = messages.msg_getdata([messages.CInv(1, 1)] * 50001) msg = messages.msg_getdata([messages.CInv(messages.MSG_TX, 1)] * 50001)
conn.send_and_ping(msg) conn.send_and_ping(msg)
with self.nodes[0].assert_debug_log(['Misbehaving', 'peer=4 (40 -> 60): headers message size = 2001']): with self.nodes[0].assert_debug_log(['Misbehaving', 'peer=4 (40 -> 60): headers message size = 2001']):
msg = messages.msg_headers([messages.CBlockHeader()] * 2001) msg = messages.msg_headers([messages.CBlockHeader()] * 2001)

View File

@ -4,7 +4,7 @@
# file COPYING or http://www.opensource.org/licenses/mit-license.php. # file COPYING or http://www.opensource.org/licenses/mit-license.php.
"""Test that we don't leak txs to inbound peers that we haven't yet announced to""" """Test that we don't leak txs to inbound peers that we haven't yet announced to"""
from test_framework.messages import msg_getdata, CInv from test_framework.messages import msg_getdata, CInv, MSG_TX
from test_framework.mininode import P2PDataStore from test_framework.mininode import P2PDataStore
from test_framework.test_framework import BitcoinTestFramework from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import ( from test_framework.util import (
@ -37,7 +37,7 @@ class P2PLeakTxTest(BitcoinTestFramework):
txid = gen_node.sendtoaddress(gen_node.getnewaddress(), 0.01) txid = gen_node.sendtoaddress(gen_node.getnewaddress(), 0.01)
want_tx = msg_getdata() want_tx = msg_getdata()
want_tx.inv.append(CInv(t=1, h=int(txid, 16))) want_tx.inv.append(CInv(t=MSG_TX, h=int(txid, 16)))
inbound_peer.last_message.pop('notfound', None) inbound_peer.last_message.pop('notfound', None)
inbound_peer.send_and_ping(want_tx) inbound_peer.send_and_ping(want_tx)

View File

@ -8,7 +8,7 @@ Tests that a node configured with -prune=550 signals NODE_NETWORK_LIMITED correc
and that it responds to getdata requests for blocks correctly: and that it responds to getdata requests for blocks correctly:
- send a block within 288 + 2 of the tip - send a block within 288 + 2 of the tip
- disconnect peers who request blocks older than that.""" - disconnect peers who request blocks older than that."""
from test_framework.messages import CInv, msg_getdata, msg_verack, NODE_NETWORK_LIMITED, NODE_WITNESS from test_framework.messages import CInv, MSG_BLOCK, msg_getdata, msg_verack, NODE_NETWORK_LIMITED, NODE_WITNESS
from test_framework.mininode import P2PInterface, mininode_lock from test_framework.mininode import P2PInterface, mininode_lock
from test_framework.test_framework import BitcoinTestFramework from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import ( from test_framework.util import (
@ -31,7 +31,7 @@ class P2PIgnoreInv(P2PInterface):
wait_until(test_function, timeout=timeout, lock=mininode_lock) wait_until(test_function, timeout=timeout, lock=mininode_lock)
def send_getdata_for_block(self, blockhash): def send_getdata_for_block(self, blockhash):
getdata_request = msg_getdata() getdata_request = msg_getdata()
getdata_request.inv.append(CInv(2, int(blockhash, 16))) getdata_request.inv.append(CInv(MSG_BLOCK, int(blockhash, 16)))
self.send_message(getdata_request) self.send_message(getdata_request)
class NodeNetworkLimitedTest(BitcoinTestFramework): class NodeNetworkLimitedTest(BitcoinTestFramework):

View File

@ -22,6 +22,8 @@ from test_framework.messages import (
CTxOut, CTxOut,
CTxWitness, CTxWitness,
MAX_BLOCK_BASE_SIZE, MAX_BLOCK_BASE_SIZE,
MSG_BLOCK,
MSG_TX,
MSG_WITNESS_FLAG, MSG_WITNESS_FLAG,
NODE_NETWORK, NODE_NETWORK,
NODE_WITNESS, NODE_WITNESS,
@ -157,7 +159,7 @@ class TestP2PConn(P2PInterface):
def announce_tx_and_wait_for_getdata(self, tx, timeout=60, success=True): def announce_tx_and_wait_for_getdata(self, tx, timeout=60, success=True):
with mininode_lock: with mininode_lock:
self.last_message.pop("getdata", None) self.last_message.pop("getdata", None)
self.send_message(msg_inv(inv=[CInv(1, tx.sha256)])) self.send_message(msg_inv(inv=[CInv(MSG_TX, tx.sha256)]))
if success: if success:
self.wait_for_getdata([tx.sha256], timeout) self.wait_for_getdata([tx.sha256], timeout)
else: else:
@ -173,7 +175,7 @@ class TestP2PConn(P2PInterface):
if use_header: if use_header:
self.send_message(msg) self.send_message(msg)
else: else:
self.send_message(msg_inv(inv=[CInv(2, block.sha256)])) self.send_message(msg_inv(inv=[CInv(MSG_BLOCK, block.sha256)]))
self.wait_for_getheaders() self.wait_for_getheaders()
self.send_message(msg) self.send_message(msg)
self.wait_for_getdata([block.sha256]) self.wait_for_getdata([block.sha256])
@ -576,7 +578,7 @@ class SegWitTest(BitcoinTestFramework):
# Verify that if a peer doesn't set nServices to include NODE_WITNESS, # Verify that if a peer doesn't set nServices to include NODE_WITNESS,
# the getdata is just for the non-witness portion. # the getdata is just for the non-witness portion.
self.old_node.announce_tx_and_wait_for_getdata(tx) self.old_node.announce_tx_and_wait_for_getdata(tx)
assert self.old_node.last_message["getdata"].inv[0].type == 1 assert self.old_node.last_message["getdata"].inv[0].type == MSG_TX
# Since we haven't delivered the tx yet, inv'ing the same tx from # Since we haven't delivered the tx yet, inv'ing the same tx from
# a witness transaction ought not result in a getdata. # a witness transaction ought not result in a getdata.
@ -1310,9 +1312,9 @@ class SegWitTest(BitcoinTestFramework):
tx3.wit.vtxinwit[0].scriptWitness.stack = [witness_program] tx3.wit.vtxinwit[0].scriptWitness.stack = [witness_program]
# Also check that old_node gets a tx announcement, even though this is # Also check that old_node gets a tx announcement, even though this is
# a witness transaction. # a witness transaction.
self.old_node.wait_for_inv([CInv(1, tx2.sha256)]) # wait until tx2 was inv'ed self.old_node.wait_for_inv([CInv(MSG_TX, tx2.sha256)]) # wait until tx2 was inv'ed
test_transaction_acceptance(self.nodes[0], self.test_node, tx3, with_witness=True, accepted=True) test_transaction_acceptance(self.nodes[0], self.test_node, tx3, with_witness=True, accepted=True)
self.old_node.wait_for_inv([CInv(1, tx3.sha256)]) self.old_node.wait_for_inv([CInv(MSG_TX, tx3.sha256)])
# Test that getrawtransaction returns correct witness information # Test that getrawtransaction returns correct witness information
# hash, size, vsize # hash, size, vsize

View File

@ -92,6 +92,7 @@ from test_framework.mininode import (
NODE_WITNESS, NODE_WITNESS,
P2PInterface, P2PInterface,
mininode_lock, mininode_lock,
MSG_BLOCK,
msg_block, msg_block,
msg_getblocks, msg_getblocks,
msg_getdata, msg_getdata,
@ -120,7 +121,7 @@ class BaseNode(P2PInterface):
"""Request data for a list of block hashes.""" """Request data for a list of block hashes."""
msg = msg_getdata() msg = msg_getdata()
for x in block_hashes: for x in block_hashes:
msg.inv.append(CInv(2, x)) msg.inv.append(CInv(MSG_BLOCK, x))
self.send_message(msg) self.send_message(msg)
def send_get_headers(self, locator, hashstop): def send_get_headers(self, locator, hashstop):
@ -131,7 +132,7 @@ class BaseNode(P2PInterface):
def send_block_inv(self, blockhash): def send_block_inv(self, blockhash):
msg = msg_inv() msg = msg_inv()
msg.inv = [CInv(2, blockhash)] msg.inv = [CInv(MSG_BLOCK, blockhash)]
self.send_message(msg) self.send_message(msg)
def send_header_for_blocks(self, new_blocks): def send_header_for_blocks(self, new_blocks):

View File

@ -63,7 +63,7 @@ class TxDownloadTest(BitcoinTestFramework):
txid = 0xdeadbeef txid = 0xdeadbeef
self.log.info("Announce the txid from each incoming peer to node 0") self.log.info("Announce the txid from each incoming peer to node 0")
msg = msg_inv([CInv(t=1, h=txid)]) msg = msg_inv([CInv(t=MSG_TX, h=txid)])
for p in self.nodes[0].p2ps: for p in self.nodes[0].p2ps:
p.send_and_ping(msg) p.send_and_ping(msg)
@ -104,7 +104,7 @@ class TxDownloadTest(BitcoinTestFramework):
self.log.info( self.log.info(
"Announce the transaction to all nodes from all {} incoming peers, but never send it".format(NUM_INBOUND)) "Announce the transaction to all nodes from all {} incoming peers, but never send it".format(NUM_INBOUND))
msg = msg_inv([CInv(t=1, h=txid)]) msg = msg_inv([CInv(t=MSG_TX, h=txid)])
for p in self.peers: for p in self.peers:
p.send_and_ping(msg) p.send_and_ping(msg)
@ -135,13 +135,13 @@ class TxDownloadTest(BitcoinTestFramework):
with mininode_lock: with mininode_lock:
p.tx_getdata_count = 0 p.tx_getdata_count = 0
p.send_message(msg_inv([CInv(t=1, h=i) for i in txids])) p.send_message(msg_inv([CInv(t=MSG_TX, h=i) for i in txids]))
wait_until(lambda: p.tx_getdata_count >= MAX_GETDATA_IN_FLIGHT, lock=mininode_lock) wait_until(lambda: p.tx_getdata_count >= MAX_GETDATA_IN_FLIGHT, lock=mininode_lock)
with mininode_lock: with mininode_lock:
assert_equal(p.tx_getdata_count, MAX_GETDATA_IN_FLIGHT) assert_equal(p.tx_getdata_count, MAX_GETDATA_IN_FLIGHT)
self.log.info("Now check that if we send a NOTFOUND for a transaction, we'll get one more request") self.log.info("Now check that if we send a NOTFOUND for a transaction, we'll get one more request")
p.send_message(msg_notfound(vec=[CInv(t=1, h=txids[0])])) p.send_message(msg_notfound(vec=[CInv(t=MSG_TX, h=txids[0])]))
wait_until(lambda: p.tx_getdata_count >= MAX_GETDATA_IN_FLIGHT + 1, timeout=10, lock=mininode_lock) wait_until(lambda: p.tx_getdata_count >= MAX_GETDATA_IN_FLIGHT + 1, timeout=10, lock=mininode_lock)
with mininode_lock: with mininode_lock:
assert_equal(p.tx_getdata_count, MAX_GETDATA_IN_FLIGHT + 1) assert_equal(p.tx_getdata_count, MAX_GETDATA_IN_FLIGHT + 1)
@ -154,7 +154,7 @@ class TxDownloadTest(BitcoinTestFramework):
def test_spurious_notfound(self): def test_spurious_notfound(self):
self.log.info('Check that spurious notfound is ignored') self.log.info('Check that spurious notfound is ignored')
self.nodes[0].p2ps[0].send_message(msg_notfound(vec=[CInv(1, 1)])) self.nodes[0].p2ps[0].send_message(msg_notfound(vec=[CInv(MSG_TX, 1)]))
def run_test(self): def run_test(self):
# Setup the p2p connections # Setup the p2p connections

View File

@ -54,7 +54,7 @@ Node1 is unused in tests 3-7:
import time import time
from test_framework.blocktools import create_block, create_coinbase, create_tx_with_script from test_framework.blocktools import create_block, create_coinbase, create_tx_with_script
from test_framework.messages import CBlockHeader, CInv, msg_block, msg_headers, msg_inv from test_framework.messages import CBlockHeader, CInv, MSG_BLOCK, msg_block, msg_headers, msg_inv
from test_framework.mininode import mininode_lock, P2PInterface from test_framework.mininode import mininode_lock, P2PInterface
from test_framework.test_framework import BitcoinTestFramework from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import ( from test_framework.util import (
@ -210,7 +210,7 @@ class AcceptBlockTest(BitcoinTestFramework):
with mininode_lock: with mininode_lock:
# Clear state so we can check the getdata request # Clear state so we can check the getdata request
test_node.last_message.pop("getdata", None) test_node.last_message.pop("getdata", None)
test_node.send_message(msg_inv([CInv(2, block_h3.sha256)])) test_node.send_message(msg_inv([CInv(MSG_BLOCK, block_h3.sha256)]))
test_node.sync_with_ping() test_node.sync_with_ping()
with mininode_lock: with mininode_lock: