Skip to content

Commit

Permalink
Update non owner test to use update proxy method
Browse files Browse the repository at this point in the history
  • Loading branch information
sdankel committed Jul 25, 2024
1 parent 38b3d43 commit 17b2bd7
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 37 deletions.
2 changes: 1 addition & 1 deletion forc-plugins/forc-client/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
pub mod cmd;
pub mod constants;
pub mod op;
mod util;
pub mod util;

use clap::Parser;
use serde::{Deserialize, Serialize};
Expand Down
2 changes: 1 addition & 1 deletion forc-plugins/forc-client/src/util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ pub(crate) mod gas;
pub(crate) mod node_url;
pub(crate) mod pkg;
pub(crate) mod target;
pub(crate) mod tx;
pub mod tx;
10 changes: 5 additions & 5 deletions forc-plugins/forc-client/src/util/tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use fuel_crypto::{Message, PublicKey, SecretKey, Signature};
use fuel_tx::{
field, Address, AssetId, Buildable, ContractId, Input, Output, TransactionBuilder, Witness,
};
use fuels::macros::abigen;
use fuels::{macros::abigen, programs::responses::CallResponse};
use fuels_accounts::{
provider::Provider,
wallet::{Wallet, WalletUnlocked},
Expand Down Expand Up @@ -256,12 +256,12 @@ pub(crate) async fn select_secret_key(
Ok(signing_key)
}

pub(crate) async fn update_proxy_contract_target(
pub async fn update_proxy_contract_target(
provider: &Provider,
secret_key: SecretKey,
proxy_contract_id: ContractId,
new_target: ContractId,
) -> Result<()> {
) -> Result<CallResponse<()>> {
abigen!(Contract(
name = "ProxyContract",
abi = "forc-plugins/forc-client/abi/proxy_contract-abi.json"
Expand All @@ -272,7 +272,7 @@ pub(crate) async fn update_proxy_contract_target(
let proxy_contract = ProxyContract::new(proxy_contract_id, wallet);

// TODO: what happens if the call fails? Does 'FuelCallResponse' is returned as Err() in that case?
proxy_contract
let result = proxy_contract
.methods()
.set_proxy_target(new_target)
.call()
Expand All @@ -281,7 +281,7 @@ pub(crate) async fn update_proxy_contract_target(
"Updated",
&format!("proxy contract target to 0x{new_target}"),
);
Ok(())
Ok(result)
}

#[async_trait]
Expand Down
54 changes: 24 additions & 30 deletions forc-plugins/forc-client/tests/deploy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use forc::cli::shared::Pkg;
use forc_client::{
cmd,
op::{deploy, DeployedContract},
util::tx::update_proxy_contract_target,
NodeTarget,
};
use forc_pkg::manifest::Proxy;
Expand Down Expand Up @@ -326,16 +327,19 @@ async fn test_non_owner_fails_to_set_target() {
// Proxy contract's id.
let proxy_id = contract_id.first().and_then(|f| f.proxy).unwrap();

// create an another account and fund it.
// Create and fund an owner account and an attacker account.
let provider = Provider::connect(&node_url).await.unwrap();
let secret_key = SecretKey::random(&mut thread_rng());
let attacker_wallet = WalletUnlocked::new_from_private_key(secret_key, Some(provider.clone()));

let secret_key = SecretKey::from_str(forc_client::constants::DEFAULT_PRIVATE_KEY).unwrap();
let owner_wallet = WalletUnlocked::new_from_private_key(secret_key, Some(provider.clone()));
let attacker_secret_key = SecretKey::random(&mut thread_rng());
let attacker_wallet =
WalletUnlocked::new_from_private_key(attacker_secret_key, Some(provider.clone()));

let owner_secret_key =
SecretKey::from_str(forc_client::constants::DEFAULT_PRIVATE_KEY).unwrap();
let owner_wallet =
WalletUnlocked::new_from_private_key(owner_secret_key, Some(provider.clone()));
let base_asset_id = provider.base_asset_id();

// fund attacker wallet so that it can try to make a set proxy target call.
// Fund attacker wallet so that it can try to make a set proxy target call.
owner_wallet
.transfer(
attacker_wallet.address(),
Expand All @@ -352,31 +356,21 @@ async fn test_non_owner_fails_to_set_target() {
abi = "forc-plugins/forc-client/abi/proxy_contract-abi.json"
));

let proxy_contract = ProxyContract::new(proxy_id, attacker_wallet);
// try to change target of the proxy with a random wallet which is not the
// owner of the proxy.
let res = proxy_contract
.methods()
.set_proxy_target(dummy_contract_id_target)
.call()
.await
.err()
.unwrap();
// Try to change target of the proxy with a random wallet which is not the owner of the proxy.
let res = update_proxy_contract_target(
&provider,
attacker_secret_key,
proxy_id,
dummy_contract_id_target,
)
.await
.err()
.unwrap();

node.kill().unwrap();
match res {
fuels::types::errors::Error::Transaction(
fuels::types::errors::transaction::Reason::Reverted { reason, .. },
) => {
assert_eq!(
reason,
"NotOwner".to_string(),
"Expected 'NotOwner' error, but got: {}",
reason
);
}
_ => panic!("Expected a Reverted transaction error, but got: {:?}", res),
}
assert!(res
.to_string()
.starts_with("transaction reverted: NotOwner"));
}

// TODO: https://github.com/FuelLabs/sway/issues/6283
Expand Down

0 comments on commit 17b2bd7

Please sign in to comment.