Skip to content

Commit

Permalink
feat: World::run() returns ArbiterDB
Browse files Browse the repository at this point in the history
  • Loading branch information
Autoparallel committed Feb 29, 2024
1 parent 8c007b5 commit 78b5e84
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
4 changes: 4 additions & 0 deletions engine/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,8 @@ pub enum ArbiterEngineError {
/// Error occurred in deserializing toml.
#[error(transparent)]
TomlError(#[from] toml::de::Error),

/// Error occurred within [`arbiter_core`].
#[error(transparent)]
ArbiterCoreError(#[from] arbiter_core::errors::ArbiterCoreError),
}
14 changes: 8 additions & 6 deletions engine/src/world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use std::collections::VecDeque;

use arbiter_core::{environment::Environment, middleware::ArbiterMiddleware};
use arbiter_core::{database::ArbiterDB, environment::Environment, middleware::ArbiterMiddleware};
use futures_util::future::join_all;
use serde::de::DeserializeOwned;
use tokio::spawn;
Expand Down Expand Up @@ -32,7 +32,7 @@ pub struct World {
pub agents: Option<HashMap<String, Agent>>,

/// The environment for the world.
pub environment: Environment,
pub environment: Option<Environment>,

/// The messaging layer for the world.
pub messager: Messager,
Expand All @@ -45,7 +45,7 @@ impl World {
Self {
id: id.to_owned(),
agents: Some(HashMap::new()),
environment: Environment::builder().build(),
environment: Some(Environment::builder().build()),
messager: Messager::new(),
}
}
Expand Down Expand Up @@ -158,7 +158,7 @@ impl World {
/// This will add the agent defined by `agent_builder` to the world.
pub fn add_agent(&mut self, agent_builder: AgentBuilder) {
let id = agent_builder.id.clone();
let client = ArbiterMiddleware::new(&self.environment, Some(&id))
let client = ArbiterMiddleware::new(self.environment.as_ref().unwrap(), Some(&id))
.expect("Failed to create RevmMiddleware client for agent");
let messager = self.messager.for_agent(&id);
let agent = agent_builder
Expand All @@ -185,7 +185,7 @@ impl World {
/// Returns an error if no agents are found in the world, possibly
/// indicating that the world has already been run or that no agents
/// were added prior to execution.
pub async fn run(&mut self) -> Result<(), ArbiterEngineError> {
pub async fn run(&mut self) -> Result<ArbiterDB, ArbiterEngineError> {
let agents = match self.agents.take() {
Some(agents) => agents,
None => {
Expand Down Expand Up @@ -218,6 +218,8 @@ impl World {
}
// Await the completion of all tasks.
join_all(tasks).await;
Ok(())

let db = self.environment.take().unwrap().stop()?;
Ok(db)
}
}

0 comments on commit 78b5e84

Please sign in to comment.