Skip to content

Commit

Permalink
feat(network)!: Network isolation [fixes NET-818] (#2245)
Browse files Browse the repository at this point in the history
  • Loading branch information
gurinderu authored May 30, 2024
1 parent 117fbd6 commit f5eb980
Show file tree
Hide file tree
Showing 24 changed files with 300 additions and 111 deletions.
14 changes: 8 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion aquamarine/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ eyre = { workspace = true }
bytesize = { workspace = true }
async-trait = { workspace = true }
health = { workspace = true }
config = { version = "0.13.4", features = [] }
enum_dispatch = { workspace = true }

[dev-dependencies]
Expand Down
3 changes: 1 addition & 2 deletions crates/chain-data/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,4 @@ log = { workspace = true }
thiserror = { workspace = true }
eyre = { workspace = true }
alloy-sol-types = { workspace = true }
alloy_serde_macro = { workspace = true }
const-hex = { workspace = true }
const-hex = { workspace = true }
1 change: 0 additions & 1 deletion crates/connected-client/src/connected_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ impl ConnectedClient {
node_address: Multiaddr,
timeout: Duration,
idle_connection_timeout: Duration,

particle_ttl: Option<Duration>,
) -> Result<Self> {
Self::connect_with_timeout(
Expand Down
1 change: 1 addition & 0 deletions crates/created-swarm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,4 @@ reqwest = { workspace = true }
# for re-export for outside tests
fluence-spell-dtos = { workspace = true }
fluence-app-service = { workspace = true }
rand = { workspace = true }
83 changes: 59 additions & 24 deletions crates/created-swarm/src/swarm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@ use fs_utils::to_abs_path;
use futures::stream::iter;
use nox::{Connectivity, Node};
use particle_protocol::ProtocolConfig;
use rand::RngCore;
use server_config::{
persistent_dir, system_services_config, BootstrapConfig, ChainConfig, ResolvedConfig,
persistent_dir, system_services_config, BootstrapConfig, ChainConfig, Network, ResolvedConfig,
UnresolvedConfig,
};
use tempfile::TempDir;
Expand Down Expand Up @@ -81,6 +82,7 @@ pub struct CreatedSwarm {
#[derivative(Debug = "ignore")]
pub aquamarine_api: AquamarineApi,
http_listen_addr: SocketAddr,
pub network_key: NetworkKey,
}

pub async fn make_swarms(n: usize) -> Vec<CreatedSwarm> {
Expand All @@ -94,7 +96,7 @@ where
make_swarms_with(
n,
move |bs, maddr| {
let cfg = update_cfg(SwarmConfig::new(bs, maddr));
let cfg = update_cfg(SwarmConfig::new(bs, maddr, NetworkKey::random()));
async move { create_swarm(cfg).await }
},
create_memory_maddr,
Expand All @@ -108,10 +110,13 @@ pub async fn make_swarms_with_transport_and_mocked_vm(
n: usize,
transport: Transport,
) -> Vec<CreatedSwarm> {
let network_key = NetworkKey::random();

make_swarms_with::<EasyVM, _, _, _, _>(
n,
|bs, maddr| async {
create_swarm_with_runtime(SwarmConfig::new(bs, maddr), |_| None).await
create_swarm_with_runtime(SwarmConfig::new(bs, maddr, network_key.clone()), |_| None)
.await
},
move || match transport {
Transport::Memory => create_memory_maddr(),
Expand All @@ -133,10 +138,12 @@ where
F: (FnMut(SwarmConfig) -> SwarmConfig),
B: (FnMut(Vec<Multiaddr>) -> Vec<Multiaddr>),
{
let network_key = NetworkKey::random();

make_swarms_with::<EasyVM, _, _, _, _>(
n,
move |bs, maddr| {
let cfg = update_cfg(SwarmConfig::new(bs, maddr));
let cfg = update_cfg(SwarmConfig::new(bs, maddr, network_key.clone()));
async move { create_swarm_with_runtime(cfg, move |_| delay).await }
},
create_memory_maddr,
Expand Down Expand Up @@ -211,6 +218,7 @@ where
connectivity,
aquamarine_api,
http_listen_addr,
network_key: input_config.network_key.clone(),
}
}
.boxed_local()
Expand Down Expand Up @@ -253,6 +261,30 @@ async fn wait_connected_on_addrs(addrs: Vec<SocketAddr>) {
healthcheck.await;
}

#[derive(Clone, Debug)]
pub struct NetworkKey([u8; 32]);

impl NetworkKey {
pub fn random() -> Self {
let mut rng = rand::thread_rng();
let mut res: [u8; 32] = Default::default();
rng.fill_bytes(&mut res);
NetworkKey(res)
}
}

impl From<[u8; 32]> for NetworkKey {
fn from(value: [u8; 32]) -> Self {
NetworkKey(value)
}
}

impl From<NetworkKey> for [u8; 32] {
fn from(value: NetworkKey) -> Self {
value.0
}
}

#[derive(Clone, Derivative)]
#[derivative(Debug)]
pub struct SwarmConfig {
Expand All @@ -278,10 +310,11 @@ pub struct SwarmConfig {
pub connector_api_endpoint: Option<String>,
pub chain_config: Option<ChainConfig>,
pub cc_events_dir: Option<PathBuf>,
pub network_key: NetworkKey,
}

impl SwarmConfig {
pub fn new(bootstraps: Vec<Multiaddr>, listen_on: Multiaddr) -> Self {
pub fn new(bootstraps: Vec<Multiaddr>, listen_on: Multiaddr, network_key: NetworkKey) -> Self {
let transport = match listen_on.iter().next() {
Some(Protocol::Memory(_)) => Transport::Memory,
_ => Transport::Network,
Expand All @@ -308,6 +341,7 @@ impl SwarmConfig {
connector_api_endpoint: None,
chain_config: None,
cc_events_dir: None,
network_key,
}
}
}
Expand Down Expand Up @@ -365,25 +399,25 @@ pub async fn create_swarm_with_runtime<RT: AquaRuntime>(
let tmp_dir = config.tmp_dir.path().to_path_buf();

let node_config = json!({
"base_dir": tmp_dir.to_string_lossy(),
"root_key_pair": {
"format": format,
"generate_on_absence": false,
"value": base64.encode(config.keypair.to_vec()),
},
"builtins_key_pair": {
"format": format,
"generate_on_absence": false,
"value": base64.encode(config.builtins_keypair.to_vec()),
},

"builtins_base_dir": config.builtins_dir,
"external_multiaddresses": [config.listen_on],
"spell_base_dir": Some(config.spell_base_dir.clone().unwrap_or(to_abs_path(PathBuf::from("spell")))),
"http_port": config.http_port,
"listen_ip": "127.0.0.1",
"cc_events_dir": config.cc_events_dir,
});
"network": "Dar",
"base_dir": tmp_dir.to_string_lossy(),
"root_key_pair": {
"format": format,
"generate_on_absence": false,
"value": base64.encode(config.keypair.to_vec()),
},
"builtins_key_pair": {
"format": format,
"generate_on_absence": false,
"value": base64.encode(config.builtins_keypair.to_vec()),
},
"builtins_base_dir": config.builtins_dir,
"external_multiaddresses": [config.listen_on],
"spell_base_dir": Some(config.spell_base_dir.clone().unwrap_or(to_abs_path(PathBuf::from("spell")))),
"http_port": config.http_port,
"listen_ip": "127.0.0.1",
"cc_events_dir": config.cc_events_dir,
});

let node_config: UnresolvedConfig =
UnresolvedConfig::deserialize(node_config).expect("created_swarm: deserialize config");
Expand All @@ -393,6 +427,7 @@ pub async fn create_swarm_with_runtime<RT: AquaRuntime>(
resolved.node_config.transport_config.socket_timeout = TRANSPORT_TIMEOUT;
resolved.node_config.protocol_config =
ProtocolConfig::new(TRANSPORT_TIMEOUT, TRANSPORT_TIMEOUT);
resolved.network=Network::Custom(config.network_key.clone().into());

resolved.node_config.bootstrap_nodes = config.bootstraps.clone();
resolved.node_config.bootstrap_config = BootstrapConfig::zero();
Expand Down
4 changes: 2 additions & 2 deletions crates/kademlia/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ particle-protocol = { workspace = true }

control-macro = { workspace = true }
fluence-libp2p = { workspace = true }
server-config = { workspace = true }
fluence-keypair = { workspace = true }

libp2p = { workspace = true }
Expand All @@ -29,6 +28,7 @@ thiserror = { workspace = true }
tokio = { workspace = true }
tracing = { workspace = true }


[dev-dependencies]
log-utils = { workspace = true }

rand = { workspace = true }
Loading

0 comments on commit f5eb980

Please sign in to comment.