mirror of
https://github.com/dashpay/dash.git
synced 2024-12-25 03:52:49 +01:00
f293c046f4
223588b1bbc63dc57098bbd0baa48635e0cc0b82 Add a --descriptors option to various tests (Andrew Chow) 869f7ab30aeb4d7fbd563c535b55467a8a0430cf tests: Add RPCOverloadWrapper which overloads some disabled RPCs (Andrew Chow) cf060628590fab87d73f278e744d70ef2d5d81db Correctly check for default wallet (Andrew Chow) 886e0d75f5fea2421190aa4812777d89f68962cc Implement CWallet::IsSpentKey for non-LegacySPKMans (Andrew Chow) 3c19fdd2a2fd5394fcfa75b2ba84ab2277cbdabf Return error when no ScriptPubKeyMan is available for specified type (Andrew Chow) 388ba94231f2f10a0be751c562cdd4650510a90a Change wallet_encryption.py to use signmessage instead of dumpprivkey (Andrew Chow) 1346e14831489f9c8f53a08f9dfed61d55d53c6f Functional tests for descriptor wallets (Andrew Chow) f193ea889ddb53d9a5c47647966681d525e38368 add importdescriptors RPC and tests for native descriptor wallets (Hugo Nguyen) ce24a944940019185efebcc5d85eac458ed26016 Add IsLegacy to CWallet so that the GUI knows whether to show watchonly (Andrew Chow) 1cb42b22b11c27e64462afc25a94b2fc50bfa113 Generate new descriptors when encrypting (Andrew Chow) 82ae02b1656819f4bd5023b8955447e1d4ea8692 Be able to create new wallets with DescriptorScriptPubKeyMans as backing (Andrew Chow) b713baa75a62335ab9c0eed9ef76a95bfec30668 Implement GetMetadata in DescriptorScriptPubKeyMan (Andrew Chow) 8b9603bd0b443e2f7984eb72bf2e21cf02af0bcb Change GetMetadata to use unique_ptr<CKeyMetadata> (Andrew Chow) 72a9540df96ffdb94f039b9c14eaacdc7d961196 Implement FillPSBT in DescriptorScriptPubKeyMan (Andrew Chow) 84b4978c02102171775c77a45f6ec198930f0a88 Implement SignMessage for descriptor wallets (Andrew Chow) bde7c9fa38775a81d53ac0484fa9c98076a0c7d1 Implement SignTransaction in DescriptorScriptPubKeyMan (Andrew Chow) d50c8ddd4190f20bf0debd410348b73408ec3143 Implement GetSolvingProvider for DescriptorScriptPubKeyMan (Andrew Chow) f1ca5feb4ad668a3e1ae543d0addd5f483f1a88f Implement GetKeypoolOldestTime and only display it if greater than 0 (Andrew Chow) 586b57a9a6b4b12a78f792785b63a5a1743bce0c Implement ReturnDestination in DescriptorScriptPubKeyMan (Andrew Chow) f866957979c23cefd41efa9dae9e53b9177818dc Implement GetReservedDestination in DescriptorScriptPubKeyMan (Andrew Chow) a775f7c7fd0b9094fcbeee6ba92206d5bbb19164 Implement Unlock and Encrypt in DescriptorScriptPubKeyMan (Andrew Chow) bfdd0734869a22217c15858d7a76d0dacc2ebc86 Implement GetNewDestination for DescriptorScriptPubKeyMan (Andrew Chow) 58c7651821b0eeff0a99dc61d78d2e9e07986580 Implement TopUp in DescriptorScriptPubKeyMan (Andrew Chow) e014886a342508f7c8d80323eee9a5f314eaf94c Implement SetupGeneration for DescriptorScriptPubKeyMan (Andrew Chow) 46dfb99768e7d03a3cf552812d5b41ceaebc06be Implement writing descriptorkeys, descriptorckeys, and descriptors to wallet file (Andrew Chow) 4cb9b69be031e1dc65d8964794781b347fd948f5 Implement several simple functions in DescriptorScriptPubKeyMan (Andrew Chow) d1ec3e4f19487b4b100f80ad02eac063c571777d Add IsSingleType to Descriptors (Andrew Chow) 953feb3d2724f5398dd48990c4957a19313d2c8c Implement loading of keys for DescriptorScriptPubKeyMan (Andrew Chow) 2363e9fcaa41b68bf11153f591b95f2d41ff9a1a Load the descriptor cache from the wallet file (Andrew Chow) 46c46aebb7943e1e2e96755e94dc6c197920bf75 Implement GetID for DescriptorScriptPubKeyMan (Andrew Chow) ec2f9e1178c8e38c0a5ca063fe81adac8f916348 Implement IsHDEnabled in DescriptorScriptPubKeyMan (Andrew Chow) 741122d4c1a62ced3e96d16d67f4eeb3a6522d99 Implement MarkUnusedAddresses in DescriptorScriptPubKeyMan (Andrew Chow) 2db7ca765c8fb2c71dd6f7c4f29ad70e68ff1720 Implement IsMine for DescriptorScriptPubKeyMan (Andrew Chow) db7177af8c159abbcc209f2caafcd45d54c181c5 Add LoadDescriptorScriptPubKeyMan and SetActiveScriptPubKeyMan to CWallet (Andrew Chow) 78f8a92910d34247fa5d04368338c598d9908267 Implement SetType in DescriptorScriptPubKeyMan (Andrew Chow) 834de0300cde57ca3f662fb7aa5b1bdaed68bc8f Store WalletDescriptor in DescriptorScriptPubKeyMan (Andrew Chow) d8132669e10c1db9ae0c2ea0d3f822d7d2f01345 Add a lock cs_desc_man for DescriptorScriptPubKeyMan (Andrew Chow) 3194a7f88ac1a32997b390b4f188c4b6a4af04a5 Introduce WalletDescriptor class (Andrew Chow) 6b13cd3fa854dfaeb9e269bff3d67cacc0e5b5dc Create LegacyScriptPubKeyMan when not a descriptor wallet (Andrew Chow) aeac157c9dc141546b45e06ba9c2e641ad86083f Return nullptr from GetLegacyScriptPubKeyMan if descriptor wallet (Andrew Chow) 96accc73f067c7c95946e9932645dd821ef67f63 Add WALLET_FLAG_DESCRIPTORS (Andrew Chow) 6b8119af53ee2fdb4c4b5b24b4e650c0dc3bd27c Introduce DescriptorScriptPubKeyMan as a dummy class (Andrew Chow) 06620302c713cae65ee8e4ff9302e4c88e2a1285 Introduce SetType function to tell ScriptPubKeyMans the type and internal-ness of it (Andrew Chow) Pull request description: Introducing the wallet of the glorious future (again): native descriptor wallets. With native descriptor wallets, addresses are generated from descriptors. Instead of generating keys and deriving addresses from keys, addresses come from the scriptPubKeys produced by a descriptor. Native descriptor wallets will be optional for now and can only be created by using `createwallet`. Descriptor wallets will store descriptors, master keys from the descriptor, and descriptor cache entries. Keys are derived from descriptors on the fly. In order to allow choosing different address types, 6 descriptors are needed for normal use. There is a pair of primary and change descriptors for each of the 3 address types. With the default keypool size of 1000, each descriptor has 1000 scriptPubKeys and descriptor cache entries pregenerated. This has a side effect of making wallets large since 6000 pubkeys are written to the wallet by default, instead of the current 2000. scriptPubKeys are kept only in memory and are generated every time a descriptor is loaded. By default, we use the standard BIP 44, 49, 84 derivation paths with an external and internal derivation chain for each. Descriptors can also be imported with a new `importdescriptors` RPC. Native descriptor wallets use the `ScriptPubKeyMan` interface introduced in #16341 to add a `DescriptorScriptPubKeyMan`. This defines a different IsMine which uses the simpler model of "does this scriptPubKey exist in this wallet". Furthermore, `DescriptorScriptPubKeyMan` does not have watchonly, so with native descriptor wallets, it is not possible to have a wallet with both watchonly and non-watchonly things. Rather a wallet with `disable_private_keys` needs to be used for watchonly things. A `--descriptor` option was added to some tests (`wallet_basic.py`, `wallet_encryption.py`, `wallet_keypool.py`, `wallet_keypool_topup.py`, and `wallet_labels.py`) to allow for these tests to use descriptor wallets. Additionally, several RPCs are disabled for descriptor wallets (`importprivkey`, `importpubkey`, `importaddress`, `importmulti`, `addmultisigaddress`, `dumpprivkey`, `dumpwallet`, `importwallet`, and `sethdseed`). ACKs for top commit: Sjors: utACK 223588b1bbc63dc57098bbd0baa48635e0cc0b82 (rebased, nits addressed) jonatack: Code review re-ACK 223588b1bbc63dc57098bbd0baa48635e0cc0b82. fjahr: re-ACK 223588b1bbc63dc57098bbd0baa48635e0cc0b82 instagibbs: light re-ACK 223588b meshcollider: Code review ACK 223588b1bbc63dc57098bbd0baa48635e0cc0b82 Tree-SHA512: 59bc52aeddbb769ed5f420d5d240d8137847ac821b588eb616b34461253510c1717d6a70bab8765631738747336ae06f45ba39603ccd17f483843e5ed9a90986 Introduce SetType function to tell ScriptPubKeyMans the type and internal-ness of it Introduce DescriptorScriptPubKeyMan as a dummy class Add WALLET_FLAG_DESCRIPTORS Return nullptr from GetLegacyScriptPubKeyMan if descriptor wallet Create LegacyScriptPubKeyMan when not a descriptor wallet Introduce WalletDescriptor class WalletDescriptor is a Descriptor with other wallet metadata Add a lock cs_desc_man for DescriptorScriptPubKeyMan Store WalletDescriptor in DescriptorScriptPubKeyMan Implement SetType in DescriptorScriptPubKeyMan Add LoadDescriptorScriptPubKeyMan and SetActiveScriptPubKeyMan to CWallet Implement IsMine for DescriptorScriptPubKeyMan Adds a set of scriptPubKeys that DescriptorScriptPubKeyMan tracks. If the given script is in that set, it is considered ISMINE_SPENDABLE Implement MarkUnusedAddresses in DescriptorScriptPubKeyMan Implement IsHDEnabled in DescriptorScriptPubKeyMan Implement GetID for DescriptorScriptPubKeyMan Load the descriptor cache from the wallet file Implement loading of keys for DescriptorScriptPubKeyMan Add IsSingleType to Descriptors IsSingleType will return whether the descriptor will give one or multiple scriptPubKeys Implement several simple functions in DescriptorScriptPubKeyMan Implements a bunch of one liners: UpgradeKeyMetadata, IsFirstRun, HavePrivateKeys, KeypoolCountExternalKeys, GetKeypoolSize, GetTimeFirstKey, CanGetAddresses, RewriteDB Implement writing descriptorkeys, descriptorckeys, and descriptors to wallet file Implement SetupGeneration for DescriptorScriptPubKeyMan Implement TopUp in DescriptorScriptPubKeyMan Implement GetNewDestination for DescriptorScriptPubKeyMan Implement Unlock and Encrypt in DescriptorScriptPubKeyMan Implement GetReservedDestination in DescriptorScriptPubKeyMan Implement ReturnDestination in DescriptorScriptPubKeyMan Implement GetKeypoolOldestTime and only display it if greater than 0 Implement GetSolvingProvider for DescriptorScriptPubKeyMan Internally, a GetSigningProvider function is introduced which allows for some private keys to be optionally included. This can be called with a script as the argument (i.e. a scriptPubKey from our wallet when we are signing) or with a pubkey. In order to know what index to expand the private keys for that pubkey, we need to also cache all of the pubkeys involved when we expand the descriptor. So SetCache and TopUp are updated to do this too. Implement SignTransaction in DescriptorScriptPubKeyMan Implement SignMessage for descriptor wallets Implement FillPSBT in DescriptorScriptPubKeyMan FillPSBT will add our own scripts to the PSBT if those inputs are ours. If an input also lists pubkeys that we happen to know the private keys for, we will sign those inputs too. Change GetMetadata to use unique_ptr<CKeyMetadata> Implement GetMetadata in DescriptorScriptPubKeyMan Be able to create new wallets with DescriptorScriptPubKeyMans as backing Generate new descriptors when encrypting Add IsLegacy to CWallet so that the GUI knows whether to show watchonly add importdescriptors RPC and tests for native descriptor wallets Co-authored-by: Andrew Chow <achow101-github@achow101.com> Functional tests for descriptor wallets Change wallet_encryption.py to use signmessage instead of dumpprivkey Return error when no ScriptPubKeyMan is available for specified type When a CWallet doesn't have a ScriptPubKeyMan for the requested type in GetNewDestination, give a meaningful error. Also handle this in Qt which did not do anything with errors. Implement CWallet::IsSpentKey for non-LegacySPKMans tests: Add RPCOverloadWrapper which overloads some disabled RPCs RPCOverloadWrapper overloads some deprecated or disabled RPCs with an implementation using other RPCs to avoid having a ton of code churn around replacing those RPCs. Add a --descriptors option to various tests Adds a --descriptors option globally to the test framework. This will make the test create and use descriptor wallets. However some tests may not work with this. Some tests are modified to work with --descriptors and run with that option in test_runer: * wallet_basic.py * wallet_encryption.py * wallet_keypool.py <---- wallet_keypool_hd.py actually * wallet_keypool_topup.py * wallet_labels.py * wallet_avoidreuse.py
365 lines
15 KiB
Python
Executable File
365 lines
15 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
# Copyright (c) 2018-2020 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 the avoid_reuse and setwalletflag features."""
|
|
|
|
from test_framework.test_framework import BitcoinTestFramework
|
|
from test_framework.util import (
|
|
assert_approx,
|
|
assert_equal,
|
|
assert_raises_rpc_error,
|
|
)
|
|
|
|
def reset_balance(node, discardaddr):
|
|
'''Throw away all owned coins by the node so it gets a balance of 0.'''
|
|
balance = node.getbalance(avoid_reuse=False)
|
|
if balance > 0.5:
|
|
node.sendtoaddress(address=discardaddr, amount=balance, subtractfeefromamount=True, avoid_reuse=False)
|
|
|
|
def count_unspent(node):
|
|
'''Count the unspent outputs for the given node and return various statistics'''
|
|
r = {
|
|
"total": {
|
|
"count": 0,
|
|
"sum": 0,
|
|
},
|
|
"reused": {
|
|
"count": 0,
|
|
"sum": 0,
|
|
},
|
|
}
|
|
supports_reused = True
|
|
for utxo in node.listunspent(minconf=0):
|
|
r["total"]["count"] += 1
|
|
r["total"]["sum"] += utxo["amount"]
|
|
if supports_reused and "reused" in utxo:
|
|
if utxo["reused"]:
|
|
r["reused"]["count"] += 1
|
|
r["reused"]["sum"] += utxo["amount"]
|
|
else:
|
|
supports_reused = False
|
|
r["reused"]["supported"] = supports_reused
|
|
return r
|
|
|
|
def assert_unspent(node, total_count=None, total_sum=None, reused_supported=None, reused_count=None, reused_sum=None, margin=0.001):
|
|
'''Make assertions about a node's unspent output statistics'''
|
|
stats = count_unspent(node)
|
|
if total_count is not None:
|
|
assert_equal(stats["total"]["count"], total_count)
|
|
if total_sum is not None:
|
|
assert_approx(stats["total"]["sum"], total_sum, margin)
|
|
if reused_supported is not None:
|
|
assert_equal(stats["reused"]["supported"], reused_supported)
|
|
if reused_count is not None:
|
|
assert_equal(stats["reused"]["count"], reused_count)
|
|
if reused_sum is not None:
|
|
assert_approx(stats["reused"]["sum"], reused_sum, margin)
|
|
|
|
def assert_balances(node, mine, margin=0.001):
|
|
'''Make assertions about a node's getbalances output'''
|
|
got = node.getbalances()["mine"]
|
|
for k,v in mine.items():
|
|
assert_approx(got[k], v, margin)
|
|
|
|
class AvoidReuseTest(BitcoinTestFramework):
|
|
|
|
def set_test_params(self):
|
|
self.num_nodes = 2
|
|
# This test isn't testing txn relay/timing, so set whitelist on the
|
|
# peers for instant txn relay. This speeds up the test run time 2-3x.
|
|
self.extra_args = [["-whitelist=noban@127.0.0.1"]] * self.num_nodes
|
|
|
|
def skip_test_if_missing_module(self):
|
|
self.skip_if_no_wallet()
|
|
|
|
def run_test(self):
|
|
'''Set up initial chain and run tests defined below'''
|
|
|
|
self.test_persistence()
|
|
self.test_immutable()
|
|
|
|
self.nodes[0].generate(110)
|
|
self.sync_all()
|
|
self.test_change_remains_change(self.nodes[1])
|
|
reset_balance(self.nodes[1], self.nodes[0].getnewaddress())
|
|
self.test_sending_from_reused_address_without_avoid_reuse()
|
|
reset_balance(self.nodes[1], self.nodes[0].getnewaddress())
|
|
self.test_sending_from_reused_address_fails()
|
|
reset_balance(self.nodes[1], self.nodes[0].getnewaddress())
|
|
self.test_getbalances_used()
|
|
reset_balance(self.nodes[1], self.nodes[0].getnewaddress())
|
|
self.test_full_destination_group_is_preferred()
|
|
reset_balance(self.nodes[1], self.nodes[0].getnewaddress())
|
|
self.test_all_destination_groups_are_used()
|
|
|
|
def test_persistence(self):
|
|
'''Test that wallet files persist the avoid_reuse flag.'''
|
|
self.log.info("Test wallet files persist avoid_reuse flag")
|
|
|
|
# Configure node 1 to use avoid_reuse
|
|
self.nodes[1].setwalletflag('avoid_reuse')
|
|
|
|
# Flags should be node1.avoid_reuse=false, node2.avoid_reuse=true
|
|
assert_equal(self.nodes[0].getwalletinfo()["avoid_reuse"], False)
|
|
assert_equal(self.nodes[1].getwalletinfo()["avoid_reuse"], True)
|
|
|
|
# Stop and restart node 1
|
|
self.restart_node(1)
|
|
self.connect_nodes(0, 1)
|
|
|
|
# Flags should still be node1.avoid_reuse=false, node2.avoid_reuse=true
|
|
assert_equal(self.nodes[0].getwalletinfo()["avoid_reuse"], False)
|
|
assert_equal(self.nodes[1].getwalletinfo()["avoid_reuse"], True)
|
|
|
|
# Attempting to set flag to its current state should throw
|
|
assert_raises_rpc_error(-8, "Wallet flag is already set to false", self.nodes[0].setwalletflag, 'avoid_reuse', False)
|
|
assert_raises_rpc_error(-8, "Wallet flag is already set to true", self.nodes[1].setwalletflag, 'avoid_reuse', True)
|
|
|
|
def test_immutable(self):
|
|
'''Test immutable wallet flags'''
|
|
self.log.info("Test immutable wallet flags")
|
|
|
|
# Attempt to set the disable_private_keys flag; this should not work
|
|
assert_raises_rpc_error(-8, "Wallet flag is immutable", self.nodes[1].setwalletflag, 'disable_private_keys')
|
|
|
|
tempwallet = ".wallet_avoidreuse.py_test_immutable_wallet.dat"
|
|
|
|
# Create a wallet with disable_private_keys set; this should work
|
|
self.nodes[1].createwallet(wallet_name=tempwallet, disable_private_keys=True)
|
|
w = self.nodes[1].get_wallet_rpc(tempwallet)
|
|
|
|
# Attempt to unset the disable_private_keys flag; this should not work
|
|
assert_raises_rpc_error(-8, "Wallet flag is immutable", w.setwalletflag, 'disable_private_keys', False)
|
|
|
|
# Unload temp wallet
|
|
self.nodes[1].unloadwallet(tempwallet)
|
|
|
|
def test_change_remains_change(self, node):
|
|
self.log.info("Test that change doesn't turn into non-change when spent")
|
|
|
|
reset_balance(node, node.getnewaddress())
|
|
addr = node.getnewaddress()
|
|
txid = node.sendtoaddress(addr, 1)
|
|
out = node.listunspent(minconf=0, query_options={'minimumAmount': 2})
|
|
assert_equal(len(out), 1)
|
|
assert_equal(out[0]['txid'], txid)
|
|
changeaddr = out[0]['address']
|
|
|
|
# Make sure it's starting out as change as expected
|
|
assert node.getaddressinfo(changeaddr)['ischange']
|
|
for logical_tx in node.listtransactions():
|
|
assert logical_tx.get('address') != changeaddr
|
|
|
|
# Spend it
|
|
reset_balance(node, node.getnewaddress())
|
|
|
|
# It should still be change
|
|
assert node.getaddressinfo(changeaddr)['ischange']
|
|
for logical_tx in node.listtransactions():
|
|
assert logical_tx.get('address') != changeaddr
|
|
|
|
def test_sending_from_reused_address_without_avoid_reuse(self):
|
|
'''
|
|
Test the same as test_sending_from_reused_address_fails, except send the 10 BTC with
|
|
the avoid_reuse flag set to false. This means the 10 BTC send should succeed,
|
|
where it fails in test_sending_from_reused_address_fails.
|
|
'''
|
|
self.log.info("Test sending from reused address with avoid_reuse=false")
|
|
|
|
fundaddr = self.nodes[1].getnewaddress()
|
|
retaddr = self.nodes[0].getnewaddress()
|
|
|
|
self.nodes[0].sendtoaddress(fundaddr, 10)
|
|
self.nodes[0].generate(1)
|
|
self.sync_all()
|
|
|
|
# listunspent should show 1 single, unused 10 btc output
|
|
assert_unspent(self.nodes[1], total_count=1, total_sum=10, reused_supported=True, reused_count=0)
|
|
# getbalances should show no used, 10 btc trusted
|
|
assert_balances(self.nodes[1], mine={"used": 0, "trusted": 10})
|
|
# node 0 should not show a used entry, as it does not enable avoid_reuse
|
|
assert("used" not in self.nodes[0].getbalances()["mine"])
|
|
|
|
self.nodes[1].sendtoaddress(retaddr, 5)
|
|
self.nodes[0].generate(1)
|
|
self.sync_all()
|
|
|
|
# listunspent should show 1 single, unused 5 btc output
|
|
assert_unspent(self.nodes[1], total_count=1, total_sum=5, reused_supported=True, reused_count=0)
|
|
# getbalances should show no used, 5 btc trusted
|
|
assert_balances(self.nodes[1], mine={"used": 0, "trusted": 5})
|
|
|
|
self.nodes[0].sendtoaddress(fundaddr, 10)
|
|
self.nodes[0].generate(1)
|
|
self.sync_all()
|
|
|
|
# listunspent should show 2 total outputs (5, 10 btc), one unused (5), one reused (10)
|
|
assert_unspent(self.nodes[1], total_count=2, total_sum=15, reused_count=1, reused_sum=10)
|
|
# getbalances should show 10 used, 5 btc trusted
|
|
assert_balances(self.nodes[1], mine={"used": 10, "trusted": 5})
|
|
|
|
self.nodes[1].sendtoaddress(address=retaddr, amount=10, avoid_reuse=False)
|
|
|
|
# listunspent should show 1 total outputs (5 btc), unused
|
|
assert_unspent(self.nodes[1], total_count=1, total_sum=5, reused_count=0)
|
|
# getbalances should show no used, 5 btc trusted
|
|
assert_balances(self.nodes[1], mine={"used": 0, "trusted": 5})
|
|
|
|
# node 1 should now have about 5 btc left (for both cases)
|
|
assert_approx(self.nodes[1].getbalance(), 5, 0.001)
|
|
assert_approx(self.nodes[1].getbalance(avoid_reuse=False), 5, 0.001)
|
|
|
|
def test_sending_from_reused_address_fails(self):
|
|
'''
|
|
Test the simple case where [1] generates a new address A, then
|
|
[0] sends 10 BTC to A.
|
|
[1] spends 5 BTC from A. (leaving roughly 5 BTC useable)
|
|
[0] sends 10 BTC to A again.
|
|
[1] tries to spend 10 BTC (fails; dirty).
|
|
[1] tries to spend 4 BTC (succeeds; change address sufficient)
|
|
'''
|
|
self.log.info("Test sending from reused address fails")
|
|
|
|
fundaddr = self.nodes[1].getnewaddress(label="")
|
|
retaddr = self.nodes[0].getnewaddress()
|
|
|
|
self.nodes[0].sendtoaddress(fundaddr, 10)
|
|
self.nodes[0].generate(1)
|
|
self.sync_all()
|
|
|
|
# listunspent should show 1 single, unused 10 btc output
|
|
assert_unspent(self.nodes[1], total_count=1, total_sum=10, reused_supported=True, reused_count=0)
|
|
# getbalances should show no used, 10 btc trusted
|
|
assert_balances(self.nodes[1], mine={"used": 0, "trusted": 10})
|
|
|
|
self.nodes[1].sendtoaddress(retaddr, 5)
|
|
self.nodes[0].generate(1)
|
|
self.sync_all()
|
|
|
|
# listunspent should show 1 single, unused 5 btc output
|
|
assert_unspent(self.nodes[1], total_count=1, total_sum=5, reused_supported=True, reused_count=0)
|
|
# getbalances should show no used, 5 btc trusted
|
|
assert_balances(self.nodes[1], mine={"used": 0, "trusted": 5})
|
|
|
|
if not self.options.descriptors:
|
|
self.nodes[0].sendtoaddress(fundaddr, 10)
|
|
self.nodes[0].generate(1)
|
|
self.sync_all()
|
|
|
|
# listunspent should show 2 total outputs (5, 10 btc), one unused (5), one reused (10)
|
|
assert_unspent(self.nodes[1], total_count=2, total_sum=15, reused_count=1, reused_sum=10)
|
|
# getbalances should show 10 used, 5 btc trusted
|
|
assert_balances(self.nodes[1], mine={"used": 10, "trusted": 5})
|
|
|
|
# node 1 should now have a balance of 5 (no dirty) or 15 (including dirty)
|
|
assert_approx(self.nodes[1].getbalance(), 5, 0.001)
|
|
assert_approx(self.nodes[1].getbalance(avoid_reuse=False), 15, 0.001)
|
|
|
|
assert_raises_rpc_error(-6, "Insufficient funds", self.nodes[1].sendtoaddress, retaddr, 10)
|
|
|
|
self.nodes[1].sendtoaddress(retaddr, 4)
|
|
|
|
# listunspent should show 2 total outputs (1, 10 btc), one unused (1), one reused (10)
|
|
assert_unspent(self.nodes[1], total_count=2, total_sum=11, reused_count=1, reused_sum=10)
|
|
# getbalances should show 10 used, 1 btc trusted
|
|
assert_balances(self.nodes[1], mine={"used": 10, "trusted": 1})
|
|
|
|
# node 1 should now have about 1 btc left (no dirty) and 11 (including dirty)
|
|
assert_approx(self.nodes[1].getbalance(), 1, 0.001)
|
|
assert_approx(self.nodes[1].getbalance(avoid_reuse=False), 11, 0.001)
|
|
|
|
def test_getbalances_used(self):
|
|
'''
|
|
getbalances and listunspent should pick up on reused addresses
|
|
immediately, even for address reusing outputs created before the first
|
|
transaction was spending from that address
|
|
'''
|
|
self.log.info("Test getbalances used category")
|
|
|
|
# node under test should be completely empty
|
|
assert_equal(self.nodes[1].getbalance(avoid_reuse=False), 0)
|
|
|
|
new_addr = self.nodes[1].getnewaddress()
|
|
ret_addr = self.nodes[0].getnewaddress()
|
|
|
|
# send multiple transactions, reusing one address
|
|
for _ in range(101):
|
|
self.nodes[0].sendtoaddress(new_addr, 1)
|
|
|
|
self.nodes[0].generate(1)
|
|
self.sync_all()
|
|
|
|
# send transaction that should not use all the available outputs
|
|
# per the current coin selection algorithm
|
|
self.nodes[1].sendtoaddress(ret_addr, 5)
|
|
|
|
# getbalances and listunspent should show the remaining outputs
|
|
# in the reused address as used/reused
|
|
assert_unspent(self.nodes[1], total_count=2, total_sum=96, reused_count=1, reused_sum=1, margin=0.01)
|
|
assert_balances(self.nodes[1], mine={"used": 1, "trusted": 95}, margin=0.01)
|
|
|
|
def test_full_destination_group_is_preferred(self):
|
|
'''
|
|
Test the case where [1] only has 101 outputs of 1 BTC in the same reused
|
|
address and tries to send a small payment of 0.5 BTC. The wallet
|
|
should use 100 outputs from the reused address as inputs and not a
|
|
single 1 BTC input, in order to join several outputs from the reused
|
|
address.
|
|
'''
|
|
self.log.info("Test that full destination groups are preferred in coin selection")
|
|
|
|
# Node under test should be empty
|
|
assert_equal(self.nodes[1].getbalance(avoid_reuse=False), 0)
|
|
|
|
new_addr = self.nodes[1].getnewaddress()
|
|
ret_addr = self.nodes[0].getnewaddress()
|
|
|
|
# Send 101 outputs of 1 BTC to the same, reused address in the wallet
|
|
for _ in range(101):
|
|
self.nodes[0].sendtoaddress(new_addr, 1)
|
|
|
|
self.nodes[0].generate(1)
|
|
self.sync_all()
|
|
|
|
# Sending a transaction that is smaller than each one of the
|
|
# available outputs
|
|
txid = self.nodes[1].sendtoaddress(address=ret_addr, amount=0.5)
|
|
inputs = self.nodes[1].getrawtransaction(txid, 1)["vin"]
|
|
|
|
# The transaction should use 100 inputs exactly
|
|
assert_equal(len(inputs), 100)
|
|
|
|
def test_all_destination_groups_are_used(self):
|
|
'''
|
|
Test the case where [1] only has 202 outputs of 1 BTC in the same reused
|
|
address and tries to send a payment of 200.5 BTC. The wallet
|
|
should use all 202 outputs from the reused address as inputs.
|
|
'''
|
|
self.log.info("Test that all destination groups are used")
|
|
|
|
# Node under test should be empty
|
|
assert_equal(self.nodes[1].getbalance(avoid_reuse=False), 0)
|
|
|
|
new_addr = self.nodes[1].getnewaddress()
|
|
ret_addr = self.nodes[0].getnewaddress()
|
|
|
|
# Send 202 outputs of 1 BTC to the same, reused address in the wallet
|
|
for _ in range(202):
|
|
self.nodes[0].sendtoaddress(new_addr, 1)
|
|
|
|
self.nodes[0].generate(1)
|
|
self.sync_all()
|
|
|
|
# Sending a transaction that needs to use the full groups
|
|
# of 100 inputs but also the incomplete group of 2 inputs.
|
|
txid = self.nodes[1].sendtoaddress(address=ret_addr, amount=200.5)
|
|
inputs = self.nodes[1].getrawtransaction(txid, 1)["vin"]
|
|
|
|
# The transaction should use 202 inputs exactly
|
|
assert_equal(len(inputs), 202)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
AvoidReuseTest().main()
|