Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.

Commit 8be126a

Browse files
authored
Revert "grandpa: Use storage proofs for Grandpa authorities (#3734)"
This reverts commit c3b1a98.
1 parent 5b24ad6 commit 8be126a

File tree

18 files changed

+195
-263
lines changed

18 files changed

+195
-263
lines changed

Cargo.lock

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

core/finality-grandpa/primitives/Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ authors = ["Parity Technologies <admin@parity.io>"]
55
edition = "2018"
66

77
[dependencies]
8+
client = { package = "substrate-client", path = "../../client", default-features = false }
89
app-crypto = { package = "substrate-application-crypto", path = "../../application-crypto", default-features = false }
910
codec = { package = "parity-scale-codec", version = "1.0.0", default-features = false, features = ["derive"] }
1011
sr-primitives = { path = "../../sr-primitives", default-features = false }
@@ -14,6 +15,7 @@ serde = { version = "1.0.101", optional = true, features = ["derive"] }
1415
[features]
1516
default = ["std"]
1617
std = [
18+
"client/std",
1719
"codec/std",
1820
"sr-primitives/std",
1921
"rstd/std",

core/finality-grandpa/primitives/src/lib.rs

+22-56
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ extern crate alloc;
2323

2424
#[cfg(feature = "std")]
2525
use serde::Serialize;
26-
use codec::{Encode, Decode, Input, Codec};
26+
use codec::{Encode, Decode, Codec};
2727
use sr_primitives::{ConsensusEngineId, RuntimeDebug};
28-
use rstd::borrow::Cow;
28+
use client::decl_runtime_apis;
2929
use rstd::vec::Vec;
3030

3131
mod app {
@@ -46,10 +46,6 @@ pub type AuthoritySignature = app::Signature;
4646
/// The `ConsensusEngineId` of GRANDPA.
4747
pub const GRANDPA_ENGINE_ID: ConsensusEngineId = *b"FRNK";
4848

49-
/// The storage key for the current set of weighted Grandpa authorities.
50-
/// The value stored is an encoded VersionedAuthorityList.
51-
pub const GRANDPA_AUTHORITIES_KEY: &'static [u8] = b":grandpa_authorities";
52-
5349
/// The weight of an authority.
5450
pub type AuthorityWeight = u64;
5551

@@ -62,15 +58,12 @@ pub type SetId = u64;
6258
/// The round indicator.
6359
pub type RoundNumber = u64;
6460

65-
/// A list of Grandpa authorities with associated weights.
66-
pub type AuthorityList = Vec<(AuthorityId, AuthorityWeight)>;
67-
6861
/// A scheduled change of authority set.
6962
#[cfg_attr(feature = "std", derive(Serialize))]
7063
#[derive(Clone, Eq, PartialEq, Encode, Decode, RuntimeDebug)]
7164
pub struct ScheduledChange<N> {
7265
/// The new authorities after the change, along with their respective weights.
73-
pub next_authorities: AuthorityList,
66+
pub next_authorities: Vec<(AuthorityId, AuthorityWeight)>,
7467
/// The number of blocks to delay.
7568
pub delay: N,
7669
}
@@ -161,51 +154,24 @@ pub const PENDING_CHANGE_CALL: &str = "grandpa_pending_change";
161154
/// WASM function call to get current GRANDPA authorities.
162155
pub const AUTHORITIES_CALL: &str = "grandpa_authorities";
163156

164-
/// The current version of the stored AuthorityList type. The encoding version MUST be updated any
165-
/// time the AuthorityList type changes.
166-
const AUTHORITIES_VERISON: u8 = 1;
167-
168-
/// An AuthorityList that is encoded with a version specifier. The encoding version is updated any
169-
/// time the AuthorityList type changes. This ensures that encodings of different versions of an
170-
/// AuthorityList are differentiable. Attempting to decode an authority list with an unknown
171-
/// version will fail.
172-
#[derive(Default)]
173-
pub struct VersionedAuthorityList<'a>(Cow<'a, AuthorityList>);
174-
175-
impl<'a> From<AuthorityList> for VersionedAuthorityList<'a> {
176-
fn from(authorities: AuthorityList) -> Self {
177-
VersionedAuthorityList(Cow::Owned(authorities))
178-
}
179-
}
180-
181-
impl<'a> From<&'a AuthorityList> for VersionedAuthorityList<'a> {
182-
fn from(authorities: &'a AuthorityList) -> Self {
183-
VersionedAuthorityList(Cow::Borrowed(authorities))
184-
}
185-
}
186-
187-
impl<'a> Into<AuthorityList> for VersionedAuthorityList<'a> {
188-
fn into(self) -> AuthorityList {
189-
self.0.into_owned()
190-
}
191-
}
192-
193-
impl<'a> Encode for VersionedAuthorityList<'a> {
194-
fn size_hint(&self) -> usize {
195-
(AUTHORITIES_VERISON, self.0.as_ref()).size_hint()
196-
}
197-
198-
fn using_encoded<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
199-
(AUTHORITIES_VERISON, self.0.as_ref()).using_encoded(f)
200-
}
201-
}
202-
203-
impl<'a> Decode for VersionedAuthorityList<'a> {
204-
fn decode<I: Input>(value: &mut I) -> Result<Self, codec::Error> {
205-
let (version, authorities): (u8, AuthorityList) = Decode::decode(value)?;
206-
if version != AUTHORITIES_VERISON {
207-
return Err("unknown Grandpa authorities version".into());
208-
}
209-
Ok(authorities.into())
157+
decl_runtime_apis! {
158+
/// APIs for integrating the GRANDPA finality gadget into runtimes.
159+
/// This should be implemented on the runtime side.
160+
///
161+
/// This is primarily used for negotiating authority-set changes for the
162+
/// gadget. GRANDPA uses a signaling model of changing authority sets:
163+
/// changes should be signaled with a delay of N blocks, and then automatically
164+
/// applied in the runtime after those N blocks have passed.
165+
///
166+
/// The consensus protocol will coordinate the handoff externally.
167+
#[api_version(2)]
168+
pub trait GrandpaApi {
169+
/// Get the current GRANDPA authorities and weights. This should not change except
170+
/// for when changes are scheduled and the corresponding delay has passed.
171+
///
172+
/// When called at block B, it will return the set of authorities that should be
173+
/// used to finalize descendants of this block (B+1, B+2, ...). The block B itself
174+
/// is finalized by the authorities from block B-1.
175+
fn grandpa_authorities() -> Vec<(AuthorityId, AuthorityWeight)>;
210176
}
211177
}

core/finality-grandpa/src/authorities.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use grandpa::voter_set::VoterSet;
2222
use codec::{Encode, Decode};
2323
use log::{debug, info};
2424
use substrate_telemetry::{telemetry, CONSENSUS_INFO};
25-
use fg_primitives::{AuthorityId, AuthorityList};
25+
use fg_primitives::AuthorityId;
2626

2727
use std::cmp::Ord;
2828
use std::fmt::Debug;
@@ -86,7 +86,7 @@ pub(crate) struct Status<H, N> {
8686
/// A set of authorities.
8787
#[derive(Debug, Clone, Encode, Decode, PartialEq)]
8888
pub(crate) struct AuthoritySet<H, N> {
89-
pub(crate) current_authorities: AuthorityList,
89+
pub(crate) current_authorities: Vec<(AuthorityId, u64)>,
9090
pub(crate) set_id: u64,
9191
// Tree of pending standard changes across forks. Standard changes are
9292
// enacted on finality and must be enacted (i.e. finalized) in-order across
@@ -103,7 +103,7 @@ where H: PartialEq,
103103
N: Ord,
104104
{
105105
/// Get a genesis set with given authorities.
106-
pub(crate) fn genesis(initial: AuthorityList) -> Self {
106+
pub(crate) fn genesis(initial: Vec<(AuthorityId, u64)>) -> Self {
107107
AuthoritySet {
108108
current_authorities: initial,
109109
set_id: 0,
@@ -390,7 +390,7 @@ pub(crate) enum DelayKind<N> {
390390
#[derive(Debug, Clone, Encode, PartialEq)]
391391
pub(crate) struct PendingChange<H, N> {
392392
/// The new authorities and weights to apply.
393-
pub(crate) next_authorities: AuthorityList,
393+
pub(crate) next_authorities: Vec<(AuthorityId, u64)>,
394394
/// How deep in the chain the announcing block must be
395395
/// before the change is applied.
396396
pub(crate) delay: N,

core/finality-grandpa/src/aux_schema.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use fork_tree::ForkTree;
2525
use grandpa::round::State as RoundState;
2626
use sr_primitives::traits::{Block as BlockT, NumberFor};
2727
use log::{info, warn};
28-
use fg_primitives::{AuthorityList, SetId, RoundNumber};
28+
use fg_primitives::{AuthorityId, AuthorityWeight, SetId, RoundNumber};
2929

3030
use crate::authorities::{AuthoritySet, SharedAuthoritySet, PendingChange, DelayKind};
3131
use crate::consensus_changes::{SharedConsensusChanges, ConsensusChanges};
@@ -55,15 +55,15 @@ type V0VoterSetState<H, N> = (RoundNumber, RoundState<H, N>);
5555

5656
#[derive(Debug, Clone, Encode, Decode, PartialEq)]
5757
struct V0PendingChange<H, N> {
58-
next_authorities: AuthorityList,
58+
next_authorities: Vec<(AuthorityId, AuthorityWeight)>,
5959
delay: N,
6060
canon_height: N,
6161
canon_hash: H,
6262
}
6363

6464
#[derive(Debug, Clone, Encode, Decode, PartialEq)]
6565
struct V0AuthoritySet<H, N> {
66-
current_authorities: AuthorityList,
66+
current_authorities: Vec<(AuthorityId, AuthorityWeight)>,
6767
set_id: SetId,
6868
pending_changes: Vec<V0PendingChange<H, N>>,
6969
}
@@ -266,7 +266,7 @@ pub(crate) fn load_persistent<Block: BlockT, B, G>(
266266
-> ClientResult<PersistentData<Block>>
267267
where
268268
B: AuxStore,
269-
G: FnOnce() -> ClientResult<AuthorityList>,
269+
G: FnOnce() -> ClientResult<Vec<(AuthorityId, AuthorityWeight)>>,
270270
{
271271
let version: Option<u32> = load_decode(backend, VERSION_KEY)?;
272272
let consensus_changes = load_decode(backend, CONSENSUS_CHANGES_KEY)?
@@ -426,7 +426,6 @@ pub(crate) fn load_authorities<B: AuxStore, H: Decode, N: Decode>(backend: &B)
426426

427427
#[cfg(test)]
428428
mod test {
429-
use fg_primitives::AuthorityId;
430429
use primitives::H256;
431430
use test_client;
432431
use super::*;

core/finality-grandpa/src/communication/tests.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ use codec::Encode;
2828
use sr_primitives::traits::NumberFor;
2929

3030
use crate::environment::SharedVoterSetState;
31-
use fg_primitives::AuthorityList;
3231
use super::gossip::{self, GossipValidator};
3332
use super::{AuthorityId, VoterSet, Round, SetId};
3433

@@ -201,7 +200,7 @@ fn make_test_network() -> (
201200
)
202201
}
203202

204-
fn make_ids(keys: &[Ed25519Keyring]) -> AuthorityList {
203+
fn make_ids(keys: &[Ed25519Keyring]) -> Vec<(AuthorityId, u64)> {
205204
keys.iter()
206205
.map(|key| key.clone().public().into())
207206
.map(|id| (id, 1))

0 commit comments

Comments
 (0)