Skip to content

Commit

Permalink
Add topics list
Browse files Browse the repository at this point in the history
  • Loading branch information
anchpop committed Jan 10, 2025
1 parent 25d8f70 commit 77f82ee
Show file tree
Hide file tree
Showing 14 changed files with 420 additions and 21 deletions.
8 changes: 8 additions & 0 deletions rs/sns/governance/api/src/ic_sns_governance.pb.v1.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::topics;
use std::collections::BTreeMap;

/// A principal with a particular set of permissions over a neuron.
Expand Down Expand Up @@ -80,6 +81,9 @@ pub struct Neuron {
/// The neuron's followees, specified as a map of proposal functions IDs to followees neuron IDs.
/// The map's keys are represented by integers as Protobuf does not support enum keys in maps.
pub followees: BTreeMap<u64, neuron::Followees>,
/// The neuron's followees on topics, specified as a map of topic IDs to followees neuron IDs.
/// The map's keys are represented by integers as Protobuf does not support enum keys in maps.
pub topic_followees: BTreeMap<topics::Topic, neuron::Followees>,
/// The accumulated unstaked maturity of the neuron, measured in "e8s equivalent", i.e., in equivalent of
/// 10E-8 of a governance token.
///
Expand Down Expand Up @@ -208,6 +212,8 @@ pub struct NervousSystemFunction {
}
/// Nested message and enum types in `NervousSystemFunction`.
pub mod nervous_system_function {
use super::*;

#[derive(Default, candid::CandidType, candid::Deserialize, Debug, Clone, PartialEq)]
pub struct GenericNervousSystemFunction {
/// The id of the target canister that will be called to execute the proposal.
Expand All @@ -224,6 +230,8 @@ pub mod nervous_system_function {
/// The signature of the method must be equivalent to the following:
/// <method_name>(proposal_data: ProposalData) -> Result<String, String>
pub validator_method_name: Option<String>,
/// The topic this function belongs to
pub topic: Option<topics::Topic>,
}
#[derive(candid::CandidType, candid::Deserialize, Debug, Clone, PartialEq)]
pub enum FunctionType {
Expand Down
1 change: 1 addition & 0 deletions rs/sns/governance/api/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod pb;
pub mod topics;

/// Formats the 32 bytes of a hash as a hexadecimal string. Corresponds to 64 ascii symbols.
pub fn format_full_hash(hash: &[u8]) -> String {
Expand Down
89 changes: 89 additions & 0 deletions rs/sns/governance/api/src/topics.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/// Functions are categorized into topics.
/// (As a reminder, a function is either a built-in proposal type, or a generic function that has been added via an
/// AddGenericNervousSystemFunction proposal)
#[derive(Debug, candid::CandidType, candid::Deserialize, Ord, PartialOrd, Eq, Clone, PartialEq)]
pub enum Topic {
NonCriticalProposals,
DaoCommunitySettings,
SnsFrameworkManagement,
DappCanisterManagement,
ApplicationBusinessLogic,
Governance,
TreasuryAssetManagement,
CriticalDappOperations,
}

mod topic_ids {
pub const NON_CRITICAL_PROPOSALS: u64 = 0;
pub const DAO_COMMUNITY_SETTINGS: u64 = 1;
pub const SNS_FRAMEWORK_MANAGEMENT: u64 = 2;
pub const DAPP_CANISTER_MANAGEMENT: u64 = 3;
pub const APPLICATION_BUSINESS_LOGIC: u64 = 4;
pub const GOVERNANCE: u64 = 5;
pub const TREASURY_ASSET_MANAGEMENT: u64 = 6;
pub const CRITICAL_DAPP_OPERATIONS: u64 = 7;
}

impl From<Topic> for u64 {
fn from(topic: Topic) -> Self {
match topic {
Topic::NonCriticalProposals => topic_ids::NON_CRITICAL_PROPOSALS,
Topic::DaoCommunitySettings => topic_ids::DAO_COMMUNITY_SETTINGS,
Topic::SnsFrameworkManagement => topic_ids::SNS_FRAMEWORK_MANAGEMENT,
Topic::DappCanisterManagement => topic_ids::DAPP_CANISTER_MANAGEMENT,
Topic::ApplicationBusinessLogic => topic_ids::APPLICATION_BUSINESS_LOGIC,
Topic::Governance => topic_ids::GOVERNANCE,
Topic::TreasuryAssetManagement => topic_ids::TREASURY_ASSET_MANAGEMENT,
Topic::CriticalDappOperations => topic_ids::CRITICAL_DAPP_OPERATIONS,
}
}
}

impl TryFrom<u64> for Topic {
type Error = ();

fn try_from(value: u64) -> Result<Self, Self::Error> {
match value {
topic_ids::NON_CRITICAL_PROPOSALS => Ok(Topic::NonCriticalProposals),
topic_ids::DAO_COMMUNITY_SETTINGS => Ok(Topic::DaoCommunitySettings),
topic_ids::SNS_FRAMEWORK_MANAGEMENT => Ok(Topic::SnsFrameworkManagement),
topic_ids::DAPP_CANISTER_MANAGEMENT => Ok(Topic::DappCanisterManagement),
topic_ids::APPLICATION_BUSINESS_LOGIC => Ok(Topic::ApplicationBusinessLogic),
topic_ids::GOVERNANCE => Ok(Topic::Governance),
topic_ids::TREASURY_ASSET_MANAGEMENT => Ok(Topic::TreasuryAssetManagement),
topic_ids::CRITICAL_DAPP_OPERATIONS => Ok(Topic::CriticalDappOperations),
_ => Err(()),
}
}
}

/// Each topic has some information associated with it. This information is for the benefit of the user but has
/// no effect on the behavior of the SNS.
#[derive(Debug, candid::CandidType, candid::Deserialize, Clone, PartialEq)]
pub struct TopicInfo<C> {
pub topic: Topic,
pub name: String,
pub description: String,
pub content: C,
pub nested_topics: Vec<TopicInfo<C>>,
pub critical: bool,
}

#[derive(Debug, candid::CandidType, candid::Deserialize, Clone, PartialEq)]
pub struct BuiltInFunctions {
pub built_in_functions: Vec<u64>,
}

#[derive(Debug, candid::CandidType, candid::Deserialize, Clone, PartialEq)]
pub struct BuiltInAndGenericFunctions {
pub built_in_functions: Vec<u64>,
pub generic_functions: Vec<u64>,
}

#[derive(Debug, candid::CandidType, candid::Deserialize, Clone, PartialEq)]
pub struct ListTopicsRequest {}

#[derive(Debug, candid::CandidType, candid::Deserialize, Clone, PartialEq)]
pub struct ListTopicsResponse {
pub topics: Option<Vec<TopicInfo<BuiltInAndGenericFunctions>>>,
}
37 changes: 24 additions & 13 deletions rs/sns/governance/canister/canister.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,27 @@ use ic_sns_governance::{
types::{Environment, HeapGrowthPotential},
upgrade_journal::serve_journal,
};
use ic_sns_governance_api::pb::v1::{
get_running_sns_version_response::UpgradeInProgress, governance::Version,
ClaimSwapNeuronsRequest, ClaimSwapNeuronsResponse, FailStuckUpgradeInProgressRequest,
FailStuckUpgradeInProgressResponse, GetMaturityModulationRequest,
GetMaturityModulationResponse, GetMetadataRequest, GetMetadataResponse, GetMode,
GetModeResponse, GetNeuron, GetNeuronResponse, GetProposal, GetProposalResponse,
GetRunningSnsVersionRequest, GetRunningSnsVersionResponse,
GetSnsInitializationParametersRequest, GetSnsInitializationParametersResponse,
GetUpgradeJournalRequest, GetUpgradeJournalResponse, ListNervousSystemFunctionsResponse,
ListNeurons, ListNeuronsResponse, ListProposals, ListProposalsResponse, ManageNeuron,
ManageNeuronResponse, NervousSystemParameters, RewardEvent, SetMode, SetModeResponse,
};
#[cfg(feature = "test")]
use ic_sns_governance_api::pb::v1::{
AddMaturityRequest, AddMaturityResponse, AdvanceTargetVersionRequest,
AdvanceTargetVersionResponse, GovernanceError, MintTokensRequest, MintTokensResponse, Neuron,
RefreshCachedUpgradeStepsRequest, RefreshCachedUpgradeStepsResponse,
};
use ic_sns_governance_api::{
pb::v1::{
get_running_sns_version_response::UpgradeInProgress, governance::Version,
ClaimSwapNeuronsRequest, ClaimSwapNeuronsResponse, FailStuckUpgradeInProgressRequest,
FailStuckUpgradeInProgressResponse, GetMaturityModulationRequest,
GetMaturityModulationResponse, GetMetadataRequest, GetMetadataResponse, GetMode,
GetModeResponse, GetNeuron, GetNeuronResponse, GetProposal, GetProposalResponse,
GetRunningSnsVersionRequest, GetRunningSnsVersionResponse,
GetSnsInitializationParametersRequest, GetSnsInitializationParametersResponse,
GetUpgradeJournalRequest, GetUpgradeJournalResponse, ListNervousSystemFunctionsResponse,
ListNeurons, ListNeuronsResponse, ListProposals, ListProposalsResponse, ManageNeuron,
ManageNeuronResponse, NervousSystemParameters, RewardEvent, SetMode, SetModeResponse,
},
topics::{ListTopicsRequest, ListTopicsResponse},
};
use prost::Message;
use rand::{RngCore, SeedableRng};
use rand_chacha::ChaCha20Rng;
Expand Down Expand Up @@ -383,9 +386,10 @@ async fn manage_neuron(request: ManageNeuron) -> ManageNeuronResponse {
fn update_neuron(neuron: Neuron) -> Option<GovernanceError> {
log!(INFO, "update_neuron");
let governance = governance_mut();
let neuron = sns_gov_pb::Neuron::from(neuron);
measure_span(governance.profiling_information, "update_neuron", || {
governance
.update_neuron(sns_gov_pb::Neuron::from(neuron))
.update_neuron(neuron)
.map_err(GovernanceError::from)
.err()
})
Expand Down Expand Up @@ -450,6 +454,13 @@ fn list_nervous_system_functions() -> ListNervousSystemFunctionsResponse {
ListNervousSystemFunctionsResponse::from(governance().list_nervous_system_functions())
}

/// Returns the current list of topics.
#[query]
fn list_topics(_: ListTopicsRequest) -> ListTopicsResponse {
log!(INFO, "list_topics");
unimplemented!()
}

/// Returns the latest reward event.
#[query]
fn get_latest_reward_event() -> RewardEvent {
Expand Down
75 changes: 72 additions & 3 deletions rs/sns/governance/canister/governance.did
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ type GenericNervousSystemFunction = record {
target_canister_id : opt principal;
validator_method_name : opt text;
target_method_name : opt text;
topic: opt Topic;
};

type GetMaturityModulationResponse = record {
Expand Down Expand Up @@ -278,9 +279,44 @@ type CachedUpgradeSteps = record {
response_timestamp_seconds : opt nat64;
};

type NeuronInit = record {
id : opt NeuronId;
staked_maturity_e8s_equivalent : opt nat64;
permissions : vec NeuronPermission;
maturity_e8s_equivalent : nat64;
cached_neuron_stake_e8s : nat64;
created_timestamp_seconds : nat64;
source_nns_neuron_id : opt nat64;
auto_stake_maturity : opt bool;
aging_since_timestamp_seconds : nat64;
dissolve_state : opt DissolveState;
voting_power_percentage_multiplier : nat64;
vesting_period_seconds : opt nat64;
disburse_maturity_in_progress : vec DisburseMaturityInProgress;
followees : vec record { nat64; Followees };
topic_followees : vec record { nat64; Followees };
neuron_fees_e8s : nat64;
};

type NervousSystemFunctionInit = record {
id : nat64;
name : text;
description : opt text;
function_type : opt variant {
NativeNervousSystemFunction : record {};
GenericNervousSystemFunction : record {
validator_canister_id : opt principal;
target_canister_id : opt principal;
validator_method_name : opt text;
target_method_name : opt text;
topic: opt nat64;
};
};
};

type Governance = record {
root_canister_id : opt principal;
id_to_nervous_system_functions : vec record { nat64; NervousSystemFunction };
id_to_nervous_system_functions : vec record { nat64; NervousSystemFunctionInit };
metrics : opt GovernanceCachedMetrics;
maturity_modulation : opt MaturityModulation;
mode : int32;
Expand All @@ -293,10 +329,10 @@ type Governance = record {
pending_version : opt PendingVersion;
swap_canister_id : opt principal;
ledger_canister_id : opt principal;
proposals : vec record { nat64; ProposalData };
proposals : vec record { nat64; ProposalData }; // this would also change but I haven't done it yet
in_flight_commands : vec record { text; NeuronInFlightCommand };
sns_metadata : opt ManageSnsMetadata;
neurons : vec record { text; Neuron };
neurons : vec record { text; NeuronInit };
genesis_timestamp_seconds : nat64;
target_version : opt Version;
timers : opt Timers;
Expand Down Expand Up @@ -483,6 +519,17 @@ type NervousSystemParameters = record {
max_number_of_principals_per_neuron : opt nat64;
};

type Topic = variant {
NonCriticalProposals;
DaoCommunitySettings;
SnsFrameworkManagement;
DappCanisterManagement;
ApplicationBusinessLogic;
Governance;
TreasuryAssetManagement;
CriticalDappOperations;
};

type Neuron = record {
id : opt NeuronId;
staked_maturity_e8s_equivalent : opt nat64;
Expand All @@ -498,6 +545,7 @@ type Neuron = record {
vesting_period_seconds : opt nat64;
disburse_maturity_in_progress : vec DisburseMaturityInProgress;
followees : vec record { nat64; Followees };
topic_followees : vec record { Topic; Followees };
neuron_fees_e8s : nat64;
};

Expand Down Expand Up @@ -822,6 +870,26 @@ type GetUpgradeJournalResponse = record {
upgrade_journal_entry_count: opt nat64;
};

type ListTopicsRequest = record {};

type BuiltInAndGenericFunctions = record {
built_in_functions : vec nat64;
generic_functions : vec nat64;
};

type TopicInfo = record {
topic : Topic;
name : text;
description : text;
content : BuiltInAndGenericFunctions;
nested_topics : vec TopicInfo;
critical : bool;
};

type ListTopicsResponse = record {
topics: opt vec TopicInfo;
};

service : (Governance) -> {
claim_swap_neurons : (ClaimSwapNeuronsRequest) -> (ClaimSwapNeuronsResponse);
fail_stuck_upgrade_in_progress : (record {}) -> (record {});
Expand All @@ -840,6 +908,7 @@ service : (Governance) -> {
list_nervous_system_functions : () -> (ListNervousSystemFunctionsResponse) query;
list_neurons : (ListNeurons) -> (ListNeuronsResponse) query;
list_proposals : (ListProposals) -> (ListProposalsResponse) query;
list_topics : (ListTopicsRequest) -> (ListTopicsResponse) query;
manage_neuron : (ManageNeuron) -> (ManageNeuronResponse);
set_mode : (SetMode) -> (record {});
reset_timers : (record {}) -> (record {});
Expand Down
Loading

0 comments on commit 77f82ee

Please sign in to comment.