about summary refs log tree commit diff
path: root/tvix/store/src/proto/grpc_pathinfoservice_wrapper.rs
blob: c070b883fa1682a5951d3ac126616e794b2aa80f (plain) (blame)
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
use crate::blobservice::BlobService;
use crate::directoryservice::DirectoryService;
use crate::nar::{calculate_size_and_sha256, RenderError};
use crate::pathinfoservice::PathInfoService;
use crate::proto;
use tonic::{async_trait, Request, Response, Result, Status};
use tracing::{instrument, warn};

pub struct GRPCPathInfoServiceWrapper<PS: PathInfoService, DS: DirectoryService> {
    path_info_service: PS,
    blob_service: Box<dyn BlobService>,
    directory_service: DS,
}

impl<PS: PathInfoService, DS: DirectoryService> GRPCPathInfoServiceWrapper<PS, DS> {
    pub fn new(
        path_info_service: PS,
        blob_service: Box<dyn BlobService>,
        directory_service: DS,
    ) -> Self {
        Self {
            path_info_service,
            blob_service,
            directory_service,
        }
    }
}

#[async_trait]
impl<
        PS: PathInfoService + Send + Sync + 'static,
        DS: DirectoryService + Send + Sync + Clone + 'static,
    > proto::path_info_service_server::PathInfoService for GRPCPathInfoServiceWrapper<PS, DS>
{
    #[instrument(skip(self))]
    async fn get(
        &self,
        request: Request<proto::GetPathInfoRequest>,
    ) -> Result<Response<proto::PathInfo>> {
        match request.into_inner().by_what {
            None => Err(Status::unimplemented("by_what needs to be specified")),
            Some(proto::get_path_info_request::ByWhat::ByOutputHash(digest)) => {
                let digest: [u8; 20] = digest
                    .try_into()
                    .map_err(|_e| Status::invalid_argument("invalid digest length"))?;
                match self.path_info_service.get(digest) {
                    Ok(None) => Err(Status::not_found("PathInfo not found")),
                    Ok(Some(path_info)) => Ok(Response::new(path_info)),
                    Err(e) => {
                        warn!("failed to retrieve PathInfo: {}", e);
                        Err(e.into())
                    }
                }
            }
        }
    }

    #[instrument(skip(self))]
    async fn put(&self, request: Request<proto::PathInfo>) -> Result<Response<proto::PathInfo>> {
        let path_info = request.into_inner();

        // Store the PathInfo in the client. Clients MUST validate the data
        // they receive, so we don't validate additionally here.
        match self.path_info_service.put(path_info) {
            Ok(path_info_new) => Ok(Response::new(path_info_new)),
            Err(e) => {
                warn!("failed to insert PathInfo: {}", e);
                Err(e.into())
            }
        }
    }

    #[instrument(skip(self))]
    async fn calculate_nar(
        &self,
        request: Request<proto::Node>,
    ) -> Result<Response<proto::CalculateNarResponse>> {
        match request.into_inner().node {
            None => Err(Status::invalid_argument("no root node sent")),
            Some(root_node) => {
                let (nar_size, nar_sha256) = calculate_size_and_sha256(
                    &root_node,
                    &self.blob_service,
                    self.directory_service.clone(),
                )
                .expect("error during nar calculation"); // TODO: handle error

                Ok(Response::new(proto::CalculateNarResponse {
                    nar_size,
                    nar_sha256: nar_sha256.to_vec(),
                }))
            }
        }
    }
}

impl From<RenderError> for tonic::Status {
    fn from(value: RenderError) -> Self {
        match value {
            RenderError::BlobNotFound(_, _) => Self::not_found(value.to_string()),
            RenderError::DirectoryNotFound(_, _) => Self::not_found(value.to_string()),
            RenderError::NARWriterError(_) => Self::internal(value.to_string()),
            RenderError::StoreError(_) => Self::internal(value.to_string()),
            RenderError::UnexpectedBlobMeta(_, _, _, _) => Self::internal(value.to_string()),
        }
    }
}