Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions src/interfaces/wallet.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,26 @@ 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;

//! The height startRescan(false) starts at, when this node has pruned
//! blocks it would read; nullopt when it has them all.
virtual std::optional<int> 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<int64_t> owedRescanFrom() = 0;

//! Lock masternode collaterals
virtual void autoLockMasternodeCollaterals() = 0;

Expand Down Expand Up @@ -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<wallet::FriendshipXpub> ensureFriendshipReceivingKeychain(
const wallet::FriendshipKeychainRequest& request) = 0;

Expand Down
37 changes: 37 additions & 0 deletions src/wallet/interfaces.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<int64_t>(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<int> rescanPrunedFrom() override
{
Expand All @@ -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<int64_t> 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<bilingual_str>& warnings) override
Expand Down
4 changes: 3 additions & 1 deletion src/wallet/platformtypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
212 changes: 195 additions & 17 deletions src/wallet/test/platformkeys_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <chain.h>
#include <chainparams.h>
#include <interfaces/handler.h>
#include <interfaces/wallet.h>
#include <key.h>
#include <key_io.h>
Expand All @@ -12,6 +14,7 @@
#include <test/util/random.h>
#include <test/util/setup_common.h>
#include <util/strencodings.h>
#include <util/time.h>
#include <wallet/bip39.h>
#include <wallet/coincontrol.h>
#include <wallet/context.h>
Expand All @@ -28,6 +31,8 @@
#include <iterator>
#include <optional>
#include <set>
#include <string>
#include <utility>
#include <vector>

using namespace wallet;
Expand Down Expand Up @@ -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<int64_t>{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<int64_t>{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<CWallet>(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<int64_t>{first_request});

const auto owed{m_iface_b->owedRescanFrom()};
BOOST_REQUIRE(owed == std::optional<int64_t>{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<CWallet> Restart(CWallet& wallet)
{
auto restarted{std::make_shared<CWallet>(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<int64_t> 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<int64_t>{m_birth});
BOOST_CHECK(OwedAfterRestart(*m_wallet) == std::optional<int64_t>{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<int64_t>{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<int64_t>{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<int64_t>{m_birth - 10});
BOOST_CHECK(OwedAfterRestart(*m_wallet) == std::optional<int64_t>{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<DescriptorScriptPubKeyMan&>(
**m_wallet->GetScriptPubKeyMans(GetScriptForDestination(destination)).begin())};
LOCK(chain.cs_desc_man);
return chain.GetWalletDescriptor();
}()};
without_key.cache = DescriptorCache{};
const auto wallet{std::make_shared<CWallet>(m_node.chain.get(), "", std::make_unique<MockableDatabase>(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<int64_t>{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<int64_t>{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<int64_t>{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<int64_t>{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
Expand Down
17 changes: 17 additions & 0 deletions src/wallet/test/wallet_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<int>{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<CWallet>(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<int64_t>{0});
}

// Verify ScanForWalletTransactions only picks transactions in the new block
// file.
{
Expand Down
Loading
Loading