From 397d55d83f72c25472722b3f811ca08b7f930fd0 Mon Sep 17 00:00:00 2001 From: Blazej Kolad Date: Mon, 17 Jul 2023 22:27:13 +0200 Subject: [PATCH] Make `DaService` async (#508) --- Cargo.toml | 1 + adapters/celestia/Cargo.toml | 1 + adapters/celestia/src/da_service.rs | 159 +++++++++--------- examples/demo-prover/Cargo.lock | 44 ++--- examples/demo-prover/host/src/main.rs | 3 +- examples/demo-prover/methods/guest/Cargo.lock | 38 +++-- examples/demo-rollup/Cargo.toml | 1 + examples/demo-rollup/src/main.rs | 15 +- examples/demo-rollup/src/rng_xfers.rs | 15 +- full-node/sov-sequencer/Cargo.toml | 1 + full-node/sov-sequencer/src/lib.rs | 15 +- rollup-interface/Cargo.toml | 1 + rollup-interface/src/node/services/da.rs | 17 +- 13 files changed, 158 insertions(+), 153 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 15106f7599..44dd2faa31 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -47,6 +47,7 @@ rust-version = "1.66" jmt = "0.6.0" # External dependencies +async-trait = "0.1.71" anyhow = "1.0.68" borsh = { version = "0.10.3", features = ["rc", "bytes"] } byteorder = "1.4.3" diff --git a/adapters/celestia/Cargo.toml b/adapters/celestia/Cargo.toml index cc21164514..6242528d4c 100644 --- a/adapters/celestia/Cargo.toml +++ b/adapters/celestia/Cargo.toml @@ -14,6 +14,7 @@ tendermint = "0.32" tendermint-proto = "0.32" # Convenience +async-trait = { workspace = true } anyhow = { workspace = true } sha2 = { workspace = true } base64 = "0.13.1" diff --git a/adapters/celestia/src/da_service.rs b/adapters/celestia/src/da_service.rs index eff53ee0e6..fe034906c7 100644 --- a/adapters/celestia/src/da_service.rs +++ b/adapters/celestia/src/da_service.rs @@ -1,8 +1,7 @@ use std::collections::HashMap; -use std::future::Future; -use std::pin::Pin; use std::str::FromStr; +use async_trait::async_trait; use jsonrpsee::core::client::ClientT; use jsonrpsee::core::params::ArrayParams; use jsonrpsee::http_client::{HeaderMap, HttpClient}; @@ -113,6 +112,7 @@ const fn default_request_timeout_seconds() -> u64 { 60 } +#[async_trait] impl DaService for CelestiaService { type RuntimeConfig = DaServiceConfig; @@ -120,11 +120,9 @@ impl DaService for CelestiaService { type FilteredBlock = FilteredCelestiaBlock; - type Future = Pin> + Send>>; - type Error = BoxError; - fn new(config: Self::RuntimeConfig, chain_params: RollupParams) -> Self { + async fn new(config: Self::RuntimeConfig, chain_params: RollupParams) -> Self { let client = { let mut headers = HeaderMap::new(); headers.insert( @@ -147,74 +145,70 @@ impl DaService for CelestiaService { Self::with_client(client, chain_params.namespace) } - fn get_finalized_at(&self, height: u64) -> Self::Future { + async fn get_finalized_at(&self, height: u64) -> Result { let client = self.client.clone(); let rollup_namespace = self.rollup_namespace; - Box::pin(async move { - let _span = span!(Level::TRACE, "fetching finalized block", height = height); - // Fetch the header and relevant shares via RPC - info!("Fetching header at height={}...", height); - let header = client - .request::("header.GetByHeight", vec![height]) - .await?; - debug!(header_result = ?header); - debug!("Fetching shares..."); - let (rollup_shares, tx_data) = - fetch_needed_shares_by_header(rollup_namespace, &client, &header).await?; - - debug!("Fetching EDS..."); - // Fetch entire extended data square - let data_square = client - .request::( - "share.GetEDS", - vec![header - .get("dah") - .ok_or(BoxError::msg("missing 'dah' in block header"))?], - ) - .await?; - let unmarshalled_header: CelestiaHeaderResponse = serde_json::from_value(header)?; - let dah: DataAvailabilityHeader = unmarshalled_header.dah.try_into()?; - debug!("Parsing namespaces..."); - // Parse out all of the rows containing etxs - let etx_rows = - get_rows_containing_namespace(PFB_NAMESPACE, &dah, data_square.rows()?.into_iter()) - .await?; - // Parse out all of the rows containing rollup data - let rollup_rows = get_rows_containing_namespace( - rollup_namespace, - &dah, - data_square.rows()?.into_iter(), + let _span = span!(Level::TRACE, "fetching finalized block", height = height); + // Fetch the header and relevant shares via RPC + info!("Fetching header at height={}...", height); + let header = client + .request::("header.GetByHeight", vec![height]) + .await?; + debug!(header_result = ?header); + debug!("Fetching shares..."); + let (rollup_shares, tx_data) = + fetch_needed_shares_by_header(rollup_namespace, &client, &header).await?; + + debug!("Fetching EDS..."); + // Fetch entire extended data square + let data_square = client + .request::( + "share.GetEDS", + vec![header + .get("dah") + .ok_or(BoxError::msg("missing 'dah' in block header"))?], ) .await?; - debug!("Decoding pfb protobufs..."); - // Parse out the pfds and store them for later retrieval - let pfds = parse_pfb_namespace(tx_data)?; - let mut pfd_map = HashMap::new(); - for tx in pfds { - for (idx, nid) in tx.0.namespace_ids.iter().enumerate() { - if nid == &rollup_namespace.0[..] { - // TODO: Retool this map to avoid cloning txs - pfd_map.insert(tx.0.share_commitments[idx].clone(), tx.clone()); - } + let unmarshalled_header: CelestiaHeaderResponse = serde_json::from_value(header)?; + let dah: DataAvailabilityHeader = unmarshalled_header.dah.try_into()?; + debug!("Parsing namespaces..."); + // Parse out all of the rows containing etxs + let etx_rows = + get_rows_containing_namespace(PFB_NAMESPACE, &dah, data_square.rows()?.into_iter()) + .await?; + // Parse out all of the rows containing rollup data + let rollup_rows = + get_rows_containing_namespace(rollup_namespace, &dah, data_square.rows()?.into_iter()) + .await?; + + debug!("Decoding pfb protobufs..."); + // Parse out the pfds and store them for later retrieval + let pfds = parse_pfb_namespace(tx_data)?; + let mut pfd_map = HashMap::new(); + for tx in pfds { + for (idx, nid) in tx.0.namespace_ids.iter().enumerate() { + if nid == &rollup_namespace.0[..] { + // TODO: Retool this map to avoid cloning txs + pfd_map.insert(tx.0.share_commitments[idx].clone(), tx.clone()); } } + } - let filtered_block = FilteredCelestiaBlock { - header: CelestiaHeader::new(dah, unmarshalled_header.header.into()), - rollup_data: rollup_shares, - relevant_pfbs: pfd_map, - rollup_rows, - pfb_rows: etx_rows, - }; + let filtered_block = FilteredCelestiaBlock { + header: CelestiaHeader::new(dah, unmarshalled_header.header.into()), + rollup_data: rollup_shares, + relevant_pfbs: pfd_map, + rollup_rows, + pfb_rows: etx_rows, + }; - Ok::(filtered_block) - }) + Ok::(filtered_block) } - fn get_block_at(&self, height: u64) -> Self::Future { - self.get_finalized_at(height) + async fn get_block_at(&self, height: u64) -> Result { + self.get_finalized_at(height).await } fn extract_relevant_txs( @@ -261,7 +255,7 @@ impl DaService for CelestiaService { (etx_proofs.0, rollup_row_proofs.0) } - fn send_transaction(&self, blob: &[u8]) -> ::Future<()> { + async fn send_transaction(&self, blob: &[u8]) -> Result<(), Self::Error> { // https://node-rpc-docs.celestia.org/ let client = self.client.clone(); info!("Sending {} bytes of raw data to Celestia.", blob.len()); @@ -271,26 +265,24 @@ impl DaService for CelestiaService { // We factor extra share to be occupied for namespace, which is pessimistic let gas_limit = get_gas_limit_for_bytes(blob.len()); - Box::pin(async move { - let mut params = ArrayParams::new(); - params.insert(namespace)?; - params.insert(blob)?; - params.insert(fee.to_string())?; - params.insert(gas_limit)?; - // Note, we only deserialize what we can use, other fields might be left over - let response = client - .request::("state.SubmitPayForBlob", params) - .await?; - if !response.is_success() { - anyhow::bail!("Error returned from Celestia node: {:?}", response); - } - debug!("Response after submitting blob: {:?}", response); - info!( - "Blob has been submitted to Celestia. tx-hash={}", - response.tx_hash, - ); - Ok::<(), BoxError>(()) - }) + let mut params = ArrayParams::new(); + params.insert(namespace)?; + params.insert(blob)?; + params.insert(fee.to_string())?; + params.insert(gas_limit)?; + // Note, we only deserialize what we can use, other fields might be left over + let response = client + .request::("state.SubmitPayForBlob", params) + .await?; + if !response.is_success() { + anyhow::bail!("Error returned from Celestia node: {:?}", response); + } + debug!("Response after submitting blob: {:?}", response); + info!( + "Blob has been submitted to Celestia. tx-hash={}", + response.tx_hash, + ); + Ok::<(), BoxError>(()) } } @@ -406,7 +398,8 @@ mod tests { RollupParams { namespace: NamespaceId(namespace), }, - ); + ) + .await; (mock_server, config, da_service, namespace) } diff --git a/examples/demo-prover/Cargo.lock b/examples/demo-prover/Cargo.lock index 6818778aac..27285fcd8a 100644 --- a/examples/demo-prover/Cargo.lock +++ b/examples/demo-prover/Cargo.lock @@ -111,13 +111,13 @@ checksum = "8da52d66c7071e2e3fa2a1e5c6d088fec47b593032b254f5e980de8ea54454d6" [[package]] name = "async-trait" -version = "0.1.68" +version = "0.1.71" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b9ccdd8f2a161be9bd5c023df56f1b2a0bd1d83872ae53b71a84a12c9bf6e842" +checksum = "a564d521dd56509c4c47480d00b80ee55f7e385ae48db5744c67ad50c92d2ebf" dependencies = [ "proc-macro2", "quote", - "syn 2.0.16", + "syn 2.0.26", ] [[package]] @@ -300,7 +300,7 @@ checksum = "fdde5c9cd29ebd706ce1b35600920a33550e402fc998a2e53ad3b42c3c47a192" dependencies = [ "proc-macro2", "quote", - "syn 2.0.16", + "syn 2.0.26", ] [[package]] @@ -449,7 +449,7 @@ dependencies = [ "heck", "proc-macro2", "quote", - "syn 2.0.16", + "syn 2.0.26", ] [[package]] @@ -932,7 +932,7 @@ checksum = "89ca545a94061b6365f2c7355b4b32bd20df3ff95f02da9329b34ccc3bd6ee72" dependencies = [ "proc-macro2", "quote", - "syn 2.0.16", + "syn 2.0.26", ] [[package]] @@ -1456,6 +1456,7 @@ name = "jupiter" version = "0.1.0" dependencies = [ "anyhow", + "async-trait", "base64 0.13.1", "bech32", "borsh", @@ -1814,7 +1815,7 @@ checksum = "a948666b637a0f465e8564c73e89d4dde00d72d4d473cc972f390fc3dcee7d9c" dependencies = [ "proc-macro2", "quote", - "syn 2.0.16", + "syn 2.0.26", ] [[package]] @@ -1996,9 +1997,9 @@ dependencies = [ [[package]] name = "proc-macro2" -version = "1.0.58" +version = "1.0.66" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fa1fb82fc0c281dd9671101b66b771ebbe1eaf967b96ac8740dcba4b70005ca8" +checksum = "18fb31db3f9bddb2ea821cde30a9f70117e3f119938b5ee630b7403aa6e2ead9" dependencies = [ "unicode-ident", ] @@ -2073,9 +2074,9 @@ dependencies = [ [[package]] name = "quote" -version = "1.0.27" +version = "1.0.31" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8f4f29d145265ec1c483c7c654450edde0bfe043d3938d6972630663356d9500" +checksum = "5fe8a65d69dd0808184ebb5f836ab526bb259db23c657efa38711b1072ee47f0" dependencies = [ "proc-macro2", ] @@ -2653,7 +2654,7 @@ checksum = "8c805777e3930c8883389c602315a24224bcc738b63905ef87cd1420353ea93e" dependencies = [ "proc-macro2", "quote", - "syn 2.0.16", + "syn 2.0.26", ] [[package]] @@ -2675,7 +2676,7 @@ checksum = "bcec881020c684085e55a25f7fd888954d56609ef363479dc5a1305eb0d40cab" dependencies = [ "proc-macro2", "quote", - "syn 2.0.16", + "syn 2.0.26", ] [[package]] @@ -2986,6 +2987,7 @@ name = "sov-rollup-interface" version = "0.1.0" dependencies = [ "anyhow", + "async-trait", "borsh", "bytes", "hex", @@ -3126,9 +3128,9 @@ dependencies = [ [[package]] name = "syn" -version = "2.0.16" +version = "2.0.26" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a6f671d4b5ffdb8eadec19c0ae67fe2639df8684bd7bc4b83d986b8db549cf01" +checksum = "45c3457aacde3c65315de5031ec191ce46604304d2446e803d71ade03308d970" dependencies = [ "proc-macro2", "quote", @@ -3212,7 +3214,7 @@ checksum = "f9456a42c5b0d803c8cd86e73dd7cc9edd429499f37a3550d286d5e86720569f" dependencies = [ "proc-macro2", "quote", - "syn 2.0.16", + "syn 2.0.26", ] [[package]] @@ -3293,7 +3295,7 @@ checksum = "630bdcf245f78637c13ec01ffae6187cca34625e8c63150d424b59e55af2675e" dependencies = [ "proc-macro2", "quote", - "syn 2.0.16", + "syn 2.0.26", ] [[package]] @@ -3430,7 +3432,7 @@ checksum = "0f57e3ca2a01450b1a921183a9c9cbfda207fd822cef4ccb00a65402cbba7a74" dependencies = [ "proc-macro2", "quote", - "syn 2.0.16", + "syn 2.0.26", ] [[package]] @@ -3585,7 +3587,7 @@ dependencies = [ "once_cell", "proc-macro2", "quote", - "syn 2.0.16", + "syn 2.0.26", "wasm-bindgen-shared", ] @@ -3619,7 +3621,7 @@ checksum = "e128beba882dd1eb6200e1dc92ae6c5dbaa4311aa7bb211ca035779e5efc39f8" dependencies = [ "proc-macro2", "quote", - "syn 2.0.16", + "syn 2.0.26", "wasm-bindgen-backend", "wasm-bindgen-shared", ] @@ -3874,7 +3876,7 @@ checksum = "ce36e65b0d2999d2aafac989fb249189a141aee1f53c612c1f37d72631959f69" dependencies = [ "proc-macro2", "quote", - "syn 2.0.16", + "syn 2.0.26", ] [[package]] diff --git a/examples/demo-prover/host/src/main.rs b/examples/demo-prover/host/src/main.rs index 81eace3ea3..409e1d7166 100644 --- a/examples/demo-prover/host/src/main.rs +++ b/examples/demo-prover/host/src/main.rs @@ -50,7 +50,8 @@ async fn main() -> Result<(), anyhow::Error> { RollupParams { namespace: ROLLUP_NAMESPACE, }, - ); + ) + .await; let sequencer_private_key = DefaultPrivateKey::generate(); diff --git a/examples/demo-prover/methods/guest/Cargo.lock b/examples/demo-prover/methods/guest/Cargo.lock index 646311d060..fcd81b2f51 100644 --- a/examples/demo-prover/methods/guest/Cargo.lock +++ b/examples/demo-prover/methods/guest/Cargo.lock @@ -63,13 +63,13 @@ checksum = "8da52d66c7071e2e3fa2a1e5c6d088fec47b593032b254f5e980de8ea54454d6" [[package]] name = "async-trait" -version = "0.1.68" +version = "0.1.71" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b9ccdd8f2a161be9bd5c023df56f1b2a0bd1d83872ae53b71a84a12c9bf6e842" +checksum = "a564d521dd56509c4c47480d00b80ee55f7e385ae48db5744c67ad50c92d2ebf" dependencies = [ "proc-macro2", "quote", - "syn 2.0.15", + "syn 2.0.26", ] [[package]] @@ -232,7 +232,7 @@ checksum = "fdde5c9cd29ebd706ce1b35600920a33550e402fc998a2e53ad3b42c3c47a192" dependencies = [ "proc-macro2", "quote", - "syn 2.0.15", + "syn 2.0.26", ] [[package]] @@ -708,7 +708,7 @@ checksum = "89ca545a94061b6365f2c7355b4b32bd20df3ff95f02da9329b34ccc3bd6ee72" dependencies = [ "proc-macro2", "quote", - "syn 2.0.15", + "syn 2.0.26", ] [[package]] @@ -1179,6 +1179,7 @@ name = "jupiter" version = "0.1.0" dependencies = [ "anyhow", + "async-trait", "base64 0.13.1", "bech32", "borsh", @@ -1384,7 +1385,7 @@ checksum = "a948666b637a0f465e8564c73e89d4dde00d72d4d473cc972f390fc3dcee7d9c" dependencies = [ "proc-macro2", "quote", - "syn 2.0.15", + "syn 2.0.26", ] [[package]] @@ -1544,9 +1545,9 @@ dependencies = [ [[package]] name = "proc-macro2" -version = "1.0.56" +version = "1.0.66" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2b63bdb0cd06f1f4dedf69b254734f9b45af66e4a031e42a7480257d9898b435" +checksum = "18fb31db3f9bddb2ea821cde30a9f70117e3f119938b5ee630b7403aa6e2ead9" dependencies = [ "unicode-ident", ] @@ -1607,9 +1608,9 @@ dependencies = [ [[package]] name = "quote" -version = "1.0.26" +version = "1.0.31" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4424af4bf778aae2051a77b60283332f386554255d722233d09fbfc7e30da2fc" +checksum = "5fe8a65d69dd0808184ebb5f836ab526bb259db23c657efa38711b1072ee47f0" dependencies = [ "proc-macro2", ] @@ -2056,7 +2057,7 @@ checksum = "291a097c63d8497e00160b166a967a4a79c64f3facdd01cbd7502231688d77df" dependencies = [ "proc-macro2", "quote", - "syn 2.0.15", + "syn 2.0.26", ] [[package]] @@ -2078,7 +2079,7 @@ checksum = "bcec881020c684085e55a25f7fd888954d56609ef363479dc5a1305eb0d40cab" dependencies = [ "proc-macro2", "quote", - "syn 2.0.15", + "syn 2.0.26", ] [[package]] @@ -2328,6 +2329,7 @@ name = "sov-rollup-interface" version = "0.1.0" dependencies = [ "anyhow", + "async-trait", "borsh", "bytes", "hex", @@ -2429,9 +2431,9 @@ dependencies = [ [[package]] name = "syn" -version = "2.0.15" +version = "2.0.26" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a34fcf3e8b60f57e6a14301a2e916d323af98b0ea63c599441eec8558660c822" +checksum = "45c3457aacde3c65315de5031ec191ce46604304d2446e803d71ade03308d970" dependencies = [ "proc-macro2", "quote", @@ -2515,7 +2517,7 @@ checksum = "f9456a42c5b0d803c8cd86e73dd7cc9edd429499f37a3550d286d5e86720569f" dependencies = [ "proc-macro2", "quote", - "syn 2.0.15", + "syn 2.0.26", ] [[package]] @@ -2584,7 +2586,7 @@ checksum = "630bdcf245f78637c13ec01ffae6187cca34625e8c63150d424b59e55af2675e" dependencies = [ "proc-macro2", "quote", - "syn 2.0.15", + "syn 2.0.26", ] [[package]] @@ -2687,7 +2689,7 @@ checksum = "0f57e3ca2a01450b1a921183a9c9cbfda207fd822cef4ccb00a65402cbba7a74" dependencies = [ "proc-macro2", "quote", - "syn 2.0.15", + "syn 2.0.26", ] [[package]] @@ -3078,7 +3080,7 @@ checksum = "ce36e65b0d2999d2aafac989fb249189a141aee1f53c612c1f37d72631959f69" dependencies = [ "proc-macro2", "quote", - "syn 2.0.15", + "syn 2.0.26", ] [[package]] diff --git a/examples/demo-rollup/Cargo.toml b/examples/demo-rollup/Cargo.toml index a49558f984..a7b2332aa0 100644 --- a/examples/demo-rollup/Cargo.toml +++ b/examples/demo-rollup/Cargo.toml @@ -10,6 +10,7 @@ resolver = "2" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +async-trait = { workspace = true } anyhow = { workspace = true } borsh = { workspace = true, features = ["bytes"] } jsonrpsee = { workspace = true, features = ["http-client", "server"] } diff --git a/examples/demo-rollup/src/main.rs b/examples/demo-rollup/src/main.rs index 11d8632bdb..43baabd8c1 100644 --- a/examples/demo-rollup/src/main.rs +++ b/examples/demo-rollup/src/main.rs @@ -133,12 +133,15 @@ async fn main() -> Result<(), anyhow::Error> { let ledger_db = initialize_ledger(&rollup_config.runner.storage.path); // Initialize the Celestia service using the DaService interface - let da_service = Arc::new(CelestiaService::new( - rollup_config.da.clone(), - RollupParams { - namespace: ROLLUP_NAMESPACE, - }, - )); + let da_service = Arc::new( + CelestiaService::new( + rollup_config.da.clone(), + RollupParams { + namespace: ROLLUP_NAMESPACE, + }, + ) + .await, + ); // Our state transition function implements the StateTransitionRunner interface, // so we use that to initialize the STF diff --git a/examples/demo-rollup/src/rng_xfers.rs b/examples/demo-rollup/src/rng_xfers.rs index 81a32afa53..5338ec7ae6 100644 --- a/examples/demo-rollup/src/rng_xfers.rs +++ b/examples/demo-rollup/src/rng_xfers.rs @@ -1,7 +1,6 @@ use std::env; -use std::future::Future; -use std::pin::Pin; +use async_trait::async_trait; use borsh::ser::BorshSerialize; use const_rollup_config::SEQUENCER_DA_ADDRESS; use demo_stf::runtime::Runtime; @@ -97,21 +96,21 @@ impl DaSpec for RngDaSpec { type ChainParams = (); } +#[async_trait] impl DaService for RngDaService { type RuntimeConfig = (); type Spec = RngDaSpec; type FilteredBlock = TestBlock; - type Future = Pin> + Send>>; type Error = anyhow::Error; - fn new( + async fn new( _config: Self::RuntimeConfig, _chain_params: ::ChainParams, ) -> Self { RngDaService::new() } - fn get_finalized_at(&self, height: u64) -> Self::Future { + async fn get_finalized_at(&self, height: u64) -> Result { let num_bytes = height.to_le_bytes(); let mut barray = [0u8; 32]; barray[..num_bytes.len()].copy_from_slice(&num_bytes); @@ -124,10 +123,10 @@ impl DaService for RngDaService { height, }; - Box::pin(async move { Ok(block) }) + Ok(block) } - fn get_block_at(&self, _height: u64) -> Self::Future { + async fn get_block_at(&self, _height: u64) -> Result { unimplemented!() } @@ -167,7 +166,7 @@ impl DaService for RngDaService { unimplemented!() } - fn send_transaction(&self, _blob: &[u8]) -> Self::Future<()> { + async fn send_transaction(&self, _blob: &[u8]) -> Result<(), Self::Error> { unimplemented!() } } diff --git a/full-node/sov-sequencer/Cargo.toml b/full-node/sov-sequencer/Cargo.toml index c310344753..325596c69b 100644 --- a/full-node/sov-sequencer/Cargo.toml +++ b/full-node/sov-sequencer/Cargo.toml @@ -22,5 +22,6 @@ tracing = { workspace = true } sov-rollup-interface = { path = "../../rollup-interface" } [dev-dependencies] +async-trait = { workspace = true } sov-rollup-interface = { path = "../../rollup-interface", features = ["mocks"] } tokio = { workspace = true } \ No newline at end of file diff --git a/full-node/sov-sequencer/src/lib.rs b/full-node/sov-sequencer/src/lib.rs index 66e043765d..4712f8e6a2 100644 --- a/full-node/sov-sequencer/src/lib.rs +++ b/full-node/sov-sequencer/src/lib.rs @@ -105,11 +105,10 @@ pub enum SubmitTransactionResponse { #[cfg(test)] mod tests { - use std::future::Future; - use std::pin::Pin; use std::sync::Arc; use anyhow::bail; + use async_trait::async_trait; use sov_rollup_interface::da::DaSpec; use sov_rollup_interface::mocks::{MockDaSpec, TestBlock}; @@ -135,25 +134,25 @@ mod tests { } } + #[async_trait] impl DaService for MockDaService { type RuntimeConfig = (); type Spec = MockDaSpec; type FilteredBlock = TestBlock; - type Future = Pin> + Send>>; type Error = anyhow::Error; - fn new( + async fn new( _config: Self::RuntimeConfig, _chain_params: ::ChainParams, ) -> Self { MockDaService::new() } - fn get_finalized_at(&self, _height: u64) -> Self::Future { + async fn get_finalized_at(&self, _height: u64) -> Result { todo!() } - fn get_block_at(&self, _height: u64) -> Self::Future { + async fn get_block_at(&self, _height: u64) -> Result { todo!() } @@ -175,9 +174,9 @@ mod tests { todo!() } - fn send_transaction(&self, blob: &[u8]) -> Self::Future<()> { + async fn send_transaction(&self, blob: &[u8]) -> Result<(), Self::Error> { self.submitted.lock().unwrap().push(blob.to_vec()); - Box::pin(async move { Ok(()) }) + Ok(()) } } diff --git a/rollup-interface/Cargo.toml b/rollup-interface/Cargo.toml index c004f389c2..f0e0693ea4 100644 --- a/rollup-interface/Cargo.toml +++ b/rollup-interface/Cargo.toml @@ -18,6 +18,7 @@ exclude = [ # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +async-trait = { workspace = true } borsh = { workspace = true, features = ["rc"] } serde = { workspace = true } bytes = { workspace = true } diff --git a/rollup-interface/src/node/services/da.rs b/rollup-interface/src/node/services/da.rs index be2378e6aa..3825bf6fb3 100644 --- a/rollup-interface/src/node/services/da.rs +++ b/rollup-interface/src/node/services/da.rs @@ -1,7 +1,7 @@ //! The da module defines traits used by the full node to interact with the DA layer. use std::fmt; -use std::future::Future; +use async_trait::async_trait; use serde::de::DeserializeOwned; use serde::Serialize; @@ -12,6 +12,7 @@ use crate::da::{BlockHeaderTrait, DaSpec}; /// /// The DaService has two responsibilities - fetching data from the DA layer, transforming the /// data into a representation that can be efficiently verified in circuit. +#[async_trait] pub trait DaService { /// A handle to the types used by the DA layer. type RuntimeConfig: DeserializeOwned; @@ -22,23 +23,23 @@ pub trait DaService { /// A DA layer block, possibly excluding some irrelevant information. type FilteredBlock: SlotData::BlockHeader>; - /// The output of an async call. Used in place of a dependency on async_trait. - type Future: Future> + Send; - /// The error type for fallible methods. type Error: fmt::Debug + Send + Sync; /// Create a new instance of the DaService - fn new(config: Self::RuntimeConfig, chain_params: ::ChainParams) -> Self; + async fn new( + config: Self::RuntimeConfig, + chain_params: ::ChainParams, + ) -> Self; /// Retrieve the data for the given height, waiting for it to be /// finalized if necessary. The block, once returned, must not be reverted /// without a consensus violation. - fn get_finalized_at(&self, height: u64) -> Self::Future; + async fn get_finalized_at(&self, height: u64) -> Result; /// Fetch the block at the given height, waiting for one to be mined if necessary. /// The returned block may not be final, and can be reverted without a consensus violation - fn get_block_at(&self, height: u64) -> Self::Future; + async fn get_block_at(&self, height: u64) -> Result; /// Extract the relevant transactions from a block. For example, this method might return /// all of the blob transactions in rollup's namespace on Celestia. @@ -82,7 +83,7 @@ pub trait DaService { /// Send a transaction directly to the DA layer. /// blob is the serialized and signed transaction. /// Returns nothing if the transaction was successfully sent. - fn send_transaction(&self, blob: &[u8]) -> Self::Future<()>; + async fn send_transaction(&self, blob: &[u8]) -> Result<(), Self::Error>; } /// `SlotData` is the subset of a DA layer block which is stored in the rollup's database.