mirror of
https://github.com/ElementsProject/elements.git
synced 2026-08-17 13:07:54 +02:00
Merge ac61ec9da6 into merged_master (Bitcoin PR #17843)
This commit is contained in:
commit
8bbbb28dae
3 changed files with 63 additions and 5 deletions
|
|
@ -714,7 +714,7 @@ bool CWallet::MarkReplaced(const uint256& originalHash, const uint256& newHash)
|
|||
return success;
|
||||
}
|
||||
|
||||
void CWallet::SetUsedDestinationState(WalletBatch& batch, const uint256& hash, unsigned int n, bool used)
|
||||
void CWallet::SetUsedDestinationState(WalletBatch& batch, const uint256& hash, unsigned int n, bool used, std::set<CTxDestination>& tx_destinations)
|
||||
{
|
||||
AssertLockHeld(cs_wallet);
|
||||
const CWalletTx* srctx = GetWalletTx(hash);
|
||||
|
|
@ -724,7 +724,9 @@ void CWallet::SetUsedDestinationState(WalletBatch& batch, const uint256& hash, u
|
|||
if (ExtractDestination(srctx->tx->vout[n].scriptPubKey, dst)) {
|
||||
if (IsMine(dst)) {
|
||||
if (used && !GetDestData(dst, "used", nullptr)) {
|
||||
AddDestData(batch, dst, "used", "p"); // p for "present", opposite of absent (null)
|
||||
if (AddDestData(batch, dst, "used", "p")) { // p for "present", opposite of absent (null)
|
||||
tx_destinations.insert(dst);
|
||||
}
|
||||
} else if (!used && GetDestData(dst, "used", nullptr)) {
|
||||
EraseDestData(batch, dst, "used");
|
||||
}
|
||||
|
|
@ -771,10 +773,14 @@ bool CWallet::AddToWallet(const CWalletTx& wtxIn, bool fFlushOnClose)
|
|||
|
||||
if (IsWalletFlagSet(WALLET_FLAG_AVOID_REUSE)) {
|
||||
// Mark used destinations
|
||||
std::set<CTxDestination> tx_destinations;
|
||||
|
||||
for (const CTxIn& txin : wtxIn.tx->vin) {
|
||||
const COutPoint& op = txin.prevout;
|
||||
SetUsedDestinationState(batch, op.hash, op.n, true);
|
||||
SetUsedDestinationState(batch, op.hash, op.n, true, tx_destinations);
|
||||
}
|
||||
|
||||
MarkDestinationsDirty(tx_destinations);
|
||||
}
|
||||
|
||||
// Inserts only if not already there, returns tx inserted or tx found
|
||||
|
|
@ -3879,6 +3885,21 @@ int64_t CWallet::GetOldestKeyPoolTime()
|
|||
return oldestKey;
|
||||
}
|
||||
|
||||
void CWallet::MarkDestinationsDirty(const std::set<CTxDestination>& destinations) {
|
||||
for (auto& entry : mapWallet) {
|
||||
CWalletTx& wtx = entry.second;
|
||||
|
||||
for (unsigned int i = 0; i < wtx.tx->vout.size(); i++) {
|
||||
CTxDestination dst;
|
||||
|
||||
if (ExtractDestination(wtx.tx->vout[i].scriptPubKey, dst) && destinations.count(dst)) {
|
||||
wtx.MarkDirty();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::map<CTxDestination, CAmount> CWallet::GetAddressBalances(interfaces::Chain::Lock& locked_chain)
|
||||
{
|
||||
std::map<CTxDestination, CAmount> balances;
|
||||
|
|
|
|||
|
|
@ -916,7 +916,7 @@ public:
|
|||
|
||||
// Whether this or any known UTXO with the same single key has been spent.
|
||||
bool IsUsedDestination(const uint256& hash, unsigned int n) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
|
||||
void SetUsedDestinationState(WalletBatch& batch, const uint256& hash, unsigned int n, bool used) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
|
||||
void SetUsedDestinationState(WalletBatch& batch, const uint256& hash, unsigned int n, bool used, std::set<CTxDestination>& tx_destinations) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
|
||||
|
||||
std::vector<OutputGroup> GroupOutputs(const std::vector<COutput>& outputs, bool single_coin) const;
|
||||
|
||||
|
|
@ -1076,6 +1076,12 @@ public:
|
|||
|
||||
std::set<CTxDestination> GetLabelAddresses(const std::string& label) const;
|
||||
|
||||
/**
|
||||
* Marks all outputs in each one of the destinations dirty, so their cache is
|
||||
* reset and does not return outdated information.
|
||||
*/
|
||||
void MarkDestinationsDirty(const std::set<CTxDestination>& destinations) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
|
||||
|
||||
bool GetOnlinePakKey(CPubKey& online_pubkey, std::string& error);
|
||||
bool GetNewDestination(const OutputType type, const std::string label, CTxDestination& dest, std::string& error, bool add_blinding_key = false);
|
||||
bool GetNewChangeDestination(const OutputType type, CTxDestination& dest, std::string& error, bool add_blinding_key = false);
|
||||
|
|
|
|||
|
|
@ -91,7 +91,8 @@ class AvoidReuseTest(BitcoinTestFramework):
|
|||
self.test_fund_send_fund_send("p2sh-segwit")
|
||||
reset_balance(self.nodes[1], self.nodes[0].getnewaddress())
|
||||
self.test_fund_send_fund_send("bech32")
|
||||
|
||||
reset_balance(self.nodes[1], self.nodes[0].getnewaddress())
|
||||
self.test_getbalances_used()
|
||||
|
||||
def test_persistence(self):
|
||||
'''Test that wallet files persist the avoid_reuse flag.'''
|
||||
|
|
@ -257,5 +258,35 @@ class AvoidReuseTest(BitcoinTestFramework):
|
|||
assert_approx(self.nodes[1].getbalance()["bitcoin"], 1, 0.001)
|
||||
assert_approx(self.nodes[1].getbalance(avoid_reuse=False)["bitcoin"], 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), {'bitcoin': 0 })
|
||||
|
||||
new_addr = self.nodes[1].getnewaddress()
|
||||
ret_addr = self.nodes[0].getnewaddress()
|
||||
|
||||
# send multiple transactions, reusing one address
|
||||
for _ in range(11):
|
||||
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=6, reused_count=1, reused_sum=1)
|
||||
assert_balances(self.nodes[1], mine={"used": 1, "trusted": 5})
|
||||
|
||||
if __name__ == '__main__':
|
||||
AvoidReuseTest().main()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue