fuel_core_e2e_client/
lib.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
use crate::{
    config::SuiteConfig,
    test_context::TestContext,
};
use libtest_mimic::{
    Arguments,
    Failed,
    Trial,
};
use std::{
    env,
    fs,
    future::Future,
    time::Duration,
};

pub const CONFIG_FILE_KEY: &str = "FUEL_CORE_E2E_CONFIG";
pub const SYNC_TIMEOUT: Duration = Duration::from_secs(10);

pub mod config;
pub mod test_context;
pub mod tests;

pub fn main_body(config: SuiteConfig, mut args: Arguments) {
    fn with_cloned(
        config: &SuiteConfig,
        f: impl FnOnce(SuiteConfig) -> anyhow::Result<(), Failed>,
    ) -> impl FnOnce() -> anyhow::Result<(), Failed> {
        let config = config.clone();
        move || f(config)
    }

    // If we run tests in parallel they may fail because try to use the same state like UTXOs.
    args.test_threads = Some(1);

    let tests = vec![
        Trial::test(
            "can transfer from alice to bob",
            with_cloned(&config, |config| {
                async_execute(async {
                    let ctx = TestContext::new(config).await;
                    tests::transfers::basic_transfer(&ctx).await
                })
            }),
        ),
        Trial::test(
            "can transfer from alice to bob and back",
            with_cloned(&config, |config| {
                async_execute(async {
                    let ctx = TestContext::new(config).await;
                    tests::transfers::transfer_back(&ctx).await
                })
            }),
        ),
        Trial::test(
            "can collect fee from alice",
            with_cloned(&config, |config| {
                async_execute(async {
                    let ctx = TestContext::new(config).await;
                    tests::collect_fee::collect_fee(&ctx).await
                })
            }),
        ),
        Trial::test(
            "can execute script and get receipts",
            with_cloned(&config, |config| {
                async_execute(async {
                    let ctx = TestContext::new(config).await;
                    tests::transfers::transfer_back(&ctx).await
                })
            }),
        ),
        Trial::test(
            "can dry run transfer script and get receipts",
            with_cloned(&config, |config| {
                async_execute(async {
                    let ctx = TestContext::new(config).await;
                    tests::script::dry_run(&ctx).await
                })?;
                Ok(())
            }),
        ),
        Trial::test(
            "can dry run multiple transfer scripts and get receipts",
            with_cloned(&config, |config| {
                async_execute(async {
                    let ctx = TestContext::new(config).await;
                    tests::script::dry_run_multiple_txs(&ctx).await
                })?;
                Ok(())
            }),
        ),
        Trial::test(
            "dry run script that touches the contract with large state",
            with_cloned(&config, |config| {
                async_execute(async {
                    let ctx = TestContext::new(config).await;
                    tests::script::run_contract_large_state(&ctx).await
                })?;
                Ok(())
            }),
        ),
        Trial::test(
            "dry run transaction from `arbitrary_tx.raw` file",
            with_cloned(&config, |config| {
                async_execute(async {
                    let ctx = TestContext::new(config).await;
                    tests::script::arbitrary_transaction(&ctx).await
                })?;
                Ok(())
            }),
        ),
        Trial::test(
            "can deploy a large contract",
            with_cloned(&config, |config| {
                async_execute(async {
                    let ctx = TestContext::new(config).await;
                    tests::transfers::transfer_back(&ctx).await
                })
            }),
        ),
    ];

    libtest_mimic::run(&args, tests).exit();
}

pub fn load_config_env() -> SuiteConfig {
    // load from env var
    env::var_os(CONFIG_FILE_KEY)
        .map(|path| load_config(path.to_string_lossy().to_string()))
        .unwrap_or_default()
}

pub fn load_config(path: String) -> SuiteConfig {
    let file = fs::read(path).unwrap();
    toml::from_slice(&file).unwrap()
}

fn async_execute<F: Future<Output = anyhow::Result<(), Failed>>>(
    func: F,
) -> Result<(), Failed> {
    tokio::runtime::Builder::new_current_thread()
        .enable_all()
        .build()
        .unwrap()
        .block_on(func)
}