sui_rpc_api/service/
committee.rs

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
// Copyright (c) Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

use crate::Result;
use crate::RpcService;
use sui_sdk_types::{EpochId, ValidatorCommittee};

impl RpcService {
    pub fn get_committee(&self, epoch: Option<EpochId>) -> Result<ValidatorCommittee> {
        let epoch = if let Some(epoch) = epoch {
            epoch
        } else {
            self.reader.inner().get_latest_checkpoint()?.epoch()
        };

        let committee = self
            .reader
            .get_committee(epoch)
            .ok_or_else(|| CommitteeNotFoundError::new(epoch))?;

        Ok(committee)
    }
}

#[derive(Debug)]
pub struct CommitteeNotFoundError {
    epoch: EpochId,
}

impl CommitteeNotFoundError {
    pub fn new(epoch: EpochId) -> Self {
        Self { epoch }
    }
}

impl std::fmt::Display for CommitteeNotFoundError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "Committee for epoch {} not found", self.epoch)
    }
}

impl std::error::Error for CommitteeNotFoundError {}

impl From<CommitteeNotFoundError> for crate::RpcError {
    fn from(value: CommitteeNotFoundError) -> Self {
        Self::new(tonic::Code::NotFound, value.to_string())
    }
}