Skip to content

Commit

Permalink
[feat] ticket proc
Browse files Browse the repository at this point in the history
  • Loading branch information
ppodolsky committed Apr 25, 2024
1 parent 724239e commit f351c58
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 51 deletions.
77 changes: 39 additions & 38 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,4 @@ tracing-subscriber = { version = "0.3", features = ["env-filter"] }
futures = "0.3"
lru = "0.12"
tokio-task-pool = "0.1"
url = "2.5.0"
24 changes: 13 additions & 11 deletions bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -320,10 +320,7 @@ async fn tables_exists(State(state): State<AppState>, Path(table): Path<String>)

async fn tables_peers(State(state): State<AppState>, Path(table): Path<String>) -> Response {
match state.iroh_node.read().await.tables_peers(&table).await {
Ok(peers) => Json(TablesPeersResponse {
peers,
})
.into_response(),
Ok(peers) => Json(TablesPeersResponse { peers }).into_response(),
Err(error) => error.into_response(),
}
}
Expand Down Expand Up @@ -416,13 +413,18 @@ async fn table_share(
Path((table, mode)): Path<(String, ShareMode)>,
table_share_request: Option<Json<TableShareRequest>>,
) -> Response {
let peers = table_share_request.map(|Json(table_share_request)| {
table_share_request
.peers
.iter()
.filter_map(|peer| PublicKey::from_str(peer).map(NodeAddr::from).ok())
.collect()
});
let peers = if let Some(table_share_request) = table_share_request {
let mut peers = vec![];
for peer in &table_share_request.peers {
match PublicKey::from_str(&peer).map(NodeAddr::new).map_err(Error::table_ticket) {
Ok(peer) => peers.push(peer),
Err(e) => return e.into_response(),
}
}
Some(peers)
} else {
None
};
match state
.iroh_node
.read()
Expand Down
2 changes: 2 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ pub struct IrohConfig {
#[serde(default = "HashMap::new")]
pub storages: HashMap<String, StorageEngineConfig>,
pub gc_interval_secs: Option<u64>,
pub relays: Option<Vec<String>>,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
Expand Down Expand Up @@ -104,6 +105,7 @@ impl Config {
},
)]),
gc_interval_secs: None,
relays: None,
},
}
}
Expand Down
29 changes: 27 additions & 2 deletions src/iroh_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ use async_stream::stream;
use futures::StreamExt;
use iroh::bytes::store::fs::Store;
use iroh::bytes::Hash;
use iroh::net::defaults::DEFAULT_RELAY_STUN_PORT;
use iroh::net::relay::{RelayMap, RelayMode, RelayNode};
use iroh::node::{GcPolicy, Node};
use iroh::rpc_protocol::ShareMode;
use iroh::sync::store::DownloadPolicy;
Expand All @@ -23,6 +25,7 @@ use tokio_stream::Stream;
use tokio_util::sync::CancellationToken;
use tokio_util::task::TaskTracker;
use tracing::info;
use url::Url;

pub struct IrohNode {
tables: HashMap<String, Table>,
Expand Down Expand Up @@ -51,9 +54,25 @@ impl IrohNode {
.await
.map_err(Error::node_create)?;

let relay_mode = match &config_lock.iroh.relays {
None => RelayMode::Default,
Some(relays) => RelayMode::Custom(
RelayMap::from_nodes(relays.iter().map(|r| {
let url: Url = r.parse().expect("default url");
RelayNode {
url: url.into(),
stun_only: false,
stun_port: DEFAULT_RELAY_STUN_PORT,
}
}))
.expect("relay config error"),
),
};

let mut node_builder = Node::persistent(&config_lock.iroh.path)
.await
.map_err(Error::node_create)?
.relay_mode(relay_mode)
.bind_port(config_lock.iroh.bind_port);

if let Some(gc_interval_secs) = config_lock.iroh.gc_interval_secs {
Expand Down Expand Up @@ -224,9 +243,15 @@ impl IrohNode {
match self.tables.entry(table_name.to_string()) {
Entry::Occupied(entry) => {
let iroh_doc = entry.get().iroh_doc();
if iroh_doc.id() != ticket.capability.id() {
return Err(Error::existing_table(table_name));
if ticket.capability.id() != iroh_doc.id() {
return Err(Error::existing_table("different document in table"));
}
self.node
.client()
.docs
.import(ticket)
.await
.map_err(Error::table)?;
iroh_doc.start_sync(nodes).await.map_err(Error::doc)?;
Ok(entry.get().iroh_doc().id())
}
Expand Down

0 comments on commit f351c58

Please sign in to comment.