Skip to content

Commit

Permalink
Revert "Identity staking follow up " (#3077)
Browse files Browse the repository at this point in the history
* Revert "Identity staking follow up  (#2976)"

This reverts commit 2f55394.

* Revert "fixing clippy issues (#3076)"

This reverts commit ac42ede.
  • Loading branch information
BillyWooo authored Sep 21, 2024
1 parent 136f684 commit 7607ebf
Show file tree
Hide file tree
Showing 35 changed files with 151 additions and 917 deletions.
3 changes: 0 additions & 3 deletions Cargo.lock

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

5 changes: 0 additions & 5 deletions pallets/score-staking/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,12 @@ serde = { workspace = true }
frame-benchmarking = { workspace = true, optional = true }
frame-support = { workspace = true }
frame-system = { workspace = true }
pallet-timestamp = { workspace = true }
sp-core = { workspace = true }
sp-runtime = { workspace = true }
sp-std = { workspace = true }

core-primitives = { workspace = true }
pallet-parachain-staking = { workspace = true }
pallet-teebag = { workspace = true }

[dev-dependencies]
sp-io = { workspace = true }
Expand All @@ -44,15 +42,12 @@ std = [
"pallet-balances/std",
"core-primitives/std",
"pallet-parachain-staking/std",
"pallet-teebag/std",
"pallet-timestamp/std",
]
runtime-benchmarks = [
"frame-benchmarking/runtime-benchmarks",
"frame-support/runtime-benchmarks",
"frame-system/runtime-benchmarks",
"sp-runtime/runtime-benchmarks",
"pallet-timestamp/runtime-benchmarks",
]
try-runtime = [
"frame-support/try-runtime",
Expand Down
88 changes: 9 additions & 79 deletions pallets/score-staking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,6 @@ pub mod pallet {
type AdminOrigin: EnsureOrigin<Self::RuntimeOrigin>;
/// AccountId converter
type AccountIdConvert: AccountIdConvert<Self>;
// For extrinsics that should only be called by origins from TEE
type TEECallOrigin: EnsureOrigin<Self::RuntimeOrigin>;
}

#[pallet::error]
Expand Down Expand Up @@ -152,45 +150,20 @@ pub mod pallet {
ScoreUserCountUnderflow,
// when the score user count would exceed `MaxScoreUserCount`
MaxScoreUserCountReached,
// the token staking amount has been updated already for the round
TokenStakingAmountAlreadyUpdated,
}

#[pallet::event]
#[pallet::generate_deposit(pub (crate) fn deposit_event)]
pub enum Event<T: Config> {
PoolStarted {
start_block: BlockNumberFor<T>,
},
PoolStarted { start_block: BlockNumberFor<T> },
PoolStopped {},
ScoreFeederSet {
new_score_feeder: Option<T::AccountId>,
},
RoundConfigSet {
new_config: RoundSetting,
},
ScoreUpdated {
who: Identity,
new_score: Score,
},
ScoreRemoved {
who: Identity,
},
ScoreFeederSet { new_score_feeder: Option<T::AccountId> },
RoundConfigSet { new_config: RoundSetting },
ScoreUpdated { who: Identity, new_score: Score },
ScoreRemoved { who: Identity },
ScoreCleared {},
TokenStakingAmountUpdated {
account: T::AccountId,
amount: BalanceOf<T>,
},
RewardDistributionStarted {
total: BalanceOf<T>,
distributed: BalanceOf<T>,
round_index: RoundIndex,
},
RewardDistributionCompleted {},
RewardClaimed {
who: T::AccountId,
amount: BalanceOf<T>,
},
RewardCalculated { total: BalanceOf<T>, distributed: BalanceOf<T> },
RewardClaimed { who: T::AccountId, amount: BalanceOf<T> },
}

#[pallet::storage]
Expand Down Expand Up @@ -269,8 +242,7 @@ pub mod pallet {

// We are about to start a new round
// 1. update round info
let round_index = r.index.saturating_add(1);
r.index = round_index;
r.index = r.index.saturating_add(1);
r.start_block = now;
Round::<T>::put(r);
weight = weight.saturating_add(T::DbWeight::get().reads_writes(1, 1));
Expand Down Expand Up @@ -307,10 +279,9 @@ pub mod pallet {
weight = weight.saturating_add(T::DbWeight::get().reads_writes(2, 1));
}

Self::deposit_event(Event::<T>::RewardDistributionStarted {
Self::deposit_event(Event::<T>::RewardCalculated {
total: round_reward,
distributed: all_user_reward,
round_index,
});

weight
Expand Down Expand Up @@ -474,47 +445,6 @@ pub mod pallet {
let payment = Scores::<T>::get(&account).ok_or(Error::<T>::UserNotExist)?;
Self::claim(origin, payment.unpaid_reward)
}

#[pallet::call_index(9)]
#[pallet::weight((195_000_000, DispatchClass::Normal))]
pub fn update_token_staking_amount(
origin: OriginFor<T>,
account: T::AccountId,
amount: BalanceOf<T>,
round_index: RoundIndex,
) -> DispatchResultWithPostInfo {
let _ = T::TEECallOrigin::ensure_origin(origin)?;

match Scores::<T>::get(&account) {
Some(mut s) => {
if round_index <= s.last_token_distributed_round {
return Err(Error::<T>::TokenStakingAmountAlreadyUpdated.into());
}
let last_round = Round::<T>::get();
s.last_round_reward += amount;
s.total_reward += amount;
s.unpaid_reward += amount;
s.last_token_distributed_round = last_round.index;
Scores::<T>::insert(account.clone(), s);
Self::deposit_event(Event::TokenStakingAmountUpdated {
account: account.clone(),
amount,
});
Ok(Pays::No.into())
},
None => Err(Error::<T>::UserNotExist.into()),
}
}

#[pallet::call_index(10)]
#[pallet::weight((195_000_000, DispatchClass::Normal))]
pub fn complete_reward_distribution(origin: OriginFor<T>) -> DispatchResultWithPostInfo {
let _ = T::TEECallOrigin::ensure_origin(origin)?;

Self::deposit_event(Event::RewardDistributionCompleted {});

Ok(Pays::No.into())
}
}
}

Expand Down
74 changes: 1 addition & 73 deletions pallets/score-staking/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,8 @@
use crate::{
self as pallet_score_staking, AccountIdConvert, Config, Perbill, PoolState, RoundSetting,
};
use core::marker::PhantomData;
use frame_support::{
assert_ok, construct_runtime, ord_parameter_types,
pallet_prelude::EnsureOrigin,
parameter_types,
assert_ok, construct_runtime, ord_parameter_types, parameter_types,
traits::{OnFinalize, OnInitialize},
};
use frame_system::{EnsureRoot, EnsureSignedBy};
Expand Down Expand Up @@ -54,7 +51,6 @@ construct_runtime!(
Balances: pallet_balances,
ParachainStaking: pallet_parachain_staking,
ScoreStaking: pallet_score_staking,
Teebag: pallet_teebag,
}
);

Expand Down Expand Up @@ -170,66 +166,6 @@ impl pallet_score_staking::Config for Test {
type YearlyIssuance = ConstU128<{ 100_000_000 * UNIT }>;
type YearlyInflation = DefaultYearlyInflation;
type MaxScoreUserCount = ConstU32<2>;
type TEECallOrigin = EnsureEnclaveSigner<Self>;
}

parameter_types! {
pub const MinimumPeriod: u64 = 6000 / 2;
}

pub type Moment = u64;

impl pallet_timestamp::Config for Test {
type Moment = Moment;
type OnTimestampSet = ();
type MinimumPeriod = MinimumPeriod;
type WeightInfo = ();
}

parameter_types! {
pub const MomentsPerDay: u64 = 86_400_000; // [ms/d]
}

impl pallet_teebag::Config for Test {
type RuntimeEvent = RuntimeEvent;
type MomentsPerDay = MomentsPerDay;
type SetAdminOrigin = EnsureRoot<Self::AccountId>;
type MaxEnclaveIdentifier = ConstU32<1>;
type MaxAuthorizedEnclave = ConstU32<2>;
type WeightInfo = ();
}

pub struct EnsureEnclaveSigner<T>(PhantomData<T>);
impl<T> EnsureOrigin<T::RuntimeOrigin> for EnsureEnclaveSigner<T>
where
T: frame_system::Config + pallet_teebag::Config + pallet_timestamp::Config<Moment = u64>,
<T as frame_system::Config>::AccountId: From<[u8; 32]>,
<T as frame_system::Config>::Hash: From<[u8; 32]>,
{
type Success = T::AccountId;
fn try_origin(o: T::RuntimeOrigin) -> Result<Self::Success, T::RuntimeOrigin> {
o.into().and_then(|o| match o {
frame_system::RawOrigin::Signed(who)
if pallet_teebag::EnclaveRegistry::<T>::contains_key(&who) =>
{
Ok(who)
},
r => Err(T::RuntimeOrigin::from(r)),
})
}

#[cfg(feature = "runtime-benchmarks")]
fn try_successful_origin() -> Result<T::RuntimeOrigin, ()> {
use pallet_teebag::test_util::{get_signer, TEST8_MRENCLAVE, TEST8_SIGNER_PUB};
let signer: <T as frame_system::Config>::AccountId = get_signer(TEST8_SIGNER_PUB);
if !pallet_teebag::EnclaveRegistry::<T>::contains_key(signer.clone()) {
assert_ok!(pallet_teebag::Pallet::<T>::add_enclave(
&signer,
&pallet_teebag::Enclave::default().with_mrenclave(TEST8_MRENCLAVE),
));
}
Ok(frame_system::RawOrigin::Signed(signer).into())
}
}

pub fn alice() -> AccountId {
Expand All @@ -250,14 +186,6 @@ pub fn new_test_ext(fast_round: bool) -> sp_io::TestExternalities {
.assimilate_storage(&mut t)
.unwrap();

pallet_teebag::GenesisConfig::<Test> {
allow_sgx_debug_mode: true,
admin: Some(AccountKeyring::Alice.to_account_id()),
mode: pallet_teebag::OperationalMode::Production,
}
.assimilate_storage(&mut t)
.unwrap();

pallet_score_staking::GenesisConfig::<Test> {
state: PoolState::Stopped,
marker: Default::default(),
Expand Down
Loading

0 comments on commit 7607ebf

Please sign in to comment.