Skip to content

Commit

Permalink
feat: economic model requirements change (#241)
Browse files Browse the repository at this point in the history
  • Loading branch information
ytqaljn authored Oct 12, 2023
1 parent 2fb5a0a commit dbe2206
Show file tree
Hide file tree
Showing 13 changed files with 189 additions and 48 deletions.
16 changes: 16 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions c-pallets/audit/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ cp-bloom-filter = { path = '../../primitives/bloom-filter', version = '0.1.0', d
cp-scheduler-credit = { path = '../../primitives/scheduler-credit', version = '0.1.0', default-features = false }
cp-cess-common = { path = '../../primitives/common', version = '0.1.0', default-features = false }
cp-enclave-verify = { path = '../../primitives/enclave-verify', version = '0.1.0', default-features = false }
pallet-sminer-reward = { default-features = false, path = "../sminer-reward", version = "0.7.4" }

[dependencies.frame-benchmarking]
default-features = false
Expand Down Expand Up @@ -148,6 +149,7 @@ std = [
"pallet-tee-worker/std",
"cp-cess-common/std",
"cp-cess-common/std",
"pallet-sminer-reward/std",
]
runtime-benchmarks = [
"frame-benchmarking/runtime-benchmarks",
Expand Down
2 changes: 1 addition & 1 deletion c-pallets/file-bank/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -860,7 +860,7 @@ pub mod pallet {
ensure!(Self::check_permission(sender.clone(), owner.clone()), Error::<T>::NoPermission);
ensure!(<Bucket<T>>::contains_key(&owner, &name), Error::<T>::NonExistent);
let bucket = <Bucket<T>>::try_get(&owner, &name).map_err(|_| Error::<T>::Unexpected)?;
ensure!(bucket.object_list == 0, Error::<T>::NotEmpty);
ensure!(bucket.object_list.len() == 0, Error::<T>::NotEmpty);

<Bucket<T>>::remove(&owner, &name);
<UserBucketList<T>>::try_mutate(&owner, |bucket_list| -> DispatchResult {
Expand Down
31 changes: 31 additions & 0 deletions c-pallets/sminer-reward/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
[package]
name = "pallet-sminer-reward"
authors = ["CESS LAB"]
version = "0.7.4"
edition = "2021"
license = "Apache-2.0"
repository = "https://github.com/CESSProject/cess"
description = "FRAME pallet for sminer management"
readme = "README.md"

[dependencies]
scale-info = { default-features = false, features = ['derive'], version = "2.0.1" }
codec = { package = "parity-scale-codec", version = "3.0.0", default-features = false, features = ["derive"] }

#substrate pallet
frame-support = { version = "4.0.0-dev", default-features = false, git = "https://github.com/CESSProject/substrate", branch = "cess-polkadot-v0.9.42" }
frame-system = { version = "4.0.0-dev", default-features = false, git = "https://github.com/CESSProject/substrate", branch = "cess-polkadot-v0.9.42" }
sp-std = { version = "5.0.0", git = 'https://github.com/CESSProject/substrate', branch = "cess-polkadot-v0.9.42", default-features = false }
sp-runtime = { version = "7.0.0", git = 'https://github.com/CESSProject/substrate', branch = "cess-polkadot-v0.9.42", default-features = false }


[features]
default = ["std"]
std = [
"codec/std",
"scale-info/std",
"frame-support/std",
"frame-system/std",
"sp-std/std",
"sp-runtime/std",
]
83 changes: 83 additions & 0 deletions c-pallets/sminer-reward/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#![cfg_attr(not(feature = "std"), no_std)]
use frame_support::{
traits::{
Currency, ReservableCurrency,
},
dispatch::{DispatchResult},
pallet_prelude::{StorageValue, ValueQuery},
};
use codec::{Decode, Encode};
use scale_info::TypeInfo;
// use sp_std::prelude::*;
use sp_runtime::{
SaturatedConversion,
traits::{CheckedAdd, CheckedSub},
};
// use frame_system::pallet_prelude::*;

pub use pallet::*;

type BalanceOf<T> =
<<T as pallet::Config>::Currency as Currency<<T as frame_system::Config>::AccountId>>::Balance;

#[frame_support::pallet]
pub mod pallet {
use super::*;

#[pallet::pallet]
pub struct Pallet<T>(sp_std::marker::PhantomData<T>);

#[pallet::config]
pub trait Config: frame_system::Config {
/// The currency trait.
type Currency: ReservableCurrency<Self::AccountId>;
}

#[pallet::error]
pub enum Error<T> {
Overflow,
}

#[pallet::storage]
#[pallet::getter(fn currency_reward)]
pub(super) type CurrencyReward<T: Config> = StorageValue<_, BalanceOf<T>, ValueQuery>;
}

pub trait RewardPool<Balance> {
fn get_reward() -> Balance;
fn get_reward_128() -> u128;
fn add_reward(amount: Balance) -> DispatchResult;
fn sub_reward(amount: Balance) -> DispatchResult;
}

impl<T: Config> RewardPool<BalanceOf<T>> for Pallet<T> {
fn get_reward() -> BalanceOf<T> {
<CurrencyReward<T>>::get()
}

fn get_reward_128() -> u128 {
<CurrencyReward<T>>::get().saturated_into()
}

fn add_reward(amount: BalanceOf<T>) -> DispatchResult {
<CurrencyReward<T>>::mutate(|v| -> DispatchResult {
// The total issuance amount will not exceed u128::Max, so there is no overflow risk
*v = v.checked_add(&amount).ok_or(Error::<T>::Overflow)?;

Ok(())
})?;

Ok(())
}

fn sub_reward(amount: BalanceOf<T>) -> DispatchResult {
<CurrencyReward<T>>::mutate(|v| -> DispatchResult {
// The total issuance amount will not exceed u128::Max, so there is no overflow risk
*v = v.checked_sub(&amount).ok_or(Error::<T>::Overflow)?;

Ok(())
})?;

Ok(())
}
}
2 changes: 2 additions & 0 deletions c-pallets/sminer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ pallet-cess-staking = { path = '../staking', version = '4.0.0-dev', default-feat
#local crate
pallet-tee-worker = { default-features = false, path = '../tee-worker', version = '0.7.0' }
pallet-storage-handler = { default-features = false, path = '../storage-handler', version = '0.7.0' }
pallet-sminer-reward = { default-features = false, path = "../sminer-reward", version = "0.7.4" }

[dependencies.frame-benchmarking]
default-features = false
Expand Down Expand Up @@ -112,6 +113,7 @@ std = [
"pallet-scheduler/std",
"frame-benchmarking/std",
"cp-cess-common/std",
"pallet-sminer-reward/std",
]

runtime-benchmarks = [
Expand Down
12 changes: 3 additions & 9 deletions c-pallets/sminer/src/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,12 @@ impl<T: Config> Pallet<T> {
if miner_info.collaterals > punish_amount {
T::Currency::unreserve(miner, punish_amount);
T::Currency::transfer(miner, &reward_pot, punish_amount, KeepAlive)?;
<CurrencyReward<T>>::mutate(|reward| {
*reward = *reward + punish_amount;
});
T::RewardPool::add_reward(punish_amount)?;
miner_info.collaterals = miner_info.collaterals.checked_sub(&punish_amount).ok_or(Error::<T>::Overflow)?;
} else {
T::Currency::unreserve(miner, miner_info.collaterals);
T::Currency::transfer(miner, &reward_pot, miner_info.collaterals, KeepAlive)?;
<CurrencyReward<T>>::mutate(|reward| {
*reward = *reward + miner_info.collaterals;
});
T::RewardPool::add_reward(miner_info.collaterals)?;
miner_info.collaterals = BalanceOf::<T>::zero();
miner_info.debt = punish_amount.checked_sub(&miner_info.collaterals).ok_or(Error::<T>::Overflow)?;
}
Expand Down Expand Up @@ -95,9 +91,7 @@ impl<T: Config> Pallet<T> {
if let Ok(reward_info) = <RewardMap<T>>::try_get(acc).map_err(|_| Error::<T>::NotExisted) {
let reward = reward_info.total_reward
.checked_sub(&reward_info.reward_issued).ok_or(Error::<T>::Overflow)?;
<CurrencyReward<T>>::mutate(|v| {
*v = *v + reward;
});
T::RewardPool::add_reward(reward)?;
}

let mut miner_list = AllMiner::<T>::get();
Expand Down
12 changes: 3 additions & 9 deletions c-pallets/sminer/src/helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ impl<T: Config> Pallet<T> {
miner_idle_space: u128,
miner_service_space: u128,
) -> DispatchResult {
let total_reward = <CurrencyReward<T>>::get();
let total_reward = T::RewardPool::get_reward();
let total_power = Self::calculate_power(total_idle_space, total_service_space);
let miner_power = Self::calculate_power(miner_idle_space, miner_service_space);

Expand Down Expand Up @@ -189,11 +189,7 @@ impl<T: Config> Pallet<T> {

Ok(())
})?;

<CurrencyReward<T>>::mutate(|v| -> DispatchResult {
*v = v.checked_sub(&order.order_reward).ok_or(Error::<T>::Overflow)?;
Ok(())
})?;
T::RewardPool::sub_reward(order.order_reward)?;

Ok(())
}
Expand Down Expand Up @@ -240,9 +236,7 @@ impl<T: Config> Pallet<T> {
if let Ok(reward_info) = <RewardMap<T>>::try_get(acc).map_err(|_| Error::<T>::NotExisted) {
let reward = reward_info.total_reward
.checked_sub(&reward_info.reward_issued).ok_or(Error::<T>::Overflow)?;
<CurrencyReward<T>>::mutate(|v| {
*v = *v + reward;
});
T::RewardPool::add_reward(reward)?;
}
let mut miner_list = AllMiner::<T>::get();
miner_list.retain(|s| s != acc);
Expand Down
28 changes: 7 additions & 21 deletions c-pallets/sminer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ use cp_enclave_verify::verify_rsa;
use pallet_tee_worker::TeeWorkerHandler;
use cp_bloom_filter::BloomFilter;
use pallet_storage_handler::StorageHandle;
use pallet_sminer_reward::RewardPool;

pub use pallet::*;

Expand Down Expand Up @@ -107,6 +108,8 @@ pub mod pallet {
/// The WeightInfo.
type WeightInfo: WeightInfo;

type RewardPool: RewardPool<BalanceOf<Self>>;

type FScheduler: ScheduleNamed<Self::BlockNumber, Self::SProposal, Self::SPalletsOrigin>;

type AScheduler: ScheduleAnon<Self::BlockNumber, Self::SProposal, Self::SPalletsOrigin>;
Expand Down Expand Up @@ -246,10 +249,6 @@ pub mod pallet {
pub(super) type FaucetRecordMap<T: Config> =
StorageMap<_, Blake2_128Concat, T::AccountId, FaucetRecord<BlockNumberOf<T>>>;

#[pallet::storage]
#[pallet::getter(fn currency_reward)]
pub(super) type CurrencyReward<T: Config> = StorageValue<_, BalanceOf<T>, ValueQuery>;

#[pallet::storage]
#[pallet::getter(fn miner_public_key)]
pub(super) type MinerPublicKey<T: Config> = StorageMap<_, Blake2_128Concat, [u8; 32], AccountOf<T>>;
Expand Down Expand Up @@ -402,16 +401,10 @@ pub mod pallet {
if miner_info.debt > collaterals {
miner_info.debt = miner_info.debt.checked_sub(&collaterals).ok_or(Error::<T>::Overflow)?;
remaining = BalanceOf::<T>::zero();
<CurrencyReward<T>>::mutate(|reward| -> DispatchResult {
*reward = reward.checked_add(&collaterals).ok_or(Error::<T>::Overflow)?;
Ok(())
})?;
T::RewardPool::add_reward(collaterals)?;
} else {
remaining = remaining.checked_sub(&miner_info.debt).ok_or(Error::<T>::Overflow)?;
<CurrencyReward<T>>::mutate(|reward| -> DispatchResult {
*reward = reward.checked_add(&miner_info.debt).ok_or(Error::<T>::Overflow)?;
Ok(())
})?;
T::RewardPool::add_reward(miner_info.debt)?;
miner_info.debt = BalanceOf::<T>::zero();
}
}
Expand Down Expand Up @@ -746,10 +739,8 @@ impl<T: Config> OnUnbalanced<NegativeImbalanceOf<T>> for Pallet<T> {

// Must resolve into existing but better to be safe.
let _ = T::Currency::resolve_creating(&T::PalletId::get().into_account_truncating(), amount);
<CurrencyReward<T>>::mutate(|v| {
// The total issuance amount will not exceed u128::Max, so there is no overflow risk
*v = *v + numeric_amount;
});
// The total issuance amount will not exceed u128::Max, so there is no overflow risk
T::RewardPool::add_reward(numeric_amount).unwrap();

Self::deposit_event(Event::Deposit { balance: numeric_amount });
}
Expand All @@ -776,7 +767,6 @@ pub trait MinerControl<AccountId, BlockNumber> {

fn get_miner_idle_space(acc: &AccountId) -> Result<u128, DispatchError>;
fn get_miner_count() -> u32;
fn get_reward() -> u128;
fn calculate_miner_reward(
miner: &AccountId,
total_idle_space: u128,
Expand Down Expand Up @@ -919,10 +909,6 @@ impl<T: Config> MinerControl<<T as frame_system::Config>::AccountId, BlockNumber
<MinerItems<T>>::count()
}

fn get_reward() -> u128 {
<CurrencyReward<T>>::get().saturated_into()
}

fn calculate_miner_reward(
miner: &AccountOf<T>,
total_idle_space: u128,
Expand Down
2 changes: 2 additions & 0 deletions c-pallets/storage-handler/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ frame-benchmarking = { version = '4.0.0-dev', git = 'https://github.com/CESSProj

# local dependencies
cp-cess-common = { version = '0.1.0', path = '../../primitives/common', default-features = false }
pallet-sminer-reward = { default-features = false, path = "../sminer-reward", version = "0.7.4" }

[features]
default = ["std"]
Expand All @@ -36,6 +37,7 @@ std = [
"sp-std/std",
"sp-runtime/std",
"frame-benchmarking/std",
"pallet-sminer-reward/std",
]

try-runtime = [ "frame-support/try-runtime" ]
Expand Down
Loading

0 comments on commit dbe2206

Please sign in to comment.