diff --git a/polkadot/api/src/lib.rs b/polkadot/api/src/lib.rs index c2c0ae82aeb33..848bf0c72c418 100644 --- a/polkadot/api/src/lib.rs +++ b/polkadot/api/src/lib.rs @@ -233,6 +233,8 @@ impl PolkadotApi for Client> extrinsics: extrinsics.clone(), }; + builder.initialise_block()?; + for inherent in extrinsics { builder.apply_extrinsic(inherent)?; } @@ -255,6 +257,32 @@ pub struct ClientBlockBuilder { impl ClientBlockBuilder where S::Error: Into { + // executes a extrinsic, inherent or otherwise, without appending to the list + fn initialise_block(&mut self) -> Result<()> { + let mut ext = state_machine::Ext { + overlay: &mut self.changes, + backend: &self.state, + }; + + let h = self.header.clone(); + + let result = ::substrate_executor::with_native_environment( + &mut ext, + || runtime::Executive::initialise_block(&h), + ).map_err(Into::into); + + match result { + Ok(_) => { + ext.overlay.commit_prospective(); + Ok(()) + } + Err(e) => { + ext.overlay.discard_prospective(); + Err(e) + } + } + } + // executes a extrinsic, inherent or otherwise, without appending to the list fn apply_extrinsic(&mut self, extrinsic: UncheckedExtrinsic) -> Result<()> { let mut ext = state_machine::Ext { @@ -340,10 +368,10 @@ mod tests { validators: validators(), session_length: 100, }), - council: None, - democracy: None, - parachains: None, - staking: None, + council: Some(Default::default()), + democracy: Some(Default::default()), + parachains: Some(Default::default()), + staking: Some(Default::default()), }; ::client::new_in_mem( LocalDispatch::new(), diff --git a/substrate/runtime/executive/src/lib.rs b/substrate/runtime/executive/src/lib.rs index 2350666e497b7..519d7e6e7dd8b 100644 --- a/substrate/runtime/executive/src/lib.rs +++ b/substrate/runtime/executive/src/lib.rs @@ -124,8 +124,6 @@ impl< /// Apply outside of the block execution function. /// This doesn't attempt to validate anything regarding the block. pub fn apply_extrinsic(uxt: Block::Extrinsic) { - >::put(>::get() + 1u32); - // Verify the signature is good. let xt = match uxt.check() { Ok(xt) => xt, @@ -146,6 +144,8 @@ impl< // decode parameters and dispatch xt.apply(); + + >::put(>::get() + 1u32); } fn final_checks(header: &System::Header) { diff --git a/substrate/runtime/timestamp/src/lib.rs b/substrate/runtime/timestamp/src/lib.rs index 300d2c22eb248..5f67345c4b05d 100644 --- a/substrate/runtime/timestamp/src/lib.rs +++ b/substrate/runtime/timestamp/src/lib.rs @@ -64,9 +64,9 @@ impl Module { fn set(aux: &T::PublicAux, now: T::Value) { assert!(aux.is_empty()); assert!(!::DidUpdate::exists(), "Timestamp must be updated only once in the block"); + assert!(>::extrinsic_index() == 0, "Timestamp must be first extrinsic in the block"); ::Now::put(now); ::DidUpdate::put(true); - assert!(>::extrinsic_index() == 0, "Timestamp must be first extrinsic in the block"); } }