Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: enable testing against a live node #1257

Closed
wants to merge 19 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ on:
- master
pull_request:
release:
types: [published]
types: [ published ]

concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
Expand All @@ -22,6 +22,9 @@ env:
FORC_VERSION: 0.49.1
FORC_PATCH_BRANCH: ""
FORC_PATCH_REVISION: ""
TEST_WALLET_SECRET_KEY_1: ${{ secrets.TEST_WALLET_SECRET_KEY_1 }}
TEST_WALLET_SECRET_KEY_2: ${{ secrets.TEST_WALLET_SECRET_KEY_2 }}
TEST_WALLET_SECRET_KEY_3: ${{ secrets.TEST_WALLET_SECRET_KEY_3 }}

jobs:
setup-test-projects:
Expand Down Expand Up @@ -152,8 +155,8 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
cargo_command: [check]
args: [--all-features]
cargo_command: [ check ]
args: [ --all-features ]
package: ${{fromJSON(needs.get-workspace-members.outputs.members)}}
include:
- cargo_command: fmt
Expand All @@ -172,6 +175,13 @@ jobs:
args: run --all-targets --workspace
download_sway_artifacts: sway-examples
install_fuel_core: true
# test some functions against live nodes
- cargo_command: nextest
# the live node features requires using one thread only to avoid transaction collision
args: run --all-targets --features "test-against-live-node" -j 1 --workspace
download_sway_artifacts: sway-examples
# not all tests use the live nodes so fuel-core binary remains necessary
install_fuel_core: true
- cargo_command: test
args: --doc --workspace
- cargo_command: machete
Expand Down
2 changes: 1 addition & 1 deletion docs/src/connecting/external-node.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ In the code example, we connected a new provider to the Testnet node and created

