hypercore/replication/
mod.rs

1//! External interface for replication
2pub mod events;
3#[cfg(feature = "shared-core")]
4pub mod shared_core;
5
6#[cfg(feature = "shared-core")]
7pub use shared_core::SharedCore;
8
9use crate::{
10    AppendOutcome, HypercoreError, Info, PartialKeypair, Proof, RequestBlock, RequestSeek,
11    RequestUpgrade,
12};
13
14pub use events::Event;
15
16use async_broadcast::Receiver;
17use std::future::Future;
18
19/// Methods related to just this core's information
20pub trait CoreInfo {
21    /// Get core info (see: [`crate::Hypercore::info`]
22    fn info(&self) -> impl Future<Output = Info> + Send;
23    /// Get the key_pair (see: [`crate::Hypercore::key_pair`]
24    fn key_pair(&self) -> impl Future<Output = PartialKeypair> + Send;
25}
26
27/// Error for ReplicationMethods trait
28#[derive(thiserror::Error, Debug)]
29pub enum ReplicationMethodsError {
30    /// Error from hypercore
31    #[error("Got a hypercore error: [{0}]")]
32    HypercoreError(#[from] HypercoreError),
33    /// Error from CoreMethods
34    #[error("Got a CoreMethods error: [{0}]")]
35    CoreMethodsError(#[from] CoreMethodsError),
36}
37
38/// Methods needed for replication
39pub trait ReplicationMethods: CoreInfo + Send {
40    /// ref Core::verify_and_apply_proof
41    fn verify_and_apply_proof(
42        &self,
43        proof: &Proof,
44    ) -> impl Future<Output = Result<bool, ReplicationMethodsError>> + Send;
45    /// ref Core::missing_nodes
46    fn missing_nodes(
47        &self,
48        index: u64,
49    ) -> impl Future<Output = Result<u64, ReplicationMethodsError>> + Send;
50    /// ref Core::create_proof
51    fn create_proof(
52        &self,
53        block: Option<RequestBlock>,
54        hash: Option<RequestBlock>,
55        seek: Option<RequestSeek>,
56        upgrade: Option<RequestUpgrade>,
57    ) -> impl Future<Output = Result<Option<Proof>, ReplicationMethodsError>> + Send;
58    /// subscribe to core events
59    fn event_subscribe(&self) -> impl Future<Output = Receiver<Event>>;
60}
61
62/// Error for CoreMethods trait
63#[derive(thiserror::Error, Debug)]
64pub enum CoreMethodsError {
65    /// Error from hypercore
66    #[error("Got a hypercore error [{0}]")]
67    HypercoreError(#[from] HypercoreError),
68}
69
70/// Trait for things that consume [`crate::Hypercore`] can instead use this trait
71/// so they can use all Hypercore-like things such as `SharedCore`.
72pub trait CoreMethods: CoreInfo {
73    /// Check if the core has the block at the given index locally
74    fn has(&self, index: u64) -> impl Future<Output = bool> + Send;
75
76    /// get a block
77    fn get(
78        &self,
79        index: u64,
80    ) -> impl Future<Output = Result<Option<Vec<u8>>, CoreMethodsError>> + Send;
81
82    /// Append data to the core
83    fn append(
84        &self,
85        data: &[u8],
86    ) -> impl Future<Output = Result<AppendOutcome, CoreMethodsError>> + Send;
87
88    /// Append a batch of data to the core
89    fn append_batch<A: AsRef<[u8]>, B: AsRef<[A]> + Send>(
90        &self,
91        batch: B,
92    ) -> impl Future<Output = Result<AppendOutcome, CoreMethodsError>> + Send;
93}