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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
//! Testing helpers/utilities for Fuel SDK.
extern crate core;

#[cfg(feature = "fuels-accounts")]
pub use accounts::*;
use fuel_tx::{Bytes32, ConsensusParameters, ContractParameters, TxParameters, UtxoId};
use fuel_types::{AssetId, Nonce};
use fuels_accounts::provider::Provider;
use fuels_core::types::{
    bech32::Bech32Address,
    coin::{Coin, CoinStatus},
    errors::Result,
    message::{Message, MessageStatus},
};
pub use node_types::*;
use rand::{rngs::StdRng, Fill, Rng, SeedableRng};
use utils::{into_coin_configs, into_message_configs};
pub use wallets_config::*;
mod node_types;

#[cfg(not(feature = "fuel-core-lib"))]
pub(crate) mod fuel_bin_service;

#[cfg(feature = "fuels-accounts")]
mod accounts;

pub use service::*;
mod service;

mod utils;
mod wallets_config;

/// Create a vector of `num_asset`*`coins_per_asset` UTXOs and a vector of the unique corresponding
/// asset IDs. `AssetId`. Each UTXO (=coin) contains `amount_per_coin` amount of a random asset. The
/// output of this function can be used with `setup_test_provider` to get a client with some
/// pre-existing coins, with `num_asset` different asset ids. Note that one of the assets is the
/// base asset to pay for gas.
pub fn setup_multiple_assets_coins(
    owner: &Bech32Address,
    num_asset: u64,
    coins_per_asset: u64,
    amount_per_coin: u64,
) -> (Vec<Coin>, Vec<AssetId>) {
    let mut rng = rand::thread_rng();
    // Create `num_asset-1` asset ids so there is `num_asset` in total with the base asset
    let asset_ids = (0..(num_asset - 1))
        .map(|_| {
            let mut random_asset_id = AssetId::zeroed();
            random_asset_id
                .try_fill(&mut rng)
                .expect("failed to fill with random data");
            random_asset_id
        })
        .chain([AssetId::zeroed()])
        .collect::<Vec<AssetId>>();

    let coins = asset_ids
        .iter()
        .flat_map(|id| setup_single_asset_coins(owner, *id, coins_per_asset, amount_per_coin))
        .collect::<Vec<Coin>>();

    (coins, asset_ids)
}

/// Create a vector of UTXOs with the provided AssetIds, num_coins, and amount_per_coin
pub fn setup_custom_assets_coins(owner: &Bech32Address, assets: &[AssetConfig]) -> Vec<Coin> {
    let coins = assets
        .iter()
        .flat_map(|asset| {
            setup_single_asset_coins(owner, asset.id, asset.num_coins, asset.coin_amount)
        })
        .collect::<Vec<Coin>>();
    coins
}

/// Create a vector of `num_coins` UTXOs containing `amount_per_coin` amount of asset `asset_id`.
/// The output of this function can be used with `setup_test_provider` to get a client with some
/// pre-existing coins, but with only one asset ID.
pub fn setup_single_asset_coins(
    owner: &Bech32Address,
    asset_id: AssetId,
    num_coins: u64,
    amount_per_coin: u64,
) -> Vec<Coin> {
    let mut rng = rand::thread_rng();

    let coins: Vec<Coin> = (1..=num_coins)
        .map(|_i| {
            let mut r = Bytes32::zeroed();
            r.try_fill(&mut rng)
                .expect("failed to fill with random data");
            let utxo_id = UtxoId::new(r, 0);

            Coin {
                owner: owner.clone(),
                utxo_id,
                amount: amount_per_coin,
                asset_id,
                status: CoinStatus::Unspent,
                block_created: Default::default(),
            }
        })
        .collect();

    coins
}

pub fn setup_single_message(
    sender: &Bech32Address,
    recipient: &Bech32Address,
    amount: u64,
    nonce: Nonce,
    data: Vec<u8>,
) -> Message {
    Message {
        sender: sender.clone(),
        recipient: recipient.clone(),
        nonce,
        amount,
        data,
        da_height: 0,
        status: MessageStatus::Unspent,
    }
}

