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 canisters workflow #109

Merged
merged 2 commits into from
Dec 11, 2024
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
52 changes: 45 additions & 7 deletions src/canister/upgrade_user_token_sns_canister.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use ic_agent::Agent;
use ic_sns_governance::init::GovernanceCanisterInitPayloadBuilder;
use k256::elliptic_curve::rand_core::le;
use serde::{Deserialize, Serialize};
use std::{error::Error, sync::Arc, vec};
use std::{error::Error, sync::Arc, time::Duration, vec};
use yral_canisters_client::{
individual_user_template::{DeployedCdaoCanisters, IndividualUserTemplate},
platform_orchestrator::{self, PlatformOrchestrator},
Expand Down Expand Up @@ -191,6 +191,28 @@ pub async fn setup_sns_canisters_of_a_user_canister_for_upgrade(
Ok(())
}

pub async fn verify_if_proposal_executed_successfully_impl(
agent: &Agent,
qstash_client: &QStashClient,
verify_proposal_request: VerifyUpgradeProposalRequest,
) -> Result<bool, Box<dyn Error + Send + Sync>> {
let sns_governance = SnsGovernance(verify_proposal_request.sns_canisters.governance, agent);

let proposal_executed_successfully = check_if_the_proposal_executed_successfully(
&sns_governance,
verify_proposal_request.proposal_id,
)
.await?;

if proposal_executed_successfully {
qstash_client
.upgrade_sns_creator_dao_canister(verify_proposal_request.sns_canisters)
.await?;
}

Ok(proposal_executed_successfully)
}

async fn upgrade_sns_governance_canister_with_custom_wasm(
agent: &Agent,
governance_canister_id: Principal,
Expand Down Expand Up @@ -219,6 +241,9 @@ async fn upgrade_sns_governance_canister_with_custom_wasm(
.start_canister(&governance_canister_id)
.await?;

//wait for the canister to startup
tokio::time::sleep(Duration::from_secs(5)).await;

upgrade_result
}

Expand Down Expand Up @@ -405,7 +430,7 @@ pub async fn is_upgrade_required(
.deployed_version
.ok_or("deployed version not found")?;

let result = check_if_version_matches_deployed_canister_version(deployed_version);
let result = !check_if_version_matches_deployed_canister_version(deployed_version);

Ok(result)
}
Expand Down Expand Up @@ -476,11 +501,16 @@ pub async fn recharge_canisters(

pub async fn upgrade_user_token_sns_canister_impl(
agent: &Agent,
governance_canister_id: String,
) -> Result<u64, Box<dyn Error + Send + Sync>> {
let governance_canister_id_principal = Principal::from_text(governance_canister_id)?;
qstash_client: &QStashClient,
sns_canisters: SnsCanisters,
) -> Result<(), Box<dyn Error + Send + Sync>> {
let sns_governance = SnsGovernance(sns_canisters.governance, agent);

let is_upgrade_required = is_upgrade_required(&sns_governance).await?;

let sns_governance = SnsGovernance(governance_canister_id_principal, agent);
if !is_upgrade_required {
return Ok(());
}

let neuron_list = sns_governance
.list_neurons(ListNeurons {
Expand Down Expand Up @@ -516,7 +546,15 @@ pub async fn upgrade_user_token_sns_canister_impl(
if let Command1::MakeProposal(proposal_id) = proposal_id {
let proposal_id_u64 = proposal_id.proposal_id.ok_or("proposal id not found")?.id;

Ok(proposal_id_u64)
let verify_request = VerifyUpgradeProposalRequest {
sns_canisters: sns_canisters,
proposal_id: proposal_id_u64,
};

qstash_client
.verify_sns_canister_upgrade_proposal(verify_request)
.await?;
Ok(())
} else {
Err(format!("{:?}", proposal_id).into())
}
Expand Down
56 changes: 13 additions & 43 deletions src/qstash/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ use crate::{
check_if_the_proposal_executed_successfully, is_upgrade_required,
setup_sns_canisters_of_a_user_canister_for_upgrade,
upgrade_user_token_sns_canister_for_entire_network_impl,
upgrade_user_token_sns_canister_impl, SnsCanisters, VerifyUpgradeProposalRequest,
upgrade_user_token_sns_canister_impl, verify_if_proposal_executed_successfully_impl,
SnsCanisters, VerifyUpgradeProposalRequest,
},
consts::ICP_LEDGER_CANISTER_ID,
events::{
Expand Down Expand Up @@ -311,26 +312,14 @@ async fn upgrade_sns_creator_dao_canister(
State(state): State<Arc<AppState>>,
Json(req): Json<SnsCanisters>,
) -> Result<Response, StatusCode> {
let governance_canister_id = req.governance.to_text();

let result =
upgrade_user_token_sns_canister_impl(&state.agent, governance_canister_id.clone()).await;
upgrade_user_token_sns_canister_impl(&state.agent, &state.qstash_client, req).await;

match result {
Ok(proposal_id) => {
let verify_request = VerifyUpgradeProposalRequest {
sns_canisters: req,
proposal_id,
};
state
.qstash_client
.verify_sns_canister_upgrade_proposal(verify_request)
.await
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;

Ok(()) => {
let response = Response::builder()
.status(StatusCode::OK)
.body(format!("upgrade proposal id: {} submitted", proposal_id).into())
.body(format!("upgrade proposal id submitted").into())
.unwrap();

Ok(response)
Expand All @@ -350,37 +339,18 @@ async fn verify_sns_canister_upgrade_proposal(
State(state): State<Arc<AppState>>,
Json(verify_sns_canister_proposal_request): Json<VerifyUpgradeProposalRequest>,
) -> Result<Response, StatusCode> {
let governance_canister_principal = verify_sns_canister_proposal_request
.sns_canisters
.governance;

let sns_governance = SnsGovernance(governance_canister_principal, &state.agent);

let result = check_if_the_proposal_executed_successfully(
&sns_governance,
verify_sns_canister_proposal_request.proposal_id,
let result = verify_if_proposal_executed_successfully_impl(
&state.agent,
&state.qstash_client,
verify_sns_canister_proposal_request,
)
.await;

let is_upgrade_required = is_upgrade_required(&sns_governance).await.unwrap_or(true);

match result {
Ok(executed) if executed => {
if is_upgrade_required {
state
.qstash_client
.upgrade_sns_creator_dao_canister(
verify_sns_canister_proposal_request.sns_canisters,
)
.await
.map_err(|_e| StatusCode::INTERNAL_SERVER_ERROR)?;
}

Ok(Response::builder()
.status(StatusCode::OK)
.body("Proposal executed successfully".into())
.unwrap())
}
Ok(executed) if executed => Ok(Response::builder()
.status(StatusCode::OK)
.body("Proposal executed successfully".into())
.unwrap()),
Ok(_) => Err(StatusCode::BAD_REQUEST),

Err(e) => Ok(Response::builder()
Expand Down
Loading