Skip to content

Commit

Permalink
fix: linter
Browse files Browse the repository at this point in the history
  • Loading branch information
alenmestrov committed Dec 1, 2024
1 parent 53986b4 commit cf5a494
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 40 deletions.
2 changes: 1 addition & 1 deletion contracts/icp/context-config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ use crate::types::{
pub mod guard;
pub mod mutate;
pub mod query;
pub mod types;
pub mod sys;
pub mod types;

#[derive(CandidType, Serialize, Deserialize, Clone, Debug)]
pub struct Context {
Expand Down
49 changes: 24 additions & 25 deletions contracts/icp/context-config/src/mutate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ use std::ops::Deref;

use calimero_context_config::repr::{ReprBytes, ReprTransmute};
use candid::Principal;
use ic_cdk::api::management_canister::main::{
create_canister, install_code, CanisterSettings, CreateCanisterArgument, InstallCodeArgument,
};

use crate::guard::Guard;
use crate::types::{
Expand All @@ -10,10 +13,6 @@ use crate::types::{
};
use crate::{Context, CONTEXT_CONFIGS};

use ic_cdk::api::management_canister::main::{
create_canister, install_code, CanisterSettings, CreateCanisterArgument, InstallCodeArgument
};

#[ic_cdk::update]
pub async fn mutate(signed_request: ICPSigned<Request>) -> Result<(), String> {
let request = signed_request
Expand Down Expand Up @@ -65,7 +64,8 @@ async fn add_context(
return Err("context addition must be signed by the context itself".into());
}

let proxy_canister_id = deploy_proxy_contract(&context_id).await
let proxy_canister_id = deploy_proxy_contract(&context_id)
.await
.unwrap_or_else(|e| panic!("Failed to deploy proxy contract: {}", e));

CONTEXT_CONFIGS.with(|configs| {
Expand Down Expand Up @@ -93,13 +93,11 @@ async fn add_context(
})
}

async fn deploy_proxy_contract(
context_id: &ICContextId,
) -> Result<String, String> {
async fn deploy_proxy_contract(context_id: &ICContextId) -> Result<String, String> {
// Get the proxy code
let proxy_code = CONTEXT_CONFIGS.with(|configs| {
configs.borrow().proxy_code.clone()
}).ok_or("proxy code not set")?;
let proxy_code = CONTEXT_CONFIGS
.with(|configs| configs.borrow().proxy_code.clone())
.ok_or("proxy code not set")?;

// Create canister with cycles
let create_args = CreateCanisterArgument {
Expand All @@ -113,19 +111,19 @@ async fn deploy_proxy_contract(
wasm_memory_limit: None,
}),
};

let (canister_record,) = ic_cdk::api::management_canister::main::create_canister(
create_args,
500_000_000_000_000u128
).await.map_err(|e| format!("Failed to create canister: {:?}", e))?;
create_args,
500_000_000_000_000u128,
)
.await
.map_err(|e| format!("Failed to create canister: {:?}", e))?;

let canister_id = canister_record.canister_id;

// Encode init args matching the proxy's init(context_id: ICContextId, ledger_id: Principal)
let init_args = candid::encode_args((
context_id.clone(),
Principal::anonymous(),
)).map_err(|e| format!("Failed to encode init args: {}", e))?;
let init_args = candid::encode_args((context_id.clone(), Principal::anonymous()))
.map_err(|e| format!("Failed to encode init args: {}", e))?;

let install_args = InstallCodeArgument {
mode: ic_cdk::api::management_canister::main::CanisterInstallMode::Install,
Expand Down Expand Up @@ -341,7 +339,9 @@ async fn update_proxy_contract(
) -> Result<(), String> {
let mut context = CONTEXT_CONFIGS.with(|configs| {
let configs = configs.borrow();
configs.contexts.get(&context_id)
configs
.contexts
.get(&context_id)
.ok_or_else(|| "context does not exist".to_string())
.cloned()
})?;
Expand All @@ -355,18 +355,17 @@ async fn update_proxy_contract(
.clone();

// Get the proxy code
let proxy_code = CONTEXT_CONFIGS.with(|configs| {
configs.borrow().proxy_code.clone()
}).ok_or("proxy code not set")?;
let proxy_code = CONTEXT_CONFIGS
.with(|configs| configs.borrow().proxy_code.clone())
.ok_or("proxy code not set")?;

// Update the proxy contract code
let install_args = InstallCodeArgument {
mode: ic_cdk::api::management_canister::main::CanisterInstallMode::Upgrade(None),
canister_id: Principal::from_text(proxy_canister_id)
.map_err(|e| format!("Invalid canister ID: {}", e))?,
wasm_module: proxy_code,
arg: candid::encode_one(&context_id)
.map_err(|e| format!("Encoding error: {}", e))?,
arg: candid::encode_one(&context_id).map_err(|e| format!("Encoding error: {}", e))?,
};

ic_cdk::api::management_canister::main::install_code(install_args)
Expand Down
6 changes: 3 additions & 3 deletions contracts/icp/context-config/src/sys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ use crate::CONTEXT_CONFIGS;
pub fn set_proxy_code(proxy_code: Vec<u8>) -> Result<(), String> {
CONTEXT_CONFIGS.with(|configs| {
let mut configs = configs.borrow_mut();

// Check if caller is the owner
if ic_cdk::api::caller() != configs.owner {
return Err("Unauthorized: only owner can set proxy code".to_string());
}

configs.proxy_code = Some(proxy_code);
Ok(())
})
}
}
28 changes: 17 additions & 11 deletions contracts/icp/context-config/tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use rand::Rng;

fn setup() -> (PocketIc, Principal) {
let pic = PocketIc::new();

// Deploy the context contract
let wasm = std::fs::read("target/wasm32-unknown-unknown/release/context_contract.wasm")
.expect("failed to read wasm");
Expand All @@ -25,14 +25,17 @@ fn setup() -> (PocketIc, Principal) {
);

// Set the proxy code
let proxy_code = std::fs::read("../proxy-contract/target/wasm32-unknown-unknown/release/proxy_contract.wasm")
.expect("failed to read proxy wasm");
let proxy_code = std::fs::read(
"../proxy-contract/target/wasm32-unknown-unknown/release/proxy_contract.wasm",
)
.expect("failed to read proxy wasm");
pic.update_call(
canister,
Principal::anonymous(),
"set_proxy_code",
candid::encode_one(proxy_code).unwrap(),
).expect("Failed to set proxy code");
)
.expect("Failed to set proxy code");

(pic, canister)
}
Expand All @@ -50,19 +53,23 @@ fn get_time_nanos(pic: &PocketIc) -> u64 {
}

fn handle_response(
response: Result<WasmResult, UserError>,
response: Result<WasmResult, UserError>,
expected_success: bool,
operation_name: &str
operation_name: &str,
) {
match response {
Ok(WasmResult::Reply(bytes)) => {
let result: Result<(), String> = candid::decode_one(&bytes)
.unwrap_or_else(|e| panic!("Failed to decode response for {}: {}", operation_name, e));

let result: Result<(), String> = candid::decode_one(&bytes).unwrap_or_else(|e| {
panic!("Failed to decode response for {}: {}", operation_name, e)
});

match (result, expected_success) {
(Ok(_), true) => println!("{} succeeded as expected", operation_name),
(Ok(_), false) => panic!("{} succeeded when it should have failed", operation_name),
(Err(e), true) => panic!("{} failed when it should have succeeded: {}", operation_name, e),
(Err(e), true) => panic!(
"{} failed when it should have succeeded: {}",
operation_name, e
),
(Err(e), false) => println!("{} failed as expected: {}", operation_name, e),
}
}
Expand Down Expand Up @@ -167,7 +174,6 @@ fn test_proxy_management() {
handle_response(response, true, "mutate");
}


#[test]
fn test_mutate_success_cases() {
let (pic, canister) = setup();
Expand Down

0 comments on commit cf5a494

Please sign in to comment.