Skip to content

Commit 4dcf514

Browse files
Address feedbacks
1 parent 920c504 commit 4dcf514

3 files changed

Lines changed: 43 additions & 53 deletions

File tree

rs/nns/governance/BUILD.bazel

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ DEPENDENCIES = [
7878
"//rs/nns/governance/api",
7979
"//rs/nns/governance/init",
8080
"//rs/nns/gtc_accounts",
81+
"//rs/nns/handlers/lifeline/interface",
8182
"//rs/nns/handlers/root/interface",
8283
"//rs/nns/sns-wasm",
8384
"//rs/node_rewards/canister/api",
@@ -386,6 +387,10 @@ rust_test(
386387
name = "governance_test",
387388
srcs = glob(["src/**/*.rs"]),
388389
aliases = ALIASES,
390+
data = [
391+
"//rs/nns/handlers/lifeline/impl:lifeline.did",
392+
"//rs/nns/handlers/root/impl:canister/root.did",
393+
],
389394
proc_macro_deps = MACRO_DEPENDENCIES + MACRO_DEV_DEPENDENCIES,
390395
deps = DEPENDENCIES + DEV_DEPENDENCIES + [
391396
":build_script",
@@ -397,6 +402,10 @@ rust_test(
397402
srcs = glob(["src/**/*.rs"]),
398403
aliases = ALIASES,
399404
crate_features = test_with_tla(),
405+
data = [
406+
"//rs/nns/handlers/lifeline/impl:lifeline.did",
407+
"//rs/nns/handlers/root/impl:canister/root.did",
408+
],
400409
proc_macro_deps = MACRO_DEPENDENCIES + MACRO_DEV_DEPENDENCIES,
401410
deps = DEPENDENCIES + DEV_DEPENDENCIES + [
402411
":build_script",

rs/nns/governance/src/proposals/execute_nns_function.rs

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,21 @@ fn maybe_hash_large_blobs(
305305
value: Some(Value::Map(SelfDescribingValueMap { values: values_map })),
306306
}
307307
}
308-
308+
/// If the provided `key` is one of the `field_names`, and the associated `SelfDescribingValue` is a
309+
/// blob, replaces the value with its SHA-256 hash and modifies the key to be `<key>_hash`.
310+
/// Otherwise, leaves the entry unchanged.
311+
///
312+
/// # Example
313+
/// ```
314+
/// let field_names = &["wasm_module", "init_arg"];
315+
/// let (k, v) = maybe_replace_large_blob_entry_with_hash(
316+
/// "wasm_module".to_string(),
317+
/// SelfDescribingValue { value: Some(Value::Blob(vec![1,2,3])) },
318+
/// field_names,
319+
/// );
320+
/// assert_eq!(k, "wasm_module_hash");
321+
/// assert!(matches!(v.value, Some(Value::Blob(_))));
322+
/// ```
309323
fn maybe_replace_large_blob_entry_with_hash(
310324
key: String,
311325
value: SelfDescribingValue,
@@ -314,17 +328,16 @@ fn maybe_replace_large_blob_entry_with_hash(
314328
if !field_names.contains(&key.as_str()) {
315329
return (key, value);
316330
}
317-
// Use multiple-level match so that the original value can be returned without cloning if it
318-
// doesn't match.
319-
let SelfDescribingValue {
320-
value: Some(Value::Blob(blob)),
321-
} = value
322-
else {
331+
332+
let Some(inner_value) = &value.value else {
333+
return (key, value);
334+
};
335+
let Value::Blob(blob) = inner_value else {
323336
return (key, value);
324337
};
325338

326339
let key = format!("{key}_hash");
327-
let hash = Sha256::hash(&blob).to_vec();
340+
let hash = Sha256::hash(blob).to_vec();
328341
let hashed_value = SelfDescribingValue {
329342
value: Some(Value::Blob(hash)),
330343
};

rs/nns/governance/src/proposals/execute_nns_function_tests.rs

Lines changed: 13 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@ use candid::{Decode, Encode, Nat};
1010
use ic_base_types::{CanisterId, PrincipalId};
1111
use ic_crypto_sha2::Sha256;
1212
use ic_management_canister_types_private::{CanisterMetadataRequest, CanisterMetadataResponse};
13+
use ic_nervous_system_root::change_canister::AddCanisterRequest;
1314
use ic_nns_constants::{BITCOIN_MAINNET_CANISTER_ID, CYCLES_MINTING_CANISTER_ID};
1415
use ic_nns_governance_api::{
1516
SelfDescribingValue,
1617
bitcoin::{BitcoinNetwork, BitcoinSetConfigProposal},
1718
subnet_rental::{RentalConditionId, SubnetRentalRequest},
1819
};
20+
use ic_nns_handler_lifeline_interface::HardResetNnsRootToVersionPayload;
1921
use ic_sns_wasm::pb::v1::{AddWasmRequest, SnsCanisterType, SnsWasm};
2022
use maplit::hashmap;
2123
use std::sync::Arc;
@@ -122,6 +124,8 @@ fn test_execute_nns_function_try_from_errors() {
122124
}
123125
}
124126

127+
// This tests a "normal" NNS function where the payload is translated through a candid file fetched
128+
// by the `canister_metadata` method on the management canister.
125129
#[tokio::test]
126130
async fn test_to_self_describing_update_subnet_type() {
127131
// Minimal CMC candid file with only update_subnet_type method
@@ -419,41 +423,19 @@ fn test_re_encode_payload_to_target_canister_overrides_proposal_id_for_add_wasm(
419423

420424
#[tokio::test]
421425
async fn test_to_self_describing_nns_canister_install() {
422-
let root_candid = r#"
423-
type AddCanisterRequest = record {
424-
arg : blob;
425-
initial_cycles : nat64;
426-
wasm_module : blob;
427-
name : text;
428-
memory_allocation : opt nat;
429-
compute_allocation : opt nat;
430-
};
431-
432-
service : {
433-
add_nns_canister : (AddCanisterRequest) -> ();
434-
}
435-
"#;
436-
437-
#[derive(candid::CandidType)]
438-
struct AddCanisterRequest {
439-
arg: Vec<u8>,
440-
initial_cycles: u64,
441-
wasm_module: Vec<u8>,
442-
name: String,
443-
memory_allocation: Option<u64>,
444-
compute_allocation: Option<u64>,
445-
}
426+
let root_candid =
427+
std::fs::read_to_string("rs/nns/handlers/root/impl/canister/root.did").unwrap();
446428

447429
let wasm_module = vec![0_u8, 0x61, 0x73, 0x6D, 1_u8, 0_u8, 0_u8, 0_u8];
448430
let arg = vec![1_u8, 2_u8, 3_u8];
449431

450432
let request = AddCanisterRequest {
451-
arg: arg.clone(),
452-
initial_cycles: 1_000_000_000_000_u64,
453-
wasm_module: wasm_module.clone(),
454433
name: "test-canister".to_string(),
455-
memory_allocation: None,
434+
wasm_module: wasm_module.clone(),
435+
arg: arg.clone(),
456436
compute_allocation: None,
437+
memory_allocation: None,
438+
initial_cycles: 1_000_000_000_000_u64,
457439
};
458440
let payload = Encode!(&request).unwrap();
459441

@@ -503,27 +485,13 @@ service : {
503485

504486
#[tokio::test]
505487
async fn test_to_self_describing_hard_reset_nns_root_to_version() {
506-
let lifeline_candid = r#"
507-
type HardResetRootToVersionPayload = record {
508-
wasm_module: blob;
509-
init_arg: blob;
510-
};
511-
512-
service : {
513-
hard_reset_root_to_version: (HardResetRootToVersionPayload) -> ();
514-
}
515-
"#;
516-
517-
#[derive(candid::CandidType)]
518-
struct HardResetRootToVersionPayload {
519-
wasm_module: Vec<u8>,
520-
init_arg: Vec<u8>,
521-
}
488+
let lifeline_candid =
489+
std::fs::read_to_string("rs/nns/handlers/lifeline/impl/lifeline.did").unwrap();
522490

523491
let wasm_module = vec![0_u8, 0x61, 0x73, 0x6D, 1_u8, 0_u8, 0_u8, 0_u8];
524492
let init_arg = vec![4_u8, 5_u8, 6_u8];
525493

526-
let request = HardResetRootToVersionPayload {
494+
let request = HardResetNnsRootToVersionPayload {
527495
wasm_module: wasm_module.clone(),
528496
init_arg: init_arg.clone(),
529497
};

0 commit comments

Comments
 (0)