oasis_core_runtime/storage/mkvs/sync/
mod.rs

1//! The read-only tree sync interface.
2mod errors;
3mod host;
4mod merge;
5mod noop;
6mod proof;
7mod stats;
8
9pub use errors::SyncerError;
10pub use host::HostReadSyncer;
11pub use merge::merge_verified_subtree;
12pub use noop::NoopReadSyncer;
13pub use proof::{Proof, ProofBuilder, ProofVerifier, RawProofEntry};
14pub use stats::StatsCollector;
15
16use std::any::Any;
17
18use anyhow::Result;
19
20use crate::{
21    common::crypto::hash::Hash,
22    storage::mkvs::{tree::Root, Prefix},
23};
24
25/// Identifies a specific tree and a position within that tree.
26#[derive(Clone, Debug, Default, PartialEq, Eq, cbor::Encode, cbor::Decode)]
27pub struct TreeID {
28    /// The Merkle tree root.
29    pub root: Root,
30    /// The caller's position in the tree structure to allow
31    /// returning partial proofs if possible.
32    pub position: Hash,
33}
34
35/// Request for the SyncGet operation.
36#[derive(Clone, Debug, Default, cbor::Encode, cbor::Decode)]
37pub struct GetRequest {
38    pub tree: TreeID,
39    pub key: Vec<u8>,
40    #[cbor(optional)]
41    pub include_siblings: bool,
42}
43
44/// Request for the SyncGetPrefixes operation.
45#[derive(Clone, Debug, Default, cbor::Encode, cbor::Decode)]
46pub struct GetPrefixesRequest {
47    pub tree: TreeID,
48    pub prefixes: Vec<Prefix>,
49    pub limit: u16,
50}
51
52/// Request for the SyncIterate operation.
53#[derive(Clone, Debug, Default, cbor::Encode, cbor::Decode)]
54pub struct IterateRequest {
55    pub tree: TreeID,
56    pub key: Vec<u8>,
57    pub prefetch: u16,
58}
59
60/// Response for requests that produce proofs.
61#[derive(Clone, Debug, Default, cbor::Encode, cbor::Decode)]
62pub struct ProofResponse {
63    pub proof: Proof,
64}
65
66/// ReadSync is the interface for synchronizing the in-memory cache
67/// with another (potentially untrusted) MKVS.
68pub trait ReadSync {
69    /// Return `self` as an `Any` object, useful for downcasting.
70    fn as_any(&self) -> &dyn Any;
71
72    /// Fetch a single key and returns the corresponding proof.
73    fn sync_get(&mut self, request: GetRequest) -> Result<ProofResponse>;
74
75    /// Fetch all keys under the given prefixes and returns the corresponding proofs.
76    fn sync_get_prefixes(&mut self, request: GetPrefixesRequest) -> Result<ProofResponse>;
77
78    /// Seek to a given key and then fetch the specified number of following items
79    /// based on key iteration order.
80    fn sync_iterate(&mut self, request: IterateRequest) -> Result<ProofResponse>;
81}
82
83#[cfg(test)]
84mod test;