Skip to content

Commit a86ab19

Browse files
committed
Check block cache on create_cross_chain_requests
1 parent 570fcbd commit a86ab19

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
@@ -519,26 +519,50 @@ where
519519
})?;
520520
hashes.push(hash);
521521
}
522-
let certificates = self.storage.read_certificates(hashes.clone()).await?;
523-
let certificates = match ResultReadCertificates::new(certificates, hashes) {
524-
ResultReadCertificates::Certificates(certificates) => certificates,
525-
ResultReadCertificates::InvalidHashes(hashes) => {
526-
return Err(WorkerError::ReadCertificatesError(hashes))
522+
523+
let mut uncached_hashes = Vec::new();
524+
let mut height_to_blocks: HashMap<BlockHeight, Hashed<Block>> = HashMap::new();
525+
526+
for hash in hashes {
527+
if let Some(hashed_block) = self.block_values.get(&hash) {
528+
height_to_blocks.insert(hashed_block.inner().header.height, hashed_block);
529+
} else {
530+
uncached_hashes.push(hash);
527531
}
528-
};
529-
let height_to_certificates = heights
530-
.into_iter()
531-
.zip(certificates)
532-
.collect::<HashMap<_, _>>();
533-
// For each medium, select the relevant messages.
532+
}
533+
534+
if !uncached_hashes.is_empty() {
535+
let certificates = self
536+
.storage
537+
.read_certificates(uncached_hashes.clone())
538+
.await?;
539+
let certificates = match ResultReadCertificates::new(certificates, uncached_hashes) {
540+
ResultReadCertificates::Certificates(certificates) => certificates,
541+
ResultReadCertificates::InvalidHashes(hashes) => {
542+
return Err(WorkerError::ReadCertificatesError(hashes))
543+
}
544+
};
545+
546+
for cert in certificates {
547+
let hashed_block = cert.into_value().into_inner();
548+
let height = hashed_block.inner().header.height;
549+
self.block_values.insert(Cow::Owned(hashed_block.clone()));
550+
height_to_blocks.insert(height, hashed_block);
551+
}
552+
}
553+
534554
let mut cross_chain_requests = Vec::new();
535555
for (recipient, heights) in heights_by_recipient {
536556
let mut bundles = Vec::new();
537557
for height in heights {
538-
let cert = height_to_certificates
558+
let hashed_block = height_to_blocks
539559
.get(&height)
540-
.ok_or_else(|| ChainError::InternalError("missing certificates".to_string()))?;
541-
bundles.extend(cert.message_bundles_for(recipient));
560+
.ok_or_else(|| ChainError::InternalError("missing block".to_string()))?;
561+
bundles.extend(
562+
hashed_block
563+
.inner()
564+
.message_bundles_for(recipient, hashed_block.hash()),
565+
);
542566
}
543567
let request = CrossChainRequest::UpdateRecipient {
544568
sender: self.chain.chain_id(),

0 commit comments

Comments
 (0)