use super::error::{ChannelStoreError, SecretStoreError, ShareStoreError, StateStoreError};
use crate::Result;
use crate::protocol::types::{
ChannelQuery, ChannelRecord, HelperChannel, HelperFilter, MissingPolicy, ReplicaFilter,
ReplicaMember, SecretKind, SecretValue, Share, StateItem, StateKey, StateKind, UserSecrets,
};
use crate::types::ChannelId;
use derec_proto::TransportProtocol;
use std::{future::Future, pin::Pin};
#[cfg(target_arch = "wasm32")]
pub type SecretStoreFuture<'a, T> =
Pin<Box<dyn Future<Output = std::result::Result<T, SecretStoreError>> + 'a>>;
#[cfg(not(target_arch = "wasm32"))]
pub type SecretStoreFuture<'a, T> =
Pin<Box<dyn Future<Output = std::result::Result<T, SecretStoreError>> + Send + 'a>>;
#[cfg(target_arch = "wasm32")]
pub type ChannelStoreFuture<'a, T> =
Pin<Box<dyn Future<Output = std::result::Result<T, ChannelStoreError>> + 'a>>;
#[cfg(not(target_arch = "wasm32"))]
pub type ChannelStoreFuture<'a, T> =
Pin<Box<dyn Future<Output = std::result::Result<T, ChannelStoreError>> + Send + 'a>>;
#[cfg(target_arch = "wasm32")]
pub type ShareStoreFuture<'a, T> =
Pin<Box<dyn Future<Output = std::result::Result<T, ShareStoreError>> + 'a>>;
#[cfg(not(target_arch = "wasm32"))]
pub type ShareStoreFuture<'a, T> =
Pin<Box<dyn Future<Output = std::result::Result<T, ShareStoreError>> + Send + 'a>>;
#[cfg(target_arch = "wasm32")]
pub type TransportFuture<'a> = Pin<Box<dyn Future<Output = Result<()>> + 'a>>;
#[cfg(not(target_arch = "wasm32"))]
pub type TransportFuture<'a> = Pin<Box<dyn Future<Output = Result<()>> + Send + 'a>>;
#[cfg(target_arch = "wasm32")]
pub type StateStoreFuture<'a, T> =
Pin<Box<dyn Future<Output = std::result::Result<T, StateStoreError>> + 'a>>;
#[cfg(not(target_arch = "wasm32"))]
pub type StateStoreFuture<'a, T> =
Pin<Box<dyn Future<Output = std::result::Result<T, StateStoreError>> + Send + 'a>>;
#[cfg(not(target_arch = "wasm32"))]
const _: () = {
const fn require_send<T: Send>() {}
require_send::<SecretStoreFuture<'static, ()>>();
require_send::<ChannelStoreFuture<'static, ()>>();
require_send::<ShareStoreFuture<'static, ()>>();
require_send::<StateStoreFuture<'static, ()>>();
require_send::<TransportFuture<'static>>();
};
pub trait DeRecSecretStore {
fn load(
&self,
secret_id: u64,
channel_id: ChannelId,
kind: SecretKind,
) -> SecretStoreFuture<'_, Option<SecretValue>>;
fn load_many(
&self,
secret_id: u64,
channel_ids: &[ChannelId],
kind: SecretKind,
missing_policy: MissingPolicy,
) -> SecretStoreFuture<'_, Vec<(ChannelId, SecretValue)>>;
fn save(
&mut self,
secret_id: u64,
channel_id: ChannelId,
value: SecretValue,
) -> SecretStoreFuture<'_, ()>;
fn remove(
&mut self,
secret_id: u64,
channel_id: ChannelId,
kind: SecretKind,
) -> SecretStoreFuture<'_, ()>;
}
pub trait DeRecChannelStore {
fn load(
&self,
secret_id: u64,
query: ChannelQuery,
) -> ChannelStoreFuture<'_, Option<ChannelRecord>>;
fn save(&mut self, secret_id: u64, record: ChannelRecord) -> ChannelStoreFuture<'_, ()>;
fn remove(&mut self, secret_id: u64, query: ChannelQuery) -> ChannelStoreFuture<'_, bool>;
fn helpers(
&self,
secret_id: u64,
filter: HelperFilter,
) -> ChannelStoreFuture<'_, Vec<HelperChannel>>;
fn replicas(
&self,
secret_id: u64,
filter: ReplicaFilter,
) -> ChannelStoreFuture<'_, Vec<ReplicaMember>>;
fn link_channel(
&mut self,
secret_id: u64,
a: ChannelId,
b: ChannelId,
) -> ChannelStoreFuture<'_, ()>;
fn linked_channels(
&self,
secret_id: u64,
channel_id: ChannelId,
) -> ChannelStoreFuture<'_, Vec<ChannelId>>;
}
pub trait DeRecShareStore {
fn load(
&self,
secret_id: u64,
channel_id: ChannelId,
versions: &[u32],
) -> ShareStoreFuture<'_, Vec<Share>>;
fn load_many(
&self,
secret_id: u64,
channel_ids: &[ChannelId],
versions: &[u32],
) -> ShareStoreFuture<'_, Vec<Share>>;
fn load_all(
&self,
secret_id: u64,
channel_ids: &[ChannelId],
) -> ShareStoreFuture<'_, Vec<Share>>;
fn latest_version(&self, secret_id: u64) -> ShareStoreFuture<'_, Option<u32>>;
fn save(
&mut self,
secret_id: u64,
channel_id: ChannelId,
share: Share,
) -> ShareStoreFuture<'_, ()>;
fn remove_channel(&mut self, secret_id: u64, channel_id: ChannelId)
-> ShareStoreFuture<'_, ()>;
fn remove_versions(
&mut self,
secret_id: u64,
channel_id: ChannelId,
versions: &[u32],
) -> ShareStoreFuture<'_, ()>;
fn keep_list(&self, secret_id: u64, version: u32) -> ShareStoreFuture<'_, Option<Vec<u32>>>;
fn record_verification(
&mut self,
secret_id: u64,
channel_id: ChannelId,
version: u32,
) -> ShareStoreFuture<'_, ()> {
let _ = (secret_id, channel_id, version);
Box::pin(std::future::ready(Ok(())))
}
}
pub trait DeRecUserSecretStore {
fn load_latest(&self, secret_id: u64) -> ShareStoreFuture<'_, Option<UserSecrets>>;
fn save_latest(&mut self, secret_id: u64, value: UserSecrets) -> ShareStoreFuture<'_, ()>;
fn remove(&mut self, secret_id: u64) -> ShareStoreFuture<'_, ()>;
}
pub trait DeRecTransport {
fn send(&self, endpoints: &[TransportProtocol], message: Vec<u8>) -> TransportFuture<'_>;
}
pub trait DeRecStateStore {
fn save(&mut self, secret_id: u64, item: StateItem) -> StateStoreFuture<'_, ()>;
fn load(&self, secret_id: u64, key: StateKey) -> StateStoreFuture<'_, Option<StateItem>>;
fn remove(&mut self, secret_id: u64, key: StateKey) -> StateStoreFuture<'_, bool>;
fn load_all(&self, secret_id: u64, kind: StateKind) -> StateStoreFuture<'_, Vec<StateItem>>;
}
impl<T: DeRecSecretStore + ?Sized> DeRecSecretStore for Box<T> {
fn load(
&self,
secret_id: u64,
channel_id: ChannelId,
kind: SecretKind,
) -> SecretStoreFuture<'_, Option<SecretValue>> {
(**self).load(secret_id, channel_id, kind)
}
fn load_many(
&self,
secret_id: u64,
channel_ids: &[ChannelId],
kind: SecretKind,
missing_policy: MissingPolicy,
) -> SecretStoreFuture<'_, Vec<(ChannelId, SecretValue)>> {
(**self).load_many(secret_id, channel_ids, kind, missing_policy)
}
fn save(
&mut self,
secret_id: u64,
channel_id: ChannelId,
value: SecretValue,
) -> SecretStoreFuture<'_, ()> {
(**self).save(secret_id, channel_id, value)
}
fn remove(
&mut self,
secret_id: u64,
channel_id: ChannelId,
kind: SecretKind,
) -> SecretStoreFuture<'_, ()> {
(**self).remove(secret_id, channel_id, kind)
}
}
impl<T: DeRecChannelStore + ?Sized> DeRecChannelStore for Box<T> {
fn load(
&self,
secret_id: u64,
query: ChannelQuery,
) -> ChannelStoreFuture<'_, Option<ChannelRecord>> {
(**self).load(secret_id, query)
}
fn save(&mut self, secret_id: u64, record: ChannelRecord) -> ChannelStoreFuture<'_, ()> {
(**self).save(secret_id, record)
}
fn remove(&mut self, secret_id: u64, query: ChannelQuery) -> ChannelStoreFuture<'_, bool> {
(**self).remove(secret_id, query)
}
fn helpers(
&self,
secret_id: u64,
filter: HelperFilter,
) -> ChannelStoreFuture<'_, Vec<HelperChannel>> {
(**self).helpers(secret_id, filter)
}
fn replicas(
&self,
secret_id: u64,
filter: ReplicaFilter,
) -> ChannelStoreFuture<'_, Vec<ReplicaMember>> {
(**self).replicas(secret_id, filter)
}
fn link_channel(
&mut self,
secret_id: u64,
a: ChannelId,
b: ChannelId,
) -> ChannelStoreFuture<'_, ()> {
(**self).link_channel(secret_id, a, b)
}
fn linked_channels(
&self,
secret_id: u64,
channel_id: ChannelId,
) -> ChannelStoreFuture<'_, Vec<ChannelId>> {
(**self).linked_channels(secret_id, channel_id)
}
}
impl<T: DeRecShareStore + ?Sized> DeRecShareStore for Box<T> {
fn load(
&self,
secret_id: u64,
channel_id: ChannelId,
versions: &[u32],
) -> ShareStoreFuture<'_, Vec<Share>> {
(**self).load(secret_id, channel_id, versions)
}
fn load_many(
&self,
secret_id: u64,
channel_ids: &[ChannelId],
versions: &[u32],
) -> ShareStoreFuture<'_, Vec<Share>> {
(**self).load_many(secret_id, channel_ids, versions)
}
fn load_all(
&self,
secret_id: u64,
channel_ids: &[ChannelId],
) -> ShareStoreFuture<'_, Vec<Share>> {
(**self).load_all(secret_id, channel_ids)
}
fn latest_version(&self, secret_id: u64) -> ShareStoreFuture<'_, Option<u32>> {
(**self).latest_version(secret_id)
}
fn save(
&mut self,
secret_id: u64,
channel_id: ChannelId,
share: Share,
) -> ShareStoreFuture<'_, ()> {
(**self).save(secret_id, channel_id, share)
}
fn remove_channel(
&mut self,
secret_id: u64,
channel_id: ChannelId,
) -> ShareStoreFuture<'_, ()> {
(**self).remove_channel(secret_id, channel_id)
}
fn remove_versions(
&mut self,
secret_id: u64,
channel_id: ChannelId,
versions: &[u32],
) -> ShareStoreFuture<'_, ()> {
(**self).remove_versions(secret_id, channel_id, versions)
}
fn keep_list(&self, secret_id: u64, version: u32) -> ShareStoreFuture<'_, Option<Vec<u32>>> {
(**self).keep_list(secret_id, version)
}
fn record_verification(
&mut self,
secret_id: u64,
channel_id: ChannelId,
version: u32,
) -> ShareStoreFuture<'_, ()> {
(**self).record_verification(secret_id, channel_id, version)
}
}
impl<T: DeRecUserSecretStore + ?Sized> DeRecUserSecretStore for Box<T> {
fn load_latest(&self, secret_id: u64) -> ShareStoreFuture<'_, Option<UserSecrets>> {
(**self).load_latest(secret_id)
}
fn save_latest(&mut self, secret_id: u64, value: UserSecrets) -> ShareStoreFuture<'_, ()> {
(**self).save_latest(secret_id, value)
}
fn remove(&mut self, secret_id: u64) -> ShareStoreFuture<'_, ()> {
(**self).remove(secret_id)
}
}
impl<T: DeRecStateStore + ?Sized> DeRecStateStore for Box<T> {
fn save(&mut self, secret_id: u64, item: StateItem) -> StateStoreFuture<'_, ()> {
(**self).save(secret_id, item)
}
fn load(&self, secret_id: u64, key: StateKey) -> StateStoreFuture<'_, Option<StateItem>> {
(**self).load(secret_id, key)
}
fn remove(&mut self, secret_id: u64, key: StateKey) -> StateStoreFuture<'_, bool> {
(**self).remove(secret_id, key)
}
fn load_all(&self, secret_id: u64, kind: StateKind) -> StateStoreFuture<'_, Vec<StateItem>> {
(**self).load_all(secret_id, kind)
}
}
impl<T: DeRecTransport + ?Sized> DeRecTransport for Box<T> {
fn send(&self, endpoints: &[TransportProtocol], message: Vec<u8>) -> TransportFuture<'_> {
(**self).send(endpoints, message)
}
}
impl<T: DeRecSecretStore + ?Sized> DeRecSecretStore for &mut T {
fn load(
&self,
secret_id: u64,
channel_id: ChannelId,
kind: SecretKind,
) -> SecretStoreFuture<'_, Option<SecretValue>> {
(**self).load(secret_id, channel_id, kind)
}
fn load_many(
&self,
secret_id: u64,
channel_ids: &[ChannelId],
kind: SecretKind,
missing_policy: MissingPolicy,
) -> SecretStoreFuture<'_, Vec<(ChannelId, SecretValue)>> {
(**self).load_many(secret_id, channel_ids, kind, missing_policy)
}
fn save(
&mut self,
secret_id: u64,
channel_id: ChannelId,
value: SecretValue,
) -> SecretStoreFuture<'_, ()> {
(**self).save(secret_id, channel_id, value)
}
fn remove(
&mut self,
secret_id: u64,
channel_id: ChannelId,
kind: SecretKind,
) -> SecretStoreFuture<'_, ()> {
(**self).remove(secret_id, channel_id, kind)
}
}
impl<T: DeRecChannelStore + ?Sized> DeRecChannelStore for &mut T {
fn load(
&self,
secret_id: u64,
query: ChannelQuery,
) -> ChannelStoreFuture<'_, Option<ChannelRecord>> {
(**self).load(secret_id, query)
}
fn save(&mut self, secret_id: u64, record: ChannelRecord) -> ChannelStoreFuture<'_, ()> {
(**self).save(secret_id, record)
}
fn remove(&mut self, secret_id: u64, query: ChannelQuery) -> ChannelStoreFuture<'_, bool> {
(**self).remove(secret_id, query)
}
fn helpers(
&self,
secret_id: u64,
filter: HelperFilter,
) -> ChannelStoreFuture<'_, Vec<HelperChannel>> {
(**self).helpers(secret_id, filter)
}
fn replicas(
&self,
secret_id: u64,
filter: ReplicaFilter,
) -> ChannelStoreFuture<'_, Vec<ReplicaMember>> {
(**self).replicas(secret_id, filter)
}
fn link_channel(
&mut self,
secret_id: u64,
a: ChannelId,
b: ChannelId,
) -> ChannelStoreFuture<'_, ()> {
(**self).link_channel(secret_id, a, b)
}
fn linked_channels(
&self,
secret_id: u64,
channel_id: ChannelId,
) -> ChannelStoreFuture<'_, Vec<ChannelId>> {
(**self).linked_channels(secret_id, channel_id)
}
}
impl<T: DeRecShareStore + ?Sized> DeRecShareStore for &mut T {
fn load(
&self,
secret_id: u64,
channel_id: ChannelId,
versions: &[u32],
) -> ShareStoreFuture<'_, Vec<Share>> {
(**self).load(secret_id, channel_id, versions)
}
fn load_many(
&self,
secret_id: u64,
channel_ids: &[ChannelId],
versions: &[u32],
) -> ShareStoreFuture<'_, Vec<Share>> {
(**self).load_many(secret_id, channel_ids, versions)
}
fn load_all(
&self,
secret_id: u64,
channel_ids: &[ChannelId],
) -> ShareStoreFuture<'_, Vec<Share>> {
(**self).load_all(secret_id, channel_ids)
}
fn latest_version(&self, secret_id: u64) -> ShareStoreFuture<'_, Option<u32>> {
(**self).latest_version(secret_id)
}
fn save(
&mut self,
secret_id: u64,
channel_id: ChannelId,
share: Share,
) -> ShareStoreFuture<'_, ()> {
(**self).save(secret_id, channel_id, share)
}
fn remove_channel(
&mut self,
secret_id: u64,
channel_id: ChannelId,
) -> ShareStoreFuture<'_, ()> {
(**self).remove_channel(secret_id, channel_id)
}
fn remove_versions(
&mut self,
secret_id: u64,
channel_id: ChannelId,
versions: &[u32],
) -> ShareStoreFuture<'_, ()> {
(**self).remove_versions(secret_id, channel_id, versions)
}
fn keep_list(&self, secret_id: u64, version: u32) -> ShareStoreFuture<'_, Option<Vec<u32>>> {
(**self).keep_list(secret_id, version)
}
fn record_verification(
&mut self,
secret_id: u64,
channel_id: ChannelId,
version: u32,
) -> ShareStoreFuture<'_, ()> {
(**self).record_verification(secret_id, channel_id, version)
}
}
impl<T: DeRecUserSecretStore + ?Sized> DeRecUserSecretStore for &mut T {
fn load_latest(&self, secret_id: u64) -> ShareStoreFuture<'_, Option<UserSecrets>> {
(**self).load_latest(secret_id)
}
fn save_latest(&mut self, secret_id: u64, value: UserSecrets) -> ShareStoreFuture<'_, ()> {
(**self).save_latest(secret_id, value)
}
fn remove(&mut self, secret_id: u64) -> ShareStoreFuture<'_, ()> {
(**self).remove(secret_id)
}
}
impl<T: DeRecStateStore + ?Sized> DeRecStateStore for &mut T {
fn save(&mut self, secret_id: u64, item: StateItem) -> StateStoreFuture<'_, ()> {
(**self).save(secret_id, item)
}
fn load(&self, secret_id: u64, key: StateKey) -> StateStoreFuture<'_, Option<StateItem>> {
(**self).load(secret_id, key)
}
fn remove(&mut self, secret_id: u64, key: StateKey) -> StateStoreFuture<'_, bool> {
(**self).remove(secret_id, key)
}
fn load_all(&self, secret_id: u64, kind: StateKind) -> StateStoreFuture<'_, Vec<StateItem>> {
(**self).load_all(secret_id, kind)
}
}
impl<T: DeRecTransport + ?Sized> DeRecTransport for &mut T {
fn send(&self, endpoints: &[TransportProtocol], message: Vec<u8>) -> TransportFuture<'_> {
(**self).send(endpoints, message)
}
}
impl<T: DeRecTransport + ?Sized> DeRecTransport for std::sync::Arc<T> {
fn send(&self, endpoints: &[TransportProtocol], message: Vec<u8>) -> TransportFuture<'_> {
(**self).send(endpoints, message)
}
}
#[cfg(test)]
mod pointer_forwarding_tests {
use super::*;
use crate::protocol::tests::{
InMemChannelStore, InMemPersistedStateStore, InMemSecretStore, InMemShareStore,
InMemUserSecretStore, NoopTransport, run_async,
};
use crate::protocol::{DeRecProtocol, DeRecProtocolBuilder};
const SECRET_ID: u64 = 0x0B0_9ED;
type ErasedProtocol = DeRecProtocol<
Box<dyn DeRecChannelStore>,
Box<dyn DeRecShareStore>,
Box<dyn DeRecSecretStore>,
Box<dyn DeRecUserSecretStore>,
Box<dyn DeRecStateStore>,
std::sync::Arc<NoopTransport>,
>;
fn build_erased() -> ErasedProtocol {
DeRecProtocolBuilder::new(SECRET_ID)
.with_channel_store(Box::new(InMemChannelStore::default()) as Box<dyn DeRecChannelStore>)
.with_share_store(Box::new(InMemShareStore::default()) as Box<dyn DeRecShareStore>)
.with_secret_store(Box::new(InMemSecretStore::default()) as Box<dyn DeRecSecretStore>)
.with_user_secret_store(
Box::new(InMemUserSecretStore::default()) as Box<dyn DeRecUserSecretStore>
)
.with_state_store(
Box::new(InMemPersistedStateStore::default()) as Box<dyn DeRecStateStore>
)
.with_transport(std::sync::Arc::new(NoopTransport))
.with_own_transports(["https://erased.example.com"])
.with_threshold(2)
.build()
.expect("a fully type-erased protocol must build")
}
#[test]
fn an_erased_protocol_reads_back_what_it_wrote() {
run_async(async {
let mut protocol = build_erased();
protocol
.state_store
.save(
SECRET_ID,
StateItem::PendingUnpair {
channel_id: ChannelId(4242),
started_at: 1_700_000_000,
},
)
.await
.expect("save through Box<dyn DeRecStateStore>");
let loaded = protocol
.state_store
.load(
SECRET_ID,
StateKey::PendingUnpair {
channel_id: ChannelId(4242),
},
)
.await
.expect("load through Box<dyn DeRecStateStore>");
assert!(
loaded.is_some(),
"the write must be visible through the box"
);
let removed = protocol
.state_store
.remove(
SECRET_ID,
StateKey::PendingUnpair {
channel_id: ChannelId(4242),
},
)
.await
.expect("remove through Box<dyn DeRecStateStore>");
assert!(removed, "remove reports it deleted the row");
});
}
#[test]
fn an_erased_protocol_ticks() {
run_async(async {
let mut protocol = build_erased();
let events = protocol.tick().await;
assert!(events.is_empty(), "idle tick; got {events:?}");
});
}
#[test]
fn a_borrowed_store_satisfies_the_bound() {
fn assert_store<S: DeRecStateStore>() {}
assert_store::<&mut InMemPersistedStateStore>();
assert_store::<Box<InMemPersistedStateStore>>();
assert_store::<Box<dyn DeRecStateStore>>();
}
#[test]
fn a_transport_can_be_shared_behind_an_arc() {
fn assert_transport<T: DeRecTransport>() {}
assert_transport::<std::sync::Arc<NoopTransport>>();
assert_transport::<std::sync::Arc<dyn DeRecTransport>>();
assert_transport::<Box<dyn DeRecTransport>>();
}
}