radicle-node 0.21.1

The Radicle Node
Documentation
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
use std::collections::HashSet;
use std::iter;
use std::net;
use std::ops::{Deref, DerefMut};
use std::str::FromStr;

use localtime::{LocalDuration, LocalTime};
use log::*;
use protocol::bounded::BoundedVec;
use protocol::service;
use protocol::service::io::Io;
use protocol::service::message::*;
use protocol::service::*;
use protocol::wire::MessageType;
use radicle::Storage;
use radicle::crypto::{Signer as _, SigningKey};
use radicle::git::Oid;
use radicle::identity::RepoId;
use radicle::identity::Visibility;
use radicle::node;
use radicle::node::Database;
use radicle::node::Link;
use radicle::node::PROTOCOL_VERSION;
use radicle::node::UserAgent;
use radicle::node::address::Store as _;
use radicle::node::events::Emitter;
use radicle::node::events::Events;
use radicle::node::policy::config as policy;
use radicle::node::policy::{Scope, SeedingPolicy};
use radicle::node::routing::Store as _;
use radicle::node::{Address, Event, NodeId, Timestamp};
use radicle::node::{Alias, ConnectOptions, address};
use radicle::rad;
use radicle::storage::WriteStorage;
use radicle::storage::refs;
use radicle::storage::refs::{RefsAt, SignedRefs};
use radicle::storage::{ReadRepository, RemoteRepository};
use radicle::test::storage::MockStorage;
use radicle::test::{arbitrary, fixtures};

/// Service instantiation used for testing.
pub type Service<S> = service::Service<Database, S>;

pub const AMY: u8 = 0x0A;
pub const BOB: u8 = 0x0B;
pub const CID: u8 = 0x0C;
pub const DAN: u8 = 0x0D;
pub const EVE: u8 = 0x0E;

#[derive(Debug)]
pub struct Peer<S> {
    name: &'static str,
    service: Service<S>,
    addr: net::SocketAddr,
    tempdir: tempfile::TempDir,
}

impl<S> Peer<S> {
    pub fn address(&self) -> Address {
        Address::from(self.addr)
    }

    pub fn into_service(self) -> Service<S> {
        self.service
    }
}

impl<S> Deref for Peer<S> {
    type Target = Service<S>;

    fn deref(&self) -> &Self::Target {
        &self.service
    }
}

impl<S> DerefMut for Peer<S> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.service
    }
}

impl Peer<MockStorage> {
    pub fn amy() -> Self {
        Peer::new_empty_storage("amy", AMY)
    }

    pub fn amy_with(f: impl FnOnce(&mut Config)) -> Self {
        Peer::new_empty_with("amy", AMY, f)
    }

    pub fn bob() -> Self {
        Peer::new_empty_storage("bob", BOB)
    }

    pub fn bob_with(f: impl FnOnce(&mut Config)) -> Self {
        Peer::new_empty_with("bob", BOB, f)
    }

    pub fn cid() -> Self {
        Peer::new_empty_storage("cid", CID)
    }

    pub fn dan() -> Self {
        Peer::new_empty_storage("dan", DAN)
    }

    pub fn eve() -> Self {
        Peer::new_empty_storage("eve", EVE)
    }

    pub fn new_empty_storage(name: &'static str, id: u8) -> Self {
        Self::new_empty_with(name, id, |_| {})
    }

    pub(crate) fn new_empty_with(name: &'static str, id: u8, f: impl FnOnce(&mut Config)) -> Self {
        Self::new_with(name, id, MockStorage::empty(), f)
    }
}

pub struct Config {
    pub(crate) config: radicle::node::Config,
    pub(crate) local_time: LocalTime,
    pub(crate) policy: SeedingPolicy,
    pub(crate) secret_key: SigningKey,
}

impl Config {
    pub(crate) fn new(id: usize) -> Self {
        let config = radicle::node::Config::test(Alias::from_str("mocky").unwrap());

        Config {
            config,
            local_time: LocalTime::now(),
            policy: SeedingPolicy::default(),
            secret_key: SigningKey::mock(id),
        }
    }
}

impl Peer<Storage> {
    pub fn project(&mut self, name: &str, description: &str) -> RepoId {
        radicle::storage::git::transport::local::register(self.storage().clone());
        let (repo, _) = fixtures::repository(self.tempdir.path().join(name));
        let (rid, _, _) = rad::init(
            &repo,
            name.try_into().unwrap(),
            description,
            radicle::git::fmt::refname!("master"),
            Visibility::default(),
            self.secret_key(),
            self.storage(),
        )
        .unwrap();

        rid
    }
}

