Repository navigation
Make the player statement limit a dynamic parameter #206
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
base: main
Are you sure you want to change the base?
Changes from all commits
c1a3790
c2efc38
c5cb0e2
05b3514
620f000
e5ce15a
95a06b9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -191,6 +191,16 @@ | |
| //! All players in the game are given some statement store usage allowance. The player's allowance | ||
| //! is cleared and their statements removed when archived or offboarded. | ||
| //! | ||
| //! Every live player holds [`PlayerStatementLimitsRecord::granted`]. The statement store sums the | ||
| //! grants of all pallets per account, so a revocation uses the granted amount, not the current | ||
| //! `PlayerStatementLimit`. The player processing of the next game moves every live player to a | ||
| //! changed `PlayerStatementLimit`. Until then, new players get the previous limit. | ||
|
agustinustheo marked this conversation as resolved.
Resolved
|
||
| //! The pallet accepts no sign-up, offboard, statement account change or game cancellation | ||
| //! during player processing, so one record covers every live player. | ||
| //! | ||
| //! A `PlayerStatementLimit` with a zero dimension grants zero. The statement store deletes such | ||
| //! an allowance, so a later revocation of the other dimension would reduce the restored limit. | ||
| //! | ||
| //! # NFT claim credits | ||
| //! | ||
| //! A player earns one credit per successful report of one player on another, in one round of one | ||
|
|
@@ -338,6 +348,7 @@ use indiv_pallet_score::AccountOrPerson; | |
| use indiv_support::{ | ||
| credit_trees::AwardCredits, | ||
| offchain::{submit_authorized, TX_LONGEVITY}, | ||
| statement_allowance::BoundedStatementAllowance, | ||
| traits::{Alias, CommunicationIdentifier, Context}, | ||
| tx_priority, | ||
| weight_budget::OcwWeightBudget, | ||
|
|
@@ -393,7 +404,7 @@ pub mod pallet { | |
| pub const PRODUCT_NAME: &[u8] = b"dim2"; | ||
|
|
||
| /// The current storage version. | ||
| const STORAGE_VERSION: StorageVersion = StorageVersion::new(1); | ||
| const STORAGE_VERSION: StorageVersion = StorageVersion::new(2); | ||
|
|
||
| #[pallet::pallet] | ||
| #[pallet::storage_version(STORAGE_VERSION)] | ||
|
|
@@ -779,6 +790,13 @@ pub mod pallet { | |
| pub(crate) type StmtAccountToAlias<T: Config> = | ||
| StorageMap<_, Blake2_128Concat, T::AccountId, Alias>; | ||
|
|
||
| /// The statement allowances that the pallet grants to live players. | ||
| /// | ||
| /// It is unset only while no player holds an allowance. The first grant then stores the | ||
| /// current `PlayerStatementLimit`. | ||
| #[pallet::storage] | ||
| pub type PlayerStatementLimits<T: Config> = StorageValue<_, PlayerStatementLimitsRecord>; | ||
|
|
||
| /// The communication identifiers used by players to establish an encrypted P2P connection in | ||
| /// order to play the game. The account under which the communication identifier is registered | ||
| /// should be the same account used to interact with the statement store. | ||
|
|
@@ -1562,10 +1580,7 @@ pub mod pallet { | |
|
|
||
| if let Some(stmt_account) = stmt_account { | ||
| if player_is_offboarding { | ||
| decrease_allowance_by( | ||
| stmt_account.clone().into(), | ||
| T::PlayerStatementLimit::get(), | ||
| ); | ||
| Self::revoke_player_allowance(&stmt_account); | ||
| } | ||
| Self::deposit_event(Event::<T>::StmtUsageRemoved { who: stmt_account.into() }); | ||
| } | ||
|
|
@@ -2782,6 +2797,69 @@ pub mod pallet { | |
| Self::deposit_event(Event::<T>::GameCancelled { game_index: game.index, reason }); | ||
| } | ||
|
|
||
| /// Return `PlayerStatementLimit`, or zero when one of its dimensions is zero. | ||
| fn player_statement_limit() -> BoundedStatementAllowance { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe let's rename this function, it is too close to |
||
| let limit = T::PlayerStatementLimit::get(); | ||
| if limit.is_depleted() { | ||
| BoundedStatementAllowance::default() | ||
| } else { | ||
| limit.into() | ||
| } | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe we should fix |
||
|
|
||
| /// Return [`PlayerStatementLimits`], or [`Self::player_statement_limit`] when unset. | ||
| pub(crate) fn player_statement_limits() -> PlayerStatementLimitsRecord { | ||
| PlayerStatementLimits::<T>::get().unwrap_or_else(|| PlayerStatementLimitsRecord { | ||
| granted: Self::player_statement_limit(), | ||
| next: None, | ||
| }) | ||
| } | ||
|
|
||
| /// Grant [`PlayerStatementLimitsRecord::granted`] to `account`. | ||
| /// | ||
| /// Store [`PlayerStatementLimits`] when unset, so revocations use the same amount. | ||
| fn grant_player_allowance(account: &T::AccountId) { | ||
| let granted = PlayerStatementLimits::<T>::get() | ||
| .unwrap_or_else(|| { | ||
| let limits = PlayerStatementLimitsRecord { | ||
| granted: Self::player_statement_limit(), | ||
| next: None, | ||
| }; | ||
| PlayerStatementLimits::<T>::put(&limits); | ||
| limits | ||
| }) | ||
| .granted; | ||
| increase_allowance_by(account.clone().into(), granted.into()); | ||
| } | ||
|
|
||
| fn revoke_player_allowance(account: &T::AccountId) { | ||
| let granted = Self::player_statement_limits().granted; | ||
| decrease_allowance_by(account.clone().into(), granted.into()); | ||
| } | ||
|
|
||
| /// Move the allowance of `player_id` from [`PlayerStatementLimitsRecord::granted`] to | ||
| /// [`PlayerStatementLimitsRecord::next`], if set. | ||
| /// | ||
| /// Grant before revoking: the statement store removes the whole allowance of an account | ||
| /// when one dimension reaches zero. | ||
| fn update_player_allowance(player_id: &AccountOrPerson<T::AccountId>) { | ||
| let PlayerStatementLimitsRecord { granted, next: Some(next) } = | ||
| Self::player_statement_limits() | ||
| else { | ||
| return; | ||
| }; | ||
| let stmt_account = match player_id { | ||
| AccountOrPerson::Person(alias) => AliasToStmtAccount::<T>::get(alias) | ||
| .defensive_proof( | ||
| "indiv-pallet-game: alias player must have a statement account", | ||
| ), | ||
| AccountOrPerson::Account(account) => Some(account.clone()), | ||
| }; | ||
| let Some(stmt_account) = stmt_account else { return }; | ||
| increase_allowance_by(stmt_account.clone().into(), next.into()); | ||
| decrease_allowance_by(stmt_account.into(), granted.into()); | ||
| } | ||
|
|
||
| /// **Warning**: Storage must be rollbacked on error. | ||
| fn sign_up_inner( | ||
| args: SignUpArgs<T::AccountId, T::AccountSignature, AirdropProofOf<T>>, | ||
|
|
@@ -2816,10 +2894,7 @@ pub mod pallet { | |
| if prev_stmt_account != Some(statement_account.clone()) { | ||
| if let Some(prev_stmt_account) = prev_stmt_account { | ||
| StmtAccountToAlias::<T>::remove(&prev_stmt_account); | ||
| decrease_allowance_by( | ||
| prev_stmt_account.clone().into(), | ||
| T::PlayerStatementLimit::get(), | ||
| ); | ||
| Self::revoke_player_allowance(&prev_stmt_account); | ||
| Self::deposit_event(Event::<T>::StmtUsageRemoved { | ||
| who: prev_stmt_account.into(), | ||
| }); | ||
|
|
@@ -2838,10 +2913,7 @@ pub mod pallet { | |
|
|
||
| StmtAccountToAlias::<T>::insert(&statement_account, who); | ||
| AliasToStmtAccount::<T>::insert(who, &statement_account); | ||
| increase_allowance_by( | ||
| statement_account.clone().into(), | ||
| T::PlayerStatementLimit::get(), | ||
| ); | ||
| Self::grant_player_allowance(&statement_account); | ||
| } | ||
| CommunicationIdentifiers::<T>::insert(&statement_account, identifier_key); | ||
|
|
||
|
|
@@ -2915,7 +2987,7 @@ pub mod pallet { | |
| // An account-identified player gets allowance when signed up | ||
| if !already_playing { | ||
| if let Some(account) = who.account() { | ||
| increase_allowance_by(account.clone().into(), T::PlayerStatementLimit::get()); | ||
| Self::grant_player_allowance(account); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -3720,10 +3792,7 @@ pub mod pallet { | |
| }; | ||
|
|
||
| if let Some(stmt_account) = stmt_account { | ||
| decrease_allowance_by( | ||
| stmt_account.clone().into(), | ||
| T::PlayerStatementLimit::get(), | ||
| ); | ||
| Self::revoke_player_allowance(&stmt_account); | ||
| Self::deposit_event(Event::<T>::StmtUsageRemoved { who: stmt_account.into() }); | ||
| } | ||
|
|
||
|
|
@@ -3749,6 +3818,7 @@ pub mod pallet { | |
| credibility => credibility, | ||
| }; | ||
|
|
||
| Self::update_player_allowance(&player_id); | ||
| Players::<T>::insert( | ||
| &player_id, | ||
| Player { | ||
|
|
@@ -3796,6 +3866,15 @@ pub mod pallet { | |
| return; | ||
| }; | ||
|
|
||
| if last_iteration.is_none() { | ||
| let limits = Self::player_statement_limits(); | ||
| let limit = Self::player_statement_limit(); | ||
| let next = (limit != limits.granted).then_some(limit); | ||
| if next != limits.next { | ||
| PlayerStatementLimits::<T>::put(PlayerStatementLimitsRecord { next, ..limits }); | ||
| } | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe we could move it into its own function and the code at the end too, then we write down on top of But also it is ok as it is. |
||
|
|
||
| let mut iterator = match last_iteration.clone() { | ||
| None => Players::<T>::iter(), | ||
| Some(last) => Players::<T>::iter_from_key(last), | ||
|
|
@@ -3849,6 +3928,15 @@ pub mod pallet { | |
| "indiv-pallet-game: session was started before the process players phase", | ||
| ); | ||
|
|
||
| if let Some(PlayerStatementLimitsRecord { next: Some(next), .. }) = | ||
| PlayerStatementLimits::<T>::get() | ||
| { | ||
| PlayerStatementLimits::<T>::put(PlayerStatementLimitsRecord { | ||
| granted: next, | ||
| next: None, | ||
| }); | ||
| } | ||
|
|
||
| game.state = | ||
| GameState::PlayerProcess { step: PlayerProcessStep::Step2ClearIndices }; | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the weights need a regen for this.