pub async fn setup_test_provider(
    coins: Vec<Coin>,
    messages: Vec<Message>,
    node_config: Option<NodeConfig>,
    chain_config: Option<ChainConfig>,
) -> Result<Provider> {
    let node_config = node_config.unwrap_or_default();
    let chain_config = chain_config.unwrap_or_else(testnet_chain_config);

    let coin_configs = into_coin_configs(coins);
    let message_configs = into_message_configs(messages);

    let state_config = StateConfig {
        coins: coin_configs,
        messages: message_configs,
        ..StateConfig::local_testnet()
    };

    let srv = FuelService::start(node_config, chain_config, state_config).await?;

    let address = srv.bound_address();

    tokio::spawn(async move {
        let _own_the_handle = srv;
        let () = futures::future::pending().await;
    });

    Provider::from(address).await
}

// Testnet ChainConfig with increased tx size and contract size limits
fn testnet_chain_config() -> ChainConfig {
    let mut consensus_parameters = ConsensusParameters::default();
    let tx_params = TxParameters::default().with_max_size(10_000_000);
    let contract_params = ContractParameters::default().with_contract_max_size(1_000_000);
    consensus_parameters.set_tx_params(tx_params);
    consensus_parameters.set_contract_params(contract_params);

    ChainConfig {
        consensus_parameters,
        ..ChainConfig::local_testnet()
    }
}

pub fn generate_random_salt() -> [u8; 32] {
    StdRng::from_entropy().gen()
}

#[cfg(test)]
mod tests {
    use std::net::{Ipv4Addr, SocketAddr};

    use fuel_tx::{ConsensusParameters, ContractParameters, FeeParameters, TxParameters};
    use fuels_core::types::bech32::FUEL_BECH32_HRP;

    use super::*;

    #[tokio::test]
    async fn test_setup_single_asset_coins() -> Result<()> {
        let mut rng = rand::thread_rng();
        let mut addr_data = Bytes32::new([0u8; 32]);
        addr_data
            .try_fill(&mut rng)
            .expect("failed to fill with random data");
        let address = Bech32Address::new("test", addr_data);

        let mut asset_id = AssetId::zeroed();
        asset_id
            .try_fill(&mut rng)
            .expect("failed to fill with random data");

        let number_of_coins = 11;
        let amount_per_coin = 10;
        let coins = setup_single_asset_coins(&address, asset_id, number_of_coins, amount_per_coin);

        assert_eq!(coins.len() as u64, number_of_coins);
        for coin in coins {
            assert_eq!(coin.asset_id, asset_id);
            assert_eq!(coin.amount, amount_per_coin);
            assert_eq!(coin.owner, address);
        }

        Ok(())
    }

    #[tokio::test]
    async fn test_setup_multiple_assets_coins() -> Result<()> {
        let mut rng = rand::thread_rng();
        let mut addr_data = Bytes32::new([0u8; 32]);
        addr_data
            .try_fill(&mut rng)
            .expect("failed to fill with random data");
        let address = Bech32Address::new("test", addr_data);

        let number_of_assets = 7;
        let coins_per_asset = 10;
        let amount_per_coin = 13;
        let (coins, unique_asset_ids) = setup_multiple_assets_coins(
            &address,
            number_of_assets,
            coins_per_asset,
            amount_per_coin,
        );

        assert_eq!(coins.len() as u64, number_of_assets * coins_per_asset);
        assert_eq!(unique_asset_ids.len() as u64, number_of_assets);
        // Check that the wallet has base assets to pay for gas
        assert!(unique_asset_ids
            .iter()
            .any(|&asset_id| asset_id == AssetId::zeroed()));
        for asset_id in unique_asset_ids {
            let coins_asset_id: Vec<Coin> = coins
                .clone()
                .into_iter()
                .filter(|c| c.asset_id == asset_id)
                .collect();
            assert_eq!(coins_asset_id.len() as u64, coins_per_asset);
            for coin in coins_asset_id {
                assert_eq!(coin.owner, address);
                assert_eq!(coin.amount, amount_per_coin);
            }
        }

        Ok(())
    }

