diff --git a/src/extrinsic/mod.rs b/src/extrinsic/mod.rs index c3b90feac0..65643db1e0 100644 --- a/src/extrinsic/mod.rs +++ b/src/extrinsic/mod.rs @@ -17,7 +17,6 @@ //! Create signed or unsigned extrinsics. mod extra; -mod mortal_period; mod signer; pub use self::{ @@ -26,7 +25,6 @@ pub use self::{ Extra, SignedExtra, }, - mortal_period::derive_mortal_period, signer::{ PairSigner, Signer, diff --git a/src/extrinsic/mortal_period.rs b/src/extrinsic/mortal_period.rs deleted file mode 100644 index 115705438a..0000000000 --- a/src/extrinsic/mortal_period.rs +++ /dev/null @@ -1,55 +0,0 @@ -// Copyright 2019-2020 Parity Technologies (UK) Ltd. -// This file is part of substrate-subxt. -// -// subxt is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// subxt is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with substrate-subxt. If not, see . - -use crate::{ - Error, - Metadata, -}; - -/// 5 min `mortal_period` in milliseconds, to be adjusted based on expected block time -pub const BASELINE_MORTAL_PERIOD: u64 = 5 * 60 * 1000; - -/// Fallback `BlockHashCount` of 2,400 blocks -pub const FALLBACK_BLOCK_HASH_COUNT: u64 = 2_400; - -/// Fallback expected block time of 6,000 milliseconds -pub const FALLBACK_EXPECTED_BLOCK_TIME: u64 = 6_000; - -/// Derive a default mortal period based on a chain's metadata -pub fn derive_mortal_period(metadata: &Metadata) -> Result { - let block_hash_count = if let Ok(system_meta) = metadata.module("System") { - if let Ok(count) = system_meta.constant("BlockHashCount") { - count.value::()?.into() - } else { - FALLBACK_BLOCK_HASH_COUNT - } - } else { - FALLBACK_BLOCK_HASH_COUNT - }; - let block_time = if let Ok(babe_meta) = metadata.module("Babe") { - if let Ok(milliseconds) = babe_meta.constant("ExpectedBlockTime") { - milliseconds.value::()? - } else { - FALLBACK_EXPECTED_BLOCK_TIME - } - } else { - FALLBACK_EXPECTED_BLOCK_TIME - }; - - Ok((BASELINE_MORTAL_PERIOD / block_time) - .next_power_of_two() - .min(block_hash_count.next_power_of_two())) -} diff --git a/src/lib.rs b/src/lib.rs index b69fead1ff..f542bb188d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -89,7 +89,6 @@ pub use crate::{ RawEvent, }, extrinsic::{ - derive_mortal_period, PairSigner, SignedExtra, Signer, @@ -180,8 +179,8 @@ impl ClientBuilder { rpc.system_properties(), ) .await; - let metadata = metadata?; - let mortal_period = derive_mortal_period(&metadata).ok(); + let metadata = metadata?; + let mortal_period = Some(metadata.derive_mortal_period()?); Ok(Client { rpc, genesis_hash: genesis_hash?, diff --git a/src/metadata.rs b/src/metadata.rs index 27b3446cbd..3b9769b81f 100644 --- a/src/metadata.rs +++ b/src/metadata.rs @@ -40,6 +40,15 @@ use sp_core::storage::StorageKey; use crate::Encoded; +/// 5 min `mortal_period`in milliseconds, to be adjusted based on expected block time +pub const BASELINE_MORTAL_PERIOD: u64 = 5 * 60 * 1000; + +/// Fallback `BlockHashCount` +pub const FALLBACK_BLOCK_HASH_COUNT: u64 = 2_400; + +/// Fallback expected block time in milliseconds +pub const FALLBACK_EXPECTED_BLOCK_TIME: u64 = 6_000; + /// Metadata error. #[derive(Debug, thiserror::Error)] pub enum MetadataError { @@ -164,7 +173,33 @@ impl Metadata { } } string - } + } + + /// Derive a default mortal period + pub(crate) fn derive_mortal_period(&self) -> Result { + let block_hash_count = if let Ok(system_meta) = self.module("System") { + if let Ok(count) = system_meta.constant("BlockHashCount") { + count.value::()?.into() + } else { + FALLBACK_BLOCK_HASH_COUNT + } + } else { + FALLBACK_BLOCK_HASH_COUNT + }; + let block_time = if let Ok(babe_meta) = self.module("Babe") { + if let Ok(milliseconds) = babe_meta.constant("ExpectedBlockTime") { + milliseconds.value::()? + } else { + FALLBACK_EXPECTED_BLOCK_TIME + } + } else { + FALLBACK_EXPECTED_BLOCK_TIME + }; + + Ok((BASELINE_MORTAL_PERIOD / block_time) + .next_power_of_two() + .min(block_hash_count.next_power_of_two())) + } } #[derive(Clone, Debug)]