Don't repeatedly lookup keys in babe_epochAuthorship rpc function (#5962)

* babe: don't repeatedly lookup keys in authorship rpc function

Expose a new function `claim_slot_using_keypars` in Babe so that the `babe_epochAuthorship` can
lookup authorship for all slots in the epoch without repeatedly looking up keys in the keystore.

Time to run the `babe_epochAuthorship` RPC call goes from 7s to 25ms on a local dev chain on my
machine.

* babe: pass reference to slice instead of ref to Vec

* babe: fix bunch of clippy warnings
This commit is contained in:
Jon Häggblad
2020-05-11 10:48:34 +02:00
committed by GitHub
parent b21b70f830
commit 66931028f4
4 changed files with 55 additions and 39 deletions
+13 -2
View File
@@ -116,9 +116,20 @@ impl<B, C, SC> BabeApi for BabeRPCHandler<B, C, SC>
let mut claims: HashMap<AuthorityId, EpochAuthorship> = HashMap::new();
let key_pairs = {
let keystore = keystore.read();
epoch.authorities.iter().enumerate()
.flat_map(|(i, a)| {
keystore.key_pair::<sp_consensus_babe::AuthorityPair>(&a.0).ok().map(|kp| (kp, i))
})
.collect::<Vec<_>>()
};
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, &keystore) {
if let Some((claim, key)) =
authorship::claim_slot_using_key_pairs(slot_number, &epoch, &key_pairs)
{
match claim {
PreDigest::Primary { .. } => {
claims.entry(key.public()).or_default().primary.push(slot_number);
@@ -163,7 +174,7 @@ pub enum Error {
impl From<Error> for jsonrpc_core::Error {
fn from(error: Error) -> Self {
jsonrpc_core::Error {
message: format!("{}", error).into(),
message: format!("{}", error),
code: jsonrpc_core::ErrorCode::ServerError(1234),
data: None,
}