From 5a8146d64e1c72f87effb71c44d02f6964a8fefc Mon Sep 17 00:00:00 2001 From: Robert Habermeier Date: Fri, 22 Nov 2019 13:18:31 +0100 Subject: [PATCH] Updating GRANDPA to v0.10.0 (#4174) * update finality-grandpa to v0.10.0 rev * add concluded implementation to environment * also update state when concluding * switch to the cargo version * address comment grumbles --- substrate/Cargo.lock | 7 +-- substrate/client/finality-grandpa/Cargo.toml | 4 +- .../finality-grandpa/src/environment.rs | 57 ++++++++++++++++++- substrate/client/finality-grandpa/src/lib.rs | 3 +- 4 files changed, 63 insertions(+), 8 deletions(-) diff --git a/substrate/Cargo.lock b/substrate/Cargo.lock index 0e622563f7..1c2a9608f2 100644 --- a/substrate/Cargo.lock +++ b/substrate/Cargo.lock @@ -1132,11 +1132,10 @@ dependencies = [ [[package]] name = "finality-grandpa" -version = "0.9.1" +version = "0.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "futures 0.1.29 (registry+https://github.com/rust-lang/crates.io-index)", - "hashbrown 0.6.3 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)", "num-traits 0.2.9 (registry+https://github.com/rust-lang/crates.io-index)", "parity-scale-codec 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)", @@ -5809,7 +5808,7 @@ name = "substrate-finality-grandpa" version = "2.0.0" dependencies = [ "env_logger 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)", - "finality-grandpa 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)", + "finality-grandpa 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)", "fork-tree 2.0.0", "futures 0.1.29 (registry+https://github.com/rust-lang/crates.io-index)", "futures 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", @@ -7796,7 +7795,7 @@ dependencies = [ "checksum fallible-iterator 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "4443176a9f2c162692bd3d352d745ef9413eec5782a80d8fd6f8a1ac692a07f7" "checksum fdlimit 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "b1ee15a7050e5580b3712877157068ea713b245b080ff302ae2ca973cfcd9baa" "checksum file-per-thread-logger 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "8505b75b31ef7285168dd237c4a7db3c1f3e0927e7d314e670bc98e854272fe9" -"checksum finality-grandpa 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)" = "34754852da8d86bc509715292c73140a5b678656d0b16132acd6737bdb5fd5f8" +"checksum finality-grandpa 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b36ece7dc398ce17438d815f3202d2cdba8fd930452a68b616965662742b7e10" "checksum fixed-hash 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "72fe7539e2c5692c6989f2f9c0457e42f1e5768f96b85c87d273574670ae459f" "checksum fixedbitset 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)" = "86d4de0081402f5e88cdac65c8dcdcc73118c1a7a465e2a05f0da05843a8ea33" "checksum flate2 1.0.13 (registry+https://github.com/rust-lang/crates.io-index)" = "6bd6d6f4752952feb71363cffc9ebac9411b75b87c6ab6058c40c8900cf43c0f" diff --git a/substrate/client/finality-grandpa/Cargo.toml b/substrate/client/finality-grandpa/Cargo.toml index 85c49cf8ce..efeac40974 100644 --- a/substrate/client/finality-grandpa/Cargo.toml +++ b/substrate/client/finality-grandpa/Cargo.toml @@ -27,10 +27,10 @@ inherents = { package = "substrate-inherents", path = "../../primitives/inherent network = { package = "substrate-network", path = "../network" } sp-finality-tracker = { path = "../../primitives/finality-tracker" } fg_primitives = { package = "substrate-finality-grandpa-primitives", path = "../../primitives/finality-grandpa" } -grandpa = { package = "finality-grandpa", version = "0.9.0", features = ["derive-codec"] } +grandpa = { package = "finality-grandpa", version = "0.10.0", features = ["derive-codec"] } [dev-dependencies] -grandpa = { package = "finality-grandpa", version = "0.9.0", features = ["derive-codec", "test-helpers"] } +grandpa = { package = "finality-grandpa", version = "0.10.0", features = ["derive-codec", "test-helpers"] } network = { package = "substrate-network", path = "../network", features = ["test-helpers"] } keyring = { package = "substrate-keyring", path = "../../primitives/keyring" } test-client = { package = "substrate-test-runtime-client", path = "../../test/utils/runtime/client"} diff --git a/substrate/client/finality-grandpa/src/environment.rs b/substrate/client/finality-grandpa/src/environment.rs index b0eaf298b5..214e8bc798 100644 --- a/substrate/client/finality-grandpa/src/environment.rs +++ b/substrate/client/finality-grandpa/src/environment.rs @@ -798,7 +798,7 @@ where let mut completed_rounds = completed_rounds.clone(); // TODO: Future integration will store the prevote and precommit index. See #2611. - let votes = historical_votes.seen().clone(); + let votes = historical_votes.seen().to_vec(); completed_rounds.push(CompletedRound { number: round, @@ -825,6 +825,61 @@ where Ok(()) } + fn concluded( + &self, + round: RoundNumber, + state: RoundState>, + _base: (Block::Hash, NumberFor), + historical_votes: &HistoricalVotes, + ) -> Result<(), Self::Error> { + debug!( + target: "afg", "Voter {} concluded round {} in set {}. Estimate = {:?}, Finalized in round = {:?}", + self.config.name(), + round, + self.set_id, + state.estimate.as_ref().map(|e| e.1), + state.finalized.as_ref().map(|e| e.1), + ); + + self.update_voter_set_state(|voter_set_state| { + // NOTE: we don't use `with_current_round` here, because a concluded + // round is completed and cannot be current. + let (completed_rounds, current_rounds) = + if let VoterSetState::Live { completed_rounds, current_rounds } = voter_set_state { + (completed_rounds, current_rounds) + } else { + let msg = "Voter acting while in paused state."; + return Err(Error::Safety(msg.to_string())); + }; + + let mut completed_rounds = completed_rounds.clone(); + + if let Some(already_completed) = completed_rounds.rounds + .iter_mut().find(|r| r.number == round) + { + let n_existing_votes = already_completed.votes.len(); + + // the interface of Environment guarantees that the previous `historical_votes` + // from `completable` is a prefix of what is passed to `concluded`. + already_completed.votes.extend( + historical_votes.seen().iter().skip(n_existing_votes).cloned() + ); + already_completed.state = state; + } + + let set_state = VoterSetState::::Live { + completed_rounds, + current_rounds: current_rounds.clone(), + }; + + crate::aux_schema::write_voter_set_state(&*self.client, &set_state)?; + + Ok(Some(set_state)) + })?; + + Ok(()) + } + fn finalize_block( &self, hash: Block::Hash, diff --git a/substrate/client/finality-grandpa/src/lib.rs b/substrate/client/finality-grandpa/src/lib.rs index 52cd436cf3..8355f7c93e 100644 --- a/substrate/client/finality-grandpa/src/lib.rs +++ b/substrate/client/finality-grandpa/src/lib.rs @@ -746,7 +746,8 @@ where (*self.env.voters).clone(), global_comms, last_completed_round.number, - last_completed_round.state.clone(), + last_completed_round.votes.clone(), + last_completed_round.base.clone(), last_finalized, );