thruput-operator 0.71.0

Bindings for Thruput Operator1, the read-only status program every daemon can serve
Documentation
//! The generated `Operator1` client and dispatcher, end to end over a loopback link.

use std::sync::Arc;

use thruput_operator::{
    Operator_Operator1, Operator_Operator1_Client,
    Operator_Operator1_Dispatcher, OperatorPosition, OperatorReadiness,
    OperatorStatus,
};
use thruput_rpc_driver::loopback::LoopbackLink;
use thruput_rpc_driver::{
    CallBuf, ProgVers, ProgramError, RpcError, RpcSender, ServiceMap,
};

struct Fixed(OperatorStatus);

#[allow(refining_impl_trait)]
impl Operator_Operator1 for Fixed {
    async fn operator_null(&self) -> Result<(), ProgramError> {
        Ok(())
    }

    async fn operator_status(&self) -> Result<OperatorStatus, ProgramError> {
        Ok(self.0.clone())
    }
}

fn text<T: TryFrom<String>>(s: &str) -> T
where
    T::Error: std::fmt::Debug,
{
    T::try_from(s.to_string()).unwrap()
}

fn status() -> OperatorStatus {
    OperatorStatus {
        role: text("coordinator"),
        process_name: text("coordinator"),
        build_version: text("1.2.3"),
        build_commit: text("abcdef0"),
        protocol_token: text("hogp/0123456789abcde"),
        network_hash: Some([0x41; 32]),
        readiness: OperatorReadiness::OPERATOR_READY,
        readiness_detail: text(""),
        positions: vec![
            OperatorPosition {
                name: text("decided"),
                height: 11,
                block_hash: Some([0x11; 32]),
            },
            OperatorPosition {
                name: text("committed"),
                height: 10,
                block_hash: Some([0x10; 32]),
            },
        ]
        .try_into()
        .unwrap(),
    }
}

fn loopback(
    expected: OperatorStatus,
) -> (LoopbackLink, Arc<dyn RpcSender>, Operator_Operator1_Client) {
    let mut services = ServiceMap::new();
    services.add_service(Box::new(Operator_Operator1_Dispatcher::new(
        Box::new(Fixed(expected)),
    )));
    let link = LoopbackLink::connect(ServiceMap::new(), services);
    let (client, _server) = link.senders();
    let sender: Arc<dyn RpcSender> = Arc::new(client);
    let client = Operator_Operator1_Client::new(sender.clone());
    (link, sender, client)
}

#[tokio::test]
async fn status_round_trips() {
    let want = status();
    let (_link, _sender, client) = loopback(want.clone());
    client.operator_null().await.unwrap();
    let got = client.operator_status().await.unwrap();
    assert_eq!(got, want);
}

#[tokio::test]
async fn unknown_procedure_is_invalid_procedure() {
    let (_link, sender, _client) = loopback(status());
    let err = sender
        .send_call(ProgVers::new(0x2000_0000, 1), 2, CallBuf::new())
        .await
        .unwrap_err();
    assert!(matches!(err, RpcError::InvalidProcedure), "{err:?}");
}