Skip to content

Commit 81b2fd7

Browse files
author
Adrian Nagy
committed
refactor(ledger): move min_balance_at_slot under Account as method
1 parent d542f9e commit 81b2fd7

File tree

2 files changed

+61
-93
lines changed

2 files changed

+61
-93
lines changed

ledger/src/account/account.rs

Lines changed: 59 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,7 @@ use crate::{
2727
},
2828
witness::Witness,
2929
},
30-
scan_state::{
31-
currency::{Balance, Magnitude, Nonce, Slot, TxnVersion},
32-
transaction_logic::account_min_balance_at_slot,
33-
},
30+
scan_state::currency::{Amount, Balance, Magnitude, Nonce, Slot, TxnVersion},
3431
zkapps::snark::FlaggedOption,
3532
AppendToInputs as _, MerklePath, MyCow, ToInputs,
3633
};
@@ -1386,21 +1383,8 @@ impl Account {
13861383
pub fn has_locked_tokens(&self, global_slot: Slot) -> bool {
13871384
match self.timing {
13881385
Timing::Untimed => false,
1389-
Timing::Timed {
1390-
initial_minimum_balance,
1391-
cliff_time,
1392-
cliff_amount,
1393-
vesting_period,
1394-
vesting_increment,
1395-
} => {
1396-
let curr_min_balance = account_min_balance_at_slot(
1397-
global_slot,
1398-
cliff_time,
1399-
cliff_amount,
1400-
vesting_period,
1401-
vesting_increment,
1402-
initial_minimum_balance,
1403-
);
1386+
Timing::Timed { .. } => {
1387+
let curr_min_balance = self.min_balance_at_slot(global_slot);
14041388

14051389
!curr_min_balance.is_zero()
14061390
}
@@ -1438,26 +1422,68 @@ impl Account {
14381422
pub fn liquid_balance_at_slot(&self, global_slot: Slot) -> Balance {
14391423
match self.timing {
14401424
Timing::Untimed => self.balance,
1425+
Timing::Timed { .. } => self
1426+
.balance
1427+
.sub_amount(self.min_balance_at_slot(global_slot).to_amount())
1428+
.unwrap(),
1429+
}
1430+
}
1431+
1432+
pub fn min_balance_at_slot(&self, global_slot: Slot) -> Balance {
1433+
match self.timing {
1434+
Timing::Untimed => Balance::zero(),
14411435
Timing::Timed {
14421436
initial_minimum_balance,
14431437
cliff_time,
14441438
cliff_amount,
14451439
vesting_period,
14461440
vesting_increment,
1447-
} => self
1448-
.balance
1449-
.sub_amount(
1450-
account_min_balance_at_slot(
1451-
global_slot,
1452-
cliff_time,
1453-
cliff_amount,
1454-
vesting_period,
1455-
vesting_increment,
1456-
initial_minimum_balance,
1457-
)
1458-
.to_amount(),
1459-
)
1460-
.unwrap(),
1441+
} => {
1442+
if global_slot < cliff_time {
1443+
initial_minimum_balance
1444+
} else if vesting_period.is_zero() {
1445+
// If vesting period is zero then everything vests immediately at the cliff
1446+
Balance::zero()
1447+
} else {
1448+
match initial_minimum_balance.sub_amount(cliff_amount) {
1449+
None => Balance::zero(),
1450+
Some(min_balance_past_cliff) => {
1451+
// take advantage of fact that global slots are uint32's
1452+
1453+
let num_periods = (global_slot.as_u32() - cliff_time.as_u32())
1454+
/ vesting_period.as_u32();
1455+
let num_periods: u64 = num_periods.into();
1456+
1457+
let vesting_decrement = {
1458+
let vesting_increment = vesting_increment.as_u64();
1459+
1460+
if u64::MAX
1461+
.checked_div(num_periods)
1462+
.map(|res| {
1463+
matches!(
1464+
res.cmp(&vesting_increment),
1465+
std::cmp::Ordering::Less
1466+
)
1467+
})
1468+
.unwrap_or(false)
1469+
{
1470+
// The vesting decrement will overflow, use [max_int] instead.
1471+
Amount::from_u64(u64::MAX)
1472+
} else {
1473+
Amount::from_u64(
1474+
num_periods.checked_mul(vesting_increment).unwrap(),
1475+
)
1476+
}
1477+
};
1478+
1479+
match min_balance_past_cliff.sub_amount(vesting_decrement) {
1480+
None => Balance::zero(),
1481+
Some(amount) => amount,
1482+
}
1483+
}
1484+
}
1485+
}
1486+
}
14611487
}
14621488
}
14631489

ledger/src/scan_state/transaction_logic.rs

Lines changed: 2 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -7710,10 +7710,7 @@ fn validate_timing_with_min_balance_impl(
77107710
}
77117711
Timed {
77127712
initial_minimum_balance,
7713-
cliff_time,
7714-
cliff_amount,
7715-
vesting_period,
7716-
vesting_increment,
7713+
..
77177714
} => {
77187715
let account_balance = account.balance;
77197716

@@ -7727,14 +7724,7 @@ fn validate_timing_with_min_balance_impl(
77277724
(true, false, *initial_minimum_balance)
77287725
}
77297726
Some(proposed_new_balance) => {
7730-
let curr_min_balance = account_min_balance_at_slot(
7731-
*txn_global_slot,
7732-
*cliff_time,
7733-
*cliff_amount,
7734-
*vesting_period,
7735-
*vesting_increment,
7736-
*initial_minimum_balance,
7737-
);
7727+
let curr_min_balance = account.min_balance_at_slot(*txn_global_slot);
77387728

77397729
if proposed_new_balance < curr_min_balance {
77407730
(false, true, curr_min_balance)
@@ -7764,54 +7754,6 @@ fn validate_timing_with_min_balance_impl(
77647754
}
77657755
}
77667756

7767-
// TODO: This should be in `account.rs`
7768-
pub fn account_min_balance_at_slot(
7769-
global_slot: Slot,
7770-
cliff_time: Slot,
7771-
cliff_amount: Amount,
7772-
vesting_period: SlotSpan,
7773-
vesting_increment: Amount,
7774-
initial_minimum_balance: Balance,
7775-
) -> Balance {
7776-
if global_slot < cliff_time {
7777-
initial_minimum_balance
7778-
} else if vesting_period.is_zero() {
7779-
// If vesting period is zero then everything vests immediately at the cliff
7780-
Balance::zero()
7781-
} else {
7782-
match initial_minimum_balance.sub_amount(cliff_amount) {
7783-
None => Balance::zero(),
7784-
Some(min_balance_past_cliff) => {
7785-
// take advantage of fact that global slots are uint32's
7786-
7787-
let num_periods =
7788-
(global_slot.as_u32() - cliff_time.as_u32()) / vesting_period.as_u32();
7789-
let num_periods: u64 = num_periods.into();
7790-
7791-
let vesting_decrement = {
7792-
let vesting_increment = vesting_increment.as_u64();
7793-
7794-
if u64::MAX
7795-
.checked_div(num_periods)
7796-
.map(|res| matches!(res.cmp(&vesting_increment), std::cmp::Ordering::Less))
7797-
.unwrap_or(false)
7798-
{
7799-
// The vesting decrement will overflow, use [max_int] instead.
7800-
Amount::from_u64(u64::MAX)
7801-
} else {
7802-
Amount::from_u64(num_periods.checked_mul(vesting_increment).unwrap())
7803-
}
7804-
};
7805-
7806-
match min_balance_past_cliff.sub_amount(vesting_decrement) {
7807-
None => Balance::zero(),
7808-
Some(amount) => amount,
7809-
}
7810-
}
7811-
}
7812-
}
7813-
}
7814-
78157757
fn sub_amount(balance: Balance, amount: Amount) -> Result<Balance, String> {
78167758
balance
78177759
.sub_amount(amount)

0 commit comments

Comments
 (0)