Skip to content

Commit

Permalink
Auto merge of #3784 - Eirik0:3718-txexpiringsoon, r=<try>
Browse files Browse the repository at this point in the history
Fix tx expiring soon test

Closes #3718
  • Loading branch information
zkbot committed Jan 15, 2019
2 parents 8df7a07 + 1e8fcaa commit 6ec45fc
Show file tree
Hide file tree
Showing 3 changed files with 210 additions and 257 deletions.
291 changes: 98 additions & 193 deletions qa/rpc-tests/p2p_txexpiringsoon.py
Expand Up @@ -6,111 +6,81 @@
import sys; assert sys.version_info < (3,), ur"This script does not run under Python 3. Please use Python 2.7.x."

from test_framework.authproxy import JSONRPCException
from test_framework.mininode import NodeConn, NodeConnCB, NetworkThread, \
CTransaction, CInv, msg_mempool, msg_getdata, msg_tx, mininode_lock, \
msg_ping, msg_pong, OVERWINTER_PROTO_VERSION
from test_framework.mininode import NodeConn, NetworkThread, CInv, \
msg_mempool, msg_getdata, msg_tx, mininode_lock, OVERWINTER_PROTO_VERSION
from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import initialize_chain_clean, start_nodes, \
p2p_port, assert_equal, sync_blocks, sync_mempools, connect_nodes_bi

import time, cStringIO
from binascii import hexlify, unhexlify

class TestNode(NodeConnCB):
def __init__(self):
NodeConnCB.__init__(self)
self.create_callback_map()
self.connection = None
self.ping_counter = 1
self.last_pong = msg_pong()

def add_connection(self, conn):
self.connection = conn

# Spin until verack message is received from the node.
# We use this to signal that our test can begin. This
# is called from the testing thread, so it needs to acquire
# the global lock.
def wait_for_verack(self):
while True:
with mininode_lock:
if self.verack_received:
return
time.sleep(0.05)

# Wrapper for the NodeConn's send_message function
def send_message(self, message):
self.connection.send_message(message)

def on_close(self, conn):
pass

def on_reject(self, conn, message):
conn.rejectMessage = message

# Track the last getdata message we receive (used in the test)
def on_getdata(self, conn, message):
self.last_getdata = message

def on_tx(self, conn, message):
self.last_tx = message

def on_inv(self, conn, message):
self.last_inv = message

def on_notfound(self, conn, message):
self.last_notfound = message

def on_pong(self, conn, message):
self.last_pong = message

# Sync up with the node after delivery of a message
def sync_with_ping(self, timeout=30):
self.connection.send_message(msg_ping(nonce=self.ping_counter))
received_pong = False
sleep_time = 0.05
while not received_pong and timeout > 0:
time.sleep(sleep_time)
timeout -= sleep_time
with mininode_lock:
if self.last_pong.nonce == self.ping_counter:
received_pong = True
self.ping_counter += 1
return received_pong
from test_framework.util import assert_equal, connect_nodes_bi, fail, \
initialize_chain_clean, p2p_port, start_nodes, sync_blocks, sync_mempools
from tx_expity_helper import TestNode, create_transaction

from binascii import hexlify


class TxExpiringSoonTest(BitcoinTestFramework):

def setup_chain(self):
print "Initializing test directory "+self.options.tmpdir
print "Initializing test directory " + self.options.tmpdir
initialize_chain_clean(self.options.tmpdir, 3)

def setup_network(self):
self.nodes = start_nodes(3, self.options.tmpdir,
extra_args=[[
'-nuparams=5ba81b19:10',
]] * 3)
connect_nodes_bi(self.nodes,0,1)
extra_args=[['-nuparams=5ba81b19:10']] * 3)
connect_nodes_bi(self.nodes, 0, 1)
# We don't connect node 2

def create_transaction(self, node, coinbase, to_address, amount, expiry_height):
from_txid = node.getblock(coinbase)['tx'][0]
inputs = [{ "txid" : from_txid, "vout" : 0}]
outputs = { to_address : amount }
rawtx = node.createrawtransaction(inputs, outputs)
tx = CTransaction()

# Set the expiry height
f = cStringIO.StringIO(unhexlify(rawtx))
tx.deserialize(f)
tx.nExpiryHeight = expiry_height
rawtx = hexlify(tx.serialize())

signresult = node.signrawtransaction(rawtx)
f = cStringIO.StringIO(unhexlify(signresult['hex']))
tx.deserialize(f)
tx.rehash()
def send_transaction(self, testnode, block, address, expiry_height):
tx = create_transaction(self.nodes[0],
block,
address,
10.0,
expiry_height)
testnode.send_message(msg_tx(tx))

# Sync up with node after p2p messages delivered
testnode.sync_with_ping()

# Sync nodes 0 and 1
sync_blocks(self.nodes[:2])
sync_mempools(self.nodes[:2])

return tx

def verify_inv(self, testnode, tx):
# Make sure we are synced before sending the mempool message
testnode.sync_with_ping()

# Send p2p message "mempool" to receive contents from zcashd node in "inv" message
with mininode_lock:
testnode.last_inv = None
testnode.send_message(msg_mempool())

# Sync up with node after p2p messages delivered
testnode.sync_with_ping()

with mininode_lock:
msg = testnode.last_inv
assert_equal(len(msg.inv), 1)
assert_equal(tx.sha256, msg.inv[0].hash)

def send_data_message(self, testnode, tx):
# Send p2p message "getdata" to verify tx gets sent in "tx" message
getdatamsg = msg_getdata()
getdatamsg.inv = [CInv(1, tx.sha256)]
with mininode_lock:
testnode.last_notfound = None
testnode.last_tx = None
testnode.send_message(getdatamsg)

def verify_last_tx(self, testnode, tx):
# Sync up with node after p2p messages delivered
testnode.sync_with_ping()

# Verify data received in "tx" message is for tx
with mininode_lock:
incoming_tx = testnode.last_tx.tx
incoming_tx.rehash()
assert_equal(tx.sha256, incoming_tx.sha256)

def run_test(self):
testnode0 = TestNode()
connections = []
Expand All @@ -129,8 +99,8 @@ def run_test(self):
assert_equal(0, peerinfo[0]["banscore"])

# Mine some blocks so we can spend
self.coinbase_blocks = self.nodes[0].generate(200)
self.nodeaddress = self.nodes[0].getnewaddress()
coinbase_blocks = self.nodes[0].generate(200)
node_address = self.nodes[0].getnewaddress()

# Sync nodes 0 and 1
sync_blocks(self.nodes[:2])
Expand All @@ -142,65 +112,29 @@ def run_test(self):
assert_equal(self.nodes[2].getblockcount(), 0)

# Mininodes send expiring soon transaction in "tx" message to zcashd node
tx1 = self.create_transaction(self.nodes[0],
self.coinbase_blocks[0],
self.nodeaddress, 10.0,
203)
testnode0.send_message(msg_tx(tx1))

# Mininodes send transaction in "tx" message to zcashd node
tx2 = self.create_transaction(self.nodes[0],
self.coinbase_blocks[1],
self.nodeaddress, 10.0,
204)
testnode0.send_message(msg_tx(tx2))
self.send_transaction(testnode0, coinbase_blocks[0], node_address, 203)

# Sync up with node after p2p messages delivered
testnode0.sync_with_ping()

# Sync nodes 0 and 1
sync_blocks(self.nodes[:2])
sync_mempools(self.nodes[:2])

# Verify contents of mempool
assert(tx1.hash not in self.nodes[0].getrawmempool()) # tx1 rejected as expiring soon
assert(tx1.hash not in self.nodes[1].getrawmempool())
assert(tx2.hash in self.nodes[0].getrawmempool()) # tx2 accepted
assert(tx2.hash in self.nodes[1].getrawmempool())
assert_equal(len(self.nodes[2].getrawmempool()), 0) # node 2 is isolated and empty
# Assert that the tx is not in the mempool (expiring soon)
assert_equal([], self.nodes[0].getrawmempool())
assert_equal([], self.nodes[1].getrawmempool())
assert_equal([], self.nodes[2].getrawmempool())

# Send p2p message "mempool" to receive contents from zcashd node in "inv" message
with mininode_lock:
testnode0.last_inv = None
testnode0.send_message(msg_mempool())
# Mininodes send transaction in "tx" message to zcashd node
tx2 = self.send_transaction(testnode0, coinbase_blocks[1], node_address, 204)

# Sync up with node after p2p messages delivered
testnode0.sync_with_ping()
# tx2 is not expiring soon
assert_equal([tx2.hash], self.nodes[0].getrawmempool())
assert_equal([tx2.hash], self.nodes[1].getrawmempool())
# node 2 is isolated
assert_equal([], self.nodes[2].getrawmempool())

# Verify txid for tx2
with mininode_lock:
msg = testnode0.last_inv
assert_equal(len(msg.inv), 1)
assert_equal(tx2.sha256, msg.inv[0].hash)

# Send p2p message "getdata" to verify tx2 gets sent in "tx" message
getdatamsg = msg_getdata()
getdatamsg.inv = [ CInv(1, tx2.sha256) ]
with mininode_lock:
testnode0.last_tx = None
testnode0.send_message(getdatamsg)

# Sync up with node after p2p messages delivered
testnode0.sync_with_ping()

