aboutsummaryrefslogtreecommitdiff
path: root/test/functional/wallet_change_address.py
diff options
context:
space:
mode:
authorglozow <gloriajzhao@gmail.com>2023-02-27 17:53:20 +0000
committerglozow <gloriajzhao@gmail.com>2023-02-27 18:09:31 +0000
commitc8c85ca16e6abbf9072dee9e526f09b6fcb35037 (patch)
tree2bf39d29063a9f2d22c2573d84390652b2593e59 /test/functional/wallet_change_address.py
parent2b87e905857ba456db2cb3d0297a3102f17417be (diff)
parent784a754aa47ce10c6fd99c09cdfc76ee9bc91652 (diff)
downloadbitcoin-c8c85ca16e6abbf9072dee9e526f09b6fcb35037.tar.xz
Merge bitcoin/bitcoin#26878: [24.x] Backports
784a754aa47ce10c6fd99c09cdfc76ee9bc91652 wallet, rpc: Update migratewallet help text for encrypted wallets (Andrew Chow) debcfe313a22fddc2a6247c55f3c7893f6134c05 tests: Tests for migrating wallets by name, and providing passphrase (Andrew Chow) ccc72fecd7287471bf8c3858a4e6c2ddadb64863 wallet: Be able to unlock the wallet for migration (Andrew Chow) 50dd8b13dfc070b550680617e76eff1c09af3a58 rpc: Allow users to specify wallet name for migratewallet (Andrew Chow) 648b06256da65b4513977e351aabf3e5c06ce060 wallet: Allow MigrateLegacyToDescriptor to take a wallet name (Andrew Chow) ab3bd457cdb5c5ee2626081d703d675c2573f28a i2p: use consistent number of tunnels with i2pd and Java I2P (Vasil Dimov) 29cdf42226c0983076701552b351f08351dd54a5 i2p: lower the number of tunnels for transient sessions (Vasil Dimov) 5027e93b6a1e5a5c87b9450c60e2a4572eb32653 i2p: reuse created I2P sessions if not used (Vasil Dimov) a62c541ae8a579d19b8fc55bbb922f0939ab6110 wallet: reuse change dest when recreating TX with avoidpartialspends (Matthew Zipkin) 64e7db6f4f256656f4d78a96b07e51f7d5c6d526 Zero out wallet master key upon lock (John Moffett) b7e242ecb3aa0074aea753e5bc9f8d22674e8294 Correctly limit overview transaction list (John Moffett) cff67180b3ba9ab53e01d44769059aa5559c01f7 depends: fix systemtap download URL (fanquake) 7cf73dfed5757819c0a5485ae05e8e1a57528a0e Add missing includes to fix gcc-13 compile error (MarcoFalke) 07397cdedeffb4da0aedd454d4539d65a0204291 addrdb: Only call Serialize() once (Martin Zumsande) 91f83dbeb197fc0fff574d9e29b4560b1d236bec hash: add HashedSourceWriter (Martin Zumsande) 5c824ac5e1e35f77e323319849b03ac9d8cf45d3 For feebump, ignore abandoned descendant spends (John Moffett) 428dcd51e6adab564ffb87ed678317924868572f wallet: Skip rescanning if wallet is more recent than tip (Andrew Chow) cbcdafa471da3d1edd183143ae9d433627ef16dd test: wallet: check that labels are migrated to watchonly wallet (Sebastian Falbesoner) 342abfb3f4368fcdb67f3002c5558d4106d9bf83 wallet: fully migrate address book entries for watchonly/solvable wallets (Sebastian Falbesoner) Pull request description: Backports: * https://github.com/bitcoin/bitcoin/pull/26595 * https://github.com/bitcoin/bitcoin/pull/26675 * https://github.com/bitcoin/bitcoin/pull/26679 * https://github.com/bitcoin/bitcoin/pull/26761 * https://github.com/bitcoin/bitcoin/pull/26837 * https://github.com/bitcoin/bitcoin/pull/26909 * https://github.com/bitcoin/bitcoin/pull/26924 * https://github.com/bitcoin/bitcoin/pull/26944 * https://github.com/bitcoin-core/gui/pull/704 * https://github.com/bitcoin/bitcoin/pull/27053 * https://github.com/bitcoin/bitcoin/pull/27080 ACKs for top commit: instagibbs: ACK https://github.com/bitcoin/bitcoin/pull/26878/commits/784a754aa47ce10c6fd99c09cdfc76ee9bc91652 achow101: ACK 784a754aa47ce10c6fd99c09cdfc76ee9bc91652 hebasto: ACK 784a754aa47ce10c6fd99c09cdfc76ee9bc91652, I've made backporting locally and got a diff between my branch and this PR as follows: Tree-SHA512: 8ea84aa02d7907ff1e202e1302b441ce9ed2198bf383620ad40056a5d7e8ea88e1047abef0b92d85648016bf9b3195c974be3806ccebd85bef4f85c326869e43
Diffstat (limited to 'test/functional/wallet_change_address.py')
-rwxr-xr-xtest/functional/wallet_change_address.py105
1 files changed, 105 insertions, 0 deletions
diff --git a/test/functional/wallet_change_address.py b/test/functional/wallet_change_address.py
new file mode 100755
index 0000000000..1c0dd09c82
--- /dev/null
+++ b/test/functional/wallet_change_address.py
@@ -0,0 +1,105 @@
+#!/usr/bin/env python3
+# Copyright (c) 2023 The Bitcoin Core developers
+# Distributed under the MIT software license, see the accompanying
+# file COPYING or http://www.opensource.org/licenses/mit-license.php.
+"""Test wallet change address selection"""
+
+import re
+
+from test_framework.blocktools import COINBASE_MATURITY
+from test_framework.test_framework import BitcoinTestFramework
+from test_framework.util import (
+ assert_equal,
+)
+
+
+class WalletChangeAddressTest(BitcoinTestFramework):
+ def set_test_params(self):
+ self.setup_clean_chain = True
+ self.num_nodes = 3
+ # discardfee is used to make change outputs less likely in the change_pos test
+ self.extra_args = [
+ [],
+ ["-discardfee=1"],
+ ["-avoidpartialspends", "-discardfee=1"]
+ ]
+
+ def skip_test_if_missing_module(self):
+ self.skip_if_no_wallet()
+
+ def assert_change_index(self, node, tx, index):
+ change_index = None
+ for vout in tx["vout"]:
+ info = node.getaddressinfo(vout["scriptPubKey"]["address"])
+ if (info["ismine"] and info["ischange"]):
+ change_index = int(re.findall(r'\d+', info["hdkeypath"])[-1])
+ break
+ assert_equal(change_index, index)
+
+ def assert_change_pos(self, wallet, tx, pos):
+ change_pos = None
+ for index, output in enumerate(tx["vout"]):
+ info = wallet.getaddressinfo(output["scriptPubKey"]["address"])
+ if (info["ismine"] and info["ischange"]):
+ change_pos = index
+ break
+ assert_equal(change_pos, pos)
+
+ def run_test(self):
+ self.log.info("Setting up")
+ # Mine some coins
+ self.generate(self.nodes[0], COINBASE_MATURITY + 1)
+
+ # Get some addresses from the two nodes
+ addr1 = [self.nodes[1].getnewaddress() for _ in range(3)]
+ addr2 = [self.nodes[2].getnewaddress() for _ in range(3)]
+ addrs = addr1 + addr2
+
+ # Send 1 + 0.5 coin to each address
+ [self.nodes[0].sendtoaddress(addr, 1.0) for addr in addrs]
+ [self.nodes[0].sendtoaddress(addr, 0.5) for addr in addrs]
+ self.generate(self.nodes[0], 1)
+
+ for i in range(20):
+ for n in [1, 2]:
+ self.log.debug(f"Send transaction from node {n}: expected change index {i}")
+ txid = self.nodes[n].sendtoaddress(self.nodes[0].getnewaddress(), 0.2)
+ tx = self.nodes[n].getrawtransaction(txid, True)
+ # find the change output and ensure that expected change index was used
+ self.assert_change_index(self.nodes[n], tx, i)
+
+ # Start next test with fresh wallets and new coins
+ self.nodes[1].createwallet("w1")
+ self.nodes[2].createwallet("w2")
+ w1 = self.nodes[1].get_wallet_rpc("w1")
+ w2 = self.nodes[2].get_wallet_rpc("w2")
+ addr1 = w1.getnewaddress()
+ addr2 = w2.getnewaddress()
+ self.nodes[0].sendtoaddress(addr1, 3.0)
+ self.nodes[0].sendtoaddress(addr1, 0.1)
+ self.nodes[0].sendtoaddress(addr2, 3.0)
+ self.nodes[0].sendtoaddress(addr2, 0.1)
+ self.generate(self.nodes[0], 1)
+
+ sendTo1 = self.nodes[0].getnewaddress()
+ sendTo2 = self.nodes[0].getnewaddress()
+ sendTo3 = self.nodes[0].getnewaddress()
+
+ # The avoid partial spends wallet will always create a change output
+ node = self.nodes[2]
+ res = w2.send(outputs=[{sendTo1: 1.0}, {sendTo2: 1.0}, {sendTo3: 0.9999}], options={"change_position": 0})
+ tx = node.getrawtransaction(res["txid"], True)
+ self.assert_change_pos(w2, tx, 0)
+
+ # The default wallet will internally create a tx without change first,
+ # then create a second candidate using APS that requires a change output.
+ # Ensure that the user-configured change position is kept
+ node = self.nodes[1]
+ res = w1.send(outputs=[{sendTo1: 1.0}, {sendTo2: 1.0}, {sendTo3: 0.9999}], options={"change_position": 0})
+ tx = node.getrawtransaction(res["txid"], True)
+ # If the wallet ignores the user's change_position there is still a 25%
+ # that the random change position passes the test
+ self.assert_change_pos(w1, tx, 0)
+
+if __name__ == '__main__':
+ WalletChangeAddressTest().main()