From f9989ee990c00977c084377eb9d3a29ffe25f6f5 Mon Sep 17 00:00:00 2001 From: Nikolas Haimerl Date: Tue, 29 Aug 2023 11:06:18 +0000 Subject: [PATCH 1/6] changed: assert for trap --- cycles-ledger/src/storage.rs | 112 +++++++++++++++++++++-------------- 1 file changed, 68 insertions(+), 44 deletions(-) diff --git a/cycles-ledger/src/storage.rs b/cycles-ledger/src/storage.rs index 97a55eb..f54477b 100644 --- a/cycles-ledger/src/storage.rs +++ b/cycles-ledger/src/storage.rs @@ -293,12 +293,13 @@ pub fn mutate_state(now: u64, f: impl FnOnce(&mut State) -> R) -> R { } fn check_invariants(s: &State) { - debug_assert!( - s.expiration_queue.len() <= s.approvals.len(), - "expiration_queue len ({}) larger than approvals len ({})", - s.expiration_queue.len(), - s.approvals.len() - ); + if s.expiration_queue.len() > s.approvals.len() { + ic_cdk::trap(&format!( + "expiration_queue len ({}) larger than approvals len ({})", + s.expiration_queue.len(), + s.approvals.len() + )) + } } #[derive(Default)] @@ -350,7 +351,13 @@ pub fn record_deposit( memo: Option, now: u64, ) -> (u64, u128, Hash) { - assert!(amount >= crate::config::FEE); + if amount < crate::config::FEE { + ic_cdk::trap(&format!( + "The requested amount {} to be deposited is be less than the cycles ledger fee: {}", + amount, + crate::config::FEE + )) + } let key = to_account_key(account); mutate_state(now, |s| { @@ -457,14 +464,16 @@ fn check_transfer_preconditions( now: u64, created_at_time: Option, ) { - assert!(from_balance >= total_spent_amount); + if from_balance < total_spent_amount { + ic_cdk::trap(&format!("The balance of the account sending cycles {} is lower than the total amount of cycles needed to make the transfer {}",from_balance,total_spent_amount)) + } if let Some(time) = created_at_time { - assert!( - time <= now.saturating_add(crate::config::PERMITTED_DRIFT.as_nanos() as u64), - "Transfer created in the future, created_at_time: {}, now: {}", - time, - now - ); + if time > now.saturating_add(crate::config::PERMITTED_DRIFT.as_nanos() as u64) { + ic_cdk::trap(&format!( + "Transfer created in the future, created_at_time: {}, now: {}", + time, now + )) + } } } @@ -513,7 +522,9 @@ pub fn send( let from_balance = s.balances.get(&from_key).unwrap_or_default(); let total_balance_deduction = amount.saturating_add(crate::config::FEE); - assert!(from_balance >= total_balance_deduction); + if from_balance < total_balance_deduction { + ic_cdk::trap(&format!("The balance of the account sending cycles {} is lower than the total amount of cycles needed to make the transfer {}",from_balance,total_balance_deduction)) + } s.debit(from_key, total_balance_deduction); let phash = s.last_block_hash(); @@ -603,21 +614,30 @@ fn check_approve_preconditions( now: u64, created_at_time: Option, ) { - assert!(from_balance >= crate::config::FEE); - assert!( - from != spender, - "self approvals are not allowed, should be checked in the endpoint" - ); - assert!( - expires_at.unwrap_or(REMOTE_FUTURE) > now, - "Approval expiration time ({}) should be later than now ({now})", - expires_at.unwrap_or(REMOTE_FUTURE), - ); + if from_balance < crate::config::FEE { + ic_cdk::trap(&format!( + "The balance of the account {:?} is {} which is lower than the cycles ledger fee {}", + from, + from_balance, + crate::config::FEE + )) + } + if from == spender { + ic_cdk::trap("self approvals are not allowed, should be checked in the endpoint") + } + if expires_at.unwrap_or(REMOTE_FUTURE) <= now { + ic_cdk::trap(&format!( + "Approval expiration time ({}) should be later than now ({now})", + expires_at.unwrap_or(REMOTE_FUTURE) + )) + } if let Some(time) = created_at_time { - assert!( - time <= now.saturating_add(crate::config::PERMITTED_DRIFT.as_nanos() as u64), - "Approval created in the future, created_at_time: {time}, now: {now}" - ); + if time > now.saturating_add(crate::config::PERMITTED_DRIFT.as_nanos() as u64) { + ic_cdk::trap(&format!( + "Approval created in the future, created_at_time: {}, now: {}", + time, now + )) + } } } @@ -675,25 +695,29 @@ fn record_approval( fn use_allowance(s: &mut State, account: &Account, spender: &Account, amount: u128, now: u64) { let key = (to_account_key(account), to_account_key(spender)); - assert!(amount > 0, "Cannot use amount 0 from allowance"); + if amount == 0 { + ic_cdk::trap("Cannot use amount 0 from allowance") + } let (current_allowance, current_expiration) = s.approvals.get(&key).unwrap_or_else(|| { - panic!( + ic_cdk::trap(&format!( "Allowance does not exist, account {}, spender {}", account, spender - ) + )); }); - assert!( - current_expiration == 0 || current_expiration > now, - "Expired allowance, expiration {} is earlier than now {}", - current_expiration, - now - ); - assert!( - current_allowance >= amount, - "Insufficient allowance, current_allowance {}, total spent amount {}", - current_allowance, - amount - ); + + if !(current_expiration == 0 || current_expiration > now) { + ic_cdk::trap(&format!( + "Expired allowance, expiration {} is earlier than now {}", + current_expiration, now + )) + } + + if current_allowance < amount { + ic_cdk::trap(&format!( + "Insufficient allowance, current_allowance {}, total spent amount {}", + current_allowance, amount + )) + } let new_amount = current_allowance - amount; if new_amount == 0 { From 76b6debc3acf4b81767dff89e32f38c4fb8bf5d3 Mon Sep 17 00:00:00 2001 From: Nikolas Haimerl Date: Tue, 29 Aug 2023 11:09:11 +0000 Subject: [PATCH 2/6] fmt --- cycles-ledger/src/storage.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cycles-ledger/src/storage.rs b/cycles-ledger/src/storage.rs index f54477b..a89fd50 100644 --- a/cycles-ledger/src/storage.rs +++ b/cycles-ledger/src/storage.rs @@ -353,7 +353,7 @@ pub fn record_deposit( ) -> (u64, u128, Hash) { if amount < crate::config::FEE { ic_cdk::trap(&format!( - "The requested amount {} to be deposited is be less than the cycles ledger fee: {}", + "The requested amount {} to be deposited is less than the cycles ledger fee: {}", amount, crate::config::FEE )) From 080049bb30f5fa05ef73241f22ed8b2ffdf18599 Mon Sep 17 00:00:00 2001 From: NikolasHai <113891786+NikolasHai@users.noreply.github.com> Date: Tue, 29 Aug 2023 13:31:48 +0200 Subject: [PATCH 3/6] Update cycles-ledger/src/storage.rs Co-authored-by: Thomas Locher --- cycles-ledger/src/storage.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cycles-ledger/src/storage.rs b/cycles-ledger/src/storage.rs index a89fd50..a7865fb 100644 --- a/cycles-ledger/src/storage.rs +++ b/cycles-ledger/src/storage.rs @@ -616,7 +616,7 @@ fn check_approve_preconditions( ) { if from_balance < crate::config::FEE { ic_cdk::trap(&format!( - "The balance of the account {:?} is {} which is lower than the cycles ledger fee {}", + "The balance of the account {:?} is {}, which is lower than the cycles ledger fee: {}", from, from_balance, crate::config::FEE From 5f11d46321de61158bf6c1590f48ad128bbf0c1c Mon Sep 17 00:00:00 2001 From: NikolasHai <113891786+NikolasHai@users.noreply.github.com> Date: Tue, 29 Aug 2023 13:31:56 +0200 Subject: [PATCH 4/6] Update cycles-ledger/src/storage.rs Co-authored-by: Thomas Locher --- cycles-ledger/src/storage.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cycles-ledger/src/storage.rs b/cycles-ledger/src/storage.rs index a7865fb..297e92e 100644 --- a/cycles-ledger/src/storage.rs +++ b/cycles-ledger/src/storage.rs @@ -465,7 +465,7 @@ fn check_transfer_preconditions( created_at_time: Option, ) { if from_balance < total_spent_amount { - ic_cdk::trap(&format!("The balance of the account sending cycles {} is lower than the total amount of cycles needed to make the transfer {}",from_balance,total_spent_amount)) + ic_cdk::trap(&format!("The balance of the account sending cycles ({}) is lower than the total number of cycles needed to make the transfer ({})",from_balance,total_spent_amount)) } if let Some(time) = created_at_time { if time > now.saturating_add(crate::config::PERMITTED_DRIFT.as_nanos() as u64) { From 3bdbeb1fe5b5a748ecb66d5e8739512f85b69cec Mon Sep 17 00:00:00 2001 From: NikolasHai <113891786+NikolasHai@users.noreply.github.com> Date: Tue, 29 Aug 2023 13:32:01 +0200 Subject: [PATCH 5/6] Update cycles-ledger/src/storage.rs Co-authored-by: Thomas Locher --- cycles-ledger/src/storage.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cycles-ledger/src/storage.rs b/cycles-ledger/src/storage.rs index 297e92e..f9b4e1f 100644 --- a/cycles-ledger/src/storage.rs +++ b/cycles-ledger/src/storage.rs @@ -523,7 +523,7 @@ pub fn send( let total_balance_deduction = amount.saturating_add(crate::config::FEE); if from_balance < total_balance_deduction { - ic_cdk::trap(&format!("The balance of the account sending cycles {} is lower than the total amount of cycles needed to make the transfer {}",from_balance,total_balance_deduction)) + ic_cdk::trap(&format!("The balance of the account sending cycles ({}) is lower than the total number of cycles needed to make the transfer ({})",from_balance,total_balance_deduction)) } s.debit(from_key, total_balance_deduction); From 4aa392ac5668e4ac457193c05f1686731f1b67b3 Mon Sep 17 00:00:00 2001 From: NikolasHai <113891786+NikolasHai@users.noreply.github.com> Date: Tue, 29 Aug 2023 13:32:26 +0200 Subject: [PATCH 6/6] Update cycles-ledger/src/storage.rs Co-authored-by: Thomas Locher --- cycles-ledger/src/storage.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cycles-ledger/src/storage.rs b/cycles-ledger/src/storage.rs index f9b4e1f..974de2b 100644 --- a/cycles-ledger/src/storage.rs +++ b/cycles-ledger/src/storage.rs @@ -696,7 +696,7 @@ fn use_allowance(s: &mut State, account: &Account, spender: &Account, amount: u1 let key = (to_account_key(account), to_account_key(spender)); if amount == 0 { - ic_cdk::trap("Cannot use amount 0 from allowance") + ic_cdk::trap("Cannot deduct amount 0 from allowance") } let (current_allowance, current_expiration) = s.approvals.get(&key).unwrap_or_else(|| { ic_cdk::trap(&format!(