From bd374179404b214951d84cd395c6acc9dffba612 Mon Sep 17 00:00:00 2001 From: lilyjjo Date: Wed, 9 Oct 2024 15:12:09 +0200 Subject: [PATCH] add missing files for last commit --- Cargo.lock | 3 + charts/composer/templates/configmap.yaml | 2 +- crates/astria-grpc-mock/Cargo.toml | 7 + crates/astria-grpc-mock/src/lib.rs | 1 + .../src/mock_grpc_sequencer.rs | 121 ++++++++++++++++++ 5 files changed, 133 insertions(+), 1 deletion(-) create mode 100644 crates/astria-grpc-mock/src/mock_grpc_sequencer.rs diff --git a/Cargo.lock b/Cargo.lock index 5aefbc5dc0..c11d1aeff7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -743,6 +743,8 @@ name = "astria-grpc-mock" version = "0.1.0" dependencies = [ "assert-json-diff", + "astria-core", + "astria-eyre", "erased-serde 0.4.5", "futures", "indenter", @@ -750,6 +752,7 @@ dependencies = [ "serde", "serde_json", "tokio", + "tokio-stream", "tonic 0.10.2", "tracing", ] diff --git a/charts/composer/templates/configmap.yaml b/charts/composer/templates/configmap.yaml index 92e0d8ae5f..f083acc528 100644 --- a/charts/composer/templates/configmap.yaml +++ b/charts/composer/templates/configmap.yaml @@ -8,7 +8,7 @@ data: ASTRIA_COMPOSER_API_LISTEN_ADDR: "0.0.0.0:{{ .Values.ports.healthApi }}" ASTRIA_COMPOSER_GRPC_ADDR: "0.0.0.0:{{ .Values.ports.grpc }}" ASTRIA_COMPOSER_SEQUENCER_CHAIN_ID: "{{ tpl .Values.config.sequencerChainId . }}" - ASTRIA_COMPOSER_SEQUENCER_HTTP_URL: "{{ tpl .Values.config.sequencerRpc . }}" + ASTRIA_COMPOSER_COMETBFT_ENDPOINT: "{{ tpl .Values.config.sequencerRpc . }}" ASTRIA_COMPOSER_SEQUENCER_GRPC_URL: "{{ tpl .Values.config.sequencerGrpc . }}" ASTRIA_COMPOSER_ROLLUPS: "{{ include "composer.rollups" . }}" ASTRIA_COMPOSER_PRIVATE_KEY_FILE: "/var/secrets/{{ .Values.config.privateKey.secret.filename }}" diff --git a/crates/astria-grpc-mock/Cargo.toml b/crates/astria-grpc-mock/Cargo.toml index 97f7c6a5b8..aadab5d700 100644 --- a/crates/astria-grpc-mock/Cargo.toml +++ b/crates/astria-grpc-mock/Cargo.toml @@ -8,6 +8,7 @@ rust-version = "1.81.0" [dependencies] assert-json-diff = "2.0.2" +astria-eyre = { path = "../astria-eyre" } erased-serde = "0.4.4" futures.workspace = true indenter = "0.3.3" @@ -17,3 +18,9 @@ serde_json.workspace = true tokio.workspace = true tonic.workspace = true tracing.workspace = true +tokio-stream = { workspace = true, features = ["net"] } +astria-core = { path = "../astria-core", features = [ + "client", + "serde", + "server", +] } \ No newline at end of file diff --git a/crates/astria-grpc-mock/src/lib.rs b/crates/astria-grpc-mock/src/lib.rs index 3a5c898bcb..35eb8e70da 100644 --- a/crates/astria-grpc-mock/src/lib.rs +++ b/crates/astria-grpc-mock/src/lib.rs @@ -11,6 +11,7 @@ use std::any::Any; pub mod matcher; mod mock; +pub mod mock_grpc_sequencer; mod mock_server; mod mock_set; mod mounted_mock; diff --git a/crates/astria-grpc-mock/src/mock_grpc_sequencer.rs b/crates/astria-grpc-mock/src/mock_grpc_sequencer.rs new file mode 100644 index 0000000000..4851d5ab96 --- /dev/null +++ b/crates/astria-grpc-mock/src/mock_grpc_sequencer.rs @@ -0,0 +1,121 @@ +use std::{ + net::SocketAddr, + sync::Arc, +}; + +use astria_core::{ + self, + generated::sequencerblock::v1alpha1::{ + sequencer_service_server::{ + SequencerService, + SequencerServiceServer, + }, + FilteredSequencerBlock as RawFilteredSequencerBlock, + GetFilteredSequencerBlockRequest, + GetPendingNonceRequest, + GetPendingNonceResponse, + GetSequencerBlockRequest, + SequencerBlock as RawSequencerBlock, + }, +}; +use astria_eyre::eyre::{ + self, + WrapErr as _, +}; +use tokio::task::JoinHandle; +use tonic::{ + transport::Server, + Request, + Response, + Status, +}; + +use crate::{ + matcher::message_type, + response::constant_response, + Mock, + MockServer, +}; + +const GET_PENDING_NONCE_GRPC_NAME: &str = "get_pending_nonce"; + +pub struct MockGrpcSequencer { + _server: JoinHandle>, + pub mock_server: MockServer, + pub local_addr: SocketAddr, +} + +impl MockGrpcSequencer { + pub async fn spawn() -> Self { + use tokio_stream::wrappers::TcpListenerStream; + + let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); + let local_addr = listener.local_addr().unwrap(); + + let mock_server = MockServer::new(); + + let server = { + let sequencer_service = SequencerServiceImpl(mock_server.clone()); + tokio::spawn(async move { + Server::builder() + .add_service(SequencerServiceServer::new(sequencer_service)) + .serve_with_incoming(TcpListenerStream::new(listener)) + .await + .wrap_err("gRPC sequencer server failed") + }) + }; + Self { + _server: server, + mock_server, + local_addr, + } + } + + pub async fn mount_pending_nonce_response( + &self, + nonce_to_mount: u32, + debug_name: impl Into, + ) { + let resp = GetPendingNonceResponse { + inner: nonce_to_mount, + }; + Mock::for_rpc_given( + GET_PENDING_NONCE_GRPC_NAME, + message_type::(), + ) + .respond_with(constant_response(resp)) + .up_to_n_times(1) + .expect(1) + .with_name(debug_name) + .mount(&self.mock_server) + .await; + } +} + +struct SequencerServiceImpl(MockServer); + +#[tonic::async_trait] +impl SequencerService for SequencerServiceImpl { + async fn get_sequencer_block( + self: Arc, + _request: Request, + ) -> Result, Status> { + unimplemented!() + } + + async fn get_filtered_sequencer_block( + self: Arc, + _request: Request, + ) -> Result, Status> { + unimplemented!() + } + + async fn get_pending_nonce( + self: Arc, + request: Request, + ) -> Result, Status> { + self.0 + .handle_request(GET_PENDING_NONCE_GRPC_NAME, request) + .await + } +}