From 64995764541e4a8f6520e80187bd1130300af868 Mon Sep 17 00:00:00 2001 From: Gabriel de Quadros Ligneul Date: Wed, 24 Jan 2024 14:53:41 -0300 Subject: [PATCH] refactor: remove dead code The dispatcher doesn't use the history foldable because this code was reimplemented in the authority-claimer service. --- offchain/dispatcher/src/dispatcher.rs | 6 - .../types/src/foldables/authority/rollups.rs | 45 +---- offchain/types/src/foldables/claims.rs | 166 ------------------ offchain/types/src/foldables/mod.rs | 1 - 4 files changed, 6 insertions(+), 212 deletions(-) delete mode 100644 offchain/types/src/foldables/claims.rs diff --git a/offchain/dispatcher/src/dispatcher.rs b/offchain/dispatcher/src/dispatcher.rs index b454d9bf8..3eaccc2bb 100644 --- a/offchain/dispatcher/src/dispatcher.rs +++ b/offchain/dispatcher/src/dispatcher.rs @@ -58,12 +58,6 @@ pub async fn start( ); let initial_state = RollupsInitialState { - history_address: config - .blockchain_config - .history_address - .clone() - .into_inner() - .into(), input_box_address: config .blockchain_config .input_box_address diff --git a/offchain/types/src/foldables/authority/rollups.rs b/offchain/types/src/foldables/authority/rollups.rs index 8d60cf063..728c74751 100644 --- a/offchain/types/src/foldables/authority/rollups.rs +++ b/offchain/types/src/foldables/authority/rollups.rs @@ -2,10 +2,7 @@ // SPDX-License-Identifier: Apache-2.0 (see LICENSE) use crate::{ - foldables::{ - claims::{History, HistoryInitialState}, - input_box::{InputBox, InputBoxInitialState}, - }, + foldables::input_box::{InputBox, InputBoxInitialState}, FoldableError, UserData, }; @@ -23,7 +20,6 @@ use std::sync::{Arc, Mutex}; #[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize)] pub struct RollupsInitialState { - pub history_address: Address, pub input_box_address: Address, } @@ -31,9 +27,6 @@ pub struct RollupsInitialState { pub struct RollupsState { pub input_box_initial_state: Arc, pub input_box: Arc, - - pub history_initial_state: Arc, - pub history: Arc, } #[async_trait] @@ -48,34 +41,17 @@ impl Foldable for RollupsState { env: &StateFoldEnvironment, _access: Arc>, ) -> Result { - let (input_box_initial_state, history_initial_state) = { + let input_box_initial_state = { let mut user_data = env .user_data() .lock() .expect("Mutex should never be poisoned"); - - let i = { - let input_box_address = - user_data.get(initial_state.input_box_address); - Arc::new(InputBoxInitialState { input_box_address }) - }; - - let h = { - let history_address = - user_data.get(initial_state.history_address); - Arc::new(HistoryInitialState { history_address }) - }; - - (i, h) + let input_box_address = + user_data.get(initial_state.input_box_address); + Arc::new(InputBoxInitialState { input_box_address }) }; - fetch_sub_foldables( - env, - block, - input_box_initial_state, - history_initial_state, - ) - .await + fetch_sub_foldables(env, block, input_box_initial_state).await } async fn fold( @@ -89,7 +65,6 @@ impl Foldable for RollupsState { env, block, previous_state.input_box_initial_state.clone(), - previous_state.history_initial_state.clone(), ) .await } @@ -99,7 +74,6 @@ async fn fetch_sub_foldables( env: &StateFoldEnvironment::UserData>, block: &Block, input_box_initial_state: Arc, - history_initial_state: Arc, ) -> Result::Error> { // TODO: Change state-fold sync/fold to receive Arc let block = QueryBlock::Block(Arc::new(block.clone())); @@ -112,15 +86,8 @@ async fn fetch_sub_foldables( .await? .state; - let history = env - .get_state_for_block::(&history_initial_state, block) - .await? - .state; - Ok(RollupsState { input_box, input_box_initial_state, - history, - history_initial_state, }) } diff --git a/offchain/types/src/foldables/claims.rs b/offchain/types/src/foldables/claims.rs deleted file mode 100644 index 5bb2c18ac..000000000 --- a/offchain/types/src/foldables/claims.rs +++ /dev/null @@ -1,166 +0,0 @@ -// (c) Cartesi and individual authors (see AUTHORS) -// SPDX-License-Identifier: Apache-2.0 (see LICENSE) - -use crate::{FoldableError, UserData}; - -use eth_state_fold::{ - utils as fold_utils, FoldMiddleware, Foldable, StateFoldEnvironment, - SyncMiddleware, -}; -use eth_state_fold_types::{ - ethers::{ - prelude::EthEvent, - providers::Middleware, - types::{Address, H256, U256}, - }, - Block, -}; - -use anyhow::Context; -use async_trait::async_trait; -use im::{HashMap, Vector}; -use serde::{Deserialize, Serialize}; -use std::sync::{Arc, Mutex}; - -#[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize)] -pub struct HistoryInitialState { - pub history_address: Arc
, -} - -#[derive(Clone, Debug, Serialize, Deserialize)] -pub struct Claim { - pub epoch_hash: H256, - - // Both "closed/inclusive" - pub start_input_index: usize, - pub end_input_index: usize, - - pub claim_timestamp: u64, -} - -impl From<(contracts::history::Claim, U256)> for Claim { - fn from(x: (contracts::history::Claim, U256)) -> Self { - let c = x.0; - let t = x.1; - Self { - epoch_hash: c.epoch_hash.into(), - start_input_index: c.first_index as usize, - end_input_index: c.last_index as usize, - claim_timestamp: t.as_u64(), - } - } -} - -#[derive(Clone, Debug, Serialize, Deserialize)] -pub struct DAppClaims { - pub claims: Vector>, -} - -#[derive(Clone, Debug, Serialize, Deserialize)] -pub struct History { - pub history_address: Arc
, - pub dapp_claims: Arc, Arc>>, -} - -#[async_trait] -impl Foldable for History { - type InitialState = HistoryInitialState; - type Error = FoldableError; - type UserData = Mutex; - - async fn sync( - initial_state: &Self::InitialState, - _block: &Block, - env: &StateFoldEnvironment, - access: Arc>, - ) -> Result { - let history_address = Arc::clone(&initial_state.history_address); - - let dapp_claims = - fetch_history(access, env, &history_address, &HashMap::new()) - .await?; - - Ok(Self { - history_address, - dapp_claims, - }) - } - - async fn fold( - previous_state: &Self, - block: &Block, // TODO: when new version of state-fold gets released, change this to Arc - // and save on cloning. - env: &StateFoldEnvironment, - access: Arc>, - ) -> Result { - let history_address = Arc::clone(&previous_state.history_address); - - if !(fold_utils::contains_address(&block.logs_bloom, &history_address) - && (fold_utils::contains_topic( - &block.logs_bloom, - &contracts::history::NewClaimToHistoryFilter::signature(), - ))) - { - return Ok(previous_state.clone()); - } - - let new_dapp_claims = fetch_history( - access, - env, - &history_address, - &previous_state.dapp_claims, - ) - .await?; - - Ok(Self { - history_address, - dapp_claims: new_dapp_claims, - }) - } -} - -async fn fetch_history( - provider: Arc, - env: &StateFoldEnvironment::UserData>, - contract_address: &Address, - previous_dapp_claims: &HashMap, Arc>, -) -> Result, Arc>>, FoldableError> { - use contracts::history::*; - let contract = History::new(*contract_address, Arc::clone(&provider)); - - let mut dapp_claims = previous_dapp_claims.clone(); - - // Retrieve `NewClaim` events - let claims = contract - .new_claim_to_history_filter() - .query_with_meta() - .await - .context("Error querying for new claim events")?; - - for (claim, meta) in claims { - let timestamp = env - .block_with_hash(&meta.block_hash) - .await - .context("Error querying for block")? - .timestamp; - - let new_claim: Arc = - Arc::new((claim.claim, timestamp).into()); - let dapp_address = Arc::new(claim.dapp); - - dapp_claims - .entry(dapp_address) - .and_modify(|h| { - let mut new_history = (**h).clone(); - new_history.claims.push_back(new_claim.clone()); - *h = Arc::new(new_history); - }) - .or_insert_with(|| { - Arc::new(DAppClaims { - claims: im::vector![new_claim], - }) - }); - } - - Ok(Arc::new(dapp_claims)) -} diff --git a/offchain/types/src/foldables/mod.rs b/offchain/types/src/foldables/mod.rs index 820d13db2..ef75a8111 100644 --- a/offchain/types/src/foldables/mod.rs +++ b/offchain/types/src/foldables/mod.rs @@ -3,5 +3,4 @@ pub mod authority; -pub mod claims; pub mod input_box;