sui_rpc_api/service/
committee.rs

1// Copyright (c) Mysten Labs, Inc.
2// SPDX-License-Identifier: Apache-2.0
3
4use crate::Result;
5use crate::RpcService;
6use sui_sdk_types::{EpochId, ValidatorCommittee};
7
8impl RpcService {
9    pub fn get_committee(&self, epoch: Option<EpochId>) -> Result<ValidatorCommittee> {
10        let epoch = if let Some(epoch) = epoch {
11            epoch
12        } else {
13            self.reader.inner().get_latest_checkpoint()?.epoch()
14        };
15
16        let committee = self
17            .reader
18            .get_committee(epoch)
19            .ok_or_else(|| CommitteeNotFoundError::new(epoch))?;
20
21        Ok(committee)
22    }
23}
24
25#[derive(Debug)]
26pub struct CommitteeNotFoundError {
27    epoch: EpochId,
28}
29
30impl CommitteeNotFoundError {
31    pub fn new(epoch: EpochId) -> Self {
32        Self { epoch }
33    }
34}
35
36impl std::fmt::Display for CommitteeNotFoundError {
37    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
38        write!(f, "Committee for epoch {} not found", self.epoch)
39    }
40}
41
42impl std::error::Error for CommitteeNotFoundError {}
43
44impl From<CommitteeNotFoundError> for crate::RpcError {
45    fn from(value: CommitteeNotFoundError) -> Self {
46        Self::new(tonic::Code::NotFound, value.to_string())
47    }
48}