Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Next Next commit
babe_epochAuthorship
remove test-helpers from sp-keyring, bump spec_version, impl_version
  • Loading branch information
seunlanlege committed Feb 14, 2020
commit 8bb3ad4986d7756033f5aa59a0779c0d6bcc890d
8 changes: 6 additions & 2 deletions bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
// and set impl_version to 0. If only runtime
// implementation changes and behavior does not, then leave spec_version as
// is and increment impl_version.
spec_version: 219,
impl_version: 0,
spec_version: 217,
impl_version: 3,
apis: RUNTIME_API_VERSIONS,
};

Expand Down Expand Up @@ -737,6 +737,10 @@ impl_runtime_apis! {
secondary_slots: true,
}
}

fn current_epoch_start() -> sp_consensus_babe::SlotNumber {
Babe::current_epoch_start()
}
}

impl sp_authority_discovery::AuthorityDiscoveryApi<Block> for Runtime {
Expand Down
5 changes: 5 additions & 0 deletions client/consensus/babe/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ license = "GPL-3.0"

[dependencies]
codec = { package = "parity-scale-codec", version = "1.0.0", features = ["derive"] }
err-derive = "0.2.2"
sp-consensus-babe = { version = "0.8", path = "../../../primitives/consensus/babe" }
sp-core = { version = "2.0.0", path = "../../../primitives/core" }
sp-application-crypto = { version = "2.0.0", path = "../../../primitives/application-crypto" }
num-bigint = "0.2.3"
num-rational = "0.2.2"
num-traits = "0.2.8"
serde = { version = "1.0.104", features=["derive"] }
sp-version = { version = "2.0.0", path = "../../../primitives/version" }
sp-io = { version = "2.0.0", path = "../../../primitives/io" }
sp-inherents = { version = "2.0.0", path = "../../../primitives/inherents" }
Expand All @@ -33,6 +35,9 @@ sp-runtime = { version = "2.0.0", path = "../../../primitives/runtime" }
fork-tree = { version = "2.0.0", path = "../../../utils/fork-tree" }
futures = "0.3.1"
futures-timer = "3.0.1"
jsonrpc-core = "14.0.3"
jsonrpc-core-client = "14.0.3"
jsonrpc-derive = "14.0.3"
parking_lot = "0.10.0"
log = "0.4.8"
schnorrkel = { version = "0.8.5", features = ["preaudit_deprecated"] }
Expand Down
1 change: 1 addition & 0 deletions client/consensus/babe/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ use sp_api::ApiExt;
mod aux_schema;
mod verification;
mod authorship;
pub mod rpc;
#[cfg(test)]
mod tests;

Expand Down
216 changes: 216 additions & 0 deletions client/consensus/babe/src/rpc.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,216 @@
// Copyright 2020 Parity Technologies (UK) Ltd.
// This file is part of Substrate.

// Substrate is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Substrate is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.

//! rpc api for babe.

use crate::{Epoch, SharedEpochChanges, authorship, Config};
use futures::{
FutureExt as _, TryFutureExt as _,
executor::ThreadPool,
channel::oneshot,
future::ready,
};
use jsonrpc_core::{
Error as RpcError,
futures::future as rpc_future,
};
use jsonrpc_derive::rpc;
use sc_consensus_epochs::{descendent_query, Epoch as EpochT};
use sp_consensus_babe::{
AuthorityId,
BabeApi,
digests::PreDigest,
};
use serde::{Deserialize, Serialize};
use sc_keystore::KeyStorePtr;
use sp_api::{ProvideRuntimeApi, BlockId};
use sp_core::crypto::Pair;
use sp_runtime::traits::{Block as BlockT, Header as _};
use sp_consensus::{SelectChain, Error as ConsensusError};
use sp_blockchain::{HeaderBackend, HeaderMetadata, Error as BlockChainError};
use std::{collections::HashMap, fmt, io, sync::Arc};

type FutureResult<T> = Box<dyn rpc_future::Future<Item = T, Error = RpcError> + Send>;

/// Provides rpc methods for interacting with Babe.
#[rpc]
pub trait BabeRPC {
/// Returns data about which slots (primary or secondary) can be claimed in the current epoch
/// with the keys in the keystore.
#[rpc(name = "babe_epochAuthorship")]
fn epoch_authorship(&self) -> FutureResult<HashMap<AuthorityId, EpochAuthorship>>;
}

/// Implements the BabeRPC trait for interacting with Babe.
///
/// Uses a background thread to calculate epoch_authorship data.
pub struct BabeRPCHandler<B: BlockT, C, SC> {
/// shared refernce to the client.
client: Arc<C>,
/// shared reference to EpochChanges
shared_epoch_changes: SharedEpochChanges<B, Epoch>,
/// shared reference to the Keystore
keystore: KeyStorePtr,
/// config (actually holds the slot duration)
babe_config: Config,
/// threadpool for spawning cpu bound tasks.
threadpool: ThreadPool,
/// select chain
select_chain: Arc<SC>,
}

impl<B: BlockT, C, SC> BabeRPCHandler<B, C, SC> {
/// creates a new instance of the BabeRpc handler.
pub fn new(
client: Arc<C>,
shared_epoch_changes: SharedEpochChanges<B, Epoch>,
keystore: KeyStorePtr,
babe_config: Config,
select_chain: Arc<SC>,
) -> io::Result<Self> {
let threadpool = ThreadPool::builder()
// single thread is fine.
.pool_size(1)
.create()?;

Ok(Self {
client,
shared_epoch_changes,
keystore,
babe_config,
threadpool,
select_chain,
})
}
}

impl<B, C, SC> BabeRPC for BabeRPCHandler<B, C, SC>
where
B: BlockT,
C: ProvideRuntimeApi<B> + HeaderBackend<B> + HeaderMetadata<B, Error=BlockChainError> + 'static,
C::Api: BabeApi<B>,
<C::Api as sp_api::ApiErrorExt>::Error: fmt::Debug,
SC: SelectChain<B> + 'static,
{
fn epoch_authorship(&self) -> FutureResult<HashMap<AuthorityId, EpochAuthorship>> {
let (
babe_config,
keystore,
shared_epoch,
client,
select_chain,
) = (
self.babe_config.clone(),
self.keystore.clone(),
self.shared_epoch_changes.clone(),
self.client.clone(),
self.select_chain.clone(),
);
let (tx, rx) = oneshot::channel();

let future = async move {
let header = select_chain.best_chain().map_err(Error::Consensus)?;
let epoch_start = client.runtime_api()
.current_epoch_start(&BlockId::Hash(header.hash()))
.map_err(|err| {
Error::StringError(format!("{:?}", err))
})?;
let epoch = epoch_data(&shared_epoch, &client, &babe_config, epoch_start, &select_chain)?;
let (epoch_start, epoch_end) = (epoch.start_slot(), epoch.end_slot());

let mut claims: HashMap<AuthorityId, EpochAuthorship> = HashMap::new();

for slot_number in epoch_start..epoch_end {
let epoch = epoch_data(&shared_epoch, &client, &babe_config, slot_number, &select_chain)?;
if let Some((claim, key)) = authorship::claim_slot(slot_number, &epoch, &babe_config, &keystore) {
match claim {
PreDigest::Primary { .. } => {
claims.entry(key.public()).or_default().primary.push(slot_number);
}
PreDigest::Secondary { .. } => {
claims.entry(key.public()).or_default().secondary.push(slot_number);
}
};
}
}

Ok(claims)
}.then(|result| {
let _ = tx.send(result).expect("receiever is never dropped; qed");
ready(())
}).boxed();

self.threadpool.spawn_ok(future);

Box::new(async { rx.await.expect("sender is never dropped; qed") }.boxed().compat())
}
}

/// Holds information about the `slot_number`'s that can be claimed by a given key.
#[derive(Default, Debug, Deserialize, Serialize)]
pub struct EpochAuthorship {
/// the array of primary slots that can be claimed
primary: Vec<u64>,
/// the array of secondary slots that can be claimed
secondary: Vec<u64>,
}

/// Errors encountered by the RPC
#[derive(Debug, err_derive::Error, derive_more::From)]
pub enum Error {
/// Consensus error
#[error(display = "Consensus Error: {}", _0)]
Consensus(ConsensusError),
/// Errors that can be formatted as a String
#[error(display = "{}", _0)]
StringError(String)
}

impl From<Error> for jsonrpc_core::Error {
fn from(error: Error) -> Self {
jsonrpc_core::Error {
message: format!("{}", error).into(),
code: jsonrpc_core::ErrorCode::ServerError(1234),
data: None,
}
}
}

/// fetches the epoch data for a given slot_number.
fn epoch_data<B, C, SC>(
epoch_changes: &SharedEpochChanges<B, Epoch>,
client: &Arc<C>,
babe_config: &Config,
slot_number: u64,
select_chain: &Arc<SC>,
) -> Result<Epoch, Error>
where
B: BlockT,
C: HeaderBackend<B> + HeaderMetadata<B, Error=BlockChainError> + 'static,
SC: SelectChain<B>,
{
let parent = select_chain.best_chain()?;
epoch_changes.lock().epoch_for_child_of(
descendent_query(&**client),
&parent.hash(),
parent.number().clone(),
slot_number,
|slot| babe_config.genesis_epoch(slot),
)
.map_err(|e| Error::Consensus(ConsensusError::ChainLookup(format!("{:?}", e))))?
.map(|e| e.into_inner())
.ok_or(Error::Consensus(ConsensusError::InvalidAuthoritiesSet))
}
37 changes: 37 additions & 0 deletions client/consensus/babe/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ use sp_consensus::{
NoNetwork as DummyOracle, Proposal, RecordProof,
import_queue::{BoxBlockImport, BoxJustificationImport, BoxFinalityProofImport},
};
use sc_keystore::{KeyStorePtr, Store};
use sp_application_crypto::AppPair;
use sc_network_test::*;
use sc_network_test::{Block as TestBlock, PeersClient};
use sc_network::config::{BoxFinalityProofRequestBuilder, ProtocolConfig};
Expand All @@ -36,6 +38,9 @@ use tokio::runtime::current_thread;
use sc_client_api::{BlockchainEvents, backend::TransactionFor};
use log::debug;
use std::{time::Duration, cell::RefCell};
use sp_keyring::Ed25519Keyring;
use rpc::{BabeRPC, BabeRPCHandler};
use jsonrpc_core::IoHandler;

type Item = DigestItem<Hash>;

Expand Down Expand Up @@ -333,6 +338,16 @@ impl TestNetFactory for BabeTestNet {
}
}

