1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
|
//! Discovery Sync Tests
//!
//! Tests for relay discovery from announcement events.
//! When a relay receives an announcement listing another relay,
//! it should discover and connect to that relay to sync events.
//!
//! # Tests
//! - Test 2: Direct Layer 3 discovery from Layer 2
//! - Test 3: Recursive multi-hop Layer 3 discovery
use std::time::Duration;
use nostr_sdk::prelude::*;
use crate::common::{sync_helpers::*, TestRelay};
/// Kind 1617 - Patch event (NIP-34)
const KIND_PATCH: u16 = 1617;
/// Create a valid repository announcement event for testing sync.
///
/// This creates a kind 30617 event with required clone and relays tags.
fn create_repo_announcement(keys: &Keys, domains: &[&str], identifier: &str) -> Event {
let clone_urls: Vec<String> = domains
.iter()
.map(|d| format!("http://{}/{}.git", d, identifier))
.collect();
let relay_urls: Vec<String> = domains.iter().map(|d| format!("ws://{}", d)).collect();
let tags = vec![
Tag::identifier(identifier),
Tag::custom(TagKind::custom("clone"), clone_urls),
Tag::custom(TagKind::custom("relays"), relay_urls),
];
EventBuilder::new(Kind::Custom(KIND_REPOSITORY_STATE), "Repository state")
.tags(tags)
.sign_with_keys(keys)
.expect("Failed to sign repo announcement")
}
/// Create an event referencing a repository coordinate via 'a' tag.
///
/// Used to create Layer 2 events like patches that reference a repository.
fn create_event_referencing_repo(keys: &Keys, repo_coord: &str, kind: u16, content: &str) -> Event {
let tags = vec![Tag::custom(
TagKind::custom("a"),
vec![repo_coord.to_string()],
)];
EventBuilder::new(Kind::Custom(kind), content)
.tags(tags)
.sign_with_keys(keys)
.expect("Failed to sign event")
}
/// Test 2: Relay discovers another relay via announcement and syncs Layer 2 events
///
/// Scenario:
/// 1. relay_a has announcement + patch event (Layer 2)
/// 2. relay_b (sync enabled, NO bootstrap) receives the announcement directly
/// 3. relay_b discovers relay_a from the announcement's relays tag
/// 4. relay_b connects to relay_a and syncs the patch event
///
/// This tests dynamic relay discovery from direct submissions.
#[tokio::test]
async fn test_discovers_layer3_via_layer2() {
// 1. Start relay_a (source) with the patch event
let relay_a = TestRelay::start().await;
println!(
"relay_a started at {} (domain: {})",
relay_a.url(),
relay_a.domain()
);
// 2. Start relay_b: sync enabled but NO bootstrap relay - will discover relay_a
let relay_b = TestRelay::start_with_sync(None).await;
println!(
"relay_b started at {} (domain: {})",
relay_b.url(),
relay_b.domain()
);
// 3. Create test keys
let keys = Keys::generate();
// 4. Create a repository announcement that lists BOTH relays
let announcement = create_repo_announcement(
&keys,
&[&relay_a.domain(), &relay_b.domain()],
"test-repo-discovery",
);
let announcement_id = announcement.id;
println!(
"Created announcement {} (kind {})",
announcement_id,
announcement.kind.as_u16()
);
for tag in announcement.tags.iter() {
println!(" Tag: {:?}", tag.as_slice());
}
// 5. Build the repo coordinate for the 'a' tag in the patch
let repo_coord = format!(
"{}:{}:{}",
KIND_REPOSITORY_STATE,
keys.public_key().to_hex(),
"test-repo-discovery"
);
// 6. Create a patch event (Layer 2) that references the announcement
let patch = create_event_referencing_repo(&keys, &repo_coord, KIND_PATCH, "Test patch proposal");
let patch_id = patch.id;
println!("Created patch {} (kind {})", patch_id, patch.kind.as_u16());
for tag in patch.tags.iter() {
println!(" Tag: {:?}", tag.as_slice());
}
// 7. Send announcement and patch to relay_a ONLY
let client_a = TestClient::new(relay_a.url(), keys.clone())
.await
.expect("Failed to connect to relay_a");
client_a
.send_event(&announcement)
.await
.expect("Failed to send announcement to relay_a");
println!("Announcement sent to relay_a");
client_a
.send_event(&patch)
.await
.expect("Failed to send patch to relay_a");
println!("Patch sent to relay_a");
client_a.disconnect().await;
// 8. Send announcement to relay_b directly (triggers discovery of relay_a)
let client_b = TestClient::new(relay_b.url(), keys.clone())
.await
.expect("Failed to connect to relay_b");
client_b
.send_event(&announcement)
.await
.expect("Failed to send announcement to relay_b");
println!("Announcement sent to relay_b (should trigger discovery of relay_a)");
client_b.disconnect().await;
// 9. Wait for relay_b to discover relay_a and sync the patch
println!("Waiting 3s for relay_b to discover relay_a and sync patch...");
tokio::time::sleep(Duration::from_secs(3)).await;
// 10. Verify patch was synced to relay_b
let filter = Filter::new()
.kind(Kind::Custom(KIND_PATCH))
.author(keys.public_key());
let patch_synced = wait_for_event_on_relay(relay_b.url(), filter, Duration::from_secs(5)).await;
if patch_synced {
println!(
"Patch {} found on relay_b (synced from discovered relay_a)",
patch_id
);
} else {
println!("Patch {} NOT found on relay_b", patch_id);
}
// 11. Cleanup
relay_b.stop().await;
relay_a.stop().await;
assert!(
patch_synced,
"Patch {} should have been synced to relay_b from discovered relay_a",
patch_id
);
}
/// Test 3: Layer 2 discovery with full event chain
///
/// Scenario:
/// 1. relay_a has: announcement → issue (Layer 2)
/// 2. relay_b receives announcement directly
/// 3. relay_b discovers relay_a and syncs the issue (Layer 2)
///
/// This tests that Layer 2 events (issues/patches) are synced when their
/// parent repository is discovered. The chain is:
/// Layer 1 (30617): Repository announcement
/// Layer 2 (1618): Issue referencing repo
///
/// Note: Layer 3 (comments on issues) sync is tracked separately and may
/// be implemented in future phases. This test focuses on Layer 2 discovery.
#[tokio::test]
async fn test_layer2_discovery_with_chain() {
// 1. Start relay_a (source) with the event chain
let relay_a = TestRelay::start().await;
println!(
"relay_a started at {} (domain: {})",
relay_a.url(),
relay_a.domain()
);
// 2. Start relay_b: sync enabled but NO bootstrap relay
let relay_b = TestRelay::start_with_sync(None).await;
println!(
"relay_b started at {} (domain: {})",
relay_b.url(),
relay_b.domain()
);
// 3. Create test keys
let keys = Keys::generate();
// 4. Create the event chain on relay_a:
// Layer 1: Repository announcement
let announcement = create_repo_announcement(
&keys,
&[&relay_a.domain(), &relay_b.domain()],
"test-repo-chain",
);
let announcement_id = announcement.id;
println!("Created announcement {} (Layer 1)", announcement_id);
// Build repo coordinate for Layer 2 reference
let repo_coord = repo_coord(&keys, "test-repo-chain");
// Layer 2: Issue referencing the repo
let issue = build_layer2_issue_event(&keys, &repo_coord, "Test issue for chain discovery")
.expect("Failed to create issue");
let issue_id = issue.id;
println!("Created issue {} (Layer 2)", issue_id);
// 5. Send all events to relay_a
let client_a = TestClient::new(relay_a.url(), keys.clone())
.await
.expect("Failed to connect to relay_a");
client_a
.send_event(&announcement)
.await
.expect("Failed to send announcement");
client_a
.send_event(&issue)
.await
.expect("Failed to send issue");
println!("Events sent to relay_a");
client_a.disconnect().await;
// 6. Send only the announcement to relay_b (triggers discovery)
let client_b = TestClient::new(relay_b.url(), keys.clone())
.await
.expect("Failed to connect to relay_b");
client_b
.send_event(&announcement)
.await
.expect("Failed to send announcement to relay_b");
println!("Announcement sent to relay_b (should trigger discovery)");
client_b.disconnect().await;
// 7. Wait for sync
println!("Waiting 3s for Layer 2 sync...");
tokio::time::sleep(Duration::from_secs(3)).await;
// 8. Verify Layer 2 event synced to relay_b
let issue_filter = Filter::new()
.kind(Kind::Custom(KIND_ISSUE))
.author(keys.public_key());
let issue_synced = wait_for_event_on_relay(relay_b.url(), issue_filter, Duration::from_secs(5)).await;
println!("Sync result:");
println!(" Issue {} synced: {}", issue_id, issue_synced);
// 9. Cleanup
relay_b.stop().await;
relay_a.stop().await;
// 10. Assert Layer 2 event synced
assert!(
issue_synced,
"Issue {} (Layer 2) should have synced to relay_b via discovery",
issue_id
);
}
|