@@ -945,6 +945,37 @@ bool CWallet::MarkReplaced(const uint256& originalHash, const uint256& newHash)
945945 return success;
946946}
947947
948+ void CWallet::SetUsedDestinationState (const uint256& hash, unsigned int n, bool used)
949+ {
950+ const CWalletTx* srctx = GetWalletTx (hash);
951+ if (!srctx) return ;
952+
953+ CTxDestination dst;
954+ if (ExtractDestination (srctx->tx ->vout [n].scriptPubKey , dst)) {
955+ if (::IsMine (*this , dst)) {
956+ LOCK (cs_wallet);
957+ if (used && !GetDestData (dst, " used" , nullptr )) {
958+ AddDestData (dst, " used" , " p" ); // p for "present", opposite of absent (null)
959+ } else if (!used && GetDestData (dst, " used" , nullptr )) {
960+ EraseDestData (dst, " used" );
961+ }
962+ }
963+ }
964+ }
965+
966+ bool CWallet::IsUsedDestination (const CTxDestination& dst) const
967+ {
968+ LOCK (cs_wallet);
969+ return ::IsMine (*this , dst) && GetDestData (dst, " used" , nullptr );
970+ }
971+
972+ bool CWallet::IsUsedDestination (const uint256& hash, unsigned int n) const
973+ {
974+ CTxDestination dst;
975+ const CWalletTx* srctx = GetWalletTx (hash);
976+ return srctx && ExtractDestination (srctx->tx ->vout [n].scriptPubKey , dst) && IsUsedDestination (dst);
977+ }
978+
948979bool CWallet::AddToWallet (const CWalletTx& wtxIn, bool fFlushOnClose )
949980{
950981 LOCK (cs_wallet);
@@ -953,6 +984,14 @@ bool CWallet::AddToWallet(const CWalletTx& wtxIn, bool fFlushOnClose)
953984
954985 uint256 hash = wtxIn.GetHash ();
955986
987+ if (IsWalletFlagSet (WALLET_FLAG_AVOID_REUSE)) {
988+ // Mark used destinations
989+ for (const CTxIn& txin : wtxIn.tx ->vin ) {
990+ const COutPoint& op = txin.prevout ;
991+ SetUsedDestinationState (op.hash , op.n , true );
992+ }
993+ }
994+
956995 // Inserts only if not already there, returns tx inserted or tx found
957996 std::pair<std::map<uint256, CWalletTx>::iterator, bool > ret = mapWallet.insert (std::make_pair (hash, wtxIn));
958997 CWalletTx& wtx = (*ret.first ).second ;
@@ -2072,7 +2111,7 @@ CAmount CWalletTx::GetAvailableCredit(interfaces::Chain::Lock& locked_chain, boo
20722111 return 0 ;
20732112
20742113 // Avoid caching ismine for NO or ALL cases (could remove this check and simplify in the future).
2075- bool allow_cache = filter == ISMINE_SPENDABLE || filter == ISMINE_WATCH_ONLY ;
2114+ bool allow_cache = ( filter & ISMINE_ALL) && ( filter & ISMINE_ALL) != ISMINE_ALL ;
20762115
20772116 // Must wait until coinbase is safely deep enough in the chain before valuing it
20782117 if (IsImmatureCoinBase (locked_chain))
@@ -2082,12 +2121,12 @@ CAmount CWalletTx::GetAvailableCredit(interfaces::Chain::Lock& locked_chain, boo
20822121 return m_amounts[AVAILABLE_CREDIT].m_value [filter];
20832122 }
20842123
2124+ bool allow_used_addresses = (filter & ISMINE_USED) || !pwallet->IsWalletFlagSet (WALLET_FLAG_AVOID_REUSE);
20852125 CAmount nCredit = 0 ;
20862126 uint256 hashTx = GetHash ();
20872127 for (unsigned int i = 0 ; i < tx->vout .size (); i++)
20882128 {
2089- if (!pwallet->IsSpent (locked_chain, hashTx, i))
2090- {
2129+ if (!pwallet->IsSpent (locked_chain, hashTx, i) && (allow_used_addresses || !pwallet->IsUsedDestination (hashTx, i))) {
20912130 const CTxOut &txout = tx->vout [i];
20922131 nCredit += pwallet->GetCredit (txout, filter);
20932132 if (!MoneyRange (nCredit))
@@ -2229,9 +2268,10 @@ void MaybeResendWalletTxs()
22292268 */
22302269
22312270
2232- CWallet::Balance CWallet::GetBalance (const int min_depth) const
2271+ CWallet::Balance CWallet::GetBalance (const int min_depth, bool avoid_reuse ) const
22332272{
22342273 Balance ret;
2274+ isminefilter reuse_filter = avoid_reuse ? 0 : ISMINE_USED;
22352275 {
22362276 auto locked_chain = chain ().lock ();
22372277 LOCK (cs_wallet);
@@ -2240,8 +2280,8 @@ CWallet::Balance CWallet::GetBalance(const int min_depth) const
22402280 const CWalletTx& wtx = entry.second ;
22412281 const bool is_trusted{wtx.IsTrusted (*locked_chain)};
22422282 const int tx_depth{wtx.GetDepthInMainChain (*locked_chain)};
2243- const CAmount tx_credit_mine{wtx.GetAvailableCredit (*locked_chain, /* fUseCache */ true , ISMINE_SPENDABLE)};
2244- const CAmount tx_credit_watchonly{wtx.GetAvailableCredit (*locked_chain, /* fUseCache */ true , ISMINE_WATCH_ONLY)};
2283+ const CAmount tx_credit_mine{wtx.GetAvailableCredit (*locked_chain, /* fUseCache */ true , ISMINE_SPENDABLE | reuse_filter )};
2284+ const CAmount tx_credit_watchonly{wtx.GetAvailableCredit (*locked_chain, /* fUseCache */ true , ISMINE_WATCH_ONLY | reuse_filter )};
22452285 if (is_trusted && tx_depth >= min_depth) {
22462286 ret.m_mine_trusted += tx_credit_mine;
22472287 ret.m_watchonly_trusted += tx_credit_watchonly;
@@ -2279,6 +2319,9 @@ void CWallet::AvailableCoins(interfaces::Chain::Lock& locked_chain, std::vector<
22792319
22802320 vCoins.clear ();
22812321 CAmount nTotal = 0 ;
2322+ // Either the WALLET_FLAG_AVOID_REUSE flag is not set (in which case we always allow), or we default to avoiding, and only in the case where
2323+ // a coin control object is provided, and has the avoid address reuse flag set to false, do we allow already used addresses
2324+ bool allow_used_addresses = !IsWalletFlagSet (WALLET_FLAG_AVOID_REUSE) || (coinControl && !coinControl->m_avoid_address_reuse );
22822325
22832326 for (const auto & entry : mapWallet)
22842327 {
@@ -2360,6 +2403,10 @@ void CWallet::AvailableCoins(interfaces::Chain::Lock& locked_chain, std::vector<
23602403 continue ;
23612404 }
23622405
2406+ if (!allow_used_addresses && IsUsedDestination (wtxid, i)) {
2407+ continue ;
2408+ }
2409+
23632410 bool solvable = IsSolvable (*this , wtx.tx ->vout [i].scriptPubKey );
23642411 bool spendable = ((mine & ISMINE_SPENDABLE) != ISMINE_NO) || (((mine & ISMINE_WATCH_ONLY) != ISMINE_NO) && (coinControl && coinControl->fAllowWatchOnly && solvable));
23652412
@@ -4150,16 +4197,12 @@ std::shared_ptr<CWallet> CWallet::CreateWalletFromFile(interfaces::Chain& chain,
41504197 // ensure this wallet.dat can only be opened by clients supporting HD with chain split and expects no default key
41514198 walletInstance->SetMinVersion (FEATURE_LATEST);
41524199
4153- if ((wallet_creation_flags & WALLET_FLAG_DISABLE_PRIVATE_KEYS)) {
4154- // selective allow to set flags
4155- walletInstance->SetWalletFlag (WALLET_FLAG_DISABLE_PRIVATE_KEYS);
4156- } else if (wallet_creation_flags & WALLET_FLAG_BLANK_WALLET) {
4157- walletInstance->SetWalletFlag (WALLET_FLAG_BLANK_WALLET);
4158- } else {
4200+ walletInstance->SetWalletFlags (wallet_creation_flags, false );
4201+ if (!(wallet_creation_flags & (WALLET_FLAG_DISABLE_PRIVATE_KEYS | WALLET_FLAG_BLANK_WALLET))) {
41594202 // generate a new seed
41604203 CPubKey seed = walletInstance->GenerateNewSeed ();
41614204 walletInstance->SetHDSeed (seed);
4162- } // Otherwise, do not generate a new seed
4205+ }
41634206
41644207 // Top up the keypool
41654208 if (walletInstance->CanGenerateKeys () && !walletInstance->TopUpKeyPool ()) {
0 commit comments