From 268707a78aaf96de6fd394118680bc3356a723bd Mon Sep 17 00:00:00 2001 From: silverpill Date: Wed, 8 Mar 2023 23:16:42 +0000 Subject: [PATCH] Disable post tokenization feature --- CHANGELOG.md | 4 ++-- Cargo.toml | 3 +++ src/ethereum/contracts.rs | 4 +++- src/ethereum/mod.rs | 4 +++- src/ipfs/utils.rs | 1 + src/job_queue/periodic_tasks.rs | 5 +++- src/job_queue/scheduler.rs | 25 +++++++++++++------- src/mastodon_api/statuses/views.rs | 38 ++++++++++++++++++++---------- 8 files changed, 58 insertions(+), 26 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2ca1e48..676c573 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,9 +6,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). ## [Unreleased] -### Changed +### Removed -- Store NFT monitor state in database. +- Disabled post tokenization (can be re-enabled with `ethereum-extras` feature). ## [1.16.0] - 2023-03-08 diff --git a/Cargo.toml b/Cargo.toml index 0a10f96..76c50ed 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -92,7 +92,10 @@ web3 = { version = "0.18.0", default-features = false, features = ["http", "http [dev-dependencies] mitra-config = { path = "mitra-config", features = ["test-utils"] } mitra-utils = { path = "mitra-utils", features = ["test-utils"] } + serial_test = "0.7.0" [features] +ethereum-extras = [] + production = ["mitra-config/production"] diff --git a/src/ethereum/contracts.rs b/src/ethereum/contracts.rs index 476d901..9a66b45 100644 --- a/src/ethereum/contracts.rs +++ b/src/ethereum/contracts.rs @@ -133,7 +133,9 @@ pub async fn get_contracts( }; let minter_abi = load_abi(&config.contract_dir, MINTER)?; - if is_interface_supported(&erc165, &minter_abi).await? { + if cfg!(feature = "ethereum-extras") && + is_interface_supported(&erc165, &minter_abi).await? + { let minter = Contract::new( web3.eth(), adapter_address, diff --git a/src/ethereum/mod.rs b/src/ethereum/mod.rs index 63ff5b3..d94ee68 100644 --- a/src/ethereum/mod.rs +++ b/src/ethereum/mod.rs @@ -4,8 +4,10 @@ pub mod eip4361; mod errors; pub mod gate; pub mod identity; -pub mod nft; pub mod signatures; pub mod subscriptions; pub mod sync; pub mod utils; + +#[cfg(feature = "ethereum-extras")] +pub mod nft; diff --git a/src/ipfs/utils.rs b/src/ipfs/utils.rs index 97f0231..fc56e3a 100644 --- a/src/ipfs/utils.rs +++ b/src/ipfs/utils.rs @@ -8,6 +8,7 @@ pub fn get_ipfs_url(cid: &str) -> String { #[error("parse error")] pub struct ParseError; +#[allow(dead_code)] pub fn parse_ipfs_url(url: &str) -> Result { let regexp = Regex::new(r"ipfs://(?P\w+)").unwrap(); let caps = regexp.captures(url).ok_or(ParseError)?; diff --git a/src/job_queue/periodic_tasks.rs b/src/job_queue/periodic_tasks.rs index ddd5bb4..393efe4 100644 --- a/src/job_queue/periodic_tasks.rs +++ b/src/job_queue/periodic_tasks.rs @@ -12,7 +12,6 @@ use crate::activitypub::queues::{ use crate::database::{get_database_client, DbPool}; use crate::ethereum::{ contracts::Blockchain, - nft::process_nft_events, subscriptions::{ check_ethereum_subscriptions, update_expired_subscriptions, @@ -28,6 +27,10 @@ use crate::models::{ }, }; +#[cfg(feature = "ethereum-extras")] +use crate::ethereum::nft::process_nft_events; + +#[cfg(feature = "ethereum-extras")] pub async fn nft_monitor( maybe_blockchain: Option<&mut Blockchain>, db_pool: &DbPool, diff --git a/src/job_queue/scheduler.rs b/src/job_queue/scheduler.rs index 3e771a5..31c5acb 100644 --- a/src/job_queue/scheduler.rs +++ b/src/job_queue/scheduler.rs @@ -11,7 +11,6 @@ use super::periodic_tasks::*; #[derive(Debug, Eq, Hash, PartialEq)] enum PeriodicTask { - NftMonitor, EthereumSubscriptionMonitor, SubscriptionExpirationMonitor, MoneroPaymentMonitor, @@ -19,13 +18,15 @@ enum PeriodicTask { OutgoingActivityQueueExecutor, DeleteExtraneousPosts, DeleteEmptyProfiles, + + #[cfg(feature = "ethereum-extras")] + NftMonitor, } impl PeriodicTask { /// Returns task period (in seconds) fn period(&self) -> i64 { match self { - Self::NftMonitor => 30, Self::EthereumSubscriptionMonitor => 300, Self::SubscriptionExpirationMonitor => 300, Self::MoneroPaymentMonitor => 30, @@ -33,6 +34,9 @@ impl PeriodicTask { Self::OutgoingActivityQueueExecutor => 5, Self::DeleteExtraneousPosts => 3600, Self::DeleteEmptyProfiles => 3600, + + #[cfg(feature = "ethereum-extras")] + Self::NftMonitor => 30, } } @@ -54,12 +58,14 @@ pub fn run( ) -> () { tokio::spawn(async move { let mut scheduler_state = HashMap::from([ - (PeriodicTask::NftMonitor, None), (PeriodicTask::EthereumSubscriptionMonitor, None), (PeriodicTask::SubscriptionExpirationMonitor, None), (PeriodicTask::MoneroPaymentMonitor, None), (PeriodicTask::IncomingActivityQueueExecutor, None), (PeriodicTask::OutgoingActivityQueueExecutor, None), + + #[cfg(feature = "ethereum-extras")] + (PeriodicTask::NftMonitor, None), ]); if config.retention.extraneous_posts.is_some() { scheduler_state.insert(PeriodicTask::DeleteExtraneousPosts, None); @@ -77,12 +83,6 @@ pub fn run( continue; }; let task_result = match task { - PeriodicTask::NftMonitor => { - nft_monitor( - maybe_blockchain.as_mut(), - &db_pool, - ).await - }, PeriodicTask::EthereumSubscriptionMonitor => { ethereum_subscription_monitor( &config, @@ -108,6 +108,13 @@ pub fn run( PeriodicTask::DeleteEmptyProfiles => { delete_empty_profiles(&config, &db_pool).await }, + #[cfg(feature = "ethereum-extras")] + PeriodicTask::NftMonitor => { + nft_monitor( + maybe_blockchain.as_mut(), + &db_pool, + ).await + }, }; task_result.unwrap_or_else(|err| { log::error!("{:?}: {}", task, err); diff --git a/src/mastodon_api/statuses/views.rs b/src/mastodon_api/statuses/views.rs index eaa2015..3d35b5a 100644 --- a/src/mastodon_api/statuses/views.rs +++ b/src/mastodon_api/statuses/views.rs @@ -13,10 +13,7 @@ use chrono::Utc; use uuid::Uuid; use mitra_config::Config; -use mitra_utils::{ - currencies::Currency, - markdown::markdown_lite_to_html, -}; +use mitra_utils::markdown::markdown_lite_to_html; use crate::activitypub::builders::{ announce::prepare_announce, @@ -28,12 +25,10 @@ use crate::activitypub::builders::{ }; use crate::database::{get_database_client, DatabaseError, DbPool}; use crate::errors::ValidationError; -use crate::ethereum::nft::create_mint_signature; use crate::http::{get_request_base_url, FormOrJson}; use crate::ipfs::{ store as ipfs_store, posts::PostMetadata, - utils::get_ipfs_url, }; use crate::mastodon_api::{ errors::MastodonError, @@ -47,7 +42,6 @@ use crate::models::{ get_thread, find_reposts_by_user, set_post_ipfs_cid, - set_post_token_tx_id, delete_post, }, posts::types::{PostCreateData, Visibility}, @@ -74,7 +68,6 @@ use super::types::{ StatusData, StatusPreview, StatusPreviewData, - TransactionData, }; #[post("")] @@ -600,6 +593,16 @@ async fn make_permanent( Ok(HttpResponse::Ok().json(status)) } +#[cfg(feature = "ethereum-extras")] +use { + mitra_utils::currencies::Currency, + crate::ethereum::nft::create_mint_signature, + crate::ipfs::utils::get_ipfs_url, + crate::models::posts::queries::set_post_token_tx_id, + super::types::TransactionData, +}; + +#[cfg(feature = "ethereum-extras")] #[get("/{status_id}/signature")] async fn get_signature( auth: BearerAuth, @@ -634,6 +637,7 @@ async fn get_signature( Ok(HttpResponse::Ok().json(signature)) } +#[cfg(feature = "ethereum-extras")] #[post("/{status_id}/token_minted")] async fn token_minted( auth: BearerAuth, @@ -666,8 +670,19 @@ async fn token_minted( Ok(HttpResponse::Ok().json(status)) } +#[cfg(feature = "ethereum-extras")] +fn with_ethereum_extras(scope: Scope) -> Scope { + scope + .service(get_signature) + .service(token_minted) +} +#[cfg(not(feature = "ethereum-extras"))] +fn with_ethereum_extras(scope: Scope) -> Scope { + scope +} + pub fn status_api_scope() -> Scope { - web::scope("/api/v1/statuses") + let scope = web::scope("/api/v1/statuses") // Routes without status ID .service(create_status) .service(preview_status) @@ -680,7 +695,6 @@ pub fn status_api_scope() -> Scope { .service(unfavourite) .service(reblog) .service(unreblog) - .service(make_permanent) - .service(get_signature) - .service(token_minted) + .service(make_permanent); + with_ethereum_extras(scope) }