drk/txs_history: better handling of records

This commit is contained in:
skoupidi
2024-12-13 17:16:18 +02:00
parent 341f38579f
commit 0e600d3e93
3 changed files with 41 additions and 3 deletions

View File

@@ -392,6 +392,9 @@ pub fn generate_completions(shell: &str) -> Result<()> {
.about("Fetch broadcasted transactions history")
.args(&vec![tx_hash, encode]);
let clear_reverted =
SubCommand::with_name("clear-reverted").about("Remove reverted transactions from history");
let height = Arg::with_name("height").help("Fetch specific height record (optional)");
let scanned_blocks = SubCommand::with_name("scanned-blocks")
@@ -400,7 +403,7 @@ pub fn generate_completions(shell: &str) -> Result<()> {
let explorer = SubCommand::with_name("explorer")
.about("Explorer related subcommands")
.subcommands(vec![fetch_tx, simulate_tx, txs_history, scanned_blocks]);
.subcommands(vec![fetch_tx, simulate_tx, txs_history, clear_reverted, scanned_blocks]);
// Alias
let alias = Arg::with_name("alias").help("Token alias");

View File

@@ -411,6 +411,9 @@ enum ExplorerSubcmd {
encode: bool,
},
/// Remove reverted transactions from history
ClearReverted,
/// Fetch scanned blocks records
ScannedBlocks {
/// Fetch specific height record (optional)
@@ -1967,6 +1970,24 @@ async fn realmain(args: Args, ex: Arc<smol::Executor<'static>>) -> Result<()> {
Ok(())
}
ExplorerSubcmd::ClearReverted => {
let drk = new_wallet(
blockchain_config.wallet_path,
blockchain_config.wallet_pass,
None,
ex,
args.fun,
)
.await;
if let Err(e) = drk.remove_reverted_txs() {
eprintln!("Failed to remove reverted transactions: {e:?}");
exit(2);
};
Ok(())
}
ExplorerSubcmd::ScannedBlocks { height } => {
let drk = new_wallet(
blockchain_config.wallet_path,

View File

@@ -53,7 +53,7 @@ impl Drk {
// Create its inverse query
let tx_hash = tx.hash().to_string();
// We only need to reverse the transaction status to "Broadcasted"
// We only need to set the transaction status to "Reverted"
let inverse = self.wallet.create_prepared_statement(
&format!(
"UPDATE {} SET {} = ?1 WHERE {} = ?2;",
@@ -61,7 +61,7 @@ impl Drk {
WALLET_TXS_HISTORY_COL_STATUS,
WALLET_TXS_HISTORY_COL_TX_HASH
),
rusqlite::params!["Broadcasted", tx_hash],
rusqlite::params!["Reverted", tx_hash],
)?;
// Execute the query
@@ -159,4 +159,18 @@ impl Drk {
Ok(())
}
/// Remove the transaction history records in the wallet
/// that have been reverted.
pub fn remove_reverted_txs(&self) -> WalletDbResult<()> {
println!("Removing reverted transactions history records");
let query = format!(
"DELETE FROM {} WHERE {} = 'Reverted';",
WALLET_TXS_HISTORY_TABLE, WALLET_TXS_HISTORY_COL_STATUS
);
self.wallet.exec_sql(&query, &[])?;
println!("Successfully removed reverted transactions history records");
Ok(())
}
}