Skip to content

Commit

Permalink
Add RPC endpoint for marking MUTXOs as abandoned
Browse files Browse the repository at this point in the history
  • Loading branch information
Sword-Smith committed Jul 24, 2023
1 parent 9cc2916 commit c64116b
Showing 1 changed file with 44 additions and 3 deletions.
47 changes: 44 additions & 3 deletions src/rpc_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::str::FromStr;
use std::time::Duration;
use tarpc::context;
use tokio::sync::mpsc::error::SendError;
use tracing::info;
use tracing::{error, info};
use twenty_first::shared_math::digest::Digest;
use twenty_first::util_types::algebraic_hasher::AlgebraicHasher;

Expand Down Expand Up @@ -42,7 +42,7 @@ pub struct DashBoardOverviewDataFromClient {

#[tarpc::service]
pub trait RPC {
/* READ DATA */
/******** READ DATA ********/
// Place all methods that only read here
// Return which network the client is running
async fn get_network() -> Network;
Expand Down Expand Up @@ -93,7 +93,7 @@ pub trait RPC {
/// Determine whether the given amount is less than (or equal to) the balance
async fn amount_leq_balance(amount: Amount) -> bool;

/* CHANGE THINGS */
/******** CHANGE THINGS ********/
// Place all things that change state here
// Gracious shutdown.
async fn shutdown() -> bool;
Expand All @@ -116,6 +116,9 @@ pub trait RPC {

// Start miner if not running
async fn restart_miner();

// mark MUTXOs as abandoned
async fn prune_abandoned_monitored_utxos();
}

#[derive(Clone)]
Expand Down Expand Up @@ -150,6 +153,7 @@ impl RPC for NeptuneRPCServer {
type GetDashboardOverviewDataFut = Ready<DashBoardOverviewDataFromClient>;
type PauseMinerFut = Ready<()>;
type RestartMinerFut = Ready<()>;
type PruneAbandonedMonitoredUtxosFut = Ready<()>;

fn get_network(self, _: context::Context) -> Self::GetNetworkFut {
let network = self.state.cli.network;
Expand Down Expand Up @@ -506,6 +510,43 @@ impl RPC for NeptuneRPCServer {

future::ready(())
}

fn prune_abandoned_monitored_utxos(
self,
_context: tarpc::context::Context,
) -> Self::PruneAbandonedMonitoredUtxosFut {
let prune_count_res = {
// Hold lock on wallet_db
let mut wallet_db_lock = executor::block_on(self.state.wallet_state.wallet_db.lock());
let tip_block_header =
executor::block_on(self.state.chain.light_state.get_latest_block_header());
const DEFAULT_MUTXO_PRUNE_DEPTH: usize = 200;

let prune_count_res = executor::block_on(
self.state
.wallet_state
.prune_abandoned_monitored_utxos_with_lock(
DEFAULT_MUTXO_PRUNE_DEPTH,
&mut wallet_db_lock,
&tip_block_header,
&self.state.chain.archival_state.unwrap(),
),
);

prune_count_res
};

match prune_count_res {
Ok(prune_count) => {
info!("Marked {prune_count} monitored UTXOs as abandoned");
}
Err(err) => {
error!("Pruning monitored UTXOs failed with error: {err}");
}
};

future::ready(())
}
}

#[cfg(test)]
Expand Down

0 comments on commit c64116b

Please sign in to comment.