1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
use std::sync::Arc;
use async_trait::async_trait;
use multiversx_sc::codec::TopEncodeMulti;
use multiversx_sc_scenario::scenario_model::TypedScDeploy;
use tokio::sync::Mutex;
use crate::error::executor::ExecutorError;

/// A trait defining the contract for executing smart contract deployment operations asynchronously.
#[async_trait]
pub trait DeployExecutor: Send + Sync {
    /// Executes a smart contract deployment step asynchronously.
    ///
    /// # Type Parameters
    ///
    /// * `OriginalResult` - The result type expected from the smart contract deployment.
    ///   Must implement `TopEncodeMulti`, `Send`, and `Sync`.
    /// * `S` - The type encapsulating the smart contract deployment step.
    ///   Must implement `AsMut<TypedScDeploy<OriginalResult>>` and `Send`.
    ///
    /// # Parameters
    ///
    /// * `sc_deploy_step` - The smart contract deployment step to be executed.
    ///
    /// # Returns
    ///
    /// A `Result` with an empty `Ok(())` value for success, or `Err(ExecutorError)` for failure.
    async fn sc_deploy<OriginalResult>(&mut self, sc_deploy_step: &mut TypedScDeploy<OriginalResult>) -> Result<(), ExecutorError>
        where
            OriginalResult: TopEncodeMulti + Send + Sync;

    /// Indicates whether to skip deserialization during the deployment execution.
    ///
    /// This could be useful in cases where deserialization is either unnecessary or could cause errors,
    /// for example, with the `DummyExecutor`.
    ///
    /// # Returns
    ///
    /// A `bool` indicating whether deserialization should be skipped.
    async fn should_skip_deserialization(&self) -> bool;
}

/// An implementation of `DeployExecutor` for `Arc<Mutex<T>>` where `T: DeployExecutor`.
/// This wrapper allows for thread-safe, shared ownership of a deploy executor.
#[async_trait]
impl<T: DeployExecutor> DeployExecutor for Arc<Mutex<T>> {
    /// Executes a smart contract deployment step asynchronously, delegating to the inner `DeployExecutor`.
    async fn sc_deploy<OriginalResult>(&mut self, sc_deploy_step: &mut TypedScDeploy<OriginalResult>) -> Result<(), ExecutorError>
        where
            OriginalResult: TopEncodeMulti + Send + Sync,
    {
        {
            let mut locked = self.lock().await;
            locked.sc_deploy(sc_deploy_step).await
        }
    }

    /// Indicates whether to skip deserialization during the deployment execution, delegating to the inner `DeployExecutor`.
    async fn should_skip_deserialization(&self) -> bool {
        {
            // Locking here can lead to some performance penalty. A potential solution may be to use
            // another type other than Mutex, like RwLock.
            let locked = self.lock().await;
            locked.should_skip_deserialization().await
        }
    }
}