Skip to content

Commit

Permalink
sdk: update reconcilie method
Browse files Browse the repository at this point in the history
  • Loading branch information
yukibtc committed Sep 19, 2023
1 parent 691df17 commit e686558
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
8 changes: 3 additions & 5 deletions crates/nostr-sdk/examples/reconcilie.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,15 @@ async fn main() -> Result<()> {
let my_keys = Keys::new(secret_key);

let client = Client::new(&my_keys);
client.add_relay("wss://relay.damus.io", None).await?;
client.add_relay("wss://atl.purplerelay.com", None).await?;

client.connect().await;

let filter = Filter::new()
.author(my_keys.public_key().to_string())
.limit(10);
let relay = client.relay("wss://relay.damus.io").await?;
relay
.reconcilie(filter, vec![(EventId::all_zeros(), Timestamp::now())])
.await?;
let relay = client.relay("wss://atl.purplerelay.com").await?;
relay.reconcilie(filter, Vec::new()).await?;

Ok(())
}
24 changes: 17 additions & 7 deletions crates/nostr-sdk/src/relay/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1577,8 +1577,8 @@ impl Relay {

negentropy.seal()?;

let id = SubscriptionId::generate();
let open_msg = ClientMessage::neg_open(&mut negentropy, &id, filter)?;
let sub_id = SubscriptionId::generate();
let open_msg = ClientMessage::neg_open(&mut negentropy, &sub_id, filter)?;

self.send_msg(open_msg, Some(Duration::from_secs(10)))
.await?;
Expand All @@ -1592,7 +1592,7 @@ impl Relay {
subscription_id,
message,
} => {
if subscription_id == id {
if subscription_id == sub_id {
let query: Bytes = Bytes::from_hex(message)?;
let mut need_ids: Vec<Bytes> = Vec::new();
let msg: Option<Bytes> = negentropy.reconcile_with_ids(
Expand All @@ -1601,14 +1601,24 @@ impl Relay {
&mut need_ids,
)?;

// TODO: request ids to relay
println!("IDs: {need_ids:?}");
let ids: Vec<String> =
need_ids.into_iter().map(|id| id.to_hex()).collect();
let filter = Filter::new().ids(ids);
self.req_events_of(
vec![filter],
Duration::from_secs(120),
FilterOptions::ExitOnEOSE,
);

match msg {
Some(query) => {
tracing::info!(
"Continue with reconciliation with {}",
self.url
);
self.send_msg(
ClientMessage::NegMsg {
subscription_id: id.clone(),
subscription_id: sub_id.clone(),
message: query.to_hex(),
},
None,
Expand All @@ -1626,7 +1636,7 @@ impl Relay {
subscription_id,
code,
} => {
if subscription_id == id {
if subscription_id == sub_id {
tracing::error!("Negentropy syncing error: {code}");
break;
}
Expand Down

0 comments on commit e686558

Please sign in to comment.