hypercore/replication/
mod.rs1pub 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
19pub trait CoreInfo {
21 fn info(&self) -> impl Future<Output = Info> + Send;
23 fn key_pair(&self) -> impl Future<Output = PartialKeypair> + Send;
25}
26
27#[derive(thiserror::Error, Debug)]
29pub enum ReplicationMethodsError {
30 #[error("Got a hypercore error: [{0}]")]
32 HypercoreError(#[from] HypercoreError),
33 #[error("Got a CoreMethods error: [{0}]")]
35 CoreMethodsError(#[from] CoreMethodsError),
36}
37
38pub trait ReplicationMethods: CoreInfo + Send {
40 fn verify_and_apply_proof(
42 &self,
43 proof: &Proof,
44 ) -> impl Future<Output = Result<bool, ReplicationMethodsError>> + Send;
45 fn missing_nodes(
47 &self,
48 index: u64,
49 ) -> impl Future<Output = Result<u64, ReplicationMethodsError>> + Send;
50 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 fn event_subscribe(&self) -> impl Future<Output = Receiver<Event>>;
60}
61
62#[derive(thiserror::Error, Debug)]
64pub enum CoreMethodsError {
65 #[error("Got a hypercore error [{0}]")]
67 HypercoreError(#[from] HypercoreError),
68}
69
70pub trait CoreMethods: CoreInfo {
73 fn has(&self, index: u64) -> impl Future<Output = bool> + Send;
75
76 fn get(
78 &self,
79 index: u64,
80 ) -> impl Future<Output = Result<Option<Vec<u8>>, CoreMethodsError>> + Send;
81
82 fn append(
84 &self,
85 data: &[u8],
86 ) -> impl Future<Output = Result<AppendOutcome, CoreMethodsError>> + Send;
87
88 fn append_batch<A: AsRef<[u8]>, B: AsRef<[A]> + Send>(
90 &self,
91 batch: B,
92 ) -> impl Future<Output = Result<AppendOutcome, CoreMethodsError>> + Send;
93}