Skip to content

Commit

Permalink
chore: make lint happy
Browse files Browse the repository at this point in the history
  • Loading branch information
hunjixin committed Aug 2, 2022
1 parent bd51867 commit af42b15
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 75 deletions.
61 changes: 29 additions & 32 deletions actors/miner/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3158,7 +3158,7 @@ impl Actor {
}
amount_withdrawn = std::cmp::min(amount_withdrawn, &remaining_quota);
if amount_withdrawn.is_positive() {
info.beneficiary_term.used_quota = info.beneficiary_term.used_quota + amount_withdrawn;
info.beneficiary_term.used_quota += amount_withdrawn;
state.save_info(rt.store(), &info).map_err(|e| {
e.downcast_default(ExitCode::USR_ILLEGAL_STATE, "failed to save miner info")
})?;
Expand All @@ -3177,7 +3177,7 @@ impl Actor {
notify_pledge_changed(rt, &newly_vested.neg())?;

state.check_balance_invariants(&rt.current_balance()).map_err(balance_invariants_broken)?;
Ok(WithdrawBalanceReturn { amount_withdrawn: amount_withdrawn.clone() })
Ok(WithdrawBalanceReturn { amount_withdrawn })
}

/// Proposes or confirms a change of owner address.
Expand Down Expand Up @@ -3240,35 +3240,33 @@ impl Actor {
pending_beneficiary_term.approved_by_beneficiary = true;
}
info.pending_beneficiary_term = Some(pending_beneficiary_term);
} else {
if let Some(pending_term) = &info.pending_beneficiary_term {
if pending_term.new_beneficiary != new_beneficiary {
return Err(actor_error!(
illegal_argument,
"new beneficiary address must be equal expect {}, but got {}",
pending_term.new_beneficiary,
params.new_beneficiary
));
}
if pending_term.new_quota != params.new_quota {
return Err(actor_error!(
illegal_argument,
"new beneficiary quota must be equal expect {}, but got {}",
pending_term.new_quota,
params.new_quota
));
}
if pending_term.new_expiration != params.new_expiration {
return Err(actor_error!(
illegal_argument,
"new beneficiary expire date must be equal expect {}, but got {}",
pending_term.new_expiration,
params.new_expiration
));
}
} else {
return Err(actor_error!(forbidden, "No changeBeneficiary proposal exists"));
} else if let Some(pending_term) = &info.pending_beneficiary_term {
if pending_term.new_beneficiary != new_beneficiary {
return Err(actor_error!(
illegal_argument,
"new beneficiary address must be equal expect {}, but got {}",
pending_term.new_beneficiary,
params.new_beneficiary
));
}
if pending_term.new_quota != params.new_quota {
return Err(actor_error!(
illegal_argument,
"new beneficiary quota must be equal expect {}, but got {}",
pending_term.new_quota,
params.new_quota
));
}
if pending_term.new_expiration != params.new_expiration {
return Err(actor_error!(
illegal_argument,
"new beneficiary expire date must be equal expect {}, but got {}",
pending_term.new_expiration,
params.new_expiration
));
}
} else {
return Err(actor_error!(forbidden, "No changeBeneficiary proposal exists"));
}

if let Some(pending_term) = info.pending_beneficiary_term.as_mut() {
Expand Down Expand Up @@ -3310,8 +3308,7 @@ impl Actor {
RT: Runtime<BS>,
{
rt.validate_immediate_caller_accept_any()?;
let info =
rt.transaction(|state: &mut State, rt| Ok(get_miner_info(rt.store(), &state)?))?;
let info = rt.transaction(|state: &mut State, rt| get_miner_info(rt.store(), state))?;

Ok(GetBeneficiaryReturn {
active: ActiveBeneficiary {
Expand Down
46 changes: 23 additions & 23 deletions actors/miner/tests/change_beneficiary_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ fn successfully_change_owner_to_another_address_two_message() {
// proposal beneficiary change
h.change_beneficiary(&mut rt, h.owner, beneficiary_change.clone(), None).unwrap();
// assert change has been made in state
let mut info = h.get_info(&mut rt);
let mut info = h.get_info(&rt);
let pending_beneficiary_term = info.pending_beneficiary_term.unwrap();
assert_eq!(beneficiary_change.expiration, pending_beneficiary_term.new_expiration);
assert_eq!(beneficiary_change.quota.clone(), pending_beneficiary_term.new_quota);
assert_eq!(beneficiary_change.quota, pending_beneficiary_term.new_quota);
assert_eq!(first_beneficiary_id, pending_beneficiary_term.new_beneficiary);

//confirm proposal
Expand All @@ -44,10 +44,10 @@ fn successfully_change_owner_to_another_address_two_message() {
)
.unwrap();

info = h.get_info(&mut rt);
info = h.get_info(&rt);
assert_eq!(None, info.pending_beneficiary_term);
assert_eq!(first_beneficiary_id, info.beneficiary);
assert_eq!(beneficiary_change.quota.clone(), info.beneficiary_term.quota);
assert_eq!(beneficiary_change.quota, info.beneficiary_term.quota);
assert_eq!(beneficiary_change.expiration, info.beneficiary_term.expiration);

h.check_state(&rt);
Expand All @@ -70,31 +70,31 @@ fn successfully_change_from_not_owner_beneficiary_to_another_address_three_messa
ChainEpoch::from(201),
);
h.change_beneficiary(&mut rt, h.owner, second_beneficiary_change.clone(), None).unwrap();
let mut info = h.get_info(&mut rt);
let mut info = h.get_info(&rt);
assert_eq!(first_beneficiary_id, info.beneficiary);

let mut pending_beneficiary_term = info.pending_beneficiary_term.unwrap();
assert_eq!(second_beneficiary_change.expiration, pending_beneficiary_term.new_expiration);
assert_eq!(second_beneficiary_change.quota.clone(), pending_beneficiary_term.new_quota);
assert_eq!(second_beneficiary_change.quota, pending_beneficiary_term.new_quota);
assert_eq!(second_beneficiary_id, pending_beneficiary_term.new_beneficiary);
assert!(!pending_beneficiary_term.approved_by_beneficiary);
assert!(!pending_beneficiary_term.approved_by_nominee);

h.change_beneficiary(&mut rt, first_beneficiary_id, second_beneficiary_change.clone(), None)
.unwrap();
info = h.get_info(&mut rt);
info = h.get_info(&rt);
assert_eq!(first_beneficiary_id, info.beneficiary);

pending_beneficiary_term = info.pending_beneficiary_term.unwrap();
assert_eq!(second_beneficiary_change.expiration, pending_beneficiary_term.new_expiration);
assert_eq!(second_beneficiary_change.quota.clone(), pending_beneficiary_term.new_quota);
assert_eq!(second_beneficiary_change.quota, pending_beneficiary_term.new_quota);
assert_eq!(second_beneficiary_id, pending_beneficiary_term.new_beneficiary);
assert!(pending_beneficiary_term.approved_by_beneficiary);
assert!(!pending_beneficiary_term.approved_by_nominee);

h.change_beneficiary(&mut rt, second_beneficiary_id, second_beneficiary_change.clone(), None)
.unwrap();
info = h.get_info(&mut rt);
info = h.get_info(&rt);
assert_eq!(second_beneficiary_id, info.beneficiary);

assert_eq!(None, info.pending_beneficiary_term);
Expand All @@ -115,7 +115,7 @@ fn successfully_change_from_not_owner_beneficiary_to_another_address_when_benefi
h.propose_approve_initial_beneficiary(
&mut rt,
first_beneficiary_id,
BeneficiaryTerm::new(quota.clone(), TokenAmount::zero(), expiration),
BeneficiaryTerm::new(quota, TokenAmount::zero(), expiration),
)
.unwrap();

Expand All @@ -125,15 +125,15 @@ fn successfully_change_from_not_owner_beneficiary_to_another_address_when_benefi
let another_beneficiary_change =
BeneficiaryChange::new(second_beneficiary_id, another_quota.clone(), another_expiration);
h.change_beneficiary(&mut rt, h.owner, another_beneficiary_change.clone(), None).unwrap();
let mut info = h.get_info(&mut rt);
let mut info = h.get_info(&rt);

let pending_beneficiary_term = info.pending_beneficiary_term.unwrap();
assert_eq!(second_beneficiary_id, pending_beneficiary_term.new_beneficiary);
assert_eq!(another_quota, pending_beneficiary_term.new_quota);
assert_eq!(another_expiration, pending_beneficiary_term.new_expiration);

h.change_beneficiary(&mut rt, second_beneficiary_id, another_beneficiary_change, None).unwrap();
info = h.get_info(&mut rt);
info = h.get_info(&rt);
assert_eq!(None, info.pending_beneficiary_term);
assert_eq!(second_beneficiary_id, info.beneficiary);
assert_eq!(another_quota, info.beneficiary_term.quota);
Expand All @@ -152,7 +152,7 @@ fn successfully_owner_immediate_revoking_unapproved_proposal() {
// proposal beneficiary change
h.change_beneficiary(&mut rt, h.owner, beneficiary_change.clone(), None).unwrap();
// assert change has been made in state
let mut info = h.get_info(&mut rt);
let mut info = h.get_info(&rt);
let pending_beneficiary_term = info.pending_beneficiary_term.unwrap();
assert_eq!(beneficiary_change.expiration, pending_beneficiary_term.new_expiration);
assert_eq!(beneficiary_change.quota, pending_beneficiary_term.new_quota);
Expand All @@ -167,7 +167,7 @@ fn successfully_owner_immediate_revoking_unapproved_proposal() {
)
.unwrap();

info = h.get_info(&mut rt);
info = h.get_info(&rt);
assert_eq!(None, info.pending_beneficiary_term);
assert_eq!(h.owner, info.beneficiary);

Expand Down Expand Up @@ -197,7 +197,7 @@ fn successfully_immediately_change_back_to_owner_address_while_used_up_quota() {
)
.unwrap();

let info = h.get_info(&mut rt);
let info = h.get_info(&rt);
assert_eq!(None, info.pending_beneficiary_term);
assert_eq!(h.owner, info.beneficiary);
assert_eq!(TokenAmount::zero(), info.beneficiary_term.quota);
Expand All @@ -216,7 +216,7 @@ fn successfully_immediately_change_back_to_owner_while_expired() {
h.propose_approve_initial_beneficiary(
&mut rt,
first_beneficiary_id,
BeneficiaryTerm::new(quota.clone(), TokenAmount::zero(), expiration),
BeneficiaryTerm::new(quota, TokenAmount::zero(), expiration),
)
.unwrap();

Expand All @@ -229,7 +229,7 @@ fn successfully_immediately_change_back_to_owner_while_expired() {
)
.unwrap();

let info = h.get_info(&mut rt);
let info = h.get_info(&rt);
assert_eq!(None, info.pending_beneficiary_term);
assert_eq!(h.owner, info.beneficiary);
assert_eq!(TokenAmount::zero(), info.beneficiary_term.quota);
Expand All @@ -255,7 +255,7 @@ fn successfully_increase_quota() {
beneficiary_term.expiration,
);
h.change_beneficiary(&mut rt, h.owner, increase_beneficiary_change.clone(), None).unwrap();
let mut info = h.get_info(&mut rt);
let mut info = h.get_info(&rt);
let pending_beneficiary_term = info.pending_beneficiary_term.unwrap();
assert_eq!(first_beneficiary_id, info.beneficiary);
assert_eq!(increase_quota, pending_beneficiary_term.new_quota);
Expand All @@ -269,7 +269,7 @@ fn successfully_increase_quota() {
Some(first_beneficiary_id),
)
.unwrap();
info = h.get_info(&mut rt);
info = h.get_info(&rt);
assert_eq!(None, info.pending_beneficiary_term);
assert_eq!(first_beneficiary_id, info.beneficiary);
assert_eq!(increase_quota, info.beneficiary_term.quota);
Expand All @@ -290,7 +290,7 @@ fn fails_approval_message_with_invalidate_params() {
None,
)
.unwrap();
assert!(h.get_info(&mut rt).pending_beneficiary_term.is_some());
assert!(h.get_info(&rt).pending_beneficiary_term.is_some());

//expiration in approval message must equal with proposal
expect_abort(
Expand Down Expand Up @@ -380,7 +380,7 @@ fn fails_proposal_beneficiary_with_invalidate_params() {
#[test]
fn successfully_get_beneficiary() {
let (mut h, mut rt) = setup();
let mut info = h.get_info(&mut rt);
let mut info = h.get_info(&rt);
assert_eq!(h.owner, info.beneficiary);
assert_eq!(ChainEpoch::from(0), info.beneficiary_term.expiration);
assert_eq!(TokenAmount::zero(), info.beneficiary_term.quota);
Expand All @@ -392,7 +392,7 @@ fn successfully_get_beneficiary() {
h.propose_approve_initial_beneficiary(&mut rt, first_beneficiary_id, beneficiary_term.clone())
.unwrap();

info = h.get_info(&mut rt);
info = h.get_info(&rt);
assert_eq!(first_beneficiary_id, info.beneficiary);
assert_eq!(beneficiary_term.expiration, info.beneficiary_term.expiration);
assert_eq!(beneficiary_term.quota, info.beneficiary_term.quota);
Expand All @@ -403,7 +403,7 @@ fn successfully_get_beneficiary() {
h.withdraw_funds(&mut rt, h.beneficiary, &withdraw_fund, &withdraw_fund, &TokenAmount::zero())
.unwrap();

info = h.get_info(&mut rt);
info = h.get_info(&rt);
assert_eq!(first_beneficiary_id, info.beneficiary);
assert_eq!(beneficiary_term.expiration, info.beneficiary_term.expiration);
assert_eq!(left_quota, info.beneficiary_term.available(rt.epoch));
Expand Down
50 changes: 30 additions & 20 deletions actors/miner/tests/withdraw_balance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ fn successfully_withdraw() {
h.propose_approve_initial_beneficiary(
&mut rt,
first_beneficiary_id,
BeneficiaryTerm::new(quota.clone(), TokenAmount::zero(), PERIOD_OFFSET + 100),
BeneficiaryTerm::new(quota, TokenAmount::zero(), PERIOD_OFFSET + 100),
)
.unwrap();
h.withdraw_funds(&mut rt, h.owner, &one, &one, &TokenAmount::zero()).unwrap();
Expand All @@ -108,7 +108,7 @@ fn successfully_withdraw_allow_zero() {
h.propose_approve_initial_beneficiary(
&mut rt,
first_beneficiary_id,
BeneficiaryTerm::new( TokenAmount::from(1), TokenAmount::zero(), PERIOD_OFFSET + 100),
BeneficiaryTerm::new(TokenAmount::from(1), TokenAmount::zero(), PERIOD_OFFSET + 100),
)
.unwrap();
h.withdraw_funds(
Expand All @@ -123,7 +123,7 @@ fn successfully_withdraw_allow_zero() {
}

#[test]
fn successfully_withdraw_from_non_main_beneficiary_and_failure_when_used_all_quota() {
fn successfully_withdraw_more_than_quota() {
let mut h = ActorHarness::new(PERIOD_OFFSET);
let mut rt = h.new_runtime();
rt.set_balance(TokenAmount::from(BIG_BALANCE));
Expand All @@ -137,16 +137,15 @@ fn successfully_withdraw_from_non_main_beneficiary_and_failure_when_used_all_quo
BeneficiaryTerm::new(quota.clone(), TokenAmount::zero(), PERIOD_OFFSET + 100),
)
.unwrap();
h.withdraw_funds(&mut rt, h.beneficiary, &quota, &quota, &TokenAmount::zero()).unwrap();
expect_abort(
ExitCode::USR_FORBIDDEN,
h.withdraw_funds(&mut rt, h.beneficiary, &quota, &quota, &TokenAmount::zero()),
);

let withdraw_amount = TokenAmount::from(ONE_PERCENT_BALANCE * 2);
h.withdraw_funds(&mut rt, h.beneficiary, &withdraw_amount, &quota, &TokenAmount::zero())
.unwrap();
h.check_state(&rt);
}

#[test]
fn successfully_withdraw_more_than_quota() {
fn allow_withdraw_but_no_send_when_beneficiary_not_efficient() {
let mut h = ActorHarness::new(PERIOD_OFFSET);
let mut rt = h.new_runtime();
rt.set_balance(TokenAmount::from(BIG_BALANCE));
Expand All @@ -157,37 +156,48 @@ fn successfully_withdraw_more_than_quota() {
h.propose_approve_initial_beneficiary(
&mut rt,
first_beneficiary_id,
BeneficiaryTerm::new(quota.clone(), TokenAmount::zero(), PERIOD_OFFSET + 100),
BeneficiaryTerm::new(quota.clone(), TokenAmount::zero(), PERIOD_OFFSET - 10),
)
.unwrap();

let withdraw_amount = TokenAmount::from(ONE_PERCENT_BALANCE * 2);
h.withdraw_funds(&mut rt, h.beneficiary, &withdraw_amount, &quota, &TokenAmount::zero())
.unwrap();
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();
h.check_state(&rt);
}

#[test]
fn fails_withdraw_when_beneficiary_expired() {
fn fail_withdraw_from_non_beneficiary() {
let mut h = ActorHarness::new(PERIOD_OFFSET);
let mut rt = h.new_runtime();
rt.set_balance(TokenAmount::from(BIG_BALANCE));
h.construct_and_verify(&mut rt);

let first_beneficiary_id = Address::new_id(999);
let another_actor = Address::new_id(1000);
let quota = TokenAmount::from(ONE_PERCENT_BALANCE);
let one = TokenAmount::from(1);

expect_abort(
ExitCode::USR_FORBIDDEN,
h.withdraw_funds(&mut rt, first_beneficiary_id, &one, &TokenAmount::zero(), &TokenAmount::zero()),
);

h.propose_approve_initial_beneficiary(
&mut rt,
first_beneficiary_id,
BeneficiaryTerm::new(quota.clone(), TokenAmount::zero(), PERIOD_OFFSET - 10),
)
.unwrap();
let info = h.get_info(&mut rt);
assert_eq!(PERIOD_OFFSET - 10, info.beneficiary_term.expiration);
rt.set_epoch(100);
.unwrap();

expect_abort(
ExitCode::USR_FORBIDDEN,
h.withdraw_funds(&mut rt, h.beneficiary, &quota, &quota, &TokenAmount::zero()),
h.withdraw_funds(&mut rt, another_actor, &one, &TokenAmount::zero(), &TokenAmount::zero()),
);

//allow owner withdraw
h.withdraw_funds(&mut rt, h.owner, &one, &one, &TokenAmount::zero()).unwrap();
//allow beneficiary withdraw
h.withdraw_funds(&mut rt, first_beneficiary_id, &one, &one, &TokenAmount::zero()).unwrap();
h.check_state(&rt);
}

0 comments on commit af42b15

Please sign in to comment.