oasis_core_runtime/storage/mkvs/sync/
host.rs1use std::{any::Any, sync::Arc};
2
3use anyhow::Result;
4
5use crate::{
6 protocol::{Protocol, ProtocolError},
7 storage::mkvs::sync::{
8 GetPrefixesRequest, GetRequest, IterateRequest, ProofResponse, ReadSync,
9 },
10 types::{
11 Body, HostStorageEndpoint, StorageSyncRequest, StorageSyncRequestWithEndpoint,
12 StorageSyncResponse,
13 },
14};
15
16pub struct HostReadSyncer {
18 protocol: Arc<Protocol>,
19 endpoint: HostStorageEndpoint,
20}
21
22impl HostReadSyncer {
23 pub fn new(protocol: Arc<Protocol>, endpoint: HostStorageEndpoint) -> HostReadSyncer {
25 HostReadSyncer { protocol, endpoint }
26 }
27
28 fn call_host_with_proof(&self, request: StorageSyncRequest) -> Result<ProofResponse> {
29 let request = Body::HostStorageSyncRequest(StorageSyncRequestWithEndpoint {
30 endpoint: self.endpoint,
31 request,
32 });
33 match self.protocol.call_host(request) {
34 Ok(Body::HostStorageSyncResponse(StorageSyncResponse::ProofResponse(response))) => {
35 Ok(response)
36 }
37 Ok(_) => Err(ProtocolError::InvalidResponse.into()),
38 Err(error) => Err(error.into()),
39 }
40 }
41}
42
43impl ReadSync for HostReadSyncer {
44 fn as_any(&self) -> &dyn Any {
45 self
46 }
47
48 fn sync_get(&mut self, request: GetRequest) -> Result<ProofResponse> {
49 self.call_host_with_proof(StorageSyncRequest::SyncGet(request))
50 }
51
52 fn sync_get_prefixes(&mut self, request: GetPrefixesRequest) -> Result<ProofResponse> {
53 self.call_host_with_proof(StorageSyncRequest::SyncGetPrefixes(request))
54 }
55
56 fn sync_iterate(&mut self, request: IterateRequest) -> Result<ProofResponse> {
57 self.call_host_with_proof(StorageSyncRequest::SyncIterate(request))
58 }
59}