From 299014d1f05fc27c80890a96f0a4aaa5c5c3aecc Mon Sep 17 00:00:00 2001 From: pasta Date: Fri, 13 Nov 2026 02:49:55 -0600 Subject: [PATCH] feat(wallet): owe a rescan for each DIP-15 receiving chain the wallet gains A DashPay contact made on another device, or before a seed restore, can pay our receiving chain for that friendship before this wallet holds it. Those payments are in blocks the wallet has already scanned, so importing the chain, or moving its birth back to an earlier contact request, owes a rescan from that birth. The wallet kept no record of that: the caller had to remember it, and a crash between the import and that note left the chain imported, ensureFriendshipReceivingKeychain() a no-op, and the payments missing. The wallet now owns that obligation: - ensureFriendshipReceivingKeychain() writes an owed-rescan record (DBKeys::OWED_RESCAN) before it imports a chain, lowers a held chain's birth, or gives a chain held without its key that key. If that write fails the chain is left as it was. A chain held with its key and born no later is left untouched. Lowering a held chain's birth also lowers the wallet's birth time, as a reload would. - owedRescanFrom() says where the owed rescan starts, and rescanFromTime(T) runs it on the caller's thread: it scans the blocks a key born at T may be paid in, as importdescriptors does, or only replays the mempool when no block is that recent. - Each record has a sequence number. A rescanFromTime() that completes settles the records it covers that were recorded before it started, so a chain imported while a rescan runs stays owed. An aborted or failed rescan settles nothing, and neither does any other rescan, which costs at most one extra scan. - Records survive a restart. Older versions skip them as unknown. A failed wallet database write during the import or the rescan throws, as it does on develop and for importdescriptors, and is not handled here. Settling relies on the scan's status, so this depends on bitcoin#35512: without it a scan from genesis aborted before its first block reports success. Co-Authored-By: Claude Fable 5.2 --- src/interfaces/wallet.h | 21 ++- src/wallet/interfaces.cpp | 37 +++++ src/wallet/platformtypes.h | 4 +- src/wallet/test/platformkeys_tests.cpp | 212 +++++++++++++++++++++++-- src/wallet/test/wallet_tests.cpp | 17 ++ src/wallet/test/walletdb_tests.cpp | 5 +- src/wallet/wallet.cpp | 54 ++++++- src/wallet/wallet.h | 16 ++ src/wallet/walletdb.cpp | 23 +++ src/wallet/walletdb.h | 5 + 10 files changed, 372 insertions(+), 22 deletions(-) diff --git a/src/interfaces/wallet.h b/src/interfaces/wallet.h index dc2a6e91411d..eab51d5a36a7 100644 --- a/src/interfaces/wallet.h +++ b/src/interfaces/wallet.h @@ -113,6 +113,14 @@ class Wallet //! Initiate a rescan. Returns status indicating success, failure, abort, or already rescanning. virtual wallet::RescanStatus startRescan(bool from_genesis) = 0; + //! Rescan, on the calling thread, the blocks a key born at from_time + //! (UNIX time in seconds) may be paid in, as importdescriptors does. + //! Returns as startRescan() does. Only SUCCESS settles the owed rescans + //! it covers that were recorded before it started; no other rescan + //! settles any. Throws std::runtime_error if a wallet database write + //! fails: reload the wallet before running it again. + virtual wallet::RescanStatus rescanFromTime(int64_t from_time) = 0; + //! Abort a rescan. virtual void abortRescan() = 0; @@ -120,6 +128,11 @@ class Wallet //! blocks it would read; nullopt when it has them all. virtual std::optional rescanPrunedFrom() = 0; + //! The time, in seconds, the rescan this wallet owes starts from, for + //! receiving chains it was given or whose birth moved earlier; nullopt + //! when it owes none. Run it with rescanFromTime(). + virtual std::optional owedRescanFrom() = 0; + //! Lock masternode collaterals virtual void autoLockMasternodeCollaterals() = 0; @@ -172,8 +185,12 @@ class Wallet const wallet::CompactXpub& compact_xpub) = 0; //! Ensure our private DIP-15 receiving chain is imported as a ranged - //! descriptor and return the corresponding public chain. Derivation, - //! descriptor update and result publication are one wallet-locked action. + //! descriptor born no later than request.birth_time (in seconds) and + //! return the corresponding public chain. Derivation, descriptor update + //! and result publication are one wallet-locked action. Importing the + //! chain, or moving its birth earlier, first records a rescan owed from + //! that birth (see owedRescanFrom()). May throw std::runtime_error if a + //! wallet database write fails, as importdescriptors does. virtual wallet::PlatformKeyResult ensureFriendshipReceivingKeychain( const wallet::FriendshipKeychainRequest& request) = 0; diff --git a/src/wallet/interfaces.cpp b/src/wallet/interfaces.cpp index 10ecf2114814..9a83d6ce2d3c 100644 --- a/src/wallet/interfaces.cpp +++ b/src/wallet/interfaces.cpp @@ -224,6 +224,38 @@ class WalletImpl : public Wallet Assume(false); // unreachable return wallet::RescanStatus::FAILURE; // fallback for release builds } + wallet::RescanStatus rescanFromTime(int64_t from_time) override + { + from_time = std::max(from_time, 0); + WalletRescanReserver reserver(*m_wallet); + if (!reserver.reserve()) { + return wallet::RescanStatus::BUSY; + } + // Taken before looking for blocks: a chain imported later may be paid + // in one that arrives meanwhile. + const uint64_t owed{WITH_LOCK(m_wallet->cs_wallet, return m_wallet->GetOwedRescanSequence())}; + uint256 start_hash; + int start_height{0}; + auto status{CWallet::ScanResult::SUCCESS}; + if (m_wallet->chain().findFirstBlockWithTimeAndHeight(from_time - TIMESTAMP_WINDOW, /*min_height=*/0, + FoundBlock().hash(start_hash).height(start_height))) { + status = m_wallet->ScanForWalletTransactions(start_hash, start_height, /*max_height=*/std::nullopt, reserver, + /*fUpdate=*/true, /*save_progress=*/false).status; + } else { + // No block is that recent: only the mempool can pay the key. + WITH_LOCK(m_wallet->cs_wallet, m_wallet->chain().requestMempoolTransactions(*m_wallet)); + } + switch (status) { + case CWallet::ScanResult::FAILURE: + return wallet::RescanStatus::FAILURE; + case CWallet::ScanResult::USER_ABORT: + return wallet::RescanStatus::USER_ABORT; + case CWallet::ScanResult::SUCCESS: + break; + } + WITH_LOCK(m_wallet->cs_wallet, m_wallet->SettleOwedRescans(owed, from_time)); + return wallet::RescanStatus::SUCCESS; + } void abortRescan() override { m_wallet->AbortRescan(); } std::optional rescanPrunedFrom() override { @@ -233,6 +265,11 @@ class WalletImpl : public Wallet if (m_wallet->chain().hasBlocks(m_wallet->GetLastBlockHash(), rescan_height)) return std::nullopt; return rescan_height; } + std::optional owedRescanFrom() override + { + LOCK(m_wallet->cs_wallet); + return m_wallet->GetOwedRescanFrom(); + } void autoLockMasternodeCollaterals() override { m_wallet->AutoLockMasternodeCollaterals(); } bool backupWallet(const std::string& filename) override { return m_wallet->BackupWallet(filename); } bool autoBackupWallet(const fs::path& wallet_path, bilingual_str& error_string, std::vector& warnings) override diff --git a/src/wallet/platformtypes.h b/src/wallet/platformtypes.h index 25af37fd1896..aa6a3f24075c 100644 --- a/src/wallet/platformtypes.h +++ b/src/wallet/platformtypes.h @@ -80,7 +80,9 @@ struct FriendshipKeychainRequest { uint32_t account{0}; uint256 local_id; uint256 remote_id; - int64_t birth_time{0}; //!< 0 means unknown/genesis. + //! UNIX time in seconds, as a descriptor's creation time (Platform's + //! $createdAt is in milliseconds); 0 means unknown, i.e. genesis. + int64_t birth_time{0}; }; struct FriendshipXpub { diff --git a/src/wallet/test/platformkeys_tests.cpp b/src/wallet/test/platformkeys_tests.cpp index 088b7c84ad3c..5f8a0d849018 100644 --- a/src/wallet/test/platformkeys_tests.cpp +++ b/src/wallet/test/platformkeys_tests.cpp @@ -2,7 +2,9 @@ // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. +#include #include +#include #include #include #include @@ -12,6 +14,7 @@ #include #include #include +#include #include #include #include @@ -28,6 +31,8 @@ #include #include #include +#include +#include #include using namespace wallet; @@ -717,34 +722,207 @@ BOOST_FIXTURE_TEST_CASE(dip15_compact_xpub_account_reference_and_payment_vectors //! A payment received before the loss must become spendable again once the //! restored wallet re-imports the friendship keychain: the recovery contract -//! for DIP-15 funds. +//! for DIP-15 funds. The restored wallet has already scanned the payment's +//! block, so the import owes a rescan from the chain's birth, and an earlier +//! request for the same chain moves that birth back and owes another. BOOST_FIXTURE_TEST_CASE(recovery_import_after_restore_is_spendable, SeededWalletPair) { + const auto tip{[&] { return WITH_LOCK(::cs_main, return m_node.chainman->ActiveChain().Tip()); }}; // The payment targets the xpub the contact holds — exported by wallet A - // before the loss. + // before the loss in its first request — and is mined. + const int64_t first_request{tip()->GetBlockTime()}; const auto original_keychain{m_iface->ensureFriendshipReceivingKeychain( - FriendshipKeychainRequest{ACCOUNT, m_my_id, m_their_id, /*birth_time=*/0})}; + FriendshipKeychainRequest{ACCOUNT, m_my_id, m_their_id, first_request})}; BOOST_REQUIRE(original_keychain); CTxDestination destination; BOOST_REQUIRE(DeriveFriendshipPaymentDestination(original_keychain.value, /*index=*/0, destination)); - const CScript script{GetScriptForDestination(destination)}; - CMutableTransaction payment; - payment.vin.emplace_back(g_insecure_rand_ctx.rand256(), 0); - payment.vout.emplace_back(COIN, script); - - // Restored wallet B: not ours until the friendship keychain is back. + const CMutableTransaction payment{CreateValidMempoolTransaction(m_coinbase_txns[0], /*input_vout=*/0, + /*input_height=*/1, coinbaseKey, + GetScriptForDestination(destination), COIN, + /*submit=*/false)}; + CreateAndProcessBlock({payment}, GetScriptForRawPubKey(coinbaseKey.GetPubKey())); + // A second request for the same chain, sent long after the payment. + SetMockTime(GetTime() + 2 * TIMESTAMP_WINDOW); + CreateAndProcessBlock({}, GetScriptForRawPubKey(coinbaseKey.GetPubKey())); + const int64_t second_request{tip()->GetBlockTime()}; + + // Restored wallet B has scanned the whole chain without the friendship + // keychain: the payment is not ours. + WITH_LOCK(m_wallet_b->cs_wallet, m_wallet_b->SetLastBlockProcessed(tip()->nHeight, tip()->GetBlockHash())); BOOST_CHECK_EQUAL(IsMineIn(*m_wallet_b, destination), ISMINE_NO); + const auto spendable_coins{[&] { + LOCK(m_wallet_b->cs_wallet); + return AvailableCoins(*m_wallet_b).Size(); + }}; + BOOST_CHECK(!m_iface_b->owedRescanFrom()); + + // Imported from the second request only, the rescan it owes starts after the payment. + BOOST_REQUIRE(m_iface_b->ensureFriendshipReceivingKeychain( + FriendshipKeychainRequest{ACCOUNT, m_my_id, m_their_id, second_request})); + BOOST_CHECK_EQUAL(IsMineIn(*m_wallet_b, destination), ISMINE_SPENDABLE); + BOOST_REQUIRE(m_iface_b->owedRescanFrom() == std::optional{second_request}); + BOOST_CHECK(m_iface_b->rescanFromTime(second_request) == RescanStatus::SUCCESS); + BOOST_CHECK(!m_iface_b->owedRescanFrom()); + BOOST_CHECK_EQUAL(spendable_coins(), 0U); + + // The first request makes the chain older, and the rescan that owes finds the payment. + const FriendshipKeychainRequest earliest{ACCOUNT, m_my_id, m_their_id, first_request}; + BOOST_REQUIRE(m_iface_b->ensureFriendshipReceivingKeychain(earliest)); + BOOST_REQUIRE(m_iface_b->owedRescanFrom() == std::optional{first_request}); + + // A rescan aborted before it reads a block settles nothing, even one from + // genesis, and what is owed outlives a restart. + { + const auto abort_at_start{m_iface_b->handleShowProgress([&](const std::string&, int percent) { + if (percent == 0) m_iface_b->abortRescan(); + })}; + BOOST_CHECK(m_iface_b->rescanFromTime(0) == RescanStatus::USER_ABORT); + } + BOOST_CHECK_EQUAL(spendable_coins(), 0U); + const auto restarted{ + std::make_shared(m_node.chain.get(), "", DuplicateMockDatabase(m_wallet_b->GetDatabase()))}; + BOOST_REQUIRE_EQUAL(restarted->LoadWallet(), DBErrors::LOAD_OK); + BOOST_CHECK(interfaces::MakeWallet(m_context, restarted)->owedRescanFrom() == std::optional{first_request}); + + const auto owed{m_iface_b->owedRescanFrom()}; + BOOST_REQUIRE(owed == std::optional{first_request}); + BOOST_CHECK(m_iface_b->rescanFromTime(*owed) == RescanStatus::SUCCESS); + BOOST_CHECK(!m_iface_b->owedRescanFrom()); + BOOST_CHECK_EQUAL(spendable_coins(), 1U); +} - const auto imported{m_iface_b->ensureFriendshipReceivingKeychain( - FriendshipKeychainRequest{ACCOUNT, m_my_id, m_their_id, /*birth_time=*/0})}; - BOOST_REQUIRE(imported); +//! The birth of a receiving chain and what the owed-rescan scenarios share. +struct OwedRescanSetup : public FriendshipWalletSetup { + const int64_t m_birth{WITH_LOCK(::cs_main, return m_node.chainman->ActiveChain().Tip()->GetBlockTime())}; + + FriendshipKeychainRequest Request(const uint256& their_id, int64_t birth_time) const + { + return FriendshipKeychainRequest{ACCOUNT, m_my_id, their_id, birth_time}; + } + //! The wallet as a restart would load it. + std::shared_ptr Restart(CWallet& wallet) + { + auto restarted{std::make_shared(m_node.chain.get(), "", DuplicateMockDatabase(wallet.GetDatabase()))}; + BOOST_REQUIRE_EQUAL(restarted->LoadWallet(), DBErrors::LOAD_OK); + return restarted; + } + //! What the wallet owes after a restart. + std::optional OwedAfterRestart(CWallet& wallet) + { + return interfaces::MakeWallet(m_context, Restart(wallet))->owedRescanFrom(); + } +}; - LOCK(m_wallet_b->cs_wallet); - m_wallet_b->AddToWallet(MakeTransactionRef(payment), TxStateInMempool{}); +//! Importing a receiving chain, or moving its birth earlier, owes a rescan +//! from that birth, recorded before the chain changes. +BOOST_FIXTURE_TEST_CASE(friendship_import_owes_rescan, OwedRescanSetup) +{ + BOOST_CHECK(!m_iface->owedRescanFrom()); + const MockableData without_chain{GetMockableDatabase(*m_wallet).m_records}; + const auto keychain{m_iface->ensureFriendshipReceivingKeychain(Request(m_their_id, m_birth))}; + BOOST_REQUIRE(keychain); + BOOST_CHECK(m_iface->owedRescanFrom() == std::optional{m_birth}); + BOOST_CHECK(OwedAfterRestart(*m_wallet) == std::optional{m_birth}); + // A restarted wallet keeps the records it loaded when it adds one. + const auto restarted{Restart(*m_wallet)}; + BOOST_REQUIRE(interfaces::MakeWallet(m_context, restarted)->ensureFriendshipReceivingKeychain(Request(uint256::ONE, m_birth + 10))); + BOOST_CHECK(OwedAfterRestart(*restarted) == std::optional{m_birth}); - CCoinControl coin_control; - coin_control.m_include_unsafe_inputs = true; - BOOST_CHECK_EQUAL(AvailableCoins(*m_wallet_b, &coin_control).Size(), 1U); + CTxDestination destination; + BOOST_REQUIRE(DeriveFriendshipPaymentDestination(keychain.value, /*index=*/0, destination)); + const auto held_birth{[&] { + LOCK(m_wallet->cs_wallet); + const auto chain{m_wallet->GetScriptPubKeyMans(GetScriptForDestination(destination))}; + BOOST_REQUIRE_EQUAL(chain.size(), 1U); + return (*chain.begin())->GetTimeFirstKey(); + }}; + + // A later or the same birth leaves the chain as it is, without a write. + GetMockableDatabase(*m_wallet).m_pass = false; + BOOST_CHECK(m_iface->ensureFriendshipReceivingKeychain(Request(m_their_id, m_birth + 10))); + BOOST_CHECK(m_iface->ensureFriendshipReceivingKeychain(Request(m_their_id, m_birth))); + BOOST_CHECK_EQUAL(held_birth(), m_birth); + + // When the owed rescan cannot be saved, the chain is left as it was. + BOOST_CHECK(m_iface->ensureFriendshipReceivingKeychain(Request(m_their_id, m_birth - 10)).status == + PlatformKeyStatus::IMPORT_ERROR); + GetMockableDatabase(*m_wallet).m_pass = true; + BOOST_CHECK_EQUAL(held_birth(), m_birth); + BOOST_CHECK(m_iface->owedRescanFrom() == std::optional{m_birth}); + + // An earlier birth for a chain the wallet holds owes a rescan from it, + // and the wallet's birth, which later blocks are scanned from, follows. + BOOST_REQUIRE(m_iface->ensureFriendshipReceivingKeychain(Request(m_their_id, m_birth - 10))); + BOOST_CHECK_EQUAL(held_birth(), m_birth - 10); + BOOST_CHECK_EQUAL(m_wallet->GetBirthTime(), m_birth - 10); + BOOST_CHECK(m_iface->owedRescanFrom() == std::optional{m_birth - 10}); + BOOST_CHECK(OwedAfterRestart(*m_wallet) == std::optional{m_birth - 10}); + + // A chain held without its key is given it, and owes a rescan as a new one does. + WalletDescriptor without_key{[&] { + LOCK(m_wallet->cs_wallet); + const auto& chain{dynamic_cast( + **m_wallet->GetScriptPubKeyMans(GetScriptForDestination(destination)).begin())}; + LOCK(chain.cs_desc_man); + return chain.GetWalletDescriptor(); + }()}; + without_key.cache = DescriptorCache{}; + const auto wallet{std::make_shared(m_node.chain.get(), "", std::make_unique(without_chain))}; + BOOST_REQUIRE_EQUAL(wallet->LoadWallet(), DBErrors::LOAD_OK); + BOOST_REQUIRE(WITH_LOCK(wallet->cs_wallet, return wallet->AddWalletDescriptor(without_key, FlatSigningProvider{}, + /*label=*/"", /*internal=*/false))); + const auto iface{interfaces::MakeWallet(m_context, wallet)}; + BOOST_REQUIRE(iface->ensureFriendshipReceivingKeychain(Request(m_their_id, m_birth - 10))); + BOOST_CHECK(iface->owedRescanFrom() == std::optional{m_birth - 10}); +} + +//! A rescan owed for a receiving chain is settled by a completed rescan from +//! no later than its birth that started after it was recorded. +BOOST_FIXTURE_TEST_CASE(friendship_owed_rescan_settles, OwedRescanSetup) +{ + BOOST_REQUIRE(m_iface->ensureFriendshipReceivingKeychain(Request(m_their_id, m_birth))); + // A rescan from after the birth does not cover it. + BOOST_CHECK(m_iface->rescanFromTime(m_birth + 1) == RescanStatus::SUCCESS); + BOOST_CHECK(m_iface->owedRescanFrom() == std::optional{m_birth}); + BOOST_CHECK(m_iface->rescanFromTime(m_birth) == RescanStatus::SUCCESS); + BOOST_CHECK(!m_iface->owedRescanFrom()); + BOOST_CHECK(!OwedAfterRestart(*m_wallet)); + + // A chain imported while a rescan runs may be paid in a block it has + // already read: the rescan it owes outlives the one running. + const uint256 other_id{uint256S("3333333333333333333333333333333333333333333333333333333333333333")}; + BOOST_REQUIRE(m_iface->ensureFriendshipReceivingKeychain(Request(m_their_id, m_birth - 10))); + { + bool imported_while_scanning{false}; + const auto progress{m_iface->handleShowProgress([&](const std::string&, int) { + if (std::exchange(imported_while_scanning, true)) return; + BOOST_CHECK(m_iface->ensureFriendshipReceivingKeychain(Request(other_id, m_birth))); + })}; + BOOST_CHECK(m_iface->rescanFromTime(m_birth - 10) == RescanStatus::SUCCESS); + BOOST_REQUIRE(imported_while_scanning); + } + BOOST_CHECK(m_iface->owedRescanFrom() == std::optional{m_birth}); + BOOST_CHECK(m_iface->rescanFromTime(m_birth) == RescanStatus::SUCCESS); + BOOST_CHECK(!m_iface->owedRescanFrom()); + + // Born after every block: the rescan reads no block and replays the mempool. + const int64_t unmined{m_birth + 2 * TIMESTAMP_WINDOW}; + const auto unmined_chain{m_iface->ensureFriendshipReceivingKeychain(Request(uint256::ONE, unmined))}; + BOOST_REQUIRE(unmined_chain); + CTxDestination unconfirmed_destination; + BOOST_REQUIRE(DeriveFriendshipPaymentDestination(unmined_chain.value, /*index=*/0, unconfirmed_destination)); + const CMutableTransaction unconfirmed{ + CreateValidMempoolTransaction(m_coinbase_txns[0], /*input_vout=*/0, + /*input_height=*/1, coinbaseKey, GetScriptForDestination(unconfirmed_destination))}; + const auto holds_unconfirmed{[&] { + LOCK(m_wallet->cs_wallet); + return m_wallet->GetWalletTx(unconfirmed.GetHash()) != nullptr; + }}; + BOOST_CHECK(!holds_unconfirmed()); + BOOST_CHECK(m_iface->owedRescanFrom() == std::optional{unmined}); + BOOST_CHECK(m_iface->rescanFromTime(unmined) == RescanStatus::SUCCESS); + BOOST_CHECK(!m_iface->owedRescanFrom()); + BOOST_CHECK(holds_unconfirmed()); } //! Recovery re-runs re-import friendships it already imported; that must diff --git a/src/wallet/test/wallet_tests.cpp b/src/wallet/test/wallet_tests.cpp index 462ee572c58b..44e2bfd531f7 100644 --- a/src/wallet/test/wallet_tests.cpp +++ b/src/wallet/test/wallet_tests.cpp @@ -219,6 +219,23 @@ BOOST_FIXTURE_TEST_CASE(scan_for_wallet_transactions, TestChain100Setup) m_node.chainman->m_blockman.UnlinkPrunedFiles({file_number}); BOOST_CHECK(rescan_pruned_from() == std::optional{0}); + // A rescan that cannot read every block it needs fails, and the rescan + // owed from before the pruned blocks stays owed. + { + const auto wallet{std::make_shared(m_node.chain.get(), "", CreateMockableWalletDatabase())}; + { + LOCK(wallet->cs_wallet); + wallet->SetWalletFlag(WALLET_FLAG_DESCRIPTORS); + wallet->SetLastBlockProcessed(newTip->nHeight, newTip->GetBlockHash()); + BOOST_REQUIRE(wallet->AddOwedRescan(0)); + } + AddKey(*wallet, coinbaseKey); + WalletContext context; + const auto iface{interfaces::MakeWallet(context, wallet)}; + BOOST_CHECK(iface->rescanFromTime(0) == RescanStatus::FAILURE); + BOOST_CHECK(iface->owedRescanFrom() == std::optional{0}); + } + // Verify ScanForWalletTransactions only picks transactions in the new block // file. { diff --git a/src/wallet/test/walletdb_tests.cpp b/src/wallet/test/walletdb_tests.cpp index b3c0dc72bfcc..00149a1439db 100644 --- a/src/wallet/test/walletdb_tests.cpp +++ b/src/wallet/test/walletdb_tests.cpp @@ -132,7 +132,7 @@ BOOST_AUTO_TEST_CASE(walletdb_dash_record_load_policy) { // Damage to an hdpubkey or g_object record is noncritical. An HD chain // whose encryption state disagrees with its record type, or with the - // wallet's, is corrupt. + // wallet's, is corrupt, and so is an unreadable owed_rescan record. CHDChain plain_chain; BOOST_REQUIRE(plain_chain.SetSeed(SecureVector(32, 0x01), /*fUpdateID=*/true)); CHDChain crypted_chain{plain_chain}; @@ -161,6 +161,9 @@ BOOST_AUTO_TEST_CASE(walletdb_dash_record_load_policy) {[&](DatabaseBatch& batch) { return batch.Write(std::make_pair(DBKeys::G_OBJECT, uint256::ONE), Span{truncated}); }, DBErrors::NONCRITICAL_ERROR}, + {[&](DatabaseBatch& batch) { + return batch.Write(std::make_pair(DBKeys::OWED_RESCAN, uint64_t{1}), Span{truncated}); + }, DBErrors::CORRUPT}, }; for (size_t i{0}; i < rows.size(); ++i) { CWallet wallet(m_node.chain.get(), "", CreateMockableWalletDatabase()); diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index 18719bce0f55..e74082438f99 100644 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -4293,12 +4293,22 @@ PlatformKeyResult CWallet::EnsureFriendshipReceivingKeychain(con descriptor.range_start = current.range_start; descriptor.range_end = std::max(descriptor.range_end, current.range_end); descriptor.next_index = current.next_index; + // Held with its key and born no later: no block was scanned without it. + if (descriptor.creation_time >= current.creation_time && existing->HavePrivateKeys()) { + result.value = {key.key.GetPubKey(), key.chaincode, key.parent_fingerprint}; + return result; + } descriptor.creation_time = std::min(descriptor.creation_time, current.creation_time); } - if (!AddWalletDescriptor(descriptor, provider, /*label=*/"", /*internal=*/false)) { + // Recorded before the chain changes: a crash in between costs a needless + // rescan rather than missed payments. + if (!AddOwedRescan(descriptor.creation_time) || + !AddWalletDescriptor(descriptor, provider, /*label=*/"", /*internal=*/false)) { result.status = PlatformKeyStatus::IMPORT_ERROR; return result; } + // A held chain is not an active one, so nothing else lowers the wallet's birth to its new one. + MaybeUpdateBirthTime(descriptor.creation_time); result.value = {key.key.GetPubKey(), key.chaincode, key.parent_fingerprint}; return result; @@ -4337,6 +4347,48 @@ PlatformKeyResult> CWallet::ListAssetLockFundings( return result; } +void CWallet::LoadOwedRescan(uint64_t sequence, int64_t from_time) +{ + AssertLockHeld(cs_wallet); + m_owed_rescans[sequence] = std::max(from_time, 0); + m_next_owed_rescan = std::max(m_next_owed_rescan, sequence + 1); +} + +bool CWallet::AddOwedRescan(int64_t from_time) +{ + AssertLockHeld(cs_wallet); + from_time = std::max(from_time, 0); + if (!WalletBatch{GetDatabase()}.WriteOwedRescan(m_next_owed_rescan, from_time)) return false; + m_owed_rescans.emplace(m_next_owed_rescan++, from_time); + return true; +} + +std::optional CWallet::GetOwedRescanFrom() const +{ + AssertLockHeld(cs_wallet); + std::optional from; + for (const auto& [sequence, from_time] : m_owed_rescans) { + if (!from || from_time < *from) from = from_time; + } + return from; +} + +void CWallet::SettleOwedRescans(uint64_t sequence, int64_t covered_from) +{ + AssertLockHeld(cs_wallet); + WalletBatch batch{GetDatabase()}; + for (auto it{m_owed_rescans.begin()}; it != m_owed_rescans.end() && it->first <= sequence;) { + if (it->second < covered_from) { + ++it; + continue; + } + if (!batch.EraseOwedRescan(it->first)) { + WalletLogPrintf("%s: could not erase owed rescan %d from the wallet database\n", __func__, it->first); + } + it = m_owed_rescans.erase(it); + } +} + std::vector CWallet::GetGovernanceObjects() { AssertLockHeld(cs_wallet); diff --git a/src/wallet/wallet.h b/src/wallet/wallet.h index b6fb1df9ca35..f2fc1c10abb7 100644 --- a/src/wallet/wallet.h +++ b/src/wallet/wallet.h @@ -534,6 +534,11 @@ class CWallet final : public WalletStorage, public interfaces::Chain::Notificati PlatformKeyStatus DerivePlatformKey(const platformkeys::Path& path, platformkeys::ExtKey256& out) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet); + //! Rescans owed from a birth time, by the sequence number they were + //! recorded under (persisted as DBKeys::OWED_RESCAN). + std::map m_owed_rescans GUARDED_BY(cs_wallet); + uint64_t m_next_owed_rescan GUARDED_BY(cs_wallet){1}; + public: /** * Main wallet lock. @@ -1148,6 +1153,17 @@ class CWallet final : public WalletStorage, public interfaces::Chain::Notificati PlatformKeyResult EnsureFriendshipReceivingKeychain(const FriendshipKeychainRequest& request); PlatformKeyResult> ListAssetLockFundings(const std::vector& keys) const; + /** Load an owed rescan record (wallet load). */ + void LoadOwedRescan(uint64_t sequence, int64_t from_time) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet); + /** Record that the blocks a key born at from_time may be paid in must be rescanned. */ + bool AddOwedRescan(int64_t from_time) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet); + /** The earliest time an owed rescan starts from; nullopt when none is owed. */ + std::optional GetOwedRescanFrom() const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet); + /** The sequence number of the last owed rescan recorded, for SettleOwedRescans(). */ + uint64_t GetOwedRescanSequence() const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet) { return m_next_owed_rescan - 1; } + /** Settle the owed rescans recorded up to sequence that a completed rescan from covered_from read. */ + void SettleOwedRescans(uint64_t sequence, int64_t covered_from) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet); + /* Returns the time of the first created key or, in case of an import, it could be the time of the first received transaction */ int64_t GetBirthTime() const { return m_birth_time; } diff --git a/src/wallet/walletdb.cpp b/src/wallet/walletdb.cpp index 2a762742e4be..9ab43186efb3 100644 --- a/src/wallet/walletdb.cpp +++ b/src/wallet/walletdb.cpp @@ -60,6 +60,7 @@ const std::string MINVERSION{"minversion"}; const std::string NAME{"name"}; const std::string OLD_KEY{"wkey"}; const std::string ORDERPOSNEXT{"orderposnext"}; +const std::string OWED_RESCAN{"owed_rescan"}; const std::string PLATFORM_DATA{"platform_data"}; const std::string POOL{"pool"}; const std::string PURPOSE{"purpose"}; @@ -263,6 +264,16 @@ bool WalletBatch::ErasePlatformData(const std::string& key) return EraseIC(std::make_pair(DBKeys::PLATFORM_DATA, key)); } +bool WalletBatch::WriteOwedRescan(uint64_t sequence, int64_t from_time) +{ + return WriteIC(std::make_pair(DBKeys::OWED_RESCAN, sequence), from_time); +} + +bool WalletBatch::EraseOwedRescan(uint64_t sequence) +{ + return EraseIC(std::make_pair(DBKeys::OWED_RESCAN, sequence)); +} + bool WalletBatch::WriteActiveScriptPubKeyMan(const uint256& id, bool internal) { std::string key = internal ? DBKeys::ACTIVEINTERNALSPK : DBKeys::ACTIVEEXTERNALSPK; @@ -1226,6 +1237,18 @@ static DBErrors LoadDashRecords(CWallet* pwallet, DatabaseBatch& batch) EXCLUSIV }); result = std::max(result, platform_data_res.m_result); + // Load owed rescan records + LoadResult owed_rescan_res = LoadRecords(pwallet, batch, DBKeys::OWED_RESCAN, + [] (CWallet* pwallet, DataStream& key, CDataStream& value, std::string& err) EXCLUSIVE_LOCKS_REQUIRED(pwallet->cs_wallet) { + uint64_t sequence; + key >> sequence; + int64_t from_time; + value >> from_time; + pwallet->LoadOwedRescan(sequence, from_time); + return DBErrors::LOAD_OK; + }); + result = std::max(result, owed_rescan_res.m_result); + return result; } diff --git a/src/wallet/walletdb.h b/src/wallet/walletdb.h index 4494a1c5c173..466a1c7d2200 100644 --- a/src/wallet/walletdb.h +++ b/src/wallet/walletdb.h @@ -90,6 +90,7 @@ extern const std::string MINVERSION; extern const std::string NAME; extern const std::string OLD_KEY; extern const std::string ORDERPOSNEXT; +extern const std::string OWED_RESCAN; extern const std::string PLATFORM_DATA; extern const std::string POOL; extern const std::string PURPOSE; @@ -239,6 +240,10 @@ class WalletBatch bool WritePlatformData(const std::string& key, const std::vector& value); bool ErasePlatformData(const std::string& key); + //! A rescan the wallet owes from from_time, recorded under sequence. + bool WriteOwedRescan(uint64_t sequence, int64_t from_time); + bool EraseOwedRescan(uint64_t sequence); + bool WriteDescriptorKey(const uint256& desc_id, const CPubKey& pubkey, const CPrivKey& privkey, const SecureString& mnemonic, const SecureString& mnemonic_passphrase); bool WriteCryptedDescriptorKey(const uint256& desc_id, const CPubKey& pubkey, const std::vector& secret, const std::vector& crypted_mnemonic, const std::vector& crypted_mnemonic_passphrase); bool WriteDescriptor(const uint256& desc_id, const WalletDescriptor& descriptor);