Skip to content

Commit c24ac04

Browse files
committed
chore: use shared api types to deserialize
1 parent 9c88937 commit c24ac04

File tree

6 files changed

+58
-49
lines changed

6 files changed

+58
-49
lines changed

crates/rpc/src/bin/metric_exporter/routes/game/add_player.rs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,17 @@
1-
use hydra_control_plane_rpc::model::cluster::{ConnectionInfo, NodeClient};
1+
use hydra_control_plane_rpc::model::cluster::{
2+
shared::AddPlayerLocalResponse, ConnectionInfo, NodeClient,
3+
};
24
use pallas::ledger::addresses::Address;
35
use rocket::{get, http::Status, serde::json::Json, State};
4-
use serde::Serialize;
56
use tracing::error;
67

78
use crate::LocalState;
89

9-
#[derive(Serialize)]
10-
pub struct AddPlayerResponse {
11-
player_state: String,
12-
admin_pkh: String,
13-
}
14-
1510
#[get("/game/add_player?<address>")]
1611
pub async fn add_player(
1712
address: &str,
1813
state: &State<LocalState>,
19-
) -> Result<Json<AddPlayerResponse>, Status> {
14+
) -> Result<Json<AddPlayerLocalResponse>, Status> {
2015
let pkh = match Address::from_bech32(address).map_err(|_| Status::BadRequest)? {
2116
Address::Shelley(shelley) => Ok(*shelley.payment().as_hash()),
2217
_ => Err(Status::BadRequest),
@@ -34,7 +29,7 @@ pub async fn add_player(
3429
.inspect_err(|err| error!("error adding player: {}", err))
3530
.map_err(|_| Status::InternalServerError)?;
3631

37-
Ok(Json(AddPlayerResponse {
32+
Ok(Json(AddPlayerLocalResponse {
3833
player_state: format!("{}#1", hex::encode(tx_hash)),
3934
admin_pkh: hex::encode(client.tx_builder.admin_pkh),
4035
}))

crates/rpc/src/bin/metric_exporter/routes/game/new_game.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,19 @@
11
use anyhow::{anyhow, Context};
2-
use hydra_control_plane_rpc::model::cluster::{ConnectionInfo, NodeClient};
2+
use hydra_control_plane_rpc::model::cluster::{
3+
shared::NewGameLocalResponse, ConnectionInfo, NodeClient,
4+
};
35
use pallas::ledger::addresses::Address;
46
use rocket::{get, serde::json::Json, State};
57
use rocket_errors::anyhow::Result;
6-
use serde::Serialize;
78
use tracing::info;
89

910
use crate::LocalState;
1011

11-
#[derive(Serialize)]
12-
pub struct NewGameResponse {
13-
player_state: String,
14-
admin_pkh: String,
15-
}
16-
1712
#[get("/game/new_game?<address>")]
18-
pub async fn new_game(address: &str, state: &State<LocalState>) -> Result<Json<NewGameResponse>> {
13+
pub async fn new_game(
14+
address: &str,
15+
state: &State<LocalState>,
16+
) -> Result<Json<NewGameLocalResponse>> {
1917
info!("Creating a new game for {}", address);
2018

2119
let pkh = match Address::from_bech32(address).context("invalid address")? {
@@ -34,7 +32,7 @@ pub async fn new_game(address: &str, state: &State<LocalState>) -> Result<Json<N
3432
.await
3533
.context("error creating new game")?;
3634

37-
Ok(Json(NewGameResponse {
35+
Ok(Json(NewGameLocalResponse {
3836
player_state: format!("{}#1", hex::encode(tx_hash)),
3937
admin_pkh: hex::encode(client.tx_builder.admin_pkh),
4038
}))

crates/rpc/src/model/cluster/mod.rs

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use serde::Deserialize;
1212

1313
mod crd;
1414
mod node;
15+
pub mod shared;
1516

1617
pub use crd::*;
1718
pub use node::*;
@@ -68,7 +69,11 @@ impl ClusterState {
6869
let watcher_handle = tokio::spawn(async move {
6970
let infinite_watch = rf.applied_objects().for_each(|node| {
7071
if let Ok(node) = node {
71-
if node.status.as_ref().is_some_and(|n| n.game_state != "Waiting") {
72+
if node
73+
.status
74+
.as_ref()
75+
.is_some_and(|n| n.game_state != "Waiting")
76+
{
7277
let id = node.metadata.name.as_ref().unwrap();
7378
let mut claims = claims.lock().unwrap();
7479
if claims.remove(id).is_some() {
@@ -93,23 +98,36 @@ impl ClusterState {
9398

9499
pub fn select_node_for_new_game(&self) -> anyhow::Result<Arc<HydraDoomNode>> {
95100
let mut claimed = self.recently_claimed.lock().unwrap();
96-
let node = self.store
101+
let node = self
102+
.store
97103
.state()
98104
.iter()
99105
.filter(|n| {
100106
let id = n.metadata.name.as_ref().unwrap();
101107
let recently_claimed = claimed.get(id).unwrap_or(&false);
102-
info!("checking node {}, recently claimed: {}, status: {}", id, recently_claimed, n.status.as_ref().map(|s| s.game_state.as_str()).unwrap_or("unknown"));
108+
info!(
109+
"checking node {}, recently claimed: {}, status: {}",
110+
id,
111+
recently_claimed,
112+
n.status
113+
.as_ref()
114+
.map(|s| s.game_state.as_str())
115+
.unwrap_or("unknown")
116+
);
103117
if let Some(status) = n.status.as_ref() {
104-
!recently_claimed && status.node_state == "HeadIsOpen" && status.game_state == "Waiting"
118+
!recently_claimed
119+
&& status.node_state == "HeadIsOpen"
120+
&& status.game_state == "Waiting"
105121
} else {
106122
false
107123
}
108124
})
109125
.max_by_key(|n| n.metadata.creation_timestamp.clone())
110126
.cloned()
111127
.ok_or(anyhow::anyhow!("no available nodes found"))?;
112-
claimed.entry(node.metadata.name.clone().expect("node without a name")).or_insert(true);
128+
claimed
129+
.entry(node.metadata.name.clone().expect("node without a name"))
130+
.or_insert(true);
113131
Ok(node)
114132
}
115133

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
use serde::{Deserialize, Serialize};
2+
3+
#[derive(Serialize, Deserialize, Debug)]
4+
pub struct NewGameLocalResponse {
5+
pub player_state: String,
6+
pub admin_pkh: String,
7+
}
8+
9+
#[derive(Serialize, Deserialize, Debug)]
10+
pub struct AddPlayerLocalResponse {
11+
pub player_state: String,
12+
pub admin_pkh: String,
13+
}

crates/rpc/src/routes/add_player.rs

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
use std::collections::HashMap;
2-
31
use rocket::{get, http::Status, serde::json::Json, State};
42
use serde::Serialize;
53

6-
use crate::model::cluster::ClusterState;
4+
use crate::model::cluster::{shared::AddPlayerLocalResponse, ClusterState};
75

86
#[derive(Serialize)]
97
pub struct AddPlayerResponse {
@@ -35,19 +33,13 @@ pub async fn add_player(
3533
let response = reqwest::get(url).await.map_err(|_| Status::BadGateway)?;
3634

3735
let body = response
38-
.json::<HashMap<String, String>>()
36+
.json::<AddPlayerLocalResponse>()
3937
.await
4038
.map_err(|_| Status::InternalServerError)?;
4139

4240
Ok(Json(AddPlayerResponse {
4341
ip: external_url,
44-
player_state: body
45-
.get("player_state")
46-
.ok_or(Status::InternalServerError)?
47-
.to_owned(),
48-
admin_pkh: body
49-
.get("admin_pkh")
50-
.ok_or(Status::InternalServerError)?
51-
.to_owned(),
42+
player_state: body.player_state,
43+
admin_pkh: body.admin_pkh,
5244
}))
5345
}

crates/rpc/src/routes/new_game.rs

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
use std::collections::HashMap;
2-
31
use anyhow::Context;
42
use rocket::{get, serde::json::Json, State};
53
use rocket_errors::anyhow::Result;
64
use serde::Serialize;
75
use tracing::info;
86

9-
use crate::model::cluster::ClusterState;
7+
use crate::model::cluster::{shared::NewGameLocalResponse, ClusterState};
108

119
#[derive(Serialize)]
1210
pub struct NewGameResponse {
@@ -41,21 +39,16 @@ pub async fn new_game(address: &str, state: &State<ClusterState>) -> Result<Json
4139
let response = reqwest::get(url)
4240
.await
4341
.context("failed to hit new_game metrics server endpoint")?;
42+
4443
let body = response
45-
.json::<HashMap<String, String>>()
44+
.json::<NewGameLocalResponse>()
4645
.await
4746
.context("http error")?;
4847

4948
Ok(Json(NewGameResponse {
5049
game_id: node_id,
5150
ip: external_url,
52-
player_state: body
53-
.get("player_state")
54-
.context("missing player_state in response")?
55-
.to_owned(),
56-
admin_pkh: body
57-
.get("admin_pkh")
58-
.context("missing admin_pkh in response")?
59-
.to_owned(),
51+
player_state: body.player_state,
52+
admin_pkh: body.admin_pkh,
6053
}))
6154
}

0 commit comments

Comments
 (0)