Skip to content

Commit

Permalink
Migrate finality-grandpa to the 2018 edition (paritytech#1797)
Browse files Browse the repository at this point in the history
  • Loading branch information
stanislav-tkach authored and rphmeier committed Feb 15, 2019
1 parent 1752b28 commit 50bfe37
Show file tree
Hide file tree
Showing 19 changed files with 71 additions and 106 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.

32 changes: 15 additions & 17 deletions core/finality-grandpa/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,31 @@
name = "substrate-finality-grandpa"
version = "0.1.0"
authors = ["Parity Technologies <admin@parity.io>"]
edition = "2018"

[dependencies]
futures = "0.1"
parity-codec = "3.0"
parity-codec-derive = "3.0"
sr-primitives = { path = "../sr-primitives" }
substrate-consensus-common = { path = "../consensus/common" }
substrate-primitives = { path = "../primitives" }
substrate-client = { path = "../client" }
substrate-network = { path = "../network" }
substrate-service = { path = "../service", optional = true }
log = "0.4"
parking_lot = "0.7.1"
tokio = "0.1.7"
substrate-finality-grandpa-primitives = { path = "primitives" }
rand = "0.6"

[dependencies.finality-grandpa]
version = "0.6.0"
features = ["derive-codec"]
parity-codec = "3.0"
parity-codec-derive = "3.0"
runtime_primitives = { package = "sr-primitives", path = "../sr-primitives" }
consensus_common = { package = "substrate-consensus-common", path = "../consensus/common" }
substrate-primitives = { path = "../primitives" }
client = { package = "substrate-client", path = "../client" }
network = { package = "substrate-network", path = "../network" }
service = { package = "substrate-service", path = "../service", optional = true }
fg_primitives = { package = "substrate-finality-grandpa-primitives", path = "primitives" }
grandpa = { package = "finality-grandpa", version = "0.6.0", features = ["derive-codec"] }

[dev-dependencies]
substrate-network = { path = "../network", features = ["test-helpers"] }
substrate-keyring = { path = "../keyring" }
substrate-test-client = { path = "../test-client"}
network = { package = "substrate-network", path = "../network", features = ["test-helpers"] }
keyring = { package = "substrate-keyring", path = "../keyring" }
test_client = { package = "substrate-test-client", path = "../test-client"}
env_logger = "0.6"

[features]
default = ["service-integration"]
service-integration = ["substrate-service"]
service-integration = ["service"]
9 changes: 5 additions & 4 deletions core/finality-grandpa/primitives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,23 @@
name = "substrate-finality-grandpa-primitives"
version = "0.1.0"
authors = ["Parity Technologies <admin@parity.io>"]
edition = "2018"

[dependencies]
substrate-client = { path = "../../client", default-features = false }
client = { package = "substrate-client", path = "../../client", default-features = false }
substrate-primitives = { path = "../../primitives", default-features = false }
parity-codec = { version = "3.0", default-features = false }
parity-codec-derive = { version = "3.0", default-features = false }
sr-primitives = { path = "../../sr-primitives", default-features = false }
sr-std = { path = "../../sr-std", default-features = false }
rstd = { package = "sr-std", path = "../../sr-std", default-features = false }

[features]
default = ["std"]
std = [
"substrate-primitives/std",
"substrate-client/std",
"client/std",
"parity-codec/std",
"parity-codec-derive/std",
"sr-primitives/std",
"sr-std/std",
"rstd/std",
]
14 changes: 2 additions & 12 deletions core/finality-grandpa/primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,10 @@
#[cfg(not(feature = "std"))]
extern crate alloc;

extern crate substrate_primitives;
extern crate sr_primitives;
extern crate parity_codec;

#[macro_use]
extern crate parity_codec_derive;

#[macro_use]
extern crate substrate_client as client;

extern crate sr_std as rstd;

use parity_codec_derive::{Encode, Decode};
use substrate_primitives::Ed25519AuthorityId;
use sr_primitives::traits::{DigestFor, NumberFor};
use client::decl_runtime_apis;
use rstd::vec::Vec;

/// A scheduled change of authority set.
Expand Down
4 changes: 3 additions & 1 deletion core/finality-grandpa/src/authorities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
use parking_lot::RwLock;
use substrate_primitives::Ed25519AuthorityId;
use grandpa::VoterSet;
use parity_codec_derive::{Encode, Decode};
use log::{debug, info};

use std::cmp::Ord;
use std::fmt::Debug;
Expand Down Expand Up @@ -276,7 +278,7 @@ impl<H, N: Add<Output=N> + Clone> PendingChange<H, N> {
mod tests {
use super::*;

fn ignore_existing_changes<A>(_a: &A) -> Result<(), ::Error> {
fn ignore_existing_changes<A>(_a: &A) -> Result<(), crate::Error> {
Ok(())
}

Expand Down
9 changes: 5 additions & 4 deletions core/finality-grandpa/src/communication.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@ use std::sync::Arc;
use grandpa::VoterSet;
use futures::prelude::*;
use futures::sync::mpsc;
use codec::{Encode, Decode};
use log::{debug, trace};
use parity_codec::{Encode, Decode};
use substrate_primitives::{ed25519, Ed25519AuthorityId};
use runtime_primitives::traits::Block as BlockT;
use tokio::timer::Interval;
use {Error, Network, Message, SignedMessage, Commit, CompactCommit};
use crate::{Error, Network, Message, SignedMessage, Commit, CompactCommit};

fn localized_payload<E: Encode>(round: u64, set_id: u64, message: &E) -> Vec<u8> {
(message, round, set_id).encode()
Expand Down Expand Up @@ -245,9 +246,9 @@ pub(crate) fn check_message_sig<Block: BlockT>(
round: u64,
set_id: u64,
) -> Result<(), ()> {
let as_public = ::ed25519::Public::from_raw(id.0);
let as_public = ed25519::Public::from_raw(id.0);
let encoded_raw = localized_payload(round, set_id, message);
if ::ed25519::verify_strong(signature, &encoded_raw, as_public) {
if ed25519::verify_strong(signature, &encoded_raw, as_public) {
Ok(())
} else {
debug!(target: "afg", "Bad signature on message from {:?}", id);
Expand Down
1 change: 1 addition & 0 deletions core/finality-grandpa/src/consensus_changes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.

use std::sync::Arc;
use parity_codec_derive::{Encode, Decode};

/// Consensus-related data changes tracker.
#[derive(Clone, Debug, Encode, Decode)]
Expand Down
15 changes: 8 additions & 7 deletions core/finality-grandpa/src/environment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ use std::fmt;
use std::sync::Arc;
use std::time::{Duration, Instant};

use codec::Encode;
use log::{debug, warn, info};
use parity_codec::Encode;
use futures::prelude::*;
use tokio::timer::Delay;

Expand All @@ -38,10 +39,10 @@ use crate::{
AUTHORITY_SET_KEY, CONSENSUS_CHANGES_KEY, LAST_COMPLETED_KEY,
Commit, Config, Error, Network, Precommit, Prevote, LastCompleted,
};
use authorities::{AuthoritySet, SharedAuthoritySet};
use consensus_changes::SharedConsensusChanges;
use justification::GrandpaJustification;
use until_imported::UntilVoteTargetImported;
use crate::authorities::{AuthoritySet, SharedAuthoritySet};
use crate::consensus_changes::SharedConsensusChanges;
use crate::justification::GrandpaJustification;
use crate::until_imported::UntilVoteTargetImported;

/// The environment we run GRANDPA in.
pub(crate) struct Environment<B, E, Block: BlockT, N: Network<Block>, RA> {
Expand Down Expand Up @@ -246,7 +247,7 @@ impl<B, E, Block: BlockT<Hash=H256>, N, RA> voter::Environment<Block::Hash, Numb
let prevote_timer = Delay::new(now + self.config.gossip_duration * 2);
let precommit_timer = Delay::new(now + self.config.gossip_duration * 4);

let incoming = ::communication::checked_message_stream::<Block, _>(
let incoming = crate::communication::checked_message_stream::<Block, _>(
round,
self.set_id,
self.network.messages_for(round, self.set_id),
Expand All @@ -256,7 +257,7 @@ impl<B, E, Block: BlockT<Hash=H256>, N, RA> voter::Environment<Block::Hash, Numb
let local_key = self.config.local_key.as_ref()
.filter(|pair| self.voters.contains_key(&pair.public().into()));

let (out_rx, outgoing) = ::communication::outgoing_messages::<Block, _>(
let (out_rx, outgoing) = crate::communication::outgoing_messages::<Block, _>(
round,
self.set_id,
local_key.cloned(),
Expand Down
5 changes: 3 additions & 2 deletions core/finality-grandpa/src/finality_proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,16 @@ use client::{
error::{Error as ClientError, ErrorKind as ClientErrorKind, Result as ClientResult},
light::fetcher::RemoteCallRequest,
};
use codec::{Encode, Decode};
use parity_codec::{Encode, Decode};
use parity_codec_derive::{Encode, Decode};
use grandpa::BlockNumberOps;
use runtime_primitives::generic::BlockId;
use runtime_primitives::traits::{
NumberFor, Block as BlockT, Header as HeaderT, One,
};
use substrate_primitives::{Ed25519AuthorityId, H256};

use justification::GrandpaJustification;
use crate::justification::GrandpaJustification;

/// Prepare proof-of-finality for the given block.
///
Expand Down
13 changes: 7 additions & 6 deletions core/finality-grandpa/src/import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@

use std::sync::Arc;

use codec::Encode;
use log::{debug, trace, info};
use parity_codec::Encode;
use futures::sync::mpsc;

use client::{blockchain, CallExecutor, Client};
Expand All @@ -35,10 +36,10 @@ use runtime_primitives::traits::{
use substrate_primitives::{H256, Ed25519AuthorityId, Blake2Hasher};

use crate::{AUTHORITY_SET_KEY, Error};
use authorities::SharedAuthoritySet;
use consensus_changes::SharedConsensusChanges;
use environment::{canonical_at_height, finalize_block, ExitOrError, NewAuthoritySet};
use justification::GrandpaJustification;
use crate::authorities::SharedAuthoritySet;
use crate::consensus_changes::SharedConsensusChanges;
use crate::environment::{canonical_at_height, finalize_block, ExitOrError, NewAuthoritySet};
use crate::justification::GrandpaJustification;

/// A block-import handler for GRANDPA.
///
Expand Down Expand Up @@ -123,7 +124,7 @@ impl<B, E, Block: BlockT<Hash=H256>, RA, PRA> BlockImport<Block>
fn import_block(&self, mut block: ImportBlock<Block>, new_authorities: Option<Vec<Ed25519AuthorityId>>)
-> Result<ImportResult, Self::Error>
{
use authorities::PendingChange;
use crate::authorities::PendingChange;
use client::blockchain::HeaderBackend;

let hash = block.post_header().hash();
Expand Down
5 changes: 3 additions & 2 deletions core/finality-grandpa/src/justification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,16 @@ use client::{CallExecutor, Client};
use client::backend::Backend;
use client::blockchain::HeaderBackend;
use client::error::{Error as ClientError, ErrorKind as ClientErrorKind};
use codec::Decode;
use parity_codec::Decode;
use parity_codec_derive::{Encode, Decode};
use grandpa::VoterSet;
use grandpa::{Error as GrandpaError};
use runtime_primitives::generic::BlockId;
use runtime_primitives::traits::{NumberFor, Block as BlockT, Header as HeaderT};
use substrate_primitives::{H256, Ed25519AuthorityId, Blake2Hasher};

use crate::{Commit, Error};
use communication;
use crate::communication;

/// A GRANDPA justification for block finality, it includes a commit message and
/// an ancestry proof including all headers routing all precommit target blocks
Expand Down
40 changes: 5 additions & 35 deletions core/finality-grandpa/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,45 +52,15 @@
//! any signaled changes based on whether the signaling block is included in the
//! newly-finalized chain.
extern crate finality_grandpa as grandpa;
extern crate futures;
extern crate substrate_client as client;
extern crate sr_primitives as runtime_primitives;
extern crate substrate_consensus_common as consensus_common;
extern crate substrate_network as network;
extern crate substrate_primitives;
extern crate tokio;
extern crate parking_lot;
extern crate parity_codec as codec;
extern crate substrate_finality_grandpa_primitives as fg_primitives;
extern crate rand;

#[macro_use]
extern crate log;

#[cfg(feature="service-integration")]
extern crate substrate_service as service;

#[cfg(test)]
extern crate substrate_keyring as keyring;

#[cfg(test)]
extern crate substrate_test_client as test_client;

#[cfg(test)]
extern crate env_logger;

#[macro_use]
extern crate parity_codec_derive;

use futures::prelude::*;
use log::{debug, info, warn};
use futures::sync::{self, mpsc, oneshot};
use client::{
BlockchainEvents, CallExecutor, Client, backend::Backend,
error::Error as ClientError,
};
use client::blockchain::HeaderBackend;
use codec::{Encode, Decode};
use parity_codec::{Encode, Decode};
use runtime_primitives::traits::{
NumberFor, Block as BlockT, Header as HeaderT, DigestFor, ProvideRuntimeApi, Hash as HashT,
DigestItemFor, DigestItem,
Expand Down Expand Up @@ -407,7 +377,7 @@ pub fn block_import<B, E, Block: BlockT<Hash=H256>, RA, PRA>(

authority_set
}
Some(raw) => ::authorities::AuthoritySet::decode(&mut &raw[..])
Some(raw) => crate::authorities::AuthoritySet::decode(&mut &raw[..])
.ok_or_else(|| ::client::error::ErrorKind::Backend(
format!("GRANDPA authority set kept in invalid format")
))?
Expand Down Expand Up @@ -466,7 +436,7 @@ fn committer_communication<Block: BlockT<Hash=H256>, B, E, N, RA>(
DigestItemFor<Block>: DigestItem<AuthorityId=Ed25519AuthorityId>,
{
// verification stream
let commit_in = ::communication::checked_commit_stream::<Block, _>(
let commit_in = crate::communication::checked_commit_stream::<Block, _>(
set_id,
network.commit_messages(set_id),
voters.clone(),
Expand All @@ -483,7 +453,7 @@ fn committer_communication<Block: BlockT<Hash=H256>, B, E, N, RA>(
.map(|pair| voters.contains_key(&pair.public().into()))
.unwrap_or(false);

let commit_out = ::communication::CommitsOut::<Block, _>::new(
let commit_out = crate::communication::CommitsOut::<Block, _>::new(
network.clone(),
set_id,
is_voter,
Expand Down
6 changes: 3 additions & 3 deletions core/finality-grandpa/src/service_integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
use client;
use service::{FullBackend, FullExecutor, ServiceFactory};

pub type BlockImportForService<F> = ::GrandpaBlockImport<
pub type BlockImportForService<F> = crate::GrandpaBlockImport<
FullBackend<F>,
FullExecutor<F>,
<F as ServiceFactory>::Block,
Expand All @@ -32,9 +32,9 @@ pub type BlockImportForService<F> = ::GrandpaBlockImport<
>,
>;

pub type LinkHalfForService<F> = ::LinkHalf<
pub type LinkHalfForService<F> = crate::LinkHalf<
FullBackend<F>,
FullExecutor<F>,
<F as ServiceFactory>::Block,
<F as ServiceFactory>::RuntimeApi
>;
>;
Loading

0 comments on commit 50bfe37

Please sign in to comment.