casper-node 2.0.3

The Casper blockchain 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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
//! Tests for the `network` component.
//!
//! Calling these "unit tests" would be a bit of a misnomer, since they deal mostly with multiple
//! instances of `net` arranged in a network.

use std::{
    collections::{HashMap, HashSet},
    fmt::{self, Debug, Display, Formatter},
    sync::Arc,
    time::{Duration, Instant},
};

use derive_more::From;
use futures::FutureExt;
use prometheus::Registry;
use reactor::ReactorEvent;
use serde::{Deserialize, Serialize};
use smallvec::smallvec;
use tracing::{debug, info};

use casper_types::{Chainspec, ChainspecRawBytes, SecretKey};

use super::{
    chain_info::ChainInfo, Event as NetworkEvent, FromIncoming, GossipedAddress, Identity,
    MessageKind, Network, Payload,
};
use crate::{
    components::{
        gossiper::{self, GossipItem, Gossiper},
        network, Component, InitializedComponent,
    },
    effect::{
        announcements::{ControlAnnouncement, GossiperAnnouncement, PeerBehaviorAnnouncement},
        incoming::GossiperIncoming,
        requests::{
            BeginGossipRequest, ChainspecRawBytesRequest, ContractRuntimeRequest, NetworkRequest,
            StorageRequest,
        },
        EffectBuilder, Effects,
    },
    protocol,
    reactor::{self, main_reactor::Config, EventQueueHandle, Finalize, Reactor, Runner},
    testing::{
        self, init_logging,
        network::{NetworkedReactor, Nodes, TestingNetwork},
        ConditionCheckReactor,
    },
    types::{NodeId, SyncHandling, ValidatorMatrix},
    NodeRng,
};