impl<S> Peer<S>
where
    S: WriteStorage + 'static,
{
    pub fn with_storage(name: &'static str, id: u8, storage: S) -> Self {
        Self::new_with(name, id, storage, |_| {})
    }

    pub(crate) fn new_with(
        name: &'static str,
        id: u8,
        storage: S,
        f: impl FnOnce(&mut Config),
    ) -> Self {
        let mut config = Config::new(id as usize);

        let policies = policy::Store::<policy::store::Write>::memory().unwrap();
        let mut policies = policy::Config::new(config.policy, policies);
        let ip = [198, 18, 0, id].into();

        let addr = net::SocketAddr::new(ip, 58776 + (id as u16));
        let inventory = storage.repositories().unwrap();

        // Make sure the peer address is advertised.
        config.config.external_addresses.push(addr.into());
        for repo in &inventory {
            policies.seed(&repo.rid, Scope::Followed).unwrap();
        }

        f(&mut config);

        let tempdir = tempfile::TempDir::with_prefix(name).unwrap();

        let nid = *config.secret_key.public_key();

        // Initialize database.
        let db = Database::open(
            tempdir.path().join(node::NODE_DB_FILE),
            node::db::config::Config::default(),
        )
        .unwrap()
        .init(
            &nid,
            config.config.features(),
            &config.config.alias,
            &UserAgent::default(),
            config.local_time.into(),
            config.config.external_addresses.iter(),
        )
        .unwrap()
        .into();

        let announcement =
            service::gossip::node(&config.config, Timestamp::from(config.local_time) + 1);
        let emitter: Emitter<Event> = Default::default();

        let mut service = Service::new(
            config.config,
            db,
            storage,
            policies,
            config.secret_key,
            fastrand::Rng::with_seed(id as u64),
            announcement,
            emitter,
        );

        info!(
            target: "test",
            "{}: Initializing: id = {}, address = {}",
            name, nid, addr
        );

        service.initialize(config.local_time).unwrap();

        Self {
            name,
            service,
            addr,
            tempdir,
        }
    }

    pub fn restart(&mut self) {
        info!(
            target: "test",
            "{}: Restarting: id = {}, address = {}",
            self.name, *self.nid(), self.address()
        );
        self.service.initialize(*self.service.clock()).unwrap();
    }

    pub fn import_addresses<'a>(&mut self, peers: impl IntoIterator<Item = &'a Self>) {
        let timestamp = Timestamp::from(*self.clock());
        for peer in peers.into_iter() {
            let known_address = node::KnownAddress::new(peer.address(), address::Source::Peer);
            self.service
                .database_mut()
                .addresses_mut()
                .insert(
                    peer.nid(),
                    PROTOCOL_VERSION,
                    radicle::node::Features::default(),
                    &Alias::from_str(peer.name).unwrap(),
                    0,
                    &UserAgent::default(),
                    timestamp,
                    Some(known_address),
                )
                .unwrap();
        }
    }

    pub fn inventory(&self) -> HashSet<RepoId> {
        self.service
            .database()
            .routing()
            .get_inventory(self.nid())
            .unwrap()
    }

    pub fn receive(&mut self, peer: NodeId, msg: Message) -> &mut Self {
        self.service.received_message(peer, msg);
        self
    }

    pub fn inventory_announcement(&self) -> Message {
        Message::inventory(
            InventoryAnnouncement {
                inventory: arbitrary::vec(3).try_into().unwrap(),
                timestamp: Timestamp::from(*self.clock()),
            },
            self.secret_key(),
        )
    }

    pub fn node_announcement(&self) -> Message {
        Message::node(
            NodeAnnouncement {
                version: PROTOCOL_VERSION,
                features: node::Features::SEED,
                timestamp: Timestamp::from(*self.clock()),
                alias: Alias::from_str(self.name).unwrap(),
                addresses: Some(self.address()).into(),
                nonce: 0,
                agent: UserAgent::test(),
            }
            .solve(0)
            .unwrap(),
            self.secret_key(),
        )
    }

    pub fn refs_announcement(&self, rid: RepoId) -> Message {
        let mut refs = BoundedVec::new();
        if let Ok(repo) = self.storage().repository(rid)
            && let Ok(false) = repo.is_empty()
            && let Ok(remotes) = repo.remotes()
        {
            for (remote_id, _) in remotes.into_iter() {
                match RefsAt::new(&repo, remote_id) {
                    Ok(refs_at) => {
                        if let Err(e) = refs.push(refs_at) {
                            debug!(target: "test", "Failed to push {remote_id} to refs: {e}");
                            break;
                        }
                    }
                    Err(e) => {
                        debug!(target: "test", "Failed to get `rad/sigrefs` for {remote_id}: {e}")
                    }
                }
            }
        }

        self.announcement(RefsAnnouncement {
            rid,
            refs,
            timestamp: Timestamp::from(*self.clock()),
        })
    }

    pub fn announcement(&self, ann: impl Into<AnnouncementMessage>) -> Message {
        ann.into().signed(self.secret_key()).into()
    }

    pub fn signed_refs_at(&self, root: Oid) -> SignedRefs {
        arbitrary::with_gen(8, |g| {
            refs::arbitrary::signed_refs_at(g, root, self.secret_key())
        })
    }

    pub fn connect_from(&mut self, peer: &Self) {
        let remote_id = *peer.nid();

        self.service
            .connected(remote_id, peer.address(), Link::Inbound);
        self.service
            .received_message(remote_id, peer.node_announcement());

        let mut msgs = self.messages(remote_id);
        msgs.find(|m| {
            matches!(
                m,
                Message::Announcement(Announcement {
                    message: AnnouncementMessage::Inventory(_),
                    ..
                })
            )
        })
        .expect("`inventory-announcement` must be sent");
    }

    pub fn connect_to<T: WriteStorage + 'static>(&mut self, peer: &Peer<T>) {
        let remote_id = *peer.nid();
        let remote_addr = peer.address();

        self.service.command(Command::Connect(
            remote_id,
            remote_addr.clone(),
            ConnectOptions::default(),
        ));

        self.outbox()
            .find(|o| matches!(o, Io::Connect { .. }))
            .unwrap();

        self.service.attempted(remote_id, remote_addr.clone());
        self.service
            .connected(remote_id, remote_addr, Link::Outbound);
        self.service
            .received_message(remote_id, peer.node_announcement());

        let mut msgs = self.messages(remote_id);
        msgs.find(|m| {
            matches!(
                m,
                Message::Announcement(Announcement {
                    message: AnnouncementMessage::Inventory(_),
                    ..
                })
            )
        })
        .expect("`inventory-announcement` must be sent");
    }

    pub fn elapse(&mut self, duration: LocalDuration) {
        self.clock_mut().elapse(duration);
        self.service.wake();
    }

    /// Drain outgoing messages sent from this peer to the remote peer.
    pub fn messages(&mut self, remote: NodeId) -> impl Iterator<Item = Message> + use<S> {
        let mut msgs = Vec::new();

        Service::outbox(&mut self.service)
            .queue()
            .retain(|o| match o {
                Io::Write(a, messages) if *a == remote => {
                    msgs.extend(messages.clone());
                    false
                }
                _ => true,
            });

        msgs.into_iter()
    }

    /// Drain outgoing *relayed* announcements to the remote peer. This doesn't include messages
    /// originating from our own node.
    pub fn relayed(&mut self, remote: NodeId) -> impl Iterator<Item = Message> {
        let mut filtered: Vec<Message> = Vec::new();
        let nid = *self.nid();

        for o in Service::outbox(&mut self.service).queue() {
            match o {
                Io::Write(a, messages) if *a == remote => {
                    let (relayed, other): (Vec<Message>, _) =
                        messages.iter().cloned().partition(|m| {
                            matches!(
                                m,
                                Message::Announcement(Announcement { node, .. })
                                if *node != nid
                            )
                        });
                    *messages = other;
                    filtered.extend(relayed);
                }
                _ => {}
            }
        }

        filtered.into_iter()
    }

    /// Drain outgoing inventories sent from this peer to the remote peer.
    pub fn inventory_announcements(&mut self, remote: NodeId) -> impl Iterator<Item = Message> {
        let mut invs: Vec<Message> = Vec::new();

        for o in Service::outbox(&mut self.service).queue() {
            match o {
                Io::Write(a, messages) if *a == remote => {
                    let (inventories, other): (Vec<Message>, _) =
                        messages.iter().cloned().partition(|m| {
                            MessageType::try_from(m.type_id())
                                == Ok(MessageType::InventoryAnnouncement)
                        });
                    *messages = other;
                    invs.extend(inventories);
                }
                _ => {}
            }
        }

        invs.into_iter()
    }

    /// Get a stream of the peer's emitted events.
    pub fn events(&mut self) -> Events {
        self.service.events()
    }

    /// Get a draining iterator over the peer's I/O outbox.
    pub fn outbox(&mut self) -> impl Iterator<Item = Io> + '_ {
        iter::from_fn(|| Service::outbox(&mut self.service).next())
    }

    /// Get a draining iterator over the peer's I/O outbox, which only returns fetches.
    pub fn fetches(&mut self) -> impl Iterator<Item = (RepoId, NodeId)> + '_ {
        iter::from_fn(|| Service::outbox(&mut self.service).next()).filter_map(|io| {
            if let Io::Fetch { rid, remote, .. } = io {
                Some((rid, remote))
            } else {
                None
            }
        })
    }
}