cfx_rpc_cfx_types/pos/
committee.rs1use cfx_types::{H256, U64};
2use diem_types::{
3 epoch_state::EpochState,
4 term_state::{NodeList, TermData},
5};
6use serde::Serialize;
7
8#[derive(Debug, Serialize, Default)]
9#[serde(rename_all = "camelCase")]
10pub struct CommitteeState {
11 pub current_committee: RpcCommittee,
12 pub elections: Vec<RpcTermData>,
13}
14
15#[derive(Debug, Serialize, Default)]
16#[serde(rename_all = "camelCase")]
17pub struct RpcCommittee {
18 pub epoch_number: U64,
19 pub quorum_voting_power: U64,
20 pub total_voting_power: U64,
21 pub nodes: Vec<NodeVotingPower>,
22}
23
24impl RpcCommittee {
25 pub fn from_epoch_state(epoch_state: &EpochState) -> RpcCommittee {
26 let mut committee = RpcCommittee::default();
27 committee.epoch_number = U64::from(epoch_state.epoch);
28 committee.quorum_voting_power =
29 U64::from(epoch_state.verifier().quorum_voting_power());
30 committee.total_voting_power =
31 U64::from(epoch_state.verifier().total_voting_power());
32 let validator_info = epoch_state.verifier().address_to_validator_info();
33
34 for (account_address, validator_consensus_info) in validator_info {
35 committee.nodes.push(NodeVotingPower::new(
36 H256::from(account_address.to_u8()),
37 validator_consensus_info.voting_power(),
38 ))
39 }
40 committee
41 }
42}
43
44#[derive(Debug, Serialize, Default)]
45#[serde(rename_all = "camelCase")]
46pub struct RpcTermData {
47 pub start_block_number: U64,
48 pub is_finalized: bool,
49 pub top_electing_nodes: Vec<NodeVotingPower>,
50}
51
52impl From<&TermData> for RpcTermData {
53 fn from(term_data: &TermData) -> Self {
54 let mut value = RpcTermData::default();
55 match term_data.node_list() {
56 NodeList::Electing(electing_heap) => {
57 value.is_finalized = false;
58 value.start_block_number = U64::from(term_data.start_view());
59 for (account, votes) in electing_heap.read_top_electing() {
60 value.top_electing_nodes.push(NodeVotingPower::new(
61 H256::from(account.to_u8()),
62 votes,
63 ))
64 }
65 }
66 NodeList::Elected(elected) => {
67 value.is_finalized = true;
68 value.start_block_number = U64::from(term_data.start_view());
69 for (account, votes) in elected.inner() {
70 value.top_electing_nodes.push(NodeVotingPower::new(
71 H256::from(account.to_u8()),
72 *votes,
73 ))
74 }
75 }
76 }
77 value
78 }
79}
80
81#[derive(Debug, Serialize, Default)]
82#[serde(rename_all = "camelCase")]
83pub struct NodeVotingPower {
84 pub address: H256,
85 pub voting_power: U64,
86}
87
88impl NodeVotingPower {
89 pub fn new(address: H256, voting_power: u64) -> Self {
90 NodeVotingPower {
91 address,
92 voting_power: U64::from(voting_power),
93 }
94 }
95}