Skip to content

Commit

Permalink
chore(simulator): use transaction endpoint directly instead of spinning
Browse files Browse the repository at this point in the history
servers
  • Loading branch information
cfcosta committed Aug 20, 2024
1 parent d137174 commit 6848646
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 24 deletions.
3 changes: 3 additions & 0 deletions node/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ use tracing::warn;
mod context;
mod route;

pub use context::Context;
pub use route::v0;

#[derive(Debug, Clone, Parser)]
pub struct Config {
#[clap(long)]
Expand Down
6 changes: 3 additions & 3 deletions node/src/route/v0/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ pub async fn health() -> &'static str {
"OK"
}

async fn transaction(transaction: Transaction, ctx: Context) -> Result<Response, Error> {
pub async fn transaction(transaction: Transaction, ctx: &mut Context) -> Result<Response, Error> {
let mut outputs = vec![];

if !transaction.is_balanced() {
Expand Down Expand Up @@ -92,11 +92,11 @@ async fn transaction(transaction: Transaction, ctx: Context) -> Result<Response,
}

pub async fn rpc(
State(ctx): State<Context>,
State(mut ctx): State<Context>,
Json(request): Json<Request>,
) -> axum::response::Response {
match request {
Request::V0(V0Request::Transaction(t)) => match transaction(t, ctx).await {
Request::V0(V0Request::Transaction(t)) => match transaction(t, &mut ctx).await {
Ok(response) => Json(response).into_response(),
Err(e) => Json(e).into_response(),
},
Expand Down
25 changes: 6 additions & 19 deletions simulator/src/agents/delegate.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
use color_eyre::eyre::{ErrReport, Result};
use color_eyre::eyre::Result;
use mugraph_client::prelude::*;
use rand::prelude::*;
use mugraph_node::{v0::transaction, Context};
use rand_chacha::ChaCha20Rng;
use tokio::task;

pub struct Delegate {
pub keypair: Keypair,
pub rng: ChaCha20Rng,
pub context: Context,
}

impl Delegate {
pub fn new(mut rng: ChaCha20Rng) -> Self {
Self {
keypair: Keypair::random(&mut rng),
context: Context::new(&mut rng).unwrap(),
rng,
}
}
Expand All @@ -34,21 +35,7 @@ impl Delegate {
Ok(note)
}

pub fn spawn(&mut self) {
let seed = self.rng.gen();
let keypair = self.keypair;

task::spawn(async move {
let config = mugraph_node::Config {
seed: Some(seed),
secret_key: Some(keypair.secret_key.to_string()),
public_key: Some(keypair.public_key.to_string()),
..Default::default()
};

mugraph_node::start(&config).await?;

Ok::<_, ErrReport>(())
});
pub async fn recv_transaction(&mut self, tx: Transaction) -> Result<Response> {
Ok(transaction(tx, &mut self.context).await?)
}
}
2 changes: 0 additions & 2 deletions simulator/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,6 @@ impl Simulator {
));
}

delegate.spawn();

Ok(Self {
delegate,
rng,
Expand Down

0 comments on commit 6848646

Please sign in to comment.