Skip to content

Commit

Permalink
make DaService::get_extraction_proof async (#511)
Browse files Browse the repository at this point in the history
  • Loading branch information
bkolad authored Jul 18, 2023
1 parent a649a21 commit f64576c
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 22 deletions.
22 changes: 13 additions & 9 deletions adapters/celestia/src/celestia.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::cell::RefCell;
use std::fmt::{Display, Formatter};
use std::ops::Range;
use std::sync::{Arc, Mutex};

use borsh::{BorshDeserialize, BorshSerialize};
use nmt_rs::NamespacedHash;
Expand Down Expand Up @@ -223,21 +223,27 @@ pub struct NamespacedSharesResponse {
pub height: u64,
}

#[derive(Debug, PartialEq, Clone, Serialize, Deserialize, BorshSerialize, BorshDeserialize)]
#[derive(Debug, Clone, Serialize, Deserialize, BorshSerialize, BorshDeserialize)]
pub struct CelestiaHeader {
pub dah: DataAvailabilityHeader,
pub header: CompactHeader,
#[borsh_skip]
#[serde(skip)]
cached_prev_hash: RefCell<Option<TmHash>>,
cached_prev_hash: Arc<Mutex<Option<TmHash>>>,
}

impl PartialEq for CelestiaHeader {
fn eq(&self, other: &Self) -> bool {
self.dah == other.dah && self.header == other.header
}
}

impl CelestiaHeader {
pub fn new(dah: DataAvailabilityHeader, header: CompactHeader) -> Self {
Self {
dah,
header,
cached_prev_hash: RefCell::new(None),
cached_prev_hash: Arc::new(Mutex::new(None)),
}
}

Expand All @@ -257,13 +263,11 @@ impl BlockHeader for CelestiaHeader {
type Hash = TmHash;

fn prev_hash(&self) -> Self::Hash {
// Try to return the cached value
if let Some(hash) = self.cached_prev_hash.borrow().as_ref() {
let mut cached_hash = self.cached_prev_hash.lock().unwrap();
if let Some(hash) = cached_hash.as_ref() {
return hash.clone();
}
// If we reach this point, we know that the cach is empty - so there can't be any outstanding references to its value.
// That means its safe to borrow the cache mutably and populate it.
let mut cached_hash = self.cached_prev_hash.borrow_mut();

let hash =
<tendermint::block::Id as Protobuf<celestia_tm_version::types::BlockId>>::decode(
self.header.last_block_id.as_ref(),
Expand Down
2 changes: 1 addition & 1 deletion adapters/celestia/src/da_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ impl DaService for CelestiaService {
output
}

fn get_extraction_proof(
async fn get_extraction_proof(
&self,
block: &Self::FilteredBlock,
blobs: &[<Self::Spec as sov_rollup_interface::da::DaSpec>::BlobTransaction],
Expand Down
5 changes: 3 additions & 2 deletions examples/demo-prover/host/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,9 @@ async fn main() -> Result<(), anyhow::Error> {
let filtered_block = da_service.get_finalized_at(height).await?;
let header_hash = hex::encode(filtered_block.header.header.hash());
host.write_to_guest(&filtered_block.header);
let (blob_txs, inclusion_proof, completeness_proof) =
da_service.extract_relevant_txs_with_proof(&filtered_block);
let (blob_txs, inclusion_proof, completeness_proof) = da_service
.extract_relevant_txs_with_proof(&filtered_block)
.await;

host.write_to_guest(&inclusion_proof);
host.write_to_guest(&completeness_proof);
Expand Down
5 changes: 3 additions & 2 deletions examples/demo-rollup/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,8 +246,9 @@ async fn main() -> Result<(), anyhow::Error> {
}
let (next_state_root, _witness) = demo.end_slot();

let (inclusion_proof, completeness_proof) =
da_service.get_extraction_proof(&filtered_block, &blobs);
let (inclusion_proof, completeness_proof) = da_service
.get_extraction_proof(&filtered_block, &blobs)
.await;

let validity_condition = da_verifier
.verify_relevant_tx_list::<NoOpHasher>(
Expand Down
2 changes: 1 addition & 1 deletion examples/demo-rollup/src/rng_xfers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ impl DaService for RngDaService {
vec![blob]
}

fn get_extraction_proof(
async fn get_extraction_proof(
&self,
_block: &Self::FilteredBlock,
_blobs: &[<Self::Spec as DaSpec>::BlobTransaction],
Expand Down
2 changes: 1 addition & 1 deletion full-node/sov-sequencer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ mod tests {
todo!()
}

fn get_extraction_proof(
async fn get_extraction_proof(
&self,
_block: &Self::FilteredBlock,
_blobs: &[<Self::Spec as DaSpec>::BlobTransaction],
Expand Down
13 changes: 8 additions & 5 deletions rollup-interface/src/node/services/da.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ pub trait DaService {

/// Generate a proof that the relevant blob transactions have been extracted correctly from the DA layer
/// block.
fn get_extraction_proof(
async fn get_extraction_proof(
&self,
block: &Self::FilteredBlock,
blobs: &[<Self::Spec as DaSpec>::BlobTransaction],
Expand All @@ -64,7 +64,7 @@ pub trait DaService {
/// together with a range proof against the root of the namespaced-merkle-tree, demonstrating that the entire
/// rollup namespace has been covered.
#[allow(clippy::type_complexity)]
fn extract_relevant_txs_with_proof(
async fn extract_relevant_txs_with_proof(
&self,
block: &Self::FilteredBlock,
) -> (
Expand All @@ -74,8 +74,9 @@ pub trait DaService {
) {
let relevant_txs = self.extract_relevant_txs(block);

let (etx_proofs, rollup_row_proofs) =
self.get_extraction_proof(block, relevant_txs.as_slice());
let (etx_proofs, rollup_row_proofs) = self
.get_extraction_proof(block, relevant_txs.as_slice())
.await;

(relevant_txs, etx_proofs, rollup_row_proofs)
}
Expand All @@ -89,7 +90,9 @@ pub trait DaService {
/// `SlotData` is the subset of a DA layer block which is stored in the rollup's database.
/// At the very least, the rollup needs access to the hashes and headers of all DA layer blocks, but rollups
/// may choose to partial (or full) block data as well.
pub trait SlotData: Serialize + DeserializeOwned + PartialEq + core::fmt::Debug + Clone {
pub trait SlotData:
Serialize + DeserializeOwned + PartialEq + core::fmt::Debug + Clone + Send + Sync
{
/// The header type for a DA layer block as viewed by the rollup. This need not be identical
/// to the underlying rollup's header type, but it must be sufficient to reconstruct the block hash.
///
Expand Down
2 changes: 1 addition & 1 deletion rollup-interface/src/state_machine/da.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ impl<B: Buf> Read for CountedBufReader<B> {
}

/// A transaction on a data availability layer, including the address of the sender.
pub trait BlobTransactionTrait: Serialize + DeserializeOwned {
pub trait BlobTransactionTrait: Serialize + DeserializeOwned + Send + Sync {
/// The type of the raw data of the blob. For example, the "calldata" of an Ethereum rollup transaction
type Data: Buf;

Expand Down

0 comments on commit f64576c

Please sign in to comment.