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
use std::convert::{TryFrom, TryInto};

use casper_execution_engine::core::engine_state::era_validators::GetEraValidatorsRequest;
use casper_types::{auction::ValidatorWeights, bytesrepr::ToBytes};

use crate::engine_server::{ipc, mappings::MappingError};

impl TryFrom<ipc::GetEraValidatorsRequest> for GetEraValidatorsRequest {
    type Error = MappingError;

    fn try_from(
        mut pb_get_era_validators_request: ipc::GetEraValidatorsRequest,
    ) -> Result<Self, Self::Error> {
        let pre_state_hash = pb_get_era_validators_request
            .get_parent_state_hash()
            .try_into()
            .map_err(|_| MappingError::InvalidStateHash("parent_state_hash".to_string()))?;

        let protocol_version = pb_get_era_validators_request.take_protocol_version().into();

        Ok(GetEraValidatorsRequest::new(
            pre_state_hash,
            protocol_version,
        ))
    }
}

impl TryFrom<ValidatorWeights> for ipc::GetEraValidatorsResponse_ValidatorWeights {
    type Error = MappingError;

    fn try_from(validator_weights: ValidatorWeights) -> Result<Self, Self::Error> {
        let mut pb_validator_weights = ipc::GetEraValidatorsResponse_ValidatorWeights::new();

        for (public_key, weight) in validator_weights {
            let mut pb_validator_weight =
                ipc::GetEraValidatorsResponse_ValidatorWeights_ValidatorWeight::new();
            pb_validator_weight.set_public_key_bytes(public_key.to_bytes()?);
            pb_validator_weight.set_weight(weight.into());

            pb_validator_weights
                .mut_validator_weights()
                .push(pb_validator_weight);
        }

        Ok(pb_validator_weights)
    }
}