Skip to content

Commit

Permalink
Cleanup Errors
Browse files Browse the repository at this point in the history
  • Loading branch information
mousetrail committed Jul 17, 2024
1 parent 3573e69 commit cf2775a
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 32 deletions.
2 changes: 1 addition & 1 deletion sibyls/src/common.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use crate::error::SibylsError;
use clap::ValueEnum;
use dlc_messages::oracle_msgs::EventDescriptor::{DigitDecompositionEvent, EnumEvent};
use dlc_messages::oracle_msgs::{
DigitDecompositionEventDescriptor, EventDescriptor, OracleAnnouncement, OracleAttestation,
};
use serde::{Deserialize, Serialize};
use std::fmt::{self, Debug, Display, Formatter};
use std::str::FromStr;
use clap::ValueEnum;
use time::{serde::format_description, Duration, OffsetDateTime, Time};

use crate::oracle::pricefeeds::FeedId;
Expand Down
12 changes: 7 additions & 5 deletions sibyls/src/db/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@ pub enum EventStorage {
}

impl EventStorage {
pub fn new(database_url: &Option<String>, database_backend: &DatabaseBackend, asset_pair: AssetPair) -> Result<EventStorage, SibylsError> {
match database_backend{
DatabaseBackend::Sled => {
Ok(EventStorage::Sled(SledEventStorage::new(asset_pair)?))
}
pub fn new(
database_url: &Option<String>,
database_backend: &DatabaseBackend,
asset_pair: AssetPair,
) -> Result<EventStorage, SibylsError> {
match database_backend {
DatabaseBackend::Sled => Ok(EventStorage::Sled(SledEventStorage::new(asset_pair)?)),
DatabaseBackend::Pg => {
if let Some(url) = database_url {
Ok(EventStorage::Pg(PgEventStorage::new(url)?))
Expand Down
24 changes: 15 additions & 9 deletions sibyls/src/db/postgres.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::error::DbError::{PgDatabaseError, PgDatabasePoolError};
use crate::error::SibylsError;
use crate::{AssetPair, Filters, OracleEvent, SortOrder, PAGE_SIZE};
use diesel::r2d2::{ConnectionManager, Pool};
Expand Down Expand Up @@ -93,12 +94,13 @@ impl PgEventStorage {
let mut conn = self
.pool
.get()
.map_err(|e| SibylsError::PgDatabasePoolError(e.to_string()))?;
.map_err(|e| PgDatabasePoolError(e.to_string()))?;
let result = events
.find((maturation, asset_pair.to_string()))
.select(EventDTO::as_select())
.first(&mut conn)
.optional()?;
.optional()
.map_err(|e| PgDatabaseError(e))?;

if let Some(event) = result {
event.to_oracle_event()
Expand All @@ -124,22 +126,24 @@ impl PgEventStorage {
let mut conn = self
.pool
.get()
.map_err(|e| SibylsError::PgDatabasePoolError(e.to_string()))?;
.map_err(|e| PgDatabasePoolError(e.to_string()))?;
let results = match filters.sort_by {
SortOrder::Insertion => events
.filter(asset_pair.eq(filters.asset_pair.to_string()))
.select(EventDTO::as_select())
.order(maturation.asc())
.limit(PAGE_SIZE as i64)
.offset((filters.page * PAGE_SIZE) as i64)
.load(&mut conn)?,
.load(&mut conn)
.map_err(|e| PgDatabaseError(e))?,
SortOrder::ReverseInsertion => events
.filter(asset_pair.eq(filters.asset_pair.to_string()))
.select(EventDTO::as_select())
.order(maturation.desc())
.limit(PAGE_SIZE as i64)
.offset((filters.page * PAGE_SIZE) as i64)
.load(&mut conn)?,
.load(&mut conn)
.map_err(|e| PgDatabaseError(e))?,
};

let mut res = vec![];
Expand Down Expand Up @@ -180,12 +184,13 @@ impl PgEventStorage {
let mut conn = self
.pool
.get()
.map_err(|e| SibylsError::PgDatabasePoolError(e.to_string()))?;
.map_err(|e| PgDatabasePoolError(e.to_string()))?;

diesel::insert_into(crate::schema::events::table)
.values(new_event)
.returning(EventDTO::as_returning())
.get_result(&mut conn)?;
.get_result(&mut conn)
.map_err(|e| PgDatabaseError(e))?;
Ok(())
}

Expand All @@ -205,7 +210,7 @@ impl PgEventStorage {
let mut conn = self
.pool
.get()
.map_err(|e| SibylsError::PgDatabasePoolError(e.to_string()))?;
.map_err(|e| PgDatabasePoolError(e.to_string()))?;

let mut attestation_bytes = Vec::new();
write_as_tlv(att, &mut attestation_bytes)
Expand All @@ -216,7 +221,8 @@ impl PgEventStorage {

diesel::update(events.find((maturation, asset_pair.to_string())))
.set((attestation.eq(attestation_hex), price.eq(p)))
.execute(&mut conn)?;
.execute(&mut conn)
.map_err(|e| PgDatabaseError(e))?;
Ok(())
}
}
Expand Down
19 changes: 10 additions & 9 deletions sibyls/src/db/sled.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::error::DbError::SledDatabaseError;
use crate::error::SibylsError;
use crate::{AssetPair, Filters, OracleEvent, SortOrder, PAGE_SIZE};
use dlc_messages::oracle_msgs::{EventDescriptor, OracleAnnouncement, OracleAttestation};
Expand Down Expand Up @@ -26,7 +27,7 @@ impl SledEventStorage {
pub fn new(asset_pair: AssetPair) -> Result<Self, SibylsError> {
let path = format!("events/{}", asset_pair);
info!("creating sled at {}", path);
let event_database = sled::open(path)?;
let event_database = sled::open(path).map_err(|e| SledDatabaseError(e))?;
Ok(SledEventStorage { event_database })
}

Expand Down Expand Up @@ -62,7 +63,7 @@ impl SledEventStorage {
match self
.event_database
.get(id.to_owned().into_bytes())
.map_err(SibylsError::SledDatabaseError)?
.map_err(|e| SledDatabaseError(e))?
{
Some(event) => Ok(crate::db::SledEventStorage::parse_database_entry(
asset_pair,
Expand All @@ -87,7 +88,7 @@ impl SledEventStorage {
let init_key = self
.event_database
.first()
.map_err(SibylsError::SledDatabaseError)?
.map_err(|e| SledDatabaseError(e))?
.unwrap()
.0;
let start_key =
Expand All @@ -100,7 +101,7 @@ impl SledEventStorage {
== self
.event_database
.first()
.map_err(SibylsError::SledDatabaseError)?
.map_err(|e| SledDatabaseError(e))?
.unwrap()
.0
{
Expand All @@ -126,7 +127,7 @@ impl SledEventStorage {
let init_key = self
.event_database
.last()
.map_err(SibylsError::SledDatabaseError)?
.map_err(|e| SledDatabaseError(e))?
.unwrap()
.0;
let end_key =
Expand All @@ -139,7 +140,7 @@ impl SledEventStorage {
== self
.event_database
.last()
.map_err(SibylsError::SledDatabaseError)?
.map_err(|e| SledDatabaseError(e))?
.unwrap()
.0
{
Expand Down Expand Up @@ -210,7 +211,7 @@ impl SledEventStorage {
serde_json::to_string(&db_value).unwrap().into_bytes(),
) {
Ok(_) => Ok(()),
Err(err) => Err(SibylsError::SledDatabaseError(err)),
Err(err) => Err(SibylsError::from(SledDatabaseError(err))),
}
}

Expand All @@ -225,7 +226,7 @@ impl SledEventStorage {
match self
.event_database
.get(id.to_owned().into_bytes())
.map_err(SibylsError::SledDatabaseError)?
.map_err(|e| SledDatabaseError(e))?
{
Some(event) => {
let mut db_value: DbValue =
Expand All @@ -240,7 +241,7 @@ impl SledEventStorage {
serde_json::to_string(&db_value).unwrap().into_bytes(),
) {
Ok(_) => Ok(()),
Err(err) => Err(SibylsError::SledDatabaseError(err)),
Err(err) => Err(SibylsError::from(SledDatabaseError(err))),
}
}
None => return Err(SibylsError::OracleEventNotFoundError(id.to_string()).into()),
Expand Down
17 changes: 12 additions & 5 deletions sibyls/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,26 @@ pub enum SibylsError {
OracleEventNotFoundError(String),

/// database error: {0}
DatabaseError(#[from] DbError),

/// {0}
InternalError(String),
}

#[allow(clippy::enum_variant_names)]
#[derive(Debug, Display, Error, PartialEq)]
pub enum DbError {
/// {0}
SledDatabaseError(#[from] sled::Error),

/// database connection error: {0}
/// connection error: {0}
PgDatabaseConnectionError(#[from] diesel::ConnectionError),

/// database error: {0}
/// {0}
PgDatabaseError(#[from] diesel::result::Error),

/// database pool error: {0}
PgDatabasePoolError(String),

/// {0}
InternalError(String),
}

impl actix_web::error::ResponseError for SibylsError {
Expand Down
8 changes: 7 additions & 1 deletion sibyls/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,13 @@ async fn main() -> anyhow::Result<()> {
info!("creating oracle for {}", asset_pair);
// setup event database

let oracle = Oracle::new(oracle_config, asset_pair_info, keypair, &args.database_url, &args.database_backend)?;
let oracle = Oracle::new(
oracle_config,
asset_pair_info,
keypair,
&args.database_url,
&args.database_backend,
)?;

// pricefeed retrieval
info!("creating pricefeeds for {asset_pair}");
Expand Down
5 changes: 3 additions & 2 deletions sibyls/src/oracle/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,12 @@ impl Oracle {
oracle_config.announcement_offset,
));
}
match database_backend{
match database_backend {
DatabaseBackend::Sled => {}
DatabaseBackend::Pg => {}
}
let event_database = EventStorage::new(database_url, database_backend, asset_pair_info.asset_pair)?;
let event_database =
EventStorage::new(database_url, database_backend, asset_pair_info.asset_pair)?;

Ok(Oracle {
oracle_config,
Expand Down

0 comments on commit cf2775a

Please sign in to comment.