More testnet fixes (#145)

* initialise dummy block before API calls

* test for random-seed

* revert polkadot-runtime changes
This commit is contained in:
Robert Habermeier
2018-05-08 14:55:21 +02:00
committed by Gav Wood
parent ff93bc2a79
commit 39d691821d
3 changed files with 25 additions and 13 deletions
+25 -13
View File
@@ -153,17 +153,30 @@ impl CheckedBlockId for CheckedId {
} }
} }
// set up the necessary scaffolding to execute the runtime. // set up the necessary scaffolding to execute a set of calls to the runtime.
// this creates a new block on top of the given ID and initialises it.
macro_rules! with_runtime { macro_rules! with_runtime {
($client: ident, $at: expr, $exec: expr) => {{ ($client: ident, $at: expr, $exec: expr) => {{
$client.state_at($at.block_id()).map_err(Error::from).and_then(|state| { let parent = $at.block_id();
let header = Header {
parent_hash: $client.block_hash_from_id(parent)?.ok_or(ErrorKind::UnknownBlock(*parent))?,
number: $client.block_number_from_id(parent)?.ok_or(ErrorKind::UnknownBlock(*parent))? + 1,
state_root: Default::default(),
extrinsics_root: Default::default(),
digest: Default::default(),
};
$client.state_at(parent).map_err(Error::from).and_then(|state| {
let mut changes = Default::default(); let mut changes = Default::default();
let mut ext = state_machine::Ext { let mut ext = state_machine::Ext {
overlay: &mut changes, overlay: &mut changes,
backend: &state, backend: &state,
}; };
::substrate_executor::with_native_environment(&mut ext, $exec).map_err(Into::into) ::substrate_executor::with_native_environment(&mut ext, || {
::runtime::Executive::initialise_block(&header);
($exec)()
}).map_err(Into::into)
}) })
}} }}
} }
@@ -196,16 +209,7 @@ impl<B: Backend> PolkadotApi for Client<B, NativeExecutor<LocalDispatch>>
} }
fn duty_roster(&self, at: &CheckedId) -> Result<DutyRoster> { fn duty_roster(&self, at: &CheckedId) -> Result<DutyRoster> {
// duty roster can only be queried at the start of a block, with_runtime!(self, at, ::runtime::Parachains::calculate_duty_roster)
// so we create a dummy.
let id = at.block_id();
let parent_hash = self.block_hash_from_id(id)?.ok_or(ErrorKind::UnknownBlock(*id))?;
let number = self.block_number_from_id(id)?.ok_or(ErrorKind::UnknownBlock(*id))? + 1;
with_runtime!(self, at, || {
::runtime::System::initialise(&number, &parent_hash, &Default::default());
::runtime::Parachains::calculate_duty_roster()
})
} }
fn timestamp(&self, at: &CheckedId) -> Result<Timestamp> { fn timestamp(&self, at: &CheckedId) -> Result<Timestamp> {
@@ -422,4 +426,12 @@ mod tests {
fn fails_to_check_id_for_unknown_block() { fn fails_to_check_id_for_unknown_block() {
assert!(client().check_id(BlockId::Number(100)).is_err()); assert!(client().check_id(BlockId::Number(100)).is_err());
} }
#[test]
fn gets_random_seed_with_genesis() {
let client = client();
let id = client.check_id(BlockId::Number(0)).unwrap();
assert!(client.random_seed(&id).is_ok());
}
} }