Run cargo fmt on the whole code base (#9394)

* Run cargo fmt on the whole code base

* Second run

* Add CI check

* Fix compilation

* More unnecessary braces

* Handle weights

* Use --all

* Use correct attributes...

* Fix UI tests

* AHHHHHHHHH

* 🤦

* Docs

* Fix compilation

* 🤷

* Please stop

* 🤦 x 2

* More

* make rustfmt.toml consistent with polkadot

Co-authored-by: André Silva <andrerfosilva@gmail.com>
This commit is contained in:
Bastian Köcher
2021-07-21 16:32:32 +02:00
committed by GitHub
parent d451c38c1c
commit 7b56ab15b4
1010 changed files with 53339 additions and 51208 deletions
+76 -103
View File
@@ -18,23 +18,23 @@
//! A module responsible for computing the right amount of weight and charging it.
use crate::{
AliveContractInfo, BalanceOf, ContractInfo, ContractInfoOf, Pallet, Event,
TombstoneContractInfo, Config, CodeHash, Error,
storage::Storage, wasm::PrefabWasmModule, exec::Executable, gas::GasMeter,
exec::Executable, gas::GasMeter, storage::Storage, wasm::PrefabWasmModule, AliveContractInfo,
BalanceOf, CodeHash, Config, ContractInfo, ContractInfoOf, Error, Event, Pallet,
TombstoneContractInfo,
};
use sp_std::prelude::*;
use sp_io::hashing::blake2_256;
use sp_core::crypto::UncheckedFrom;
use frame_support::{
storage::child,
traits::{Currency, ExistenceRequirement, Get, OnUnbalanced, WithdrawReasons},
DefaultNoBound,
};
use pallet_contracts_primitives::{ContractAccessError, RentProjection, RentProjectionResult};
use sp_core::crypto::UncheckedFrom;
use sp_io::hashing::blake2_256;
use sp_runtime::{
DispatchError,
traits::{Bounded, CheckedDiv, CheckedMul, SaturatedConversion, Saturating, Zero},
DispatchError,
};
use sp_std::prelude::*;
/// Information about the required deposit and resulting rent.
///
@@ -83,13 +83,8 @@ where
code_size: u32,
) -> Result<Option<AliveContractInfo<T>>, DispatchError> {
let current_block_number = <frame_system::Pallet<T>>::block_number();
let verdict = Self::consider_case(
account,
current_block_number,
Zero::zero(),
&contract,
code_size,
);
let verdict =
Self::consider_case(account, current_block_number, Zero::zero(), &contract, code_size);
Self::enact_verdict(account, contract, current_block_number, verdict, None)
}
@@ -136,10 +131,14 @@ where
.unwrap_or_else(|| <BalanceOf<T>>::zero())
.saturating_add(contract.rent_paid);
Self::enact_verdict(
account, contract, current_block_number, verdict, Some(module),
account,
contract,
current_block_number,
verdict,
Some(module),
)?;
Ok((Some(rent_paid), code_len))
}
},
_ => Ok((None, code_len)),
}
}
@@ -155,9 +154,7 @@ where
/// NOTE that this is not a side-effect free function! It will actually collect rent and then
/// compute the projection. This function is only used for implementation of an RPC method through
/// `RuntimeApi` meaning that the changes will be discarded anyway.
pub fn compute_projection(
account: &T::AccountId,
) -> RentProjectionResult<T::BlockNumber> {
pub fn compute_projection(account: &T::AccountId) -> RentProjectionResult<T::BlockNumber> {
use ContractAccessError::IsTombstone;
let contract_info = <ContractInfoOf<T>>::get(account);
@@ -179,45 +176,42 @@ where
// We skip the eviction in case one is in order.
// Evictions should only be performed by [`try_eviction`].
let new_contract_info = Self::enact_verdict(
account, alive_contract_info, current_block_number, verdict, None,
);
let new_contract_info =
Self::enact_verdict(account, alive_contract_info, current_block_number, verdict, None);
// Check what happened after enaction of the verdict.
let alive_contract_info = new_contract_info.map_err(|_| IsTombstone)?.ok_or_else(|| IsTombstone)?;
let alive_contract_info =
new_contract_info.map_err(|_| IsTombstone)?.ok_or_else(|| IsTombstone)?;
// Compute how much would the fee per block be with the *updated* balance.
let total_balance = T::Currency::total_balance(account);
let free_balance = T::Currency::free_balance(account);
let fee_per_block = Self::fee_per_block(
&free_balance, &alive_contract_info, code_size,
);
let fee_per_block = Self::fee_per_block(&free_balance, &alive_contract_info, code_size);
if fee_per_block.is_zero() {
return Ok(RentProjection::NoEviction);
return Ok(RentProjection::NoEviction)
}
// Then compute how much the contract will sustain under these circumstances.
let rent_budget = Self::rent_budget(&total_balance, &free_balance, &alive_contract_info).expect(
"the contract exists and in the alive state;
let rent_budget = Self::rent_budget(&total_balance, &free_balance, &alive_contract_info)
.expect(
"the contract exists and in the alive state;
the updated balance must be greater than subsistence deposit;
this function doesn't return `None`;
qed
",
);
);
let blocks_left = match rent_budget.checked_div(&fee_per_block) {
Some(blocks_left) => blocks_left,
None => {
// `fee_per_block` is not zero here, so `checked_div` can return `None` if
// there is an overflow. This cannot happen with integers though. Return
// `NoEviction` here just in case.
return Ok(RentProjection::NoEviction);
}
return Ok(RentProjection::NoEviction)
},
};
let blocks_left = blocks_left.saturated_into::<u32>().into();
Ok(RentProjection::EvictionAt(
current_block_number + blocks_left,
))
Ok(RentProjection::EvictionAt(current_block_number + blocks_left))
}
/// Restores the destination account using the origin as prototype.
@@ -246,18 +240,15 @@ where
let current_block = <frame_system::Pallet<T>>::block_number();
if origin_contract.last_write == Some(current_block) {
return Err(Error::<T>::InvalidContractOrigin.into());
return Err(Error::<T>::InvalidContractOrigin.into())
}
let dest_tombstone = <ContractInfoOf<T>>::get(&dest)
.and_then(|c| c.get_tombstone())
.ok_or(Error::<T>::InvalidDestinationContract)?;
let last_write = if !delta.is_empty() {
Some(current_block)
} else {
origin_contract.last_write
};
let last_write =
if !delta.is_empty() { Some(current_block) } else { origin_contract.last_write };
// Fails if the code hash does not exist on chain
E::add_user(code_hash, gas_meter)?;
@@ -266,7 +257,8 @@ where
// fail later due to tombstones not matching. This is because the restoration
// is always called from a contract and therefore in a storage transaction.
// The failure of this function will lead to this transaction's rollback.
let bytes_taken: u32 = delta.iter()
let bytes_taken: u32 = delta
.iter()
.filter_map(|key| {
let key = blake2_256(key);
child::get_raw(&child_trie_info, &key).map(|value| {
@@ -284,21 +276,24 @@ where
);
if tombstone != dest_tombstone {
return Err(Error::<T>::InvalidTombstone.into());
return Err(Error::<T>::InvalidTombstone.into())
}
origin_contract.storage_size -= bytes_taken;
<ContractInfoOf<T>>::remove(&origin);
E::remove_user(origin_contract.code_hash, gas_meter)?;
<ContractInfoOf<T>>::insert(&dest, ContractInfo::Alive(AliveContractInfo::<T> {
code_hash,
rent_allowance,
rent_paid: <BalanceOf<T>>::zero(),
deduct_block: current_block,
last_write,
.. origin_contract
}));
<ContractInfoOf<T>>::insert(
&dest,
ContractInfo::Alive(AliveContractInfo::<T> {
code_hash,
rent_allowance,
rent_paid: <BalanceOf<T>>::zero(),
deduct_block: current_block,
last_write,
..origin_contract
}),
);
let origin_free_balance = T::Currency::free_balance(&origin);
T::Currency::make_free_balance_be(&origin, <BalanceOf<T>>::zero());
@@ -314,42 +309,34 @@ where
current_refcount: u32,
at_refcount: u32,
) -> RentStatus<T> {
let calc_share = |refcount: u32| {
aggregated_code_size.checked_div(refcount).unwrap_or(0)
};
let calc_share = |refcount: u32| aggregated_code_size.checked_div(refcount).unwrap_or(0);
let current_share = calc_share(current_refcount);
let custom_share = calc_share(at_refcount);
RentStatus {
max_deposit: Self::required_deposit(contract, aggregated_code_size),
current_deposit: Self::required_deposit(contract, current_share),
custom_refcount_deposit:
if at_refcount > 0 {
Some(Self::required_deposit(contract, custom_share))
} else {
None
},
custom_refcount_deposit: if at_refcount > 0 {
Some(Self::required_deposit(contract, custom_share))
} else {
None
},
max_rent: Self::fee_per_block(free_balance, contract, aggregated_code_size),
current_rent: Self::fee_per_block(free_balance, contract, current_share),
custom_refcount_rent:
if at_refcount > 0 {
Some(Self::fee_per_block(free_balance, contract, custom_share))
} else {
None
},
custom_refcount_rent: if at_refcount > 0 {
Some(Self::fee_per_block(free_balance, contract, custom_share))
} else {
None
},
_reserved: None,
}
}
/// Returns how much deposit is required to not pay rent.
fn required_deposit(
contract: &AliveContractInfo<T>,
code_size_share: u32,
) -> BalanceOf<T> {
fn required_deposit(contract: &AliveContractInfo<T>, code_size_share: u32) -> BalanceOf<T> {
T::DepositPerStorageByte::get()
.saturating_mul(contract.storage_size.saturating_add(code_size_share).into())
.saturating_add(
T::DepositPerStorageItem::get()
.saturating_mul(contract.pair_count.into())
T::DepositPerStorageItem::get().saturating_mul(contract.pair_count.into()),
)
.saturating_add(T::DepositPerContract::get())
}
@@ -363,8 +350,8 @@ where
contract: &AliveContractInfo<T>,
code_size_share: u32,
) -> BalanceOf<T> {
let missing_deposit = Self::required_deposit(contract, code_size_share)
.saturating_sub(*free_balance);
let missing_deposit =
Self::required_deposit(contract, code_size_share).saturating_sub(*free_balance);
T::RentFraction::get().mul_ceil(missing_deposit)
}
@@ -383,16 +370,13 @@ where
// Reserved balance contributes towards the subsistence threshold to stay consistent
// with the existential deposit where the reserved balance is also counted.
if *total_balance < subsistence_threshold {
return None;
return None
}
// However, reserved balance cannot be charged so we need to use the free balance
// to calculate the actual budget (which can be 0).
let rent_allowed_to_charge = free_balance.saturating_sub(subsistence_threshold);
Some(<BalanceOf<T>>::min(
contract.rent_allowance,
rent_allowed_to_charge,
))
Some(<BalanceOf<T>>::min(contract.rent_allowance, rent_allowed_to_charge))
}
/// Consider the case for rent payment of the given account and returns a `Verdict`.
@@ -414,7 +398,7 @@ where
};
if blocks_passed.is_zero() {
// Rent has already been paid
return Verdict::Exempt;
return Verdict::Exempt
}
let total_balance = T::Currency::total_balance(account);
@@ -425,7 +409,7 @@ where
if fee_per_block.is_zero() {
// The rent deposit offset reduced the fee to 0. This means that the contract
// gets the rent for free.
return Verdict::Exempt;
return Verdict::Exempt
}
let rent_budget = match Self::rent_budget(&total_balance, &free_balance, contract) {
@@ -443,7 +427,7 @@ where
account,
);
0u32.into()
}
},
};
let dues = fee_per_block
@@ -469,18 +453,15 @@ where
if insufficient_rent || !can_withdraw_rent {
// The contract cannot afford the rent payment and has a balance above the subsistence
// threshold, so it leaves a tombstone.
let amount = if can_withdraw_rent {
Some(OutstandingAmount::new(dues_limited))
} else {
None
};
return Verdict::Evict { amount };
let amount =
if can_withdraw_rent { Some(OutstandingAmount::new(dues_limited)) } else { None };
return Verdict::Evict { amount }
}
return Verdict::Charge {
// We choose to use `dues_limited` here instead of `dues` just to err on the safer side.
amount: OutstandingAmount::new(dues_limited),
};
}
}
/// Enacts the given verdict and returns the updated `ContractInfo`.
@@ -511,9 +492,7 @@ where
}
// Note: this operation is heavy.
let child_storage_root = child::root(
&alive_contract_info.child_trie_info(),
);
let child_storage_root = child::root(&alive_contract_info.child_trie_info());
let tombstone = <TombstoneContractInfo<T>>::new(
&child_storage_root[..],
@@ -524,11 +503,9 @@ where
code.drop_from_storage();
<Pallet<T>>::deposit_event(Event::Evicted(account.clone()));
Ok(None)
}
(Verdict::Evict { amount: _ }, None) => {
Ok(None)
}
(Verdict::Exempt, _) => {
},
(Verdict::Evict { amount: _ }, None) => Ok(None),
(Verdict::Exempt, _) => {
let contract = ContractInfo::Alive(AliveContractInfo::<T> {
deduct_block: current_block_number,
..alive_contract_info
@@ -546,11 +523,9 @@ where
<ContractInfoOf<T>>::insert(account, &contract);
amount.withdraw(account);
Ok(Some(contract.get_alive().expect("We just constructed it as alive. qed")))
}
},
}
}
}
/// The amount to charge.
@@ -596,9 +571,7 @@ enum Verdict<T: Config> {
Exempt,
/// The contract cannot afford payment within its rent budget so it gets evicted. However,
/// because its balance is greater than the subsistence threshold it leaves a tombstone.
Evict {
amount: Option<OutstandingAmount<T>>,
},
Evict { amount: Option<OutstandingAmount<T>> },
/// Everything is OK, we just only take some charge.
Charge { amount: OutstandingAmount<T> },
}