From ddbf9d18d305be92ee3965099bdbcabd58c7d576 Mon Sep 17 00:00:00 2001 From: Blake Griffith Date: Mon, 1 Jul 2024 01:59:05 -0700 Subject: [PATCH 1/4] Update Hypercore dependency --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 638c50b..943ca94 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -36,7 +36,7 @@ tracing = "0.1" pretty-hash = "0.4" futures-timer = "3" futures-lite = "1" -hypercore = { version = "0.12", default-features = false } +hypercore = { version = "0.13.0", default-features = false } sha2 = "0.10" curve25519-dalek = "4" crypto_secretstream = "0.2" From dca9a165c836971341de2aeba54f6c7146bc1837 Mon Sep 17 00:00:00 2001 From: Blake Griffith Date: Mon, 1 Jul 2024 01:46:28 -0700 Subject: [PATCH 2/4] Remove Hypercore generic params in example --- examples/replication.rs | 54 ++++++++++++----------------------------- 1 file changed, 16 insertions(+), 38 deletions(-) diff --git a/examples/replication.rs b/examples/replication.rs index fca2bd9..bf65b72 100644 --- a/examples/replication.rs +++ b/examples/replication.rs @@ -10,8 +10,6 @@ use hypercore::{ VerifyingKey, }; use log::*; -use random_access_memory::RandomAccessMemory; -use random_access_storage::RandomAccess; use std::collections::HashMap; use std::convert::TryInto; use std::env; @@ -38,7 +36,7 @@ fn main() { }); task::block_on(async move { - let mut hypercore_store: HypercoreStore = HypercoreStore::new(); + let mut hypercore_store: HypercoreStore = HypercoreStore::new(); let storage = Storage::new_memory().await.unwrap(); // Create a hypercore. let hypercore = if let Some(key) = key { @@ -86,14 +84,11 @@ fn usage() { // or once when connected (if client). // Unfortunately, everything that touches the hypercore_store or a hypercore has to be generic // at the moment. -async fn onconnection( +async fn onconnection( stream: TcpStream, is_initiator: bool, - hypercore_store: Arc>, -) -> Result<()> -where - T: RandomAccess + Debug + Send, -{ + hypercore_store: Arc, +) -> Result<()> { info!("onconnection, initiator: {}", is_initiator); let mut protocol = ProtocolBuilder::new(is_initiator).connect(stream); info!("protocol created, polling for next()"); @@ -127,27 +122,21 @@ where /// A container for hypercores. #[derive(Debug)] -struct HypercoreStore -where - T: RandomAccess + Debug + Send, -{ - hypercores: HashMap>>, +struct HypercoreStore { + hypercores: HashMap>, } -impl HypercoreStore -where - T: RandomAccess + Debug + Send, -{ +impl HypercoreStore { pub fn new() -> Self { let hypercores = HashMap::new(); Self { hypercores } } - pub fn add(&mut self, hypercore: HypercoreWrapper) { + pub fn add(&mut self, hypercore: HypercoreWrapper) { let hdkey = hex::encode(hypercore.discovery_key); self.hypercores.insert(hdkey, Arc::new(hypercore)); } - pub fn get(&self, discovery_key: &[u8; 32]) -> Option<&Arc>> { + pub fn get(&self, discovery_key: &[u8; 32]) -> Option<&Arc> { let hdkey = hex::encode(discovery_key); self.hypercores.get(&hdkey) } @@ -155,17 +144,14 @@ where /// A Hypercore is a single unit of replication, an append-only log. #[derive(Debug, Clone)] -struct HypercoreWrapper -where - T: RandomAccess + Debug + Send, -{ +struct HypercoreWrapper { discovery_key: [u8; 32], key: [u8; 32], - hypercore: Arc>>, + hypercore: Arc>, } -impl HypercoreWrapper { - pub fn from_memory_hypercore(hypercore: Hypercore) -> Self { +impl HypercoreWrapper { + pub fn from_memory_hypercore(hypercore: Hypercore) -> Self { let key = hypercore.key_pair().public.to_bytes(); HypercoreWrapper { key, @@ -173,12 +159,7 @@ impl HypercoreWrapper { hypercore: Arc::new(Mutex::new(hypercore)), } } -} -impl HypercoreWrapper -where - T: RandomAccess + Debug + Send + 'static, -{ pub fn key(&self) -> &[u8; 32] { &self.key } @@ -263,15 +244,12 @@ impl Default for PeerState { } } -async fn onmessage( - hypercore: &mut Arc>>, +async fn onmessage( + hypercore: &mut Arc>, peer_state: &mut PeerState, channel: &mut Channel, message: Message, -) -> Result<()> -where - T: RandomAccess + Debug + Send, -{ +) -> Result<()> { match message { Message::Synchronize(message) => { println!("Got Synchronize message {message:?}"); From 3fdd5cdbfd83d9be752b9aa6e2292faaeeb8e51a Mon Sep 17 00:00:00 2001 From: Blake Griffith Date: Mon, 1 Jul 2024 01:55:15 -0700 Subject: [PATCH 3/4] Update js interop tests to rm generic HC params --- tests/js_interop.rs | 54 ++++++++++++++------------------------------- 1 file changed, 16 insertions(+), 38 deletions(-) diff --git a/tests/js_interop.rs b/tests/js_interop.rs index fb0e4ea..0e572e7 100644 --- a/tests/js_interop.rs +++ b/tests/js_interop.rs @@ -8,8 +8,6 @@ use hypercore::{ VerifyingKey, PUBLIC_KEY_LENGTH, SECRET_KEY_LENGTH, }; use instant::Duration; -use random_access_disk::RandomAccessDisk; -use random_access_storage::RandomAccess; use std::fmt::Debug; use std::path::Path; use std::sync::Arc; @@ -388,7 +386,7 @@ async fn create_writer_hypercore( data_size: usize, data_char: char, path: &str, -) -> Result> { +) -> Result { let path = Path::new(path).to_owned(); let key_pair = get_test_key_pair(true); let storage = Storage::new_disk(&path, false).await?; @@ -403,7 +401,7 @@ async fn create_writer_hypercore( Ok(hypercore) } -async fn create_reader_hypercore(path: &str) -> Result> { +async fn create_reader_hypercore(path: &str) -> Result { let path = Path::new(path).to_owned(); let key_pair = get_test_key_pair(false); let storage = Storage::new_disk(&path, false).await?; @@ -442,14 +440,11 @@ pub fn get_test_key_pair(include_secret: bool) -> PartialKeypair { } #[cfg(feature = "async-std")] -async fn on_replication_connection( +async fn on_replication_connection( stream: TcpStream, is_initiator: bool, - hypercore: Arc>, -) -> Result<()> -where - T: RandomAccess + Debug + Send, -{ + hypercore: Arc, +) -> Result<()> { let mut protocol = ProtocolBuilder::new(is_initiator).connect(stream); while let Some(event) = protocol.next().await { let event = event?; @@ -479,14 +474,11 @@ where } #[cfg(feature = "tokio")] -async fn on_replication_connection( +async fn on_replication_connection( stream: TcpStream, is_initiator: bool, - hypercore: Arc>, -) -> Result<()> -where - T: RandomAccess + Debug + Send, -{ + hypercore: Arc, +) -> Result<()> { let mut protocol = ProtocolBuilder::new(is_initiator).connect(stream.compat()); while let Some(event) = protocol.next().await { let event = event?; @@ -516,21 +508,15 @@ where } #[derive(Debug, Clone)] -pub struct HypercoreWrapper -where - T: RandomAccess + Debug + Send, -{ +pub struct HypercoreWrapper { discovery_key: [u8; 32], key: [u8; 32], - hypercore: Arc>>, + hypercore: Arc>, result_path: Option, } -impl HypercoreWrapper { - pub fn from_disk_hypercore( - hypercore: Hypercore, - result_path: Option, - ) -> Self { +impl HypercoreWrapper { + pub fn from_disk_hypercore(hypercore: Hypercore, result_path: Option) -> Self { let key = hypercore.key_pair().public.to_bytes(); HypercoreWrapper { key, @@ -539,12 +525,7 @@ impl HypercoreWrapper { result_path, } } -} -impl HypercoreWrapper -where - T: RandomAccess + Debug + Send + 'static, -{ pub fn key(&self) -> &[u8; 32] { &self.key } @@ -609,16 +590,13 @@ where } } -async fn on_replication_message( - hypercore: &mut Arc>>, +async fn on_replication_message( + hypercore: &mut Arc>, peer_state: &mut PeerState, result_path: Option, channel: &mut Channel, message: Message, -) -> Result -where - T: RandomAccess + Debug + Send, -{ +) -> Result { match message { Message::Synchronize(message) => { let length_changed = message.length != peer_state.remote_length; @@ -857,7 +835,7 @@ impl RustServer { RustServer { handle: None } } - pub async fn run(&mut self, hypercore: Arc>, port: u32) { + pub async fn run(&mut self, hypercore: Arc, port: u32) { self.handle = Some(task::spawn(async move { tcp_server(port, on_replication_connection, hypercore) .await From e608ef02696e5e2dc8dfd2f1497269106fbb3bd3 Mon Sep 17 00:00:00 2001 From: Blake Griffith Date: Mon, 1 Jul 2024 02:14:19 -0700 Subject: [PATCH 4/4] Remove random-access-* dependencies --- Cargo.toml | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 943ca94..bcb850f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -46,9 +46,6 @@ async-std = { version = "1.12.0", features = ["attributes", "unstable"] } async-compat = "0.2.1" tokio = { version = "1.27.0", features = ["macros", "net", "process", "rt", "rt-multi-thread", "sync", "time"] } env_logger = "0.7.1" -random-access-storage = "5.0.0" -random-access-disk = { version = "3.0.0", default-features = false } -random-access-memory = "3.0.0" anyhow = "1.0.28" instant = "0.1" criterion = { version = "0.4", features = ["async_std"] } @@ -67,8 +64,8 @@ wasm-bindgen = [ ] sparse = ["hypercore/sparse"] cache = ["hypercore/cache"] -tokio = ["hypercore/tokio", "random-access-disk/tokio"] -async-std = ["hypercore/async-std", "random-access-disk/async-std"] +tokio = ["hypercore/tokio"] +async-std = ["hypercore/async-std"] # Used only in interoperability tests under tests/js-interop which use the javascript version of hypercore # to verify that this crate works. To run them, use: # cargo test --features js_interop_tests