Skip to content

Commit

Permalink
feat(task-manager): use result type with thiserror
Browse files Browse the repository at this point in the history
  • Loading branch information
petarvujovic98 committed Jun 13, 2024
1 parent 2c4add1 commit 9390f0b
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 15 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

7 changes: 4 additions & 3 deletions task_manager/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ authors = ["Mamy Ratsimbazafy <mamy $at$ taiko.xyz>"]
edition = "2021" # { workspace = true }

[dependencies]
raiko-lib = { workspace = true }
raiko-lib.workspace = true
rusqlite = { workspace = true, features = ["chrono"] }
num_enum = { workspace = true }
chrono = { workspace = true }
num_enum.workspace = true
chrono.workspace = true
thiserror.workspace = true

[dev-dependencies]
rand = "0.9.0-alpha.1" # This is an alpha version, that has rng.gen_iter::<T>()
Expand Down
28 changes: 16 additions & 12 deletions task_manager/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,12 +169,16 @@ use num_enum::{FromPrimitive, IntoPrimitive};
// Types
// ----------------------------------------------------------------

#[derive(PartialEq, Debug)]
#[derive(PartialEq, Debug, thiserror::Error)]
pub enum TaskManagerError {
#[error("IO Error {0}")]
IOError(IOErrorKind),
#[error("SQL Error {0}")]
SqlError(String),
}

pub type TaskManagerResult<T> = Result<T, TaskManagerError>;

impl From<IOError> for TaskManagerError {
fn from(error: IOError) -> TaskManagerError {
TaskManagerError::IOError(error.kind())
Expand Down Expand Up @@ -235,7 +239,7 @@ pub enum TaskStatus {
// ----------------------------------------------------------------

impl TaskDb {
fn open(path: &Path) -> Result<Connection, TaskManagerError> {
fn open(path: &Path) -> TaskManagerResult<Connection> {
let conn = Connection::open_with_flags(path, OpenFlags::SQLITE_OPEN_READ_WRITE)?;
conn.pragma_update(None, "foreign_keys", true)?;
conn.pragma_update(None, "locking_mode", "EXCLUSIVE")?;
Expand All @@ -245,7 +249,7 @@ impl TaskDb {
Ok(conn)
}

fn create(path: &Path) -> Result<Connection, TaskManagerError> {
fn create(path: &Path) -> TaskManagerResult<Connection> {
let _file = File::options()
.write(true)
.read(true)
Expand All @@ -261,7 +265,7 @@ impl TaskDb {

/// Open an existing TaskDb database at "path"
/// If a database does not exist at the path, one is created.
pub fn open_or_create(path: &Path) -> Result<Self, TaskManagerError> {
pub fn open_or_create(path: &Path) -> TaskManagerResult<Self> {
let conn = if path.exists() {
Self::open(path)
} else {
Expand All @@ -273,7 +277,7 @@ impl TaskDb {
// SQL
// ----------------------------------------------------------------

fn create_tables(conn: &Connection) -> Result<(), TaskManagerError> {
fn create_tables(conn: &Connection) -> TaskManagerResult<()> {
// Change the task_db_version if backward compatibility is broken
// and introduce a migration on DB opening ... if conserving history is important.
conn.execute_batch(
Expand Down Expand Up @@ -396,7 +400,7 @@ impl TaskDb {
Ok(())
}

fn create_views(conn: &Connection) -> Result<(), TaskManagerError> {
fn create_views(conn: &Connection) -> TaskManagerResult<()> {
// By convention, views will use an action verb as name.
conn.execute_batch(
r#"
Expand Down Expand Up @@ -458,7 +462,7 @@ impl TaskDb {
self.conn.trace(trace_fn);
}

pub fn manage(&self) -> Result<TaskManager<'_>, TaskManagerError> {
pub fn manage(&self) -> TaskManagerResult<TaskManager<'_>> {
// To update all the tables with the task_id assigned by Sqlite
// we require row IDs for the tasks table
// and we use last_insert_rowid() which is not reentrant and need a transaction lock
Expand Down Expand Up @@ -714,7 +718,7 @@ impl<'db> TaskManager<'db> {
gas_used,
payload,
}: EnqueTaskParams,

Check warning on line 720 in task_manager/src/lib.rs

View workflow job for this annotation

GitHub Actions / check-for-typos

"Enque" should be "Enqueue".
) -> Result<(), TaskManagerError> {
) -> TaskManagerResult<()> {
self.enqueue_task.execute(named_params! {
":chain_id": chain_id,
":blockhash": blockhash.as_slice(),
Expand All @@ -738,7 +742,7 @@ impl<'db> TaskManager<'db> {
fulfiller: Option<&str>,
status: TaskStatus,
proof: Option<&[u8]>,
) -> Result<(), TaskManagerError> {
) -> TaskManagerResult<()> {
self.update_task_progress.execute(named_params! {
":chain_id": chain_id,
":blockhash": blockhash.as_slice(),
Expand All @@ -756,7 +760,7 @@ impl<'db> TaskManager<'db> {
chain_id: ChainId,
blockhash: &B256,
proof_system: TaskProofsys,
) -> Result<TaskProvingStatus, TaskManagerError> {
) -> TaskManagerResult<TaskProvingStatus> {
let rows = self.get_task_proving_status.query_map(
named_params! {
":chain_id": chain_id,
Expand All @@ -781,7 +785,7 @@ impl<'db> TaskManager<'db> {
chain_id: ChainId,
blockhash: &B256,
proof_system: TaskProofsys,
) -> Result<Vec<u8>, TaskManagerError> {
) -> TaskManagerResult<Vec<u8>> {
let proof = self.get_task_proof.query_row(
named_params! {
":chain_id": chain_id,
Expand All @@ -795,7 +799,7 @@ impl<'db> TaskManager<'db> {
}

/// Returns the total and detailed database size
pub fn get_db_size(&mut self) -> Result<(usize, Vec<(String, usize)>), TaskManagerError> {
pub fn get_db_size(&mut self) -> TaskManagerResult<(usize, Vec<(String, usize)>)> {
let rows = self
.get_db_size
.query_map([], |row| Ok((row.get(0)?, row.get(1)?)))?;
Expand Down

0 comments on commit 9390f0b

Please sign in to comment.