Skip to content

Commit

Permalink
No minimum block time in Runtime (#250)
Browse files Browse the repository at this point in the history
* Remove mock timestamp provider.

* Relax runtime's minimum timestamp requirement. (We aren't using time-based slots, so it doesn't matter.)

* bump runtime version
  • Loading branch information
JoshOrndorff authored Feb 25, 2021
1 parent 7c32daf commit 1f9999c
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 60 deletions.
70 changes: 16 additions & 54 deletions node/src/inherents.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,10 @@
//! A service builder can call the `build_inherent_data_providers` function to get the providers
//! the node needs based on the parameters it passed in.
//!
//! This module also includes MOCK inherent data providers for both the timestamp and validataion
//! data inherents. These mock providers provide stub data that does not represent anything "real"
//! This module also includes a MOCK inherent data provider for the validataion
//! data inherent. This mock provider provides stub data that does not represent anything "real"
//! about the external world, but can pass the runtime's checks. This is useful in testing
//! for example, running the --dev service without a relay chain backbone, or authoring block
//! extremely quickly in testing scenarios.
//! for example, running the --dev service without a relay chain backbone.

use cumulus_primitives::{
inherents::{SystemInherentData, SYSTEM_INHERENT_IDENTIFIER},
Expand All @@ -32,85 +31,48 @@ use cumulus_primitives::{
use parity_scale_codec::Encode;
use sp_core::H160;
use sp_inherents::{InherentData, InherentDataProviders, InherentIdentifier, ProvideInherentData};
use sp_timestamp::{InherentError, INHERENT_IDENTIFIER as TIMESTAMP_IDENTIFIER};
use std::cell::RefCell;
use sp_timestamp::InherentError;

use cumulus_test_relay_sproof_builder::RelayStateSproofBuilder;
use moonbeam_runtime::MINIMUM_PERIOD;

/// Build the inherent data providers for the node.
///
/// Not all nodes will need all inherent data providers:
/// - The author provider is only necessary for block producing nodes
/// - The timestamp provider can be mocked.
/// - The validation data provider can be mocked.
pub fn build_inherent_data_providers(
author: Option<H160>,
mock: bool,
) -> Result<InherentDataProviders, sc_service::Error> {
let providers = InherentDataProviders::new();

// Timestamp provider. Needed in all nodes.
providers
.register_provider(sp_timestamp::InherentDataProvider)
.map_err(Into::into)
.map_err(sp_consensus::error::Error::InherentData)?;

// Author ID Provider for authoring node only.
if let Some(account) = author {
providers
.register_provider(author_inherent::InherentDataProvider(account.encode()))
.map_err(Into::into)
.map_err(sp_consensus::error::Error::InherentData)?;
}

// Parachain inherent provider, only for dev-service nodes.
if mock {
providers
.register_provider(MockTimestampInherentDataProvider {
duration: MINIMUM_PERIOD * 2,
})
.map_err(Into::into)
.map_err(sp_consensus::error::Error::InherentData)?;

providers
.register_provider(MockValidationDataInherentDataProvider)
.map_err(Into::into)
.map_err(sp_consensus::error::Error::InherentData)?;
} else {
providers
.register_provider(sp_timestamp::InherentDataProvider)
.map_err(Into::into)
.map_err(sp_consensus::error::Error::InherentData)?;

// When we are not mocking the validation data ,we do not register a real validation data
// provider here. The validation data inherent is inserted manually by the cumulus colaltor
// https://github.com/paritytech/cumulus/blob/c3e3f443/collator/src/lib.rs#L274-L321
}

Ok(providers)
}

/// Mocked timestamp inherent data provider.
/// Provides a fake duration starting at 0 in millisecond for timestamp inherent.
/// Each call will increment timestamp by slot_duration making the runtime think time has passed.
/// This code was inspired by https://github.com/paritytech/frontier/pull/170
struct MockTimestampInherentDataProvider {
duration: u64,
}

thread_local!(static TIMESTAMP: RefCell<u64> = RefCell::new(0));

impl ProvideInherentData for MockTimestampInherentDataProvider {
fn inherent_identifier(&self) -> &'static InherentIdentifier {
&TIMESTAMP_IDENTIFIER
}

fn provide_inherent_data(
&self,
inherent_data: &mut InherentData,
) -> Result<(), sp_inherents::Error> {
TIMESTAMP.with(|x| {
*x.borrow_mut() += self.duration;
inherent_data.put_data(TIMESTAMP_IDENTIFIER, &*x.borrow())
})
}
// When we are not mocking the validation data, we do not register a real validation data
// provider here. The validation data inherent is inserted manually by the cumulus colaltor
// https://github.com/paritytech/cumulus/blob/c3e3f443/collator/src/lib.rs#L274-L321

fn error_to_string(&self, error: &[u8]) -> Option<String> {
InherentError::try_from(&TIMESTAMP_IDENTIFIER, error).map(|e| format!("{:?}", e))
}
Ok(providers)
}

/// Inherent data provider that supplies mocked validation data.
Expand Down
7 changes: 2 additions & 5 deletions runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,6 @@ pub type Hash = sp_core::H256;
/// Digest item type.
pub type DigestItem = generic::DigestItem<Hash>;

/// Minimum time between blocks. Slot duration is double this.
pub const MINIMUM_PERIOD: u64 = 3000;

/// Maximum weight per block
pub const MAXIMUM_BLOCK_WEIGHT: Weight = WEIGHT_PER_SECOND / 2;

Expand All @@ -115,7 +112,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
spec_name: create_runtime_str!("moonbeam"),
impl_name: create_runtime_str!("moonbeam"),
authoring_version: 3,
spec_version: 22,
spec_version: 23,
impl_version: 1,
apis: RUNTIME_API_VERSIONS,
transaction_version: 2,
Expand Down Expand Up @@ -186,7 +183,7 @@ impl frame_system::Config for Runtime {
}

parameter_types! {
pub const MinimumPeriod: u64 = MINIMUM_PERIOD;
pub const MinimumPeriod: u64 = 1;
}

impl pallet_timestamp::Config for Runtime {
Expand Down
2 changes: 1 addition & 1 deletion tests/tests/test-polkadot-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ describeWithMoonbeam("Moonbeam Polkadot API", `simple-specs.json`, (context) =>
const message = `${section}.${method}(${args.map((a) => a.toString()).join(", ")})`;
switch (index) {
case 0:
expect(message).to.eq(`timestamp.set(6000)`);
expect(message.substring(0, 13)).to.eq(`timestamp.set`);
break;
case 1:
expect(message.substring(0, 33)).to.eq(`parachainSystem.setValidationData`);
Expand Down

0 comments on commit 1f9999c

Please sign in to comment.