aboutsummaryrefslogtreecommitdiff
path: root/test/functional/test_framework/util.py
diff options
context:
space:
mode:
Diffstat (limited to 'test/functional/test_framework/util.py')
-rw-r--r--test/functional/test_framework/util.py49
1 files changed, 35 insertions, 14 deletions
diff --git a/test/functional/test_framework/util.py b/test/functional/test_framework/util.py
index 55166ba0ad..35dbfbba8d 100644
--- a/test/functional/test_framework/util.py
+++ b/test/functional/test_framework/util.py
@@ -19,7 +19,7 @@ import unittest
from . import coverage
from .authproxy import AuthServiceProxy, JSONRPCException
-from io import BytesIO
+from typing import Callable, Optional
logger = logging.getLogger("TestFramework.utils")
@@ -80,7 +80,7 @@ def assert_raises_message(exc, message, fun, *args, **kwds):
raise AssertionError("No exception raised")
-def assert_raises_process_error(returncode, output, fun, *args, **kwds):
+def assert_raises_process_error(returncode: int, output: str, fun: Callable, *args, **kwds):
"""Execute a process and asserts the process return code and output.
Calls function `fun` with arguments `args` and `kwds`. Catches a CalledProcessError
@@ -88,9 +88,9 @@ def assert_raises_process_error(returncode, output, fun, *args, **kwds):
no CalledProcessError was raised or if the return code and output are not as expected.
Args:
- returncode (int): the process return code.
- output (string): [a substring of] the process output.
- fun (function): the function to call. This should execute a process.
+ returncode: the process return code.
+ output: [a substring of] the process output.
+ fun: the function to call. This should execute a process.
args*: positional arguments for the function.
kwds**: named arguments for the function.
"""
@@ -105,7 +105,7 @@ def assert_raises_process_error(returncode, output, fun, *args, **kwds):
raise AssertionError("No exception raised")
-def assert_raises_rpc_error(code, message, fun, *args, **kwds):
+def assert_raises_rpc_error(code: Optional[int], message: Optional[str], fun: Callable, *args, **kwds):
"""Run an RPC and verify that a specific JSONRPC exception code and message is raised.
Calls function `fun` with arguments `args` and `kwds`. Catches a JSONRPCException
@@ -113,11 +113,11 @@ def assert_raises_rpc_error(code, message, fun, *args, **kwds):
no JSONRPCException was raised or if the error code/message are not as expected.
Args:
- code (int), optional: the error code returned by the RPC call (defined
- in src/rpc/protocol.h). Set to None if checking the error code is not required.
- message (string), optional: [a substring of] the error string returned by the
- RPC call. Set to None if checking the error string is not required.
- fun (function): the function to call. This should be the name of an RPC.
+ code: the error code returned by the RPC call (defined in src/rpc/protocol.h).
+ Set to None if checking the error code is not required.
+ message: [a substring of] the error string returned by the RPC call.
+ Set to None if checking the error string is not required.
+ fun: the function to call. This should be the name of an RPC.
args*: positional arguments for the function.
kwds**: named arguments for the function.
"""
@@ -480,6 +480,28 @@ def create_confirmed_utxos(fee, node, count):
return utxos
+def chain_transaction(node, parent_txids, vouts, value, fee, num_outputs):
+ """Build and send a transaction that spends the given inputs (specified
+ by lists of parent_txid:vout each), with the desired total value and fee,
+ equally divided up to the desired number of outputs.
+
+ Returns a tuple with the txid and the amount sent per output.
+ """
+ send_value = satoshi_round((value - fee)/num_outputs)
+ inputs = []
+ for (txid, vout) in zip(parent_txids, vouts):
+ inputs.append({'txid' : txid, 'vout' : vout})
+ outputs = {}
+ for _ in range(num_outputs):
+ outputs[node.getnewaddress()] = send_value
+ rawtx = node.createrawtransaction(inputs, outputs, 0, True)
+ signedtx = node.signrawtransactionwithwallet(rawtx)
+ txid = node.sendrawtransaction(signedtx['hex'])
+ fulltx = node.getrawtransaction(txid, 1)
+ assert len(fulltx['vout']) == num_outputs # make sure we didn't generate a change output
+ return (txid, send_value)
+
+
# Create large OP_RETURN txouts that can be appended to a transaction
# to make it large (helper for constructing large transactions).
def gen_return_txouts():
@@ -505,7 +527,7 @@ def gen_return_txouts():
def create_lots_of_big_transactions(node, txouts, utxos, num, fee):
addr = node.getnewaddress()
txids = []
- from .messages import CTransaction
+ from .messages import tx_from_hex
for _ in range(num):
t = utxos.pop()
inputs = [{"txid": t["txid"], "vout": t["vout"]}]
@@ -513,8 +535,7 @@ def create_lots_of_big_transactions(node, txouts, utxos, num, fee):
change = t['amount'] - fee
outputs[addr] = satoshi_round(change)
rawtx = node.createrawtransaction(inputs, outputs)
- tx = CTransaction()
- tx.deserialize(BytesIO(hex_str_to_bytes(rawtx)))
+ tx = tx_from_hex(rawtx)
for txout in txouts:
tx.vout.append(txout)
newtx = tx.serialize().hex()