pub mod events;
#[cfg(feature = "shared-core")]
pub mod shared_core;
#[cfg(feature = "shared-core")]
pub use shared_core::SharedCore;
use crate::{AppendOutcome, HypercoreError, Info, PartialKeypair};
use hypercore_schema::{Proof, RequestBlock, RequestSeek, RequestUpgrade};
pub use events::Event;
use async_broadcast::Receiver;
use std::future::Future;
pub trait CoreInfo {
fn info(&self) -> impl Future<Output = Info> + Send;
fn key_pair(&self) -> impl Future<Output = PartialKeypair> + Send;
}
#[derive(thiserror::Error, Debug)]
pub enum ReplicationMethodsError {
#[error("Got a hypercore error: [{0}]")]
HypercoreError(#[from] HypercoreError),
#[error("Got a CoreMethods error: [{0}]")]
CoreMethodsError(#[from] CoreMethodsError),
}
pub trait ReplicationMethods: CoreInfo + Send {
fn verify_and_apply_proof(
&self,
proof: &Proof,
) -> impl Future<Output = Result<bool, ReplicationMethodsError>> + Send;
fn missing_nodes(
&self,
index: u64,
) -> impl Future<Output = Result<u64, ReplicationMethodsError>> + Send;
fn create_proof(
&self,
block: Option<RequestBlock>,
hash: Option<RequestBlock>,
seek: Option<RequestSeek>,
upgrade: Option<RequestUpgrade>,
) -> impl Future<Output = Result<Option<Proof>, ReplicationMethodsError>> + Send;
fn event_subscribe(&self) -> impl Future<Output = Receiver<Event>>;
}
#[derive(thiserror::Error, Debug)]
pub enum CoreMethodsError {
#[error("Got a hypercore error [{0}]")]
HypercoreError(#[from] HypercoreError),
}
pub trait CoreMethods: CoreInfo {
fn has(&self, index: u64) -> impl Future<Output = bool> + Send;
fn get(
&self,
index: u64,
) -> impl Future<Output = Result<Option<Vec<u8>>, CoreMethodsError>> + Send;
fn append(
&self,
data: &[u8],
) -> impl Future<Output = Result<AppendOutcome, CoreMethodsError>> + Send;
fn append_batch<A: AsRef<[u8]>, B: AsRef<[A]> + Send>(
&self,
batch: B,
) -> impl Future<Output = Result<AppendOutcome, CoreMethodsError>> + Send;
}