/// creates keystore backed by a temp file
fn create_temp_keystore<P: AppPair>(authority: Ed25519Keyring) -> (KeyStorePtr, tempfile::TempDir) {
let keystore_path = tempfile::tempdir().expect("Creates keystore path");
let keystore = Store::open(keystore_path.path(), None).expect("Creates keystore");
keystore.write().insert_ephemeral_from_seed::<P>(&authority.to_seed())
.expect("Creates authority key");

(keystore, keystore_path)
}

#[test]
#[should_panic]
fn rejects_empty_block() {
Expand Down Expand Up @@ -812,3 +827,25 @@ fn verify_slots_are_strictly_increasing() {
&mut block_import,
);
}

#[test]
fn rpc() {
let mut net = BabeTestNet::new(1);

let peer = net.peer(0);
let data = peer.data.as_ref().expect("babe link set up during initialization");

let client = peer.client().as_full().expect("Only full clients are used in tests").clone();
let epoch_changes = data.link.epoch_changes.clone();
let config = Config::get_or_compute(&*client).expect("lol");
let select_chain = peer.select_chain().expect("Full client has select_chain");
let keystore = create_temp_keystore::<AuthorityPair>(Ed25519Keyring::Alice).0;
let handler = BabeRPCHandler::new(client.clone(), epoch_changes, keystore, config, Arc::new(select_chain)).unwrap();
let mut io = IoHandler::new();

io.extend_with(BabeRPC::to_delegate(handler));
let request = r#"{"jsonrpc":"2.0","method":"babe_epochAuthorship","params": [],"id":1}"#;
let response = r#"{"jsonrpc":"2.0","result":{"5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY":{"primary":[0],"secondary":[1,2,4]}},"id":1}"#;

assert_eq!(Some(response.into()), io.handle_request_sync(request));
}
5 changes: 4 additions & 1 deletion client/keystore/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ rand = "0.7.2"
serde_json = "1.0.41"
subtle = "2.1.1"
parking_lot = "0.10.0"
tempfile = "3.1.0"

