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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
extern crate core;
use std::{iter::repeat, iter::zip, net::SocketAddr};
#[cfg(feature = "fuel-core-lib")]
use fuel_chain_config::StateConfig;
#[cfg(feature = "fuel-core-lib")]
use fuel_core::service::{DbType, FuelService};
#[cfg(feature = "fuel-core-lib")]
pub use fuel_core::service::Config;
#[cfg(feature = "fuel-core-lib")]
pub use utils::{get_coin_configs, get_message_configs};
#[cfg(not(feature = "fuel-core-lib"))]
pub use node::{get_socket_address, new_fuel_node, Config};
#[cfg(not(feature = "fuel-core-lib"))]
use portpicker::is_free;
use fuels_types::{
coin::{Coin, CoinStatus},
message::Message,
};
use fuel_chain_config::ChainConfig;
use fuel_gql_client::{
client::FuelClient,
fuel_tx::{Bytes32, ConsensusParameters, UtxoId},
};
use fuels_core::constants::BASE_ASSET_ID;
use fuels_signers::fuel_crypto::{fuel_types::AssetId, rand};
use fuels_types::param_types::ParamType;
use rand::Fill;
#[cfg(not(feature = "fuel-core-lib"))]
pub mod node;
mod chains;
#[cfg(feature = "fuels-signers")]
mod signers;
mod utils;
mod wallets_config;
#[cfg(not(feature = "fuel-core-lib"))]
pub use node::*;
pub use chains::*;
use fuels_types::bech32::Bech32Address;
#[cfg(feature = "fuels-signers")]
pub use signers::*;
pub use wallets_config::*;
pub fn generate_unused_field_names(types: Vec<ParamType>) -> Vec<(String, ParamType)> {
zip(repeat("unused".to_string()), types).collect()
}
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();
let asset_ids = (0..(num_asset - 1))
.map(|_| {
let mut random_asset_id = AssetId::zeroed();
random_asset_id.try_fill(&mut rng).unwrap();
random_asset_id
})
.chain([BASE_ASSET_ID].into_iter())
.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)
}
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
}
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).unwrap();
let utxo_id = UtxoId::new(r, 0);
Coin {
owner: owner.clone(),
utxo_id,
amount: amount_per_coin,
asset_id,
maturity: Default::default(),
status: CoinStatus::Unspent,
block_created: Default::default(),
}
})
.collect();
coins
}
pub fn setup_single_message(
sender: &Bech32Address,
recipient: &Bech32Address,
amount: u64,
nonce: u64,
data: Vec<u8>,
) -> Vec<Message> {
vec![Message {
sender: sender.clone(),
recipient: recipient.clone(),
nonce,
amount,
data,
da_height: 0,
fuel_block_spend: None,
}]
}
#[cfg(feature = "fuel-core-lib")]
pub async fn setup_test_client(
coins: Vec<Coin>,
messages: Vec<Message>,
node_config: Option<Config>,
chain_config: Option<ChainConfig>,
consensus_parameters_config: Option<ConsensusParameters>,
) -> (FuelClient, SocketAddr) {
let coin_configs = get_coin_configs(coins);
let message_configs = get_message_configs(messages);
let chain_conf = {
let chain_conf = chain_config.unwrap_or_else(|| ChainConfig {
initial_state: Some(StateConfig {
coins: Some(coin_configs),
contracts: None,
messages: Some(message_configs),
..StateConfig::default()
}),
..ChainConfig::local_testnet()
});
if let Some(transaction_parameters) = consensus_parameters_config {
ChainConfig {
transaction_parameters,
..chain_conf
}
} else {
chain_conf
}
};
let config = Config {
chain_conf,
database_type: DbType::InMemory,
..node_config.unwrap_or_else(Config::local_node)
};
let srv = FuelService::new_node(config).await.unwrap();
let client = FuelClient::from(srv.bound_address);
(client, srv.bound_address)
}
#[cfg(not(feature = "fuel-core-lib"))]
pub async fn setup_test_client(
coins: Vec<Coin>,
messages: Vec<Message>,
node_config: Option<Config>,
chain_config: Option<ChainConfig>,
consensus_parameters_config: Option<ConsensusParameters>,
) -> (FuelClient, SocketAddr) {
let config = node_config.unwrap_or_else(Config::local_node);
let requested_port = config.addr.port();
let bound_address = if requested_port == 0 {
get_socket_address()
} else if is_free(requested_port) {
config.addr
} else {
panic!("Error: Address already in use");
};
new_fuel_node(
coins,
messages,
Config {
addr: bound_address,
..config
},
chain_config,
consensus_parameters_config,
)
.await;
let client = FuelClient::from(bound_address);
server_health_check(&client).await;
(client, bound_address)
}
#[cfg(test)]
mod tests {
use super::*;
use fuels_contract::contract::Contract;
use fuels_core::parameters::{StorageConfiguration, TxParameters};
use fuels_signers::provider::Provider;
use fuels_signers::WalletUnlocked;
use fuels_types::bech32::FUEL_BECH32_HRP;
use std::net::Ipv4Addr;
#[tokio::test]
async fn test_setup_single_asset_coins() -> Result<(), rand::Error> {
let mut rng = rand::thread_rng();
let mut addr_data = Bytes32::new([0u8; 32]);
addr_data.try_fill(&mut rng)?;
let address = Bech32Address::new("test", addr_data);
let mut asset_id = AssetId::zeroed();
asset_id.try_fill(&mut rng)?;
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<(), rand::Error> {
let mut rng = rand::thread_rng();
let mut addr_data = Bytes32::new([0u8; 32]);
addr_data.try_fill(&mut rng)?;
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);
assert!(unique_asset_ids
.iter()
.any(|&asset_id| asset_id == BASE_ASSET_ID));
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<(), rand::Error> {
let mut rng = rand::thread_rng();
let mut hash = [0u8; 32];
hash.try_fill(&mut rng)?;
let address = Bech32Address::new(FUEL_BECH32_HRP, hash);
let asset_base = AssetConfig {
id: BASE_ASSET_ID,
num_coins: 2,
coin_amount: 4,
};
let mut asset_id_1 = AssetId::zeroed();
asset_id_1.try_fill(&mut rng)?;
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)?;
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_client_custom_config() -> Result<(), rand::Error> {
let socket = SocketAddr::new(Ipv4Addr::new(127, 0, 0, 1).into(), 4000);
let wallet = WalletUnlocked::new_random(None);
let coins: Vec<Coin> = setup_single_asset_coins(
wallet.address(),
Default::default(),
DEFAULT_NUM_COINS,
DEFAULT_COIN_AMOUNT,
);
let config = Config {
addr: socket,
..Config::local_node()
};
let wallets = setup_test_client(coins, vec![], Some(config), None, None).await;
assert_eq!(wallets.1, socket);
Ok(())
}
#[tokio::test]
async fn test_setup_test_client_consensus_parameters_config() {
let consensus_parameters_config = ConsensusParameters::DEFAULT.with_max_gas_per_tx(1);
let mut wallet = WalletUnlocked::new_random(None);
let coins: Vec<Coin> = setup_single_asset_coins(
wallet.address(),
Default::default(),
DEFAULT_NUM_COINS,
DEFAULT_COIN_AMOUNT,
);
let (fuel_client, _) =
setup_test_client(coins, vec![], None, None, Some(consensus_parameters_config)).await;
let provider = Provider::new(fuel_client);
wallet.set_provider(provider.clone());
let result = Contract::deploy(
"../fuels/tests/types/contract_output_test/out/debug/contract_output_test.bin",
&wallet,
TxParameters::default(),
StorageConfiguration::default(),
)
.await;
let expected = result.expect_err("should fail");
let error_string = "Validation error: TransactionGasLimit";
assert!(expected.to_string().contains(error_string));
}
#[tokio::test]
async fn test_chain_config_and_consensus_parameters() {
let consensus_parameters_config = ConsensusParameters::DEFAULT
.with_max_inputs(123)
.with_gas_per_byte(456);
let chain_config = ChainConfig {
chain_name: "Solo_Munib".to_string(),
..ChainConfig::local_testnet()
};
let (fuel_client, _) = setup_test_client(
vec![],
vec![],
None,
Some(chain_config),
Some(consensus_parameters_config),
)
.await;
let chain_info = fuel_client.chain_info().await.unwrap();
assert_eq!(chain_info.name, "Solo_Munib");
assert_eq!(chain_info.consensus_parameters.max_inputs.0, 123);
assert_eq!(chain_info.consensus_parameters.gas_per_byte.0, 456);
}
}