Skip to content

Commit

Permalink
adding transfer preconditions check
Browse files Browse the repository at this point in the history
  • Loading branch information
maciejdfinity committed Aug 16, 2023
1 parent 903b7fc commit f89d4f6
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions cycles-ledger/src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,14 +276,13 @@ pub fn transfer(
) -> Result<(u64, Hash), TransferFromError> {
let from_key = to_account_key(from);
let to_key = to_account_key(to);
let total_spent_amount = amount.saturating_add(crate::config::FEE);
let from_balance = read_state(|s| s.balances.get(&from_key).unwrap_or_default());
check_transfer_preconditions(from_balance, total_spent_amount, now, created_at_time);

mutate_state(|s| {
prune(s, now, APPROVE_PRUNE_LIMIT);

let total_spent_amount = amount.saturating_add(crate::config::FEE);
let from_balance = s.balances.get(&from_key).unwrap_or_default();
assert!(from_balance >= total_spent_amount);

if spender.is_some() && spender.unwrap() != *from {
use_allowance(s, from, &spender.unwrap(), total_spent_amount, now)?;
}
Expand Down Expand Up @@ -315,6 +314,23 @@ pub fn transfer(
})
}

fn check_transfer_preconditions(
from_balance: u128,
total_spent_amount: u128,
now: u64,
created_at_time: Option<u64>,
) {
assert!(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
);
}
}

pub fn penalize(from: &Account, now: u64) -> (BlockIndex, Hash) {
let from_key = to_account_key(from);

Expand Down

0 comments on commit f89d4f6

Please sign in to comment.