Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
75 changes: 0 additions & 75 deletions rs/nns/governance/src/pb/conversions/mod.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
use crate::pb::proposal_conversions::{ProposalDisplayOptions, convert_proposal};
use crate::pb::v1 as pb;

use candid::{Int, Nat};
use ic_crypto_sha2::Sha256;
use ic_nns_governance_api as pb_api;
use ic_protobuf::registry::replica_version::v1::{
GuestLaunchMeasurement as PbGuestLaunchMeasurement,
GuestLaunchMeasurementMetadata as PbGuestLaunchMeasurementMetadata,
GuestLaunchMeasurements as PbGuestLaunchMeasurements,
};
use std::collections::HashMap;

#[cfg(test)]
mod tests;
Expand Down Expand Up @@ -4176,76 +4174,3 @@ impl From<pb::MaturityDisbursement> for pb_api::MaturityDisbursement {
}
}
}

impl From<pb::SelfDescribingValue> for pb_api::SelfDescribingValue {
fn from(item: pb::SelfDescribingValue) -> Self {
let Some(value) = item.value else {
return Self::Map(HashMap::new());
};
match value {
pb::self_describing_value::Value::Blob(v) => Self::Blob(v),
pb::self_describing_value::Value::Text(v) => Self::Text(v),
pb::self_describing_value::Value::Nat(v) => {
let nat = Nat::decode(&mut v.as_slice()).unwrap();
Self::Nat(nat)
}
pb::self_describing_value::Value::Int(v) => {
let int = Int::decode(&mut v.as_slice()).unwrap();
Self::Int(int)
}
pb::self_describing_value::Value::Array(v) => {
Self::Array(v.values.into_iter().map(Self::from).collect())
}
pb::self_describing_value::Value::Map(v) => Self::Map(
v.values
.into_iter()
.map(|(k, v)| (k, Self::from(v)))
.collect(),
),
pb::self_describing_value::Value::Null(_) => Self::Null,
}
}
}

impl From<pb_api::SelfDescribingValue> for pb::SelfDescribingValue {
fn from(item: pb_api::SelfDescribingValue) -> Self {
let value = match item {
pb_api::SelfDescribingValue::Blob(v) => pb::self_describing_value::Value::Blob(v),
pb_api::SelfDescribingValue::Text(v) => pb::self_describing_value::Value::Text(v),
pb_api::SelfDescribingValue::Nat(v) => {
let mut bytes = Vec::new();
v.encode(&mut bytes).unwrap();
pb::self_describing_value::Value::Nat(bytes)
}
pb_api::SelfDescribingValue::Int(v) => {
let mut bytes = Vec::new();
v.encode(&mut bytes).unwrap();
pb::self_describing_value::Value::Int(bytes)
}
pb_api::SelfDescribingValue::Array(v) => {
pb::self_describing_value::Value::Array(pb::SelfDescribingValueArray {
values: v.into_iter().map(Self::from).collect(),
})
}
pb_api::SelfDescribingValue::Map(v) => {
pb::self_describing_value::Value::Map(pb::SelfDescribingValueMap {
values: v.into_iter().map(|(k, v)| (k, Self::from(v))).collect(),
})
}
pb_api::SelfDescribingValue::Null => {
pb::self_describing_value::Value::Null(pb::Empty {})
}
};
Self { value: Some(value) }
}
}

impl From<pb::SelfDescribingProposalAction> for pb_api::SelfDescribingProposalAction {
fn from(item: pb::SelfDescribingProposalAction) -> Self {
Self {
type_name: Some(item.type_name),
type_description: Some(item.type_description),
value: item.value.map(pb_api::SelfDescribingValue::from),
}
}
}
144 changes: 1 addition & 143 deletions rs/nns/governance/src/pb/conversions/tests.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use super::*;
use candid::{Int, Nat};

use ic_base_types::PrincipalId;
use icp_ledger::protobuf::AccountIdentifier;
use maplit::hashmap;

