Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
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
1 change: 1 addition & 0 deletions Cargo.lock

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

8 changes: 8 additions & 0 deletions prdoc/pr_11027.prdoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
title: Add functions to control statement store per-account allowances
doc:
- audience: Node Dev
description: 'The functions currently residing in `individuality`
are moved to Substrate to unify storage allowance control. '
crates:
- name: sp-statement-store
bump: minor
2 changes: 2 additions & 0 deletions substrate/primitives/statement-store/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ targets = ["x86_64-unknown-linux-gnu"]

[dependencies]
codec = { features = ["derive"], workspace = true }
frame-support = { workspace = true }
scale-info = { features = ["derive"], workspace = true }
serde = { optional = true, features = ["alloc", "derive"], workspace = true }
sp-api = { workspace = true }
Expand Down Expand Up @@ -45,6 +46,7 @@ std = [
"codec/std",
"curve25519-dalek",
"ed25519-dalek",
"frame-support/std",
"hkdf",
"hkdf?/std",
"rand",
Expand Down
44 changes: 43 additions & 1 deletion substrate/primitives/statement-store/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ pub const MAX_TOPICS: usize = 4;
pub const MAX_ANY_TOPICS: usize = 128;

/// Statement allowance limits for an account.
#[derive(Encode, Decode, TypeInfo, Debug, Clone, Copy, PartialEq, Eq)]
#[derive(Clone, Default, PartialEq, Eq, Encode, Decode, DecodeWithMemTracking, Debug, TypeInfo)]
pub struct StatementAllowance {
/// Maximum number of statements allowed
pub max_count: u32,
Expand All @@ -139,6 +139,22 @@ impl StatementAllowance {
pub fn new(max_count: u32, max_size: u32) -> Self {
Self { max_count, max_size }
}

/// Saturating addition of statement allowances.
pub const fn saturating_add(self, rhs: StatementAllowance) -> StatementAllowance {
StatementAllowance {
max_count: self.max_count.saturating_add(rhs.max_count),
max_size: self.max_size.saturating_add(rhs.max_size),
}
}

/// Saturating subtraction of statement allowances.
pub const fn saturating_sub(self, rhs: StatementAllowance) -> StatementAllowance {
StatementAllowance {
max_count: self.max_count.saturating_sub(rhs.max_count),
max_size: self.max_size.saturating_sub(rhs.max_size),
}
}
}

/// Storage key prefix for per-account statement allowances.
Expand All @@ -157,6 +173,32 @@ pub fn statement_allowance_key(account_id: impl AsRef<[u8]>) -> Vec<u8> {
key
}

/// Increase the statement allowance by the given amount.
pub fn increase_allowance_by(account_id: impl AsRef<[u8]>, by: StatementAllowance) {
let key = statement_allowance_key(account_id);
let mut allowance: StatementAllowance = frame_support::storage::unhashed::get_or_default(&key);
allowance = allowance.saturating_add(by);
frame_support::storage::unhashed::put(&key, &allowance);
}

/// Decrease the statement allowance by the given amount.
pub fn decrease_allowance_by(account_id: impl AsRef<[u8]>, by: StatementAllowance) {
let key = statement_allowance_key(account_id);
let mut allowance: StatementAllowance = frame_support::storage::unhashed::get_or_default(&key);
allowance = allowance.saturating_sub(by);
if allowance.max_count == 0 || allowance.max_size == 0 {
frame_support::storage::unhashed::kill(&key);
} else {
frame_support::storage::unhashed::put(&key, &allowance);
}
}

/// Get the statement allowance for the given account.
pub fn get_allowance(account_id: impl AsRef<[u8]>) -> StatementAllowance {
let key = statement_allowance_key(account_id);
frame_support::storage::unhashed::get_or_default(&key)
}

#[cfg(feature = "std")]
pub use store_api::{
Error, FilterDecision, InvalidReason, OptimizedTopicFilter, RejectionReason, Result,
Expand Down
Loading