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

fix: clean up heap allowance and balances #3521

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
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
8 changes: 8 additions & 0 deletions rs/ledger_suite/common/ledger_core/src/approvals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ pub trait AllowancesData {
fn len_expirations(&self) -> usize;

fn clear_arrivals(&mut self);

fn clear_all(&mut self);
}

#[derive(Debug, Deserialize, Serialize)]
Expand Down Expand Up @@ -159,6 +161,12 @@ where
fn clear_arrivals(&mut self) {
self.arrival_queue.clear();
}

fn clear_all(&mut self) {
self.allowances.clear();
self.expiration_queue.clear();
self.arrival_queue.clear();
}
}

#[derive(Clone, Eq, PartialEq, Debug, Deserialize, Serialize)]
Expand Down
4 changes: 4 additions & 0 deletions rs/ledger_suite/icp/ledger/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -732,4 +732,8 @@ impl AllowancesData for StableAllowancesData {
fn clear_arrivals(&mut self) {
panic!("The method `clear_arrivals` should not be called for StableAllowancesData")
}

fn clear_all(&mut self) {
panic!("The method `clear_all` should not be called for StableAllowancesData")
}
}
9 changes: 9 additions & 0 deletions rs/ledger_suite/icrc1/ledger/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -727,6 +727,11 @@ impl Ledger {
pub fn copy_token_pool(&mut self) {
self.stable_balances.token_pool = self.balances.token_pool;
}

pub fn clear_heap_approvals_and_balances(&mut self) {
self.approvals.allowances_data.clear_all();
self.balances.store.clear();
}
}

impl LedgerContext for Ledger {
Expand Down Expand Up @@ -1257,6 +1262,10 @@ impl AllowancesData for StableAllowancesData {
fn clear_arrivals(&mut self) {
panic!("The method `clear_arrivals` should not be called for StableAllowancesData")
}

fn clear_all(&mut self) {
panic!("The method `clear_all` should not be called for StableAllowancesData")
}
}

#[derive(Serialize, Deserialize, Debug, Default, PartialEq)]
Expand Down
4 changes: 4 additions & 0 deletions rs/ledger_suite/icrc1/ledger/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,10 @@ fn post_upgrade(args: Option<LedgerArgument>) {
migrate_next_part(
MAX_INSTRUCTIONS_PER_UPGRADE.saturating_sub(pre_upgrade_instructions_consumed),
);
} else {
Access::with_ledger_mut(|ledger| {
ledger.clear_heap_approvals_and_balances();
});
}

let end = ic_cdk::api::instruction_counter();
Expand Down
Loading