Skip to content

Commit 138f37f

Browse files
georgepisaltudmitry-markin
authored andcommitted
Add auth refresh
Signed-off-by: georgepisaltu <[email protected]>
1 parent cf3e28f commit 138f37f

File tree

3 files changed

+123
-0
lines changed

3 files changed

+123
-0
lines changed

pallets/transaction-storage/src/benchmarking.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,29 @@ mod benchmarks {
181181
Ok(())
182182
}
183183

184+
#[benchmark]
185+
fn refresh_account_authorization() -> Result<(), BenchmarkError> {
186+
let origin = T::Authorizer::try_successful_origin()
187+
.map_err(|_| BenchmarkError::Stop("unable to compute origin"))?;
188+
let who: T::AccountId = whitelisted_caller();
189+
let transactions = 10;
190+
let bytes: u64 = 1024 * 1024;
191+
let origin2 = origin.clone();
192+
TransactionStorage::<T>::authorize_account(
193+
origin2 as T::RuntimeOrigin,
194+
who.clone(),
195+
transactions,
196+
bytes,
197+
)
198+
.map_err(|_| BenchmarkError::Stop("unable to authorize account"))?;
199+
200+
#[extrinsic_call]
201+
_(origin as T::RuntimeOrigin, who.clone());
202+
203+
assert_last_event::<T>(Event::AccountAuthorizationRefreshed { who }.into());
204+
Ok(())
205+
}
206+
184207
#[benchmark]
185208
fn authorize_preimage() -> Result<(), BenchmarkError> {
186209
let origin = T::Authorizer::try_successful_origin()
@@ -195,6 +218,27 @@ mod benchmarks {
195218
Ok(())
196219
}
197220

221+
#[benchmark]
222+
fn refresh_preimage_authorization() -> Result<(), BenchmarkError> {
223+
let origin = T::Authorizer::try_successful_origin()
224+
.map_err(|_| BenchmarkError::Stop("unable to compute origin"))?;
225+
let hash = [0u8; 32];
226+
let max_size: u64 = 1024 * 1024;
227+
let origin2 = origin.clone();
228+
TransactionStorage::<T>::authorize_preimage(
229+
origin2 as T::RuntimeOrigin,
230+
hash.clone(),
231+
max_size,
232+
)
233+
.map_err(|_| BenchmarkError::Stop("unable to authorize account"))?;
234+
235+
#[extrinsic_call]
236+
_(origin as T::RuntimeOrigin, hash.clone());
237+
238+
assert_last_event::<T>(Event::PreimageAuthorizationRefreshed { hash }.into());
239+
Ok(())
240+
}
241+
198242
#[benchmark]
199243
fn remove_expired_account_authorization() -> Result<(), BenchmarkError> {
200244
let origin = T::Authorizer::try_successful_origin()

pallets/transaction-storage/src/lib.rs

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,51 @@ pub mod pallet {
513513
Self::deposit_event(Event::ExpiredPreimageAuthorizationRemoved { hash });
514514
Ok(())
515515
}
516+
517+
/// Refresh the expiration of an existing authorization for an account.
518+
///
519+
/// If the account does not have an authorization, the call will fail.
520+
///
521+
/// Parameters:
522+
///
523+
/// - `who`: The account to be credited with an authorization to store data.
524+
///
525+
/// The origin for this call must be the pallet's `Authorizer`. Emits
526+
/// [`AccountAuthorizationRefreshed`](Event::AccountAuthorizationRefreshed) when successful.
527+
#[pallet::call_index(7)]
528+
#[pallet::weight(T::WeightInfo::refresh_account_authorization())]
529+
pub fn refresh_account_authorization(
530+
origin: OriginFor<T>,
531+
who: T::AccountId,
532+
) -> DispatchResult {
533+
T::Authorizer::ensure_origin(origin)?;
534+
Self::refresh_authorization(AuthorizationScope::Account(who.clone()))?;
535+
Self::deposit_event(Event::AccountAuthorizationRefreshed { who });
536+
Ok(())
537+
}
538+
539+
/// Refresh the expiration of an existing authorization for a preimage of a BLAKE2b hash.
540+
///
541+
/// If the preimage does not have an authorization, the call will fail.
542+
///
543+
/// Parameters:
544+
///
545+
/// - `hash`: The BLAKE2b hash of the data to be submitted.
546+
///
547+
/// The origin for this call must be the pallet's `Authorizer`. Emits
548+
/// [`PreimageAuthorizationRefreshed`](Event::PreimageAuthorizationRefreshed) when
549+
/// successful.
550+
#[pallet::call_index(8)]
551+
#[pallet::weight(T::WeightInfo::refresh_preimage_authorization())]
552+
pub fn refresh_preimage_authorization(
553+
origin: OriginFor<T>,
554+
hash: ContentHash,
555+
) -> DispatchResult {
556+
T::Authorizer::ensure_origin(origin)?;
557+
Self::refresh_authorization(AuthorizationScope::Preimage(hash))?;
558+
Self::deposit_event(Event::PreimageAuthorizationRefreshed { hash });
559+
Ok(())
560+
}
516561
}
517562

518563
#[pallet::event]
@@ -526,9 +571,13 @@ pub mod pallet {
526571
ProofChecked,
527572
/// An account `who` was authorized to store `bytes` bytes in `transactions` transactions.
528573
AccountAuthorized { who: T::AccountId, transactions: u32, bytes: u64 },
574+
/// An authorization for account `who` was refreshed.
575+
AccountAuthorizationRefreshed { who: T::AccountId },
529576
/// Authorization was given for a preimage of `hash` (not exceeding `max_size`) to be
530577
/// stored by anyone.
531578
PreimageAuthorized { hash: ContentHash, max_size: u64 },
579+
/// An authorization for a preimage of `hash` was refreshed.
580+
PreimageAuthorizationRefreshed { hash: ContentHash },
532581
/// An expired account authorization was removed.
533582
ExpiredAccountAuthorizationRemoved { who: T::AccountId },
534583
/// An expired preimage authorization was removed.
@@ -678,6 +727,22 @@ pub mod pallet {
678727
});
679728
}
680729

