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

Commit

Permalink
grandpa: return error when endpoint not ready
Browse files Browse the repository at this point in the history
  • Loading branch information
octol committed Apr 15, 2020
1 parent b84bd85 commit 46f8b4a
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 10 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions client/finality-grandpa/rpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ futures = "0.3.1"
serde = { version = "1.0.105", features = ["derive"] }
serde_json = "1.0.50"
log = "0.4.8"
derive_more = "0.99.2"

[dev-dependencies]
substrate-test-runtime-client = { version = "2.0.0-dev", path = "../../../test-utils/runtime/client" }
39 changes: 29 additions & 10 deletions client/finality-grandpa/rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,30 @@ use finality_grandpa::BlockNumberOps;
use sc_finality_grandpa::{voter, AuthorityId, SharedAuthoritySet, SharedVoterState};

use futures::{FutureExt, TryFutureExt};
use jsonrpc_core::Error;
use jsonrpc_derive::rpc;
use log::warn;
use serde::{Deserialize, Serialize};
use std::{collections::HashSet, fmt::Debug};

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

#[derive(derive_more::Display, derive_more::From)]
pub enum Error {
#[display(fmt = "GRANDPA RPC endpoint not ready")]
EndpointNotReady,
}

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

#[rpc]
pub trait GrandpaApi {
Expand Down Expand Up @@ -116,14 +133,16 @@ impl ReportedRoundStates {
pub fn from<Hash, Block>(
voter_state: &SharedVoterState<AuthorityId>,
authority_set: &SharedAuthoritySet<Hash, Block>,
) -> Self
) -> Result<Self, Error>
where
Hash: Debug + Clone + Eq + Send + Sync + 'static,
Block: BlockNumberOps + Send + Sync + 'static,
{
let voter_state = voter_state.read().as_ref().map(|vs| vs.voter_state());
// WIP: handle unwrap of lazily instantiated VoterState
let voter_state = voter_state.unwrap();
let voter_state = voter_state
.read()
.as_ref()
.map(|vs| vs.voter_state())
.ok_or(Error::EndpointNotReady)?;

let current_voters: HashSet<AuthorityId> = authority_set
.current_authorities()
Expand All @@ -147,11 +166,11 @@ impl ReportedRoundStates {
.map(|(round, round_state)| RoundState::from(*round, round_state, &current_voters))
.collect();

Self {
Ok(Self {
set_id,
best,
background,
}
})
}
}

Expand All @@ -163,8 +182,8 @@ where
fn round_state(&self) -> FutureResult<ReportedRoundStates> {
let round_states =
ReportedRoundStates::from(&self.shared_voter_state, &self.shared_authority_set);
let future = async move { Ok(round_states) }.boxed();
Box::new(future.compat())
let future = async move { round_states }.boxed();
Box::new(future.map_err(jsonrpc_core::Error::from).compat())
}
}

Expand Down

0 comments on commit 46f8b4a

Please sign in to comment.