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

Commit 33e9416

Browse files
authored
Revert "Revert "grandpa: Use storage proofs for Grandpa authorities (#3734)" (#3983)"
This reverts commit 7dd7067.
1 parent 7dd7067 commit 33e9416

File tree

18 files changed

+263
-195
lines changed

18 files changed

+263
-195
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,7 +5,6 @@ authors = ["Parity Technologies <admin@parity.io>"]
55
edition = "2018"
66

77
[dependencies]
8-
client = { package = "substrate-client", path = "../../client", default-features = false }
98
app-crypto = { package = "substrate-application-crypto", path = "../../application-crypto", default-features = false }
109
codec = { package = "parity-scale-codec", version = "1.0.0", default-features = false, features = ["derive"] }
1110
sr-primitives = { path = "../../sr-primitives", default-features = false }
@@ -15,7 +14,6 @@ serde = { version = "1.0.101", optional = true, features = ["derive"] }
1514
[features]
1615
default = ["std"]
1716
std = [
18-
"client/std",
1917
"codec/std",
2018
"sr-primitives/std",
2119
"rstd/std",

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

+56-22
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, Codec};
26+
use codec::{Encode, Decode, Input, Codec};
2727
use sr_primitives::{ConsensusEngineId, RuntimeDebug};
28-
use client::decl_runtime_apis;
28+
use rstd::borrow::Cow;
2929
use rstd::vec::Vec;
3030

3131
mod app {
@@ -46,6 +46,10 @@ 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+
4953
/// The weight of an authority.
5054
pub type AuthorityWeight = u64;
5155

@@ -58,12 +62,15 @@ pub type SetId = u64;
5862
/// The round indicator.
5963
pub type RoundNumber = u64;
6064

65+
/// A list of Grandpa authorities with associated weights.
66+
pub type AuthorityList = Vec<(AuthorityId, AuthorityWeight)>;
67+
6168
/// A scheduled change of authority set.
6269
#[cfg_attr(feature = "std", derive(Serialize))]
6370
#[derive(Clone, Eq, PartialEq, Encode, Decode, RuntimeDebug)]
6471
pub struct ScheduledChange<N> {
6572
/// The new authorities after the change, along with their respective weights.
66-
pub next_authorities: Vec<(AuthorityId, AuthorityWeight)>,
73+
pub next_authorities: AuthorityList,
6774
/// The number of blocks to delay.
6875
pub delay: N,
6976
}
@@ -154,24 +161,51 @@ pub const PENDING_CHANGE_CALL: &str = "grandpa_pending_change";
154161
/// WASM function call to get current GRANDPA authorities.
155162
pub const AUTHORITIES_CALL: &str = "grandpa_authorities";
156163

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)>;
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())
176210
}
177211
}

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;
25+
use fg_primitives::{AuthorityId, AuthorityList};
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: Vec<(AuthorityId, u64)>,
89+
pub(crate) current_authorities: AuthorityList,
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: Vec<(AuthorityId, u64)>) -> Self {
106+
pub(crate) fn genesis(initial: AuthorityList) -> 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: Vec<(AuthorityId, u64)>,
393+
pub(crate) next_authorities: AuthorityList,
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

+5-4
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::{AuthorityId, AuthorityWeight, SetId, RoundNumber};
28+
use fg_primitives::{AuthorityList, 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: Vec<(AuthorityId, AuthorityWeight)>,
58+
next_authorities: AuthorityList,
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: Vec<(AuthorityId, AuthorityWeight)>,
66+
current_authorities: AuthorityList,
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<Vec<(AuthorityId, AuthorityWeight)>>,
269+
G: FnOnce() -> ClientResult<AuthorityList>,
270270
{
271271
let version: Option<u32> = load_decode(backend, VERSION_KEY)?;
272272
let consensus_changes = load_decode(backend, CONSENSUS_CHANGES_KEY)?
@@ -426,6 +426,7 @@ 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;
429430
use primitives::H256;
430431
use test_client;
431432
use super::*;

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

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

3030
use crate::environment::SharedVoterSetState;
31+
use fg_primitives::AuthorityList;
3132
use super::gossip::{self, GossipValidator};
3233
use super::{AuthorityId, VoterSet, Round, SetId};
3334

@@ -200,7 +201,7 @@ fn make_test_network() -> (
200201
)
201202
}
202203

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

0 commit comments

Comments
 (0)