Skip to content

Commit

Permalink
test:slightly refrator and add more beneficiary test
Browse files Browse the repository at this point in the history
  • Loading branch information
hunjixin committed Aug 18, 2022
1 parent dd20d3a commit e501e28
Show file tree
Hide file tree
Showing 5 changed files with 218 additions and 186 deletions.
4 changes: 2 additions & 2 deletions actors/miner/src/beneficiary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use fvm_shared::econ::TokenAmount;
use num_traits::Zero;
use std::ops::Sub;

#[derive(Debug, PartialEq, Clone, Serialize_tuple, Deserialize_tuple)]
#[derive(Debug, PartialEq, Eq, Clone, Serialize_tuple, Deserialize_tuple)]
pub struct BeneficiaryTerm {
/// The total amount the current beneficiary can withdraw. Monotonic, but reset when beneficiary changes.
#[serde(with = "bigint_ser")]
Expand Down Expand Up @@ -51,7 +51,7 @@ impl BeneficiaryTerm {
}
}

#[derive(Debug, PartialEq, Serialize_tuple, Deserialize_tuple)]
#[derive(Debug, PartialEq, Eq, Serialize_tuple, Deserialize_tuple)]
pub struct PendingBeneficiaryChange {
pub new_beneficiary: Address,
#[serde(with = "bigint_ser")]
Expand Down
35 changes: 15 additions & 20 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 fil_actors_runtime::runtime::builtins::Type;
Expand Down Expand Up @@ -3286,36 +3287,30 @@ impl Actor {
// Verify unlocked funds cover both InitialPledgeRequirement and FeeDebt
// and repay fee debt now.
let fee_to_burn = repay_debts_or_abort(rt, state)?;
let mut amount_withdrawn = std::cmp::min(&available_balance, &params.amount_requested);
let mut amount_withdrawn =
std::cmp::min(&available_balance, &params.amount_requested);
if amount_withdrawn.is_negative() {
return Err(actor_error!(
illegal_state,
"negative amount to withdraw: {}",
amount_withdrawn
));
return Err(actor_error!(
illegal_state,
"negative amount to withdraw: {}",
amount_withdrawn
));
}
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_negative() {
return Err(actor_error!(
forbidden,
"quota not enough beneficiary {} quota {} used quota {} expiration epoch {} current epoch {}",
info.beneficiary,
info.beneficiary_term.quota,
info.beneficiary_term.used_quota,
info.beneficiary_term.expiration,
rt.curr_epoch()
));
}
amount_withdrawn = std::cmp::min(amount_withdrawn, &remaining_quota);
if amount_withdrawn.is_positive() {
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")
e.downcast_default(
ExitCode::USR_ILLEGAL_STATE,
"failed to save miner info",
)
})?;
}
Ok((info, amount_withdrawn.clone(), newly_vested, fee_to_burn, state.clone()))
}else{
} else {
Ok((info, amount_withdrawn.clone(), newly_vested, fee_to_burn, state.clone()))
}
})?;
Expand Down Expand Up @@ -3444,7 +3439,7 @@ impl Actor {
}

state.save_info(rt.store(), &info).map_err(|e| {
e.downcast_default(ExitCode::USR_ILLEGAL_STATE, "failed to save miner info")
e.downcast_default(ExitCode::USR_FORBIDDEN, "failed to save miner info")
})?;
Ok(())
})
Expand Down
Loading

0 comments on commit e501e28

Please sign in to comment.