|
3 | 3 | # Distributed under the MIT software license, see the accompanying
|
4 | 4 | # file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
5 | 5 | """Test the listtransactions API."""
|
| 6 | + |
| 7 | +import shutil |
| 8 | +import os |
| 9 | + |
6 | 10 | from decimal import Decimal
|
7 | 11 |
|
8 | 12 | from test_framework.messages import (
|
|
17 | 21 |
|
18 | 22 | class ListTransactionsTest(BitcoinTestFramework):
|
19 | 23 | def set_test_params(self):
|
20 |
| - self.num_nodes = 2 |
| 24 | + self.num_nodes = 3 |
21 | 25 | # This test isn't testing txn relay/timing, so set whitelist on the
|
22 | 26 | # peers for instant txn relay. This speeds up the test run time 2-3x.
|
23 | 27 | self. extra_args = [[ "[email protected]"]] * self. num_nodes
|
@@ -104,7 +108,7 @@ def run_test(self):
|
104 | 108 | {"txid": txid, "label": "watchonly"})
|
105 | 109 |
|
106 | 110 | self.run_rbf_opt_in_test()
|
107 |
| - |
| 111 | + self.run_externally_generated_address_test() |
108 | 112 |
|
109 | 113 | def run_rbf_opt_in_test(self):
|
110 | 114 | """Test the opt-in-rbf flag for sent and received transactions."""
|
@@ -210,5 +214,63 @@ def get_unconfirmed_utxo_entry(node, txid_to_match):
|
210 | 214 | assert_equal(self.nodes[0].gettransaction(txid_3b)["bip125-replaceable"], "no")
|
211 | 215 | assert_equal(self.nodes[0].gettransaction(txid_4)["bip125-replaceable"], "unknown")
|
212 | 216 |
|
| 217 | + def run_externally_generated_address_test(self): |
| 218 | + """Test behavior when receiving address is not in the address book.""" |
| 219 | + |
| 220 | + self.log.info("Setup the same wallet on two nodes") |
| 221 | + # refill keypool otherwise the second node wouldn't recognize addresses generated on the first nodes |
| 222 | + self.nodes[0].keypoolrefill(1000) |
| 223 | + self.stop_nodes() |
| 224 | + wallet0 = os.path.join(self.nodes[0].datadir, self.chain, self.default_wallet_name, "wallet.dat") |
| 225 | + wallet2 = os.path.join(self.nodes[2].datadir, self.chain, self.default_wallet_name, "wallet.dat") |
| 226 | + shutil.copyfile(wallet0, wallet2) |
| 227 | + self.start_nodes() |
| 228 | + # reconnect nodes |
| 229 | + self.connect_nodes(0, 1) |
| 230 | + self.connect_nodes(1, 2) |
| 231 | + self.connect_nodes(2, 0) |
| 232 | + |
| 233 | + addr1 = self.nodes[0].getnewaddress("pizza1", 'legacy') |
| 234 | + addr2 = self.nodes[0].getnewaddress("pizza2", 'p2sh-segwit') |
| 235 | + addr3 = self.nodes[0].getnewaddress("pizza3", 'bech32') |
| 236 | + |
| 237 | + self.log.info("Send to externally generated addresses") |
| 238 | + # send to an address beyond the next to be generated to test the keypool gap |
| 239 | + self.nodes[1].sendtoaddress(addr3, "0.001") |
| 240 | + self.nodes[1].generate(1) |
| 241 | + self.sync_all() |
| 242 | + |
| 243 | + # send to an address that is already marked as used due to the keypool gap mechanics |
| 244 | + self.nodes[1].sendtoaddress(addr2, "0.001") |
| 245 | + self.nodes[1].generate(1) |
| 246 | + self.sync_all() |
| 247 | + |
| 248 | + # send to self transaction |
| 249 | + self.nodes[0].sendtoaddress(addr1, "0.001") |
| 250 | + self.nodes[0].generate(1) |
| 251 | + self.sync_all() |
| 252 | + |
| 253 | + self.log.info("Verify listtransactions is the same regardless of where the address was generated") |
| 254 | + transactions0 = self.nodes[0].listtransactions() |
| 255 | + transactions2 = self.nodes[2].listtransactions() |
| 256 | + |
| 257 | + # normalize results: remove fields that normally could differ and sort |
| 258 | + def normalize_list(txs): |
| 259 | + for tx in txs: |
| 260 | + tx.pop('label', None) |
| 261 | + tx.pop('time', None) |
| 262 | + tx.pop('timereceived', None) |
| 263 | + txs.sort(key=lambda x: x['txid']) |
| 264 | + |
| 265 | + normalize_list(transactions0) |
| 266 | + normalize_list(transactions2) |
| 267 | + assert_equal(transactions0, transactions2) |
| 268 | + |
| 269 | + self.log.info("Verify labels are persistent on the node generated the addresses") |
| 270 | + assert_equal(['pizza1'], self.nodes[0].getaddressinfo(addr1)['labels']) |
| 271 | + assert_equal(['pizza2'], self.nodes[0].getaddressinfo(addr2)['labels']) |
| 272 | + assert_equal(['pizza3'], self.nodes[0].getaddressinfo(addr3)['labels']) |
| 273 | + |
| 274 | + |
213 | 275 | if __name__ == '__main__':
|
214 | 276 | ListTransactionsTest().main()
|
0 commit comments