Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Identity staking follow up #2976

Merged
Merged
Show file tree
Hide file tree
Changes from 63 commits
Commits
Show all changes
73 commits
Select commit Hold shift + click to select a range
edec591
refactoring score-staking event, adding new event
silva-fj Aug 7, 2024
addc61f
exporting primitives types
silva-fj Aug 7, 2024
5a12d7b
adding new parentchain event and filters
silva-fj Aug 7, 2024
f81d31b
injecting ocall_api and state_handler to the parentchain event handler
silva-fj Aug 7, 2024
3ff0778
adding missing dependencies
silva-fj Aug 7, 2024
15a0d0c
adding key helper
silva-fj Aug 7, 2024
7da34b3
passing block_header to handle_events
silva-fj Aug 7, 2024
a3ed38d
updating generics for the event handler
silva-fj Aug 7, 2024
a7a9541
adding ParentchainEventProcessingError variant
silva-fj Aug 7, 2024
2c92a8b
fixing generics
silva-fj Aug 7, 2024
7d6ec85
draft logic of the staking updates logic, getting necessary data from
silva-fj Aug 7, 2024
9ebaa37
adding dependency to send extrinsics from the event handler
silva-fj Aug 8, 2024
551c144
refactoring collected data to make it easier to operate
silva-fj Aug 8, 2024
07a4d9d
refactoring how idgraphs are collected
silva-fj Aug 8, 2024
1ae43f9
tentative logic to update scores
silva-fj Aug 8, 2024
554574d
updating TODO
silva-fj Aug 8, 2024
e1c2b06
fixing staking amount calculation
silva-fj Aug 8, 2024
54cdfd1
cleaning up unused code
silva-fj Aug 8, 2024
44213e6
updating pallet events and fixing pallet tests
silva-fj Aug 8, 2024
a9d6518
updating parentchain events
silva-fj Aug 8, 2024
4e17619
adding new field for id_graph rewards
silva-fj Aug 8, 2024
f63032c
adding update_token_staking_amount call to pallet score-staking
silva-fj Aug 8, 2024
72160ab
adding missing dependencies
silva-fj Aug 8, 2024
58c7936
updating score-staking mock
silva-fj Aug 8, 2024
fe012ec
adding tests
silva-fj Aug 8, 2024
bc80b26
Merge remote-tracking branch 'origin/dev' into p-966-identity-staking…
silva-fj Aug 14, 2024
1bcaf5e
fixing Cargo.toml format issue
silva-fj Aug 14, 2024
169582d
adding extrinsic to complete reward distribution
silva-fj Aug 14, 2024
da41659
adding tests
silva-fj Aug 14, 2024
7366a47
updating node-api metadata, including staking-score calls
silva-fj Aug 14, 2024
02d90f5
updating metadata mocks
silva-fj Aug 14, 2024
fe15047
injecting node metadata repository to ParentchainEventHandler
silva-fj Aug 14, 2024
a4465dc
sending extrinsics to the parentchain to update staking scores and
silva-fj Aug 15, 2024
631b988
Merge branch 'dev' into p-966-identity-staking-periodic-update-stakin…
silva-fj Aug 15, 2024
cd1ea06
making score-staking pallet loosely coupled to teebag
silva-fj Aug 15, 2024
375f7e9
updating mock
silva-fj Aug 15, 2024
448768e
updating runtimes
silva-fj Aug 15, 2024
5c2dc8f
removing TODO
silva-fj Aug 15, 2024
f440a5b
updating commend
silva-fj Aug 15, 2024
c041244
Merge remote-tracking branch 'origin/dev' into p-966-identity-staking…
silva-fj Aug 15, 2024
d36294e
updating precompiles mock
silva-fj Aug 15, 2024
ad3c7c7
fixing precompiles tests
silva-fj Aug 15, 2024
8e3e0e1
fixing clippy issues
silva-fj Aug 15, 2024
2dde232
Merge branch 'dev' into p-966-identity-staking-periodic-update-stakin…
silva-fj Aug 21, 2024
844dbd0
adding trusted calls to avoid sending duplicated extrinsics on
silva-fj Aug 21, 2024
51392f6
updating event_handler to submit the new trusted calls
silva-fj Aug 21, 2024
0dbb5d6
cleaning up dependencies that are not needed anymore
silva-fj Aug 21, 2024
a6ff29e
more clean up
silva-fj Aug 21, 2024
ec23008
Merge branch 'dev' into p-966-identity-staking-periodic-update-stakin…
silva-fj Aug 21, 2024
8393226
Merge branch 'dev' into p-966-identity-staking-periodic-update-stakin…
silva-fj Aug 22, 2024
e4d4d30
Merge remote-tracking branch 'origin/dev' into p-966-identity-staking…
silva-fj Aug 23, 2024
a3a6ca3
adding NodeMetadataProvider trait to extend trait bounds
silva-fj Aug 23, 2024
717394f
exposing compose_call from api-client-types
silva-fj Aug 23, 2024
aa3a50e
refactoring to send calls in batches
silva-fj Aug 23, 2024
a4bed37
Merge remote-tracking branch 'origin/dev' into p-966-identity-staking…
silva-fj Aug 26, 2024
d6e1dc7
updating pallet to only update non distributed staking amounts
silva-fj Aug 26, 2024
cd372d5
removing added trusted calls
silva-fj Aug 26, 2024
c024590
updating event
silva-fj Aug 26, 2024
53e9064
injecting node_metadata_repository to the event handler
silva-fj Aug 26, 2024
a4b07e3
refactoring event handler
silva-fj Aug 26, 2024
f629e0c
fixing precompiles tests
silva-fj Aug 27, 2024
d4e78de
Merge branch 'dev' into p-966-identity-staking-periodic-update-stakin…
silva-fj Aug 27, 2024
cee2e50
Merge branch 'dev' into p-966-identity-staking-periodic-update-stakin…
silva-fj Aug 27, 2024
0aaab16
Merge remote-tracking branch 'origin/dev' into p-966-identity-staking…
silva-fj Sep 12, 2024
682ce17
making use of the existing EnsureEnclaveSigner struct
silva-fj Sep 12, 2024
04fc8e0
fixing mocks
silva-fj Sep 12, 2024
a256862
making the distribution directly instead of storing the amount to tok…
silva-fj Sep 12, 2024
3de1822
fixing precompiles tests
silva-fj Sep 12, 2024
a52c90d
reverting try_successful_origin
silva-fj Sep 12, 2024
af81e38
reverting it in identity-management as well
silva-fj Sep 12, 2024
8e375f0
fixing precompiles mock
silva-fj Sep 12, 2024
2db3da4
fixing precompiles score-staking mock
silva-fj Sep 13, 2024
02ce832
Merge branch 'dev' into p-966-identity-staking-periodic-update-stakin…
silva-fj Sep 13, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Cargo.lock

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

