Skip to content

Commit

Permalink
fix: post merge issues, good to go
Browse files Browse the repository at this point in the history
  • Loading branch information
Autoparallel committed Feb 2, 2024
1 parent 46e91a8 commit 4b9d59e
Show file tree
Hide file tree
Showing 12 changed files with 61 additions and 55 deletions.
14 changes: 4 additions & 10 deletions arbiter-engine/src/agent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use serde::de::DeserializeOwned;
use thiserror::Error;

use crate::{
machine::{Behavior, Engine, State, StateMachine},
machine::{Behavior, Engine, StateMachine},
messager::Messager,
};

Expand All @@ -26,9 +26,6 @@ pub struct Agent {
/// Used for routing messages.
pub id: String,

/// The status of the agent.
pub state: State,

/// The messager the agent uses to send and receive messages from other
/// agents.
pub messager: Messager,
Expand All @@ -45,22 +42,20 @@ impl Debug for Agent {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Agent")
.field("id", &self.id)
.field("state", &self.state)
.field("messager", &self.messager)
.field("client", &self.client)
.field("event_streamer", &self.event_streamer)
.field("behavior_engines", &self.behavior_engines)
.finish()
}

Check warning on line 49 in arbiter-engine/src/agent.rs

View check run for this annotation

Codecov / codecov/patch

arbiter-engine/src/agent.rs#L42-L49

Added lines #L42 - L49 were not covered by tests
}