> **Note:** New wallets on the Testnet will not have any assets! They can be obtained by providing the wallet address to the faucet at
>
>[faucet-beta-4.fuel.network](https://faucet-beta-4.fuel.network)
>[faucet-beta-5.fuel.network](https://faucet-beta-5.fuel.network)
>
> Once the assets have been transferred to the wallet, you can reuse it in other tests by providing the private key!
>
Expand Down
3 changes: 1 addition & 2 deletions examples/providers/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ mod tests {
use fuels::prelude::Result;

#[tokio::test]
#[ignore] //TODO: Enable this test once beta supports the new `fuel-core` >= `0.21.0`
async fn connect_to_fuel_node() -> Result<()> {
// ANCHOR: connect_to_testnet
use std::str::FromStr;
Expand All @@ -15,7 +14,7 @@ mod tests {
// Create a provider pointing to the testnet.
// This example will not work as the testnet does not support the new version of fuel-core
// yet
let provider = Provider::connect("beta-4.fuel.network").await.unwrap();
let provider = Provider::connect(TESTNET_NODE_URL).await.unwrap();

// Setup a private key
let secret =
Expand Down
1 change: 1 addition & 0 deletions packages/fuels-core/src/utils/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ pub const DEFAULT_GAS_ESTIMATION_TOLERANCE: f64 = 0.2;
pub const WITNESS_STATIC_SIZE: usize = 8;
const SIGNATURE_SIZE: usize = 64;
pub const SIGNATURE_WITNESS_SIZE: usize = WITNESS_STATIC_SIZE + SIGNATURE_SIZE;
pub const TESTNET_NODE_URL: &str = "beta-5.fuel.network";
17 changes: 9 additions & 8 deletions packages/fuels-macros/src/setup_program_test/code_gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub(crate) fn generate_setup_program_test_code(

let project_lookup = generate_project_lookup(&generate_bindings)?;
let abigen_code = abigen_code(&project_lookup);
let wallet_code = wallet_initialization_code(initialize_wallets);
let wallet_code = wallet_initialization_code(initialize_wallets)?;
let deploy_code = contract_deploying_code(&deploy_contract, &project_lookup);
let script_code = script_loading_code(&load_scripts, &project_lookup);

Expand Down Expand Up @@ -66,21 +66,22 @@ fn generate_abigen_targets(project_lookup: &HashMap<String, Project>) -> Vec<Abi
.collect()
}

fn wallet_initialization_code(maybe_command: Option<InitializeWalletCommand>) -> TokenStream {
fn wallet_initialization_code(
maybe_command: Option<InitializeWalletCommand>,
) -> syn::Result<TokenStream> {
let command = if let Some(command) = maybe_command {
command
} else {
return Default::default();
return Ok(Default::default());
};

let wallet_names = extract_wallet_names(&command);

if wallet_names.is_empty() {
return Default::default();
return Ok(Default::default());
}

let num_wallets = wallet_names.len();
quote! {

Ok(quote! {
let [#(#wallet_names),*]: [_; #num_wallets] = launch_custom_provider_and_get_wallets(
WalletsConfig::new(Some(#num_wallets as u64), None, None),
None,
Expand All @@ -90,7 +91,7 @@ fn wallet_initialization_code(maybe_command: Option<InitializeWalletCommand>) ->
.expect("Error while trying to fetch wallets from the custom provider")
.try_into()
.expect("Should have the exact number of wallets");
}
})
}

fn extract_wallet_names(command: &InitializeWalletCommand) -> Vec<Ident> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ command_parser!(
Wallets -> InitializeWalletCommand,
Abigen -> AbigenCommand,
Deploy -> DeployContractCommand,
LoadScript -> LoadScriptCommand
LoadScript -> LoadScriptCommand,
);

impl Parse for TestProgramCommands {
Expand Down
1 change: 1 addition & 0 deletions packages/fuels-test-helpers/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,4 @@ which = { workspace = true, default-features = false }
default = ["fuels-accounts", "std"]
std = ["fuels-accounts?/std", "fuels-core/std"]
fuel-core-lib = ["fuel-core"]
test-against-live-node = []
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather we don't introduce a feature if we don't have to. Since the test code compiles always (unlike the case with full sway type paths tests) I'd rather we make the decision to target the test net based on an env variable:

if env!(TARGET_TEST_NET) ... 

This way our internal testing approach doesn't leak into user features.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I went this direction because that was what transpired in the previous discussion here. Happy to change it though.

45 changes: 42 additions & 3 deletions packages/fuels-test-helpers/src/accounts.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use fuel_crypto::SecretKey;
use std::mem::size_of;
use std::str::FromStr;

use fuel_crypto::SecretKey;
use fuels_accounts::wallet::WalletUnlocked;
use fuels_core::types::errors::Result;
use fuels_accounts::{provider::Provider, wallet::WalletUnlocked};
use fuels_core::{constants::TESTNET_NODE_URL, error, types::errors::Result};

use crate::{
node_types::{ChainConfig, Config},
Expand Down Expand Up @@ -85,6 +86,44 @@ pub async fn launch_custom_provider_and_get_wallets(
Ok(wallets)
}

pub async fn connect_to_testnet_node_and_get_wallets(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wouldn't put these in the test helpers crate. The test helpers contain stuff we deem to be useful to all users. This is more of our internal desire to test the testnet. Why limit our users to stuff like:

  • no more than 3 wallets
  • the test net must be the official one from fuel (URL hardcoded)
  • the env variables must be strictly the ones we set here (TEST_WALLET_SECRET_KEY_*)
  • etc

I'd rather we move this helper to our e2e tests and have it be private there since they are the only reason this exists.

num_wallets: usize,
) -> Result<Vec<WalletUnlocked>> {
if num_wallets > 3 {
error!(
InvalidData,
"Trying to get more than 3 wallets from beta node"
);
}
let provider = Provider::connect(TESTNET_NODE_URL)
.await
.expect("Should be able to connect to {TESTNET_NODE_URL}");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suspect you wanted the value to be interpolated here:

Suggested change
.expect("Should be able to connect to {TESTNET_NODE_URL}");
.unwrap_or_else(|_| panic!("Should be able to connect to {TESTNET_NODE_URL}"));

Note

why not expect(&format!(...))? See this clippy lint

let wallets = (1..=num_wallets)
.map(|wallet_counter| {
let private_key_string =
std::env::var(format!("TEST_WALLET_SECRET_KEY_{wallet_counter}"))
.expect("Should find private key in ENV");
let private_key = SecretKey::from_str(private_key_string.as_str())
.expect("Should be able to transform into private key");
WalletUnlocked::new_from_private_key(private_key, Some(provider.clone()))
})
.collect::<Vec<WalletUnlocked>>();
Ok(wallets)
}

pub async fn maybe_live_wallet(num_wallets: usize) -> Result<Vec<WalletUnlocked>> {
if cfg!(feature = "test-against-live-node") {
connect_to_testnet_node_and_get_wallets(num_wallets).await
} else {
launch_custom_provider_and_get_wallets(
WalletsConfig::new(Some(num_wallets as u64), None, None),
None,
None,
)
.await
}
}

#[cfg(test)]
mod tests {
use fuel_core_chain_config::ChainConfig;
Expand Down
1 change: 1 addition & 0 deletions packages/fuels/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,6 @@ std = [
]
# TODO: To be removed once https://github.com/FuelLabs/fuels-rs/issues/881 is unblocked.
test-type-paths = []
test-against-live-node = []
fuel-core-lib = ["fuels-test-helpers?/fuel-core-lib", "dep:fuel-core"]
rocksdb = ["fuel-core?/rocksdb"]
Loading
Loading