From ac96838c50440b30e4ab399c27448895527beed4 Mon Sep 17 00:00:00 2001 From: Robert Elves Date: Mon, 25 Aug 2025 16:02:07 -0700 Subject: [PATCH 1/3] chore: update iroh-automerge-repo to samod 0.3.0 --- iroh-automerge-repo/Cargo.lock | 9 ++++----- iroh-automerge-repo/Cargo.toml | 2 +- iroh-automerge-repo/src/lib.rs | 14 +++++++------- iroh-automerge-repo/src/main.rs | 6 +++--- 4 files changed, 15 insertions(+), 16 deletions(-) diff --git a/iroh-automerge-repo/Cargo.lock b/iroh-automerge-repo/Cargo.lock index d1b2dfd..ca87996 100644 --- a/iroh-automerge-repo/Cargo.lock +++ b/iroh-automerge-repo/Cargo.lock @@ -3086,9 +3086,9 @@ dependencies = [ [[package]] name = "samod" -version = "0.2.1" +version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ecd2e176d6f533e87d4967befa3f34da76aae233a0db77a5bf260bc21443b9ce" +checksum = "daba364daaf5347a0be2e6d34553c818e88a01cb6a14e9fd1c4b1a73632f0db6" dependencies = [ "automerge", "bytes", @@ -3104,14 +3104,13 @@ dependencies = [ [[package]] name = "samod-core" -version = "0.2.0" +version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d03a96b4b6b3a61c5dda07b7fd31c9d367182e8b16cc62b9b2f2267eb7e257a1" +checksum = "84f1292c4f196552753b3be6ccdbcaee772f8cbdbb7397db9a7233dc2f9bda3b" dependencies = [ "automerge", "base64 0.21.7", "bs58", - "futures", "hex", "minicbor", "rand 0.9.1", diff --git a/iroh-automerge-repo/Cargo.toml b/iroh-automerge-repo/Cargo.toml index eddaf26..40dd807 100644 --- a/iroh-automerge-repo/Cargo.toml +++ b/iroh-automerge-repo/Cargo.toml @@ -9,7 +9,7 @@ bytes = "1.10.1" derive_more = { version = "2.0.1", features = ["debug"] } iroh = "0.91.1" n0-future = "0.2" -samod = { version = "0.2.1", features = ["tokio"] } +samod = { version = "0.3.0", features = ["tokio"] } tokio = { version = "1.47.1", features = ["sync", "net"] } tokio-util = { version = "0.7.15", features = ["codec"] } tracing = "0.1.41" diff --git a/iroh-automerge-repo/src/lib.rs b/iroh-automerge-repo/src/lib.rs index f3e1479..f182363 100644 --- a/iroh-automerge-repo/src/lib.rs +++ b/iroh-automerge-repo/src/lib.rs @@ -2,32 +2,32 @@ //! in rust that speak the automerge repo protocol. use anyhow::Result; use codec::Codec; -use samod::{ConnDirection, ConnFinishedReason, Samod}; +use samod::{ConnDirection, ConnFinishedReason, Repo}; use tokio_util::codec::{FramedRead, FramedWrite}; mod codec; -/// Combines an [`iroh::Endpoint`] with a [`Samod`] (automerge repository) and +/// Combines an [`iroh::Endpoint`] with a [`Repo`] (automerge repository) and /// implements [`iroh::protocol::ProtocolHandler`] to accept incoming connections /// in an [`iroh::protocol::Router`]. #[derive(derive_more::Debug, Clone)] pub struct IrohRepo { endpoint: iroh::Endpoint, #[debug(skip)] - repo: Samod, + repo: Repo, } impl IrohRepo { pub const SYNC_ALPN: &[u8] = b"iroh/automerge-repo/1"; /// Constructs a new [`IrohRepo`]. - pub fn new(endpoint: iroh::Endpoint, repo: Samod) -> Self { + pub fn new(endpoint: iroh::Endpoint, repo: Repo) -> Self { IrohRepo { endpoint, repo } } /// Attempts to continuously sync with a peer at given address. /// - /// To wait for the connection to be established use [`Samod::when_connected`] + /// To wait for the connection to be established use [`Repo::when_connected`] /// (accessible via [`Self::repo`]: `iroh_repo.repo().when_connected(..)`). /// with the other node's string-encoded [`NodeId`] as the [`PeerId`]. /// @@ -56,8 +56,8 @@ impl IrohRepo { Ok(conn_finished) } - /// Returns a reference to the stored [`Samod`] instance inside. - pub fn repo(&self) -> &Samod { + /// Returns a reference to the stored [`Repo`] instance inside. + pub fn repo(&self) -> &Repo { &self.repo } } diff --git a/iroh-automerge-repo/src/main.rs b/iroh-automerge-repo/src/main.rs index 4228738..2209373 100644 --- a/iroh-automerge-repo/src/main.rs +++ b/iroh-automerge-repo/src/main.rs @@ -7,7 +7,7 @@ use hex::encode; use iroh::NodeId; use iroh_automerge_repo::IrohRepo; -use samod::{DocumentId, PeerId, Samod, storage::TokioFilesystemStorage}; +use samod::{storage::TokioFilesystemStorage, DocumentId, PeerId, Repo}; #[derive(Parser, Debug)] #[command(author, version, about, long_about = None)] @@ -122,8 +122,8 @@ async fn main() -> anyhow::Result<()> { .await?; println!("Node ID: {}", endpoint.node_id()); - - let samod = Samod::build_tokio() + + let samod = Repo::build_tokio() .with_peer_id(PeerId::from_string(endpoint.node_id().to_string())) .with_storage(TokioFilesystemStorage::new(format!( "{}/{}", From d2e9bb7ffe9aa51725b0c21342d27a1ab3813cb8 Mon Sep 17 00:00:00 2001 From: Robert Elves Date: Mon, 25 Aug 2025 17:02:13 -0700 Subject: [PATCH 2/3] fix: iroh-automerge-repo uses doc change events vs polling --- iroh-automerge-repo/src/main.rs | 20 ++++---------------- 1 file changed, 4 insertions(+), 16 deletions(-) diff --git a/iroh-automerge-repo/src/main.rs b/iroh-automerge-repo/src/main.rs index 2209373..f7b6f51 100644 --- a/iroh-automerge-repo/src/main.rs +++ b/iroh-automerge-repo/src/main.rs @@ -217,23 +217,11 @@ async fn main() -> anyhow::Result<()> { anyhow::Ok(()) })?; - // Set up polling for changes (no push available yet) + // Listen for change events and print latest document contents tokio::spawn(async move { - // Track the last known heads to detect changes - let mut last_heads = doc.with_document(|doc| doc.get_heads()); - loop { - tokio::time::sleep(tokio::time::Duration::from_millis(1000)).await; - - let current_heads = doc.with_document(|doc| doc.get_heads()); - if current_heads == last_heads { - continue; - } - - last_heads = current_heads; - - println!("Document changed! New state:"); - - // When changes are detected, print the updated document contents... + use n0_future::StreamExt; + let mut changes = doc.changes(); + while let Some(_change) = changes.next().await { if let Err(e) = doc.with_document(|current_doc| { for key in current_doc.keys(automerge::ROOT) { let (value, _) = current_doc From 5a54016b87bcb373742a3f572d1b298123bb0f72 Mon Sep 17 00:00:00 2001 From: Robert Elves Date: Mon, 25 Aug 2025 17:24:34 -0700 Subject: [PATCH 3/3] fix formatting --- iroh-automerge-repo/src/main.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/iroh-automerge-repo/src/main.rs b/iroh-automerge-repo/src/main.rs index f7b6f51..b1d6a54 100644 --- a/iroh-automerge-repo/src/main.rs +++ b/iroh-automerge-repo/src/main.rs @@ -7,7 +7,7 @@ use hex::encode; use iroh::NodeId; use iroh_automerge_repo::IrohRepo; -use samod::{storage::TokioFilesystemStorage, DocumentId, PeerId, Repo}; +use samod::{DocumentId, PeerId, Repo, storage::TokioFilesystemStorage}; #[derive(Parser, Debug)] #[command(author, version, about, long_about = None)] @@ -122,7 +122,7 @@ async fn main() -> anyhow::Result<()> { .await?; println!("Node ID: {}", endpoint.node_id()); - + let samod = Repo::build_tokio() .with_peer_id(PeerId::from_string(endpoint.node_id().to_string())) .with_storage(TokioFilesystemStorage::new(format!( @@ -220,7 +220,7 @@ async fn main() -> anyhow::Result<()> { // Listen for change events and print latest document contents tokio::spawn(async move { use n0_future::StreamExt; - let mut changes = doc.changes(); + let mut changes = doc.changes(); while let Some(_change) = changes.next().await { if let Err(e) = doc.with_document(|current_doc| { for key in current_doc.keys(automerge::ROOT) {