[dev-dependencies]
tempfile = "3.1.0"

[features]
default = []
3 changes: 0 additions & 3 deletions client/keystore/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,10 @@
#![warn(missing_docs)]

use std::{collections::HashMap, path::PathBuf, fs::{self, File}, io::{self, Write}, sync::Arc};

use sp_core::{
crypto::{KeyTypeId, Pair as PairT, Public, IsWrappedBy, Protected}, traits::BareCryptoStore,
};

use sp_application_crypto::{AppKey, AppPublic, AppPair, ed25519, sr25519};

use parking_lot::RwLock;

/// Keystore pointer
Expand Down
2 changes: 1 addition & 1 deletion frame/babe/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ impl<T: Trait> Module<T> {
// finds the start slot of the current epoch. only guaranteed to
// give correct results after `do_initialize` of the first block
// in the chain (as its result is based off of `GenesisSlot`).
fn current_epoch_start() -> SlotNumber {
pub fn current_epoch_start() -> SlotNumber {
(EpochIndex::get() * T::EpochDuration::get()) + GenesisSlot::get()
}

Expand Down
3 changes: 3 additions & 0 deletions primitives/consensus/babe/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,5 +139,8 @@ sp_api::decl_runtime_apis! {
///
/// Dynamic configuration may be supported in the future.
fn configuration() -> BabeConfiguration;

/// Returns the slot number that started the current epoch.
fn current_epoch_start() -> SlotNumber;
}
}
Loading