Skip to content

Commit a41976a

Browse files
author
MarcoFalke
committed
Merge bitcoin/bitcoin#24223: test: use MiniWallet for interface_rest.py
438e6f4 test: speedup interface_rest.py by whitelisting peers (immediate tx relay) (Sebastian Falbesoner) 11b9684 test: use MiniWallet for rest_interface.py (Sebastian Falbesoner) Pull request description: This PR enables one more of the non-wallet functional tests (interface_rest.py) to be run even with the Bitcoin Core wallet disabled by using the MiniWallet instead, as proposed in #20078. Note that the original test sent funds from one node to another and checked node's balances, but the state of a node's wallet is not relevant to any of the REST endpoints, i.e. the replacement is quite stright-forward. In an additional commit, the test is further sped up by using the good ol' immediate tx relay trick (parameter `[email protected]`). ACKs for top commit: brunoerg: ACK 438e6f4 Tree-SHA512: eac351c5fb7e043c36c193d51545f20f563be9aaa04f3429a2bfb452ae4aa72294d2552800d6cac55c9a3ec2b4f30bcda2abcd74736dec3ed75e7d83c5af437f
2 parents 02e1d8d + 438e6f4 commit a41976a

File tree

1 file changed

+27
-26
lines changed

1 file changed

+27
-26
lines changed

test/functional/interface_rest.py

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,28 @@
66

77
from decimal import Decimal
88
from enum import Enum
9+
import http.client
910
from io import BytesIO
1011
import json
1112
from struct import pack, unpack
12-
13-
import http.client
1413
import urllib.parse
1514

15+
16+
from test_framework.messages import (
17+
BLOCK_HEADER_SIZE,
18+
COIN,
19+
)
1620
from test_framework.test_framework import BitcoinTestFramework
1721
from test_framework.util import (
1822
assert_equal,
1923
assert_greater_than,
2024
assert_greater_than_or_equal,
2125
)
26+
from test_framework.wallet import (
27+
MiniWallet,
28+
getnewdestination,
29+
)
2230

23-
from test_framework.messages import BLOCK_HEADER_SIZE
2431

2532
INVALID_PARAM = "abc"
2633
UNKNOWN_PARAM = "0000000000000000000000000000000000000000000000000000000000000000"
@@ -43,14 +50,13 @@ def filter_output_indices_by_value(vouts, value):
4350

4451
class RESTTest (BitcoinTestFramework):
4552
def set_test_params(self):
46-
self.setup_clean_chain = True
4753
self.num_nodes = 2
4854
self.extra_args = [["-rest", "-blockfilterindex=1"], []]
55+
# whitelist peers to speed up tx relay / mempool sync
56+
for args in self.extra_args:
57+
args.append("[email protected]")
4958
self.supports_cli = False
5059

51-
def skip_test_if_missing_module(self):
52-
self.skip_if_no_wallet()
53-
5460
def test_rest_request(self, uri, http_method='GET', req_type=ReqType.JSON, body='', status=200, ret_type=RetType.JSON):
5561
rest_uri = '/rest' + uri
5662
if req_type == ReqType.JSON:
@@ -79,17 +85,11 @@ def test_rest_request(self, uri, http_method='GET', req_type=ReqType.JSON, body=
7985

8086
def run_test(self):
8187
self.url = urllib.parse.urlparse(self.nodes[0].url)
82-
self.log.info("Mine blocks and send Bitcoin to node 1")
88+
self.wallet = MiniWallet(self.nodes[0])
89+
self.wallet.rescan_utxos()
8390

84-
# Random address so node1's balance doesn't increase
85-
not_related_address = "2MxqoHEdNQTyYeX1mHcbrrpzgojbosTpCvJ"
86-
87-
self.generate(self.nodes[0], 1)
88-
self.generatetoaddress(self.nodes[1], 100, not_related_address)
89-
90-
assert_equal(self.nodes[0].getbalance(), 50)
91-
92-
txid = self.nodes[0].sendtoaddress(self.nodes[1].getnewaddress(), 0.1)
91+
self.log.info("Broadcast test transaction and sync nodes")
92+
txid, _ = self.wallet.send_to(from_node=self.nodes[0], scriptPubKey=getnewdestination()[1], amount=int(0.1 * COIN))
9393
self.sync_all()
9494

9595
self.log.info("Test the /tx URI")
@@ -115,11 +115,9 @@ def run_test(self):
115115

116116
self.log.info("Query an unspent TXO using the /getutxos URI")
117117

118-
self.generatetoaddress(self.nodes[1], 1, not_related_address)
118+
self.generate(self.wallet, 1)
119119
bb_hash = self.nodes[0].getbestblockhash()
120120

121-
assert_equal(self.nodes[1].getbalance(), Decimal("0.1"))
122-
123121
# Check chainTip response
124122
json_obj = self.test_rest_request(f"/getutxos/{spending[0]}-{spending[1]}")
125123
assert_equal(json_obj['chaintipHash'], bb_hash)
@@ -161,15 +159,15 @@ def run_test(self):
161159
response_hash = output.read(32)[::-1].hex()
162160

163161
assert_equal(bb_hash, response_hash) # check if getutxo's chaintip during calculation was fine
164-
assert_equal(chain_height, 102) # chain height must be 102
162+
assert_equal(chain_height, 201) # chain height must be 201 (pre-mined chain [200] + generated block [1])
165163

166164
self.log.info("Test the /getutxos URI with and without /checkmempool")
167165
# Create a transaction, check that it's found with /checkmempool, but
168166
# not found without. Then confirm the transaction and check that it's
169167
# found with or without /checkmempool.
170168

171169
# do a tx and don't sync
172-
txid = self.nodes[0].sendtoaddress(self.nodes[1].getnewaddress(), 0.1)
170+
txid, _ = self.wallet.send_to(from_node=self.nodes[0], scriptPubKey=getnewdestination()[1], amount=int(0.1 * COIN))
173171
json_obj = self.test_rest_request(f"/tx/{txid}")
174172
# get the spent output to later check for utxo (should be spent by then)
175173
spent = (json_obj['vin'][0]['txid'], json_obj['vin'][0]['vout'])
@@ -301,11 +299,13 @@ def run_test(self):
301299

302300
self.log.info("Test tx inclusion in the /mempool and /block URIs")
303301

304-
# Make 3 tx and mine them on node 1
302+
# Make 3 chained txs and mine them on node 1
305303
txs = []
306-
txs.append(self.nodes[0].sendtoaddress(not_related_address, 11))
307-
txs.append(self.nodes[0].sendtoaddress(not_related_address, 11))
308-
txs.append(self.nodes[0].sendtoaddress(not_related_address, 11))
304+
input_txid = txid
305+
for _ in range(3):
306+
utxo_to_spend = self.wallet.get_utxo(txid=input_txid)
307+
txs.append(self.wallet.send_self_transfer(from_node=self.nodes[0], utxo_to_spend=utxo_to_spend)['txid'])
308+
input_txid = txs[-1]
309309
self.sync_all()
310310

311311
# Check that there are exactly 3 transactions in the TX memory pool before generating the block
@@ -351,5 +351,6 @@ def run_test(self):
351351
json_obj = self.test_rest_request("/chaininfo")
352352
assert_equal(json_obj['bestblockhash'], bb_hash)
353353

354+
354355
if __name__ == '__main__':
355356
RESTTest().main()

0 commit comments

Comments
 (0)