Skip to content

Commit

Permalink
Restructuration of the get_blobs code. (#2817)
Browse files Browse the repository at this point in the history
  • Loading branch information
MathieuDutSik authored Nov 5, 2024
1 parent 2d82e54 commit a51879f
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 20 deletions.
11 changes: 6 additions & 5 deletions linera-core/src/chain_worker/state/attempted_changes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,7 @@ where
let required_blob_ids = executed_block.required_blob_ids();
// Verify that no unrelated blobs were provided.
self.state
.check_for_unneeded_blobs(&required_blob_ids, blobs)
.await?;
.check_for_unneeded_blobs(&required_blob_ids, blobs)?;
let remaining_required_blob_ids = required_blob_ids
.difference(&blobs.iter().map(|blob| blob.id()).collect())
.cloned()
Expand Down Expand Up @@ -300,13 +299,15 @@ where
let required_blob_ids = executed_block.required_blob_ids();
// Verify that no unrelated blobs were provided.
self.state
.check_for_unneeded_blobs(&required_blob_ids, blobs)
.await?;
.check_for_unneeded_blobs(&required_blob_ids, blobs)?;
let remaining_required_blob_ids = required_blob_ids
.difference(&blobs.iter().map(|blob| blob.id()).collect())
.cloned()
.collect();
let mut blobs_in_block = self.state.get_blobs(&remaining_required_blob_ids).await?;
let mut blobs_in_block = self
.state
.get_blobs_and_checks_storage(&remaining_required_blob_ids)
.await?;
blobs_in_block.extend_from_slice(blobs);

let certificate_hash = certificate.hash();
Expand Down
20 changes: 8 additions & 12 deletions linera-core/src/chain_worker/state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ where
}

/// Returns an error if unrelated blobs were provided.
async fn check_for_unneeded_blobs(
fn check_for_unneeded_blobs(
&self,
required_blob_ids: &HashSet<BlobId>,
blobs: &[Blob],
Expand All @@ -347,9 +347,12 @@ where
Ok(())
}

/// Returns the blobs requested by their `blob_ids` that are either in the chain manager's
/// pending blobs or in storage.
async fn get_blobs(&self, blob_ids: &HashSet<BlobId>) -> Result<Vec<Blob>, WorkerError> {
/// Returns the blobs requested by their `blob_ids` that are in the chain manager's pending blobs
/// and checks that they are otherwise in storage.
async fn get_blobs_and_checks_storage(
&self,
blob_ids: &HashSet<BlobId>,
) -> Result<Vec<Blob>, WorkerError> {
let pending_blobs = &self.chain.manager.get().pending_blobs;

let mut found_blobs = Vec::new();
Expand All @@ -361,14 +364,7 @@ where
missing_blob_ids.push(*blob_id);
}
}
let blobs = self.storage.read_blobs(&missing_blob_ids).await?;
let mut not_found_blob_ids = Vec::new();
for (blob, blob_id) in blobs.into_iter().zip(missing_blob_ids) {
match blob {
None => not_found_blob_ids.push(blob_id),
Some(blob) => found_blobs.push(blob),
}
}
let not_found_blob_ids = self.storage.missing_blobs(&missing_blob_ids).await?;

if not_found_blob_ids.is_empty() {
Ok(found_blobs)
Expand Down
5 changes: 2 additions & 3 deletions linera-core/src/chain_worker/state/temporary_changes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ impl<'state, StorageClient> ChainWorkerStateWithTemporaryChanges<'state, Storage
where
StorageClient: Storage + Clone + Send + Sync + 'static,
{
/// Creates a new [`ChainWorkerStateWithAttemptedChanges`] instance to temporarily change the
/// Creates a new [`ChainWorkerStateWithTemporaryChanges`] instance to temporarily change the
/// `state`.
pub(super) async fn new(state: &'state mut ChainWorkerState<StorageClient>) -> Self {
assert!(
Expand Down Expand Up @@ -207,8 +207,7 @@ where
// Verify that no unrelated blobs were provided.
let published_blob_ids = block.published_blob_ids();
self.0
.check_for_unneeded_blobs(&published_blob_ids, blobs)
.await?;
.check_for_unneeded_blobs(&published_blob_ids, blobs)?;
let missing_published_blob_ids = published_blob_ids
.difference(&blobs.iter().map(|blob| blob.id()).collect())
.cloned()
Expand Down

0 comments on commit a51879f

Please sign in to comment.