Support Tracking Forks in Substrate Pallet (#409)

* Support multiple "best headers" from different forks

* Update the name of a test

* Add note about multiple scheduled changes

* Disallow multiple scheduled authority set changes

* Return multiple best headers from Runtime APIs

* Remove invalid test write-up

* Add some sketch-ups of test scenarios

* Clean up test scenarios

* Add module for testing forks

* Write headers to storage

* Add way to check expected outcome for header imports

* Add support for importing finality proofs

* Support importing headers which schedule changes

* Write out test scenario using new framework

* Map authority set changes across forks

Gets all the tests in the `forks` module passing

* Remove basic tests

These were used when working on the initial test helper

* Prevent multiple pending set changes on the same fork

* Remove old test which allowed imports past unfinalized header

* Ignore failing test (for now)

* Rewrite `if` comparison using `match` and `cmp`

Fixes Clippy warning: `comparison_chain`

* Add helper for writing test headers with default characteristics

* Fix test that checked authority set updates

* Make note about importing headers on different unfinalized fork

* Perform some cleanup on the fork module

* Fix Clippy complaints

* Provide list of unfinalized headers to Runtime APIs

* Add proofs to expect() calls

* Make tests the focus of the forks module

* Allow specific errors to be checked in fork tests

* Remove unused method

* Replace unreachable() statement with expect()

* Rename storage `unfinalized_headers() `to make its purpose more clear

* Update Runtime API name in relayer to match pallet

* Commit `unfinalized_headers` changes I forgot to add

* Rename ChainTipHeight to BestHeight

* Make schedule_next_set_change require a mutable reference

* Remove check for key when enacting authority set

We only expect to take the happy-path in the pallet anyways, so this check
to save ourselves the time spent decoding the entry isn't really used.

* Clear justification set when writing headers to storage

* Clarify why we only allow one set change per fork

* Change best_headers() to return HeaderIDs

Prevents us from returning full headers (which are more expensive to
get from storage) since we only care about header IDs (number, hash)
anyways.

* Fix Clippy complaint

* Make note about equivocations

* Use HeaderIds when returning incomplete headers

This change stops returning full headers which are more expensive
to get from storage than header Ids (number, hash) are. Clients likely
don't need the full header anyways which is why this change is fine.

* Introduce HeaderId type to reduce type complexity

* Add signal hash to storage during genesis config

* Return error instead of expect()-ing

* Fix Clippy lint about `ok_or` fn call

* Rename `forks` module to indicate that it's for testing

* Use `const` for `expect()` proofs

* Remove check that key exists before `kill`-ing value

Co-authored-by: Svyatoslav Nikolsky <svyatonik@gmail.com>
This commit is contained in:
Hernando Castano
2020-10-27 15:31:10 -04:00
committed by Bastian Köcher
parent 74249a0896
commit a6048bca59
11 changed files with 855 additions and 246 deletions
@@ -90,10 +90,15 @@ where
let data = Bytes(Vec::new());
let encoded_response = self.client.state_call(call, data, None).await?;
let decoded_response: (P::Number, P::Hash) =
let decoded_response: Vec<(P::Number, P::Hash)> =
Decode::decode(&mut &encoded_response.0[..]).map_err(SubstrateError::ResponseParseFailed)?;
let best_header_id = HeaderId(decoded_response.0, decoded_response.1);
let best_header = decoded_response.last().ok_or_else(|| {
SubstrateError::ResponseParseFailed(
"Parsed an empty list of headers, we should always have at least one.".into(),
)
})?;
let best_header_id = HeaderId(best_header.0, best_header.1);
Ok(best_header_id)
}
@@ -24,7 +24,7 @@ use crate::{
use async_trait::async_trait;
use bp_millau::{
BEST_MILLAU_BLOCK_METHOD, FINALIZED_MILLAU_BLOCK_METHOD, INCOMPLETE_MILLAU_HEADERS_METHOD,
BEST_MILLAU_BLOCKS_METHOD, FINALIZED_MILLAU_BLOCK_METHOD, INCOMPLETE_MILLAU_HEADERS_METHOD,
IS_KNOWN_MILLAU_BLOCK_METHOD,
};
use codec::Encode;
@@ -65,7 +65,7 @@ impl HeadersSyncPipeline for MillauHeadersToRialto {
#[async_trait]
impl SubstrateHeadersSyncPipeline for MillauHeadersToRialto {
const BEST_BLOCK_METHOD: &'static str = BEST_MILLAU_BLOCK_METHOD;
const BEST_BLOCK_METHOD: &'static str = BEST_MILLAU_BLOCKS_METHOD;
const FINALIZED_BLOCK_METHOD: &'static str = FINALIZED_MILLAU_BLOCK_METHOD;
const IS_KNOWN_BLOCK_METHOD: &'static str = IS_KNOWN_MILLAU_BLOCK_METHOD;
const INCOMPLETE_HEADERS_METHOD: &'static str = INCOMPLETE_MILLAU_HEADERS_METHOD;