Skip to content

Commit 4d22e61

Browse files
committed
Check block cache on create_cross_chain_requests
1 parent 84d8cbc commit 4d22e61

File tree

1 file changed

+38
-14
lines changed

1 file changed

+38
-14
lines changed

linera-core/src/chain_worker/state.rs

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -538,26 +538,50 @@ where
538538
})?;
539539
hashes.push(hash);
540540
}
541-
let certificates = self.storage.read_certificates(hashes.clone()).await?;
542-
let certificates = match ResultReadCertificates::new(certificates, hashes) {
543-
ResultReadCertificates::Certificates(certificates) => certificates,
544-
ResultReadCertificates::InvalidHashes(hashes) => {
545-
return Err(WorkerError::ReadCertificatesError(hashes))
541+
542+
let mut uncached_hashes = Vec::new();
543+
let mut height_to_blocks: HashMap<BlockHeight, Hashed<Block>> = HashMap::new();
544+
545+
for hash in hashes {
546+
if let Some(hashed_block) = self.block_values.get(&hash) {
547+
height_to_blocks.insert(hashed_block.inner().header.height, hashed_block);
548+
} else {
549+
uncached_hashes.push(hash);
546550
}
547-
};
548-
let height_to_certificates = heights
549-
.into_iter()
550-
.zip(certificates)
551-
.collect::<HashMap<_, _>>();
552-
// For each medium, select the relevant messages.
551+
}
552+
553+
if !uncached_hashes.is_empty() {
554+
let certificates = self
555+
.storage
556+
.read_certificates(uncached_hashes.clone())
557+
.await?;
558+
let certificates = match ResultReadCertificates::new(certificates, uncached_hashes) {
559+
ResultReadCertificates::Certificates(certificates) => certificates,
560+
ResultReadCertificates::InvalidHashes(hashes) => {
561+
return Err(WorkerError::ReadCertificatesError(hashes))
562+
}
563+
};
564+
565+
for cert in certificates {
566+
let hashed_block = cert.into_value().into_inner();
567+
let height = hashed_block.inner().header.height;
568+
self.block_values.insert(Cow::Owned(hashed_block.clone()));
569+
height_to_blocks.insert(height, hashed_block);
570+
}
571+
}
572+
553573
let mut cross_chain_requests = Vec::new();
554574
for (recipient, heights) in heights_by_recipient {
555575
let mut bundles = Vec::new();
556576
for height in heights {
557-
let cert = height_to_certificates
577+
let hashed_block = height_to_blocks
558578
.get(&height)
559-
.ok_or_else(|| ChainError::InternalError("missing certificates".to_string()))?;
560-
bundles.extend(cert.message_bundles_for(recipient));
579+
.ok_or_else(|| ChainError::InternalError("missing block".to_string()))?;
580+
bundles.extend(
581+
hashed_block
582+
.inner()
583+
.message_bundles_for(recipient, hashed_block.hash()),
584+
);
561585
}
562586
let request = CrossChainRequest::UpdateRecipient {
563587
sender: self.chain.chain_id(),

0 commit comments

Comments
 (0)