Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Network reconstruction (#3900)
* add network reconstruction for handshaking

* network construction

* process the unwrap

* fix test_handshake_message

* fix cargo fmt

* move derive and core from network-rpc to network-p2p

* fix fmt

* commit drive and core in network-p2p

* 1, rebase the master
2, resolve the compatibility issue

* move the stauts into the network module from the network-p2p module

* Add log and follow-up actions for protocol procedures

* test status in service_test.rs

* rebase network reconstruction

* network construction

* fix test_handshake_message

* fix cargo fmt

* move the stauts into the network module from the network-p2p module

* test status in service_test.rs

* fix fmt

* fix clippy

* fix fmt

* fix fmt

* fix fmt

* fix serde and fmt

* fix unittest in network-p2p

* fix clippy

* use 1.13.5

* fix:
1, add expect in upadte business status
2, handshake return the result struct hence decouple the relation of network-p2p and network modules

* fix bug: #3918
  • Loading branch information
jackzhhuang committed Jun 26, 2023
1 parent c57b53d commit 5772b32
Show file tree
Hide file tree
Showing 70 changed files with 884 additions and 569 deletions.
63 changes: 35 additions & 28 deletions Cargo.lock

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

12 changes: 6 additions & 6 deletions Cargo.toml
Expand Up @@ -56,8 +56,8 @@ members = [
"network/types",
"network/api",
"network-rpc",
"network-rpc/derive",
"network-rpc/core",
"network-p2p/derive",
"network-p2p/core",
"network-rpc/api",
"account/api",
"account",
Expand Down Expand Up @@ -160,8 +160,8 @@ default-members = [
"network",
"network/api",
"network-rpc",
"network-rpc/derive",
"network-rpc/core",
"network-p2p/derive",
"network-p2p/core",
"network-rpc/api",
"account/api",
"account",
Expand Down Expand Up @@ -352,8 +352,8 @@ names = { version = "0.14.0", default-features = false }
network-api = { path = "network/api", package = "network-api" }
network-p2p = { path = "network-p2p" }
network-p2p-types = { path = "network-p2p/types" }
network-rpc-core = { path = "network-rpc/core" }
network-rpc-derive = { path = "network-rpc/derive" }
network-p2p-core = { path = "network-p2p/core" }
network-p2p-derive = { path = "network-p2p/derive" }
network-types = { path = "network/types" }
num = "0.4.0"
num-derive = "0.3.3"
Expand Down
1 change: 1 addition & 0 deletions cmd/peer-watcher/Cargo.toml
Expand Up @@ -17,6 +17,7 @@ starcoin-logger = { workspace = true }
starcoin-network = { workspace = true }
starcoin-storage = { workspace = true }
starcoin-types = { workspace = true }
bcs-ext = { package = "bcs-ext", workspace = true }

[package]
authors = { workspace = true }
Expand Down
3 changes: 2 additions & 1 deletion cmd/peer-watcher/src/lib.rs
Expand Up @@ -5,6 +5,7 @@ use anyhow::Result;
use network_p2p::NetworkWorker;
use network_types::peer_info::PeerInfo;
use starcoin_config::{ChainNetwork, NetworkConfig};
use starcoin_network::network_p2p_handle::Networkp2pHandle;
use starcoin_network::{build_network_worker, NotificationMessage};
use starcoin_storage::storage::StorageInstance;
use starcoin_storage::Storage;
Expand All @@ -13,7 +14,7 @@ use std::sync::Arc;
pub fn build_lighting_network(
net: &ChainNetwork,
network_config: &NetworkConfig,
) -> Result<(PeerInfo, NetworkWorker)> {
) -> Result<(PeerInfo, NetworkWorker<Networkp2pHandle>)> {
let genesis = starcoin_genesis::Genesis::load_or_build(net)?;
let storage = Arc::new(Storage::new(StorageInstance::new_cache_instance())?);
let chain_info = genesis.execute_genesis_block(net, storage)?;
Expand Down
24 changes: 16 additions & 8 deletions cmd/peer-watcher/src/main.rs
@@ -1,12 +1,14 @@
// Copyright (c) The Starcoin Core Contributors
// SPDX-License-Identifier: Apache-2.0

use bcs_ext::BCSCodec;
use clap::Parser;
use futures::StreamExt;
use network_p2p::Event;
use network_types::peer_info::PeerInfo;
use starcoin_config::{NodeConfig, StarcoinOpt};
use starcoin_peer_watcher::build_lighting_network;
use starcoin_types::startup_info::ChainInfo;

/// A lighting node, connect to peer to peer network, and monitor peers.
fn main() {
Expand All @@ -25,17 +27,23 @@ fn main() {
Event::NotificationStreamOpened {
remote,
protocol: _,
info,
generic_data,
notif_protocols,
rpc_protocols,
version_string,
} => Some(PeerInfo::new(
remote.into(),
*info,
notif_protocols,
rpc_protocols,
version_string,
)),
} => match ChainInfo::decode(&generic_data) {
Ok(chain_info) => Some(PeerInfo::new(
remote.into(),
chain_info,
notif_protocols,
rpc_protocols,
version_string,
)),
Err(error) => {
println!("failed to decode generic message for the reason: {}", error);
None
}
},
_ => None,
}
})
Expand Down
2 changes: 1 addition & 1 deletion cmd/starcoin/src/node/network/call_peer_cmd.rs
Expand Up @@ -6,7 +6,7 @@ use crate::StarcoinOpt;
use anyhow::{bail, Result};
use bcs_ext::BCSCodec;
use clap::Parser;
use network_types::peer_info::PeerId;
use network_p2p_types::peer_id::PeerId;
use scmd::{CommandAction, ExecContext};
use starcoin_network_rpc_api::Ping;
use starcoin_rpc_api::types::StrView;
Expand Down
3 changes: 2 additions & 1 deletion cmd/starcoin/src/node/network/get_address_cmd.rs
Expand Up @@ -5,7 +5,8 @@ use crate::cli_state::CliState;
use crate::StarcoinOpt;
use anyhow::{format_err, Result};
use clap::Parser;
use network_types::peer_info::{Multiaddr, PeerId};
use network_p2p_types::peer_id::PeerId;
use network_types::peer_info::Multiaddr;
use scmd::{CommandAction, ExecContext};

#[derive(Debug, Parser, Default)]
Expand Down
2 changes: 1 addition & 1 deletion cmd/starcoin/src/node/network/known_peers_cmd.rs
Expand Up @@ -5,7 +5,7 @@ use crate::cli_state::CliState;
use crate::StarcoinOpt;
use anyhow::Result;
use clap::Parser;
use network_types::peer_info::PeerId;
use network_p2p_types::peer_id::PeerId;
use scmd::{CommandAction, ExecContext};

#[derive(Debug, Parser, Default)]
Expand Down
2 changes: 1 addition & 1 deletion cmd/starcoin/src/node/sync/start_cmd.rs
Expand Up @@ -6,7 +6,7 @@ use crate::StarcoinOpt;
use anyhow::Result;
use clap::Parser;
use network_api::PeerStrategy;
use network_types::peer_info::PeerId;
use network_p2p_types::peer_id::PeerId;
use scmd::{CommandAction, ExecContext};

#[derive(Debug, Parser, Default)]
Expand Down
2 changes: 1 addition & 1 deletion config/src/network_config.rs
Expand Up @@ -9,12 +9,12 @@ use crate::{
use anyhow::Result;
use clap::Parser;
use network_api::messages::{NotificationMessage, BLOCK_PROTOCOL_NAME};
use network_p2p_types::peer_id::PeerId;
use network_p2p_types::{
is_memory_addr, memory_addr,
multiaddr::{Multiaddr, Protocol},
MultiaddrWithPeerId,
};
use network_types::peer_info::PeerId;
use once_cell::sync::Lazy;
use rand::seq::SliceRandom;
use rand::thread_rng;
Expand Down
6 changes: 3 additions & 3 deletions network-rpc/core/Cargo.toml → network-p2p/core/Cargo.toml
Expand Up @@ -3,11 +3,11 @@ anyhow = { workspace = true }
bcs-ext = { package = "bcs-ext", workspace = true }
futures = { workspace = true }
log = { workspace = true }
network-rpc-derive = { workspace = true }
network-p2p-derive = { workspace = true }
num_enum = { workspace = true }
serde = { features = ["derive"], workspace = true }
starcoin-types = { package = "starcoin-types", workspace = true }
network-types = { workspace = true }
network-p2p-types = { workspace = true }

[dev-dependencies]
stest = { workspace = true }
Expand All @@ -16,7 +16,7 @@ stest = { workspace = true }
authors = { workspace = true }
edition = { workspace = true }
license = { workspace = true }
name = "network-rpc-core"
name = "network-p2p-core"
publish = { workspace = true }
version = "1.13.5"
homepage = { workspace = true }
Expand Down
Expand Up @@ -4,7 +4,7 @@
use anyhow::Result;
use futures::prelude::future::BoxFuture;
use futures::Future;
use network_types::peer_info::PeerId;
use network_p2p_types::peer_id::PeerId;
use std::borrow::Cow;
use std::collections::HashMap;
use std::sync::Arc;
Expand Down
7 changes: 3 additions & 4 deletions network-rpc/core/src/lib.rs → network-p2p/core/src/lib.rs
Expand Up @@ -8,20 +8,19 @@ use futures::FutureExt;
use num_enum::{IntoPrimitive, TryFromPrimitive};
use serde::{Deserialize, Serialize};

pub use network_types::peer_info::PeerId;

//TODO find a suitable place for this type.
use crate::server::NetworkRpcServer;
pub use network_p2p_types::peer_id::PeerId;
use std::borrow::Cow;

pub mod delegates;
pub mod server;

pub mod prelude {
pub use network_rpc_derive::net_rpc;
pub use network_p2p_derive::net_rpc;

pub use crate::NetRpcError;
pub use crate::PeerId;
pub use network_p2p_types::peer_id::PeerId;
}

pub mod export {
Expand Down
File renamed without changes.
Expand Up @@ -7,8 +7,8 @@ use futures::future::BoxFuture;
use futures::FutureExt;
use serde::{Deserialize, Serialize};

use network_rpc_core::server::NetworkRpcServer;
use network_rpc_core::{prelude::*, InmemoryRpcClient};
use network_p2p_core::server::NetworkRpcServer;
use network_p2p_core::{prelude::*, InmemoryRpcClient};

use crate::gen_client::NetworkRpcClient;
use crate::gen_server::KVRpc;
Expand Down Expand Up @@ -93,11 +93,11 @@ fn test_rpc_err() {

#[test]
fn test_result_serialize() {
let str_result: network_rpc_core::Result<String, NetRpcError> =
network_rpc_core::Result::Ok("test".to_string());
let str_result: network_p2p_core::Result<String, NetRpcError> =
network_p2p_core::Result::Ok("test".to_string());
let bytes = bcs_ext::to_bytes(&str_result).unwrap();
println!("bytes:{:?}", bytes);
let str_result2: network_rpc_core::Result<String, NetRpcError> =
let str_result2: network_p2p_core::Result<String, NetRpcError> =
bcs_ext::from_bytes(bytes.as_slice()).unwrap();
println!("result:{:?}", str_result2);
assert_eq!(str_result, str_result2);
Expand Down

0 comments on commit 5772b32

Please sign in to comment.