Skip to content

Commit

Permalink
Move upgrade-steps related functions to their own file
Browse files Browse the repository at this point in the history
  • Loading branch information
anchpop committed Nov 26, 2024
1 parent 9c4eb1f commit 9f17274
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 77 deletions.
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

0 comments on commit 9f17274

Please sign in to comment.