Skip to content

Commit

Permalink
refactor(nervous-system): Move Request implementations from canister …
Browse files Browse the repository at this point in the history
…crates to rs/nervous_system/agent (#3657)

This PR moves Request implementations from canister crates to
rs/nervous_system/agent.

This avoids the possibility of cyclic dependencies in case canister
tests will need to use the agents.

Additional changes entailed by this refactoring: 
1. rs/nervous_system/agent now depends on SNS Gov API crate, not the
internal one.
2. Similar for rs/sns/cli/

---------

Co-authored-by: Andre Popovitch <andre@popovit.ch>
  • Loading branch information
aterga and anchpop authored Jan 28, 2025
1 parent 215a697 commit ae3ab5a
Show file tree
Hide file tree
Showing 53 changed files with 922 additions and 438 deletions.
5 changes: 3 additions & 2 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion rs/nervous_system/agent/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ DEPENDENCIES = [
"//rs/nns/governance/api",
"//rs/nns/sns-wasm",
"//rs/registry/canister",
"//rs/sns/governance",
"//rs/sns/governance/api",
"//rs/sns/root",
"//rs/sns/swap",
"//rs/types/base_types",
Expand Down
2 changes: 1 addition & 1 deletion rs/nervous_system/agent/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ ic-nns-governance-api = { path = "../../nns/governance/api" }
cycles-minting-canister = { path = "../../nns/cmc" }
ic-nns-constants = { path = "../../nns/constants" }
ic-sns-wasm = { path = "../../nns/sns-wasm" }
ic-sns-governance = { path = "../../sns/governance" }
ic-sns-governance-api = { path = "../../sns/governance/api" }
pocket-ic = { path = "../../../packages/pocket-ic" }
registry-canister = { path = "../../registry/canister" }
ic-sns-root = { path = "../../sns/root" }
Expand Down
24 changes: 12 additions & 12 deletions rs/nervous_system/agent/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,19 @@ pub trait Request: Send {
type Response: CandidType + DeserializeOwned;
}

impl<R: ic_nervous_system_clients::Request> Request for R {
fn method(&self) -> &'static str {
Self::METHOD
}
fn update(&self) -> bool {
Self::UPDATE
}
fn payload(&self) -> Result<Vec<u8>, candid::Error> {
candid::encode_one(self)
}
// impl<R: ic_nervous_system_clients::Request> Request for R {
// fn method(&self) -> &'static str {
// Self::METHOD
// }
// fn update(&self) -> bool {
// Self::UPDATE
// }
// fn payload(&self) -> Result<Vec<u8>, candid::Error> {
// candid::encode_one(self)
// }

type Response = <Self as ic_nervous_system_clients::Request>::Response;
}
// type Response = <Self as ic_nervous_system_clients::Request>::Response;
// }

pub trait CallCanisters: sealed::Sealed {
type Error: Display + Send + std::error::Error + 'static;
Expand Down
65 changes: 5 additions & 60 deletions rs/nervous_system/agent/src/management_canister/mod.rs
Original file line number Diff line number Diff line change
@@ -1,45 +1,14 @@
use candid::{CandidType, Principal};
use candid::Principal;
use ic_base_types::CanisterId;
use ic_nervous_system_clients::Request;
use serde::Deserialize;
use std::collections::BTreeSet;

use crate::CallCanisters;
use std::collections::BTreeSet;

pub const CHUNK_SIZE: usize = 1024 * 1024; // 1 MiB

// ```candid
// type upload_chunk_args = record {
// canister_id : principal;
// chunk : blob;
// };
// ```
#[derive(CandidType, Deserialize, Debug, Clone)]
struct UploadChunkArgs {
pub canister_id: Principal,
pub chunk: Vec<u8>,
}

// ```candid
// type chunk_hash = record {
// hash : blob;
// };
// ```
#[derive(CandidType, Deserialize, Debug, Clone)]
pub struct ChunkHash {
pub hash: Vec<u8>,
}
pub mod requests;

// ```candid
// type upload_chunk_result = chunk_hash;
// ```
type UploadChunksResult = ChunkHash;
use requests::*;

impl Request for UploadChunkArgs {
type Response = UploadChunksResult;
const METHOD: &'static str = "upload_chunk";
const UPDATE: bool = true;
}
pub const CHUNK_SIZE: usize = 1024 * 1024; // 1 MiB

async fn upload_chunk<C: CallCanisters>(
agent: &C,
Expand All @@ -59,30 +28,6 @@ async fn upload_chunk<C: CallCanisters>(
Ok(response)
}

// ```candid
// type stored_chunks_args = record {
// canister_id : canister_id;
// };
// ```
#[derive(CandidType, Deserialize, Debug, Clone)]
struct StoredChunksArgs {
pub canister_id: Principal,
}

// ```
// type chunk_hash = record {
// hash : blob;
// };
// type stored_chunks_result = vec chunk_hash;
// ```
type StoredChunksResult = Vec<ChunkHash>;

impl Request for StoredChunksArgs {
type Response = StoredChunksResult;
const METHOD: &'static str = "stored_chunks";
const UPDATE: bool = true;
}

pub async fn stored_chunks<C: CallCanisters>(
agent: &C,
store_canister_id: CanisterId,
Expand Down
80 changes: 80 additions & 0 deletions rs/nervous_system/agent/src/management_canister/requests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
use crate::Request;
use candid::{CandidType, Principal};
use serde::Deserialize;

// ```candid
// type upload_chunk_args = record {
// canister_id : principal;
// chunk : blob;
// };
// ```
#[derive(CandidType, Deserialize, Debug, Clone)]
pub struct UploadChunkArgs {
pub canister_id: Principal,
pub chunk: Vec<u8>,
}

// ```candid
// type chunk_hash = record {
// hash : blob;
// };
// ```
#[derive(CandidType, Deserialize, Debug, Clone)]
pub struct ChunkHash {
pub hash: Vec<u8>,
}

// ```candid
// type upload_chunk_result = chunk_hash;
// ```
pub type UploadChunksResult = ChunkHash;

impl Request for UploadChunkArgs {
fn method(&self) -> &'static str {
"upload_chunk"
}

fn update(&self) -> bool {
true
}

fn payload(&self) -> Result<Vec<u8>, candid::Error> {
candid::encode_one(self)
}

type Response = UploadChunksResult;
}

// ```candid
// type stored_chunks_args = record {
// canister_id : canister_id;
// };
// ```
#[derive(CandidType, Deserialize, Debug, Clone)]
pub struct StoredChunksArgs {
pub canister_id: Principal,
}

// ```
// type chunk_hash = record {
// hash : blob;
// };
// type stored_chunks_result = vec chunk_hash;
// ```
pub type StoredChunksResult = Vec<ChunkHash>;

impl Request for StoredChunksArgs {
fn method(&self) -> &'static str {
"stored_chunks"
}

fn update(&self) -> bool {
true
}

fn payload(&self) -> Result<Vec<u8>, candid::Error> {
candid::encode_one(self)
}

type Response = StoredChunksResult;
}
2 changes: 2 additions & 0 deletions rs/nervous_system/agent/src/nns/cmc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ use cycles_minting_canister::{CanisterSettingsArgs, CreateCanister, SubnetSelect
use ic_base_types::CanisterId;
use ic_nns_constants::CYCLES_MINTING_CANISTER_ID;

pub mod requests;

pub async fn create_canister<C: CallCanisters>(
agent: &C,
subnet_selection: Option<SubnetSelection>,
Expand Down
18 changes: 18 additions & 0 deletions rs/nervous_system/agent/src/nns/cmc/requests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use crate::Request;
use cycles_minting_canister::{CreateCanister, CreateCanisterResult};

impl Request for CreateCanister {
fn method(&self) -> &'static str {
"create_canister"
}

fn update(&self) -> bool {
true
}

fn payload(&self) -> Result<Vec<u8>, candid::Error> {
candid::encode_one(self)
}

type Response = CreateCanisterResult;
}
2 changes: 2 additions & 0 deletions rs/nervous_system/agent/src/nns/governance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ use ic_nns_governance_api::pb::v1::{
GetNeuronsFundAuditInfoRequest, GetNeuronsFundAuditInfoResponse,
};

pub mod requests;

pub async fn get_neurons_fund_audit_info<C: CallCanisters>(
agent: &C,
nns_proposal_id: ProposalId,
Expand Down
20 changes: 20 additions & 0 deletions rs/nervous_system/agent/src/nns/governance/requests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use crate::Request;
use ic_nns_governance_api::pb::v1::{
GetNeuronsFundAuditInfoRequest, GetNeuronsFundAuditInfoResponse,
};

impl Request for GetNeuronsFundAuditInfoRequest {
fn method(&self) -> &'static str {
"get_neurons_fund_audit_info"
}

fn update(&self) -> bool {
false
}

fn payload(&self) -> Result<Vec<u8>, candid::Error> {
candid::encode_one(self)
}

type Response = GetNeuronsFundAuditInfoResponse;
}
3 changes: 2 additions & 1 deletion rs/nervous_system/agent/src/nns/registry.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use crate::CallCanisters;
use ic_base_types::{CanisterId, SubnetId};
use ic_nns_constants::REGISTRY_CANISTER_ID;
use registry_canister::pb::v1::GetSubnetForCanisterRequest;

use crate::CallCanisters;
pub mod requests;

pub async fn get_subnet_for_canister<C: CallCanisters>(
agent: &C,
Expand Down
18 changes: 18 additions & 0 deletions rs/nervous_system/agent/src/nns/registry/requests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use crate::Request;
use registry_canister::pb::v1::{GetSubnetForCanisterRequest, SubnetForCanister};

impl Request for GetSubnetForCanisterRequest {
fn method(&self) -> &'static str {
"get_subnet_for_canister"
}

fn update(&self) -> bool {
false
}

fn payload(&self) -> Result<Vec<u8>, candid::Error> {
candid::encode_one(self)
}

type Response = Result<SubnetForCanister, String>;
}
4 changes: 2 additions & 2 deletions rs/nervous_system/agent/src/nns/sns_wasm.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::sns::Sns;
use crate::CallCanisters;
use anyhow::anyhow;
use ic_agent::Agent;
use ic_nns_constants::SNS_WASM_CANISTER_ID;
Expand All @@ -12,8 +13,7 @@ use tokio::fs::File;
use tokio::io::AsyncWriteExt;
use tokio::io::BufWriter;
use tokio::process::Command;

use crate::CallCanisters;
pub mod requests;

pub async fn query_mainline_sns_upgrade_steps<C: CallCanisters>(
agent: &C,
Expand Down
Loading

0 comments on commit ae3ab5a

Please sign in to comment.