|
| 1 | +//! Object-safe wrapper around ldk-node's `KVStore` + `KVStoreSync` traits. |
| 2 | +//! |
| 3 | +//! `lightning`'s `KVStore` returns `impl Future` from its methods, which makes the trait not |
| 4 | +//! object-safe — orange-sdk can't share a backend across components as `Arc<dyn KVStore>`. The |
| 5 | +//! supertrait `SyncAndAsyncKVStore` that ldk-node exposes inherits the same problem, and even |
| 6 | +//! if it didn't, no `Deref` blanket impl exists for `KVStoreSync`, so `Arc<...>` doesn't satisfy |
| 7 | +//! it on its own. |
| 8 | +//! |
| 9 | +//! This module defines `DynStore`, an object-safe trait covering both sync and async kv |
| 10 | +//! methods (async ones return boxed futures), with a blanket impl over any concrete type that |
| 11 | +//! implements `KVStore + KVStoreSync`. The whole crate stores backends as |
| 12 | +//! `Arc<dyn DynStore>`; conversion to a value ldk-node accepts happens at the call site |
| 13 | +//! through a thin newtype that delegates both traits back to `DynStore`. |
| 14 | +
|
| 15 | +use std::future::Future; |
| 16 | +use std::pin::Pin; |
| 17 | +use std::sync::Arc; |
| 18 | + |
| 19 | +use ldk_node::lightning::io; |
| 20 | +use ldk_node::lightning::util::persist::{KVStore, KVStoreSync}; |
| 21 | + |
| 22 | +/// Object-safe view of a `KVStore + KVStoreSync` backend. Async methods return boxed |
| 23 | +/// futures so the trait can be used through `dyn`. |
| 24 | +pub(crate) trait DynStore: Send + Sync + 'static { |
| 25 | + fn read_async( |
| 26 | + &self, primary_namespace: &str, secondary_namespace: &str, key: &str, |
| 27 | + ) -> Pin<Box<dyn Future<Output = Result<Vec<u8>, io::Error>> + Send + 'static>>; |
| 28 | + |
| 29 | + fn write_async( |
| 30 | + &self, primary_namespace: &str, secondary_namespace: &str, key: &str, buf: Vec<u8>, |
| 31 | + ) -> Pin<Box<dyn Future<Output = Result<(), io::Error>> + Send + 'static>>; |
| 32 | + |
| 33 | + fn remove_async( |
| 34 | + &self, primary_namespace: &str, secondary_namespace: &str, key: &str, lazy: bool, |
| 35 | + ) -> Pin<Box<dyn Future<Output = Result<(), io::Error>> + Send + 'static>>; |
| 36 | + |
| 37 | + fn list_async( |
| 38 | + &self, primary_namespace: &str, secondary_namespace: &str, |
| 39 | + ) -> Pin<Box<dyn Future<Output = Result<Vec<String>, io::Error>> + Send + 'static>>; |
| 40 | + |
| 41 | + fn read_sync( |
| 42 | + &self, primary_namespace: &str, secondary_namespace: &str, key: &str, |
| 43 | + ) -> Result<Vec<u8>, io::Error>; |
| 44 | + |
| 45 | + fn write_sync( |
| 46 | + &self, primary_namespace: &str, secondary_namespace: &str, key: &str, buf: Vec<u8>, |
| 47 | + ) -> Result<(), io::Error>; |
| 48 | + |
| 49 | + fn remove_sync( |
| 50 | + &self, primary_namespace: &str, secondary_namespace: &str, key: &str, lazy: bool, |
| 51 | + ) -> Result<(), io::Error>; |
| 52 | + |
| 53 | + fn list_sync( |
| 54 | + &self, primary_namespace: &str, secondary_namespace: &str, |
| 55 | + ) -> Result<Vec<String>, io::Error>; |
| 56 | +} |
| 57 | + |
| 58 | +impl<T> DynStore for T |
| 59 | +where |
| 60 | + T: KVStore + KVStoreSync + Send + Sync + 'static, |
| 61 | +{ |
| 62 | + fn read_async( |
| 63 | + &self, p: &str, s: &str, k: &str, |
| 64 | + ) -> Pin<Box<dyn Future<Output = Result<Vec<u8>, io::Error>> + Send + 'static>> { |
| 65 | + Box::pin(<T as KVStore>::read(self, p, s, k)) |
| 66 | + } |
| 67 | + |
| 68 | + fn write_async( |
| 69 | + &self, p: &str, s: &str, k: &str, buf: Vec<u8>, |
| 70 | + ) -> Pin<Box<dyn Future<Output = Result<(), io::Error>> + Send + 'static>> { |
| 71 | + Box::pin(<T as KVStore>::write(self, p, s, k, buf)) |
| 72 | + } |
| 73 | + |
| 74 | + fn remove_async( |
| 75 | + &self, p: &str, s: &str, k: &str, lazy: bool, |
| 76 | + ) -> Pin<Box<dyn Future<Output = Result<(), io::Error>> + Send + 'static>> { |
| 77 | + Box::pin(<T as KVStore>::remove(self, p, s, k, lazy)) |
| 78 | + } |
| 79 | + |
| 80 | + fn list_async( |
| 81 | + &self, p: &str, s: &str, |
| 82 | + ) -> Pin<Box<dyn Future<Output = Result<Vec<String>, io::Error>> + Send + 'static>> { |
| 83 | + Box::pin(<T as KVStore>::list(self, p, s)) |
| 84 | + } |
| 85 | + |
| 86 | + fn read_sync(&self, p: &str, s: &str, k: &str) -> Result<Vec<u8>, io::Error> { |
| 87 | + <T as KVStoreSync>::read(self, p, s, k) |
| 88 | + } |
| 89 | + |
| 90 | + fn write_sync(&self, p: &str, s: &str, k: &str, buf: Vec<u8>) -> Result<(), io::Error> { |
| 91 | + <T as KVStoreSync>::write(self, p, s, k, buf) |
| 92 | + } |
| 93 | + |
| 94 | + fn remove_sync(&self, p: &str, s: &str, k: &str, lazy: bool) -> Result<(), io::Error> { |
| 95 | + <T as KVStoreSync>::remove(self, p, s, k, lazy) |
| 96 | + } |
| 97 | + |
| 98 | + fn list_sync(&self, p: &str, s: &str) -> Result<Vec<String>, io::Error> { |
| 99 | + <T as KVStoreSync>::list(self, p, s) |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +// Make `Arc<dyn DynStore>` itself implement `KVStore` + `KVStoreSync` so the same handle |
| 104 | +// orange-sdk shares internally can be handed to ldk-node's `build_with_store`. We give it |
| 105 | +// `KVStore::read` etc. by forwarding to the boxed-future variants on the trait, and the |
| 106 | +// sync trait by forwarding to the sync variants. |
| 107 | +// |
| 108 | +// Note: we impl on `dyn DynStore` (which is local), not `Arc` directly — that gives us |
| 109 | +// `&dyn DynStore: KVStore + KVStoreSync` and, via lightning's `Deref` blanket impl for |
| 110 | +// `KVStore`, `Arc<dyn DynStore>: KVStore`. For `KVStoreSync` (no `Deref` blanket exists) |
| 111 | +// callers wrap the `Arc` in `LdkNodeStore` below before handing it to ldk-node. |
| 112 | +impl KVStore for dyn DynStore { |
| 113 | + fn read( |
| 114 | + &self, p: &str, s: &str, k: &str, |
| 115 | + ) -> impl Future<Output = Result<Vec<u8>, io::Error>> + Send + 'static { |
| 116 | + self.read_async(p, s, k) |
| 117 | + } |
| 118 | + fn write( |
| 119 | + &self, p: &str, s: &str, k: &str, buf: Vec<u8>, |
| 120 | + ) -> impl Future<Output = Result<(), io::Error>> + Send + 'static { |
| 121 | + self.write_async(p, s, k, buf) |
| 122 | + } |
| 123 | + fn remove( |
| 124 | + &self, p: &str, s: &str, k: &str, lazy: bool, |
| 125 | + ) -> impl Future<Output = Result<(), io::Error>> + Send + 'static { |
| 126 | + self.remove_async(p, s, k, lazy) |
| 127 | + } |
| 128 | + fn list( |
| 129 | + &self, p: &str, s: &str, |
| 130 | + ) -> impl Future<Output = Result<Vec<String>, io::Error>> + Send + 'static { |
| 131 | + self.list_async(p, s) |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +impl KVStoreSync for dyn DynStore { |
| 136 | + fn read(&self, p: &str, s: &str, k: &str) -> Result<Vec<u8>, io::Error> { |
| 137 | + self.read_sync(p, s, k) |
| 138 | + } |
| 139 | + fn write(&self, p: &str, s: &str, k: &str, buf: Vec<u8>) -> Result<(), io::Error> { |
| 140 | + self.write_sync(p, s, k, buf) |
| 141 | + } |
| 142 | + fn remove(&self, p: &str, s: &str, k: &str, lazy: bool) -> Result<(), io::Error> { |
| 143 | + self.remove_sync(p, s, k, lazy) |
| 144 | + } |
| 145 | + fn list(&self, p: &str, s: &str) -> Result<Vec<String>, io::Error> { |
| 146 | + self.list_sync(p, s) |
| 147 | + } |
| 148 | +} |
| 149 | + |
| 150 | +/// Cloneable handle wrapping `Arc<dyn DynStore>` that satisfies ldk-node's |
| 151 | +/// `SyncAndAsyncKVStore + Send + Sync + 'static` bound on `build_with_store`. Both trait |
| 152 | +/// impls just forward to the underlying `dyn DynStore`. |
| 153 | +#[derive(Clone)] |
| 154 | +pub(crate) struct LdkNodeStore(pub(crate) Arc<dyn DynStore>); |
| 155 | + |
| 156 | +impl KVStore for LdkNodeStore { |
| 157 | + fn read( |
| 158 | + &self, p: &str, s: &str, k: &str, |
| 159 | + ) -> impl Future<Output = Result<Vec<u8>, io::Error>> + Send + 'static { |
| 160 | + self.0.read_async(p, s, k) |
| 161 | + } |
| 162 | + fn write( |
| 163 | + &self, p: &str, s: &str, k: &str, buf: Vec<u8>, |
| 164 | + ) -> impl Future<Output = Result<(), io::Error>> + Send + 'static { |
| 165 | + self.0.write_async(p, s, k, buf) |
| 166 | + } |
| 167 | + fn remove( |
| 168 | + &self, p: &str, s: &str, k: &str, lazy: bool, |
| 169 | + ) -> impl Future<Output = Result<(), io::Error>> + Send + 'static { |
| 170 | + self.0.remove_async(p, s, k, lazy) |
| 171 | + } |
| 172 | + fn list( |
| 173 | + &self, p: &str, s: &str, |
| 174 | + ) -> impl Future<Output = Result<Vec<String>, io::Error>> + Send + 'static { |
| 175 | + self.0.list_async(p, s) |
| 176 | + } |
| 177 | +} |
| 178 | + |
| 179 | +impl KVStoreSync for LdkNodeStore { |
| 180 | + fn read(&self, p: &str, s: &str, k: &str) -> Result<Vec<u8>, io::Error> { |
| 181 | + self.0.read_sync(p, s, k) |
| 182 | + } |
| 183 | + fn write(&self, p: &str, s: &str, k: &str, buf: Vec<u8>) -> Result<(), io::Error> { |
| 184 | + self.0.write_sync(p, s, k, buf) |
| 185 | + } |
| 186 | + fn remove(&self, p: &str, s: &str, k: &str, lazy: bool) -> Result<(), io::Error> { |
| 187 | + self.0.remove_sync(p, s, k, lazy) |
| 188 | + } |
| 189 | + fn list(&self, p: &str, s: &str) -> Result<Vec<String>, io::Error> { |
| 190 | + self.0.list_sync(p, s) |
| 191 | + } |
| 192 | +} |
0 commit comments