Skip to content

Commit

Permalink
Don't crop chain if height < offset
Browse files Browse the repository at this point in the history
- Also make sure to re-attach earliest block after offset
if detached height == offset
  • Loading branch information
j-berman committed Oct 26, 2022
1 parent 2f681a4 commit 304e64c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 12 deletions.
37 changes: 25 additions & 12 deletions src/wallet/wallet2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1734,6 +1734,19 @@ void wallet2::process_scan_txs(const tx_entry_data &sorted_txs_to_scan, const tx
<< sorted_txs_to_reprocess.tx_entries.size() << " txs");
}
//----------------------------------------------------------------------------------------------------
void reattach_blockchain(hashchain &blockchain, wallet2::detached_blockchain_data &dbd)
{
if (!dbd.detached_blockchain.empty())
{
LOG_PRINT_L0("Re-attaching " << dbd.detached_blockchain.size() << " blocks");
for (size_t i = 0; i < dbd.detached_blockchain.size(); ++i)
blockchain.push_back(dbd.detached_blockchain[i]);
}

THROW_WALLET_EXCEPTION_IF(blockchain.size() != dbd.original_chain_size,
error::wallet_internal_error, "Unexpected blockchain size after re-attaching");
}
//----------------------------------------------------------------------------------------------------
void wallet2::scan_tx(const std::unordered_set<crypto::hash> &txids)
{
// Get the transactions from daemon in batches sorted lowest height to highest
Expand All @@ -1752,6 +1765,7 @@ void wallet2::scan_tx(const std::unordered_set<crypto::hash> &txids)
// detect tx2. The wallet would need to scan tx1 first in that case.
// TODO: handle this sweep case
detached_blockchain_data dbd;
dbd.original_chain_size = m_blockchain.size();
if (m_blockchain.size() > txs_to_scan.lowest_height)
{
LOG_PRINT_L0("Re-processing wallet's existing txs (if any) starting from height " << txs_to_scan.lowest_height);
Expand All @@ -1767,11 +1781,7 @@ void wallet2::scan_tx(const std::unordered_set<crypto::hash> &txids)
tx_entry_data txs_to_reprocess = get_sorted_tx_entries(tx_hashes_to_reprocess);

process_scan_txs(txs_to_scan, txs_to_reprocess, tx_hashes_to_reprocess, dbd);

if (!dbd.detached_blockchain.empty())
LOG_PRINT_L0("Re-attaching " << dbd.detached_blockchain.size() << " blocks");
for (size_t i = 0; i < dbd.detached_blockchain.size(); ++i)
m_blockchain.push_back(std::move(dbd.detached_blockchain[i]));
reattach_blockchain(m_blockchain, dbd);

// If the highest scan_tx height > the wallet's known scan height, then the
// wallet scanner should continue scanning from that tx's height and "pretend"
Expand Down Expand Up @@ -3910,13 +3920,16 @@ wallet2::detached_blockchain_data wallet2::detach_blockchain(uint64_t height, st
MDEBUG(transfers_detached << " transfers detached / expected " << dbd.detached_tx_hashes.size());
m_transfers.erase(it, m_transfers.end());

size_t blocks_detached = m_blockchain.size() - height;
for (size_t i = height; i < m_blockchain.size(); ++i)
if (i > m_blockchain.offset())
dbd.detached_blockchain.push_back(std::move(m_blockchain[i]));
size_t expected_blocks_detached = dbd.detached_blockchain.size() + (height < m_blockchain.offset() ? m_blockchain.offset() - height : 0);
MDEBUG(blocks_detached << " blocks detached / expected " << expected_blocks_detached);
m_blockchain.crop(height);
size_t blocks_detached = 0;
dbd.original_chain_size = m_blockchain.size();
if (height >= m_blockchain.offset())
{
for (size_t i = height; i < m_blockchain.size(); ++i)
dbd.detached_blockchain.push_back(m_blockchain[i]);
blocks_detached = m_blockchain.size() - height;
m_blockchain.crop(height);
MDEBUG(blocks_detached << " blocks detached / expected " << dbd.detached_blockchain.size());
}

for (auto it = m_payments.begin(); it != m_payments.end(); )
{
Expand Down
1 change: 1 addition & 0 deletions src/wallet/wallet2.h
Original file line number Diff line number Diff line change
Expand Up @@ -819,6 +819,7 @@ namespace tools
struct detached_blockchain_data
{
hashchain detached_blockchain;
size_t original_chain_size;
std::unordered_set<crypto::hash> detached_tx_hashes;
std::unordered_map<crypto::hash, std::vector<cryptonote::tx_destination_entry>> detached_confirmed_txs_dests;
};
Expand Down

0 comments on commit 304e64c

Please sign in to comment.