Skip to content

Commit

Permalink
Merge pull request #22 from Neptune-Crypto/mark-mutxos-as-abandoned
Browse files Browse the repository at this point in the history
Mark mutxos as abandoned
  • Loading branch information
Sword-Smith authored Jul 26, 2023
2 parents 689db5c + fb6c283 commit 58f0c69
Show file tree
Hide file tree
Showing 7 changed files with 497 additions and 22 deletions.
8 changes: 8 additions & 0 deletions src/bin/neptune-cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ enum Command {
GetReceivingAddress,
MempoolTxCount,
MempoolSize,
PruneAbandonedMonitoredUtxos,
}

#[derive(Debug, Parser)]
Expand Down Expand Up @@ -146,6 +147,13 @@ async fn main() -> Result<()> {
let size_in_bytes: usize = client.get_mempool_size(context::current()).await?;
println!("{} bytes", size_in_bytes);
}

Command::PruneAbandonedMonitoredUtxos => {
let prunt_res_count = client
.prune_abandoned_monitored_utxos(context::current())
.await?;
println!("{prunt_res_count} monitored UTXOs marked as abandoned");
}
}

Ok(())
Expand Down
2 changes: 1 addition & 1 deletion src/main_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1060,7 +1060,7 @@ impl MainLoopHandler {

// Handle messages from peer threads
Some(msg) = peer_thread_to_main_rx.recv() => {
info!("Received message sent to main thread.");
debug!("Received message sent to main thread.");
self.handle_peer_thread_message(
msg,
&mut main_loop_state,
Expand Down
5 changes: 5 additions & 0 deletions src/models/state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,11 @@ impl GlobalState {
'outer: for i in 0..num_monitored_utxos {
let mut monitored_utxo = monitored_utxos.get(i).clone();

// Ignore those MUTXOs that were marked as abandoned
if monitored_utxo.abandoned_at.is_some() {
continue;
}

// ignore synced ones
if monitored_utxo.is_synced_to(&tip_hash) {
continue;
Expand Down
30 changes: 15 additions & 15 deletions src/models/state/wallet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -385,13 +385,13 @@ mod wallet_tests {
#[tokio::test]
async fn wallet_state_registration_of_monitored_utxos_test() -> Result<()> {
let own_wallet_secret = WalletSecret::new(generate_secret_key());
let wallet_state = get_mock_wallet_state(Some(own_wallet_secret.clone())).await;
let own_wallet_state = get_mock_wallet_state(Some(own_wallet_secret.clone())).await;
let other_wallet_secret = WalletSecret::new(generate_secret_key());
let other_recipient_address = other_wallet_secret
.nth_generation_spending_key(0)
.to_address();

let mut monitored_utxos = get_monitored_utxos(&wallet_state).await;
let mut monitored_utxos = get_monitored_utxos(&own_wallet_state).await;
assert!(
monitored_utxos.is_empty(),
"Monitored UTXO list must be empty at init"
Expand All @@ -403,7 +403,7 @@ mod wallet_tests {
let (block_1, block_1_coinbase_utxo, block_1_coinbase_sender_randomness) =
make_mock_block(&genesis_block, None, own_recipient_address);

wallet_state
own_wallet_state
.expected_utxos
.write()
.unwrap()
Expand All @@ -416,18 +416,18 @@ mod wallet_tests {
.unwrap();
assert_eq!(
1,
wallet_state.expected_utxos.read().unwrap().len(),
own_wallet_state.expected_utxos.read().unwrap().len(),
"Expected UTXO list must have length 1 before block registration"
);
wallet_state.update_wallet_state_with_new_block(
own_wallet_state.update_wallet_state_with_new_block(
&block_1,
&mut wallet_state.wallet_db.lock().await,
&mut own_wallet_state.wallet_db.lock().await,
)?;
assert_eq!(
1,
wallet_state.expected_utxos.read().unwrap().len(),
own_wallet_state.expected_utxos.read().unwrap().len(),
"A: Expected UTXO list must have length 1 after block registration, due to potential reorganizations");
let expected_utxos = wallet_state
let expected_utxos = own_wallet_state
.expected_utxos
.read()
.unwrap()
Expand All @@ -438,7 +438,7 @@ mod wallet_tests {
expected_utxos[0].mined_in_block.unwrap().0,
"Expected UTXO must be registered as being mined"
);
monitored_utxos = get_monitored_utxos(&wallet_state).await;
monitored_utxos = get_monitored_utxos(&own_wallet_state).await;
assert_eq!(
1,
monitored_utxos.len(),
Expand All @@ -462,7 +462,7 @@ mod wallet_tests {
// under this block as tip
let (block_2, _, _) = make_mock_block(&block_1, None, other_recipient_address);
let (block_3, _, _) = make_mock_block(&block_2, None, other_recipient_address);
monitored_utxos = get_monitored_utxos(&wallet_state).await;
monitored_utxos = get_monitored_utxos(&own_wallet_state).await;
{
let block_1_tx_output_digest = Hash::hash(&block_1_coinbase_utxo);
let ms_membership_proof = monitored_utxos[0]
Expand All @@ -478,15 +478,15 @@ mod wallet_tests {
);
}
// Verify that the membership proof is valid *after* running the updater
wallet_state.update_wallet_state_with_new_block(
own_wallet_state.update_wallet_state_with_new_block(
&block_2,
&mut wallet_state.wallet_db.lock().await,
&mut own_wallet_state.wallet_db.lock().await,
)?;
wallet_state.update_wallet_state_with_new_block(
own_wallet_state.update_wallet_state_with_new_block(
&block_3,
&mut wallet_state.wallet_db.lock().await,
&mut own_wallet_state.wallet_db.lock().await,
)?;
monitored_utxos = get_monitored_utxos(&wallet_state).await;
monitored_utxos = get_monitored_utxos(&own_wallet_state).await;

{
let block_1_tx_output_digest = Hash::hash(&block_1_coinbase_utxo);
Expand Down
5 changes: 5 additions & 0 deletions src/models/state/wallet/monitored_utxo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ pub struct MonitoredUtxo {

// hash of the block, if any, in which this UTXO was confirmed
pub confirmed_in_block: Option<(Digest, Duration, BlockHeight)>,

/// Indicator used to mark the UTXO as belonging to an abandoned fork
/// Indicates what was the block tip when UTXO was marked as abandoned
pub abandoned_at: Option<(Digest, Duration, BlockHeight)>,
}

impl MonitoredUtxo {
Expand All @@ -34,6 +38,7 @@ impl MonitoredUtxo {
number_of_mps_per_utxo: max_number_of_mps_stored,
spent_in_block: None,
confirmed_in_block: None,
abandoned_at: None,
}
}

Expand Down
Loading

0 comments on commit 58f0c69

Please sign in to comment.