Skip to content

Commit

Permalink
feat!: make functions return Result and implement Node Killer (#1067)
Browse files Browse the repository at this point in the history
Refactored functions to return Result for better error handling.

+ launch_provider_and_get_wallet()
+ launch_custom_provider_and_get_wallets()
+ setup_test_provider()
+ setup_test_client()

I've implemented FuelService and added support for the
fuel_core_services::Service trait. This allows us to have finer control
over the behavior of FuelNode;

```rust
pub struct FuelService {
    #[cfg(feature = "fuel-core-lib")]
    service: CoreFuelService,
    #[cfg(not(feature = "fuel-core-lib"))]
    service: BinFuelService,
    bound_address: SocketAddr,
}
```

Co-authored-by: Ahmed Sagdati <[email protected]>
Co-authored-by: hal3e <[email protected]>
  • Loading branch information
3 people authored Oct 14, 2023
1 parent 746dcf7 commit d416a49
Show file tree
Hide file tree
Showing 34 changed files with 749 additions and 687 deletions.
2 changes: 1 addition & 1 deletion docs/src/connecting/short-lived.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ You can also use the test helper `setup_test_provider()` for this:
You can also use `launch_provider_and_get_wallet()`, which abstracts away the `setup_test_provider()` and the wallet creation, all in one single method:

```rust,ignore
let wallet = launch_provider_and_get_wallet().await;
let wallet = launch_provider_and_get_wallet().await?;
```

## Features
Expand Down
44 changes: 18 additions & 26 deletions examples/contracts/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,20 @@
mod tests {
use fuels::{
core::codec::DecoderConfig,
prelude::{LoadConfiguration, StorageConfiguration},
types::{
errors::{error, Error, Result},
Bits256,
},
prelude::{Config, LoadConfiguration, StorageConfiguration},
types::{errors::Result, Bits256},
};

#[tokio::test]
async fn instantiate_client() -> Result<()> {
// ANCHOR: instantiate_client
use fuels::{
fuel_node::{Config, FuelService},
prelude::Provider,
};
use fuels::prelude::{FuelService, Provider};

// Run the fuel node.
let server = FuelService::new_node(Config::local_node())
.await
.map_err(|err| error!(InfrastructureError, "{err}"))?;
let server = FuelService::start(Config::local_node()).await?;

// Create a client that will talk to the node created above.
let client = Provider::from(server.bound_address).await?;
let client = Provider::from(server.bound_address()).await?;
assert!(client.healthy().await?);
// ANCHOR_END: instantiate_client
Ok(())
Expand All @@ -35,7 +27,7 @@ mod tests {

// ANCHOR: deploy_contract
// This helper will launch a local node and provide a test wallet linked to it
let wallet = launch_provider_and_get_wallet().await;
let wallet = launch_provider_and_get_wallet().await?;

// This will load and deploy your contract binary to the chain so that its ID can
// be used to initialize the instance
Expand Down Expand Up @@ -91,7 +83,7 @@ mod tests {
abi = "packages/fuels/tests/contracts/contract_test/out/debug/contract_test-abi.json"
));

let wallet = launch_provider_and_get_wallet().await;
let wallet = launch_provider_and_get_wallet().await?;

let contract_id = Contract::load_from(
"../../packages/fuels/tests/contracts/contract_test/out/debug/contract_test.bin",
Expand Down Expand Up @@ -124,7 +116,7 @@ mod tests {
};
use rand::prelude::{Rng, SeedableRng, StdRng};

let wallet = launch_provider_and_get_wallet().await;
let wallet = launch_provider_and_get_wallet().await?;

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

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

let contract_id_1 = Contract::load_from(
"../../packages/fuels/tests/contracts/contract_test/out/debug/contract_test.bin",
Expand Down Expand Up @@ -275,7 +267,7 @@ mod tests {
abi = "packages/fuels/tests/contracts/contract_test/out/debug/contract_test-abi.json"
));

let wallet = launch_provider_and_get_wallet().await;
let wallet = launch_provider_and_get_wallet().await?;

let contract_id = Contract::load_from(
"../../packages/fuels/tests/contracts/contract_test/out/debug/contract_test.bin",
Expand Down Expand Up @@ -345,7 +337,7 @@ mod tests {
abi = "packages/fuels/tests/contracts/token_ops/out/debug/token_ops-abi.json"
));

let wallet = launch_provider_and_get_wallet().await;
let wallet = launch_provider_and_get_wallet().await?;

let contract_id = Contract::load_from(
"../../packages/fuels/tests/contracts/token_ops/out/debug/token_ops\
Expand Down Expand Up @@ -385,7 +377,7 @@ mod tests {
abi="packages/fuels/tests/contracts/lib_contract_caller/out/debug/lib_contract_caller-abi.json"
));

let wallet = launch_provider_and_get_wallet().await;
let wallet = launch_provider_and_get_wallet().await?;

let called_contract_id: ContractId = Contract::load_from(
"../../packages/fuels/tests/contracts/lib_contract/out/debug/lib_contract.bin",
Expand Down Expand Up @@ -456,7 +448,7 @@ mod tests {
abi =
"packages/fuels/tests/contracts/contract_test/out/debug/contract_test-abi.json"
));
let wallet = launch_provider_and_get_wallet().await;
let wallet = launch_provider_and_get_wallet().await?;

let contract_id = Contract::load_from(
"../../packages/fuels/tests/contracts/contract_test/out/debug/contract_test.bin",
Expand Down Expand Up @@ -501,7 +493,7 @@ mod tests {
abi =
"packages/fuels/tests/contracts/contract_test/out/debug/contract_test-abi.json"
));
let wallet_original = launch_provider_and_get_wallet().await;
let wallet_original = launch_provider_and_get_wallet().await?;

let wallet = wallet_original.clone();
// Your bech32m encoded contract ID.
Expand Down Expand Up @@ -536,7 +528,7 @@ mod tests {
abi = "packages/fuels/tests/contracts/contract_test/out/debug/contract_test-abi.json"
));

let wallet = launch_provider_and_get_wallet().await;
let wallet = launch_provider_and_get_wallet().await?;

let contract_id = Contract::load_from(
"../../packages/fuels/tests/contracts/contract_test/out/debug/contract_test.bin",
Expand Down Expand Up @@ -574,7 +566,7 @@ mod tests {
abi = "packages/fuels/tests/contracts/contract_test/out/debug/contract_test-abi.json"
));

let wallet = launch_provider_and_get_wallet().await;
let wallet = launch_provider_and_get_wallet().await?;

let contract_id = Contract::load_from(
"../../packages/fuels/tests/contracts/contract_test/out/debug/contract_test.bin",
Expand Down Expand Up @@ -630,7 +622,7 @@ mod tests {
abi = "packages/fuels/tests/contracts/contract_test/out/debug/contract_test-abi.json"
));

let wallet = launch_provider_and_get_wallet().await;
let wallet = launch_provider_and_get_wallet().await?;

let contract_id = Contract::load_from(
"../../packages/fuels/tests/contracts/contract_test/out/debug/contract_test.bin",
Expand Down Expand Up @@ -672,7 +664,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, 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
14 changes: 6 additions & 8 deletions examples/cookbook/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,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, 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 @@ -91,7 +91,7 @@ mod tests {
#[tokio::test]
async fn custom_chain() -> Result<()> {
// ANCHOR: custom_chain_import
use fuels::{fuel_node::ChainConfig, prelude::*, tx::ConsensusParameters};
use fuels::{prelude::*, tx::ConsensusParameters};
// ANCHOR_END: custom_chain_import

// ANCHOR: custom_chain_consensus
Expand All @@ -118,7 +118,7 @@ mod tests {
// ANCHOR: custom_chain_provider
let node_config = Config::local_node();
let _provider =
setup_test_provider(coins, vec![], Some(node_config), Some(chain_config)).await;
setup_test_provider(coins, vec![], Some(node_config), Some(chain_config)).await?;
// ANCHOR_END: custom_chain_provider
Ok(())
}
Expand All @@ -138,7 +138,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, 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 Expand Up @@ -195,17 +195,15 @@ mod tests {
use std::path::PathBuf;

use fuels::prelude::*;

// ANCHOR: create_or_use_rocksdb
let provider_config = Config {
database_path: PathBuf::from("/tmp/.spider/db"),
database_type: DbType::RocksDb,
database_type: DbType::RocksDb(Some(PathBuf::from("/tmp/.spider/db"))),
..Config::local_node()
};
// ANCHOR_END: create_or_use_rocksdb

launch_custom_provider_and_get_wallets(Default::default(), Some(provider_config), None)
.await;
.await?;

Ok(())
}
Expand Down
4 changes: 2 additions & 2 deletions examples/predicates/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ mod tests {
})
.collect::<Vec<_>>();

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

[&mut wallet, &mut wallet2, &mut wallet3, &mut receiver]
.iter_mut()
Expand Down Expand Up @@ -138,7 +138,7 @@ mod tests {
}],
);

let wallets = &launch_custom_provider_and_get_wallets(wallets_config, None, None).await;
let wallets = &launch_custom_provider_and_get_wallets(wallets_config, None, None).await?;

let first_wallet = &wallets[0];
let second_wallet = &wallets[1];
Expand Down
7 changes: 4 additions & 3 deletions examples/providers/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ mod tests {
use fuels::prelude::Result;

#[tokio::test]
async fn connect_to_fuel_node() {
async fn connect_to_fuel_node() -> Result<()> {
// ANCHOR: connect_to_testnet
use std::str::FromStr;

Expand All @@ -28,14 +28,15 @@ mod tests {
dbg!(wallet.address().to_string());
// ANCHOR_END: connect_to_testnet

let provider = setup_test_provider(vec![], vec![], None, None).await;
let provider = setup_test_provider(vec![], vec![], None, None).await?;
let port = provider.url().split(':').last().unwrap();

// ANCHOR: local_node_address
let _provider = Provider::connect(format!("127.0.0.1:{port}"))
.await
.unwrap();
// ANCHOR_END: local_node_address
Ok(())
}

#[tokio::test]
Expand Down Expand Up @@ -66,7 +67,7 @@ mod tests {
// ANCHOR: configure_retry
let retry_config = RetryConfig::new(3, Backoff::Fixed(Duration::from_secs(2)))?;
let provider = setup_test_provider(coins.clone(), vec![], None, None)
.await
.await?
.with_retry_config(retry_config);
// ANCHOR_END: configure_retry
// ANCHOR_END: setup_test_blockchain
Expand Down
2 changes: 1 addition & 1 deletion examples/rust_bindings/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ mod tests {
#[allow(unused_variables)]
async fn transform_json_to_bindings() -> Result<()> {
use fuels::test_helpers::launch_provider_and_get_wallet;
let wallet = launch_provider_and_get_wallet().await;
let wallet = launch_provider_and_get_wallet().await?;
{
// ANCHOR: use_abigen
use fuels::prelude::*;
Expand Down
Loading

0 comments on commit d416a49

Please sign in to comment.