Skip to main content

soil_client/consensus/
select_chain.rs

1// This file is part of Soil.
2
3// Copyright (C) Soil contributors.
4// Copyright (C) Parity Technologies (UK) Ltd.
5// SPDX-License-Identifier: Apache-2.0 OR GPL-3.0-or-later WITH Classpath-exception-2.0
6
7use super::error::Error;
8use subsoil::runtime::traits::{Block as BlockT, NumberFor};
9
10/// The SelectChain trait defines the strategy upon which the head is chosen
11/// if multiple forks are present for an opaque definition of "best" in the
12/// specific chain build.
13///
14/// The Strategy can be customized for the two use cases of authoring new blocks
15/// upon the best chain or which fork to finalize. Unless implemented differently
16/// by default finalization methods fall back to use authoring, so as a minimum
17/// `_authoring`-functions must be implemented.
18///
19/// Any particular user must make explicit, however, whether they intend to finalize
20/// or author through the using the right function call, as these might differ in
21/// some implementations.
22///
23/// Non-deterministically finalizing chains may only use the `_authoring` functions.
24#[async_trait::async_trait]
25pub trait SelectChain<Block: BlockT>: Sync + Send + Clone {
26	/// Get all leaves of the chain, i.e. block hashes that have no children currently.
27	/// Leaves that can never be finalized will not be returned.
28	async fn leaves(&self) -> Result<Vec<<Block as BlockT>::Hash>, Error>;
29
30	/// Among those `leaves` deterministically pick one chain as the generally
31	/// best chain to author new blocks upon and probably (but not necessarily)
32	/// finalize.
33	async fn best_chain(&self) -> Result<<Block as BlockT>::Header, Error>;
34
35	/// Get the best descendent of `base_hash` that we should attempt to
36	/// finalize next, if any. It is valid to return the given `base_hash`
37	/// itself if no better descendent exists.
38	async fn finality_target(
39		&self,
40		base_hash: <Block as BlockT>::Hash,
41		_maybe_max_number: Option<NumberFor<Block>>,
42	) -> Result<<Block as BlockT>::Hash, Error> {
43		Ok(base_hash)
44	}
45}