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: add option to run against live node #1172

Closed
wants to merge 30 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
8f1e057
test: add option to run against live node
iqdecay Oct 24, 2023
7eaca6d
ci: test ci run
iqdecay Oct 24, 2023
14d3622
add `continue-on-error`
iqdecay Oct 24, 2023
1d2f429
fix ci step
iqdecay Oct 24, 2023
f583ca8
use correct ci
iqdecay Oct 24, 2023
c6c5817
style: appease clippy and all formatting gods
iqdecay Oct 24, 2023
e972638
run as matrix command
iqdecay Oct 24, 2023
009a817
dl sway artifacts
iqdecay Oct 24, 2023
d855386
move step outside matrix
iqdecay Oct 24, 2023
27e6214
Revert "move step outside matrix"
iqdecay Oct 25, 2023
d1d09d1
setup test projects before
iqdecay Oct 25, 2023
1b59973
Revert "move step outside matrix"
iqdecay Oct 25, 2023
be10828
add commented condition on push
iqdecay Oct 25, 2023
66c450e
fix with review comments
iqdecay Oct 26, 2023
2410a45
PR comments
hal3e Oct 25, 2023
4000715
fix ci file formatting
iqdecay Oct 26, 2023
5e6c02e
move the step up
iqdecay Oct 26, 2023
3959fc1
ci: use new secrets name
iqdecay Oct 26, 2023
1451f9a
ci: live node tests on their own job
iqdecay Oct 27, 2023
8166869
fix typo
iqdecay Oct 27, 2023
9d3f56e
don't wait for cargo verificationsgq
iqdecay Oct 27, 2023
6482f45
add continue on error
iqdecay Oct 27, 2023
0cca920
move continue on error
iqdecay Oct 30, 2023
41f15bd
feat: implement suggestion
iqdecay Nov 15, 2023
bf6b63b
remove RunOnLiveNode command
iqdecay Nov 15, 2023
da4f61d
Revert "PR comments"
iqdecay Nov 15, 2023
2530269
add comment
iqdecay Nov 15, 2023
67f8054
fmt
iqdecay Nov 15, 2023
2d6b35c
remove artifacts
iqdecay Nov 15, 2023
ecd0f83
remove the CI step
iqdecay Nov 15, 2023
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
5 changes: 4 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ env:
FORC_VERSION: 0.46.0
FORC_PATCH_BRANCH: "xunilrj/fix-implicit-std-env-vars"
FORC_PATCH_REVISION: ""
FORC_IMPLICIT_STD_GIT_BRANCH: "xunilrj/fix-implicit-std-env-vars"
FORC_IMPLICIT_STD_GIT_BRANCH: "xunilrj/fix-implicit-std-env-vars"
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
2 changes: 1 addition & 1 deletion examples/providers/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,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
2 changes: 2 additions & 0 deletions packages/fuels-core/src/utils/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ pub const BASE_ASSET_ID: AssetId = AssetId::BASE;
// ANCHOR_END: default_call_parameters

pub const DEFAULT_GAS_ESTIMATION_TOLERANCE: f64 = 0.2;

pub const TESTNET_NODE_URL: &str = "beta-4.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 @@ -34,3 +34,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 = []
45 changes: 44 additions & 1 deletion packages/fuels-test-helpers/src/accounts.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
use std::mem::size_of;
use std::str::FromStr;

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

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

pub async fn connect_to_testnet_node_and_get_wallets(
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}");
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 fuels_accounts::{fuel_crypto::fuel_types::AssetId, Account, ViewOnlyAccount};
Expand Down
1 change: 1 addition & 0 deletions packages/fuels/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,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"]
10 changes: 8 additions & 2 deletions packages/fuels/tests/contracts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,11 @@ async fn test_multiple_args() -> Result<()> {
#[tokio::test]
async fn test_contract_calling_contract() -> Result<()> {
// Tests a contract call that calls another contract (FooCaller calls FooContract underneath)
let [wallet]: [WalletUnlocked; 1] = maybe_live_wallet(1)
.await?
.try_into()
.expect("Vec can be converted to an array");
setup_program_test!(
Wallets("wallet"),
Abigen(
Contract(
name = "LibContract",
Expand Down Expand Up @@ -1502,8 +1505,11 @@ async fn can_configure_decoding_of_contract_return() -> Result<()> {

#[tokio::test]
async fn test_contract_submit_and_response() -> Result<()> {
let [wallet]: [WalletUnlocked; 1] = maybe_live_wallet(1)
.await?
.try_into()
.expect("Vec can be converted to an array");
setup_program_test!(
Wallets("wallet"),
Abigen(Contract(
name = "TestContract",
project = "packages/fuels/tests/contracts/contract_test"
Expand Down
12 changes: 7 additions & 5 deletions packages/fuels/tests/providers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ use fuels::{
tx::Receipt,
types::{block::Block, coin_type::CoinType, message::Message},
};

use fuels_core::types::{
transaction_builders::{ScriptTransactionBuilder, TransactionBuilder},
Bits256,
use fuels_core::{
constants::TESTNET_NODE_URL,
types::{
transaction_builders::{ScriptTransactionBuilder, TransactionBuilder},
Bits256,
},
};

#[tokio::test]
Expand Down Expand Up @@ -634,7 +636,7 @@ async fn testnet_hello_world() -> Result<()> {
));

// Create a provider pointing to the testnet.
let provider = Provider::connect("beta-4.fuel.network").await.unwrap();
let provider = Provider::connect(TESTNET_NODE_URL).await.unwrap();

// Setup the private key.
let secret =
Expand Down
10 changes: 8 additions & 2 deletions packages/fuels/tests/scripts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,11 @@ async fn test_transaction_script_workflow() -> Result<()> {

#[tokio::test]
async fn test_multi_call_script_workflow() -> Result<()> {
let [wallet]: [WalletUnlocked; 1] = maybe_live_wallet(1)
.await?
.try_into()
.expect("Vec can be converted to an array");
setup_program_test!(
Wallets("wallet"),
Abigen(Contract(
name = "TestContract",
project = "packages/fuels/tests/contracts/contract_test"
Expand Down Expand Up @@ -404,8 +407,11 @@ async fn can_configure_decoder_on_script_call() -> Result<()> {

#[tokio::test]
async fn test_script_submit_and_response() -> Result<()> {
let [wallet]: [WalletUnlocked; 1] = maybe_live_wallet(1)
.await?
.try_into()
.expect("Vec can be converted to an array");
setup_program_test!(
Wallets("wallet"),
Abigen(Script(
name = "MyScript",
project = "packages/fuels/tests/scripts/script_struct"
Expand Down
Loading