impl Agent {
/// Produces a minimal agent builder with the given identifier.
pub fn builder(id: &str) -> Result<AgentBuilder, AgentBuildError> {
Ok(AgentBuilder {
pub fn builder(id: &str) -> AgentBuilder {
AgentBuilder {
id: id.to_owned(),
behavior_engines: None,
})
}
}
}

Expand Down Expand Up @@ -100,7 +95,6 @@ impl AgentBuilder {
match self.behavior_engines {
Some(engines) => Ok(Agent {
id: self.id,
state: State::Uninitialized,
messager,
client,
behavior_engines: engines,
Expand Down
4 changes: 2 additions & 2 deletions arbiter-engine/src/examples/minter/agents/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
use super::*;
pub mod token_admin;
pub mod token_requester;
pub(crate) mod token_admin;
pub(crate) mod token_requester;
2 changes: 1 addition & 1 deletion arbiter-engine/src/examples/minter/agents/token_admin.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::*;

#[derive(Debug)]
pub struct TokenAdmin {
pub(crate) struct TokenAdmin {
/// The identifier of the token admin.
pub token_data: HashMap<String, TokenData>,
pub tokens: Option<HashMap<String, ArbiterToken<RevmMiddleware>>>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use super::*;
/// The token requester is responsible for requesting tokens from the token
/// admin. This agents is purely for testing purposes as far as I can tell.
#[derive(Debug)]
pub struct TokenRequester {
pub(crate) struct TokenRequester {
/// The tokens that the token requester has requested.
pub token_data: TokenData,
/// The agent ID to request tokens to.
Expand Down
4 changes: 2 additions & 2 deletions arbiter-engine/src/examples/minter/behaviors/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
use super::*;
pub mod token_admin;
pub mod token_requester;
pub(crate) mod token_admin;
pub(crate) mod token_requester;
8 changes: 4 additions & 4 deletions arbiter-engine/src/examples/minter/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::*;
pub mod agents;
pub mod behaviors;
pub mod token_minter;
pub(crate) mod agents;
pub(crate) mod behaviors;
pub(crate) mod token_minter;

use std::pin::Pin;

Expand All @@ -22,7 +22,7 @@ const TOKEN_SYMBOL: &str = "ARB";
const TOKEN_DECIMALS: u8 = 18;

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct TokenData {
pub(crate) struct TokenData {
pub name: String,
pub symbol: String,
pub decimals: u8,
Expand Down
4 changes: 2 additions & 2 deletions arbiter-engine/src/examples/minter/token_minter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ async fn token_minter_simulation() {
let client = RevmMiddleware::new(&world.environment, None).unwrap();

// Create the token admin agent
let token_admin = Agent::builder(TOKEN_ADMIN_ID).unwrap();
let token_admin = Agent::builder(TOKEN_ADMIN_ID);
let mut token_admin_behavior = TokenAdmin::new(Some(4));
token_admin_behavior.add_token(TokenData {
name: TOKEN_NAME.to_owned(),
Expand All @@ -23,7 +23,7 @@ async fn token_minter_simulation() {
address: None,
});
// Create the token requester agent
let token_requester = Agent::builder(REQUESTER_ID).unwrap();
let token_requester = Agent::builder(REQUESTER_ID);
let mut token_requester_behavior = TokenRequester::new(Some(4));
world.add_agent(token_requester.with_behavior(token_requester_behavior));

Expand Down
10 changes: 2 additions & 8 deletions arbiter-engine/src/examples/mod.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
#![warn(missing_docs)]
#![allow(unused)]
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// TODO: Notes ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Create a BlockAdmin and a TokenAdmin.
// Potentially create an `Orchestrator`` that sends instructions to both
// BlockAdmin and TokenAdmin.
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

//! The examples module contains example strategies.
Expand All @@ -18,5 +12,5 @@ use futures_util::{stream, StreamExt};

use super::*;
use crate::messager::{Message, Messager};
mod minter;
mod timed_message;
pub(crate) mod minter;
pub(crate) mod timed_message;
11 changes: 6 additions & 5 deletions arbiter-engine/src/examples/timed_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ use crate::{
world::World,
};

struct TimedMessage {
#[derive(Debug)]
pub(crate) struct TimedMessage {
delay: u64,
receive_data: String,
send_data: String,
Expand Down Expand Up @@ -99,7 +100,7 @@ impl Behavior<Message> for TimedMessage {
async fn echoer() {
let mut world = World::new("world");

let agent = Agent::builder(AGENT_ID).unwrap();
let agent = Agent::builder(AGENT_ID);
let behavior = TimedMessage::new(
1,
"Hello, world!".to_owned(),
Expand Down Expand Up @@ -135,7 +136,7 @@ async fn echoer() {
async fn ping_pong() {
let mut world = World::new("world");

let agent = Agent::builder(AGENT_ID).unwrap();
let agent = Agent::builder(AGENT_ID);
let behavior_ping = TimedMessage::new(
1,
"pong".to_owned(),
Expand Down Expand Up @@ -176,8 +177,8 @@ async fn ping_pong() {
async fn ping_pong_two_agent() {
let mut world = World::new("world");

let agent_ping = Agent::builder("agent_ping").unwrap();
let agent_pong = Agent::builder("agent_pong").unwrap();
let agent_ping = Agent::builder("agent_ping");
let agent_pong = Agent::builder("agent_pong");

let behavior_ping = TimedMessage::new(
1,
Expand Down
21 changes: 17 additions & 4 deletions arbiter-engine/src/machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ pub enum State {
/// The [`Behavior`] trait is the lowest level functionality that will be used
/// by a [`StateMachine`]. This constitutes what each state transition will do.
#[async_trait::async_trait]
pub trait Behavior<E>: Send + Sync + 'static {
pub trait Behavior<E>: Send + Sync + Debug + 'static {
/// Used to start the agent.
/// This is where the agent can engage in its specific start up activities
/// that it can do given the current state of the world.
Expand All @@ -70,7 +70,7 @@ pub trait Behavior<E>: Send + Sync + 'static {
}

#[async_trait::async_trait]
pub(crate) trait StateMachine: Send + Sync + 'static {
pub(crate) trait StateMachine: Send + Sync + Debug + 'static {
async fn execute(&mut self, instruction: MachineInstruction);
}

Expand Down Expand Up @@ -100,10 +100,23 @@ where
phantom: std::marker::PhantomData<E>,
}

impl<B, E> Engine<B, E>
impl<B, E> Debug for Engine<B, E>
where
B: Behavior<E>,
E: DeserializeOwned + Send + Sync + 'static,
{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Engine")
.field("behavior", &self.behavior)
.field("state", &self.state)
.finish()
}

Check warning on line 113 in arbiter-engine/src/machine.rs

View check run for this annotation

Codecov / codecov/patch

arbiter-engine/src/machine.rs#L108-L113

Added lines #L108 - L113 were not covered by tests
}

impl<B, E> Engine<B, E>
where
B: Behavior<E> + Debug,
E: DeserializeOwned + Send + Sync + 'static,
{
/// Creates a new [`Engine`] with the given [`Behavior`] and [`Receiver`].
pub(crate) fn new(behavior: B) -> Self {
Expand All @@ -119,7 +132,7 @@ where
#[async_trait::async_trait]
impl<B, E> StateMachine for Engine<B, E>
where
B: Behavior<E>,
B: Behavior<E> + Debug,
E: DeserializeOwned + Send + Sync + Debug + 'static,
{
async fn execute(&mut self, instruction: MachineInstruction) {
Expand Down
24 changes: 15 additions & 9 deletions arbiter-engine/src/universe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,12 @@ impl Universe {
return Err(anyhow::anyhow!("Universe is already running."));
}
let mut tasks = Vec::new();
for (_, world) in self.worlds.take().unwrap().drain() {
tasks.push(spawn(async move { world.run().await }));
// TODO: These unwraps need to be checkdd a bit.
for (_, mut world) in self.worlds.take().unwrap().drain() {
tasks.push(spawn(async move {
world.run().await.unwrap();
world
}));
}
self.world_tasks = Some(join_all(tasks.into_iter()).await);
Ok(())
Expand All @@ -61,16 +65,15 @@ mod tests {
use tracing_subscriber::{fmt, EnvFilter};

use super::*;
use crate::{agent::Agent, examples::timed_message::*, machine::State};
use crate::{agent::Agent, examples::timed_message::*};

#[tokio::test]
async fn run_universe() {
let mut universe = Universe::new();
let world = World::new("test");
universe.add_world(world);
universe.run_worlds().await.unwrap();
let world = universe.world_tasks.unwrap().remove(0).unwrap();
assert_eq!(world.state, State::Processing);
universe.world_tasks.unwrap().remove(0).unwrap();
}

#[tokio::test]
Expand All @@ -97,7 +100,7 @@ mod tests {
.expect("setting default subscriber failed");

let mut world1 = World::new("test1");
let agent1 = Agent::new("agent1", &world1);
let agent1 = Agent::builder("agent1");
let behavior1 = TimedMessage::new(
1,
"echo".to_owned(),
Expand All @@ -108,7 +111,7 @@ mod tests {
world1.add_agent(agent1.with_behavior(behavior1));

let mut world2 = World::new("test2");
let agent2 = Agent::new("agent2", &world2);
let agent2 = Agent::builder("agent2");
let behavior2 = TimedMessage::new(
1,
"echo".to_owned(),
Expand All @@ -127,14 +130,17 @@ mod tests {
let parsed_file = read_to_string("test_logs_engine.log").expect("Unable to read log file");

// Define the line to check (excluding the timestamp)
let line_to_check = "World is syncing.";
let line_to_check = "Behavior is starting up.";

// Assert that the lines appear consecutively
assert!(
lines_appear_consecutively(&parsed_file, line_to_check),
"The lines do not appear consecutively"

Check warning on line 138 in arbiter-engine/src/universe.rs

View check run for this annotation

Codecov / codecov/patch

arbiter-engine/src/universe.rs#L138

Added line #L138 was not covered by tests
);
remove_file("test_logs_engine.log").expect("Unable to remove log file");
remove_file("test_logs_engine.log").expect(
"Unable to remove log
file",
);
}

fn lines_appear_consecutively(file_contents: &str, line_to_check: &str) -> bool {
Expand Down
12 changes: 5 additions & 7 deletions arbiter-engine/src/world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use tokio::spawn;

use self::{agent::AgentBuilder, machine::MachineInstruction};
use super::*;
use crate::{agent::Agent, machine::State, messager::Messager};
use crate::{agent::Agent, messager::Messager};

/// A world is a collection of agents that use the same type of provider, e.g.,
/// operate on the same blockchain or same `Environment`. The world is
Expand All @@ -35,13 +35,11 @@ use crate::{agent::Agent, machine::State, messager::Messager};
/// [`AgentBuilder`]s and when it does so, it creates [`Agent`]s that are now
/// connected to the world via a client ([`Arc<RevmMiddleware>`]) and a messager
/// ([`Messager`]).
#[derive(Debug)]

Check warning on line 38 in arbiter-engine/src/world.rs

View check run for this annotation

Codecov / codecov/patch

arbiter-engine/src/world.rs#L38

Added line #L38 was not covered by tests
pub struct World {
/// The identifier of the world.
pub id: String,

/// The state of the [`World`].
pub state: State,

/// The agents in the world.
pub agents: Option<HashMap<String, Agent>>,

Expand All @@ -53,11 +51,10 @@ pub struct World {
}

impl World {
/// Creates a new [World] with the given identifier and provider.
/// Creates a new [`World`] with the given identifier and provider.
pub fn new(id: &str) -> Self {
Self {
id: id.to_owned(),
state: State::Uninitialized,
agents: Some(HashMap::new()),
environment: Environment::builder().build(),
messager: Messager::new(),
Expand All @@ -74,7 +71,8 @@ impl World {
agents.insert(id.to_owned(), agent);
}

/// Runs the world through up to the [`State::Processing`] stage.
/// Runs all of the [`Agent`]s and their [`crate::machine::Behavior`]s in
/// the world in parallel.
pub async fn run(&mut self) -> Result<()> {
let mut tasks = vec![];
let agents = self
Expand Down

0 comments on commit 4b9d59e

Please sign in to comment.