Skip to content

Commit

Permalink
ripple: more logs in eraseDataSince
Browse files Browse the repository at this point in the history
  • Loading branch information
jcoatelen-ledger committed Jul 5, 2023
1 parent 2d063ca commit 7ea2d67
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 9 deletions.
15 changes: 14 additions & 1 deletion core/src/wallet/common/AbstractAccount.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -285,20 +285,33 @@ namespace ledger {

void AbstractAccount::eraseSynchronizerDataSince(soci::session &sql, const std::chrono::system_clock::time_point &date) {
// Update account's internal preferences (for synchronization)
auto savedState = getInternalPreferences()->getSubPreferences("AbstractBlockchainExplorerAccountSynchronizer")->getObject<BlockchainExplorerAccountSynchronizationSavedState>("state");
constexpr auto funcPrefix = "Erasing synchronizer data (abstract account):";
auto savedState = getInternalPreferences()->getSubPreferences("AbstractBlockchainExplorerAccountSynchronizer")->getObject<BlockchainExplorerAccountSynchronizationSavedState>("state");

if (savedState.nonEmpty()) {
// Reset batches to blocks mined before given date
auto previousBlock = BlockDatabaseHelper::getPreviousBlockInDatabase(sql, getWallet()->getCurrency().name, date);
logger()->debug("{} starting with previousBlock={}", funcPrefix, previousBlock.nonEmpty() ? std::to_string(previousBlock.getValue().height) : "NA");

for (auto &batch : savedState.getValue().batches) {
if (previousBlock.nonEmpty() && batch.blockHeight > previousBlock.getValue().height) {
logger()->debug("{} block change {} -> {}", funcPrefix, batch.blockHeight, previousBlock.getValue().height);
batch.blockHeight = (uint32_t)previousBlock.getValue().height;
batch.blockHash = previousBlock.getValue().blockHash;
} else if (!previousBlock.nonEmpty()) { // if no previous block, sync should go back from genesis block
logger()->debug("{} block change {} -> 0", funcPrefix, batch.blockHeight);
batch.blockHeight = 0;
batch.blockHash = "";
} else {
logger()->debug("{} block skipped", funcPrefix);
}
}

getInternalPreferences()->getSubPreferences("AbstractBlockchainExplorerAccountSynchronizer")->editor()->putObject<BlockchainExplorerAccountSynchronizationSavedState>("state", savedState.getValue())->commit();
logger()->debug("{} finished", funcPrefix);

} else {
logger()->debug("{} no prior saved state, doing nothing.", funcPrefix);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,21 +115,24 @@ namespace ledger {
return _notifier;
}

std::shared_ptr<spdlog::logger> getLogger(const std::string &loggerPurpose, const std::string &tracePrefix, const std::shared_ptr<RippleLikeAccount> &account) {
return logger::trace(loggerPurpose, tracePrefix, account->logger());
}

Future<AccountSynchronizationContext>
RippleLikeAccountSynchronizer::performSynchronization(const std::shared_ptr<RippleLikeAccount> &account) {
auto buddy = std::make_shared<SynchronizationBuddy>();
buddy->account = account;
buddy->preferences = std::static_pointer_cast<AbstractAccount>(account)
->getInternalPreferences()
->getSubPreferences(subPreferencesPrefix);
auto loggerPurpose = fmt::format("synchronize_{}", account->getAccountUid());
auto tracePrefix = fmt::format(
"{}/{}/{}",
account->getWallet()->getPool()->getName(),
account->getWallet()->getName(),
account->getIndex());
auto tracePrefix = fmt::format(
"{}/{}/{}",
account->getWallet()->getPool()->getName(),
account->getWallet()->getName(),
account->getIndex());
buddy->synchronizationTag = tracePrefix;
buddy->logger = logger::trace(loggerPurpose, tracePrefix, account->logger());
buddy->logger = getLogger(fmt::format("synchronize_{}", account->getAccountUid()), tracePrefix, account);
buddy->startDate = DateUtils::now();
buddy->wallet = account->getWallet();
buddy->configuration = account->getWallet()->getConfig();
Expand Down Expand Up @@ -564,20 +567,37 @@ namespace ledger {
}

void RippleLikeAccountSynchronizer::eraseDataSince(soci::session &sql, const std::chrono::system_clock::time_point &date, const std::shared_ptr<RippleLikeAccount> &account) {
auto savedState = getSavedState(account);
auto tracePrefix = fmt::format(
"{}/{}/{}",
account->getWallet()->getPool()->getName(),
account->getWallet()->getName(),
account->getIndex());
auto logger = getLogger(fmt::format("eraseDataSince_{}", account->getAccountUid()), tracePrefix, account);

constexpr auto funcPrefix = "Erasing synchronizer data (ripple account):";
auto savedState = getSavedState(account);
if (savedState.nonEmpty()) {
// Reset batches to blocks mined before given date
auto previousBlock = BlockDatabaseHelper::getPreviousBlockInDatabase(sql, account->getWallet()->getCurrency().name, date);
logger->debug("{} starting with previousBlock={}", funcPrefix, previousBlock.nonEmpty() ? std::to_string(previousBlock.getValue().height) : "NA");

for (auto &batch : savedState.getValue().batches) {
if (previousBlock.nonEmpty() && batch.blockHeight > previousBlock.getValue().height) {
logger->debug("{} block change {} -> {}", funcPrefix, batch.blockHeight, previousBlock.getValue().height);
batch.blockHeight = static_cast<uint32_t>(previousBlock.getValue().height);
batch.blockHash = previousBlock.getValue().blockHash;
} else if (!previousBlock.nonEmpty()) { // if no previous block, sync should go back from genesis block
logger->debug("{} block change {} -> 0", funcPrefix, batch.blockHeight);
batch.blockHeight = 0;
batch.blockHash = "";
} else {
logger->debug("{} block skipped", funcPrefix);
}
}
setSavedState(account, savedState.getValue());
logger->debug("{} finished", funcPrefix);
} else {
logger->debug("{} no prior saved state, doing nothing.", funcPrefix);
}
}

Expand Down

0 comments on commit 7ea2d67

Please sign in to comment.