Implement Substrate Pallet Runtime APIs (#389)

* Implement public helpers for querying header info

* Update `best_header` when importing headers

* Add BestHeader to GenesisConfig

* Define extra types for Millau primitives

* Start implementing runtime APIs in Millau runtime

* Add helper for getting headers which require a justification

* Add runtime API for getting headers requiring a justification

* Reword `expect()` proof for valid authority sets

* Fix typo

* Clean up Hasher comment

* Add the Call Dispatch Pallet back to the Millau runtime

* Use types from Rialto in bridge pallet config

* Use the Rialto runtime APIS in the Millau runtime

* Include Millau bridge instance in Rialto runtime

* Add missing doc comment

* Use one storage function for setting and clearing `RequiresJustification`

* Remove TODO comments
This commit is contained in:
Hernando Castano
2020-10-06 15:53:25 -04:00
committed by Bastian Köcher
parent cfe1e43473
commit 86834e2fd6
10 changed files with 239 additions and 32 deletions
+24 -8
View File
@@ -106,13 +106,14 @@ where
/// Will perform some basic checks to make sure that this header doesn't break any assumptions
/// such as being on a different finalized fork.
pub fn import_header(&mut self, header: H) -> Result<(), ImportError> {
let hash = header.hash();
let best_finalized = self.storage.best_finalized_header();
if header.number() <= best_finalized.number() {
return Err(ImportError::OldHeader);
}
if self.storage.header_exists(header.hash()) {
if self.storage.header_exists(hash) {
return Err(ImportError::HeaderAlreadyExists);
}
@@ -176,13 +177,20 @@ where
}
};
let is_finalized = false;
self.storage.write_header(&ImportedHeader {
header,
requires_justification,
is_finalized,
is_finalized: false,
});
if requires_justification {
self.storage.update_unfinalized_header(Some(hash));
}
// Since we're not dealing with forks at the moment we know that
// the header we just got will be the one at the best height
self.storage.update_best_header(hash);
Ok(())
}
@@ -205,8 +213,8 @@ where
let current_authority_set = self.storage.current_authority_set();
let voter_set = VoterSet::new(current_authority_set.authorities).expect(
"This only fails if we have an invalid list of authorities. Since we
got this from storage it should always be valid, otherwise we have a bug.",
"We verified the correctness of the authority list during header import,
before writing them to storage. This must always be valid.",
);
verify_justification::<H>(
(hash, *header.number()),
@@ -255,6 +263,9 @@ where
let _ = self.storage.enact_authority_set().expect(
"Headers must only be marked as `requires_justification` if there's a scheduled change in storage.",
);
// Clear the storage entry since we got a justification
self.storage.update_unfinalized_header(None);
}
for header in finalized_headers.iter_mut() {
@@ -442,9 +453,11 @@ mod tests {
};
assert_ok!(verifier.import_header(header.clone()));
let stored_header = storage.header_by_hash(header.hash());
assert!(stored_header.is_some());
assert_eq!(stored_header.unwrap().is_finalized, false);
let stored_header = storage
.header_by_hash(header.hash())
.expect("Should have been imported successfully");
assert_eq!(stored_header.is_finalized, false);
assert_eq!(stored_header, storage.best_header());
})
}
@@ -648,11 +661,14 @@ mod tests {
};
assert_ok!(verifier.import_header(header.clone()));
assert_eq!(storage.unfinalized_header(), Some(header.hash()));
assert_ok!(verifier.import_finality_proof(header.hash(), justification.into()));
assert_eq!(storage.best_finalized_header().header, header);
// Make sure that we have updated the set now that we've finalized our header
assert_eq!(storage.current_authority_set(), change.authority_set);
assert_eq!(storage.unfinalized_header(), None);
})
}