mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-02-08 10:31:50 -05:00
Add m_last_block_processed_height field in CWallet
At BlockConnected/BlockDisconnected, we rely on height of block itself to know current height of wallet
This commit is contained in:
parent
10b4729e33
commit
5aacc3eff1
4 changed files with 54 additions and 5 deletions
|
@ -139,10 +139,12 @@ void TestGUI(interfaces::Node& node)
|
|||
wallet->LoadWallet(firstRun);
|
||||
{
|
||||
auto spk_man = wallet->GetLegacyScriptPubKeyMan();
|
||||
auto locked_chain = wallet->chain().lock();
|
||||
LOCK(wallet->cs_wallet);
|
||||
AssertLockHeld(spk_man->cs_wallet);
|
||||
wallet->SetAddressBook(GetDestinationForKey(test.coinbaseKey.GetPubKey(), wallet->m_default_address_type), "", "receive");
|
||||
spk_man->AddKeyPubKey(test.coinbaseKey, test.coinbaseKey.GetPubKey());
|
||||
wallet->SetLastBlockProcessed(105, ::ChainActive().Tip()->GetBlockHash());
|
||||
}
|
||||
{
|
||||
auto locked_chain = wallet->chain().lock();
|
||||
|
|
|
@ -50,6 +50,10 @@ BOOST_FIXTURE_TEST_CASE(scan_for_wallet_transactions, TestChain100Setup)
|
|||
// Verify ScanForWalletTransactions accommodates a null start block.
|
||||
{
|
||||
CWallet wallet(chain.get(), WalletLocation(), WalletDatabase::CreateDummy());
|
||||
{
|
||||
LOCK(wallet.cs_wallet);
|
||||
wallet.SetLastBlockProcessed(::ChainActive().Height(), ::ChainActive().Tip()->GetBlockHash());
|
||||
}
|
||||
AddKey(wallet, coinbaseKey);
|
||||
WalletRescanReserver reserver(&wallet);
|
||||
reserver.reserve();
|
||||
|
@ -65,6 +69,10 @@ BOOST_FIXTURE_TEST_CASE(scan_for_wallet_transactions, TestChain100Setup)
|
|||
// and new block files.
|
||||
{
|
||||
CWallet wallet(chain.get(), WalletLocation(), WalletDatabase::CreateDummy());
|
||||
{
|
||||
LOCK(wallet.cs_wallet);
|
||||
wallet.SetLastBlockProcessed(::ChainActive().Height(), ::ChainActive().Tip()->GetBlockHash());
|
||||
}
|
||||
AddKey(wallet, coinbaseKey);
|
||||
WalletRescanReserver reserver(&wallet);
|
||||
reserver.reserve();
|
||||
|
@ -84,6 +92,10 @@ BOOST_FIXTURE_TEST_CASE(scan_for_wallet_transactions, TestChain100Setup)
|
|||
// file.
|
||||
{
|
||||
CWallet wallet(chain.get(), WalletLocation(), WalletDatabase::CreateDummy());
|
||||
{
|
||||
LOCK(wallet.cs_wallet);
|
||||
wallet.SetLastBlockProcessed(::ChainActive().Height(), ::ChainActive().Tip()->GetBlockHash());
|
||||
}
|
||||
AddKey(wallet, coinbaseKey);
|
||||
WalletRescanReserver reserver(&wallet);
|
||||
reserver.reserve();
|
||||
|
@ -102,6 +114,10 @@ BOOST_FIXTURE_TEST_CASE(scan_for_wallet_transactions, TestChain100Setup)
|
|||
// Verify ScanForWalletTransactions scans no blocks.
|
||||
{
|
||||
CWallet wallet(chain.get(), WalletLocation(), WalletDatabase::CreateDummy());
|
||||
{
|
||||
LOCK(wallet.cs_wallet);
|
||||
wallet.SetLastBlockProcessed(::ChainActive().Height(), ::ChainActive().Tip()->GetBlockHash());
|
||||
}
|
||||
AddKey(wallet, coinbaseKey);
|
||||
WalletRescanReserver reserver(&wallet);
|
||||
reserver.reserve();
|
||||
|
@ -258,6 +274,7 @@ BOOST_FIXTURE_TEST_CASE(coin_mark_dirty_immature_credit, TestChain100Setup)
|
|||
LockAssertion lock(::cs_main);
|
||||
LOCK(wallet.cs_wallet);
|
||||
AssertLockHeld(spk_man->cs_wallet);
|
||||
wallet.SetLastBlockProcessed(::ChainActive().Height(), ::ChainActive().Tip()->GetBlockHash());
|
||||
|
||||
wtx.SetConf(CWalletTx::Status::CONFIRMED, ::ChainActive().Tip()->GetBlockHash(), 0);
|
||||
|
||||
|
@ -435,6 +452,10 @@ public:
|
|||
{
|
||||
CreateAndProcessBlock({}, GetScriptForRawPubKey(coinbaseKey.GetPubKey()));
|
||||
wallet = MakeUnique<CWallet>(m_chain.get(), WalletLocation(), WalletDatabase::CreateMock());
|
||||
{
|
||||
LOCK(wallet->cs_wallet);
|
||||
wallet->SetLastBlockProcessed(::ChainActive().Height(), ::ChainActive().Tip()->GetBlockHash());
|
||||
}
|
||||
bool firstRun;
|
||||
wallet->LoadWallet(firstRun);
|
||||
AddKey(*wallet, coinbaseKey);
|
||||
|
@ -473,6 +494,7 @@ public:
|
|||
|
||||
LOCK(cs_main);
|
||||
LOCK(wallet->cs_wallet);
|
||||
wallet->SetLastBlockProcessed(wallet->GetLastBlockHeight() + 1, ::ChainActive().Tip()->GetBlockHash());
|
||||
auto it = wallet->mapWallet.find(tx->GetHash());
|
||||
BOOST_CHECK(it != wallet->mapWallet.end());
|
||||
it->second.SetConf(CWalletTx::Status::CONFIRMED, ::ChainActive().Tip()->GetBlockHash(), 1);
|
||||
|
|
|
@ -1058,6 +1058,8 @@ void CWallet::BlockConnected(const CBlock& block, const std::vector<CTransaction
|
|||
auto locked_chain = chain().lock();
|
||||
LOCK(cs_wallet);
|
||||
|
||||
m_last_block_processed_height = height;
|
||||
m_last_block_processed = block_hash;
|
||||
for (size_t i = 0; i < block.vtx.size(); i++) {
|
||||
SyncTransaction(block.vtx[i], CWalletTx::Status::CONFIRMED, block_hash, i);
|
||||
TransactionRemovedFromMempool(block.vtx[i]);
|
||||
|
@ -1065,8 +1067,6 @@ void CWallet::BlockConnected(const CBlock& block, const std::vector<CTransaction
|
|||
for (const CTransactionRef& ptx : vtxConflicted) {
|
||||
TransactionRemovedFromMempool(ptx);
|
||||
}
|
||||
|
||||
m_last_block_processed = block_hash;
|
||||
}
|
||||
|
||||
void CWallet::BlockDisconnected(const CBlock& block, int height)
|
||||
|
@ -1078,6 +1078,8 @@ void CWallet::BlockDisconnected(const CBlock& block, int height)
|
|||
// be unconfirmed, whether or not the transaction is added back to the mempool.
|
||||
// User may have to call abandontransaction again. It may be addressed in the
|
||||
// future with a stickier abandoned state or even removing abandontransaction call.
|
||||
m_last_block_processed_height = height - 1;
|
||||
m_last_block_processed = block.hashPrevBlock;
|
||||
for (const CTransactionRef& ptx : block.vtx) {
|
||||
SyncTransaction(ptx, CWalletTx::Status::UNCONFIRMED, {} /* block hash */, 0 /* position in block */);
|
||||
}
|
||||
|
@ -3785,8 +3787,10 @@ std::shared_ptr<CWallet> CWallet::CreateWalletFromFile(interfaces::Chain& chain,
|
|||
const Optional<int> tip_height = locked_chain->getHeight();
|
||||
if (tip_height) {
|
||||
walletInstance->m_last_block_processed = locked_chain->getBlockHash(*tip_height);
|
||||
walletInstance->m_last_block_processed_height = *tip_height;
|
||||
} else {
|
||||
walletInstance->m_last_block_processed.SetNull();
|
||||
walletInstance->m_last_block_processed_height = -1;
|
||||
}
|
||||
|
||||
if (tip_height && *tip_height != rescan_height)
|
||||
|
|
|
@ -679,12 +679,18 @@ private:
|
|||
* The following is used to keep track of how far behind the wallet is
|
||||
* from the chain sync, and to allow clients to block on us being caught up.
|
||||
*
|
||||
* Note that this is *not* how far we've processed, we may need some rescan
|
||||
* to have seen all transactions in the chain, but is only used to track
|
||||
* live BlockConnected callbacks.
|
||||
* Processed hash is a pointer on node's tip and doesn't imply that the wallet
|
||||
* has scanned sequentially all blocks up to this one.
|
||||
*/
|
||||
uint256 m_last_block_processed GUARDED_BY(cs_wallet);
|
||||
|
||||
/* Height of last block processed is used by wallet to know depth of transactions
|
||||
* without relying on Chain interface beyond asynchronous updates. For safety, we
|
||||
* initialize it to -1. Height is a pointer on node's tip and doesn't imply
|
||||
* that the wallet has scanned sequentially all blocks up to this one.
|
||||
*/
|
||||
int m_last_block_processed_height GUARDED_BY(cs_wallet) = -1;
|
||||
|
||||
public:
|
||||
/*
|
||||
* Main wallet lock.
|
||||
|
@ -1129,6 +1135,21 @@ public:
|
|||
LegacyScriptPubKeyMan::WatchKeyMap& mapWatchKeys GUARDED_BY(cs_KeyStore) = m_spk_man->mapWatchKeys;
|
||||
WalletBatch*& encrypted_batch GUARDED_BY(cs_wallet) = m_spk_man->encrypted_batch;
|
||||
using CryptedKeyMap = LegacyScriptPubKeyMan::CryptedKeyMap;
|
||||
|
||||
/** Get last block processed height */
|
||||
int GetLastBlockHeight() const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet)
|
||||
{
|
||||
AssertLockHeld(cs_wallet);
|
||||
assert(m_last_block_processed_height >= 0);
|
||||
return m_last_block_processed_height;
|
||||
};
|
||||
/** Set last block processed height, currently only use in unit test */
|
||||
void SetLastBlockProcessed(int block_height, uint256 block_hash) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet)
|
||||
{
|
||||
AssertLockHeld(cs_wallet);
|
||||
m_last_block_processed_height = block_height;
|
||||
m_last_block_processed = block_hash;
|
||||
};
|
||||
};
|
||||
|
||||
/**
|
||||
|
|
Loading…
Add table
Reference in a new issue