#[test]
fn test_node_provider_conversions_always_create_32_byte_account_identifier() {
Expand Down Expand Up @@ -130,144 +129,3 @@ fn test_reward_to_account_invalid_account_identifier_just_return_what_is_stored(
icp_ledger::AccountIdentifier::try_from(&converted_reward_to_account.to_account.unwrap())
.expect_err("Should fail!");
}

#[test]
fn test_value_conversions() {
// Prepare test data for all value types
let nat_value = Nat::from(12345u64);
let int_value = Int::from(-9876i64);

// Encode Nat and Int to bytes
let mut nat_bytes = Vec::new();
nat_value.encode(&mut nat_bytes).unwrap();

let mut int_bytes = Vec::new();
int_value.encode(&mut int_bytes).unwrap();

// Create a comprehensive map with all possible SelfDescribingValue types
let value_pb = pb::SelfDescribingValue {
value: Some(pb::self_describing_value::Value::Map(
pb::SelfDescribingValueMap {
values: hashmap! {
// Test Text type
"text_field".to_string() => pb::SelfDescribingValue {
value: Some(pb::self_describing_value::Value::Text("some text".to_string())),
},
// Test Blob type
"blob_field".to_string() => pb::SelfDescribingValue {
value: Some(pb::self_describing_value::Value::Blob(vec![1, 2, 3, 4, 5])),
},
// Test Nat type
"nat_field".to_string() => pb::SelfDescribingValue {
value: Some(pb::self_describing_value::Value::Nat(nat_bytes.clone())),
},
// Test Int type
"int_field".to_string() => pb::SelfDescribingValue {
value: Some(pb::self_describing_value::Value::Int(int_bytes.clone())),
},
// Test Array type with various elements
"array_field".to_string() => pb::SelfDescribingValue {
value: Some(pb::self_describing_value::Value::Array(pb::SelfDescribingValueArray {
values: vec![
pb::SelfDescribingValue {
value: Some(pb::self_describing_value::Value::Text("first".to_string())),
},
pb::SelfDescribingValue {
value: Some(pb::self_describing_value::Value::Text("second".to_string())),
},
pb::SelfDescribingValue {
value: Some(pb::self_describing_value::Value::Blob(vec![10, 20, 30])),
},
],
})),
},
// Test nested Map type
"nested_map_field".to_string() => pb::SelfDescribingValue {
value: Some(pb::self_describing_value::Value::Map(pb::SelfDescribingValueMap {
values: hashmap! {
"nested_text".to_string() => pb::SelfDescribingValue {
value: Some(pb::self_describing_value::Value::Text("nested value".to_string())),
},
"nested_blob".to_string() => pb::SelfDescribingValue {
value: Some(pb::self_describing_value::Value::Blob(vec![255, 254, 253])),
},
"nested_nat".to_string() => pb::SelfDescribingValue {
value: Some(pb::self_describing_value::Value::Nat(nat_bytes.clone())),
},
},
})),
},
// Test empty Array
"empty_array_field".to_string() => pb::SelfDescribingValue {
value: Some(pb::self_describing_value::Value::Array(pb::SelfDescribingValueArray {
values: vec![],
})),
},
// Test empty Map
"empty_map_field".to_string() => pb::SelfDescribingValue {
value: Some(pb::self_describing_value::Value::Map(pb::SelfDescribingValueMap {
values: hashmap! {},
})),
},
// Test Array containing Maps
"array_of_maps_field".to_string() => pb::SelfDescribingValue {
value: Some(pb::self_describing_value::Value::Array(pb::SelfDescribingValueArray {
values: vec![
pb::SelfDescribingValue {
value: Some(pb::self_describing_value::Value::Map(pb::SelfDescribingValueMap {
values: hashmap! {
"key1".to_string() => pb::SelfDescribingValue {
value: Some(pb::self_describing_value::Value::Text("value1".to_string())),
},
},
})),
},
pb::SelfDescribingValue {
value: Some(pb::self_describing_value::Value::Map(pb::SelfDescribingValueMap {
values: hashmap! {
"key2".to_string() => pb::SelfDescribingValue {
value: Some(pb::self_describing_value::Value::Text("value2".to_string())),
},
},
})),
},
],
})),
},
},
},
)),
};

let value_pb_api = pb_api::SelfDescribingValue::from(value_pb);

assert_eq!(
value_pb_api,
pb_api::SelfDescribingValue::Map(hashmap! {
"text_field".to_string() => pb_api::SelfDescribingValue::Text("some text".to_string()),
"blob_field".to_string() => pb_api::SelfDescribingValue::Blob(vec![1, 2, 3, 4, 5]),
"nat_field".to_string() => pb_api::SelfDescribingValue::Nat(nat_value.clone()),
"int_field".to_string() => pb_api::SelfDescribingValue::Int(int_value.clone()),
"array_field".to_string() => pb_api::SelfDescribingValue::Array(vec![
pb_api::SelfDescribingValue::Text("first".to_string()),
pb_api::SelfDescribingValue::Text("second".to_string()),
pb_api::SelfDescribingValue::Blob(vec![10, 20, 30]),
]),
"nested_map_field".to_string() => pb_api::SelfDescribingValue::Map(hashmap! {
"nested_text".to_string() => pb_api::SelfDescribingValue::Text("nested value".to_string()),
"nested_blob".to_string() => pb_api::SelfDescribingValue::Blob(vec![255, 254, 253]),
"nested_nat".to_string() => pb_api::SelfDescribingValue::Nat(nat_value.clone()),
}),
"empty_array_field".to_string() => pb_api::SelfDescribingValue::Array(vec![]),
"empty_map_field".to_string() => pb_api::SelfDescribingValue::Map(hashmap! {}),
"array_of_maps_field".to_string() => pb_api::SelfDescribingValue::Array(vec![
pb_api::SelfDescribingValue::Map(hashmap! {
"key1".to_string() => pb_api::SelfDescribingValue::Text("value1".to_string()),
}),
pb_api::SelfDescribingValue::Map(hashmap! {
"key2".to_string() => pb_api::SelfDescribingValue::Text("value2".to_string()),
}),
]),
})
);
}
Loading