730+
/// Authorize data storage.
731+
fn refresh_authorization(scope: AuthorizationScopeFor<T>) -> DispatchResult {
732+
let expiration = frame_system::Pallet::<T>::block_number()
733+
.saturating_add(T::AuthorizationPeriod::get());
734+
735+
Authorizations::<T>::mutate(&scope, |maybe_authorization| {
736+
if let Some(authorization) = maybe_authorization {
737+
authorization.expiration = expiration;
738+
Ok(())
739+
} else {
740+
// No previous authorization to refresh.
741+
return Err(Error::<T>::AuthorizationNotFound.into())
742+
}
743+
})
744+
}
745+
681746
/// Remove an expired authorization.
682747
fn remove_expired_authorization(scope: AuthorizationScopeFor<T>) -> DispatchResult {
683748
// In the case of a regular unsigned transaction, pre_dispatch should have checked that

pallets/transaction-storage/src/weights.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,9 @@ pub trait WeightInfo {
5656
fn renew() -> Weight;
5757
fn check_proof() -> Weight;
5858
fn authorize_account() -> Weight;
59+
fn refresh_account_authorization() -> Weight;
5960
fn authorize_preimage() -> Weight;
61+
fn refresh_preimage_authorization() -> Weight;
6062
fn remove_expired_account_authorization() -> Weight;
6163
fn remove_expired_preimage_authorization() -> Weight;
6264
}
@@ -121,9 +123,15 @@ impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
121123
fn authorize_account() -> Weight {
122124
Weight::from_parts(1_000, 1_000)
123125
}
126+
fn refresh_account_authorization() -> Weight {
127+
Weight::from_parts(1_000, 1_000)
128+
}
124129
fn authorize_preimage() -> Weight {
125130
Weight::from_parts(1_000, 1_000)
126131
}
132+
fn refresh_preimage_authorization() -> Weight {
133+
Weight::from_parts(1_000, 1_000)
134+
}
127135
fn remove_expired_account_authorization() -> Weight {
128136
Weight::from_parts(1_000, 1_000)
129137
}
@@ -191,9 +199,15 @@ impl WeightInfo for () {
191199
fn authorize_account() -> Weight {
192200
Weight::from_parts(1_000, 1_000)
193201
}
202+
fn refresh_account_authorization() -> Weight {
203+
Weight::from_parts(1_000, 1_000)
204+
}
194205
fn authorize_preimage() -> Weight {
195206
Weight::from_parts(1_000, 1_000)
196207
}
208+
fn refresh_preimage_authorization() -> Weight {
209+
Weight::from_parts(1_000, 1_000)
210+
}
197211
fn remove_expired_account_authorization() -> Weight {
198212
Weight::from_parts(1_000, 1_000)
199213
}

0 commit comments

Comments
 (0)