Skip to content

Commit

Permalink
Merge branch 'master' into sneed
Browse files Browse the repository at this point in the history
  • Loading branch information
hpeebles authored Feb 7, 2024
2 parents a5ff33d + 1bd7fb9 commit ee9f574
Show file tree
Hide file tree
Showing 193 changed files with 5,762 additions and 1,539 deletions.
10 changes: 6 additions & 4 deletions .github/workflows/pre_release_tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Wait for wasms to be built
uses: lewagon/[email protected]
with:
ref: master
check-name: "Push canister wasms"
repo-token: ${{ secrets.GITHUB_TOKEN }}
- name: Download canister wasms
run: ./scripts/download-all-canister-wasms.sh $GITHUB_REF_NAME
continue-on-error: true
- name: Build canister wasms
if: ${{ failure() }}
uses: ./.github/workflows/push_canister_wasms.yaml
- uses: actions-rs/toolchain@v1
with:
profile: minimal
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.dfx/
.vscode/
target/
dist/
node_modules/
Expand All @@ -15,4 +16,4 @@ msg.json
notes.md
notification.did
summary.md
temp.did
temp.did
27 changes: 0 additions & 27 deletions .vscode/settings.json

This file was deleted.

11 changes: 11 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions backend/canister_installer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ async fn install_service_canisters_impl(
escrow_canister_id: canister_ids.escrow,
nns_governance_canister_id: canister_ids.nns_governance,
internet_identity_canister_id: canister_ids.nns_internet_identity,
translations_canister_id: canister_ids.translations,
wasm_version: version,
test_mode,
};
Expand Down Expand Up @@ -111,6 +112,8 @@ async fn install_service_canisters_impl(

let translations_canister_wasm = get_canister_wasm(CanisterName::Translations, version);
let translations_init_args = translations_canister::init::Args {
deployment_operators: vec![principal],
user_index_canister_id: canister_ids.user_index,
cycles_dispenser_canister_id: canister_ids.cycles_dispenser,
wasm_version: version,
test_mode,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::cell::Cell;
use std::time::Duration;
use tracing::{error, trace};
use types::{CanisterId, TimestampNanos};
use utils::consts::{MEMO_JOINING_FEE, SNS_GOVERNANCE_CANISTER_ID};
use utils::consts::{MEMO_GROUP_IMPORT_INTO_COMMUNITY, MEMO_JOINING_FEE, SNS_GOVERNANCE_CANISTER_ID};

thread_local! {
static TIMER_ID: Cell<Option<TimerId>> = Cell::default();
Expand Down Expand Up @@ -95,5 +95,6 @@ async fn make_payment(ledger_canister: CanisterId, transfer_args: TransferArg) -
fn memo(reason: PendingPaymentReason) -> Memo {
match reason {
PendingPaymentReason::AccessGate => MEMO_JOINING_FEE.to_vec().into(),
PendingPaymentReason::TransferToCommunityBeingImportedInto => MEMO_GROUP_IMPORT_INTO_COMMUNITY.to_vec().into(),
}
}
6 changes: 3 additions & 3 deletions backend/canisters/community/impl/src/updates/claim_prize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ async fn claim_prize(args: Args) -> Response {
};

// Transfer the prize to the winner
let result = process_transaction(prepare_result.transaction, prepare_result.group).await;
let result = process_transaction(prepare_result.transaction, prepare_result.this_canister_id).await;

match result {
Ok(completed_transaction) => {
Expand All @@ -45,7 +45,7 @@ async fn claim_prize(args: Args) -> Response {

struct PrepareResult {
pub transaction: PendingCryptoTransaction,
pub group: CanisterId,
pub this_canister_id: CanisterId,
pub user_id: UserId,
}

Expand Down Expand Up @@ -96,7 +96,7 @@ fn prepare(args: &Args, state: &mut RuntimeState) -> Result<PrepareResult, Box<R
let transaction = create_pending_transaction(token, ledger, amount, fee, user_id, Some(&MEMO_PRIZE_CLAIM), now_nanos);

Ok(PrepareResult {
group: state.env.canister_id(),
this_canister_id: state.env.canister_id(),
transaction,
user_id,
})
Expand Down
1 change: 1 addition & 0 deletions backend/canisters/group/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
### Added

- Implement ability to update user principals ([#5220](https://github.com/open-chat-labs/open-chat/pull/5220))
- Transfer prizes to community during import ([#5317](https://github.com/open-chat-labs/open-chat/pull/5317))

### Changed

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::cell::Cell;
use std::time::Duration;
use tracing::{error, trace};
use types::{CanisterId, TimestampNanos};
use utils::consts::{MEMO_JOINING_FEE, SNS_GOVERNANCE_CANISTER_ID};
use utils::consts::{MEMO_GROUP_IMPORT_INTO_COMMUNITY, MEMO_JOINING_FEE, SNS_GOVERNANCE_CANISTER_ID};

thread_local! {
static TIMER_ID: Cell<Option<TimerId>> = Cell::default();
Expand Down Expand Up @@ -95,5 +95,6 @@ async fn make_payment(ledger_canister: CanisterId, transfer_args: TransferArg) -
fn memo(reason: PendingPaymentReason) -> Memo {
match reason {
PendingPaymentReason::AccessGate => MEMO_JOINING_FEE.to_vec().into(),
PendingPaymentReason::TransferToCommunityBeingImportedInto => MEMO_GROUP_IMPORT_INTO_COMMUNITY.to_vec().into(),
}
}
71 changes: 67 additions & 4 deletions backend/canisters/group/impl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use notifications_canister::c2c_push_notification;
use serde::{Deserialize, Serialize};
use serde_bytes::ByteBuf;
use std::cell::RefCell;
use std::collections::hash_map::Entry::{Occupied, Vacant};
use std::collections::{HashMap, HashSet};
use std::ops::Deref;
use types::SNS_FEE_SHARE_PERCENT;
Expand Down Expand Up @@ -254,9 +255,15 @@ impl RuntimeState {
} else if self.data.is_frozen() {
ChatFrozen
} else {
self.data.community_being_imported_into = Some(community);
let serialized = msgpack::serialize_then_unwrap(&self.data.chat);
let transfers_required = self.prepare_transfers_for_import_into_community();
let serialized = serialize_then_unwrap(&self.data.chat);
let total_bytes = serialized.len() as u64;

if let Some(community_id) = community.community_id() {
self.transfer_funds_to_community_being_imported_into(community_id, &transfers_required);
}

self.data.community_being_imported_into = Some(community);
self.data.serialized_chat_state = Some(ByteBuf::from(serialized));

freeze_group_impl(
Expand All @@ -266,10 +273,61 @@ impl RuntimeState {
self,
);

Success(total_bytes)
Success(StartImportIntoCommunityResultSuccess {
total_bytes,
transfers_required,
})
}
}

pub fn prepare_transfers_for_import_into_community(&mut self) -> HashMap<CanisterId, (u128, u128)> {
let now = self.env.now();
let max_prize_message_length = 7 * DAY_IN_MS;
let pending_prize_messages = self
.data
.chat
.events
.pending_prize_messages(now.saturating_sub(max_prize_message_length));

let mut transfers_required = HashMap::new();

for (message_id, prize_message) in pending_prize_messages {
let ledger = prize_message.transaction.ledger_canister_id();
let fee = prize_message.transaction.fee();
let amount: u128 = prize_message.prizes_remaining.iter().map(|p| p.e8s() as u128 + fee).sum();

match transfers_required.entry(ledger) {
Vacant(e) => {
e.insert((amount.saturating_sub(fee), fee));
self.data.chat.events.reduce_final_prize_by_transfer_fee(message_id);
}
Occupied(e) => {
let (total, _) = e.into_mut();
*total += amount;
}
}
}

transfers_required
}

fn transfer_funds_to_community_being_imported_into(
&mut self,
community_id: CommunityId,
transfers: &HashMap<CanisterId, (u128, u128)>,
) {
for (&ledger_canister, &(amount, fee)) in transfers.iter() {
self.data.pending_payments_queue.push(PendingPayment {
amount,
fee,
ledger_canister,
recipient: PaymentRecipient::Account(Principal::from(community_id).into()),
reason: PendingPaymentReason::TransferToCommunityBeingImportedInto,
});
}
jobs::make_pending_payments::start_job_if_required(self);
}

pub fn run_event_expiry_job(&mut self) {
let now = self.env.now();
self.data.chat.remove_expired_events(now);
Expand Down Expand Up @@ -626,7 +684,12 @@ pub struct CanisterIds {
}

pub enum StartImportIntoCommunityResult {
Success(u64),
Success(StartImportIntoCommunityResultSuccess),
AlreadyImportingToAnotherCommunity,
ChatFrozen,
}

pub struct StartImportIntoCommunityResultSuccess {
pub total_bytes: u64,
pub transfers_required: HashMap<CanisterId, (u128, u128)>,
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ fn c2c_start_import_into_community_impl(args: Args, state: &mut RuntimeState) ->
}

match state.start_importing_into_community(CommunityBeingImportedInto::Existing(args.community_id)) {
StartImportIntoCommunityResult::Success(total_bytes) => Success(total_bytes),
StartImportIntoCommunityResult::Success(result) => Success(result.total_bytes),
StartImportIntoCommunityResult::AlreadyImportingToAnotherCommunity => AlreadyImportingToAnotherCommunity,
StartImportIntoCommunityResult::ChatFrozen => ChatFrozen,
}
Expand Down
29 changes: 22 additions & 7 deletions backend/canisters/group/impl/src/updates/convert_into_community.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use canister_tracing_macros::trace;
use group_canister::convert_into_community::{Response::*, *};
use ic_cdk_macros::update;
use rand::Rng;
use std::collections::HashMap;
use types::{CanisterId, UserId};
use utils::consts::OPENCHAT_BOT_USER_ID;

Expand All @@ -28,15 +29,19 @@ async fn convert_into_community(args: Args) -> Response {
_ => return NotAuthorized,
}

let c2c_args = match mutate_state(|state| start_import(caller, user_id, args, state)) {
Ok(args) => args,
let StartImportResult {
c2c_args,
transfers_required,
} = match mutate_state(|state| start_import(caller, user_id, args, state)) {
Ok(ok) => ok,
Err(response) => return response,
};

match group_index_canister_c2c_client::c2c_convert_group_into_community(group_index_canister_id, &c2c_args).await {
Ok(group_index_canister::c2c_convert_group_into_community::Response::Success(community_id)) => {
mutate_state(|state| {
state.data.community_being_imported_into = Some(CommunityBeingImportedInto::Existing(community_id))
state.data.community_being_imported_into = Some(CommunityBeingImportedInto::Existing(community_id));
state.transfer_funds_to_community_being_imported_into(community_id, &transfers_required);
});
Success(SuccessResult {
community_id,
Expand Down Expand Up @@ -79,15 +84,20 @@ fn prepare(state: &mut RuntimeState) -> Result<PrepareResult, Response> {
}
}

struct StartImportResult {
c2c_args: group_index_canister::c2c_convert_group_into_community::Args,
transfers_required: HashMap<CanisterId, (u128, u128)>,
}

fn start_import(
caller: Principal,
user_id: UserId,
args: Args,
state: &mut RuntimeState,
) -> Result<group_index_canister::c2c_convert_group_into_community::Args, Response> {
) -> Result<StartImportResult, Response> {
match state.start_importing_into_community(CommunityBeingImportedInto::New) {
StartImportIntoCommunityResult::Success(total_bytes) => {
Ok(group_index_canister::c2c_convert_group_into_community::Args {
StartImportIntoCommunityResult::Success(result) => {
let c2c_args = group_index_canister::c2c_convert_group_into_community::Args {
channel_id: state.env.rng().gen(),
user_id,
user_principal: caller,
Expand All @@ -99,7 +109,12 @@ fn start_import(
gate: state.data.chat.gate.value.clone(),
primary_language: args.primary_language.unwrap_or_else(|| "en".to_string()),
history_visible_to_new_joiners: args.history_visible_to_new_joiners,
total_bytes,
total_bytes: result.total_bytes,
};

Ok(StartImportResult {
c2c_args,
transfers_required: result.transfers_required,
})
}
StartImportIntoCommunityResult::AlreadyImportingToAnotherCommunity => Err(AlreadyImportingToAnotherCommunity),
Expand Down
1 change: 1 addition & 0 deletions backend/canisters/identity/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
- Implement ability to update user principals ([#5220](https://github.com/open-chat-labs/open-chat/pull/5220))
- Sync user principals from the UserIndex canister ([#5264](https://github.com/open-chat-labs/open-chat/pull/5264))
- Implement `migrate_legacy_principal` ([#5274](https://github.com/open-chat-labs/open-chat/pull/5274))
- Expose count of `legacy_principals` in metrics ([#5311](https://github.com/open-chat-labs/open-chat/pull/5311))
2 changes: 2 additions & 0 deletions backend/canisters/identity/impl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ impl RuntimeState {
cycles_balance: self.env.cycles_balance(),
wasm_version: WASM_VERSION.with_borrow(|v| **v),
git_commit_id: utils::git::git_commit_id().to_string(),
legacy_principals: self.data.legacy_principals.len() as u32,
canister_ids: CanisterIds {
user_index: self.data.user_index_canister_id,
cycles_dispenser: self.data.cycles_dispenser_canister_id,
Expand Down Expand Up @@ -114,6 +115,7 @@ pub struct Metrics {
pub cycles_balance: Cycles,
pub wasm_version: BuildVersion,
pub git_commit_id: String,
pub legacy_principals: u32,
pub canister_ids: CanisterIds,
}

Expand Down
Loading

0 comments on commit ee9f574

Please sign in to comment.