Skip to content

Commit

Permalink
feat(octez): serialise baker config
Browse files Browse the repository at this point in the history
  • Loading branch information
huancheng-trili committed Nov 20, 2024
1 parent 87a0da1 commit ecd8303
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 3 deletions.
50 changes: 48 additions & 2 deletions crates/octez/src/async/baker.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use anyhow::{anyhow, Result};
use serde::Serialize;
use serde_with::SerializeDisplay;
use std::{fmt::Display, path::PathBuf};
use tokio::process::{Child, Command};

use super::{endpoint::Endpoint, protocol::Protocol};

#[derive(PartialEq, Debug, Clone)]
#[derive(PartialEq, Debug, Clone, SerializeDisplay)]
pub enum BakerBinaryPath {
Env(Protocol), // The binary exists in $PATH
Custom(PathBuf), // The binary is at the given path
Expand All @@ -25,7 +27,7 @@ impl Display for BakerBinaryPath {
}
}

#[derive(Clone)]
#[derive(Clone, Serialize)]
pub struct OctezBakerConfig {
binary_path: BakerBinaryPath,
octez_client_base_dir: PathBuf,
Expand Down Expand Up @@ -94,6 +96,8 @@ impl OctezBaker {

#[cfg(test)]
mod test {
use std::str::FromStr;

use super::*;
use crate::r#async::endpoint::Endpoint;
use http::Uri;
Expand Down Expand Up @@ -126,4 +130,46 @@ mod test {
.build();
assert!(config.is_err_and(|e| e.to_string().contains("binary path not set")));
}

#[test]
fn serialize_baker_path() {
assert_eq!(
serde_json::to_string(&BakerBinaryPath::Env(Protocol::Alpha)).unwrap(),
"\"octez-baker-alpha\""
);

assert_eq!(
serde_json::to_string(&BakerBinaryPath::Env(Protocol::ParisC)).unwrap(),
"\"octez-baker-PsParisC\""
);

assert_eq!(
serde_json::to_string(&BakerBinaryPath::Custom(
PathBuf::from_str("/foo/bar").unwrap()
))
.unwrap(),
"\"/foo/bar\""
);
}

#[test]
fn serialize_config() {
let base_dir = TempDir::new().unwrap();
let endpoint =
Endpoint::try_from(Uri::from_static("http://localhost:8732")).unwrap();
let config = OctezBakerConfigBuilder::new()
.set_binary_path(BakerBinaryPath::Env(Protocol::Alpha))
.set_octez_client_base_dir(base_dir.path().to_str().unwrap())
.set_octez_node_endpoint(&endpoint)
.build()
.unwrap();
assert_eq!(
serde_json::to_value(&config).unwrap(),
serde_json::json!({
"octez_client_base_dir": base_dir.path().to_string_lossy(),
"octez_node_endpoint": "http://localhost:8732",
"binary_path": "octez-baker-alpha"
})
)
}
}
17 changes: 16 additions & 1 deletion crates/octez/src/async/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use super::bootstrap::{BootstrapAccounts, BootstrapContracts, BootstrapSmartRoll

use rust_embed::Embed;
use serde_json::Value;
use serde_with::SerializeDisplay;
use std::fmt::Display;
use std::io::{Read, Seek, Write};
use std::path::{Path, PathBuf};
Expand Down Expand Up @@ -39,7 +40,7 @@ impl Display for ProtocolConstants {
}
}

#[derive(PartialEq, Eq, Debug, Clone)]
#[derive(PartialEq, Eq, Debug, Clone, SerializeDisplay)]
pub enum Protocol {
Alpha,
ParisC,
Expand All @@ -52,6 +53,12 @@ impl Default for Protocol {
}
}

impl Display for Protocol {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.hash())
}
}

impl Protocol {
pub fn hash(&self) -> &'static str {
match self {
Expand Down Expand Up @@ -702,4 +709,12 @@ mod tests {
assert_eq!(rollups.first().unwrap(), &first_rollup);
assert_eq!(rollups.last().unwrap(), &second_rollup);
}

#[test]
fn serialize_protocol() {
assert_eq!(
serde_json::to_string(&Protocol::Alpha).unwrap(),
"\"ProtoALphaALphaALphaALphaALphaALphaALphaALphaDdp3zK\""
)
}
}

0 comments on commit ecd8303

Please sign in to comment.