blueprint_test_utils/sync/
substrate_test_channel.rs

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
67
68
69
70
use crate::sync::tracked_callback_channel::TrackedCallbackChannel;
use sp_io::TestExternalities;
use std::any::Any;

pub struct MultiThreadedTestExternalities {
    pub tx: TrackedCallbackChannel<InputFunction, Box<dyn Any + Send>>,
}

impl Clone for MultiThreadedTestExternalities {
    fn clone(&self) -> Self {
        Self {
            tx: self.tx.clone(),
        }
    }
}

pub type InputFunction = Box<dyn FnOnce() -> Box<dyn Any + Send> + 'static + Send>;

impl MultiThreadedTestExternalities {
    pub fn new(mut test_externalities: TestExternalities) -> Self {
        let (tx, mut rx) = TrackedCallbackChannel::new(1024);
        let tx_clone = tx.clone();

        std::thread::spawn(move || {
            while let Some(mut resp) = rx.blocking_recv() {
                let payload: InputFunction = resp.payload();
                let res = test_externalities.execute_with(payload);
                let response = resp.new(res);
                if let Err(err) = tx_clone.try_reply(response) {
                    log::warn!(target: "gadget", "Failed to reply to callback: {err:?}");
                }
            }
        });

        Self { tx }
    }

    pub fn execute_with<T: FnOnce() -> R + Send + 'static, R: Send + 'static>(
        &self,
        function: T,
    ) -> R {
        let wrapped_function = move || {
            let out = function();
            Box::new(out) as Box<dyn Any + Send>
        };
        let response = self
            .tx
            .blocking_send(Box::new(wrapped_function))
            .expect("Failed to receive response") as Box<dyn Any>;

        *response.downcast::<R>().expect("Should downcast")
    }

    pub async fn execute_with_async<T: FnOnce() -> R + Send + 'static, R: Send + 'static>(
        &self,
        function: T,
    ) -> R {
        let wrapped_function = move || {
            let out = function();
            Box::new(out) as Box<dyn Any + Send>
        };
        let response = self
            .tx
            .send(Box::new(wrapped_function))
            .await
            .expect("Failed to receive response") as Box<dyn Any>;

        *response.downcast::<R>().expect("Should downcast")
    }
}