From 598686939088db90044040d43fd77461b9ece211 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Drwi=C4=99ga?= Date: Fri, 3 Jan 2020 21:39:46 +0100 Subject: [PATCH] Fix `period` of offchain transactions. (#4521) * Fix period of offchain transactions. * Calculate period dynamically. * Convert to u64. --- substrate/bin/node/runtime/src/lib.rs | 6 +++++- substrate/primitives/runtime/src/generic/era.rs | 7 +++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/substrate/bin/node/runtime/src/lib.rs b/substrate/bin/node/runtime/src/lib.rs index 7a8ba7613b..b525546a11 100644 --- a/substrate/bin/node/runtime/src/lib.rs +++ b/substrate/bin/node/runtime/src/lib.rs @@ -500,7 +500,11 @@ impl frame_system::offchain::CreateTransaction for account: AccountId, index: Index, ) -> Option<(Call, ::SignaturePayload)> { - let period = 1 << 8; + // take the biggest period possible. + let period = BlockHashCount::get() + .checked_next_power_of_two() + .map(|c| c / 2) + .unwrap_or(2) as u64; let current_block = System::block_number().saturated_into::(); let tip = 0; let extra: SignedExtra = ( diff --git a/substrate/primitives/runtime/src/generic/era.rs b/substrate/primitives/runtime/src/generic/era.rs index c72e8f8b0f..8eeb550b2e 100644 --- a/substrate/primitives/runtime/src/generic/era.rs +++ b/substrate/primitives/runtime/src/generic/era.rs @@ -40,6 +40,9 @@ pub enum Era { /// implies which block hash is included in the signature material). If the `period` is /// greater than 1 << 12, then it will be a factor of the times greater than 1<<12 that /// `period` is. + /// + /// When used on `FRAME`-based runtimes, `period` cannot exceed `BlockHashCount` parameter + /// of `system` module. Mortal(Period, Phase), } @@ -55,6 +58,10 @@ n = Q(current - phase, period) + phase impl Era { /// Create a new era based on a period (which should be a power of two between 4 and 65536 inclusive) /// and a block number on which it should start (or, for long periods, be shortly after the start). + /// + /// If using `Era` in the context of `FRAME` runtime, make sure that `period` + /// does not exceed `BlockHashCount` parameter passed to `system` module, since that + /// prunes old blocks and renders transactions immediately invalid. pub fn mortal(period: u64, current: u64) -> Self { let period = period.checked_next_power_of_two() .unwrap_or(1 << 16)