Changeset View
Changeset View
Standalone View
Standalone View
src/wallet/wallet.cpp
// Copyright (c) 2009-2010 Satoshi Nakamoto | // Copyright (c) 2009-2010 Satoshi Nakamoto | ||||
// Copyright (c) 2009-2019 The Bitcoin Core developers | // Copyright (c) 2009-2019 The Bitcoin Core developers | ||||
// Distributed under the MIT software license, see the accompanying | // Distributed under the MIT software license, see the accompanying | ||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | // file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||||
#include <wallet/wallet.h> | #include <wallet/wallet.h> | ||||
#include <chain.h> | #include <chain.h> | ||||
#include <chainparams.h> | #include <chainparams.h> | ||||
#include <config.h> | #include <config.h> | ||||
#include <consensus/consensus.h> | #include <consensus/consensus.h> | ||||
#include <consensus/validation.h> | #include <consensus/validation.h> | ||||
#include <fs.h> | #include <fs.h> | ||||
#include <interfaces/wallet.h> | #include <interfaces/wallet.h> | ||||
#include <key.h> | #include <key.h> | ||||
#include <key_io.h> | #include <key_io.h> | ||||
#include <optional.h> | |||||
#include <policy/mempool.h> | #include <policy/mempool.h> | ||||
#include <policy/policy.h> | #include <policy/policy.h> | ||||
#include <primitives/transaction.h> | #include <primitives/transaction.h> | ||||
#include <random.h> | #include <random.h> | ||||
#include <script/descriptor.h> | #include <script/descriptor.h> | ||||
#include <script/script.h> | #include <script/script.h> | ||||
#include <script/sighashtype.h> | #include <script/sighashtype.h> | ||||
#include <script/sign.h> | #include <script/sign.h> | ||||
▲ Show 20 Lines • Show All 913 Lines • ▼ Show 20 Lines | #endif | ||||
return true; | return true; | ||||
} | } | ||||
void CWallet::LoadToWallet(CWalletTx &wtxIn) { | void CWallet::LoadToWallet(CWalletTx &wtxIn) { | ||||
// If wallet doesn't have a chain (e.g wallet-tool), don't bother to update | // If wallet doesn't have a chain (e.g wallet-tool), don't bother to update | ||||
// txn. | // txn. | ||||
if (HaveChain()) { | if (HaveChain()) { | ||||
Optional<int> block_height = | std::optional<int> block_height = | ||||
chain().getBlockHeight(wtxIn.m_confirm.hashBlock); | chain().getBlockHeight(wtxIn.m_confirm.hashBlock); | ||||
if (block_height) { | if (block_height) { | ||||
// Update cached block height variable since it not stored in the | // Update cached block height variable since it not stored in the | ||||
// serialized transaction. | // serialized transaction. | ||||
wtxIn.m_confirm.block_height = *block_height; | wtxIn.m_confirm.block_height = *block_height; | ||||
} else if (wtxIn.isConflicted() || wtxIn.isConfirmed()) { | } else if (wtxIn.isConflicted() || wtxIn.isConfirmed()) { | ||||
// If tx block (or conflicting block) was reorged out of chain | // If tx block (or conflicting block) was reorged out of chain | ||||
// while the wallet was shutdown, change tx status to UNCONFIRMED | // while the wallet was shutdown, change tx status to UNCONFIRMED | ||||
▲ Show 20 Lines • Show All 811 Lines • ▼ Show 20 Lines | |||||
* pruning or corruption). USER_ABORT if the rescan was aborted before | * pruning or corruption). USER_ABORT if the rescan was aborted before | ||||
* it could complete. | * it could complete. | ||||
* | * | ||||
* @pre Caller needs to make sure start_block (and the optional stop_block) are | * @pre Caller needs to make sure start_block (and the optional stop_block) are | ||||
* on the main chain after to the addition of any new keys you want to detect | * on the main chain after to the addition of any new keys you want to detect | ||||
* transactions for. | * transactions for. | ||||
*/ | */ | ||||
CWallet::ScanResult CWallet::ScanForWalletTransactions( | CWallet::ScanResult CWallet::ScanForWalletTransactions( | ||||
const BlockHash &start_block, int start_height, Optional<int> max_height, | const BlockHash &start_block, int start_height, | ||||
const WalletRescanReserver &reserver, bool fUpdate) { | std::optional<int> max_height, const WalletRescanReserver &reserver, | ||||
bool fUpdate) { | |||||
int64_t nNow = GetTime(); | int64_t nNow = GetTime(); | ||||
int64_t start_time = GetTimeMillis(); | int64_t start_time = GetTimeMillis(); | ||||
assert(reserver.isReserved()); | assert(reserver.isReserved()); | ||||
BlockHash block_hash = start_block; | BlockHash block_hash = start_block; | ||||
ScanResult result; | ScanResult result; | ||||
▲ Show 20 Lines • Show All 2,618 Lines • ▼ Show 20 Lines | std::shared_ptr<CWallet> CWallet::CreateWalletFromFile( | ||||
walletInstance->m_chain_notifications_handler = | walletInstance->m_chain_notifications_handler = | ||||
walletInstance->chain().handleNotifications(walletInstance); | walletInstance->chain().handleNotifications(walletInstance); | ||||
int rescan_height = 0; | int rescan_height = 0; | ||||
if (!gArgs.GetBoolArg("-rescan", false)) { | if (!gArgs.GetBoolArg("-rescan", false)) { | ||||
WalletBatch batch(*walletInstance->database); | WalletBatch batch(*walletInstance->database); | ||||
CBlockLocator locator; | CBlockLocator locator; | ||||
if (batch.ReadBestBlock(locator)) { | if (batch.ReadBestBlock(locator)) { | ||||
if (const Optional<int> fork_height = | if (const std::optional<int> fork_height = | ||||
chain.findLocatorFork(locator)) { | chain.findLocatorFork(locator)) { | ||||
rescan_height = *fork_height; | rescan_height = *fork_height; | ||||
} | } | ||||
} | } | ||||
} | } | ||||
const Optional<int> tip_height = chain.getHeight(); | const std::optional<int> tip_height = chain.getHeight(); | ||||
if (tip_height) { | if (tip_height) { | ||||
walletInstance->m_last_block_processed = | walletInstance->m_last_block_processed = | ||||
chain.getBlockHash(*tip_height); | chain.getBlockHash(*tip_height); | ||||
walletInstance->m_last_block_processed_height = *tip_height; | walletInstance->m_last_block_processed_height = *tip_height; | ||||
} else { | } else { | ||||
walletInstance->m_last_block_processed.SetNull(); | walletInstance->m_last_block_processed.SetNull(); | ||||
walletInstance->m_last_block_processed_height = -1; | walletInstance->m_last_block_processed_height = -1; | ||||
} | } | ||||
Show All 24 Lines | if (tip_height && *tip_height != rescan_height) { | ||||
chain.initMessage(_("Rescanning...").translated); | chain.initMessage(_("Rescanning...").translated); | ||||
walletInstance->WalletLogPrintf( | walletInstance->WalletLogPrintf( | ||||
"Rescanning last %i blocks (from block %i)...\n", | "Rescanning last %i blocks (from block %i)...\n", | ||||
*tip_height - rescan_height, rescan_height); | *tip_height - rescan_height, rescan_height); | ||||
// No need to read and scan block if block was created before our wallet | // No need to read and scan block if block was created before our wallet | ||||
// birthday (as adjusted for block time variability) | // birthday (as adjusted for block time variability) | ||||
// The way the 'time_first_key' is initialized is just a workaround for | std::optional<int64_t> time_first_key; | ||||
// the gcc bug #47679 since version 4.6.0. | |||||
Optional<int64_t> time_first_key = MakeOptional(false, int64_t()); | |||||
for (auto spk_man : walletInstance->GetAllScriptPubKeyMans()) { | for (auto spk_man : walletInstance->GetAllScriptPubKeyMans()) { | ||||
int64_t time = spk_man->GetTimeFirstKey(); | int64_t time = spk_man->GetTimeFirstKey(); | ||||
if (!time_first_key || time < *time_first_key) { | if (!time_first_key || time < *time_first_key) { | ||||
time_first_key = time; | time_first_key = time; | ||||
} | } | ||||
} | } | ||||
if (time_first_key) { | if (time_first_key) { | ||||
if (Optional<int> first_block = | if (std::optional<int> first_block = | ||||
chain.findFirstBlockWithTimeAndHeight( | chain.findFirstBlockWithTimeAndHeight( | ||||
*time_first_key - TIMESTAMP_WINDOW, rescan_height, | *time_first_key - TIMESTAMP_WINDOW, rescan_height, | ||||
nullptr)) { | nullptr)) { | ||||
rescan_height = *first_block; | rescan_height = *first_block; | ||||
} | } | ||||
} | } | ||||
{ | { | ||||
▲ Show 20 Lines • Show All 478 Lines • Show Last 20 Lines |