Skip to content

Commit c65c3f5

Browse files
committed
Add migration SetRetentionPeriodIfZero to ensure RetentionPeriod is non-zero for live solochain
1 parent 929dcee commit c65c3f5

File tree

5 files changed

+75
-4
lines changed

5 files changed

+75
-4
lines changed

pallets/transaction-storage/src/lib.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
mod benchmarking;
2828
pub mod weights;
2929

30+
pub mod migrations;
3031
#[cfg(test)]
3132
mod mock;
3233
#[cfg(test)]
@@ -37,7 +38,10 @@ use core::fmt::Debug;
3738
use polkadot_sdk_frame::{
3839
deps::{sp_core::sp_std::prelude::*, *},
3940
prelude::*,
40-
traits::fungible::{Balanced, Credit, Inspect, Mutate, MutateHold},
41+
traits::{
42+
fungible::{Balanced, Credit, Inspect, Mutate, MutateHold},
43+
parameter_types,
44+
},
4145
};
4246
use sp_transaction_storage_proof::{
4347
encode_index, num_chunks, random_chunk, ChunkIndex, InherentError, TransactionStorageProof,
@@ -57,6 +61,9 @@ const LOG_TARGET: &str = "runtime::transaction-storage";
5761

5862
/// Default retention period for data (in blocks).
5963
pub const DEFAULT_RETENTION_PERIOD: u32 = 100800;
64+
parameter_types! {
65+
pub const DefaultRetentionPeriod: u32 = DEFAULT_RETENTION_PERIOD;
66+
}
6067

6168
// TODO: https://github.com/paritytech/polkadot-bulletin-chain/issues/139 - Clarify purpose of allocator limits and decide whether to remove or use these constants.
6269
/// Maximum bytes that can be stored in one transaction.
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright (C) Parity Technologies (UK) Ltd.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
16+
use crate::{Config, RetentionPeriod, LOG_TARGET};
17+
use core::marker::PhantomData;
18+
use polkadot_sdk_frame::{
19+
prelude::{BlockNumberFor, Weight},
20+
traits::{Get, OnRuntimeUpgrade, Zero},
21+
};
22+
23+
/// Runtime migration that sets the `RetentionPeriod` storage item to a
24+
/// non-zero `NewValue` value **only if it is currently zero**.
25+
///
26+
/// Idempotent migration: safe to run multiple times
27+
pub struct SetRetentionPeriodIfZero<T, NewValue>(PhantomData<(T, NewValue)>);
28+
impl<T: Config, NewValue: Get<BlockNumberFor<T>>> OnRuntimeUpgrade
29+
for SetRetentionPeriodIfZero<T, NewValue>
30+
{
31+
fn on_runtime_upgrade() -> Weight {
32+
let mut weight = T::DbWeight::get().reads(1);
33+
34+
// If zero, let's reset.
35+
if RetentionPeriod::<T>::get().is_zero() {
36+
RetentionPeriod::<T>::set(NewValue::get());
37+
weight.saturating_accrue(T::DbWeight::get().writes(1));
38+
39+
tracing::warn!(
40+
target: LOG_TARGET,
41+
"[SetRetentionPeriodIfZero] RetentionPeriod was zero; set to {:?}",
42+
NewValue::get()
43+
);
44+
}
45+
46+
weight
47+
}
48+
}

runtime/src/lib.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -647,7 +647,12 @@ pub mod migrations {
647647
pub type Unreleased = ();
648648

649649
/// Migrations/checks that do not need to be versioned and can run on every update.
650-
pub type Permanent = ();
650+
pub type Permanent = (
651+
pallet_transaction_storage::migrations::SetRetentionPeriodIfZero<
652+
crate::Runtime,
653+
pallet_transaction_storage::DefaultRetentionPeriod,
654+
>,
655+
);
651656

652657
/// All single block migrations that will run on the next runtime upgrade.
653658
pub type SingleBlockMigrations = (Unreleased, Permanent);

runtimes/bulletin-polkadot/src/lib.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -724,7 +724,12 @@ pub mod migrations {
724724
pub type Unreleased = ();
725725

726726
/// Migrations/checks that do not need to be versioned and can run on every update.
727-
pub type Permanent = ();
727+
pub type Permanent = (
728+
pallet_transaction_storage::migrations::SetRetentionPeriodIfZero<
729+
crate::Runtime,
730+
pallet_transaction_storage::DefaultRetentionPeriod,
731+
>,
732+
);
728733

729734
/// All single block migrations that will run on the next runtime upgrade.
730735
pub type SingleBlockMigrations = (Unreleased, Permanent);

runtimes/bulletin-westend/src/lib.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,13 @@ pub mod migrations {
140140
);
141141

142142
/// Migrations/checks that do not need to be versioned and can run on every update.
143-
pub type Permanent = (pallet_xcm::migration::MigrateToLatestXcmVersion<Runtime>,);
143+
pub type Permanent = (
144+
pallet_xcm::migration::MigrateToLatestXcmVersion<Runtime>,
145+
pallet_transaction_storage::migrations::SetRetentionPeriodIfZero<
146+
Runtime,
147+
pallet_transaction_storage::DefaultRetentionPeriod,
148+
>,
149+
);
144150

145151
/// All single block migrations that will run on the next runtime upgrade.
146152
pub type SingleBlockMigrations = (Unreleased, Permanent);

0 commit comments

Comments
 (0)