Skip to content

Commit

Permalink
Create Reader and Writer traits for storage (#32)
Browse files Browse the repository at this point in the history
* Create Reader and Writer traits
* Pass only Reader to Rules
  • Loading branch information
ligustah authored Apr 17, 2024
1 parent cb00a04 commit 14309fc
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 31 deletions.
4 changes: 2 additions & 2 deletions src/controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use tokio::sync::{mpsc, oneshot};

use crate::p2p::NetworkState;
use crate::rules::{RuleContext, RulesEngine};
use crate::storage::PremintStorage;
use crate::storage::{PremintStorage, Reader, Writer};
use crate::types::{InclusionClaim, MintpoolNodeInfo, PremintTypes};

#[derive(Debug)]
Expand Down Expand Up @@ -175,7 +175,7 @@ impl Controller {
let metadata = premint.metadata();
let existing = match self
.store
.get_for_id_and_kind(metadata.id, metadata.kind)
.get_for_id_and_kind(&metadata.id, metadata.kind)
.await
{
Ok(existing) => Some(existing),
Expand Down
20 changes: 12 additions & 8 deletions src/rules.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use crate::config::Config;
use async_trait::async_trait;
use futures::future::join_all;

use crate::config::Config;
use crate::storage::PremintStorage;
use crate::types::{Premint, PremintTypes};
use crate::storage::{PremintStorage, Reader, Writer};
use crate::types::PremintTypes;

#[derive(Debug, PartialEq, Eq)]
pub enum Evaluation {
Expand Down Expand Up @@ -54,22 +54,24 @@ impl Results {
}
}

#[derive(Clone)]
pub struct RuleContext {
pub storage: PremintStorage,
pub storage: Box<dyn Reader>,
pub existing: Option<PremintTypes>,
}

impl RuleContext {
pub fn new(storage: PremintStorage, existing: Option<PremintTypes>) -> Self {
RuleContext { storage, existing }
RuleContext {
storage: Box::new(storage),
existing,
}
}
#[cfg(test)]
pub async fn test_default() -> Self {
let config = Config::test_default();

RuleContext {
storage: PremintStorage::new(&config).await,
storage: Box::new(PremintStorage::new(&config).await),
existing: None,
}
}
Expand Down Expand Up @@ -203,6 +205,7 @@ impl RulesEngine {
mod general {
use crate::rules::Evaluation::{Accept, Ignore, Reject};
use crate::rules::{Evaluation, Rule, RuleContext};
use crate::storage::Reader;
use crate::types::PremintMetadata;

pub fn all_rules() -> Vec<Box<dyn Rule>> {
Expand Down Expand Up @@ -305,7 +308,8 @@ mod test {
use crate::premints::zora_premint_v2::types::ZoraPremintV2;
use crate::rules::general::existing_token_uri;
use crate::rules::Evaluation::{Accept, Reject};
use crate::types::SimplePremint;
use crate::storage::Writer;
use crate::types::{Premint, SimplePremint};

use super::*;

Expand Down
68 changes: 47 additions & 21 deletions src/storage.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
use crate::config::Config;
use crate::types::{InclusionClaim, Premint, PremintName, PremintTypes};
use std::str::FromStr;

use async_trait::async_trait;
use eyre::WrapErr;
use sqlx::sqlite::SqliteConnectOptions;
use sqlx::{ConnectOptions, Row, SqlitePool};
use std::fs;
use std::str::FromStr;
use sqlx::Row;
use sqlx::SqlitePool;

use crate::config::Config;
use crate::types::{InclusionClaim, Premint, PremintName, PremintTypes};

async fn init_db(config: &Config) -> SqlitePool {
let expect_msg =
Expand Down Expand Up @@ -40,6 +43,24 @@ impl Clone for PremintStorage {
}
}

#[async_trait]
pub trait Writer: Reader {
async fn store(&self, premint: PremintTypes) -> eyre::Result<()>;
async fn mark_seen_on_chain(&self, claim: InclusionClaim) -> eyre::Result<()>;
}

#[async_trait]
pub trait Reader: Sync + Send {
async fn list_all(&self) -> eyre::Result<Vec<PremintTypes>>;
async fn get_for_id_and_kind(
&self,
id: &String,
kind: PremintName,
) -> eyre::Result<PremintTypes>;

async fn get_for_token_uri(&self, uri: &String) -> eyre::Result<PremintTypes>;
}

impl PremintStorage {
pub async fn new(config: &Config) -> Self {
let db = init_db(config).await;
Expand All @@ -51,7 +72,6 @@ impl PremintStorage {
prune_minted_premints: config.prune_minted_premints,
}
}

async fn create_premint_table(db: &SqlitePool) -> eyre::Result<()> {
sqlx::migrate!("./migrations")
.run(db)
Expand All @@ -63,8 +83,11 @@ impl PremintStorage {
pub fn db(&self) -> SqlitePool {
self.db.clone()
}
}

pub async fn store(&self, premint: PremintTypes) -> eyre::Result<()> {
#[async_trait]
impl Writer for PremintStorage {
async fn store(&self, premint: PremintTypes) -> eyre::Result<()> {
let metadata = premint.metadata();
let json = premint.to_json()?;
let signer = metadata.signer.to_checksum(None);
Expand All @@ -91,9 +114,9 @@ impl PremintStorage {
token_uri,
json,
)
.execute(&self.db)
.await
.map_err(|e| eyre::eyre!("Failed to store premint: {}", e))?;
.execute(&self.db)
.await
.map_err(|e| eyre::eyre!("Failed to store premint: {}", e))?;

// no rows affected means the version was not higher that what's already stored
if result.rows_affected() == 0 {
Expand All @@ -105,7 +128,7 @@ impl PremintStorage {
Ok(())
}

pub async fn mark_seen_on_chain(&self, claim: InclusionClaim) -> eyre::Result<()> {
async fn mark_seen_on_chain(&self, claim: InclusionClaim) -> eyre::Result<()> {
let chain_id = claim.chain_id as i64;
if self.prune_minted_premints {
let r = sqlx::query!(
Expand Down Expand Up @@ -137,14 +160,17 @@ impl PremintStorage {

Ok(())
}
}

pub async fn list_all(&self) -> eyre::Result<Vec<PremintTypes>> {
#[async_trait]
impl Reader for PremintStorage {
async fn list_all(&self) -> eyre::Result<Vec<PremintTypes>> {
list_all(&self.db).await
}

pub async fn get_for_id_and_kind(
async fn get_for_id_and_kind(
&self,
id: String,
id: &String,
kind: PremintName,
) -> eyre::Result<PremintTypes> {
let row = sqlx::query(
Expand All @@ -160,7 +186,7 @@ impl PremintStorage {
PremintTypes::from_json(json)
}

pub async fn get_for_token_uri(&self, uri: &String) -> eyre::Result<PremintTypes> {
async fn get_for_token_uri(&self, uri: &String) -> eyre::Result<PremintTypes> {
let row = sqlx::query("SELECT json FROM premints WHERE token_uri = ?")
.bind(uri)
.fetch_one(&self.db)
Expand Down Expand Up @@ -199,12 +225,12 @@ pub async fn list_all(db: &SqlitePool) -> eyre::Result<Vec<PremintTypes>> {

#[cfg(test)]
mod test {
use crate::config::{ChainInclusionMode, Config};
use sqlx::Row;

use crate::config::Config;
use crate::premints::zora_premint_v2::types::ZoraPremintV2;
use crate::storage::PremintStorage;
use crate::storage::{PremintStorage, Reader, Writer};
use crate::types::{InclusionClaim, Premint, PremintTypes};
use alloy_primitives::U256;
use sqlx::Row;

#[tokio::test]
async fn test_insert_and_get() {
Expand All @@ -215,7 +241,7 @@ mod test {

store.store(premint.clone()).await.unwrap();
let retrieved = store
.get_for_id_and_kind(premint.metadata().id, premint.metadata().kind)
.get_for_id_and_kind(&premint.metadata().id, premint.metadata().kind)
.await
.unwrap();
assert_eq!(premint, retrieved);
Expand Down Expand Up @@ -248,7 +274,7 @@ mod test {
store.store(premint.clone()).await.unwrap();

let retrieved = store
.get_for_id_and_kind(premint.metadata().id.clone(), premint.metadata().kind)
.get_for_id_and_kind(&premint.metadata().id.clone(), premint.metadata().kind)
.await
.unwrap();
assert_eq!(premint, retrieved);
Expand Down

0 comments on commit 14309fc

Please sign in to comment.