Skip to content

Commit

Permalink
style: Apply rust-fmt (#101)
Browse files Browse the repository at this point in the history
* style: Apply rust-fmt
* style: Fix headers
* ci: Use nightly for:
  - cargo fmt
  - cargo clippy
  • Loading branch information
mark-stopka committed Feb 8, 2022
1 parent d48de16 commit e158ff6
Show file tree
Hide file tree
Showing 19 changed files with 682 additions and 456 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/validate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,11 @@ jobs:
- name: Checkout sources
uses: actions/checkout@v2

- name: Install stable toolchain
- name: Install nightly toolchain
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable
toolchain: nightly
override: true
components: rustfmt, clippy

Expand Down
50 changes: 30 additions & 20 deletions examples/blockfetch.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
/**
© 2020 - 2022 PERLUR Group
Re-licensed under MPLv2
© 2022 PERLUR Group
SPDX-License-Identifier: MPL-2.0
*/
//
// © 2020 - 2022 PERLUR Group
//
// Re-licenses under MPLv2
// © 2022 PERLUR Group
//
// SPDX-License-Identifier: MPL-2.0
//

use cardano_ouroboros_network::{
mux::Connection,
protocols::handshake::Handshake,
protocols::blockfetch::BlockFetch,
protocols::handshake::Handshake,
};

use std::sync::Arc;
Expand All @@ -22,13 +21,16 @@ use pallas::ledger::alonzo::{
};

use oura::{
sources::MagicArg,
utils::{Utils, WithUtils},
mapper::EventWriter,
mapper::Config,
mapper::ChainWellKnownInfo,
pipelining::SinkProvider,
mapper::Config,
mapper::EventWriter,
pipelining::new_inter_stage_channel,
pipelining::SinkProvider,
sources::MagicArg,
utils::{
Utils,
WithUtils,
},
};

mod common;
Expand All @@ -43,12 +45,19 @@ async fn blockfetch(host: &String, magic: u32) -> Result<(), Box<dyn std::error:
.node_to_node()
.network_magic(magic)
.build()?
.run(&mut connection).await?;
.run(&mut connection)
.await?;

let mut blockfetch = BlockFetch::builder()
.first(26249860, hex::decode("915386f44ad3a7fccee949c9d3fe43f5a20459c7401f990e1cc7d52c10be1fd6")?)
.last(26250057, hex::decode("5fec758c8aaff4a7683c27b075dc3984d8d982839cc56470a682d1411c9f8198")?)
.build()?;
.first(
26249860,
hex::decode("915386f44ad3a7fccee949c9d3fe43f5a20459c7401f990e1cc7d52c10be1fd6")?,
)
.last(
26250057,
hex::decode("5fec758c8aaff4a7683c27b075dc3984d8d982839cc56470a682d1411c9f8198")?,
)
.build()?;
let mut blocks = blockfetch.run(&mut connection).await?;

let (tx, rx) = new_inter_stage_channel(None);
Expand All @@ -60,7 +69,8 @@ async fn blockfetch(host: &String, magic: u32) -> Result<(), Box<dyn std::error:
let well_known = ChainWellKnownInfo::try_from_magic(*MagicArg::default()).unwrap();
let utils = Arc::new(Utils::new(well_known, None));
let writer = EventWriter::standalone(tx, None, config);
let sink_handle = WithUtils::new(oura::sinks::terminal::Config::default(), utils).bootstrap(rx)?;
let sink_handle =
WithUtils::new(oura::sinks::terminal::Config::default(), utils).bootstrap(rx)?;

while let Some(block) = blocks.next().await? {
let block = BlockWrapper::decode_fragment(&block[..])?;
Expand Down
17 changes: 8 additions & 9 deletions examples/common.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
/**
© 2020 - 2022 PERLUR Group
Re-licensed under MPLv2
© 2022 PERLUR Group
SPDX-License-Identifier: MPL-2.0
*/
//
// © 2020 - 2022 PERLUR Group
//
// Re-licenses under MPLv2
// © 2022 PERLUR Group
//
// SPDX-License-Identifier: MPL-2.0
//

use std::path::PathBuf;

Expand Down
20 changes: 10 additions & 10 deletions examples/local.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
/**
© 2020 - 2022 PERLUR Group
Re-licensed under MPLv2
© 2022 PERLUR Group
SPDX-License-Identifier: MPL-2.0
*/
//
// © 2020 - 2022 PERLUR Group
//
// Re-licenses under MPLv2
// © 2022 PERLUR Group
//
// SPDX-License-Identifier: MPL-2.0
//

use cardano_ouroboros_network::{
mux::Connection,
Expand Down Expand Up @@ -34,7 +33,8 @@ async fn local(magic: u32) -> Result<(), Box<dyn std::error::Error>> {
.client_to_node()
.network_magic(magic)
.build()?
.run(&mut connection).await?;
.run(&mut connection)
.await?;

info!("Ping UNIX socket success");
Ok(())
Expand Down
39 changes: 24 additions & 15 deletions examples/ping.rs
Original file line number Diff line number Diff line change
@@ -1,39 +1,42 @@
/**
© 2020 - 2022 PERLUR Group
Re-licensed under MPLv2
© 2022 PERLUR Group
SPDX-License-Identifier: MPL-2.0
*/
//
// © 2020 - 2022 PERLUR Group
//
// Re-licenses under MPLv2
// © 2022 PERLUR Group
//
// SPDX-License-Identifier: MPL-2.0
//

use cardano_ouroboros_network::{
mux::Connection,
protocols::handshake::Handshake,
};
use futures::future::join_all;
use log::{
error,
info,
};
use std::{
env,
time::Duration,
};
use log::{info, error};
use futures::future::join_all;

mod common;

async fn ping(host: &String, magic: u32) -> Result<(Duration, Duration), String> {
info!("Pinging host {} magic {}.", host, magic);
let mut connection = match Connection::tcp_connect(&host).await {
Ok(connection) => connection,
Err(_) => { return Err("Could not connect.".to_string()) }
Err(_) => return Err("Could not connect.".to_string()),
};
let connect_duration = connection.duration();
Handshake::builder()
.client()
.node_to_node()
.network_magic(magic)
.build()?
.run(&mut connection).await?;
.run(&mut connection)
.await?;
let total_duration = connection.duration();
Ok((connect_duration, total_duration))
}
Expand All @@ -56,12 +59,18 @@ async fn main() {
async move {
match ping(&host.clone(), cfg.magic).await {
Ok((connect_duration, total_duration)) => {
info!("Ping {} success! : connect_duration: {}, total_duration: {}", &host, connect_duration.as_millis(), total_duration.as_millis());
info!(
"Ping {} success! : connect_duration: {}, total_duration: {}",
&host,
connect_duration.as_millis(),
total_duration.as_millis()
);
}
Err(error) => {
error!("Ping {} failed! : {:?}", &host, error);
}
}
}
})).await;
}))
.await;
}
91 changes: 60 additions & 31 deletions examples/pooltool.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,37 @@
/**
© 2020 PERLUR Group
Re-licensed under MPLv2
© 2022 PERLUR Group
SPDX-License-Identifier: MPL-2.0
*/
//
// © 2020 - 2022 PERLUR Group
//
// Re-licenses under MPLv2
// © 2022 PERLUR Group
//
// SPDX-License-Identifier: MPL-2.0
//