    #[tokio::test]
    async fn test_setup_custom_assets_coins() -> Result<()> {
        let mut rng = rand::thread_rng();
        let mut hash = [0u8; 32];
        hash.try_fill(&mut rng)
            .expect("failed to fill with random data");
        let address = Bech32Address::new(FUEL_BECH32_HRP, hash);

        let asset_base = AssetConfig {
            id: AssetId::zeroed(),
            num_coins: 2,
            coin_amount: 4,
        };

        let mut asset_id_1 = AssetId::zeroed();
        asset_id_1
            .try_fill(&mut rng)
            .expect("failed to fill with random data");
        let asset_1 = AssetConfig {
            id: asset_id_1,
            num_coins: 6,
            coin_amount: 8,
        };

        let mut asset_id_2 = AssetId::zeroed();
        asset_id_2
            .try_fill(&mut rng)
            .expect("failed to fill with random data");
        let asset_2 = AssetConfig {
            id: asset_id_2,
            num_coins: 10,
            coin_amount: 12,
        };

        let assets = vec![asset_base, asset_1, asset_2];
        let coins = setup_custom_assets_coins(&address, &assets);

        for asset in assets {
            let coins_asset_id: Vec<Coin> = coins
                .clone()
                .into_iter()
                .filter(|c| c.asset_id == asset.id)
                .collect();
            assert_eq!(coins_asset_id.len() as u64, asset.num_coins);
            for coin in coins_asset_id {
                assert_eq!(coin.owner, address);
                assert_eq!(coin.amount, asset.coin_amount);
            }
        }
        Ok(())
    }

    #[tokio::test]
    async fn test_setup_test_provider_custom_config() -> Result<()> {
        let socket = SocketAddr::new(Ipv4Addr::new(127, 0, 0, 1).into(), 4000);
        let config = NodeConfig {
            addr: socket,
            ..NodeConfig::default()
        };

        let provider = setup_test_provider(vec![], vec![], Some(config.clone()), None).await?;
        let node_info = provider
            .node_info()
            .await
            .expect("Failed to retrieve node info!");

        assert_eq!(provider.url(), format!("http://127.0.0.1:4000"));
        assert_eq!(node_info.utxo_validation, config.utxo_validation);

        Ok(())
    }

    #[tokio::test]
    async fn test_setup_test_client_consensus_parameters_config() -> Result<()> {
        let tx_params = TxParameters::default()
            .with_max_gas_per_tx(2)
            .with_max_inputs(58);
        let fee_params = FeeParameters::default().with_gas_per_byte(2);
        let contract_params = ContractParameters::default().with_max_storage_slots(83);

        let mut consensus_parameters = ConsensusParameters::default();
        consensus_parameters.set_tx_params(tx_params);
        consensus_parameters.set_fee_params(fee_params);
        consensus_parameters.set_contract_params(contract_params);

        let chain_config = ChainConfig {
            consensus_parameters: consensus_parameters.clone(),
            ..ChainConfig::default()
        };
        let provider = setup_test_provider(vec![], vec![], None, Some(chain_config)).await?;

        let retrieved_parameters = provider.consensus_parameters();

        assert_eq!(*retrieved_parameters, consensus_parameters);

        Ok(())
    }

    #[tokio::test]
    async fn test_chain_config_and_consensus_parameters() -> Result<()> {
        let max_inputs = 123;
        let gas_per_byte = 456;

        let mut consensus_parameters = ConsensusParameters::default();

        let tx_params = TxParameters::default().with_max_inputs(max_inputs);
        consensus_parameters.set_tx_params(tx_params);

        let fee_params = FeeParameters::default().with_gas_per_byte(gas_per_byte);
        consensus_parameters.set_fee_params(fee_params);

        let chain_name = "fuel-0".to_string();
        let chain_config = ChainConfig {
            chain_name: chain_name.clone(),
            consensus_parameters,
            ..ChainConfig::local_testnet()
        };

        let provider = setup_test_provider(vec![], vec![], None, Some(chain_config)).await?;

        let chain_info = provider.chain_info().await?;

        assert_eq!(chain_info.name, chain_name);
        assert_eq!(
            chain_info.consensus_parameters.tx_params().max_inputs(),
            max_inputs
        );
        assert_eq!(
            chain_info.consensus_parameters.fee_params().gas_per_byte(),
            gas_per_byte
        );
        Ok(())
    }
}