diff --git a/offchain/dispatcher/src/dispatcher.rs b/offchain/dispatcher/src/dispatcher.rs index b454d9bf8..a1e00640d 100644 --- a/offchain/dispatcher/src/dispatcher.rs +++ b/offchain/dispatcher/src/dispatcher.rs @@ -4,9 +4,10 @@ use eth_state_client_lib::StateServer; use eth_state_fold_types::{Block, BlockStreamItem}; use rollups_events::DAppMetadata; +use std::sync::Arc; use tokio_stream::StreamExt; use tracing::{error, instrument, trace, warn}; -use types::foldables::authority::rollups::{RollupsInitialState, RollupsState}; +use types::foldables::{InputBox, InputBoxInitialState}; use crate::{ config::DispatcherConfig, @@ -57,19 +58,15 @@ pub async fn start( .into(), ); - let initial_state = RollupsInitialState { - history_address: config - .blockchain_config - .history_address - .clone() - .into_inner() - .into(), - input_box_address: config - .blockchain_config - .input_box_address - .clone() - .into_inner() - .into(), + let initial_state = InputBoxInitialState { + input_box_address: Arc::new( + config + .blockchain_config + .input_box_address + .clone() + .into_inner() + .into(), + ), }; trace!("Creating context"); @@ -136,10 +133,10 @@ async fn process_block( block: &Block, state_server: &impl StateServer< - InitialState = RollupsInitialState, - State = RollupsState, + InitialState = InputBoxInitialState, + State = InputBox, >, - initial_state: &RollupsInitialState, + initial_state: &InputBoxInitialState, context: &mut Context, machine_driver: &mut MachineDriver, @@ -155,7 +152,7 @@ async fn process_block( // Drive machine trace!("Reacting to state with `machine_driver`"); machine_driver - .react(context, &state.block, &state.state.input_box, broker) + .react(context, &state.block, &state.state, broker) .await .context(BrokerSnafu)?; diff --git a/offchain/dispatcher/src/drivers/context.rs b/offchain/dispatcher/src/drivers/context.rs index 704611812..5c788d5ac 100644 --- a/offchain/dispatcher/src/drivers/context.rs +++ b/offchain/dispatcher/src/drivers/context.rs @@ -7,7 +7,7 @@ use crate::{ }; use rollups_events::DAppMetadata; -use types::foldables::input_box::Input; +use types::foldables::Input; #[derive(Debug)] pub struct Context { diff --git a/offchain/dispatcher/src/drivers/machine.rs b/offchain/dispatcher/src/drivers/machine.rs index b73d47680..3f22f97f6 100644 --- a/offchain/dispatcher/src/drivers/machine.rs +++ b/offchain/dispatcher/src/drivers/machine.rs @@ -6,7 +6,7 @@ use super::Context; use crate::machine::{rollups_broker::BrokerFacadeError, BrokerSend}; use eth_state_fold_types::{ethereum_types::Address, Block}; -use types::foldables::input_box::{DAppInputBox, Input, InputBox}; +use types::foldables::{DAppInputBox, Input, InputBox}; use tracing::{debug, instrument, trace}; @@ -234,7 +234,7 @@ mod tests { rollup_status, ); let machine_driver = MachineDriver::new(H160::random()); - let dapp_input_box = types::foldables::input_box::DAppInputBox { + let dapp_input_box = types::foldables::DAppInputBox { inputs: input_timestamps .iter() .map(|timestamp| Arc::new(mock::new_input(*timestamp))) diff --git a/offchain/dispatcher/src/drivers/mock.rs b/offchain/dispatcher/src/drivers/mock.rs index 6cfa03e42..29b832008 100644 --- a/offchain/dispatcher/src/drivers/mock.rs +++ b/offchain/dispatcher/src/drivers/mock.rs @@ -14,7 +14,7 @@ use std::{ ops::{Deref, DerefMut}, sync::{Arc, Mutex}, }; -use types::foldables::input_box::{DAppInputBox, Input, InputBox}; +use types::foldables::{DAppInputBox, Input, InputBox}; use crate::machine::{ rollups_broker::BrokerFacadeError, BrokerSend, BrokerStatus, RollupStatus, diff --git a/offchain/dispatcher/src/machine/mod.rs b/offchain/dispatcher/src/machine/mod.rs index b8ea37cf9..735fdff23 100644 --- a/offchain/dispatcher/src/machine/mod.rs +++ b/offchain/dispatcher/src/machine/mod.rs @@ -3,7 +3,7 @@ pub mod rollups_broker; -use types::foldables::input_box::Input; +use types::foldables::Input; use async_trait::async_trait; diff --git a/offchain/dispatcher/src/machine/rollups_broker.rs b/offchain/dispatcher/src/machine/rollups_broker.rs index 40d477cd5..e430e4fe7 100644 --- a/offchain/dispatcher/src/machine/rollups_broker.rs +++ b/offchain/dispatcher/src/machine/rollups_broker.rs @@ -10,7 +10,7 @@ use rollups_events::{ RollupsAdvanceStateInput, RollupsData, RollupsInput, RollupsInputsStream, INITIAL_ID, }; -use types::foldables::input_box::Input; +use types::foldables::Input; use super::{BrokerSend, BrokerStatus, RollupStatus}; @@ -290,7 +290,7 @@ mod broker_facade_tests { }; use test_fixtures::broker::BrokerFixture; use testcontainers::clients::Cli; - use types::foldables::input_box::Input; + use types::foldables::Input; use crate::machine::{ rollups_broker::BrokerFacadeError, BrokerSend, BrokerStatus, diff --git a/offchain/dispatcher/src/setup.rs b/offchain/dispatcher/src/setup.rs index 0966669f1..97f632168 100644 --- a/offchain/dispatcher/src/setup.rs +++ b/offchain/dispatcher/src/setup.rs @@ -10,7 +10,7 @@ use rollups_events::DAppMetadata; use snafu::{ensure, ResultExt}; use tokio_stream::{Stream, StreamExt}; use tonic::transport::Channel; -use types::foldables::authority::{RollupsInitialState, RollupsState}; +use types::foldables::{InputBox, InputBoxInitialState}; use crate::{ config::DispatcherConfig, @@ -28,7 +28,7 @@ const BUFFER_LEN: usize = 256; pub async fn create_state_server( config: &SCConfig, ) -> Result< - impl StateServer + impl StateServer + BlockServer, DispatcherError, > { diff --git a/offchain/state-server/src/main.rs b/offchain/state-server/src/main.rs index fc420c87e..b6afd84af 100644 --- a/offchain/state-server/src/main.rs +++ b/offchain/state-server/src/main.rs @@ -2,7 +2,7 @@ // SPDX-License-Identifier: Apache-2.0 (see LICENSE) mod config; use config::Config; -use types::foldables::authority::rollups::RollupsState; +use types::foldables::InputBox; #[tokio::main] async fn main() -> Result<(), Box> { @@ -12,7 +12,7 @@ async fn main() -> Result<(), Box> { log::log_service_start(&config, "State Server"); - state_server::run_server::(config.state_server_config) + state_server::run_server::(config.state_server_config) .await .map_err(|e| e.into()) } diff --git a/offchain/types/src/foldables/input_box.rs b/offchain/types/src/foldables.rs similarity index 100% rename from offchain/types/src/foldables/input_box.rs rename to offchain/types/src/foldables.rs diff --git a/offchain/types/src/foldables/authority/mod.rs b/offchain/types/src/foldables/authority/mod.rs deleted file mode 100644 index 53fd74422..000000000 --- a/offchain/types/src/foldables/authority/mod.rs +++ /dev/null @@ -1,5 +0,0 @@ -// (c) Cartesi and individual authors (see AUTHORS) -// SPDX-License-Identifier: Apache-2.0 (see LICENSE) - -pub mod rollups; -pub use rollups::{RollupsInitialState, RollupsState}; diff --git a/offchain/types/src/foldables/authority/rollups.rs b/offchain/types/src/foldables/authority/rollups.rs deleted file mode 100644 index 8d60cf063..000000000 --- a/offchain/types/src/foldables/authority/rollups.rs +++ /dev/null @@ -1,126 +0,0 @@ -// (c) Cartesi and individual authors (see AUTHORS) -// SPDX-License-Identifier: Apache-2.0 (see LICENSE) - -use crate::{ - foldables::{ - claims::{History, HistoryInitialState}, - input_box::{InputBox, InputBoxInitialState}, - }, - FoldableError, UserData, -}; - -use eth_state_fold::{ - FoldMiddleware, Foldable, StateFoldEnvironment, SyncMiddleware, -}; -use eth_state_fold_types::{ - ethers::{providers::Middleware, types::Address}, - Block, QueryBlock, -}; - -use async_trait::async_trait; -use serde::{Deserialize, Serialize}; -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, -} - -#[derive(Clone, Debug, Serialize, Deserialize)] -pub struct RollupsState { - pub input_box_initial_state: Arc, - pub input_box: Arc, - - pub history_initial_state: Arc, - pub history: Arc, -} - -#[async_trait] -impl Foldable for RollupsState { - type InitialState = Arc; - type Error = FoldableError; - type UserData = Mutex; - - async fn sync( - initial_state: &Self::InitialState, - block: &Block, - env: &StateFoldEnvironment, - _access: Arc>, - ) -> Result { - let (input_box_initial_state, history_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) - }; - - fetch_sub_foldables( - env, - block, - input_box_initial_state, - history_initial_state, - ) - .await - } - - 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 { - fetch_sub_foldables( - env, - block, - previous_state.input_box_initial_state.clone(), - previous_state.history_initial_state.clone(), - ) - .await - } -} - -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())); - - let input_box = env - .get_state_for_block::( - &input_box_initial_state, - block.clone(), - ) - .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 deleted file mode 100644 index 820d13db2..000000000 --- a/offchain/types/src/foldables/mod.rs +++ /dev/null @@ -1,7 +0,0 @@ -// (c) Cartesi and individual authors (see AUTHORS) -// SPDX-License-Identifier: Apache-2.0 (see LICENSE) - -pub mod authority; - -pub mod claims; -pub mod input_box;