Skip to content

Commit

Permalink
feat!: Accept chain config as argument for node setup test helpers (#673
Browse files Browse the repository at this point in the history
)

###### Description of changes

Enables passing a `ChainConfig` parameter to all test helpers related to
node setup.
  • Loading branch information
digorithm authored Nov 4, 2022
1 parent 31415bb commit 88eb853
Show file tree
Hide file tree
Showing 16 changed files with 78 additions and 57 deletions.
5 changes: 3 additions & 2 deletions examples/contracts/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,8 @@ mod tests {
"packages/fuels/tests/contracts/contract_test/out/debug/contract_test-abi.json"
);

let wallets = launch_custom_provider_and_get_wallets(WalletsConfig::default(), None).await;
let wallets =
launch_custom_provider_and_get_wallets(WalletsConfig::default(), None, None).await;

let contract_id_1 = Contract::deploy(
"../../packages/fuels/tests/contracts/contract_test/out/debug/contract_test.bin",
Expand Down Expand Up @@ -569,7 +570,7 @@ mod tests {
);

let config = WalletsConfig::new(Some(2), Some(1), Some(DEFAULT_COIN_AMOUNT));
let mut wallets = launch_custom_provider_and_get_wallets(config, None).await;
let mut wallets = launch_custom_provider_and_get_wallets(config, None, None).await;
let wallet_1 = wallets.pop().unwrap();
let wallet_2 = wallets.pop().unwrap();

Expand Down
5 changes: 3 additions & 2 deletions examples/cookbook/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ mod tests {
.into();

let wallet_config = WalletsConfig::new_multiple_assets(1, asset_configs);
let wallets = launch_custom_provider_and_get_wallets(wallet_config, None).await;
let wallets = launch_custom_provider_and_get_wallets(wallet_config, None, None).await;
let wallet = &wallets[0];
// ANCHOR_END: liquidity_wallet

Expand Down Expand Up @@ -105,6 +105,7 @@ mod tests {
coins,
vec![],
Some(node_config),
None,
Some(consensus_parameters_config),
)
.await;
Expand All @@ -129,7 +130,7 @@ mod tests {
let (coins, _) =
setup_multiple_assets_coins(wallet_1.address(), NUM_ASSETS, NUM_COINS, AMOUNT);

let (provider, _) = setup_test_provider(coins, vec![], None).await;
let (provider, _) = setup_test_provider(coins, vec![], None, None).await;

wallet_1.set_provider(provider.clone());
wallet_2.set_provider(provider.clone());
Expand Down
1 change: 1 addition & 0 deletions examples/predicates/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ mod tests {
utxo_validation: true,
..Config::local_node()
}),
None,
)
.await;

Expand Down
2 changes: 1 addition & 1 deletion examples/providers/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ mod tests {
);
// ANCHOR_END: setup_single_asset

let (provider, _) = setup_test_provider(coins.clone(), vec![], None).await;
let (provider, _) = setup_test_provider(coins.clone(), vec![], None, None).await;
// ANCHOR_END: setup_test_blockchain

// ANCHOR: get_coins
Expand Down
24 changes: 13 additions & 11 deletions examples/wallets/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ mod tests {
use fuels::prelude::*;

// Use the test helper to setup a test provider.
let (provider, _address) = setup_test_provider(vec![], vec![], None).await;
let (provider, _address) = setup_test_provider(vec![], vec![], None, None).await;

// Create the wallet.
let _wallet = WalletUnlocked::new_random(Some(provider));
Expand All @@ -23,7 +23,7 @@ mod tests {
use std::str::FromStr;

// Use the test helper to setup a test provider.
let (provider, _address) = setup_test_provider(vec![], vec![], None).await;
let (provider, _address) = setup_test_provider(vec![], vec![], None, None).await;

// Setup the private key.
let secret = SecretKey::from_str(
Expand All @@ -45,7 +45,7 @@ mod tests {
"oblige salon price punch saddle immune slogan rare snap desert retire surprise";

// Use the test helper to setup a test provider.
let (provider, _address) = setup_test_provider(vec![], vec![], None).await;
let (provider, _address) = setup_test_provider(vec![], vec![], None, None).await;

// Create first account from mnemonic phrase.
let _wallet = WalletUnlocked::new_from_mnemonic_phrase_with_path(
Expand Down Expand Up @@ -73,7 +73,7 @@ mod tests {
let mut rng = rand::thread_rng();

// Use the test helper to setup a test provider.
let (provider, _address) = setup_test_provider(vec![], vec![], None).await;
let (provider, _address) = setup_test_provider(vec![], vec![], None, None).await;

let password = "my_master_password";

Expand All @@ -99,7 +99,7 @@ mod tests {
"oblige salon price punch saddle immune slogan rare snap desert retire surprise";

// Use the test helper to setup a test provider.
let (provider, _address) = setup_test_provider(vec![], vec![], None).await;
let (provider, _address) = setup_test_provider(vec![], vec![], None, None).await;

// Create first account from mnemonic phrase.
let wallet = WalletUnlocked::new_from_mnemonic_phrase(phrase, Some(provider))?;
Expand All @@ -125,6 +125,7 @@ mod tests {
let wallets = launch_custom_provider_and_get_wallets(
WalletsConfig::new(num_wallets, coins_per_wallet, coin_amount),
None,
None,
)
.await;

Expand Down Expand Up @@ -165,7 +166,7 @@ mod tests {
};

let wallet_config = WalletsConfig::new_multiple_assets(1, vec![random_asset, base_asset]);
let wallet = launch_custom_provider_and_get_wallets(wallet_config, None)
let wallet = launch_custom_provider_and_get_wallets(wallet_config, None, None)
.await
.pop()
.unwrap();
Expand Down Expand Up @@ -215,7 +216,8 @@ mod tests {
use fuels::prelude::*;
// This helper will launch a local node and provide 10 test wallets linked to it.
// The initial balance defaults to 1 coin per wallet with an amount of 1_000_000_000
let wallets = launch_custom_provider_and_get_wallets(WalletsConfig::default(), None).await;
let wallets =
launch_custom_provider_and_get_wallets(WalletsConfig::default(), None, None).await;
// ANCHOR_END: multiple_wallets_helper
// ANCHOR: setup_5_wallets
let num_wallets = 5;
Expand All @@ -228,7 +230,7 @@ mod tests {
Some(amount_per_coin),
);
// Launches a local node and provides test wallets as specified by the config
let wallets = launch_custom_provider_and_get_wallets(config, None).await;
let wallets = launch_custom_provider_and_get_wallets(config, None, None).await;
// ANCHOR_END: setup_5_wallets
Ok(())
}
Expand All @@ -251,7 +253,7 @@ mod tests {
amount_per_coin,
);
// ANCHOR_END: multiple_assets_coins
let (provider, _socket_addr) = setup_test_provider(coins.clone(), vec![], None).await;
let (provider, _socket_addr) = setup_test_provider(coins.clone(), vec![], None, None).await;
wallet.set_provider(provider);
// ANCHOR_END: multiple_assets_wallet
Ok(())
Expand Down Expand Up @@ -292,13 +294,13 @@ mod tests {
let assets = vec![asset_base, asset_1, asset_2];

let coins = setup_custom_assets_coins(wallet.address(), &assets);
let (provider, _socket_addr) = setup_test_provider(coins, vec![], None).await;
let (provider, _socket_addr) = setup_test_provider(coins, vec![], None, None).await;
wallet.set_provider(provider);
// ANCHOR_END: custom_assets_wallet
// ANCHOR: custom_assets_wallet_short
let num_wallets = 1;
let wallet_config = WalletsConfig::new_multiple_assets(num_wallets, assets);
let wallets = launch_custom_provider_and_get_wallets(wallet_config, None).await;
let wallets = launch_custom_provider_and_get_wallets(wallet_config, None, None).await;
// ANCHOR_END: custom_assets_wallet_short
Ok(())
}
Expand Down
4 changes: 2 additions & 2 deletions packages/fuels-signers/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ mod tests {
coins_1.extend(coins_2);

// Setup a provider and node with both set of coins.
let (client, _) = setup_test_client(coins_1, vec![], None, None).await;
let (client, _) = setup_test_client(coins_1, vec![], None, None, None).await;
let provider = Provider::new(client);

wallet_1.set_provider(provider.clone());
Expand Down Expand Up @@ -235,7 +235,7 @@ mod tests {

coins_1.extend(coins_2);

let (client, _) = setup_test_client(coins_1, vec![], None, None).await;
let (client, _) = setup_test_client(coins_1, vec![], None, None, None).await;
let provider = Provider::new(client);

wallet_1.set_provider(provider.clone());
Expand Down
2 changes: 1 addition & 1 deletion packages/fuels-signers/src/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ impl Provider {
/// use fuels::prelude::*;
/// async fn foo() -> Result<(), Box<dyn std::error::Error>> {
/// // Setup local test node
/// let (provider, _) = setup_test_provider(vec![], vec![], None).await;
/// let (provider, _) = setup_test_provider(vec![], vec![], None, None).await;
/// let tx = Script::default();
///
/// let receipts = provider.send_transaction(&tx).await?;
Expand Down
8 changes: 4 additions & 4 deletions packages/fuels-signers/src/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ pub struct Wallet {
///
/// async fn foo() -> Result<(), Error> {
/// // Setup local test node
/// let (provider, _) = setup_test_provider(vec![], vec![], None).await;
/// let (provider, _) = setup_test_provider(vec![], vec![], None, None).await;
///
/// // Create a new local wallet with the newly generated key
/// let wallet = WalletUnlocked::new_random(Some(provider));
Expand Down Expand Up @@ -572,7 +572,7 @@ impl WalletUnlocked {
/// coins_1.extend(coins_2);
///
/// // Setup a provider and node with both set of coins
/// let (provider, _) = setup_test_provider(coins_1, vec![], None).await;
/// let (provider, _) = setup_test_provider(coins_1, vec![], None, None).await;
///
/// // Set provider for wallets
/// wallet_1.set_provider(provider.clone());
Expand Down Expand Up @@ -1014,7 +1014,7 @@ mod tests {
#[tokio::test]
async fn add_fee_coins_empty_transaction() -> Result<(), Error> {
let wallet_config = add_fee_coins_wallet_config(1);
let wallet = launch_custom_provider_and_get_wallets(wallet_config, None)
let wallet = launch_custom_provider_and_get_wallets(wallet_config, None, None)
.await
.pop()
.unwrap();
Expand Down Expand Up @@ -1043,7 +1043,7 @@ mod tests {
#[tokio::test]
async fn add_fee_coins_to_transfer_with_base_asset() -> Result<(), Error> {
let wallet_config = add_fee_coins_wallet_config(1);
let wallet = launch_custom_provider_and_get_wallets(wallet_config, None)
let wallet = launch_custom_provider_and_get_wallets(wallet_config, None, None)
.await
.pop()
.unwrap();
Expand Down
31 changes: 19 additions & 12 deletions packages/fuels-test-helpers/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ extern crate core;
use std::net::SocketAddr;

#[cfg(feature = "fuel-core-lib")]
use fuel_chain_config::{ChainConfig, CoinConfig, MessageConfig, StateConfig};
use fuel_chain_config::{CoinConfig, MessageConfig, StateConfig};

#[cfg(feature = "fuel-core-lib")]
use fuel_core::{
Expand All @@ -26,6 +26,7 @@ use fuel_core_interfaces::model::{DaBlockHeight, Message};
#[cfg(not(feature = "fuel-core-lib"))]
use portpicker::is_free;

use fuel_chain_config::ChainConfig;
use fuel_gql_client::fuel_tx::ConsensusParameters;
use fuel_gql_client::{
client::FuelClient,
Expand Down Expand Up @@ -157,6 +158,7 @@ pub async fn setup_test_client(
coins: Vec<(UtxoId, Coin)>,
messages: Vec<Message>,
node_config: Option<Config>,
chain_config: Option<ChainConfig>,
consensus_parameters_config: Option<ConsensusParameters>,
) -> (FuelClient, SocketAddr) {
let coin_configs = coins
Expand Down Expand Up @@ -185,16 +187,19 @@ pub async fn setup_test_client(
.collect();

// Setup node config with genesis coins and utxo_validation enabled
let chain_conf = chain_config.unwrap_or_else(|| ChainConfig {
initial_state: Some(StateConfig {
coins: Some(coin_configs),
contracts: None,
messages: Some(message_config),
..StateConfig::default()
}),
transaction_parameters: consensus_parameters_config.unwrap_or_default(),
..ChainConfig::local_testnet()
});

let config = Config {
chain_conf: ChainConfig {
initial_state: Some(StateConfig {
messages: Some(message_config),
coins: Some(coin_configs),
..StateConfig::default()
}),
transaction_parameters: consensus_parameters_config.unwrap_or_default(),
..ChainConfig::local_testnet()
},
chain_conf,
database_type: DbType::InMemory,
..node_config.unwrap_or_else(Config::local_node)
};
Expand All @@ -210,6 +215,7 @@ pub async fn setup_test_client(
coins: Vec<(UtxoId, 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);
Expand All @@ -230,6 +236,7 @@ pub async fn setup_test_client(
addr: bound_address,
..config
},
chain_config,
consensus_parameters_config,
)
.await;
Expand Down Expand Up @@ -378,7 +385,7 @@ mod tests {
..Config::local_node()
};

let wallets = setup_test_client(coins, vec![], Some(config), None).await;
let wallets = setup_test_client(coins, vec![], Some(config), None, None).await;

assert_eq!(wallets.1, socket);
Ok(())
Expand All @@ -398,7 +405,7 @@ mod tests {
);

let (fuel_client, _) =
setup_test_client(coins, vec![], None, Some(consensus_parameters_config)).await;
setup_test_client(coins, vec![], None, None, Some(consensus_parameters_config)).await;
let provider = Provider::new(fuel_client);
wallet.set_provider(provider.clone());

Expand Down
10 changes: 7 additions & 3 deletions packages/fuels-test-helpers/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,13 +164,14 @@ impl<'de> DeserializeAs<'de, BlockHeight> for HexNumber {
pub fn get_node_config_json(
coins: Vec<(UtxoId, Coin)>,
messages: Vec<Message>,
chain_config: Option<ChainConfig>,
consensus_parameters_config: Option<ConsensusParameters>,
) -> Value {
let coins = get_coins_value(coins);
let messages = get_messages_value(messages);
let transaction_parameters = consensus_parameters_config.unwrap_or_default();

let chain_config = ChainConfig {
let chain_config = chain_config.unwrap_or_else(|| ChainConfig {
chain_name: "local_testnet".to_string(),
block_production: BlockProduction::ProofOfAuthority {
trigger: Default::default(),
Expand All @@ -183,7 +184,7 @@ pub fn get_node_config_json(
height: None,
}),
transaction_parameters,
};
});

serde_json::to_value(&chain_config).expect("Failed to build `ChainConfig` JSON")
}
Expand Down Expand Up @@ -233,13 +234,15 @@ pub async fn new_fuel_node(
coins: Vec<(UtxoId, Coin)>,
messages: Vec<Message>,
config: Config,
chain_config: Option<ChainConfig>,
consensus_parameters_config: Option<ConsensusParameters>,
) {
// Create a new one-shot channel for sending single values across asynchronous tasks.
let (tx, rx) = oneshot::channel();

tokio::spawn(async move {
let config_json = get_node_config_json(coins, messages, consensus_parameters_config);
let config_json =
get_node_config_json(coins, messages, chain_config, consensus_parameters_config);
let temp_config_file = write_temp_config_file(config_json);

let port = &config.addr.port().to_string();
Expand Down Expand Up @@ -349,6 +352,7 @@ impl FuelService {
..config
},
None,
None,
)
.await;

Expand Down
2 changes: 1 addition & 1 deletion packages/fuels-test-helpers/src/script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ mod tests {
DEFAULT_NUM_COINS,
DEFAULT_COIN_AMOUNT,
);
let (provider, _) = setup_test_provider(coins, vec![], None).await;
let (provider, _) = setup_test_provider(coins, vec![], None, None).await;

let return_val =
run_compiled_script(path_to_bin, TxParameters::default(), Some(provider)).await?;
Expand Down
Loading

0 comments on commit 88eb853

Please sign in to comment.