23 #include <validation.h> 32 #include <boost/test/unit_test.hpp> 47 BOOST_FIXTURE_TEST_SUITE(wallet_tests, WalletTestingSetup)
53 mtx.
vin.push_back({
CTxIn{from.GetHash(), index}});
56 std::map<COutPoint, Coin> coins;
57 coins[mtx.
vin[0].prevout].out = from.vout[index];
58 std::map<int, bilingual_str> input_errors;
71 if (!
wallet.AddWalletDescriptor(w_desc, provider,
"",
false))
assert(
false);
114 std::chrono::steady_clock::time_point fake_time;
115 reserver.
setNow([&] { fake_time += 60s;
return fake_time; });
219 wallet->SetupLegacyScriptPubKeyMan();
222 context.
args = &m_args;
229 key.
pushKV(
"timestamp", 0);
246 strprintf(
"[{\"success\":false,\"error\":{\"code\":-1,\"message\":\"Rescan failed for key with creation " 247 "timestamp %d. There was an error reading a block from time %d, which is after or within %d " 248 "seconds of key creation, and could contain transactions pertaining to the key. As a result, " 249 "transactions and coins using this key may not appear in the wallet. This error could be caused " 250 "by pruning or data corruption (see bitcoind log for details) and could be dealt with by " 251 "downloading and rescanning the relevant blocks (see -reindex option and rescanblockchain " 252 "RPC).\"}},{\"success\":true}]",
268 m_coinbase_txns.emplace_back(CreateAndProcessBlock({},
GetScriptForRawPubKey(coinbaseKey.GetPubKey())).vtx[0]);
269 m_coinbase_txns.emplace_back(CreateAndProcessBlock({},
GetScriptForRawPubKey(coinbaseKey.GetPubKey())).vtx[0]);
275 m_coinbase_txns.emplace_back(CreateAndProcessBlock({},
GetScriptForRawPubKey(coinbaseKey.GetPubKey())).vtx[0]);
277 std::string backup_file =
fs::PathToString(m_args.GetDataDirNet() /
"wallet.backup");
282 context.
args = &m_args;
285 auto spk_man =
wallet->GetOrCreateLegacyScriptPubKeyMan();
287 spk_man->mapKeyMetadata[coinbaseKey.GetPubKey().GetID()].nCreateTime = KEY_TIME;
288 spk_man->AddKeyPubKey(coinbaseKey, coinbaseKey.GetPubKey());
308 wallet->SetupLegacyScriptPubKeyMan();
311 context.
args = &m_args;
324 for (
size_t i = 0; i < m_coinbase_txns.size(); ++i) {
325 bool found =
wallet->GetWalletTx(m_coinbase_txns[i]->GetHash());
326 bool expected = i >= 100;
346 wallet.SetupDescriptorScriptPubKeyMans();
370 auto inserted = chainman.
BlockIndex().emplace(std::piecewise_construct, std::make_tuple(
GetRandHash()), std::make_tuple());
372 const uint256& hash = inserted.first->first;
373 block = &inserted.first->second;
374 block->
nTime = blockTime;
420 std::vector<bilingual_str> warnings;
422 auto wallet{std::make_shared<CWallet>(chain.get(),
"", std::move(database))};
432 BOOST_CHECK(!wallet->IsAddressPreviouslySpent(PKHash()));
433 WalletBatch batch{wallet->GetDatabase()};
443 BOOST_CHECK(wallet->IsAddressPreviouslySpent(PKHash()));
444 BOOST_CHECK(wallet->IsAddressPreviouslySpent(ScriptHash()));
445 auto requests = wallet->GetAddressReceiveRequests();
446 auto erequests = {
"val_rr11",
"val_rr20"};
447 BOOST_CHECK_EQUAL_COLLECTIONS(requests.begin(), requests.end(), std::begin(erequests), std::end(erequests));
455 BOOST_CHECK(!wallet->IsAddressPreviouslySpent(PKHash()));
456 BOOST_CHECK(!wallet->IsAddressPreviouslySpent(ScriptHash()));
457 auto requests = wallet->GetAddressReceiveRequests();
458 auto erequests = {
"val_rr11"};
459 BOOST_CHECK_EQUAL_COLLECTIONS(requests.begin(), requests.end(), std::begin(erequests), std::end(erequests));
482 if (is_pubkey_fully_valid) {
493 if (is_pubkey_fully_valid) {
502 std::vector<unsigned char> pubkey_raw(pubkey.
begin(), pubkey.
end());
503 std::fill(pubkey_raw.begin()+1, pubkey_raw.end(), 0);
505 assert(!pubkey.IsFullyValid());
566 wallet->CommitTransaction(tx, {}, {});
577 auto it =
wallet->mapWallet.find(tx->GetHash());
588 std::string coinbaseAddress = coinbaseKey.GetPubKey().GetID().ToString();
592 std::map<CTxDestination, std::vector<COutput>> list;
622 for (
const auto&
group : list) {
623 for (
const auto& coin :
group.second) {
625 wallet->LockCoin(coin.outpoint);
644 std::map<OutputType, size_t>& expected_coins_sizes)
646 LOCK(context.wallet->cs_wallet);
653 for (uint32_t i = 0; i < wtx.tx->vout.size(); i++) context.wallet->LockCoin({wtx.GetHash(), i});
654 for (
const auto& [type, size] : expected_coins_sizes)
BOOST_CHECK_EQUAL(size, available_coins.
coins[type].size());
659 std::map<OutputType, size_t> expected_coins_sizes;
660 for (
const auto& out_type :
OUTPUT_TYPES) { expected_coins_sizes[out_type] = 0U; }
678 expected_coins_sizes[out_type] = 2U;
687 wallet->SetupLegacyScriptPubKeyMan();
719 CScript script_pubkey =
CScript() << OP_HASH160 << std::vector<unsigned char>(script_id.begin(), script_id.end()) <<
OP_EQUAL;
724 keystore.AddKeyPubKey(key, pubkey);
747 std::string s(e.what());
748 return s.find(
"Missing checksum") != std::string::npos;
753 std::vector<unsigned char> malformed_record;
755 vw << std::string(
"notadescriptor");
786 m_args.ForceSetArg(
"-unsafesqlitesync",
"1");
789 context.
args = &m_args;
800 DebugLogHelper addtx_counter(
"[default wallet] AddToWallet", [&](
const std::string* s) {
801 if (s) ++addtx_count;
806 bool rescan_completed =
false;
807 DebugLogHelper rescan_check(
"[default wallet] Rescan completed", [&](
const std::string* s) {
808 if (s) rescan_completed =
true;
816 std::promise<void> promise;
818 promise.get_future().wait();
821 m_coinbase_txns.push_back(CreateAndProcessBlock({},
GetScriptForRawPubKey(coinbaseKey.GetPubKey())).vtx[0]);
823 m_coinbase_txns.push_back(CreateAndProcessBlock({block_tx},
GetScriptForRawPubKey(coinbaseKey.GetPubKey())).vtx[0]);
863 m_coinbase_txns.push_back(CreateAndProcessBlock({},
GetScriptForRawPubKey(coinbaseKey.GetPubKey())).vtx[0]);
865 m_coinbase_txns.push_back(CreateAndProcessBlock({block_tx},
GetScriptForRawPubKey(coinbaseKey.GetPubKey())).vtx[0]);
887 context.
args = &m_args;
895 m_args.ForceSetArg(
"-unsafesqlitesync",
"1");
897 context.
args = &m_args;
904 m_coinbase_txns.push_back(CreateAndProcessBlock({},
GetScriptForRawPubKey(coinbaseKey.GetPubKey())).vtx[0]);
911 auto block_hash = block_tx.GetHash();
912 auto prev_tx = m_coinbase_txns[0];
918 std::vector<uint256> vHashIn{ block_hash };
938 wallet.SetupDescriptorScriptPubKeyMans();
961 mtx.
vin.emplace_back(tx_id_to_spend, 0);
963 const auto good_tx_id{mtx.
GetHash()};
982 mtx.
vin.emplace_back(good_tx_id, 0);
985 HasReason(
"DB error adding transaction to wallet, write failed"));
std::shared_ptr< const CTransaction > CTransactionRef
uint256 last_failed_block
Height of the most recent block that could not be scanned due to read errors or pruning.
RPCHelpMan importwallet()
std::unique_ptr< interfaces::Chain > chain
std::optional< DatabaseFormat > require_format
static UniValue Parse(std::string_view raw)
Parse string to UniValue or throw runtime_error if string contains invalid JSON.
void push_back(UniValue val)
bool RemoveWatchOnly(const CScript &dest)
Remove a watch only script from the keystore.
State of transaction added to mempool.
void SyncWithValidationInterfaceQueue()
This is a synonym for the following, which asserts certain locks are not held: std::promise<void> pro...
void SignTransaction(CMutableTransaction &mtx, const SigningProvider *keystore, const std::map< COutPoint, Coin > &coins, const UniValue &hashType, UniValue &result)
Sign a transaction with the given keystore and previous transactions.
static const CAmount DEFAULT_TRANSACTION_MINFEE
-mintxfee default
static constexpr unsigned int DEFAULT_INCREMENTAL_RELAY_FEE
Default for -incrementalrelayfee, which sets the minimum feerate increase for mempool limiting or rep...
static constexpr size_t DUMMY_NESTED_P2WPKH_INPUT_SIZE
Pre-calculated constants for input size estimation in virtual size
static void AddKey(CWallet &wallet, const CKey &key)
static size_t CalculateNestedKeyhashInputSize(bool use_max_sig)
Describes a place in the block chain to another node such that if the other node doesn't have the sam...
std::optional< int > last_scanned_height
bool LoadWatchOnly(const CScript &dest)
Adds a watch-only address to the store, without saving it to disk (used by LoadWallet) ...
size_t Size() const
The following methods are provided so that CoinsResult can mimic a vector, i.e., methods can work wit...
void TestLoadWallet(const std::string &name, DatabaseFormat format, std::function< void(std::shared_ptr< CWallet >)> f)
Provides an interface for creating and interacting with one or two chainstates: an IBD chainstate gen...
uint256 last_scanned_block
Hash and height of most recent block that was successfully scanned.
RecursiveMutex cs_KeyStore
static const DatabaseFormat DATABASE_FORMATS[]
CPubKey GetPubKey() const
Compute the public key from a private key.
uint256 GetRandHash() noexcept
virtual bool AddCScript(const CScript &redeemScript)
std::shared_ptr< CWallet > m_wallet
CoinsResult AvailableCoinsListUnspent(const CWallet &wallet, const CCoinControl *coinControl, CoinFilterParams params)
Wrapper function for AvailableCoins which skips the feerate and CoinFilterParams::only_spendable para...
const BaseSignatureCreator & DUMMY_SIGNATURE_CREATOR
A signature creator that just produces 71-byte empty signatures.
static constexpr unsigned int DEFAULT_MIN_RELAY_TX_FEE
Default for -minrelaytxfee, minimum relay fee for transactions.
uint256 rand256() noexcept
generate a random uint256.
const BaseSignatureCreator & DUMMY_MAXIMUM_SIGNATURE_CREATOR
A signature creator that just produces 72-byte empty signatures.
State of transaction not confirmed or conflicting with a known block and not in the mempool...
RAII object to check and reserve a wallet rescan.
Balance GetBalance(const CWallet &wallet, const int min_depth, bool avoid_reuse)
bool reserve(bool with_passphrase=false)
Access to the wallet database.
State of transaction confirmed in a block.
bool(* handler)(const std::any &context, HTTPRequest *req, const std::string &strReq)
std::shared_ptr< CWallet > TestLoadWallet(std::unique_ptr< WalletDatabase > database, WalletContext &context, uint64_t create_flags)
static constexpr int64_t TIMESTAMP_WINDOW
Timestamp window used as a grace period by code that compares external timestamps (such as timestamps...
BOOST_AUTO_TEST_CASE(WatchOnlyPubKeys)
CKeyID GetID() const
Get the KeyID of this public key (hash of its serialization)
const unsigned char * begin() const
static constexpr auto OUTPUT_TYPES
std::unique_ptr< CWallet > wallet
static void AvailableCoins(benchmark::Bench &bench, const std::vector< OutputType > &output_type)
constexpr unsigned char * begin()
Minimal stream for reading from an existing byte array by Span.
CKey GenerateRandomKey(bool compressed) noexcept
std::map< OutputType, std::vector< COutput > > coins
int64_t CAmount
Amount in satoshis (Can be negative)
static int64_t AddTx(ChainstateManager &chainman, CWallet &wallet, uint32_t lockTime, int64_t mockTime, int64_t blockTime)
COutputs available for spending, stored by OutputType.
uint256 GetBlockHash() const
void SetMockTime(int64_t nMockTimeIn)
DEPRECATED Use SetMockTime with chrono type.
A transaction with a bunch of additional info that only the owner cares about.
std::string ToString(const T &t)
Locale-independent version of std::to_string.
NodeContext struct containing references to chain state and connection state.
int64_t GetVirtualTransactionInputSize(const CTxIn &txin, int64_t nSigOpCost, unsigned int bytes_per_sigop)
static std::string PathToString(const path &path)
Convert path object to a byte string.
const unsigned char * end() const
bool IsFullyValid() const
fully validate whether this is a valid public key (more expensive than IsValid()) ...
std::string HexStr(const Span< const uint8_t > s)
Convert a span of bytes to a lower-case hexadecimal string.
static const CAmount WALLET_INCREMENTAL_RELAY_FEE
minimum recommended increment for replacement txs
An input of a transaction.
util::Result< CreatedTransactionResult > CreateTransaction(CWallet &wallet, const std::vector< CRecipient > &vecSend, std::optional< unsigned int > change_pos, const CCoinControl &coin_control, bool sign)
Create a new transaction paying the recipients with a set of coins selected by SelectCoins(); Also cr...
static void PollutePubKey(CPubKey &pubkey)
BOOST_AUTO_TEST_SUITE_END()
static void TestWatchOnlyPubKey(LegacyScriptPubKeyMan *spk_man, const CPubKey &add_pubkey)
An encapsulated public key.
Fillable signing provider that keeps keys in an address->secret map.
static CMutableTransaction TestSimpleSpend(const CTransaction &from, uint32_t index, const CKey &key, const CScript &pubkey)
void MakeNewKey(bool fCompressed)
Generate a new private key using a cryptographic PRNG.
static void AddTx(CWallet &wallet)
CAmount CachedTxGetImmatureCredit(const CWallet &wallet, const CWalletTx &wtx, const isminefilter &filter)
int64_t GetBlockTimeMax() const
Indicate that this wallet supports DescriptorScriptPubKeyMan.
BOOST_CHECK_EXCEPTION predicates to check the specific validation error.
std::string write(unsigned int prettyIndent=0, unsigned int indentLevel=0) const
A CWallet maintains a set of transactions and balances, and provides the ability to create new transa...
bool malformed_descriptor(std::ios_base::failure e)
void CallFunctionInValidationInterfaceQueue(std::function< void()> func)
Pushes a function to callback onto the notification queue, guaranteeing any callbacks generated prior...
Txid GetHash() const
Compute the hash of this CMutableTransaction.
Testing fixture that pre-creates a 100-block REGTEST-mode block chain.
MockableDatabase & GetMockableDatabase(CWallet &wallet)
std::vector< CTxOut > vout
constexpr bool IsNull() const
void UnloadWallet(std::shared_ptr< CWallet > &&wallet)
Explicitly unload and delete the wallet.
#define WITH_LOCK(cs, code)
Run code while locking a mutex.
Descriptor with some wallet metadata.
node::BlockMap & BlockIndex() EXCLUSIVE_LOCKS_REQUIRED(
CScript GetScriptForDestination(const CTxDestination &dest)
Generate a Bitcoin scriptPubKey for the given CTxDestination.
static CTransactionRef MakeTransactionRef(Tx &&txIn)
bool RemoveWallet(WalletContext &context, const std::shared_ptr< CWallet > &wallet, std::optional< bool > load_on_start, std::vector< bilingual_str > &warnings)
CWalletTx & AddTx(CRecipient recipient)
uint160 Hash160(const T1 &in1)
Compute the 160-bit hash an object.
CAmount CachedTxGetAvailableCredit(const CWallet &wallet, const CWalletTx &wtx, const isminefilter &filter)
BOOST_FIXTURE_TEST_CASE(wallet_coinsresult_test, BasicTestingSetup)
std::unique_ptr< CWallet > CreateSyncedWallet(interfaces::Chain &chain, CChain &cchain, const CKey &key)
std::unique_ptr< WalletDatabase > CreateMockableWalletDatabase(MockableData records)
#define EXCLUSIVE_LOCKS_REQUIRED(...)
std::unique_ptr< Chain > MakeChain(node::NodeContext &node)
Return implementation of Chain interface.
BOOST_FIXTURE_TEST_CASE(wallet_sync_tx_invalid_state_test, TestingSetup)
Checks a wallet invalid state where the inputs (prev-txs) of a new arriving transaction are not marke...
#define BOOST_CHECK_EQUAL(v1, v2)
bool error(const char *fmt, const Args &... args)
The block chain is a tree shaped structure starting with the genesis block at the root...
void pushKV(std::string key, UniValue val)
Serialized script, used inside transaction inputs and outputs.
static transaction_identifier FromUint256(const uint256 &id)
void TestUnloadWallet(std::shared_ptr< CWallet > &&wallet)
constexpr unsigned char * end()
static const unsigned int MAX_BLOCKFILE_SIZE
The maximum size of a blk?????.dat file (since 0.8)
A reference to a CKey: the Hash160 of its serialized public key.
FastRandomContext g_insecure_rand_ctx
This global and the helpers that use it are not thread-safe.
void UpdateInput(CTxIn &input, const SignatureData &data)
void TestCoinsResult(ListCoinsTest &context, OutputType out_type, CAmount amount, std::map< OutputType, size_t > &expected_coins_sizes)
std::unique_ptr< interfaces::Handler > HandleLoadWallet(WalletContext &context, LoadWalletFn load_wallet)
constexpr CAmount DEFAULT_TRANSACTION_MAXFEE
-maxtxfee default
WalletContext struct containing references to state shared between CWallet instances, like the reference to the chain interface, and the list of opened wallets.
bool GetWatchPubKey(const CKeyID &address, CPubKey &pubkey_out) const
Fetches a pubkey from mapWatchKeys if it exists there.
bool ProduceSignature(const SigningProvider &provider, const BaseSignatureCreator &creator, const CScript &fromPubKey, SignatureData &sigdata)
Produce a script signature using a generic signature creator.
interfaces::Chain * chain
A mutable version of CTransaction.
CScript GetScriptForRawPubKey(const CPubKey &pubKey)
Generate a P2PK script for the given pubkey.
enum wallet::CWallet::ScanResult::@19 status
An encapsulated private key.
The basic transaction that is broadcasted on the network and contained in blocks. ...
int nHeight
height of the entry in the chain. The genesis block has height 0
FlatFilePos GetBlockPos() const EXCLUSIVE_LOCKS_REQUIRED(
std::shared_ptr< CWallet > wallet
std::shared_ptr< CWallet > CreateWallet(WalletContext &context, const std::string &name, std::optional< bool > load_on_start, DatabaseOptions &options, DatabaseStatus &status, bilingual_str &error, std::vector< bilingual_str > &warnings)
BOOST_AUTO_TEST_CASE(bnb_search_test)
UniValue HandleRequest(const JSONRPCRequest &request) const
std::string EncodeSecret(const CKey &key)
static bool RunWithinTxn(WalletBatch &batch, std::string_view process_desc, const std::function< bool(WalletBatch &)> &func)
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate...
bool AddWallet(WalletContext &context, const std::shared_ptr< CWallet > &wallet)
std::variant< TxStateConfirmed, TxStateInMempool, TxStateConflicted, TxStateInactive, TxStateUnrecognized > TxState
All possible CWalletTx states.
Testing setup that configures a complete environment.
std::unique_ptr< ChainstateManager > chainman
virtual bool AddKey(const CKey &key)
std::unique_ptr< WalletDatabase > MakeWalletDatabase(const std::string &name, const DatabaseOptions &options, DatabaseStatus &status, bilingual_str &error_string)
std::map< CTxDestination, std::vector< COutput > > ListCoins(const CWallet &wallet)
Return list of available coins and locked coins grouped by non-change output address.
#define Assert(val)
Identity function.
bool HaveWatchOnly(const CScript &dest) const
Returns whether the watch-only script is in the wallet.
#define BOOST_CHECK(expr)
static constexpr CAmount COIN
The amount of satoshis in one BTC.
bool IsCompressed() const
Check whether this is a compressed public key.
const uint256 * phashBlock
pointer to the hash of the block, if any. Memory is owned by this CBlockIndex