Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fix] Update bincode #66

Merged
merged 5 commits into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion bothan-api/server-cli/src/commands.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
pub mod config;
pub mod start;
mod utils;
24 changes: 16 additions & 8 deletions bothan-api/server-cli/src/commands/config.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
use std::fs;
use std::path::PathBuf;

use anyhow::Context;
use clap::{Parser, Subcommand};

use crate::bothan_home_dir;
use anyhow::{anyhow, Context};
use bothan_api::config::manager::crypto_info::sources::CryptoSourceConfigs;
use bothan_api::config::AppConfig;

use crate::commands::utils::bothan_home_dir;
use clap::{Parser, Subcommand};
use tracing::info;

#[derive(Parser)]
pub struct ConfigCli {
Expand All @@ -22,18 +21,27 @@ enum ConfigSubCommand {
/// The path to where to initialize the configuration file (defaults to ./config.toml).
#[arg(short, long)]
path: Option<PathBuf>,

/// Whether to override the existing configuration file.
#[arg(short, long = "override")]
override_: bool,
},
}

impl ConfigCli {
pub async fn run(&self) -> anyhow::Result<()> {
match &self.subcommand {
ConfigSubCommand::Init { path } => {
ConfigSubCommand::Init { path, override_ } => {
let config_path = match path {
Some(p) => p.clone(),
None => bothan_home_dir().join("config.toml"),
};

//check if the file already exists
if config_path.exists() && !override_ {
return Err(anyhow!("Config file already exists at: {:?}", config_path));
}

if let Some(parent) = config_path.parent() {
fs::create_dir_all(parent).with_context(|| {
format!("Failed to create parent directories for {:?}", path)
Expand All @@ -45,8 +53,8 @@ impl ConfigCli {
let config_string =
toml::to_string(&app_config).with_context(|| "Failed to serialize config")?;

fs::write(config_path, config_string).with_context(|| "Failed to write config")?;
println!("Initialized default config at: {:?}", path);
fs::write(&config_path, config_string).with_context(|| "Failed to write config")?;
info!("initialized default config at: {:?}", config_path);
Ok(())
}
}
Expand Down
31 changes: 9 additions & 22 deletions bothan-api/server-cli/src/commands/start.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,6 @@ use std::str::FromStr;
use std::sync::Arc;

use anyhow::{anyhow, Context};
use clap::Parser;
use reqwest::header::{HeaderName, HeaderValue};
use semver::VersionReq;
use tonic::transport::Server;

use bothan_api::api::CryptoQueryServer;
use bothan_api::config::ipfs::IpfsAuthentication;
use bothan_api::config::manager::crypto_info::sources::CryptoSourceConfigs;
Expand All @@ -25,8 +20,11 @@ use bothan_core::registry::{Registry, Valid};
use bothan_core::store::SharedStore;
use bothan_core::worker::AssetWorkerBuilder;
use bothan_kraken::KrakenWorkerBuilder;

use crate::commands::utils::bothan_home_dir;
use clap::Parser;
use reqwest::header::{HeaderName, HeaderValue};
use semver::VersionReq;
use tonic::transport::Server;
use tracing::info;

#[derive(Parser)]
pub struct StartCli {
Expand All @@ -48,14 +46,7 @@ pub struct StartCli {
}

impl StartCli {
pub async fn run(&self) -> anyhow::Result<()> {
let config_path = self
.config
.clone()
.unwrap_or(bothan_home_dir().join("config.toml"));

let app_config = AppConfig::from(config_path)?;

pub async fn run(&self, app_config: AppConfig) -> anyhow::Result<()> {
let registry = match &self.registry {
Some(p) => {
let file =
Expand Down Expand Up @@ -84,15 +75,11 @@ async fn start_server(
registry: Registry<Valid>,
reset: bool,
) -> anyhow::Result<()> {
let log_level = &app_config.log.level;
tracing_subscriber::fmt()
.with_env_filter(format!("bothan_core={log_level},bothan_api={log_level}"))
.init();

let store = init_store(&app_config, registry, reset).await?;
let ipfs_client = init_ipfs_client(&app_config).await?;
let crypto_server = Arc::new(init_crypto_server(&app_config, store, ipfs_client).await?);

info!("server started");
Server::builder()
.add_service(PriceServiceServer::from_arc(crypto_server.clone()))
.add_service(SignalServiceServer::from_arc(crypto_server.clone()))
Expand Down Expand Up @@ -120,7 +107,7 @@ async fn init_store(
let mut store = SharedStore::new(registry, &config.store.path)
.await
.with_context(|| "Failed to create store")?;
println!("Store created successfully at \"{:?}\"", &config.store.path);
info!("store created successfully at \"{:?}\"", &config.store.path);

if !reset {
store
Expand Down Expand Up @@ -208,6 +195,6 @@ where
.with_context(|| format!("Failed to build worker {worker_name}"))?;

manager.add_worker(worker_name.to_string(), worker).await;
println!("Loaded {} worker", worker_name);
info!("loaded {} worker", worker_name);
Ok(())
}
6 changes: 0 additions & 6 deletions bothan-api/server-cli/src/commands/utils.rs

This file was deleted.

35 changes: 30 additions & 5 deletions bothan-api/server-cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
use std::path::PathBuf;

use clap::{Parser, Subcommand};

use bothan_api::config::AppConfig;

use crate::commands::config::ConfigCli;
use crate::commands::start::StartCli;

Expand All @@ -10,6 +14,10 @@ mod commands;
struct Cli {
#[command(subcommand)]
command: Option<Command>,

// global args
#[arg(long, global = true)]
config: Option<PathBuf>,
}

#[derive(Subcommand)]
Expand All @@ -30,13 +38,30 @@ async fn main() {
}
};

let config_path = cli
.config
.clone()
.unwrap_or(bothan_home_dir().join("config.toml"));

let app_config = AppConfig::from(config_path).expect("Failed to load config");

let log_level = &app_config.log.level;
tracing_subscriber::fmt()
.with_env_filter(format!(
"bothan_core={log_level},bothan_api={log_level},bothan={log_level}"
))
.init();

if let Some(command) = &cli.command {
if let Err(e) = match command {
match command {
Command::Config(config_cli) => config_cli.run().await,
Command::Start(start_cli) => start_cli.run().await,
} {
eprintln!("{}", e);
std::process::exit(1);
Command::Start(start_cli) => start_cli.run(app_config).await,
}
.expect("Failed to run command");
}
}

pub fn bothan_home_dir() -> PathBuf {
let home = dirs::home_dir().expect("Failed to get home directory");
home.join(".bothan")
}
14 changes: 7 additions & 7 deletions bothan-api/server/src/api/crypto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,27 +53,27 @@ impl SignalService for CryptoQueryServer {
Ok(Response::new(()))
}
Err(SetRegistryError::FailedToRetrieve(e)) => {
error!("Failed to retrieve registry: {}", e);
error!("failed to retrieve registry: {}", e);
Err(Status::not_found("Failed to retrieve registry"))
}
Err(SetRegistryError::InvalidRegistry(e)) => {
error!("Invalid registry: {}", e);
error!("invalid registry: {}", e);
Err(Status::invalid_argument("Registry is invalid"))
}
Err(SetRegistryError::UnsupportedVersion) => {
error!("Invalid registry");
error!("invalid registry");
Err(Status::invalid_argument("Registry is invalid"))
}
Err(SetRegistryError::FailedToParse) => {
error!("Failed to parse registry");
error!("failed to parse registry");
Err(Status::invalid_argument("Registry is invalid"))
}
Err(SetRegistryError::InvalidHash) => {
error!("Invalid IPFS hash");
error!("invalid IPFS hash");
Err(Status::invalid_argument("Invalid IPFS hash"))
}
Err(SetRegistryError::FailedToSetRegistry(_)) => {
error!("Failed to set registry");
Err(SetRegistryError::FailedToSetRegistry(e)) => {
error!("failed to set registry: {e}");
Err(Status::internal("Failed to set registry"))
}
}
Expand Down
2 changes: 1 addition & 1 deletion bothan-api/server/src/api/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub fn parse_price_state(id: String, price_state: PriceState) -> Price {
match i64::try_from(mantissa_price) {
Ok(p) => Price::new(id, p, Status::Available),
Err(_) => {
warn!("Failed to convert {mantissa_price} to i64 for id {id}");
warn!("failed to convert {mantissa_price} to i64 for id {id}");
Price::new(id, 0, Status::Unavailable)
}
}
Expand Down
2 changes: 1 addition & 1 deletion bothan-coinbase/src/api/websocket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ impl CoinbaseWebSocketConnector {

let status = resp.status();
if StatusCode::is_server_error(&status) || StatusCode::is_client_error(&status) {
warn!("Failed to connect with response code {}", resp.status());
warn!("failed to connect with response code {}", resp.status());
return Err(Error::ConnectionFailure(resp.status()));
}

Expand Down
6 changes: 3 additions & 3 deletions bothan-coinbase/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ impl Service for CoinbaseService {
.collect();

if !sub_ids.is_empty() && self.cmd_tx.send(Command::Subscribe(sub_ids)).await.is_err() {
warn!("Failed to send subscribe command");
warn!("failed to send subscribe command");
}

result
Expand Down Expand Up @@ -143,7 +143,7 @@ async fn process_command(
.await
.is_err()
{
warn!("Failed to subscribe to ids: {:?}", ids);
warn!("failed to subscribe to ids: {:?}", ids);
}
}
}
Expand Down Expand Up @@ -177,7 +177,7 @@ async fn handle_reconnect(
};

if command_tx.send(cmd).await.is_err() {
error!("Failed to send subscribe command");
error!("failed to send subscribe command");
};
}

Expand Down
2 changes: 1 addition & 1 deletion bothan-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ reqwest = { workspace = true }
rust_decimal = { workspace = true, features = ["maths", "serde-str"] }

anyhow = "1.0.86"
bincode = "1.3.3"
bincode = "2.0.0-rc.3"
chrono = "0.4.38"
clap = { version = "4.5.16", features = ["derive"] }
log = "0.4.22"
Expand Down
32 changes: 26 additions & 6 deletions bothan-core/src/registry.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
use std::collections::HashMap;
use std::marker::PhantomData;

use bincode::de::Decoder;
use bincode::enc::Encoder;
use bincode::error::{DecodeError, EncodeError};
use bincode::{Decode, Encode};
use serde::{Deserialize, Serialize};

use crate::registry::signal::Signal;
Expand All @@ -22,7 +26,7 @@ pub struct Valid;
pub struct Registry<State = Invalid> {
#[serde(flatten)]
inner: HashMap<String, Signal>,
#[serde(skip_serializing, default)]
#[serde(skip)]
_state: PhantomData<State>,
}

Expand All @@ -40,6 +44,15 @@ impl Registry<Invalid> {
}
}

impl<T> Registry<T> {
pub fn get(&self, signal_id: &str) -> Option<&Signal> {
self.inner.get(signal_id)
}
pub fn contains(&self, signal_id: &str) -> bool {
self.inner.contains_key(signal_id)
}
}

impl Default for Registry<Invalid> {
fn default() -> Self {
Registry {
Expand All @@ -49,12 +62,19 @@ impl Default for Registry<Invalid> {
}
}

impl<T> Registry<T> {
pub fn get(&self, signal_id: &str) -> Option<&Signal> {
self.inner.get(signal_id)
impl<State> Encode for Registry<State> {
fn encode<E: Encoder>(&self, encoder: &mut E) -> Result<(), EncodeError> {
self.inner.encode(encoder)
}
pub fn contains(&self, signal_id: &str) -> bool {
self.inner.contains_key(signal_id)
}

impl Decode for Registry<Invalid> {
fn decode<D: Decoder>(decoder: &mut D) -> Result<Self, DecodeError> {
let inner = HashMap::decode(decoder)?;
Ok(Registry {
inner,
_state: PhantomData,
})
}
}

Expand Down
Loading
Loading