Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(opt8n): Implement opt8n server #66

Merged
merged 16 commits into from
Aug 21, 2024
Merged

feat(opt8n): Implement opt8n server #66

merged 16 commits into from
Aug 21, 2024

Conversation

0xKitsune
Copy link
Collaborator

@0xKitsune 0xKitsune commented Aug 17, 2024

This PR introduces logic to spin up a server that proxies requests to Anvil as well as a dumpFixture endpoint that dumps an execution fixture to json when the route is called.

  pub async fn run(&self) -> color_eyre::Result<()> {
        let opt8n = Opt8n::new(
            Some(self.node_args.clone()),
            self.opt8n_args.output.clone(),
            self.opt8n_args.genesis.clone(),
        )
        .await?;

        let opt8n = Arc::new(Mutex::new(opt8n));

        let router = axum::Router::new()
            .route("/dump_fixture", axum::routing::post(dump_execution_fixture))
            .fallback(fallback_handler)
            .with_state(opt8n);

        let addr: SocketAddr = ([127, 0, 0, 1], 0).into();
        let listener = TcpListener::bind(addr).await?;

        axum::serve(listener, router.into_make_service()).await?;

        Ok(())
    }
}

async fn dump_execution_fixture(State(opt8n): State<Arc<Mutex<Opt8n>>>) -> Result<(), ServerError> {
    let mut opt8n = opt8n.lock().await;

    let mut new_blocks = opt8n.eth_api.backend.new_block_notifications();

    opt8n.mine_block().await;

    let block = new_blocks
        .next()
        .await
        .ok_or(eyre!("No new block"))
        .map_err(ServerError::Opt8nError)?;
    if let Some(block) = opt8n.eth_api.backend.get_block_by_hash(block.hash) {
        opt8n
            .generate_execution_fixture(block)
            .await
            .map_err(ServerError::Opt8nError)?;
    }

    Ok(())
}

@0xKitsune 0xKitsune marked this pull request as draft August 17, 2024 22:21
@0xKitsune 0xKitsune marked this pull request as ready for review August 20, 2024 21:02
bin/opt8n/src/main.rs Outdated Show resolved Hide resolved
@refcell refcell merged commit 822a8aa into main Aug 21, 2024
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants