2019-12-04 08:06:22 -08:00
|
|
|
#!/usr/bin/env python3
|
2019-01-14 13:01:38 -08:00
|
|
|
# Copyright (c) 2019 The Zcash developers
|
|
|
|
# Distributed under the MIT software license, see the accompanying
|
2019-07-18 07:16:09 -07:00
|
|
|
# file COPYING or https://www.opensource.org/licenses/mit-license.php .
|
2019-01-14 13:01:38 -08:00
|
|
|
|
|
|
|
#
|
|
|
|
# Common code for testing transaction expiry
|
|
|
|
#
|
|
|
|
from test_framework.mininode import CTransaction, NodeConnCB, mininode_lock, msg_ping, \
|
|
|
|
msg_pong
|
2020-01-27 18:55:26 -08:00
|
|
|
from test_framework.util import fail
|
2019-01-14 13:01:38 -08:00
|
|
|
|
2019-12-04 08:06:22 -08:00
|
|
|
import io
|
2019-01-14 13:01:38 -08:00
|
|
|
import time
|
|
|
|
|
2020-01-27 18:55:26 -08:00
|
|
|
from binascii import unhexlify
|
2019-01-14 13:01:38 -08:00
|
|
|
|
|
|
|
|
|
|
|
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()
|
2021-09-08 07:40:47 -07:00
|
|
|
self.conn_closed = False
|
2019-01-14 13:01:38 -08:00
|
|
|
|
|
|
|
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):
|
2021-09-08 07:40:47 -07:00
|
|
|
self.conn_closed = True
|
2019-01-14 13:01:38 -08:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2019-01-14 13:37:31 -08:00
|
|
|
# The following function is mostly copied from p2p-acceptblock.py
|
2019-01-14 13:01:38 -08:00
|
|
|
# Sync up with the node after delivery of a message
|
2021-08-09 09:43:48 -07:00
|
|
|
def sync_with_ping(self, timeout=30, waiting_for=None):
|
2019-01-14 13:01:38 -08:00
|
|
|
self.connection.send_message(msg_ping(nonce=self.ping_counter))
|
|
|
|
sleep_time = 0.05
|
2019-01-14 20:27:01 -08:00
|
|
|
while timeout > 0:
|
2019-01-14 13:01:38 -08:00
|
|
|
with mininode_lock:
|
2021-08-09 09:43:48 -07:00
|
|
|
ready = True if waiting_for is None else waiting_for(self) is not None
|
|
|
|
if ready and self.last_pong.nonce == self.ping_counter:
|
2019-01-14 20:27:01 -08:00
|
|
|
self.ping_counter += 1
|
|
|
|
return
|
|
|
|
time.sleep(sleep_time)
|
|
|
|
timeout -= sleep_time
|
|
|
|
fail("Should have received pong")
|
2019-01-14 13:01:38 -08:00
|
|
|
|
|
|
|
|
|
|
|
def create_transaction(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
|
2019-12-04 08:06:22 -08:00
|
|
|
f = io.BytesIO(unhexlify(rawtx))
|
2019-01-14 13:01:38 -08:00
|
|
|
tx.deserialize(f)
|
|
|
|
tx.nExpiryHeight = expiry_height
|
2019-12-05 15:55:06 -08:00
|
|
|
rawtx = tx.serialize().hex()
|
2019-01-14 13:01:38 -08:00
|
|
|
|
|
|
|
signresult = node.signrawtransaction(rawtx)
|
2019-12-04 08:06:22 -08:00
|
|
|
f = io.BytesIO(unhexlify(signresult['hex']))
|
2019-01-14 13:01:38 -08:00
|
|
|
tx.deserialize(f)
|
|
|
|
tx.rehash()
|
|
|
|
return tx
|