Skip to content

Commit

Permalink
fix(account): rename deposit to add balance and add overflow check
Browse files Browse the repository at this point in the history
  • Loading branch information
zcabter committed Aug 29, 2024
1 parent 6155f07 commit c8fcaed
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 7 deletions.
8 changes: 4 additions & 4 deletions crates/jstz_core/tests/transaction_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ mod test {

tx.begin();

let _ = Account::deposit(hrt, &mut tx, &pkh2, 25);
let _ = Account::add_balance(hrt, &mut tx, &pkh2, 25);

verify_account_balance(hrt, &mut tx, &pkh1, 0);
verify_account_balance(hrt, &mut tx, &pkh2, 25);
Expand All @@ -73,7 +73,7 @@ mod test {

verify_account_balance(hrt, &mut tx, &pkh2, 25);

let _ = Account::deposit(hrt, &mut tx, &pkh1, 57);
let _ = Account::add_balance(hrt, &mut tx, &pkh1, 57);

verify_account_balance(hrt, &mut tx, &pkh1, 57);

Expand All @@ -83,7 +83,7 @@ mod test {

verify_account_balance(hrt, &mut tx, &pkh2, 25);

let _ = Account::deposit(hrt, &mut tx, &pkh1, 57);
let _ = Account::add_balance(hrt, &mut tx, &pkh1, 57);

verify_account_balance(hrt, &mut tx, &pkh1, 2 * 57);

Expand All @@ -93,7 +93,7 @@ mod test {

verify_account_balance(hrt, &mut tx, &pkh1, 2 * 57);

let _ = Account::deposit(hrt, &mut tx, &pkh1, 57);
let _ = Account::add_balance(hrt, &mut tx, &pkh1, 57);

verify_account_balance(hrt, &mut tx, &pkh1, 3 * 57);

Expand Down
8 changes: 6 additions & 2 deletions crates/jstz_proto/src/context/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,15 +148,19 @@ impl Account {
Ok(account.amount)
}

pub fn deposit(
pub fn add_balance(
hrt: &impl HostRuntime,
tx: &mut Transaction,
addr: &Address,
amount: Amount,
) -> Result<()> {
let account = Self::get_mut(hrt, tx, addr)?;
let checked_balance = account
.amount
.checked_add(amount)
.ok_or(crate::error::Error::BalanceOverflow)?;

account.amount += amount;
account.amount = checked_balance;
Ok(())
}

Expand Down
2 changes: 1 addition & 1 deletion crates/jstz_proto/src/executor/deposit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub fn execute(
amount, receiver, ..
} = deposit;

let result = Account::deposit(hrt, tx, &receiver, amount);
let result = Account::add_balance(hrt, tx, &receiver, amount);
let hash = Blake2b::from(deposit.inbox_id.to_be_bytes().as_slice());
Receipt::new(hash, result.map(|_| crate::receipt::Content::Deposit))
}

0 comments on commit c8fcaed

Please sign in to comment.