This repository has been archived by the owner on Jul 31, 2023. It is now read-only.
forked from aptos-labs/aptos-core
-
Notifications
You must be signed in to change notification settings - Fork 1
/
node.rs
623 lines (549 loc) · 19.9 KB
/
node.rs
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
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
// Copyright © Aptos Foundation
// Parts of the project are originally copyright © Meta Platforms, Inc.
// SPDX-License-Identifier: Apache-2.0
use crate::{
core_mempool::{CoreMempool, TimelineState},
network::MempoolSyncMsg,
shared_mempool::{start_shared_mempool, types::SharedMempoolNotification},
tests::common::TestTransaction,
};
use aptos_channels::{aptos_channel, message_queues::QueueStyle};
use aptos_config::{
config::{Identity, NodeConfig, PeerRole, RoleType},
network_id::{NetworkContext, NetworkId, PeerNetworkId},
};
use aptos_crypto::{x25519::PrivateKey, Uniform};
use aptos_event_notifications::{ReconfigNotification, ReconfigNotificationListener};
use aptos_infallible::{Mutex, MutexGuard, RwLock};
use aptos_netcore::transport::ConnectionOrigin;
use aptos_network::{
application::{
interface::{NetworkClient, NetworkServiceEvents},
storage::PeersAndMetadata,
},
peer_manager::{
conn_notifs_channel, ConnectionNotification, ConnectionRequestSender,
PeerManagerNotification, PeerManagerRequest, PeerManagerRequestSender,
},
protocols::{
network::{NetworkEvents, NetworkSender, NewNetworkEvents, NewNetworkSender},
wire::handshake::v1::ProtocolId::MempoolDirectSend,
},
transport::ConnectionMetadata,
ProtocolId,
};
use aptos_storage_interface::mock::MockDbReaderWriter;
use aptos_types::{on_chain_config::OnChainConfigPayload, PeerId};
use aptos_vm_validator::mocks::mock_vm_validator::MockVMValidator;
use enum_dispatch::enum_dispatch;
use futures::{
channel::mpsc::{self, unbounded, UnboundedReceiver},
FutureExt, StreamExt,
};
use rand::rngs::StdRng;
use std::{
collections::{HashMap, HashSet},
sync::Arc,
};
use tokio::runtime::Runtime;
type MempoolNetworkHandle = (
NetworkId,
NetworkSender<MempoolSyncMsg>,
NetworkEvents<MempoolSyncMsg>,
);
/// This is a simple node identifier for testing
/// This keeps track of the `NodeType` and a simple index
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialOrd, PartialEq)]
pub struct NodeId {
pub node_type: NodeType,
num: u32,
}
impl NodeId {
pub(crate) fn new(node_type: NodeType, num: u32) -> Self {
NodeId { node_type, num }
}
}
/// Yet another type, to determine the differences between
/// Validators, ValidatorFullNodes, and FullNodes
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialOrd, PartialEq)]
pub enum NodeType {
Validator,
ValidatorFullNode,
FullNode,
}
/// A union type for all types of simulated nodes
#[enum_dispatch(NodeInfoTrait)]
#[derive(Clone, Debug)]
pub enum NodeInfo {
Validator(ValidatorNodeInfo),
ValidatorFull(ValidatorFullNodeInfo),
Full(FullNodeInfo),
}
/// Accessors to the union type of all simulated nodes
#[enum_dispatch]
pub trait NodeInfoTrait {
fn supported_networks(&self) -> Vec<NetworkId>;
fn find_common_network<T: NodeInfoTrait>(&self, other: &T) -> NetworkId {
let supported: HashSet<_> = self.supported_networks().into_iter().collect();
let other_supported: HashSet<_> = other.supported_networks().into_iter().collect();
if supported.contains(&NetworkId::Validator)
&& other_supported.contains(&NetworkId::Validator)
{
NetworkId::Validator
} else if supported.contains(&NetworkId::Public)
&& other_supported.contains(&NetworkId::Public)
{
NetworkId::Public
} else if supported.contains(&NetworkId::Vfn) && other_supported.contains(&NetworkId::Vfn) {
NetworkId::Vfn
} else {
panic!("Expected a common network")
}
}
fn peer_network_ids(&self) -> Vec<PeerNetworkId> {
self.supported_networks()
.into_iter()
.map(|network| self.peer_network_id(network))
.collect()
}
fn peer_id(&self, network_id: NetworkId) -> PeerId;
fn peer_network_id(&self, network_id: NetworkId) -> PeerNetworkId {
PeerNetworkId::new(network_id, self.peer_id(network_id))
}
/// `RoleType` of the `Node`
fn role(&self) -> RoleType;
/// `PeerRole` for use in the upstream / downstream peers
fn peer_role(&self) -> PeerRole;
}
#[derive(Clone, Debug)]
pub struct ValidatorNodeInfo {
peer_id: PeerId,
vfn_peer_id: PeerId,
}
impl ValidatorNodeInfo {
fn new(peer_id: PeerId, vfn_peer_id: PeerId) -> Self {
ValidatorNodeInfo {
peer_id,
vfn_peer_id,
}
}
}
impl NodeInfoTrait for ValidatorNodeInfo {
fn supported_networks(&self) -> Vec<NetworkId> {
vec![NetworkId::Validator, NetworkId::Vfn]
}
fn peer_id(&self, network_id: NetworkId) -> PeerId {
match network_id {
NetworkId::Validator => self.peer_id,
NetworkId::Vfn => self.vfn_peer_id,
NetworkId::Public => panic!("Invalid network id for validator"),
}
}
fn role(&self) -> RoleType {
RoleType::Validator
}
fn peer_role(&self) -> PeerRole {
PeerRole::Validator
}
}
#[derive(Clone, Debug)]
pub struct ValidatorFullNodeInfo {
peer_id: PeerId,
vfn_peer_id: PeerId,
}
impl ValidatorFullNodeInfo {
fn new(peer_id: PeerId, vfn_peer_id: PeerId) -> Self {
ValidatorFullNodeInfo {
peer_id,
vfn_peer_id,
}
}
}
impl NodeInfoTrait for ValidatorFullNodeInfo {
fn supported_networks(&self) -> Vec<NetworkId> {
vec![NetworkId::Public, NetworkId::Vfn]
}
fn peer_id(&self, network_id: NetworkId) -> PeerId {
match network_id {
NetworkId::Public => self.peer_id,
NetworkId::Vfn => self.vfn_peer_id,
NetworkId::Validator => panic!("Invalid network id for validator full node"),
}
}
fn role(&self) -> RoleType {
RoleType::FullNode
}
fn peer_role(&self) -> PeerRole {
PeerRole::ValidatorFullNode
}
}
#[derive(Clone, Debug)]
pub struct FullNodeInfo {
peer_id: PeerId,
peer_role: PeerRole,
}
impl FullNodeInfo {
fn new(peer_id: PeerId, peer_role: PeerRole) -> Self {
FullNodeInfo { peer_id, peer_role }
}
}
impl NodeInfoTrait for FullNodeInfo {
fn supported_networks(&self) -> Vec<NetworkId> {
vec![NetworkId::Public]
}
fn peer_id(&self, network_id: NetworkId) -> PeerId {
if NetworkId::Public == network_id {
self.peer_id
} else {
panic!("Invalid network id for public full node")
}
}
fn role(&self) -> RoleType {
RoleType::FullNode
}
fn peer_role(&self) -> PeerRole {
self.peer_role
}
}
/// Provides a `NodeInfo` and `NodeConfig` for a validator
pub fn validator_config(rng: &mut StdRng) -> (ValidatorNodeInfo, NodeConfig) {
let config = NodeConfig::generate_random_config_with_template(
&NodeConfig::get_default_validator_config(),
rng,
);
let peer_id = config
.validator_network
.as_ref()
.expect("Validator must have a validator network")
.peer_id();
(
ValidatorNodeInfo::new(peer_id, PeerId::from_hex_literal("0xDEADBEEF").unwrap()),
config,
)
}
/// Provides a `NodeInfo` and `NodeConfig` for a ValidatorFullNode
pub fn vfn_config(rng: &mut StdRng, peer_id: PeerId) -> (ValidatorFullNodeInfo, NodeConfig) {
let mut vfn_config = NodeConfig::generate_random_config_with_template(
&NodeConfig::get_default_vfn_config(),
rng,
);
vfn_config
.full_node_networks
.iter_mut()
.find(|network| network.network_id == NetworkId::Public)
.as_mut()
.unwrap()
.identity = Identity::from_config(PrivateKey::generate_for_testing(), peer_id);
let networks: HashMap<_, _> = vfn_config
.full_node_networks
.iter()
.map(|network| (network.network_id, network.peer_id()))
.collect();
(
ValidatorFullNodeInfo::new(
*networks
.get(&NetworkId::Public)
.expect("VFN config should have a public network"),
*networks
.get(&NetworkId::Vfn)
.expect("VFN config should have a vfn network"),
),
vfn_config,
)
}
/// Provides a `NodeInfo` and `NodeConfig` for a public full node
pub fn public_full_node_config(
rng: &mut StdRng,
peer_role: PeerRole,
) -> (FullNodeInfo, NodeConfig) {
let fn_config = NodeConfig::generate_random_config_with_template(
&NodeConfig::get_default_pfn_config(),
rng,
);
let peer_id = fn_config
.full_node_networks
.iter()
.find(|network| network.network_id == NetworkId::Public)
.expect("Full Node must have a public network")
.peer_id();
(FullNodeInfo::new(peer_id, peer_role), fn_config)
}
/// A struct representing a node, it's network interfaces, mempool, and a mempool event subscriber
pub struct Node {
/// The identifying Node
node_info: NodeInfo,
/// `CoreMempool` for this node
mempool: Arc<Mutex<CoreMempool>>,
/// Network interfaces for a node
network_interfaces: HashMap<NetworkId, NodeNetworkInterface>,
/// Tokio runtime
runtime: Arc<Runtime>,
/// Subscriber for mempool events
subscriber: UnboundedReceiver<SharedMempoolNotification>,
/// Global peer connection data
peers_and_metadata: Arc<PeersAndMetadata>,
}
/// Reimplement `NodeInfoTrait` for simplicity
impl NodeInfoTrait for Node {
fn supported_networks(&self) -> Vec<NetworkId> {
self.node_info.supported_networks()
}
fn peer_id(&self, network_id: NetworkId) -> PeerId {
self.node_info.peer_id(network_id)
}
fn role(&self) -> RoleType {
self.node_info.role()
}
fn peer_role(&self) -> PeerRole {
self.node_info.peer_role()
}
}
impl Node {
/// Sets up a single node by starting up mempool and any network handles
pub fn new(node: NodeInfo, config: NodeConfig) -> Node {
let (network_interfaces, network_client, network_service_events, peers_and_metadata) =
setup_node_network_interfaces(&node);
let (mempool, runtime, subscriber) =
start_node_mempool(config, network_client, network_service_events);
Node {
node_info: node,
mempool,
network_interfaces,
runtime: Arc::new(runtime),
subscriber,
peers_and_metadata,
}
}
/// Retrieves a `CoreMempool`
pub fn mempool(&self) -> MutexGuard<CoreMempool> {
self.mempool.lock()
}
/// Queues transactions for sending on a node. Must use `broadcast_txns` to send to other nodes
pub fn add_txns(&self, txns: Vec<TestTransaction>) {
let mut mempool = self.mempool();
for txn in txns {
let transaction = txn.make_signed_transaction_with_max_gas_amount(5);
mempool.add_txn(
transaction.clone(),
transaction.gas_unit_price(),
0,
TimelineState::NotReady,
);
}
}
/// Notifies the `Node` of a `new_peer`
pub fn send_new_peer_event(
&mut self,
peer_network_id: PeerNetworkId,
peer_role: PeerRole,
origin: ConnectionOrigin,
) {
let mut metadata = ConnectionMetadata::mock_with_role_and_origin(
peer_network_id.peer_id(),
peer_role,
origin,
);
metadata
.application_protocols
.insert(ProtocolId::MempoolDirectSend);
let notif = ConnectionNotification::NewPeer(metadata.clone(), NetworkContext::mock());
self.peers_and_metadata
.insert_connection_metadata(peer_network_id, metadata)
.unwrap();
self.send_connection_event(peer_network_id.network_id(), notif);
}
/// Sends a connection event, and waits for the notification to arrive
fn send_connection_event(&mut self, network_id: NetworkId, notif: ConnectionNotification) {
self.send_network_notif(network_id, notif);
self.wait_for_event(SharedMempoolNotification::PeerStateChange);
}
/// Waits for a specific `SharedMempoolNotification` event
pub fn wait_for_event(&mut self, expected: SharedMempoolNotification) {
let event = self.runtime.block_on(self.subscriber.next()).unwrap();
if event == expected {
return;
}
panic!("Failed to get expected event '{:?}'", expected)
}
/// Checks that there are no `SharedMempoolNotification`s on the subscriber
pub fn check_no_subscriber_events(&mut self) {
assert!(self.subscriber.select_next_some().now_or_never().is_none())
}
/// Checks that a node has no pending messages to send.
pub fn check_no_network_messages_sent(&mut self, network_id: NetworkId) {
self.check_no_subscriber_events();
assert!(self
.get_network_interface(network_id)
.network_reqs_rx
.select_next_some()
.now_or_never()
.is_none())
}
/// Retrieves a network interface for a specific `NetworkId` based on whether it's the primary network
fn get_network_interface(&mut self, network_id: NetworkId) -> &mut NodeNetworkInterface {
self.network_interfaces.get_mut(&network_id).unwrap()
}
/// Retrieves the next network request `PeerManagerRequest`
pub fn get_next_network_req(&mut self, network_id: NetworkId) -> PeerManagerRequest {
let runtime = self.runtime.clone();
self.get_network_interface(network_id)
.get_next_network_req(runtime)
}
/// Send network request `PeerManagerNotification` from a remote peer to the local node
pub fn send_network_req(
&mut self,
network_id: NetworkId,
protocol: ProtocolId,
notif: PeerManagerNotification,
) {
self.get_network_interface(network_id)
.send_network_req(protocol, notif);
}
/// Sends a `ConnectionNotification` to the local node
pub fn send_network_notif(&mut self, network_id: NetworkId, notif: ConnectionNotification) {
self.get_network_interface(network_id)
.send_connection_notif(notif)
}
}
/// A simplistic view of the entire network stack for a given `NetworkId`
/// Allows us to mock out the network without dealing with the details
pub struct NodeNetworkInterface {
/// Peer request receiver for messages
pub(crate) network_reqs_rx: aptos_channel::Receiver<(PeerId, ProtocolId), PeerManagerRequest>,
/// Peer notification sender for sending outgoing messages to other peers
pub(crate) network_notifs_tx:
aptos_channel::Sender<(PeerId, ProtocolId), PeerManagerNotification>,
/// Sender for connecting / disconnecting peers
pub(crate) network_conn_event_notifs_tx: conn_notifs_channel::Sender,
}
impl NodeNetworkInterface {
fn get_next_network_req(&mut self, runtime: Arc<Runtime>) -> PeerManagerRequest {
runtime.block_on(self.network_reqs_rx.next()).unwrap()
}
fn send_network_req(&mut self, protocol: ProtocolId, message: PeerManagerNotification) {
let remote_peer_id = match &message {
PeerManagerNotification::RecvRpc(peer_id, _) => *peer_id,
PeerManagerNotification::RecvMessage(peer_id, _) => *peer_id,
};
self.network_notifs_tx
.push((remote_peer_id, protocol), message)
.unwrap()
}
/// Send a notification specifying, where a remote peer has it's state changed
fn send_connection_notif(&mut self, notif: ConnectionNotification) {
let peer_id = match ¬if {
ConnectionNotification::NewPeer(metadata, _) => metadata.remote_peer_id,
ConnectionNotification::LostPeer(metadata, _, _) => metadata.remote_peer_id,
};
self.network_conn_event_notifs_tx
.push(peer_id, notif)
.unwrap()
}
}
// Below here are static functions to help build a new `Node`
/// Sets up the network handles for a `Node`
fn setup_node_network_interfaces(
node: &NodeInfo,
) -> (
HashMap<NetworkId, NodeNetworkInterface>,
NetworkClient<MempoolSyncMsg>,
NetworkServiceEvents<MempoolSyncMsg>,
Arc<PeersAndMetadata>,
) {
// Create the peers and metadata
let network_ids = node.supported_networks();
let peers_and_metadata = PeersAndMetadata::new(&network_ids);
// Create the network interfaces
let mut network_senders = HashMap::new();
let mut network_and_events = HashMap::new();
let mut network_interfaces = HashMap::new();
for network_id in network_ids {
let (network_interface, network_handle) =
setup_node_network_interface(PeerNetworkId::new(network_id, node.peer_id(network_id)));
network_senders.insert(network_id, network_handle.1);
network_and_events.insert(network_id, network_handle.2);
network_interfaces.insert(network_id, network_interface);
}
// Create the client and service events
let network_client = NetworkClient::new(
vec![MempoolDirectSend],
vec![],
network_senders,
peers_and_metadata.clone(),
);
let network_service_events = NetworkServiceEvents::new(network_and_events);
(
network_interfaces,
network_client,
network_service_events,
peers_and_metadata,
)
}
/// Builds a single network interface with associated queues, and attaches it to the top level network
fn setup_node_network_interface(
peer_network_id: PeerNetworkId,
) -> (NodeNetworkInterface, MempoolNetworkHandle) {
static MAX_QUEUE_SIZE: usize = 8;
let (network_reqs_tx, network_reqs_rx) =
aptos_channel::new(QueueStyle::FIFO, MAX_QUEUE_SIZE, None);
let (connection_reqs_tx, _) = aptos_channel::new(QueueStyle::FIFO, MAX_QUEUE_SIZE, None);
let (network_notifs_tx, network_notifs_rx) =
aptos_channel::new(QueueStyle::FIFO, MAX_QUEUE_SIZE, None);
let (network_conn_event_notifs_tx, conn_status_rx) = conn_notifs_channel::new();
let network_sender = NetworkSender::new(
PeerManagerRequestSender::new(network_reqs_tx),
ConnectionRequestSender::new(connection_reqs_tx),
);
let network_events = NetworkEvents::new(network_notifs_rx, conn_status_rx);
(
NodeNetworkInterface {
network_reqs_rx,
network_notifs_tx,
network_conn_event_notifs_tx,
},
(peer_network_id.network_id(), network_sender, network_events),
)
}
/// Starts up the mempool resources for a single node
fn start_node_mempool(
config: NodeConfig,
network_client: NetworkClient<MempoolSyncMsg>,
network_service_events: NetworkServiceEvents<MempoolSyncMsg>,
) -> (
Arc<Mutex<CoreMempool>>,
Runtime,
UnboundedReceiver<SharedMempoolNotification>,
) {
let mempool = Arc::new(Mutex::new(CoreMempool::new(&config)));
let (sender, subscriber) = unbounded();
let (_ac_endpoint_sender, ac_endpoint_receiver) = mpsc::channel(1_024);
let (_quorum_store_sender, quorum_store_receiver) = mpsc::channel(1_024);
let (_mempool_notifier, mempool_listener) =
aptos_mempool_notifications::new_mempool_notifier_listener_pair();
let (reconfig_sender, reconfig_events) = aptos_channel::new(QueueStyle::LIFO, 1, None);
let reconfig_event_subscriber = ReconfigNotificationListener {
notification_receiver: reconfig_events,
};
reconfig_sender
.push((), ReconfigNotification {
version: 1,
on_chain_configs: OnChainConfigPayload::new(1, Arc::new(HashMap::new())),
})
.unwrap();
let runtime = aptos_runtimes::spawn_named_runtime("shared-mem".into(), None);
start_shared_mempool(
runtime.handle(),
&config,
Arc::clone(&mempool),
network_client,
network_service_events,
ac_endpoint_receiver,
quorum_store_receiver,
mempool_listener,
reconfig_event_subscriber,
Arc::new(MockDbReaderWriter),
Arc::new(RwLock::new(MockVMValidator)),
vec![sender],
);
(mempool, runtime, subscriber)
}