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 11, 2022
1 parent 7cdb523 commit 1aa86d5
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 79 deletions.
67 changes: 32 additions & 35 deletions actors/miner/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ use fvm_shared::econ::TokenAmount;
// The following errors are particular cases of illegal state.
// They're not expected to ever happen, but if they do, distinguished codes can help us
// diagnose the problem.
pub use beneficiary::*;
use fil_actors_runtime::cbor::{deserialize, serialize, serialize_vec};
use fvm_shared::actor::builtin::{Type, CALLER_TYPES_SIGNABLE};
use fvm_shared::error::*;
Expand All @@ -53,7 +54,6 @@ pub use state::*;
pub use termination::*;
pub use types::*;
pub use vesting_state::*;
pub use beneficiary::*;

use crate::commd::{is_unsealed_sector, CompactCommD};
use crate::Code::Blake2b256;
Expand Down Expand Up @@ -120,8 +120,8 @@ pub enum Method {
ProveReplicaUpdates = 27,
PreCommitSectorBatch2 = 28,
ProveReplicaUpdates2 = 29,
ChangeBeneficiary =30,
GetBeneficiary =31,
ChangeBeneficiary = 30,
GetBeneficiary = 31,
}

pub const ERR_BALANCE_INVARIANTS_BROKEN: ExitCode = ExitCode::new(1000);
Expand Down Expand Up @@ -3309,7 +3309,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 @@ -3328,7 +3328,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 @@ -3391,35 +3391,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 @@ -3461,8 +3459,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
3 changes: 1 addition & 2 deletions actors/miner/src/types.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Copyright 2019-2022 ChainSafe Systems
// SPDX-License-Identifier: Apache-2.0, MIT

use crate::commd::CompactCommD;
use super::beneficiary::*;
use crate::commd::CompactCommD;
use cid::Cid;
use fil_actors_runtime::DealWeight;
use fvm_ipld_bitfield::UnvalidatedBitField;
Expand Down Expand Up @@ -409,7 +409,6 @@ pub struct ProveReplicaUpdatesParams2 {

impl Cbor for ProveReplicaUpdatesParams2 {}


#[derive(Debug, Serialize_tuple, Deserialize_tuple)]
pub struct ChangeBeneficiaryParams {
pub new_beneficiary: Address,
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
Loading

0 comments on commit 1aa86d5

Please sign in to comment.