Skip to content

Commit

Permalink
Expose the cycles top ups of User/Group/Community canisters (#7053)
Browse files Browse the repository at this point in the history
  • Loading branch information
hpeebles authored Dec 12, 2024
1 parent d737061 commit 719d306
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 42 deletions.
6 changes: 6 additions & 0 deletions backend/canisters/local_group_index/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

## [unreleased]

### Added

- Expose the cycles top-ups of Group/Community canisters ([#7053](https://github.com/open-chat-labs/open-chat/pull/7053))

## [[2.0.1505](https://github.com/open-chat-labs/open-chat/releases/tag/v2.0.1505-local_group_index)] - 2024-12-09

### Changed

- Increase Windoge community canister's reserved cycles limit ([#7022](https://github.com/open-chat-labs/open-chat/pull/7022))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
use crate::lifecycle::{init_env, init_state};
use crate::memory::get_upgrades_memory;
use crate::{mutate_state, read_state, Data};
use crate::Data;
use canister_logger::LogEntry;
use canister_tracing_macros::trace;
use ic_cdk::api::management_canister::main::{CanisterSettings, UpdateSettingsArgument};
use ic_cdk::post_upgrade;
use local_group_index_canister::post_upgrade::Args;
use stable_memory::get_reader;
use std::time::Duration;
use tracing::info;
use types::{CanisterId, CyclesTopUp};
use utils::canister::deposit_cycles;
use utils::cycles::init_cycles_dispenser_client;

#[post_upgrade]
Expand All @@ -29,39 +25,4 @@ fn post_upgrade(args: Args) {
init_state(env, data, args.wasm_version);

info!(version = %args.wasm_version, "Post-upgrade complete");

ic_cdk_timers::set_timer(Duration::ZERO, || {
ic_cdk::spawn(increase_windoge_reserved_cycles_limit_public())
});
}

async fn increase_windoge_reserved_cycles_limit_public() {
let windoge_canister_id = CanisterId::from_text("ow6el-gyaaa-aaaar-av5na-cai").unwrap();

if read_state(|state| state.data.local_communities.get(&windoge_canister_id.into()).is_none()) {
return;
}

ic_cdk::api::management_canister::main::update_settings(UpdateSettingsArgument {
canister_id: windoge_canister_id,
settings: CanisterSettings {
reserved_cycles_limit: Some(20_000_000_000_000u128.into()),
..Default::default()
},
})
.await
.unwrap();

let amount = 20_000_000_000_000u128;
deposit_cycles(windoge_canister_id, amount).await.unwrap();

mutate_state(|state| {
state.data.local_communities.mark_cycles_top_up(
&windoge_canister_id.into(),
CyclesTopUp {
amount,
date: state.env.now(),
},
);
})
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use crate::{read_state, RuntimeState};
use http_request::{build_json_response, encode_logs, extract_route, Route};
use ic_cdk::query;
use types::{HttpRequest, HttpResponse, TimestampMillis};
use std::collections::HashMap;
use types::{CanisterId, HttpRequest, HttpResponse, TimestampMillis};

#[query]
fn http_request(request: HttpRequest) -> HttpResponse {
Expand All @@ -21,11 +22,24 @@ fn http_request(request: HttpRequest) -> HttpResponse {
build_json_response(&state.metrics())
}

fn get_top_ups(qs: HashMap<String, String>, state: &RuntimeState) -> HttpResponse {
let canister_id = CanisterId::from_text(qs.get("canister_id").unwrap()).unwrap();

if let Some(group) = state.data.local_groups.get(&canister_id.into()) {
build_json_response(&group.cycle_top_ups)
} else if let Some(community) = state.data.local_communities.get(&canister_id.into()) {
build_json_response(&community.cycle_top_ups)
} else {
HttpResponse::not_found()
}
}

match extract_route(&request.url) {
Route::Errors(since) => get_errors_impl(since),
Route::Logs(since) => get_logs_impl(since),
Route::Traces(since) => get_traces_impl(since),
Route::Metrics => read_state(get_metrics_impl),
Route::Other(p, qs) if p == "top_ups" => read_state(|state| get_top_ups(qs, state)),
_ => HttpResponse::not_found(),
}
}
4 changes: 4 additions & 0 deletions backend/canisters/local_user_index/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

## [unreleased]

### Added

- Expose the cycles top-ups of User canisters ([#7053](https://github.com/open-chat-labs/open-chat/pull/7053))

### Changed

- Expose size of each virtual stable memory in metrics ([#6981](https://github.com/open-chat-labs/open-chat/pull/6981))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use ic_cdk::query;
use serde::Serialize;
use std::collections::{BTreeMap, HashMap};
use std::str::FromStr;
use types::{BuildVersion, HttpRequest, HttpResponse, TimestampMillis, UserId};
use types::{BuildVersion, CanisterId, HttpRequest, HttpResponse, TimestampMillis, UserId};

#[query]
fn http_request(request: HttpRequest) -> HttpResponse {
Expand All @@ -24,6 +24,16 @@ fn http_request(request: HttpRequest) -> HttpResponse {
build_json_response(&state.metrics())
}

fn get_top_ups(qs: HashMap<String, String>, state: &RuntimeState) -> HttpResponse {
let user_id: UserId = CanisterId::from_text(qs.get("canister_id").unwrap()).unwrap().into();

let Some(user) = state.data.local_users.get(&user_id) else {
return HttpResponse::not_found();
};

build_json_response(&user.cycle_top_ups)
}

fn get_user_canister_versions(state: &RuntimeState) -> HttpResponse {
let mut map = BTreeMap::new();
for (user_id, user) in state.data.local_users.iter() {
Expand Down Expand Up @@ -60,6 +70,7 @@ fn http_request(request: HttpRequest) -> HttpResponse {
Route::Logs(since) => get_logs_impl(since),
Route::Traces(since) => get_traces_impl(since),
Route::Metrics => read_state(get_metrics_impl),
Route::Other(p, qs) if p == "top_ups" => read_state(|state| get_top_ups(qs, state)),
Route::Other(p, _) if p == "user_canister_versions" => read_state(get_user_canister_versions),
Route::Other(p, qs) if p == "remote_user_events" => read_state(|state| get_remote_user_events(qs, state)),
_ => HttpResponse::not_found(),
Expand Down

0 comments on commit 719d306

Please sign in to comment.