5 changes: 5 additions & 0 deletions pallets/score-staking/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ 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 @@ -42,12 +44,15 @@ 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
98 changes: 89 additions & 9 deletions pallets/score-staking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ pub trait AccountIdConvert<T: Config> {
fn convert(account: AccountId32) -> T::AccountId;
}

pub trait TokenStakingAuthorizer<T: Config> {
fn can_update_staking(sender: &T::AccountId) -> bool;
}

#[frame_support::pallet]
pub mod pallet {
use super::*;
Expand Down Expand Up @@ -114,6 +118,8 @@ pub mod pallet {
type AdminOrigin: EnsureOrigin<Self::RuntimeOrigin>;
/// AccountId converter
type AccountIdConvert: AccountIdConvert<Self>;
/// Token staking authorizer
type TokenStakingAuthorizer: TokenStakingAuthorizer<Self>;
}

#[pallet::error]
Expand Down Expand Up @@ -150,20 +156,45 @@ 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 {},
RewardCalculated { total: BalanceOf<T>, distributed: BalanceOf<T> },
RewardClaimed { who: T::AccountId, amount: BalanceOf<T> },
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>,
},
}

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

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

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

weight
Expand Down Expand Up @@ -445,6 +478,53 @@ 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 sender = ensure_signed(origin.clone())?;
ensure!(
T::TokenStakingAuthorizer::can_update_staking(&sender),
Error::<T>::UnauthorizedOrigin
);

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.token_staking_amount = 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 {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm strange - where is the token distribution process? (I thought it would be done within this extrinsic)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just to trigger an event that marks the distribution for the round as completed. (Currently we don't do anything with the token_staking_amount, will there be another phase/issue to come?)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No I mean "distribution for the round as completed" should be triggered by the worker - when it finishes with staking amount upload for all accounts, it should trigger the distribution

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have updated the code to make the distribution on update_token_staking_amount, this extrinsic will be kept to signal that the distribution for the round is completed.

let sender = ensure_signed(origin.clone())?;
ensure!(
T::TokenStakingAuthorizer::can_update_staking(&sender),
Error::<T>::UnauthorizedOrigin
);

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

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

Expand Down
42 changes: 42 additions & 0 deletions pallets/score-staking/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ construct_runtime!(
Balances: pallet_balances,
ParachainStaking: pallet_parachain_staking,
ScoreStaking: pallet_score_staking,
Teebag: pallet_teebag,
}
);

Expand Down Expand Up @@ -166,6 +167,39 @@ impl pallet_score_staking::Config for Test {
type YearlyIssuance = ConstU128<{ 100_000_000 * UNIT }>;
type YearlyInflation = DefaultYearlyInflation;
type MaxScoreUserCount = ConstU32<2>;
type TokenStakingAuthorizer = pallet_teebag::Pallet<Test>;
}

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 = ();
}

impl pallet_score_staking::TokenStakingAuthorizer<Test> for pallet_teebag::Pallet<Test> {
fn can_update_staking(sender: &<Test as frame_system::Config>::AccountId) -> bool {
pallet_teebag::Pallet::<Test>::enclave_registry(sender).is_some()
}
}

pub fn alice() -> AccountId {
Expand All @@ -186,6 +220,14 @@ 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
Loading