ethereum-optimism / tests

Standard Tests for OP Stack Component Implementations.
https://static.optimism.io/tests
MIT License
16 stars 11 forks source link

feat(opt8n): Implement `opt8n server` #66

Closed 0xKitsune closed 2 months ago

0xKitsune commented 2 months ago

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(())
}