forked from starcoinorg/starcoin
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[cmd] PeerWatcher: A tools for find and monitor peer on peer to peer …
…network. (starcoinorg#2409)
- Loading branch information
1 parent
095e6b0
commit ca10739
Showing
10 changed files
with
166 additions
and
25 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
[package] | ||
name = "starcoin-peer-watcher" | ||
version = "1.0.0-beta.4" | ||
authors = ["Starcoin Core Dev <[email protected]>"] | ||
license = "Apache-2.0" | ||
publish = false | ||
edition = "2018" | ||
|
||
[[bin]] | ||
name = "starcoin_peer_watcher" | ||
path = "src/main.rs" | ||
|
||
[dependencies] | ||
anyhow = "1.0.40" | ||
structopt = "0.3.21" | ||
async-std = "1.9" | ||
futures = "0.3.12" | ||
starcoin-crypto = { path = "../../commons/crypto"} | ||
starcoin-config = { path = "../../config"} | ||
starcoin-genesis = { path = "../../genesis"} | ||
starcoin-types = { path = "../../types"} | ||
starcoin-logger = { path = "../../commons/logger" } | ||
network-p2p-types = {path = "../../network-p2p/types"} | ||
network-p2p = {path = "../../network-p2p"} | ||
starcoin-network = {path = "../../network"} | ||
starcoin-storage = {path = "../../storage"} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
## PeerWatcher | ||
|
||
A tools for find and monitor peer on peer to peer network. | ||
|
||
### Usage | ||
|
||
|
||
|
||
```bash | ||
$ .target/release/starcoin_peer_watcher -n barnard -d /tmp/starcoin --max-outgoing-peers 1024 --max-incoming-peers 1024 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// Copyright (c) The Starcoin Core Contributors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
use anyhow::Result; | ||
use network_p2p::NetworkWorker; | ||
use starcoin_config::{ChainNetwork, NetworkConfig}; | ||
use starcoin_network::{build_network_worker, NotificationMessage}; | ||
use starcoin_storage::storage::StorageInstance; | ||
use starcoin_storage::Storage; | ||
use starcoin_types::peer_info::PeerInfo; | ||
use std::sync::Arc; | ||
|
||
pub fn build_lighting_network( | ||
net: &ChainNetwork, | ||
network_config: &NetworkConfig, | ||
) -> Result<(PeerInfo, NetworkWorker)> { | ||
let genesis = starcoin_genesis::Genesis::load(net)?; | ||
let storage = Arc::new(Storage::new(StorageInstance::new_cache_instance())?); | ||
let chain_info = genesis.execute_genesis_block(net, storage)?; | ||
build_network_worker( | ||
network_config, | ||
chain_info, | ||
NotificationMessage::protocols(), | ||
None, | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// Copyright (c) The Starcoin Core Contributors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
use futures::StreamExt; | ||
use network_p2p::Event; | ||
use starcoin_config::{NodeConfig, StarcoinOpt}; | ||
use starcoin_peer_watcher::build_lighting_network; | ||
use starcoin_types::peer_info::PeerInfo; | ||
use structopt::StructOpt; | ||
|
||
/// A lighting node, connect to peer to peer network, and monitor peers. | ||
fn main() { | ||
let _logger = starcoin_logger::init(); | ||
let opt: StarcoinOpt = StarcoinOpt::from_args(); | ||
let config = NodeConfig::load_with_opt(&opt).unwrap(); | ||
let (peer_info, worker) = build_lighting_network(config.net(), &config.network).unwrap(); | ||
println!("Self peer_info: {:?}", peer_info); | ||
let service = worker.service().clone(); | ||
async_std::task::spawn(worker); | ||
let stream = service.event_stream("peer_watcher"); | ||
futures::executor::block_on(async move { | ||
stream | ||
.filter_map(|event| async move { | ||
match event { | ||
Event::NotificationStreamOpened { | ||
remote, | ||
protocol: _, | ||
info, | ||
notif_protocols, | ||
rpc_protocols, | ||
} => Some(PeerInfo::new( | ||
remote.into(), | ||
*info, | ||
notif_protocols, | ||
rpc_protocols, | ||
)), | ||
_ => None, | ||
} | ||
}) | ||
.for_each(|peer| async move { | ||
//TODO save peer info to database or post to a webhook | ||
// get peer's more info from network state, such as ip address, version etc. | ||
println!("Find peer: {:?}", peer) | ||
}) | ||
.await; | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters