Skip to content

Commit

Permalink
Merge pull request #29 from gonzalezzfelipe/chore/add-clippy
Browse files Browse the repository at this point in the history
chore: Add clippy
  • Loading branch information
Quantumplation authored Nov 13, 2024
2 parents 00e6a03 + 990dbca commit 09e16f3
Show file tree
Hide file tree
Showing 20 changed files with 110 additions and 119 deletions.
25 changes: 25 additions & 0 deletions .github/workflows/clippy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: Clippy

on:
push:
branches:
- main
paths:
- ".github/workflows/clippy.yml"
- "crates/**"
pull_request:
branches:
- main
paths:
- ".github/workflows/clippy.yml"
- "crates/**"

jobs:
lint:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3

- name: Clippy check lints
run: cargo clippy -- -D warnings
6 changes: 4 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
resolver = "2"

[workspace]
members = ["crates/rpc", "crates/operator"]
resolver = "2"

[profile.release]
debug = true
3 changes: 0 additions & 3 deletions crates/operator/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,3 @@ lazy_static = "1.5.0"
tracing-subscriber = "0.3.18"
reqwest = "0.12.9"
prometheus-parse = "0.2.5"

[profile.release]
debug = true
8 changes: 1 addition & 7 deletions crates/operator/src/custom_resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,13 +258,7 @@ impl HydraDoomNode {
..Default::default()
},
]),
resources: Some(
self.spec
.resources
.clone()
.unwrap_or(Default::default())
.into(),
),
resources: Some(self.spec.resources.clone().unwrap_or_default().into()),
..Default::default()
},
Container {
Expand Down
3 changes: 0 additions & 3 deletions crates/rpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,3 @@ kube = { version = "0.96.0", features = ["client", "derive", "runtime"] }
schemars = "0.8.21"
rocket-errors = "0.1.0"
rand = "0.8.5"

[profile.release]
debug = true
44 changes: 19 additions & 25 deletions crates/rpc/src/bin/metric_exporter/metrics.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use std::sync::Mutex;

use prometheus::{histogram_opts, linear_buckets, Encoder, Histogram, HistogramTimer, IntCounter, IntGauge, Registry, TextEncoder};
use prometheus::{
histogram_opts, linear_buckets, Encoder, Histogram, HistogramTimer, IntCounter, IntGauge,
Registry, TextEncoder,
};

pub enum NodeState {
Offline,
Expand All @@ -9,9 +12,9 @@ pub enum NodeState {
HeadIsOpen,
}

impl Into<i64> for NodeState {
fn into(self) -> i64 {
match self {
impl From<NodeState> for i64 {
fn from(value: NodeState) -> Self {
match value {
NodeState::Offline => 0,
NodeState::Online => 1,
NodeState::HeadIsInitializing => 2,
Expand All @@ -27,9 +30,9 @@ pub enum GameState {
Done,
}

impl Into<i64> for GameState {
fn into(self) -> i64 {
match self {
impl From<GameState> for i64 {
fn from(value: GameState) -> Self {
match value {
GameState::Waiting => 0,
GameState::Lobby => 1,
GameState::Running => 2,
Expand Down Expand Up @@ -77,7 +80,7 @@ impl Metrics {

let bytes = IntCounter::new(
"hydra_doom_node_bytes",
"Number of bytes in executed transactions."
"Number of bytes in executed transactions.",
)
.unwrap();

Expand All @@ -87,13 +90,11 @@ impl Metrics {
)
.unwrap();

let games_seconds = Histogram::with_opts(
histogram_opts!(
"hydra_doom_games_seconds",
"Duration of games in seconds.",
linear_buckets(0.0, 60.0, 20)?,
)
)
let games_seconds = Histogram::with_opts(histogram_opts!(
"hydra_doom_games_seconds",
"Duration of games in seconds.",
linear_buckets(0.0, 60.0, 20)?,
))
.unwrap();

let players_total = IntCounter::new(
Expand All @@ -108,17 +109,10 @@ impl Metrics {
)
.unwrap();

let kills = IntCounter::new(
"hydra_doom_kills",
"Number of kills in the game.",
)
.unwrap();
let kills = IntCounter::new("hydra_doom_kills", "Number of kills in the game.").unwrap();

let suicides = IntCounter::new(
"hydra_doom_suicides",
"Number of suicides in the game.",
)
.unwrap();
let suicides =
IntCounter::new("hydra_doom_suicides", "Number of suicides in the game.").unwrap();

let registry = Registry::default();
registry.register(Box::new(state.clone()))?;
Expand Down
4 changes: 1 addition & 3 deletions crates/rpc/src/bin/open_head.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,7 @@ async fn main() {
1,
)
.into(),
init_tx
.make_initial_output(Hash::from(head_id.as_slice()), participant_hash.clone())
.into(),
init_tx.make_initial_output(Hash::from(head_id.as_slice()), participant_hash.clone()),
Hash::from(participant_hash.as_slice()),
),
blueprint_tx: vec![(
Expand Down
5 changes: 2 additions & 3 deletions crates/rpc/src/model/cluster/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,13 @@ impl ClusterState {
self.store
.state()
.iter()
.filter(|n| n.status.as_ref().is_some_and(|s| s.state == "HeadIsOpen"))
.next()
.find(|n| n.status.as_ref().is_some_and(|s| s.state == "HeadIsOpen"))
.cloned()
.ok_or(anyhow::anyhow!("no available warm nodes found"))
}

pub fn get_all_nodes(&self) -> Vec<Arc<crd::HydraDoomNode>> {
self.store.state().iter().cloned().collect()
self.store.state().to_vec()
}

pub fn get_node_by_id(&self, id: &str) -> Option<Arc<HydraDoomNode>> {
Expand Down
2 changes: 1 addition & 1 deletion crates/rpc/src/model/cluster/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ impl NodeClient {
)
.expect("Failed to decode player address")
{
Address::Shelley(shelley) => shelley.payment().as_hash().clone(),
Address::Shelley(shelley) => *shelley.payment().as_hash(),
_ => panic!("Expected Shelley address"),
};

Expand Down
26 changes: 13 additions & 13 deletions crates/rpc/src/model/game/contract/game_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ pub struct PaymentCredential([u8; 28]);

#[derive(Debug)]
pub enum State {
RUNNING,
CHEATED,
FINISHED,
Running,
Cheated,
Finished,
}
#[derive(Debug)]
pub struct GameState {
Expand All @@ -33,7 +33,7 @@ impl GameState {
Self {
referee,
players: Vec::new(),
state: State::RUNNING,
state: State::Running,
winner: None,
cheater: None,
}
Expand Down Expand Up @@ -212,9 +212,9 @@ impl From<PaymentKeyHash> for PaymentCredential {
}
}

impl Into<Hash<28>> for PaymentCredential {
fn into(self) -> Hash<28> {
self.0.into()
impl From<PaymentCredential> for Hash<28> {
fn from(value: PaymentCredential) -> Hash<28> {
value.0.into()
}
}

Expand Down Expand Up @@ -271,17 +271,17 @@ impl TryFrom<PlutusData> for PaymentCredential {
impl From<State> for PlutusData {
fn from(value: State) -> Self {
PlutusData::Constr(match value {
State::RUNNING => Constr {
State::Running => Constr {
tag: 121,
any_constructor: None,
fields: alonzo::MaybeIndefArray::Def(vec![]),
},
State::CHEATED => Constr {
State::Cheated => Constr {
tag: 122,
any_constructor: None,
fields: alonzo::MaybeIndefArray::Def(vec![]),
},
State::FINISHED => Constr {
State::Finished => Constr {
tag: 123,
any_constructor: None,
fields: alonzo::MaybeIndefArray::Def(vec![]),
Expand All @@ -296,9 +296,9 @@ impl TryFrom<PlutusData> for State {
fn try_from(value: PlutusData) -> Result<Self, Self::Error> {
match value {
PlutusData::Constr(constr) => match constr.tag {
121 => Ok(State::RUNNING),
122 => Ok(State::CHEATED),
123 => Ok(State::FINISHED),
121 => Ok(State::Running),
122 => Ok(State::Cheated),
123 => Ok(State::Finished),
_ => bail!("Invalid constructor tag for State."),
},
_ => bail!("Invalid data type for State."),
Expand Down
12 changes: 6 additions & 6 deletions crates/rpc/src/model/hydra/contract/hydra_validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,18 @@ impl HydraValidator {
}

// I feel OK with an expect here, as if we have invalid script cbor encoding, it's because we have a bug in the codebase
impl Into<PlutusScript<2>> for HydraValidator {
fn into(self) -> PlutusScript<2> {
impl From<HydraValidator> for PlutusScript<2> {
fn from(value: HydraValidator) -> PlutusScript<2> {
PlutusScript(
hex::decode(self.cbor())
hex::decode(value.cbor())
.expect("invalid script cbor hex string")
.into(),
)
}
}

impl Into<Vec<u8>> for HydraValidator {
fn into(self) -> Vec<u8> {
hex::decode(self.cbor()).expect("invalid script cbor hex string")
impl From<HydraValidator> for Vec<u8> {
fn from(value: HydraValidator) -> Vec<u8> {
hex::decode(value.cbor()).expect("invalid script cbor hex string")
}
}
32 changes: 10 additions & 22 deletions crates/rpc/src/model/hydra/hydra_socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,17 +172,11 @@ pub async fn sample_txs(url: &str, count: usize, timeout: Duration) -> Result<Ve
let next = receiver.next().await.context("failed to receive")??;
let msg = HydraMessage::try_from(next).context("failed to parse hydra message")?;

match msg {
HydraMessage::HydraEvent(x) => match x {
HydraEventMessage::TxValid(tx) => {
transactions.push(tx);
if transactions.len() == count {
break;
}
}
_ => {}
},
_ => {}
if let HydraMessage::HydraEvent(HydraEventMessage::TxValid(tx)) = msg {
transactions.push(tx);
if transactions.len() == count {
break;
}
}
}

Expand Down Expand Up @@ -216,17 +210,11 @@ pub async fn submit_tx_roundtrip(url: &str, tx: NewTx, timeout: Duration) -> Res
let next = receiver.next().await.context("failed to receive")?;
let msg = HydraMessage::try_from(next?).context("failed to parse hydra message")?;

match msg {
HydraMessage::HydraEvent(x) => match x {
HydraEventMessage::TxValid(x) => {
if x.tx_id == tx_id {
info!("Tx confirmed: {:?}", x);
break anyhow::Result::Ok(());
}
}
_ => (),
},
_ => {}
if let HydraMessage::HydraEvent(HydraEventMessage::TxValid(x)) = msg {
if x.tx_id == tx_id {
info!("Tx confirmed: {:?}", x);
break anyhow::Result::Ok(());
}
}
}
});
Expand Down
4 changes: 2 additions & 2 deletions crates/rpc/src/model/hydra/tx/commit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ impl CommitTx {
.reference_input(self.script_registry.initial_reference.clone().into())
.collateral_input(
self.blueprint_tx
.get(0)
.first()
.ok_or(anyhow!(
"need at least one blueprint tx input for collateral"
))?
Expand Down Expand Up @@ -163,7 +163,7 @@ fn build_base_commit_output(outputs: Vec<Output>, network_id: u8) -> Result<Outp
for (policy, assets) in output_assets.iter() {
for (name, amount) in assets {
commit_output = commit_output
.add_asset(policy.0.into(), name.0.clone(), amount.clone())
.add_asset(policy.0.into(), name.0.clone(), *amount)
.context("Failed to add asset to commit output")?;
}
}
Expand Down
16 changes: 8 additions & 8 deletions crates/rpc/src/model/hydra/tx/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ impl TryFrom<String> for InputWrapper {
}
}

impl Into<PlutusData> for InputWrapper {
fn into(self) -> PlutusData {
impl From<InputWrapper> for PlutusData {
fn from(value: InputWrapper) -> PlutusData {
PlutusData::Constr(Constr {
tag: 121,
any_constructor: None,
Expand All @@ -57,16 +57,16 @@ impl Into<PlutusData> for InputWrapper {
tag: 121,
any_constructor: None,
fields: MaybeIndefArray::Indef(vec![PlutusData::BoundedBytes(
alonzo::BoundedBytes::from(self.inner.tx_hash.0.to_vec()),
alonzo::BoundedBytes::from(value.inner.tx_hash.0.to_vec()),
)]),
}),
PlutusData::BigInt(alonzo::BigInt::Int((self.inner.txo_index as i64).into())),
PlutusData::BigInt(alonzo::BigInt::Int((value.inner.txo_index as i64).into())),
]),
})
}
}
impl Into<PlutusData> for &InputWrapper {
fn into(self) -> PlutusData {
impl From<&InputWrapper> for PlutusData {
fn from(value: &InputWrapper) -> PlutusData {
PlutusData::Constr(Constr {
tag: 121,
any_constructor: None,
Expand All @@ -75,10 +75,10 @@ impl Into<PlutusData> for &InputWrapper {
tag: 121,
any_constructor: None,
fields: MaybeIndefArray::Indef(vec![PlutusData::BoundedBytes(
alonzo::BoundedBytes::from(self.inner.tx_hash.0.to_vec()),
alonzo::BoundedBytes::from(value.inner.tx_hash.0.to_vec()),
)]),
}),
PlutusData::BigInt(alonzo::BigInt::Int((self.inner.txo_index as i64).into())),
PlutusData::BigInt(alonzo::BigInt::Int((value.inner.txo_index as i64).into())),
]),
})
}
Expand Down
Loading

0 comments on commit 09e16f3

Please sign in to comment.