sui_network/
utils.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
// Copyright (c) Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

#[cfg(test)]
pub fn build_network(f: impl FnOnce(anemo::Router) -> anemo::Router) -> anemo::Network {
    build_network_impl(f, None).0
}

#[cfg(test)]
pub fn build_network_and_key(
    f: impl FnOnce(anemo::Router) -> anemo::Router,
) -> (anemo::Network, sui_types::crypto::NetworkKeyPair) {
    build_network_impl(f, None)
}

#[cfg(test)]
pub fn build_network_with_anemo_config(
    f: impl FnOnce(anemo::Router) -> anemo::Router,
    anemo_config: anemo::Config,
) -> (anemo::Network, sui_types::crypto::NetworkKeyPair) {
    build_network_impl(f, Some(anemo_config))
}

#[cfg(test)]
fn build_network_impl(
    f: impl FnOnce(anemo::Router) -> anemo::Router,
    anemo_config: Option<anemo::Config>,
) -> (anemo::Network, sui_types::crypto::NetworkKeyPair) {
    use fastcrypto::traits::KeyPair;

    let keypair = sui_types::crypto::NetworkKeyPair::generate(&mut rand::thread_rng());
    let router = f(anemo::Router::new());
    let network = anemo::Network::bind("localhost:0")
        .private_key(keypair.copy().private().0.to_bytes())
        .config(anemo_config.unwrap_or_default())
        .server_name("test")
        .start(router)
        .unwrap();

    println!(
        "starting network {} {}",
        network.local_addr(),
        network.peer_id(),
    );
    (network, keypair)
}