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): Move upgrade-steps-related functions to their own file #2836

Merged
merged 1 commit into from
Nov 27, 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
84 changes: 84 additions & 0 deletions rs/sns/governance/src/cached_upgrade_steps.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
use crate::governance::Governance;
use crate::governance::UPGRADE_STEPS_INTERVAL_REFRESH_BACKOFF_SECONDS;
use crate::logs::ERROR;
use crate::pb::v1::governance::CachedUpgradeSteps as CachedUpgradeStepsPb;
use crate::pb::v1::governance::Version;
use crate::pb::v1::governance::Versions;
use crate::pb::v1::upgrade_journal_entry;
use crate::sns_upgrade::SnsCanisterType;
use ic_canister_log::log;

#[derive(Clone, Debug, PartialEq)]
pub(crate) struct CachedUpgradeSteps {
Expand Down Expand Up @@ -284,3 +289,82 @@ impl CachedUpgradeSteps {
Ok(())
}
}

impl Governance {
pub fn temporarily_lock_refresh_cached_upgrade_steps(&mut self) {
let upgrade_steps =
self.proto
.cached_upgrade_steps
.get_or_insert_with(|| CachedUpgradeStepsPb {
requested_timestamp_seconds: Some(self.env.now()),
..Default::default()
});
upgrade_steps.requested_timestamp_seconds = Some(self.env.now());
}

pub fn should_refresh_cached_upgrade_steps(&mut self) -> bool {
let now = self.env.now();

if let Some(ref cached_upgrade_steps) = self.proto.cached_upgrade_steps {
let requested_timestamp_seconds = cached_upgrade_steps
.requested_timestamp_seconds
.unwrap_or(0);
if now - requested_timestamp_seconds < UPGRADE_STEPS_INTERVAL_REFRESH_BACKOFF_SECONDS {
return false;
}
}

true
}

/// Refreshes the cached_upgrade_steps field
pub async fn refresh_cached_upgrade_steps(&mut self) {
let Some(deployed_version) = self.proto.deployed_version.as_ref() else {
log!(
ERROR,
"Cannot refresh cached_upgrade_steps: deployed_version not set."
);
return;
};
let sns_governance_canister_id = self.env.canister_id().get();

let upgrade_steps = crate::sns_upgrade::get_upgrade_steps(
&*self.env,
deployed_version.clone(),
sns_governance_canister_id,
)
.await;

let upgrade_steps = match upgrade_steps {
Ok(upgrade_steps) => upgrade_steps,
Err(err) => {
log!(
ERROR,
"Cannot refresh cached_upgrade_steps: call to SNS-W failed: {}",
err
);
return;
}
};
let upgrade_steps = Versions {
versions: upgrade_steps,
};

// Ensure `cached_upgrade_steps` is initialized
let cached_upgrade_steps = self
.proto
.cached_upgrade_steps
.get_or_insert_with(Default::default);

// Update `response_timestamp_seconds`
cached_upgrade_steps.response_timestamp_seconds = Some(self.env.now());

// Refresh the upgrade steps if they have changed
if cached_upgrade_steps.upgrade_steps != Some(upgrade_steps.clone()) {
cached_upgrade_steps.upgrade_steps = Some(upgrade_steps.clone());
self.push_to_upgrade_journal(upgrade_journal_entry::UpgradeStepsRefreshed::new(
upgrade_steps.versions,
));
}
}
}
77 changes: 0 additions & 77 deletions rs/sns/governance/src/governance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5056,83 +5056,6 @@ impl Governance {
self.upgrade_periodic_task_lock = None;
}

pub fn temporarily_lock_refresh_cached_upgrade_steps(&mut self) {
let upgrade_steps =
self.proto
.cached_upgrade_steps
.get_or_insert_with(|| CachedUpgradeStepsPb {
requested_timestamp_seconds: Some(self.env.now()),
..Default::default()
});
upgrade_steps.requested_timestamp_seconds = Some(self.env.now());
}

pub fn should_refresh_cached_upgrade_steps(&mut self) -> bool {
let now = self.env.now();

if let Some(ref cached_upgrade_steps) = self.proto.cached_upgrade_steps {
let requested_timestamp_seconds = cached_upgrade_steps
.requested_timestamp_seconds
.unwrap_or(0);
if now - requested_timestamp_seconds < UPGRADE_STEPS_INTERVAL_REFRESH_BACKOFF_SECONDS {
return false;
}
}

true
}

/// Refreshes the cached_upgrade_steps field
pub async fn refresh_cached_upgrade_steps(&mut self) {
let Some(deployed_version) = self.proto.deployed_version.as_ref() else {
log!(
ERROR,
"Cannot refresh cached_upgrade_steps: deployed_version not set."
);
return;
};
let sns_governance_canister_id = self.env.canister_id().get();

let upgrade_steps = crate::sns_upgrade::get_upgrade_steps(
&*self.env,
deployed_version.clone(),
sns_governance_canister_id,
)
.await;

let upgrade_steps = match upgrade_steps {
Ok(upgrade_steps) => upgrade_steps,
Err(err) => {
log!(
ERROR,
"Cannot refresh cached_upgrade_steps: call to SNS-W failed: {}",
err
);
return;
}
};
let upgrade_steps = Versions {
versions: upgrade_steps,
};

// Ensure `cached_upgrade_steps` is initialized
let cached_upgrade_steps = self
.proto
.cached_upgrade_steps
.get_or_insert_with(Default::default);

// Update `response_timestamp_seconds`
cached_upgrade_steps.response_timestamp_seconds = Some(self.env.now());

// Refresh the upgrade steps if they have changed
if cached_upgrade_steps.upgrade_steps != Some(upgrade_steps.clone()) {
cached_upgrade_steps.upgrade_steps = Some(upgrade_steps.clone());
self.push_to_upgrade_journal(upgrade_journal_entry::UpgradeStepsRefreshed::new(
upgrade_steps.versions,
));
}
}

pub fn get_upgrade_journal(&self) -> GetUpgradeJournalResponse {
let cached_upgrade_steps = self.proto.cached_upgrade_steps.clone();
match cached_upgrade_steps {
Expand Down