use cardano_ouroboros_network::{
BlockHeader,
protocols::chainsync::Listener,
BlockHeader,
};
use chrono::{
SecondsFormat,
Utc,
};
use log::{
error,
info,
};
use std::path::PathBuf;
use log::{info, error};
use serde::Serialize;
use regex::Regex;
use chrono::{SecondsFormat, Utc};
use serde::Serialize;
use std::path::PathBuf;
use std::{
process::{Command, Stdio},
time::{Duration, Instant},
ops::Sub,
process::{
Command,
Stdio,
},
time::{
Duration,
Instant,
},
};

struct PoolTool {
Expand Down Expand Up @@ -78,15 +89,23 @@ impl Listener for PoolTool {
.output()
.expect(&*format!("Failed to execute {:?}", &self.cardano_node_path));
let version_string = String::from_utf8_lossy(&output.stdout);
let cap = Regex::new("cardano-node (\\d+\\.\\d+\\.\\d+) .*\ngit rev ([a-f0-9]{5}).*").unwrap().captures(&*version_string).unwrap();
self.node_version = format!("{}:{}", cap.get(1).map_or("", |m| m.as_str()), cap.get(2).map_or("", |m| m.as_str()));
let cap = Regex::new("cardano-node (\\d+\\.\\d+\\.\\d+) .*\ngit rev ([a-f0-9]{5}).*")
.unwrap()
.captures(&*version_string)
.unwrap();
self.node_version = format!(
"{}:{}",
cap.get(1).map_or("", |m| m.as_str()),
cap.get(2).map_or("", |m| m.as_str())
);
info!("Checking cardano-node version: {}", &self.node_version);
self.last_node_version_time = Instant::now();
}
let client = reqwest::blocking::Client::new();
let pooltool_result = client.post("https://api.pooltool.io/v0/sendstats").body(
serde_json::ser::to_string(
&PooltoolStats {
let pooltool_result = client
.post("https://api.pooltool.io/v0/sendstats")
.body(
serde_json::ser::to_string(&PooltoolStats {
api_key: self.pooltool_api_key.clone(),
pool_id: self.pool_id.clone(),
data: PooltoolData {
Expand All @@ -100,20 +119,30 @@ impl Listener for PoolTool {
leader_vrf: hex::encode(&header.leader_vrf_0),
platform: "cncli".to_string(),
},
}
).unwrap()
).send();
})
.unwrap(),
)
.send();

match pooltool_result {
Ok(response) => {
match response.text() {
Ok(text) => {
info!("Pooltool ({}, {}): ({}, {}), json: {}", &self.pool_name, &self.pool_id[..8], &header.block_number, hex::encode(&header.hash[..8]), text);
}
Err(error) => { error!("PoolTool error: {}", error); }
Ok(response) => match response.text() {
Ok(text) => {
info!(
"Pooltool ({}, {}): ({}, {}), json: {}",
&self.pool_name,
&self.pool_id[..8],
&header.block_number,
hex::encode(&header.hash[..8]),
text
);
}
Err(error) => {
error!("PoolTool error: {}", error);
}
},
Err(error) => {
error!("PoolTool error: {}", error);
}
Err(error) => { error!("PoolTool error: {}", error); }
}
}
}
29 changes: 15 additions & 14 deletions examples/server.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
/**
© 2020 - 2022 PERLUR Group
Re-licensed under MPLv2
© 2022 PERLUR Group
SPDX-License-Identifier: MPL-2.0
*/
//
// © 2020 - 2022 PERLUR Group
//
// Re-licenses under MPLv2
// © 2022 PERLUR Group
//
// SPDX-License-Identifier: MPL-2.0
//

use cardano_ouroboros_network::{
mux::Connection,
protocols::{
handshake,
},
protocols::handshake,
};
use tokio::net::{TcpListener, TcpStream};
use log::info;
use tokio::net::{
TcpListener,
TcpStream,
};

mod common;

Expand All @@ -40,7 +40,8 @@ async fn handle(stream: TcpStream, cfg: &common::Config) -> Result<(), Error> {
.node_to_node()
.network_magic(cfg.magic)
.build()?
.run(&mut connection).await?;
.run(&mut connection)
.await?;

Ok(())
}
Expand Down
Loading

0 comments on commit e158ff6

Please sign in to comment.