Skip to content

Commit

Permalink
Merge pull request #294 from Phala-Network/backport-substrate-14423
Browse files Browse the repository at this point in the history
  • Loading branch information
jasl authored Jul 18, 2023
2 parents 329fd72 + 5056a99 commit 3bd965d
Show file tree
Hide file tree
Showing 22 changed files with 16,296 additions and 2 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ exclude = [
"vendor/ring",
"polkadot/node/service",
"substrate/client/transaction-pool",
"substrate/client/consensus/grandpa",
]

members = [
Expand Down Expand Up @@ -60,3 +61,5 @@ polkadot-service = { path = "polkadot/node/service" }
# Remove after 0.9.44
sc-transaction-pool = { path = "substrate/client/transaction-pool" }
sc-transaction-pool-api = { path = "substrate/client/transaction-pool/api" }
# Remove after 0.9.45
sc-consensus-grandpa = { path = "substrate/client/consensus/grandpa" }
7 changes: 6 additions & 1 deletion polkadot/node/service/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,10 @@ pub use {rococo_runtime, rococo_runtime_constants};
#[cfg(feature = "westend-native")]
pub use {westend_runtime, westend_runtime_constants};

/// The minimum period of blocks on which justifications will be
/// imported and generated.
const GRANDPA_JUSTIFICATION_PERIOD: u32 = 512;

/// Provides the header and block number for a hash.
///
/// Decouples `sc_client_api::Backend` and `sp_blockchain::HeaderBackend`.
Expand Down Expand Up @@ -511,6 +515,7 @@ where

let (grandpa_block_import, grandpa_link) = grandpa::block_import_with_authority_set_hard_forks(
client.clone(),
GRANDPA_JUSTIFICATION_PERIOD,
&(client.clone() as Arc<_>),
select_chain.clone(),
grandpa_hard_forks,
Expand Down Expand Up @@ -1186,7 +1191,7 @@ where
// Grandpa performance can be improved a bit by tuning this parameter, see:
// https://github.com/paritytech/polkadot/issues/5464
gossip_duration: Duration::from_millis(1000),
justification_period: 1,
justification_generation_period: 1,
name: Some(name),
observer_enabled: false,
keystore: keystore_opt,
Expand Down
60 changes: 60 additions & 0 deletions substrate/client/consensus/grandpa/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
[package]
name = "sc-consensus-grandpa"
version = "0.10.0-dev"
authors = ["Parity Technologies <admin@parity.io>"]
edition = "2021"
license = "GPL-3.0-or-later WITH Classpath-exception-2.0"
homepage = "https://substrate.io"
repository = "https://github.com/paritytech/substrate/"
description = "Integration of the GRANDPA finality gadget into substrate."
documentation = "https://docs.rs/sc-consensus-grandpa"
readme = "README.md"

[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]

[dependencies]
ahash = "0.8.2"
array-bytes = "4.1"
async-trait = "0.1.57"
dyn-clone = "1.0"
finality-grandpa = { version = "0.16.2", features = ["derive-codec"] }
futures = "0.3.21"
futures-timer = "3.0.1"
log = "0.4.17"
parity-scale-codec = { version = "3.2.2", features = ["derive"] }
parking_lot = "0.12.1"
rand = "0.8.5"
serde_json = "1.0.85"
thiserror = "1.0"
fork-tree = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.43" }
prometheus-endpoint = { package = "substrate-prometheus-endpoint", git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.43" }
sc-block-builder = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.43" }
sc-chain-spec = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.43" }
sc-client-api = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.43" }
sc-consensus = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.43" }
sc-network = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.43" }
sc-network-gossip = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.43" }
sc-network-common = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.43" }
sc-telemetry = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.43" }
sc-utils = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.43" }
sp-api = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.43" }
sp-application-crypto = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.43" }
sp-arithmetic = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.43" }
sp-blockchain = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.43" }
sp-consensus = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.43" }
sp-core = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.43" }
sp-consensus-grandpa = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.43" }
sp-keystore = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.43" }
sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.43" }

[dev-dependencies]
assert_matches = "1.3.0"
finality-grandpa = { version = "0.16.2", features = ["derive-codec", "test-helpers"] }
serde = "1.0.136"
tokio = "1.22.0"
sc-network = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.43" }
sc-network-test = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.43" }
sp-keyring = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.43" }
sp-tracing = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.43" }
substrate-test-runtime-client = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.43" }
39 changes: 39 additions & 0 deletions substrate/client/consensus/grandpa/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
Integration of the GRANDPA finality gadget into substrate.

This crate is unstable and the API and usage may change.

This crate provides a long-running future that produces finality notifications.

# Usage

First, create a block-import wrapper with the `block_import` function. The
GRANDPA worker needs to be linked together with this block import object, so
a `LinkHalf` is returned as well. All blocks imported (from network or
consensus or otherwise) must pass through this wrapper, otherwise consensus
is likely to break in unexpected ways.

Next, use the `LinkHalf` and a local configuration to `run_grandpa_voter`.
This requires a `Network` implementation. The returned future should be
driven to completion and will finalize blocks in the background.

# Changing authority sets

The rough idea behind changing authority sets in GRANDPA is that at some point,
we obtain agreement for some maximum block height that the current set can
finalize, and once a block with that height is finalized the next set will
pick up finalization from there.

Technically speaking, this would be implemented as a voting rule which says,
"if there is a signal for a change in N blocks in block B, only vote on
chains with length NUM(B) + N if they contain B". This conditional-inclusion
logic is complex to compute because it requires looking arbitrarily far
back in the chain.

Instead, we keep track of a list of all signals we've seen so far (across
all forks), sorted ascending by the block number they would be applied at.
We never vote on chains with number higher than the earliest handoff block
number (this is num(signal) + N). When finalizing a block, we either apply
or prune any signaled changes based on whether the signaling block is
included in the newly-finalized chain.

License: GPL-3.0-or-later WITH Classpath-exception-2.0
Loading

0 comments on commit 3bd965d

Please sign in to comment.