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

feat(sns): Serve SNS upgrade journal over Http #2489

Merged
merged 3 commits into from
Nov 7, 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
15 changes: 15 additions & 0 deletions rs/nervous_system/common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -722,6 +722,21 @@ pub fn serve_metrics(
}
}

pub fn serve_journal<Journal>(journal: &Journal) -> HttpResponse
where
Journal: serde::Serialize,
{
match serde_json::to_string(journal) {
Err(err) => {
HttpResponseBuilder::server_error(format!("Failed to encode journal: {}", err)).build()
}
Ok(body) => HttpResponseBuilder::ok()
.header("Content-Type", "application/json")
.with_body_and_content_length(body)
.build(),
}
}

/// Returns the total amount of memory (heap, stable memory, etc) that the calling canister has allocated.
#[cfg(target_arch = "wasm32")]
pub fn total_memory_size_bytes() -> usize {
Expand Down
11 changes: 10 additions & 1 deletion rs/sns/governance/canister/canister.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use ic_nervous_system_clients::{
};
use ic_nervous_system_common::{
dfn_core_stable_mem_utils::{BufferedStableMemReader, BufferedStableMemWriter},
serve_logs, serve_logs_v2, serve_metrics,
serve_journal, serve_logs, serve_logs_v2, serve_metrics,
};
use ic_nervous_system_proto::pb::v1::{
GetTimersRequest, GetTimersResponse, ResetTimersRequest, ResetTimersResponse, Timers,
Expand Down Expand Up @@ -613,6 +613,15 @@ ic_nervous_system_common_build_metadata::define_get_build_metadata_candid_method
#[query(hidden = true, decoding_quota = 10000)]
pub fn http_request(request: HttpRequest) -> HttpResponse {
match request.path() {
"/journal/json" => {
let journal_entries = &governance()
.proto
.upgrade_journal
.as_ref()
.expect("The upgrade journal is not initialized for this SNS.")
.entries;
serve_journal(journal_entries)
}
"/metrics" => serve_metrics(encode_metrics),
"/logs" => serve_logs_v2(request, &INFO, &ERROR),

Expand Down
84 changes: 83 additions & 1 deletion rs/sns/governance/canister/tests.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
use std::collections::HashSet;

use super::*;
use assert_matches::assert_matches;
use candid_parser::utils::{service_equal, CandidSource};
use ic_sns_governance::pb::v1::{DisburseMaturityInProgress, Neuron};
use ic_sns_governance::pb::v1::{
governance::{Version, Versions},
upgrade_journal_entry::{Event, UpgradeStepsRefreshed},
DisburseMaturityInProgress, Neuron, UpgradeJournal, UpgradeJournalEntry,
};
use maplit::btreemap;

/// This is NOT affected by
Expand Down Expand Up @@ -105,3 +112,78 @@ fn test_populate_finalize_disbursement_timestamp_seconds() {
};
assert_eq!(governance_proto, expected_governance_proto);
}

#[test]
fn test_upgrade_journal() {
let journal = UpgradeJournal {
entries: vec![UpgradeJournalEntry {
timestamp_seconds: Some(1000),
event: Some(Event::UpgradeStepsRefreshed(UpgradeStepsRefreshed {
upgrade_steps: Some(Versions {
versions: vec![Version {
root_wasm_hash: vec![0, 0, 0, 0],
governance_wasm_hash: vec![0, 0, 0, 1],
swap_wasm_hash: vec![0, 0, 0, 2],
index_wasm_hash: vec![0, 0, 0, 4],
ledger_wasm_hash: vec![0, 0, 0, 5],
archive_wasm_hash: vec![0, 0, 0, 6],
}],
}),
})),
}],
};

// Currently, the `/journal` Http endpoint serves the entries directly, rather than the whole
// journal object.
let http_response = serve_journal(&journal.entries);
let expected_headers: HashSet<(_, _)> = HashSet::from_iter([
("Content-Type".to_string(), "application/json".to_string()),
("Content-Length".to_string(), "271".to_string()),
]);
let (observed_headers, observed_body) = assert_matches!(
http_response,
HttpResponse {
status_code: 200,
headers,
body
} => (headers, body)
);

let observed_headers = HashSet::from_iter(observed_headers);

assert!(
expected_headers.is_subset(&observed_headers),
"{:?} is expected to be a subset of {:?}",
expected_headers,
observed_headers
);

let observed_journal_str = std::str::from_utf8(&observed_body).unwrap();

assert_eq!(
observed_journal_str,
r#"[
{
"timestamp_seconds": 1000,
"event": {
"UpgradeStepsRefreshed": {
"upgrade_steps": {
"versions": [
{
"root_wasm_hash": [0,0,0,0],
"governance_wasm_hash": [0,0,0,1],
"ledger_wasm_hash": [0,0,0,5],
"swap_wasm_hash": [0,0,0,2],
"archive_wasm_hash": [0,0,0,6],
"index_wasm_hash": [0,0,0,4]
}
]
}
}
}
}
]"#
.replace(" ", "")
.replace("\n", "")
);
}
20 changes: 20 additions & 0 deletions rs/sns/governance/protobuf_generator/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,26 @@ pub fn generate_prost_files(proto: ProtoPaths<'_>, out: &Path) {
"#[derive(strum_macros::EnumIter)]",
vec!["Governance.Mode", "NeuronPermissionType", "Proposal.action"],
);
apply_attribute(
"#[derive(serde::Serialize)]",
vec![
"Empty",
"ProposalId",
"UpgradeJournal",
"Governance.Version",
"Governance.Versions",
"UpgradeJournalEntry",
"UpgradeJournalEntry.event",
"UpgradeJournalEntry.UpgradeStepsRefreshed",
"UpgradeJournalEntry.TargetVersionSet",
"UpgradeJournalEntry.TargetVersionReset",
"UpgradeJournalEntry.UpgradeStarted",
"UpgradeJournalEntry.UpgradeStarted.reason",
"UpgradeJournalEntry.UpgradeOutcome",
"UpgradeJournalEntry.UpgradeOutcome.status",
"UpgradeJournalEntry.UpgradeOutcome.InvalidState",
],
);
apply_attribute(
"#[self_describing]",
vec!["ProposalId", "Motion", "Ballot", "Tally"],
Expand Down
16 changes: 15 additions & 1 deletion rs/sns/governance/src/gen/ic_sns_governance.pb.v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ pub struct NeuronIds {
pub neuron_ids: ::prost::alloc::vec::Vec<NeuronId>,
}
/// The id of a specific proposal.
#[derive(candid::CandidType, candid::Deserialize, comparable::Comparable)]
#[derive(candid::CandidType, candid::Deserialize, comparable::Comparable, serde::Serialize)]
#[self_describing]
#[derive(Clone, Copy, PartialEq, ::prost::Message)]
pub struct ProposalId {
Expand Down Expand Up @@ -1835,6 +1835,7 @@ pub mod governance {
candid::CandidType,
candid::Deserialize,
comparable::Comparable,
serde::Serialize,
Clone,
PartialEq,
::prost::Message,
Expand Down Expand Up @@ -1869,6 +1870,7 @@ pub mod governance {
candid::CandidType,
candid::Deserialize,
comparable::Comparable,
serde::Serialize,
Clone,
PartialEq,
::prost::Message,
Expand Down Expand Up @@ -2141,6 +2143,7 @@ pub struct FailStuckUpgradeInProgressResponse {}
candid::CandidType,
candid::Deserialize,
comparable::Comparable,
serde::Serialize,
Clone,
Copy,
PartialEq,
Expand Down Expand Up @@ -3370,6 +3373,7 @@ pub struct AdvanceTargetVersionResponse {}
candid::CandidType,
candid::Deserialize,
comparable::Comparable,
serde::Serialize,
Clone,
PartialEq,
::prost::Message,
Expand All @@ -3386,6 +3390,7 @@ pub mod upgrade_journal_entry {
candid::CandidType,
candid::Deserialize,
comparable::Comparable,
serde::Serialize,
Clone,
PartialEq,
::prost::Message,
Expand All @@ -3398,6 +3403,7 @@ pub mod upgrade_journal_entry {
candid::CandidType,
candid::Deserialize,
comparable::Comparable,
serde::Serialize,
Clone,
PartialEq,
::prost::Message,
Expand All @@ -3412,6 +3418,7 @@ pub mod upgrade_journal_entry {
candid::CandidType,
candid::Deserialize,
comparable::Comparable,
serde::Serialize,
Clone,
PartialEq,
::prost::Message,
Expand All @@ -3426,6 +3433,7 @@ pub mod upgrade_journal_entry {
candid::CandidType,
candid::Deserialize,
comparable::Comparable,
serde::Serialize,
Clone,
PartialEq,
::prost::Message,
Expand All @@ -3444,6 +3452,7 @@ pub mod upgrade_journal_entry {
candid::CandidType,
candid::Deserialize,
comparable::Comparable,
serde::Serialize,
Clone,
Copy,
PartialEq,
Expand All @@ -3460,6 +3469,7 @@ pub mod upgrade_journal_entry {
candid::CandidType,
candid::Deserialize,
comparable::Comparable,
serde::Serialize,
Clone,
PartialEq,
::prost::Message,
Expand All @@ -3476,6 +3486,7 @@ pub mod upgrade_journal_entry {
candid::CandidType,
candid::Deserialize,
comparable::Comparable,
serde::Serialize,
Clone,
PartialEq,
::prost::Message,
Expand All @@ -3488,6 +3499,7 @@ pub mod upgrade_journal_entry {
candid::CandidType,
candid::Deserialize,
comparable::Comparable,
serde::Serialize,
Clone,
PartialEq,
::prost::Oneof,
Expand All @@ -3508,6 +3520,7 @@ pub mod upgrade_journal_entry {
candid::CandidType,
candid::Deserialize,
comparable::Comparable,
serde::Serialize,
Clone,
PartialEq,
::prost::Oneof,
Expand All @@ -3530,6 +3543,7 @@ pub mod upgrade_journal_entry {
candid::CandidType,
candid::Deserialize,
comparable::Comparable,
serde::Serialize,
Clone,
PartialEq,
::prost::Message,
Expand Down