# Verify data received in "tx" message is for tx2
with mininode_lock:
incoming_tx = testnode0.last_tx.tx
incoming_tx.rehash()
assert_equal(tx2.sha256, incoming_tx.sha256)
self.verify_inv(testnode0, tx2)
self.send_data_message(testnode0, tx2)
self.verify_last_tx(testnode0, tx2)

# Sync and mine an empty block with node 2, leaving tx in the mempool of node0 and node1
for blkhash in self.coinbase_blocks:
for blkhash in coinbase_blocks:
blk = self.nodes[0].getblock(blkhash, 0)
self.nodes[2].submitblock(blk)
self.nodes[2].generate(1)
Expand All @@ -211,7 +145,7 @@ def run_test(self):
assert_equal(self.nodes[2].getblockcount(), 201)

# Reconnect node 2 to the network
connect_nodes_bi(self.nodes,1,2)
connect_nodes_bi(self.nodes, 0, 2)

# Set up test node for node 2
testnode2 = TestNode()
Expand All @@ -226,27 +160,25 @@ def run_test(self):
assert_equal(self.nodes[2].getblockcount(), 201)

# Verify contents of mempool
assert(tx2.hash in self.nodes[0].getrawmempool())
assert(tx2.hash in self.nodes[1].getrawmempool())
assert(tx2.hash not in self.nodes[2].getrawmempool())
assert_equal([tx2.hash], self.nodes[0].getrawmempool())
assert_equal([tx2.hash], self.nodes[1].getrawmempool())
assert_equal([], self.nodes[2].getrawmempool())

# Confirm tx2 cannot be submitted to a mempool because it is expiring soon.
try:
rawtx2 = hexlify(tx2.serialize())
self.nodes[2].sendrawtransaction(rawtx2)
assert(False)
fail("Sending transaction should have failed")
except JSONRPCException as e:
errorString = e.error['message']
assert("tx-expiring-soon" in errorString)
assert_equal(
"tx-expiring-soon: expiryheight is 204 but should be at least 205 to avoid transaction expiring soon",
e.error['message']
)

# Ask node 0 for tx2...
with mininode_lock:
testnode0.last_notfound = None
testnode0.last_tx = None
testnode0.send_message(getdatamsg)
self.send_data_message(testnode0, tx2)

# Sync up with node after p2p messages delivered
[ x.sync_with_ping() for x in [testnode0, testnode2] ]
testnode0.sync_with_ping()

# Verify node 0 does not reply to "getdata" by sending "tx" message, as tx2 is expiring soon
with mininode_lock:
Expand All @@ -259,41 +191,13 @@ def run_test(self):
assert_equal(tx2.sha256, msg.inv[0].hash)

# Create a transaction to verify that processing of "getdata" messages is functioning
tx3 = self.create_transaction(self.nodes[0],
self.coinbase_blocks[2],
self.nodeaddress, 10.0,
999)

# Mininodes send tx3 to zcashd node
testnode0.send_message(msg_tx(tx3))
getdatamsg = msg_getdata()
getdatamsg.inv = [ CInv(1, tx3.sha256) ]
with mininode_lock:
testnode0.last_tx = None
testnode0.send_message(getdatamsg)

# Sync up with node after p2p messages delivered
[ x.sync_with_ping() for x in [testnode0, testnode2] ]

# Verify we received a "tx" message for tx3
with mininode_lock:
incoming_tx = testnode0.last_tx.tx
incoming_tx.rehash()
assert_equal(tx3.sha256, incoming_tx.sha256)

# Send p2p message "mempool" to receive contents from zcashd node in "inv" message
with mininode_lock:
testnode0.last_inv = None
testnode0.send_message(msg_mempool())

# Sync up with node after p2p messages delivered
[ x.sync_with_ping() for x in [testnode0, testnode2] ]
tx3 = self.send_transaction(testnode0, coinbase_blocks[2], node_address, 999)

self.send_data_message(testnode0, tx3)
self.verify_last_tx(testnode0, tx3)
# Verify txid for tx3 is returned in "inv", but tx2 which is expiring soon is not returned
with mininode_lock:
msg = testnode0.last_inv
assert_equal(len(msg.inv), 1)
assert_equal(tx3.sha256, msg.inv[0].hash)
self.verify_inv(testnode0, tx3)
self.verify_inv(testnode2, tx3)

# Verify contents of mempool
assert_equal({tx2.hash, tx3.hash}, set(self.nodes[0].getrawmempool()))
Expand All @@ -304,7 +208,8 @@ def run_test(self):
assert_equal(0, sum(peer["banscore"] for peer in self.nodes[0].getpeerinfo()))
assert_equal(0, sum(peer["banscore"] for peer in self.nodes[2].getpeerinfo()))

[ c.disconnect_node() for c in connections ]
[c.disconnect_node() for c in connections]


if __name__ == '__main__':
TxExpiringSoonTest().main()

0 comments on commit 6ec45fc

Please sign in to comment.