Persisting the sent-sweep watermark means a relaunch runs a windowed sweep, which can't see a change made outside the GUI (a Console z_importviewingkey, an external same-seed restore) that adds an old send below the window. This adds a periodic full re-sweep so that case is still caught within ~7 days. - sentSweepSafetyNetDue(now, lastFull) = lastFull > 0 && now - lastFull > 7d (a wallet that has never full-swept doesn't trigger one; the seeding sweep records the time). lastFullTime / lastFullHeight ride in the sent_sweep snapshot; any successful full sweep (seed, verify-mismatch or safety-net) resets the clock. On load, a valid watermark without a clock starts it now, so even watermarks seeded before this field eventually re-sweep. - When due, the snapshot forces a full sweep for that cycle. Gated on: a watermark exists, a routine (non-syncing) cycle, not the Mining page, and not in full-sweep backoff -- so a full sweep that can't finish on a slow daemon backs off instead of retrying every cycle. Degrades gracefully: ~0.4 s on a note-cache daemon; callLong(120 s) with backoff on an old one (the deferred banding would keep each cs_main hold short so it fits there too). Helper + persistence covered by tests. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
161 lines
7.7 KiB
C++
161 lines
7.7 KiB
C++
#pragma once
|
|
|
|
#include "wallet_state.h"
|
|
|
|
#include <array>
|
|
#include <cstdint>
|
|
#include <ctime>
|
|
#include <mutex>
|
|
#include <string>
|
|
#include <unordered_map>
|
|
#include <vector>
|
|
|
|
struct sqlite3;
|
|
|
|
namespace dragonx {
|
|
namespace data {
|
|
|
|
// Persisted z_listsentbyaddress watermark, so a relaunch runs a windowed sweep instead of a full one.
|
|
// height (W) = tip of the last successful sweep; anchorHeight (A) = W - margin, the bottom edge the window
|
|
// covered; anchorHash (H_A) = getblockhash(A) at that time, verified on the next load. backlog = txids found
|
|
// but not yet enriched (must persist WITH W, or a pure z->z send whose txid lives only in a sweep result is
|
|
// lost once the window moves past it). The backoff fields keep a relaunch from immediately re-running a full
|
|
// sweep that keeps failing; daemonId resets the backoff on a version change. (Namespace-scope, not nested, so
|
|
// it is a complete type for the default argument of replace().)
|
|
struct SentSweepState {
|
|
int height = -1;
|
|
int anchorHeight = -1;
|
|
std::string anchorHash;
|
|
std::vector<std::string> backlog;
|
|
int fullFailures = 0;
|
|
long fullRetryDelaySec = 0;
|
|
std::string daemonId;
|
|
std::int64_t lastFullTime = 0; // when the last successful FULL sweep finished: drives the weekly
|
|
int lastFullHeight = -1; // safety-net re-sweep that catches changes the per-event guards miss
|
|
bool valid() const { return height >= 0; }
|
|
};
|
|
|
|
class TransactionHistoryCache {
|
|
public:
|
|
struct LoadResult {
|
|
bool loaded = false;
|
|
bool invalidated = false;
|
|
int tipHeight = 0;
|
|
std::string tipHash;
|
|
std::time_t updatedAt = 0;
|
|
std::vector<TransactionInfo> transactions;
|
|
std::unordered_map<std::string, int> shieldedScanHeights;
|
|
SentSweepState sentSweep;
|
|
};
|
|
|
|
// Everything one snapshot write needs, BY VALUE, so it can run on the history writer thread after the UI
|
|
// state it came from has moved on (and after lockKey() zeroed the live key). Move-only; the private key
|
|
// copy is wiped by writeSnapshot() and, for a job that never ran (superseded), by the destructor.
|
|
struct SnapshotJob {
|
|
std::string walletIdentity;
|
|
std::array<unsigned char, 32> key{};
|
|
int tipHeight = 0;
|
|
std::string tipHash;
|
|
std::vector<TransactionInfo> transactions;
|
|
std::time_t updatedAt = 0;
|
|
std::unordered_map<std::string, int> shieldedScanHeights;
|
|
SentSweepState sentSweep;
|
|
|
|
SnapshotJob() = default;
|
|
SnapshotJob(const SnapshotJob&) = delete;
|
|
SnapshotJob& operator=(const SnapshotJob&) = delete;
|
|
SnapshotJob(SnapshotJob&&) = default;
|
|
SnapshotJob& operator=(SnapshotJob&&) = default;
|
|
~SnapshotJob();
|
|
};
|
|
|
|
TransactionHistoryCache();
|
|
explicit TransactionHistoryCache(std::string databasePath);
|
|
~TransactionHistoryCache();
|
|
|
|
TransactionHistoryCache(const TransactionHistoryCache&) = delete;
|
|
TransactionHistoryCache& operator=(const TransactionHistoryCache&) = delete;
|
|
|
|
static std::string defaultDatabasePath();
|
|
static std::string walletIdentityFromAddresses(const std::vector<std::string>& shieldedAddresses,
|
|
const std::vector<std::string>& transparentAddresses);
|
|
static std::string walletIdentityHash(const std::string& walletIdentity);
|
|
|
|
bool ensureOpen();
|
|
bool unlockWithPassphrase(const std::string& walletIdentity, const std::string& passphrase);
|
|
// Unlock an UNENCRYPTED wallet's cache without Argon2id: the "secret" is derivable from the DB, so a
|
|
// cheap domain-separated BLAKE2b KDF is security-equivalent and avoids a ~50-100 ms UI-thread stall.
|
|
bool unlockUnencrypted(const std::string& walletIdentity);
|
|
void lockKey();
|
|
bool hasKey() const;
|
|
bool isUnlockedFor(const std::string& walletIdentity) const;
|
|
// Copy of the live key if it is unlocked for `walletIdentity` (keyOut untouched otherwise). Caller wipes it.
|
|
bool captureKey(const std::string& walletIdentity, std::array<unsigned char, 32>& keyOut) const;
|
|
// Non-reversible id of the key last installed. Survives lockKey(), so re-unlocking with the same
|
|
// passphrase gives the same id and a passphrase change gives another. 0 = never unlocked.
|
|
std::uint64_t keyId() const;
|
|
|
|
LoadResult load(const std::string& walletIdentity,
|
|
int currentTipHeight,
|
|
const std::string& currentTipHash);
|
|
bool replace(const std::string& walletIdentity,
|
|
int tipHeight,
|
|
const std::string& tipHash,
|
|
const std::vector<TransactionInfo>& transactions,
|
|
std::time_t updatedAt,
|
|
const std::unordered_map<std::string, int>& shieldedScanHeights = {},
|
|
const SentSweepState& sentSweep = SentSweepState{});
|
|
// Thread-safe snapshot write (the history writer thread; replace() wraps it): JSON + AEAD with no lock
|
|
// held, then INSERT OR REPLACE + prune under the DB lock. Always wipes job.key. False for a keyless job.
|
|
bool writeSnapshot(SnapshotJob&& job);
|
|
void clearWallet(const std::string& walletIdentity);
|
|
int snapshotCount();
|
|
|
|
private:
|
|
bool exec(const char* sql);
|
|
bool createSchema();
|
|
std::vector<unsigned char> getOrCreateSalt(const std::string& walletHash);
|
|
static bool deriveKeyFast(const std::string& walletHash,
|
|
const std::vector<unsigned char>& salt,
|
|
std::array<unsigned char, 32>& keyOut); // BLAKE2b, label-separated
|
|
static bool deriveKey(const std::string& passphrase,
|
|
const std::vector<unsigned char>& salt,
|
|
std::array<unsigned char, 32>& keyOut);
|
|
static bool encryptPayload(const std::array<unsigned char, 32>& key,
|
|
const std::string& walletHash,
|
|
const std::string& plainText,
|
|
std::vector<unsigned char>& nonce,
|
|
std::vector<unsigned char>& cipherText);
|
|
bool decryptPayload(const std::string& walletHash,
|
|
const std::vector<unsigned char>& nonce,
|
|
const std::vector<unsigned char>& cipherText,
|
|
std::string& plainText) const;
|
|
bool readSnapshot(const std::string& walletHash,
|
|
int& tipHeight,
|
|
std::string& tipHash,
|
|
std::time_t& updatedAt,
|
|
std::vector<unsigned char>& nonce,
|
|
std::vector<unsigned char>& cipherText);
|
|
void clearWalletByHash(const std::string& walletHash);
|
|
// Delete snapshot + salt rows for every wallet hash except the live one, bounding the
|
|
// DB so generating a new address (which changes the hash) doesn't orphan history forever.
|
|
// Also keeps `alsoKeepWalletHash` (the wallet unlocked now, if any): a delayed writer job for the previous
|
|
// wallet must not delete the salt of the wallet just switched to.
|
|
void pruneOtherWallets(const std::string& keepWalletHash, const std::string& alsoKeepWalletHash);
|
|
void close();
|
|
|
|
// Lock order: db_mutex_ then key_mutex_. key_mutex_ is never held across I/O or the KDF. Only the UI
|
|
// thread changes the key fields; the writer thread reads unlocked_wallet_hash_ (prune) under both locks.
|
|
mutable std::recursive_mutex db_mutex_; // db_ and every SQL statement
|
|
mutable std::mutex key_mutex_; // key_, key_ready_, unlocked_wallet_hash_, key_id_
|
|
sqlite3* db_ = nullptr;
|
|
std::string database_path_;
|
|
std::array<unsigned char, 32> key_{};
|
|
bool key_ready_ = false;
|
|
std::string unlocked_wallet_hash_;
|
|
std::uint64_t key_id_ = 0;
|
|
};
|
|
|
|
} // namespace data
|
|
} // namespace dragonx
|