Skip to content

Commit

Permalink
Merge branch 'master' into revert_stack
Browse files Browse the repository at this point in the history
  • Loading branch information
julianjelfs authored Dec 13, 2024
2 parents 7aae353 + b32e38a commit 8246dc3
Show file tree
Hide file tree
Showing 11 changed files with 171 additions and 103 deletions.
1 change: 1 addition & 0 deletions backend/canisters/escrow/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
- Make `ChannelId` comparisons use their 32bit representation ([#6885](https://github.com/open-chat-labs/open-chat/pull/6885))
- Expose size of each virtual stable memory in metrics ([#6981](https://github.com/open-chat-labs/open-chat/pull/6981))
- Include the ledger canister Id in transfer failed error logs ([#7011](https://github.com/open-chat-labs/open-chat/pull/7011))
- Expose transfer errors that occur during swaps ([#7055](https://github.com/open-chat-labs/open-chat/pull/7055))

## [[2.0.1316](https://github.com/open-chat-labs/open-chat/releases/tag/v2.0.1316-escrow)] - 2024-09-02

Expand Down
100 changes: 53 additions & 47 deletions backend/canisters/escrow/impl/src/jobs/make_pending_payments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use icrc_ledger_types::icrc1::transfer::TransferArg;
use ledger_utils::icrc1::make_transfer;
use std::cell::Cell;
use std::time::Duration;
use tracing::trace;
use tracing::{error, trace};
use types::icrc1::{Account, CompletedCryptoTransaction};

thread_local! {
Expand Down Expand Up @@ -51,56 +51,62 @@ async fn process_payment(pending_payment: PendingPayment) {
amount: pending_payment.amount.into(),
};

match make_transfer(pending_payment.token_info.ledger, &args, true).await {
let response = make_transfer(pending_payment.token_info.ledger, &args, true).await;

mutate_state(|state| match response {
Ok(Ok(block_index)) => {
mutate_state(|state| {
if let Some(swap) = state.data.swaps.get_mut(pending_payment.swap_id) {
let transfer = CompletedCryptoTransaction {
ledger: pending_payment.token_info.ledger,
token: pending_payment.token_info.token,
amount: pending_payment.amount,
from: Account {
owner: state.env.canister_id(),
subaccount: args.from_subaccount,
}
.into(),
to: Account::from(pending_payment.user_id).into(),
fee: pending_payment.token_info.fee,
memo: None,
created: created_at_time,
block_index,
};
let notify_status_change = match pending_payment.reason {
PendingPaymentReason::Swap(_) => {
if pending_payment.token_info.ledger == swap.token0.ledger {
swap.token0_transfer_out = Some(transfer);
} else {
swap.token1_transfer_out = Some(transfer);
}
swap.is_complete()
}
PendingPaymentReason::Refund => {
swap.refunds.push(transfer);
matches!(
swap.status(state.env.now()),
SwapStatus::Expired(_) | SwapStatus::Cancelled(_)
)
if let Some(swap) = state.data.swaps.get_mut(pending_payment.swap_id) {
let transfer = CompletedCryptoTransaction {
ledger: pending_payment.token_info.ledger,
token: pending_payment.token_info.token,
amount: pending_payment.amount,
from: Account {
owner: state.env.canister_id(),
subaccount: args.from_subaccount,
}
.into(),
to: Account::from(pending_payment.user_id).into(),
fee: pending_payment.token_info.fee,
memo: None,
created: created_at_time,
block_index,
};
let notify_status_change = match pending_payment.reason {
PendingPaymentReason::Swap(_) => {
if pending_payment.token_info.ledger == swap.token0.ledger {
swap.token0_transfer_out = Some(transfer);
} else {
swap.token1_transfer_out = Some(transfer);
}
};

if notify_status_change {
state.data.notify_status_change_queue.push(swap.id);
crate::jobs::notify_status_change::start_job_if_required(state);
swap.is_complete()
}
PendingPaymentReason::Refund => {
swap.refunds.push(transfer);
matches!(
swap.status(state.env.now()),
SwapStatus::Expired(_) | SwapStatus::Cancelled(_)
)
}
};

if notify_status_change {
state.data.notify_status_change_queue.push(swap.id);
crate::jobs::notify_status_change::start_job_if_required(state);
}
});
}
}
Ok(Err(_)) => {}
Err(_) => {
mutate_state(|state| {
state.data.pending_payments_queue.push(pending_payment);
start_job_if_required(state);
});
Ok(Err(error)) => {
error!(?error, ?args, "Failed to process payment");
if let Some(swap) = state.data.swaps.get_mut(pending_payment.swap_id) {
swap.errors.push(format!("Ledger returned an error: {error:?}"));
}
}
}
Err(error) => {
if let Some(swap) = state.data.swaps.get_mut(pending_payment.swap_id) {
swap.errors.push(format!("Failed to call into ledger: {error:?}"));
}
state.data.pending_payments_queue.push(pending_payment);
start_job_if_required(state);
}
});
}
3 changes: 3 additions & 0 deletions backend/canisters/escrow/impl/src/model/swaps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ pub struct Swap {
pub refunds: Vec<CompletedCryptoTransaction>,
pub additional_admins: Vec<Principal>,
pub canister_to_notify: Option<CanisterId>,
#[serde(default)]
pub errors: Vec<String>,
}

impl Swap {
Expand All @@ -88,6 +90,7 @@ impl Swap {
refunds: Vec::new(),
additional_admins: args.additional_admins,
canister_to_notify: args.canister_to_notify,
errors: Vec::new(),
}
}

Expand Down
13 changes: 13 additions & 0 deletions backend/canisters/escrow/impl/src/queries/http_request.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use crate::{read_state, RuntimeState};
use http_request::{build_json_response, encode_logs, extract_route, Route};
use ic_cdk::query;
use std::collections::HashMap;
use std::str::FromStr;
use types::{HttpRequest, HttpResponse, TimestampMillis};

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

fn get_swap_logs(qs: HashMap<String, String>, state: &RuntimeState) -> HttpResponse {
let swap_id = u32::from_str(qs.get("swap_id").unwrap()).unwrap();

if let Some(swap) = state.data.swaps.get(swap_id) {
build_json_response(swap)
} 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 == "swap_logs" => read_state(|state| get_swap_logs(qs, state)),
_ => HttpResponse::not_found(),
}
}
10 changes: 10 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,16 @@ 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))

### Changed

- Include the total cycles topped up ([#7056](https://github.com/open-chat-labs/open-chat/pull/7056))

## [[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,9 @@
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 serde::Serialize;
use std::collections::HashMap;
use types::{CanisterId, CyclesTopUpHumanReadable, HttpRequest, HttpResponse, TimestampMillis};

#[query]
fn http_request(request: HttpRequest) -> HttpResponse {
Expand All @@ -21,11 +23,37 @@ 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();

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

let total = top_ups.iter().map(|c| c.amount).sum::<u128>() as f64 / 1_000_000_000_000f64;

build_json_response(&TopUps {
total,
top_ups: top_ups.iter().map(|c| c.into()).collect(),
})
}

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(),
}
}

#[derive(Serialize)]
struct TopUps {
total: f64,
top_ups: Vec<CyclesTopUpHumanReadable>,
}
5 changes: 5 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 All @@ -15,6 +19,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
- Switch to using `PrincipalToStableMemoryMap` ([#7023](https://github.com/open-chat-labs/open-chat/pull/7023))
- Make `MessageId` comparisons use their 64bit representation ([#7030](https://github.com/open-chat-labs/open-chat/pull/7030))
- Notify CHIT updates via LocalUserIndex ([#7033](https://github.com/open-chat-labs/open-chat/pull/7033))
- Include the total cycles topped up ([#7056](https://github.com/open-chat-labs/open-chat/pull/7056))

### Fixes

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, CyclesTopUpHumanReadable, HttpRequest, HttpResponse, TimestampMillis, UserId};

#[query]
fn http_request(request: HttpRequest) -> HttpResponse {
Expand All @@ -24,6 +24,21 @@ 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();
};

let total = user.cycle_top_ups.iter().map(|c| c.amount).sum::<u128>() as f64 / 1_000_000_000_000f64;

build_json_response(&TopUps {
total,
top_ups: user.cycle_top_ups.iter().map(|c| c.into()).collect(),
})
}

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 +75,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 All @@ -72,3 +88,9 @@ struct UserCanisterVersion {
count: u32,
users: Vec<UserId>,
}

#[derive(Serialize)]
struct TopUps {
total: f64,
top_ups: Vec<CyclesTopUpHumanReadable>,
}
13 changes: 0 additions & 13 deletions backend/libraries/constants/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,6 @@ pub const SNS_ROOT_CANISTER_ID: CanisterId = Principal::from_slice(&[0, 0, 0, 0,
pub const SNS_GOVERNANCE_CANISTER_ID: CanisterId = Principal::from_slice(&[0, 0, 0, 0, 2, 0, 0, 24, 1, 1]);
pub const SNS_LEDGER_CANISTER_ID: CanisterId = Principal::from_slice(&[0, 0, 0, 0, 2, 0, 0, 25, 1, 1]);

pub const DEV_TEAM_DFX_PRINCIPAL: CanisterId = Principal::from_slice(&[
143, 216, 195, 195, 27, 134, 102, 49, 184, 154, 196, 117, 143, 40, 192, 164, 121, 209, 89, 30, 45, 18, 30, 32, 92, 106,
138, 30, 2,
]);

pub const IC_ROOT_KEY: &[u8; 133] = b"\x30\x81\x82\x30\x1d\x06\x0d\x2b\x06\x01\x04\x01\x82\xdc\x7c\x05\x03\x01\x02\x01\x06\x0c\x2b\x06\x01\x04\x01\x82\xdc\x7c\x05\x03\x02\x01\x03\x61\x00\x81\x4c\x0e\x6e\xc7\x1f\xab\x58\x3b\x08\xbd\x81\x37\x3c\x25\x5c\x3c\x37\x1b\x2e\x84\x86\x3c\x98\xa4\xf1\xe0\x8b\x74\x23\x5d\x14\xfb\x5d\x9c\x0c\xd5\x46\xd9\x68\x5f\x91\x3a\x0c\x0b\x2c\xc5\x34\x15\x83\xbf\x4b\x43\x92\xe4\x67\xdb\x96\xd6\x5b\x9b\xb4\xcb\x71\x71\x12\xf8\x47\x2e\x0d\x5a\x4d\x14\x50\x5f\xfd\x74\x84\xb0\x12\x91\x09\x1c\x5f\x87\xb9\x88\x83\x46\x3f\x98\x09\x1a\x0b\xaa\xae";

pub const MEMO_MESSAGE: [u8; 6] = [0x4f, 0x43, 0x5f, 0x4d, 0x53, 0x47]; // OC_MSG
Expand Down Expand Up @@ -113,14 +108,6 @@ mod tests {
);
}

#[test]
fn dev_team_dfx_principal() {
assert_eq!(
DEV_TEAM_DFX_PRINCIPAL,
Principal::from_text("tu45y-p4p3d-b4gg4-gmyy3-rgweo-whsrq-fephi-vshrn-cipca-xdkri-pae").unwrap()
);
}

#[test]
fn openchat_treasury_canister_id() {
assert_eq!(
Expand Down
Loading

0 comments on commit 8246dc3

Please sign in to comment.