Skip to content

Commit

Permalink
add routing integration test
Browse files Browse the repository at this point in the history
  • Loading branch information
kayagokalp committed Jul 3, 2024
1 parent 1ac2803 commit ad73213
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 19 deletions.
5 changes: 5 additions & 0 deletions forc-plugins/forc-client/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,8 @@ pub const DEVNET_FAUCET_URL: &str = "https://faucet-devnet.fuel.network";
pub const DEVNET_ENDPOINT_URL: &str = "https://devnet.fuel.network";
pub const TESTNET_FAUCET_URL: &str = "https://faucet-testnet.fuel.network";
pub const TESTNET_ENDPOINT_URL: &str = "https://testnet.fuel.network";
/// Default PrivateKey to sign transactions submitted to local node.
pub const DEFAULT_PRIVATE_KEY: &str =
"0xde97d8624a438121b86a1956544bd72ed68cd69f2c99555b08b1e8c51ffd511c";
/// The maximum time to wait for a transaction to be included in a block by the node
pub const TX_SUBMIT_TIMEOUT_MS: u64 = 30_000u64;
2 changes: 1 addition & 1 deletion forc-plugins/forc-client/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
pub mod cmd;
mod constants;
pub mod constants;
pub mod op;
mod util;

Expand Down
3 changes: 2 additions & 1 deletion forc-plugins/forc-client/src/op/deploy.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use crate::{
cmd,
constants::TX_SUBMIT_TIMEOUT_MS,
util::{
node_url::get_node_url,
pkg::{build_proxy_contract, built_pkgs, update_proxy_address_in_manifest},
tx::{
bech32_from_secret, check_and_create_wallet_at_default_path, first_user_account,
prompt_forc_wallet_password, select_manual_secret_key, select_secret_key,
update_proxy_contract_target, WalletSelectionMode, TX_SUBMIT_TIMEOUT_MS,
update_proxy_contract_target, WalletSelectionMode,
},
},
};
Expand Down
6 changes: 2 additions & 4 deletions forc-plugins/forc-client/src/op/run/mod.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
mod encode;
use crate::{
cmd,
constants::TX_SUBMIT_TIMEOUT_MS,
util::{
gas::get_script_gas_used,
node_url::get_node_url,
pkg::built_pkgs,
tx::{
prompt_forc_wallet_password, TransactionBuilderExt, WalletSelectionMode,
TX_SUBMIT_TIMEOUT_MS,
},
tx::{prompt_forc_wallet_password, TransactionBuilderExt, WalletSelectionMode},
},
};
use anyhow::{anyhow, bail, Context, Result};
Expand Down
9 changes: 1 addition & 8 deletions forc-plugins/forc-client/src/util/tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,7 @@ use forc_wallet::{
};
use tracing::info;

use crate::util::target::Target;

/// The maximum time to wait for a transaction to be included in a block by the node
pub const TX_SUBMIT_TIMEOUT_MS: u64 = 30_000u64;

/// Default PrivateKey to sign transactions submitted to local node.
pub const DEFAULT_PRIVATE_KEY: &str =
"0xde97d8624a438121b86a1956544bd72ed68cd69f2c99555b08b1e8c51ffd511c";
use crate::{constants::DEFAULT_PRIVATE_KEY, util::target::Target};

#[derive(PartialEq, Eq)]
pub enum WalletSelectionMode {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ license = "Apache-2.0"
name = "standalone_contract"

[dependencies]
std = { path = "../../../../../../../../sway-lib-std/" }
std = { path = "../../../../../sway-lib-std/" }
69 changes: 65 additions & 4 deletions forc-plugins/forc-client/tests/deploy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ use forc_client::{
NodeTarget,
};
use forc_pkg::manifest::Proxy;
use fuel_crypto::SecretKey;
use fuel_tx::{ContractId, Salt};
use fuels::macros::abigen;
use fuels_accounts::{provider::Provider, wallet::WalletUnlocked};
use portpicker::Port;
use tempfile::tempdir;
use toml_edit::{value, Document, InlineTable, Item, Table, Value};
Expand All @@ -34,11 +37,10 @@ fn chain_config_path() -> PathBuf {
.join("local-testnode")
}

fn test_pkg_path() -> PathBuf {
fn test_data_path() -> PathBuf {
PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.join("test")
.join("data")
.join("standalone_contract")
.canonicalize()
.unwrap()
}
Expand Down Expand Up @@ -115,7 +117,7 @@ fn patch_manifest_file_with_proxy_table(manifest_dir: &Path, proxy: Proxy) -> an
async fn simple_deploy() {
let (mut node, port) = run_node();
let tmp_dir = tempdir().unwrap();
let project_dir = test_pkg_path();
let project_dir = test_data_path().join("standalone_contract");
copy_dir(&project_dir, tmp_dir.path()).unwrap();
patch_manifest_file_with_path_std(tmp_dir.path()).unwrap();

Expand Down Expand Up @@ -153,7 +155,7 @@ async fn simple_deploy() {
async fn deploy_fresh_proxy() {
let (mut node, port) = run_node();
let tmp_dir = tempdir().unwrap();
let project_dir = test_pkg_path();
let project_dir = test_data_path().join("standalone_contract");
copy_dir(&project_dir, tmp_dir.path()).unwrap();
patch_manifest_file_with_path_std(tmp_dir.path()).unwrap();
let proxy = Proxy {
Expand Down Expand Up @@ -200,3 +202,62 @@ async fn deploy_fresh_proxy() {

assert_eq!(contract_ids, expected)
}

#[tokio::test]
async fn proxy_contract_re_routes_call() {
let (mut node, port) = run_node();
let tmp_dir = tempdir().unwrap();
let project_dir = test_data_path().join("standalone_contract");
copy_dir(&project_dir, tmp_dir.path()).unwrap();
patch_manifest_file_with_path_std(tmp_dir.path()).unwrap();
let proxy = Proxy {
enabled: true,
address: None,
};
patch_manifest_file_with_proxy_table(tmp_dir.path(), proxy).unwrap();

let pkg = Pkg {
path: Some(tmp_dir.path().display().to_string()),
..Default::default()
};

let node_url = format!("http://127.0.0.1:{}/v1/graphql", port);
let target = NodeTarget {
node_url: Some(node_url.clone()),
target: None,
testnet: false,
};
let cmd = cmd::Deploy {
pkg,
salt: Some(vec![format!("{}", Salt::default())]),
node: target,
default_signer: true,
..Default::default()
};
let contract_ids = deploy(cmd).await.unwrap();
// At this point we deployed a contract with proxy. Proxy address is the
// first contract id returned.
let proxy_contract = contract_ids[0].id;
let impl_contract_id = contract_ids[1].id;
// Make a contract call into proxy contract, and check if the initial
// contract returns a true.
let provider = Provider::connect(node_url).await.unwrap();
let secret_key = SecretKey::from_str(forc_client::constants::DEFAULT_PRIVATE_KEY).unwrap();
let wallet_unlocked = WalletUnlocked::new_from_private_key(secret_key, Some(provider));

abigen!(Contract(
name = "ImplementationContract",
abi = "forc-plugins/forc-client/test/data/standalone_contract/out/debug/standalone_contract-abi.json"
));

let impl_contract_a = ImplementationContract::new(proxy_contract, wallet_unlocked);
let res = impl_contract_a
.methods()
.test_function()
.with_contract_ids(&[impl_contract_id.into()])
.call()
.await
.unwrap();
node.kill().unwrap();
assert_eq!(res.value, true)
}

0 comments on commit ad73213

Please sign in to comment.