Skip to content

Commit

Permalink
feat: improve the TMS validation process (#4694)
Browse files Browse the repository at this point in the history
Description
---
Adds a mutex on the TMS validation process to stop multiple validations running per time.

Motivation and Context
---
Validation on the transactions is run when the Base_node state changes, which is an automated event triggered when a base_node lags, a new block is received, or the wallet connects to a new base node. The other path for this is user triggered where the user manually triggers a validation. 

Triggering more than one validation at a time is a waste of resources and network traffic as the wallet now asks the same basenode for the same data more than once.
  • Loading branch information
SWvheerden authored Sep 20, 2022
1 parent 08773f4 commit 030bece
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
2 changes: 2 additions & 0 deletions base_layer/wallet/src/transaction_service/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,8 @@ pub enum TransactionServiceError {
ServiceError(String),
#[error("Wallet Recovery in progress so Transaction Service Messaging Requests ignored")]
WalletRecoveryInProgress,
#[error("Wallet Transaction Validation already in progress, request ignored")]
TransactionValidationInProgress,
#[error("Connectivity error: {source}")]
ConnectivityError {
#[from]
Expand Down
13 changes: 11 additions & 2 deletions base_layer/wallet/src/transaction_service/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ use tari_script::{inputs, script, TariScript};
use tari_service_framework::{reply_channel, reply_channel::Receiver};
use tari_shutdown::ShutdownSignal;
use tokio::{
sync::{mpsc, mpsc::Sender, oneshot},
sync::{mpsc, mpsc::Sender, oneshot, Mutex},
task::JoinHandle,
};

Expand Down Expand Up @@ -171,6 +171,7 @@ pub struct TransactionService<
wallet_db: WalletDatabase<TWalletBackend>,
base_node_service: BaseNodeServiceHandle,
last_seen_tip_height: Option<u64>,
validation_in_progress: Arc<Mutex<()>>,
}

impl<
Expand Down Expand Up @@ -269,6 +270,7 @@ where
base_node_service,
wallet_db,
last_seen_tip_height: None,
validation_in_progress: Arc::new(Mutex::new(())),
}
}

Expand Down Expand Up @@ -2199,8 +2201,15 @@ where
);

let mut base_node_watch = self.connectivity().get_current_base_node_watcher();

let validation_in_progress = self.validation_in_progress.clone();
let join_handle = tokio::spawn(async move {
let mut _lock = validation_in_progress.try_lock().map_err(|_| {
debug!(
target: LOG_TARGET,
"Transaction Validation Protocol (Id: {}) spawned while a previous protocol was busy, ignored", id
);
TransactionServiceProtocolError::new(id, TransactionServiceError::TransactionValidationInProgress)
})?;
let exec_fut = protocol.execute();
tokio::pin!(exec_fut);
loop {
Expand Down

0 comments on commit 030bece

Please sign in to comment.