/// Test-reactor event.
#[derive(Debug, From, Serialize)]
enum Event {
    #[from]
    Net(#[serde(skip_serializing)] NetworkEvent<Message>),
    #[from]
    AddressGossiper(#[serde(skip_serializing)] gossiper::Event<GossipedAddress>),
    #[from]
    NetworkRequest(#[serde(skip_serializing)] NetworkRequest<Message>),
    #[from]
    ControlAnnouncement(ControlAnnouncement),
    #[from]
    AddressGossiperAnnouncement(#[serde(skip_serializing)] GossiperAnnouncement<GossipedAddress>),
    #[from]
    BeginAddressGossipRequest(BeginGossipRequest<GossipedAddress>),
    /// An incoming network message with an address gossiper protocol message.
    AddressGossiperIncoming(GossiperIncoming<GossipedAddress>),
    #[from]
    BlocklistAnnouncement(PeerBehaviorAnnouncement),
}

impl ReactorEvent for Event {
    fn is_control(&self) -> bool {
        matches!(self, Event::ControlAnnouncement(_))
    }

    fn try_into_control(self) -> Option<ControlAnnouncement> {
        if let Self::ControlAnnouncement(ctrl_ann) = self {
            Some(ctrl_ann)
        } else {
            None
        }
    }
}

impl From<NetworkRequest<gossiper::Message<GossipedAddress>>> for Event {
    fn from(request: NetworkRequest<gossiper::Message<GossipedAddress>>) -> Self {
        Event::NetworkRequest(request.map_payload(Message::from))
    }
}

impl From<NetworkRequest<Message>> for NetworkEvent<Message> {
    fn from(request: NetworkRequest<Message>) -> NetworkEvent<Message> {
        NetworkEvent::NetworkRequest {
            req: Box::new(request),
        }
    }
}

impl From<NetworkRequest<protocol::Message>> for Event {
    fn from(_request: NetworkRequest<protocol::Message>) -> Self {
        unreachable!()
    }
}

impl From<StorageRequest> for Event {
    fn from(_request: StorageRequest) -> Self {
        unreachable!()
    }
}

impl From<ChainspecRawBytesRequest> for Event {
    fn from(_request: ChainspecRawBytesRequest) -> Self {
        unreachable!()
    }
}

impl From<ContractRuntimeRequest> for Event {
    fn from(_request: ContractRuntimeRequest) -> Self {
        unreachable!()
    }
}

impl FromIncoming<Message> for Event {
    fn from_incoming(sender: NodeId, payload: Message) -> Self {
        match payload {
            Message::AddressGossiper(message) => Event::AddressGossiperIncoming(GossiperIncoming {
                sender,
                message: Box::new(message),
            }),
        }
    }
}

impl Display for Event {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        Debug::fmt(self, f)
    }
}

#[derive(Clone, Debug, Deserialize, Serialize, From)]
enum Message {
    #[from]
    AddressGossiper(gossiper::Message<GossipedAddress>),
}

impl Display for Message {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        Debug::fmt(self, f)
    }
}

impl Payload for Message {
    #[inline]
    fn message_kind(&self) -> MessageKind {
        match self {
            Message::AddressGossiper(_) => MessageKind::AddressGossip,
        }
    }

    fn incoming_resource_estimate(&self, _weights: &super::EstimatorWeights) -> u32 {
        0
    }

    fn is_unsafe_for_syncing_peers(&self) -> bool {
        false
    }
}

/// Test reactor.
///
/// Runs a single network.
#[derive(Debug)]
struct TestReactor {
    net: Network<Event, Message>,
    address_gossiper: Gossiper<{ GossipedAddress::ID_IS_COMPLETE_ITEM }, GossipedAddress>,
}

impl Reactor for TestReactor {
    type Event = Event;
    type Config = Config;
    type Error = anyhow::Error;

    fn dispatch_event(
        &mut self,
        effect_builder: EffectBuilder<Self::Event>,
        rng: &mut NodeRng,
        event: Self::Event,
    ) -> Effects<Self::Event> {
        match event {
            Event::Net(ev) => {
                reactor::wrap_effects(Event::Net, self.net.handle_event(effect_builder, rng, ev))
            }
            Event::AddressGossiper(event) => reactor::wrap_effects(
                Event::AddressGossiper,
                self.address_gossiper
                    .handle_event(effect_builder, rng, event),
            ),
            Event::NetworkRequest(req) => reactor::wrap_effects(
                Event::Net,
                self.net.handle_event(effect_builder, rng, req.into()),
            ),
            Event::ControlAnnouncement(ctrl_ann) => {
                unreachable!("unhandled control announcement: {}", ctrl_ann)
            }
            Event::AddressGossiperAnnouncement(GossiperAnnouncement::NewCompleteItem(
                gossiped_address,
            )) => reactor::wrap_effects(
                Event::Net,
                self.net.handle_event(
                    effect_builder,
                    rng,
                    NetworkEvent::PeerAddressReceived(gossiped_address),
                ),
            ),

            Event::AddressGossiperAnnouncement(GossiperAnnouncement::GossipReceived { .. }) => {
                // We do not care about the announcement of a new gossiped item in this test.
                Effects::new()
            }
            Event::AddressGossiperAnnouncement(GossiperAnnouncement::FinishedGossiping(_)) => {
                // We do not care about the announcement of gossiping finished in this test.
                Effects::new()
            }
            Event::AddressGossiperAnnouncement(GossiperAnnouncement::NewItemBody { .. }) => {
                // Addresses shouldn't have an item body when gossiped.
                Effects::new()
            }
            Event::BeginAddressGossipRequest(ev) => reactor::wrap_effects(
                Event::AddressGossiper,
                self.address_gossiper
                    .handle_event(effect_builder, rng, ev.into()),
            ),
            Event::AddressGossiperIncoming(incoming) => reactor::wrap_effects(
                Event::AddressGossiper,
                self.address_gossiper
                    .handle_event(effect_builder, rng, incoming.into()),
            ),
            Event::BlocklistAnnouncement(_announcement) => Effects::new(),
        }
    }

    fn new(
        cfg: Self::Config,
        _chainspec: Arc<Chainspec>,
        _chainspec_raw_bytes: Arc<ChainspecRawBytes>,
        our_identity: Identity,
        registry: &Registry,
        _event_queue: EventQueueHandle<Self::Event>,
        rng: &mut NodeRng,
    ) -> anyhow::Result<(Self, Effects<Self::Event>)> {
        let secret_key = SecretKey::random(rng);
        let allow_handshake = cfg.node.sync_handling != SyncHandling::Isolated;
        let mut net = Network::new(
            cfg.network.clone(),
            our_identity,
            None,
            registry,
            ChainInfo::create_for_testing(),
            ValidatorMatrix::new_with_validator(Arc::new(secret_key)),
            allow_handshake,
        )?;
        let gossiper_config = gossiper::Config::new_with_small_timeouts();
        let address_gossiper = Gossiper::<{ GossipedAddress::ID_IS_COMPLETE_ITEM }, _>::new(
            "address_gossiper",
            gossiper_config,
            registry,
        )?;

        net.start_initialization();
        let effects = smallvec![async { smallvec![Event::Net(NetworkEvent::Initialize)] }.boxed()];

        Ok((
            TestReactor {
                net,
                address_gossiper,
            },
            effects,
        ))
    }
}

impl NetworkedReactor for TestReactor {
    fn node_id(&self) -> NodeId {
        self.net.node_id()
    }
}

impl Finalize for TestReactor {
    fn finalize(self) -> futures::future::BoxFuture<'static, ()> {
        self.net.finalize()
    }
}

/// Checks whether or not a given network with potentially blocked nodes is completely connected.
fn network_is_complete(
    blocklist: &HashSet<NodeId>,
    nodes: &HashMap<NodeId, Runner<ConditionCheckReactor<TestReactor>>>,
) -> bool {
    // Collect expected nodes.
    let expected: HashSet<_> = nodes
        .keys()
        .filter(|&node_id| !blocklist.contains(node_id))
        .copied()
        .collect();

    for (node_id, node) in nodes {
        let net = &node.reactor().inner().net;
        // TODO: Ensure the connections are symmetrical.
        let peers: HashSet<_> = net.peers().into_keys().collect();

        let mut missing = expected.difference(&peers);

        if let Some(first_missing) = missing.next() {
            // We only allow loopbacks to be missing.
            if first_missing != node_id {
                return false;
            }
        }

        if missing.next().is_some() {
            // We have at least two missing, which cannot be.
            return false;
        }
    }
    true
}

/// Checks whether or not a given network has at least one other node in it
fn network_started(net: &TestingNetwork<TestReactor>) -> bool {
    net.nodes()
        .iter()
        .map(|(_, runner)| runner.reactor().inner().net.peers())
        .all(|peers| !peers.is_empty())
}

/// Run a two-node network five times.
///
/// Ensures that network cleanup and basic networking works.
#[tokio::test]
async fn run_two_node_network_five_times() {
    let mut rng = crate::new_rng();

    // The networking port used by the tests for the root node.
    let first_node_port = testing::unused_port_on_localhost() + 1;

    init_logging();

    for i in 0..5 {
        info!("two-network test round {}", i);

        let mut net = TestingNetwork::new();

        let start = Instant::now();

        let cfg = Config::default().with_network_config(
            network::Config::default_local_net_first_node(first_node_port),
        );
        net.add_node_with_config(cfg, &mut rng).await.unwrap();

        let cfg = Config::default()
            .with_network_config(network::Config::default_local_net(first_node_port));
        net.add_node_with_config(cfg.clone(), &mut rng)
            .await
            .unwrap();
        let end = Instant::now();

        debug!(
            total_time_ms = (end - start).as_millis() as u64,
            "finished setting up networking nodes"
        );

        let timeout = Duration::from_secs(20);
        let blocklist = HashSet::new();
        net.settle_on(
            &mut rng,
            |nodes| network_is_complete(&blocklist, nodes),
            timeout,
        )
        .await;

        assert!(
            network_started(&net),
            "each node is connected to at least one other node"
        );

        let quiet_for = Duration::from_millis(25);
        let timeout = Duration::from_secs(2);
        net.settle(&mut rng, quiet_for, timeout).await;

        assert!(
            network_is_complete(&blocklist, net.nodes()),
            "network did not stay connected"
        );

        net.finalize().await;
    }
}

/// Sanity check that we can bind to a real network.
///
/// Very unlikely to ever fail on a real machine.
#[cfg(not(target_os = "macos"))]
#[tokio::test]
async fn bind_to_real_network_interface() {
    init_logging();

    let mut rng = crate::new_rng();

    let iface = pnet::datalink::interfaces()
        .into_iter()
        .find(|net| !net.ips.is_empty() && !net.ips.iter().any(|ip| ip.ip().is_loopback()))
        .expect("could not find a single networking interface that isn't localhost");

    let local_addr = iface
        .ips
        .into_iter()
        .next()
        .expect("found a interface with no ips")
        .ip();
    let port = testing::unused_port_on_localhost();

    let cfg =
        Config::default().with_network_config(network::Config::new((local_addr, port).into()));

    let mut net = TestingNetwork::<TestReactor>::new();
    net.add_node_with_config(cfg, &mut rng).await.unwrap();

    // The network should be fully connected.
    let timeout = Duration::from_secs(2);
    let blocklist = HashSet::new();
    net.settle_on(
        &mut rng,
        |nodes| network_is_complete(&blocklist, nodes),
        timeout,
    )
    .await;

    net.finalize().await;
}

/// Check that a network of varying sizes will connect all nodes properly.
#[tokio::test]
async fn check_varying_size_network_connects() {
    init_logging();

    let mut rng = crate::new_rng();

    // Try with a few predefined sets of network sizes.
    for &number_of_nodes in &[2u16, 3, 5, 9, 15] {
        let timeout = Duration::from_secs(3 * number_of_nodes as u64);

        let mut net = TestingNetwork::new();

        // Pick a random port in the higher ranges that is likely to be unused.
        let first_node_port = testing::unused_port_on_localhost();
        let cfg = Config::default().with_network_config(
            network::Config::default_local_net_first_node(first_node_port),
        );

        let _ = net.add_node_with_config(cfg, &mut rng).await.unwrap();
        let cfg = Config::default()
            .with_network_config(network::Config::default_local_net(first_node_port));

        for _ in 1..number_of_nodes {
            net.add_node_with_config(cfg.clone(), &mut rng)
                .await
                .unwrap();
        }

        // The network should be fully connected.
        let blocklist = HashSet::new();
        net.settle_on(
            &mut rng,
            |nodes| network_is_complete(&blocklist, nodes),
            timeout,
        )
        .await;

        let blocklist = HashSet::new();
        // This should not make a difference at all, but we're paranoid, so check again.
        assert!(
            network_is_complete(&blocklist, net.nodes()),
            "network did not stay connected after being settled"
        );

        // Now the network should have an appropriate number of peers.

        // This test will run multiple times, so ensure we cleanup all ports.
        net.finalize().await;
    }
}

/// Check that a network of varying sizes will connect all nodes properly.
#[tokio::test]
async fn ensure_peers_metric_is_correct() {
    init_logging();

    let mut rng = crate::new_rng();

    // Larger networks can potentially become more unreliable, so we try with small sizes only.
    for &number_of_nodes in &[2u16, 3, 5] {
        let timeout = Duration::from_secs(3 * number_of_nodes as u64);

        let mut net = TestingNetwork::new();

        // Pick a random port in the higher ranges that is likely to be unused.
        let first_node_port = testing::unused_port_on_localhost();

        let cfg = Config::default().with_network_config(
            network::Config::default_local_net_first_node(first_node_port),
        );

        let _ = net.add_node_with_config(cfg, &mut rng).await.unwrap();

        let cfg = Config::default()
            .with_network_config(network::Config::default_local_net(first_node_port));

        for _ in 1..number_of_nodes {
            net.add_node_with_config(cfg.clone(), &mut rng)
                .await
                .unwrap();
        }

        net.settle_on(
            &mut rng,
            |nodes: &Nodes<TestReactor>| {
                nodes.values().all(|runner| {
                    runner.reactor().inner().net.net_metrics.peers.get()
                        == number_of_nodes as i64 - 1
                })
            },
            timeout,
        )
        .await;

        net.finalize().await;
    }
}