1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#![warn(missing_docs)]
use futures::{FutureExt, TryFutureExt};
use jsonrpc_derive::rpc;
mod error;
mod report;
use report::{ReportAuthoritySet, ReportVoterState, ReportedRoundStates};
pub const NOT_READY_ERROR_CODE: i64 = 1;
type FutureResult<T> =
Box<dyn jsonrpc_core::futures::Future<Item = T, Error = jsonrpc_core::Error> + Send>;
#[rpc]
pub trait GrandpaApi {
#[rpc(name = "grandpa_roundState")]
fn round_state(&self) -> FutureResult<ReportedRoundStates>;
}
pub struct GrandpaRpcHandler<AuthoritySet, VoterState> {
authority_set: AuthoritySet,
voter_state: VoterState,
}
impl<AuthoritySet, VoterState> GrandpaRpcHandler<AuthoritySet, VoterState> {
pub fn new(authority_set: AuthoritySet, voter_state: VoterState) -> Self {
Self {
authority_set,
voter_state,
}
}
}
impl<AuthoritySet, VoterState> GrandpaApi for GrandpaRpcHandler<AuthoritySet, VoterState>
where
VoterState: ReportVoterState + Send + Sync + 'static,
AuthoritySet: ReportAuthoritySet + Send + Sync + 'static,
{
fn round_state(&self) -> FutureResult<ReportedRoundStates> {
let round_states = ReportedRoundStates::from(&self.authority_set, &self.voter_state);
let future = async move { round_states }.boxed();
Box::new(future.map_err(jsonrpc_core::Error::from).compat())
}
}
#[cfg(test)]
mod tests {
use super::*;
use jsonrpc_core::IoHandler;
use sc_finality_grandpa::{report, AuthorityId};
use sp_core::crypto::Public;
use std::{collections::HashSet, convert::TryInto};
struct TestAuthoritySet;
struct TestVoterState;
struct EmptyVoterState;
fn voters() -> HashSet<AuthorityId> {
let voter_id_1 = AuthorityId::from_slice(&[1; 32]);
let voter_id_2 = AuthorityId::from_slice(&[2; 32]);
vec![voter_id_1, voter_id_2].into_iter().collect()
}
impl ReportAuthoritySet for TestAuthoritySet {
fn get(&self) -> (u64, HashSet<AuthorityId>) {
(1, voters())
}
}
impl ReportVoterState for EmptyVoterState {
fn get(&self) -> Option<report::VoterState<AuthorityId>> {
None
}
}
impl ReportVoterState for TestVoterState {
fn get(&self) -> Option<report::VoterState<AuthorityId>> {
let voter_id_1 = AuthorityId::from_slice(&[1; 32]);
let voters_best: HashSet<_> = vec![voter_id_1].into_iter().collect();
let best_round_state = report::RoundState {
total_weight: 100_u64.try_into().unwrap(),
threshold_weight: 67_u64.try_into().unwrap(),
prevote_current_weight: 50.into(),
prevote_ids: voters_best,
precommit_current_weight: 0.into(),
precommit_ids: HashSet::new(),
};
let past_round_state = report::RoundState {
total_weight: 100_u64.try_into().unwrap(),
threshold_weight: 67_u64.try_into().unwrap(),
prevote_current_weight: 100.into(),
prevote_ids: voters(),
precommit_current_weight: 100.into(),
precommit_ids: voters(),
};
let background_rounds = vec![(1, past_round_state)].into_iter().collect();
Some(report::VoterState {
background_rounds,
best_round: (2, best_round_state),
})
}
}
#[test]
fn uninitialized_rpc_handler() {
let handler = GrandpaRpcHandler::new(TestAuthoritySet, EmptyVoterState);
let mut io = IoHandler::new();
io.extend_with(GrandpaApi::to_delegate(handler));
let request = r#"{"jsonrpc":"2.0","method":"grandpa_roundState","params":[],"id":1}"#;
let response = r#"{"jsonrpc":"2.0","error":{"code":1,"message":"GRANDPA RPC endpoint not ready"},"id":1}"#;
assert_eq!(Some(response.into()), io.handle_request_sync(request));
}
#[test]
fn working_rpc_handler() {
let handler = GrandpaRpcHandler::new(TestAuthoritySet, TestVoterState);
let mut io = IoHandler::new();
io.extend_with(GrandpaApi::to_delegate(handler));
let request = r#"{"jsonrpc":"2.0","method":"grandpa_roundState","params":[],"id":1}"#;
let response = "{\"jsonrpc\":\"2.0\",\"result\":{\
\"background\":[{\
\"precommits\":{\"currentWeight\":100,\"missing\":[]},\
\"prevotes\":{\"currentWeight\":100,\"missing\":[]},\
\"round\":1,\"thresholdWeight\":67,\"totalWeight\":100\
}],\
\"best\":{\
\"precommits\":{\"currentWeight\":0,\"missing\":[\"5C62Ck4UrFPiBtoCmeSrgF7x9yv9mn38446dhCpsi2mLHiFT\",\"5C7LYpP2ZH3tpKbvVvwiVe54AapxErdPBbvkYhe6y9ZBkqWt\"]},\
\"prevotes\":{\"currentWeight\":50,\"missing\":[\"5C7LYpP2ZH3tpKbvVvwiVe54AapxErdPBbvkYhe6y9ZBkqWt\"]},\
\"round\":2,\"thresholdWeight\":67,\"totalWeight\":100\
},\
\"setId\":1\
},\"id\":1}";
assert_eq!(io.handle_request_sync(request), Some(response.into()));
}
}