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
use std::{any::Any, sync::Arc};

use anyhow::Result;

use crate::{
    protocol::{Protocol, ProtocolError},
    storage::mkvs::sync::{
        GetPrefixesRequest, GetRequest, IterateRequest, ProofResponse, ReadSync,
    },
    types::{
        Body, HostStorageEndpoint, StorageSyncRequest, StorageSyncRequestWithEndpoint,
        StorageSyncResponse,
    },
};

/// A proxy read syncer which forwards calls to the runtime host.
pub struct HostReadSyncer {
    protocol: Arc<Protocol>,
    endpoint: HostStorageEndpoint,
}

impl HostReadSyncer {
    /// Construct a new host proxy instance.
    pub fn new(protocol: Arc<Protocol>, endpoint: HostStorageEndpoint) -> HostReadSyncer {
        HostReadSyncer { protocol, endpoint }
    }

    fn call_host_with_proof(&self, request: StorageSyncRequest) -> Result<ProofResponse> {
        let request = Body::HostStorageSyncRequest(StorageSyncRequestWithEndpoint {
            endpoint: self.endpoint,
            request,
        });
        match self.protocol.call_host(request) {
            Ok(Body::HostStorageSyncResponse(StorageSyncResponse::ProofResponse(response))) => {
                Ok(response)
            }
            Ok(_) => Err(ProtocolError::InvalidResponse.into()),
            Err(error) => Err(error.into()),
        }
    }
}

impl ReadSync for HostReadSyncer {
    fn as_any(&self) -> &dyn Any {
        self
    }

    fn sync_get(&mut self, request: GetRequest) -> Result<ProofResponse> {
        self.call_host_with_proof(StorageSyncRequest::SyncGet(request))
    }

    fn sync_get_prefixes(&mut self, request: GetPrefixesRequest) -> Result<ProofResponse> {
        self.call_host_with_proof(StorageSyncRequest::SyncGetPrefixes(request))
    }

    fn sync_iterate(&mut self, request: IterateRequest) -> Result<ProofResponse> {
        self.call_host_with_proof(StorageSyncRequest::SyncIterate(request))
    }
}