Skip to content

Commit

Permalink
WithdrawBalance fails when beneficiary is expired or out of quota (#602)
Browse files Browse the repository at this point in the history
* WithdrawBalance fail when beneficiary is expired or out of quota
  • Loading branch information
geoff-vball authored Sep 14, 2022
1 parent e55a4f9 commit fe531cb
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
9 changes: 9 additions & 0 deletions actors/miner/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3300,6 +3300,15 @@ impl Actor {
if info.beneficiary != info.owner {
// remaining_quota always zero and positive
let remaining_quota = info.beneficiary_term.available(rt.curr_epoch());
if remaining_quota.is_zero() {
return Err(actor_error!(
forbidden,
"beneficiary expiration of epoch {} passed or quota of {} depleted with {} used",
info.beneficiary_term.expiration,
info.beneficiary_term.quota,
info.beneficiary_term.used_quota
));
}
amount_withdrawn = std::cmp::min(amount_withdrawn, &remaining_quota);
if amount_withdrawn.is_positive() {
info.beneficiary_term.used_quota += amount_withdrawn;
Expand Down
8 changes: 4 additions & 4 deletions actors/miner/tests/change_beneficiary_test.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use fil_actor_miner::BeneficiaryTerm;
use fil_actors_runtime::test_utils::{expect_abort, MockRuntime};
use fil_actors_runtime::test_utils::{expect_abort, expect_abort_contains_message, MockRuntime};
use fvm_shared::clock::ChainEpoch;
use fvm_shared::{address::Address, econ::TokenAmount, error::ExitCode};
use num_traits::Zero;
Expand Down Expand Up @@ -274,14 +274,14 @@ fn successfully_change_quota_and_test_withdraw() {

//withdraw 0 zero
let withdraw_left = TokenAmount::from_atto(20);
h.withdraw_funds(
let ret = h.withdraw_funds(
&mut rt,
h.beneficiary,
&withdraw_left,
&TokenAmount::zero(),
&TokenAmount::zero(),
)
.unwrap();
);
expect_abort_contains_message(ExitCode::USR_FORBIDDEN, "beneficiary expiration of epoch", ret);

let increase_quota = TokenAmount::from_atto(120);
let increase_beneficiary_change =
Expand Down
7 changes: 4 additions & 3 deletions actors/miner/tests/withdraw_balance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ fn successfully_withdraw_limited_to_quota() {
}

#[test]
fn allow_withdraw_but_no_send_when_beneficiary_not_efficient() {
fn withdraw_fail_when_beneficiary_expired() {
let mut h = ActorHarness::new(PERIOD_OFFSET);
let mut rt = h.new_runtime();
rt.set_balance(BIG_BALANCE.clone());
Expand All @@ -159,8 +159,9 @@ fn allow_withdraw_but_no_send_when_beneficiary_not_efficient() {
let info = h.get_info(&rt);
assert_eq!(PERIOD_OFFSET - 10, info.beneficiary_term.expiration);
rt.set_epoch(100);
h.withdraw_funds(&mut rt, h.beneficiary, quota, &TokenAmount::zero(), &TokenAmount::zero())
.unwrap();
let ret =
h.withdraw_funds(&mut rt, h.beneficiary, quota, &TokenAmount::zero(), &TokenAmount::zero());
expect_abort_contains_message(ExitCode::USR_FORBIDDEN, "beneficiary expiration of epoch", ret);
h.check_state(&rt);
}

Expand Down

0 comments on commit fe531cb

Please sign in to comment.