Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(sns): Use ic-nervous-system-agent in sns-audit #1756

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions rs/nervous_system/agent/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@ DEPENDENCIES = [
# Keep sorted.
"//packages/pocket-ic",
"//rs/nervous_system/clients",
"//rs/nns/common",
"//rs/nns/constants",
"//rs/nns/governance/api",
"//rs/nns/sns-wasm",
"//rs/sns/governance",
"//rs/sns/root",
"//rs/sns/swap",
"//rs/types/base_types",
"@crate_index//:anyhow",
"@crate_index//:candid",
Expand Down
3 changes: 3 additions & 0 deletions rs/nervous_system/agent/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@ candid = { workspace = true }
ic-agent = { workspace = true }
ic-base-types = { path = "../../types/base_types" }
ic-nervous-system-clients = { path = "../clients" }
ic-nns-governance-api = { path = "../../nns/governance/api" }
ic-nns-common = { path = "../../nns/common" }
ic-nns-constants = { path = "../../nns/constants" }
ic-sns-wasm = { path = "../../nns/sns-wasm" }
ic-sns-governance = { path = "../../sns/governance" }
pocket-ic = { path = "../../../packages/pocket-ic" }
ic-sns-root = { path = "../../sns/root" }
ic-sns-swap = { path = "../../sns/swap" }
serde = { workspace = true }
tempfile = { workspace = true }
thiserror = { workspace = true }
Expand Down
2 changes: 2 additions & 0 deletions rs/nervous_system/agent/src/agent_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ pub enum AgentCallError {
CandidDecode(candid::Error),
}

impl crate::sealed::Sealed for Agent {}

impl CallCanisters for Agent {
type Error = AgentCallError;
async fn call<R: Request>(
Expand Down
11 changes: 9 additions & 2 deletions rs/nervous_system/agent/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,15 @@ use candid::Principal;
use ic_nervous_system_clients::Request;
use std::fmt::Display;

pub trait CallCanisters {
type Error: Display + Send;
// This is used to "seal" the CallCanisters trait so that it cannot be implemented outside of this crate.
// This is useful because it means we can modify the trait in the future without worrying about
// breaking backwards compatibility with implementations outside of this crate.
mod sealed {
pub trait Sealed {}
}

pub trait CallCanisters: sealed::Sealed {
type Error: Display + Send + std::error::Error + 'static;
fn call<R: Request>(
&self,
canister_id: impl Into<Principal> + Send,
Expand Down
16 changes: 16 additions & 0 deletions rs/nervous_system/agent/src/nns/governance.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use crate::CallCanisters;
use ic_nns_common::pb::v1::ProposalId;
use ic_nns_constants::GOVERNANCE_CANISTER_ID;
use ic_nns_governance_api::pb::v1::{
GetNeuronsFundAuditInfoRequest, GetNeuronsFundAuditInfoResponse,
};

pub async fn get_neurons_fund_audit_info<C: CallCanisters>(
agent: &C,
nns_proposal_id: ProposalId,
) -> Result<GetNeuronsFundAuditInfoResponse, C::Error> {
let request = GetNeuronsFundAuditInfoRequest {
nns_proposal_id: Some(nns_proposal_id),
};
agent.call(GOVERNANCE_CANISTER_ID, request).await
}
1 change: 1 addition & 0 deletions rs/nervous_system/agent/src/nns/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pub mod governance;
pub mod sns_wasm;
2 changes: 2 additions & 0 deletions rs/nervous_system/agent/src/pocketic_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ pub enum PocketIcCallError {
CandidDecode(candid::Error),
}

impl crate::sealed::Sealed for PocketIc {}

impl CallCanisters for PocketIc {
type Error = PocketIcCallError;
async fn call<R: Request>(
Expand Down
7 changes: 7 additions & 0 deletions rs/nervous_system/agent/src/sns/governance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,10 @@ impl GovernanceCanister {
.await
}
}

impl GovernanceCanister {
pub fn new(canister_id: impl Into<PrincipalId>) -> Self {
let canister_id = canister_id.into();
Self { canister_id }
}
}
7 changes: 7 additions & 0 deletions rs/nervous_system/agent/src/sns/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,10 @@ use serde::{Deserialize, Serialize};
pub struct IndexCanister {
pub canister_id: PrincipalId,
}

impl IndexCanister {
pub fn new(canister_id: impl Into<PrincipalId>) -> Self {
let canister_id = canister_id.into();
Self { canister_id }
}
}
7 changes: 7 additions & 0 deletions rs/nervous_system/agent/src/sns/ledger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,10 @@ use serde::{Deserialize, Serialize};
pub struct LedgerCanister {
pub canister_id: PrincipalId,
}

impl LedgerCanister {
pub fn new(canister_id: impl Into<PrincipalId>) -> Self {
let canister_id = canister_id.into();
Self { canister_id }
}
}
5 changes: 5 additions & 0 deletions rs/nervous_system/agent/src/sns/root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ pub struct RootCanister {
}

impl RootCanister {
pub fn new(canister_id: impl Into<PrincipalId>) -> Self {
let canister_id = canister_id.into();
Self { canister_id }
}

pub async fn sns_canisters_summary<C: CallCanisters>(
&self,
agent: &C,
Expand Down
75 changes: 75 additions & 0 deletions rs/nervous_system/agent/src/sns/swap.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,82 @@
use ic_base_types::PrincipalId;
use ic_sns_swap::pb::v1::{
GetDerivedStateRequest, GetDerivedStateResponse, GetInitRequest, GetInitResponse,
ListSnsNeuronRecipesRequest, ListSnsNeuronRecipesResponse, SnsNeuronRecipe,
};
use serde::{Deserialize, Serialize};
use thiserror::Error;

use crate::CallCanisters;

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct SwapCanister {
pub canister_id: PrincipalId,
}

#[derive(Debug, Error)]
pub enum ListAllSnsNeuronRecipesError<CallCanistersError: std::error::Error + 'static> {
#[error(transparent)]
CanisterCallError(#[from] CallCanistersError),
#[error("There seem to be too many neuron recipes ({0}).")]
TooManyRecipes(u64),
}

impl SwapCanister {
pub fn new(canister_id: impl Into<PrincipalId>) -> Self {
let canister_id = canister_id.into();
Self { canister_id }
}

pub async fn get_derived_state<C: CallCanisters>(
&self,
agent: &C,
) -> Result<GetDerivedStateResponse, C::Error> {
agent
.call(self.canister_id, GetDerivedStateRequest {})
.await
}

pub async fn get_init<C: CallCanisters>(&self, agent: &C) -> Result<GetInitResponse, C::Error> {
agent.call(self.canister_id, GetInitRequest {}).await
}

pub async fn list_sns_neuron_recipes<C: CallCanisters>(
&self,
agent: &C,
limit: u32,
offset: u64,
) -> Result<ListSnsNeuronRecipesResponse, C::Error> {
agent
.call(
self.canister_id,
ListSnsNeuronRecipesRequest {
limit: Some(limit),
offset: Some(offset),
},
)
.await
}

pub async fn list_all_sns_neuron_recipes<C: CallCanisters>(
&self,
agent: &C,
) -> Result<Vec<SnsNeuronRecipe>, ListAllSnsNeuronRecipesError<C::Error>> {
let mut sns_neuron_recipes: Vec<SnsNeuronRecipe> = vec![];
let batch_size = 10_000_u64;
let num_calls = 100_u64;
for i in 0..num_calls {
let new_sns_neuron_recipes = self
.list_sns_neuron_recipes(agent, batch_size as u32, batch_size * i)
.await
.map_err(ListAllSnsNeuronRecipesError::CanisterCallError)?;
if new_sns_neuron_recipes.sns_neuron_recipes.is_empty() {
return Ok(sns_neuron_recipes);
} else {
sns_neuron_recipes.extend(new_sns_neuron_recipes.sns_neuron_recipes.into_iter())
}
}
Err(ListAllSnsNeuronRecipesError::TooManyRecipes(
batch_size * num_calls,
))
}
}
1 change: 1 addition & 0 deletions rs/nns/governance/api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ pub mod bitcoin;
pub mod pb;
pub mod proposal_submission_helpers;
pub mod proposal_validation;
mod request_impls;
pub mod subnet_rental;
pub mod test_api;
2 changes: 2 additions & 0 deletions rs/nns/governance/api/src/pb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ impl fmt::Display for GovernanceError {
}
}

impl std::error::Error for GovernanceError {}

impl NeuronsFundEconomics {
/// The default values for network economics (until we initialize it).
/// Can't implement Default since it conflicts with Prost's.
Expand Down
7 changes: 7 additions & 0 deletions rs/nns/governance/api/src/request_impls.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
use ic_nervous_system_clients::Request;

impl Request for crate::pb::v1::GetNeuronsFundAuditInfoRequest {
type Response = crate::pb::v1::GetNeuronsFundAuditInfoResponse;
const METHOD: &'static str = "get_neurons_fund_audit_info";
const UPDATE: bool = false;
}
8 changes: 7 additions & 1 deletion rs/sns/audit/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ package(default_visibility = ["//visibility:public"])

DEPENDENCIES = [
# Keep sorted.
"//rs/nervous_system/agent",
"//rs/nervous_system/neurons_fund",
"//rs/nns/common",
"//rs/nns/governance/api",
"//rs/sns/governance",
"//rs/sns/swap",
"//rs/types/base_types",
"@crate_index//:candid",
"@crate_index//:colored",
"@crate_index//:csv",
Expand All @@ -18,6 +20,7 @@ DEPENDENCIES = [
"@crate_index//:serde",
"@crate_index//:serde_json",
"@crate_index//:textplots",
"@crate_index//:thiserror",
"@crate_index//:tokio",
]

Expand All @@ -30,5 +33,8 @@ rust_library(
rust_binary(
name = "sns-audit",
srcs = ["src/main.rs"],
deps = DEPENDENCIES + [":ic-sns-audit"],
deps = DEPENDENCIES + [
":ic-sns-audit",
"@crate_index//:anyhow",
],
)
4 changes: 4 additions & 0 deletions rs/sns/audit/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@ name = "ic_sns_audit"
path = "src/lib.rs"

[dependencies]
anyhow = { workspace = true }
candid = { workspace = true }
colored = "2.0.0"
csv = "1.1"
ic-agent = { workspace = true }
ic-base-types = { path = "../../types/base_types" }
ic-nervous-system-agent = { path = "../../nervous_system/agent" }
ic-nervous-system-common-test-keys = { path = "../../nervous_system/common/test_keys" }
ic-neurons-fund = { path = "../../nervous_system/neurons_fund" }
ic-nns-common = { path = "../../nns/common" }
Expand All @@ -30,4 +33,5 @@ rust_decimal = { version = "1.25" }
serde = { workspace = true }
serde_json = { workspace = true }
textplots = { version = "0.8" }
thiserror = { workspace = true }
tokio = { workspace = true }
Loading
Loading