linera-core 0.15.17

The core Linera protocol, including client and server logic, node synchronization, etc.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
// Copyright (c) Zefchain Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

use custom_debug_derive::Debug;
use linera_base::{
    data_types::{Blob, BlobContent, BlockHeight},
    identifiers::{BlobId, ChainId},
};
use linera_chain::types::ConfirmedBlockCertificate;

use crate::{
    client::requests_scheduler::cache::SubsumingKey, data_types::debug_compressed_heights,
};

/// Unique identifier for different types of download requests.
///
/// Used for request deduplication to avoid redundant downloads of the same data.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum RequestKey {
    /// Download certificates by specific heights
    Certificates {
        chain_id: ChainId,
        #[debug(with = "debug_compressed_heights")]
        heights: Vec<BlockHeight>,
    },
    /// Download a blob by ID
    Blob(BlobId),
    /// Download a pending blob
    PendingBlob { chain_id: ChainId, blob_id: BlobId },
    /// Download certificate for a specific blob
    CertificateForBlob(BlobId),
}

impl RequestKey {
    /// Returns the chain ID associated with the request, if applicable.
    pub(super) fn chain_id(&self) -> Option<ChainId> {
        match self {
            RequestKey::Certificates { chain_id, .. } => Some(*chain_id),
            RequestKey::PendingBlob { chain_id, .. } => Some(*chain_id),
            _ => None,
        }
    }

    /// Converts certificate-related requests to a common representation of (chain_id, sorted heights).
    ///
    /// This helper method normalizes both `Certificates` and `CertificatesByHeights` variants
    /// into a uniform format for easier comparison and overlap detection.
    ///
    /// # Returns
    /// - `Some((chain_id, heights))` for certificate requests, where heights are sorted
    /// - `None` for non-certificate requests (Blob, PendingBlob, CertificateForBlob)
    fn heights(&self) -> Option<Vec<BlockHeight>> {
        match self {
            RequestKey::Certificates { heights, .. } => Some(heights.clone()),
            _ => None,
        }
    }
}

/// Result types that can be shared across deduplicated requests
#[derive(Debug, Clone)]
pub enum RequestResult {
    Certificates(Vec<ConfirmedBlockCertificate>),
    Blob(Option<Blob>),
    BlobContent(BlobContent),
    Certificate(Box<ConfirmedBlockCertificate>),
}

/// Marker trait for types that can be converted to/from `RequestResult`
/// for use in the requests cache.
pub trait Cacheable: TryFrom<RequestResult> + Into<RequestResult> {}
impl<T> Cacheable for T where T: TryFrom<RequestResult> + Into<RequestResult> {}

impl From<Option<Blob>> for RequestResult {
    fn from(blob: Option<Blob>) -> Self {
        RequestResult::Blob(blob)
    }
}

impl From<Vec<ConfirmedBlockCertificate>> for RequestResult {
    fn from(certs: Vec<ConfirmedBlockCertificate>) -> Self {
        RequestResult::Certificates(certs)
    }
}

impl From<BlobContent> for RequestResult {
    fn from(content: BlobContent) -> Self {
        RequestResult::BlobContent(content)
    }
}

impl From<ConfirmedBlockCertificate> for RequestResult {
    fn from(cert: ConfirmedBlockCertificate) -> Self {
        RequestResult::Certificate(Box::new(cert))
    }
}

impl TryFrom<RequestResult> for Option<Blob> {
    type Error = ();

    fn try_from(result: RequestResult) -> Result<Self, Self::Error> {
        match result {
            RequestResult::Blob(blob) => Ok(blob),
            _ => Err(()),
        }
    }
}

impl TryFrom<RequestResult> for Vec<ConfirmedBlockCertificate> {
    type Error = ();

    fn try_from(result: RequestResult) -> Result<Self, Self::Error> {
        match result {
            RequestResult::Certificates(certs) => Ok(certs),
            _ => Err(()),
        }
    }
}

impl TryFrom<RequestResult> for BlobContent {
    type Error = ();

    fn try_from(result: RequestResult) -> Result<Self, Self::Error> {
        match result {
            RequestResult::BlobContent(content) => Ok(content),
            _ => Err(()),
        }
    }
}

impl TryFrom<RequestResult> for ConfirmedBlockCertificate {
    type Error = ();

    fn try_from(result: RequestResult) -> Result<Self, Self::Error> {
        match result {
            RequestResult::Certificate(cert) => Ok(*cert),
            _ => Err(()),
        }
    }
}

impl SubsumingKey<RequestResult> for super::request::RequestKey {
    fn subsumes(&self, other: &Self) -> bool {
        // Different chains can't subsume each other
        if self.chain_id() != other.chain_id() {
            return false;
        }

        let (in_flight_req_heights, new_req_heights) = match (self.heights(), other.heights()) {
            (Some(range1), Some(range2)) => (range1, range2),
            _ => return false, // We subsume only certificate requests
        };

        let mut in_flight_req_heights_iter = in_flight_req_heights.into_iter();

        for new_height in new_req_heights {
            if !in_flight_req_heights_iter.any(|h| h == new_height) {
                return false; // Found a height not covered by in-flight request
            }
        }
        true
    }

    fn try_extract_result(
        &self,
        in_flight_request: &RequestKey,
        result: &RequestResult,
    ) -> Option<RequestResult> {
        // Only certificate results can be extracted
        let certificates = match result {
            RequestResult::Certificates(certs) => certs,
            _ => return None,
        };

        if !in_flight_request.subsumes(self) {
            return None; // Can't extract if not subsumed
        }

        let mut requested_heights = self.heights()?;
        if requested_heights.is_empty() {
            return Some(RequestResult::Certificates(vec![])); // Nothing requested
        }
        let mut certificates_iter = certificates.iter();
        let mut collected = vec![];
        while let Some(height) = requested_heights.first() {
            // Remove certs below the requested height.
            if let Some(cert) = certificates_iter.find(|cert| &cert.value().height() == height) {
                collected.push(cert.clone());
                requested_heights.remove(0);
            } else {
                return None; // Missing a requested height
            }
        }

        Some(RequestResult::Certificates(collected))
    }
}

#[cfg(test)]
mod tests {
    use linera_base::{crypto::CryptoHash, data_types::BlockHeight, identifiers::ChainId};

    use super::{RequestKey, SubsumingKey};

    #[test]
    fn test_subsumes_complete_containment() {
        let chain_id = ChainId(CryptoHash::test_hash("chain1"));
        let large = RequestKey::Certificates {
            chain_id,
            heights: vec![BlockHeight(11), BlockHeight(12), BlockHeight(13)],
        };
        let small = RequestKey::Certificates {
            chain_id,
            heights: vec![BlockHeight(12)],
        };
        assert!(large.subsumes(&small));
        assert!(!small.subsumes(&large));
    }

    #[test]
    fn test_subsumes_partial_containment() {
        let chain_id = ChainId(CryptoHash::test_hash("chain1"));
        let req1 = RequestKey::Certificates {
            chain_id,
            heights: vec![BlockHeight(12), BlockHeight(13)],
        };
        let req2 = RequestKey::Certificates {
            chain_id,
            heights: vec![BlockHeight(12), BlockHeight(14)],
        };
        assert!(!req1.subsumes(&req2));
        assert!(!req2.subsumes(&req1));
    }

    #[test]
    fn test_subsumes_different_chains() {
        let chain1 = ChainId(CryptoHash::test_hash("chain1"));
        let chain2 = ChainId(CryptoHash::test_hash("chain2"));
        let req1 = RequestKey::Certificates {
            chain_id: chain1,
            heights: vec![BlockHeight(12)],
        };
        let req2 = RequestKey::Certificates {
            chain_id: chain2,
            heights: vec![BlockHeight(12)],
        };
        assert!(!req1.subsumes(&req2));
    }

    // Helper function to create a test certificate at a specific height
    fn make_test_cert(
        height: u64,
        chain_id: ChainId,
    ) -> linera_chain::types::ConfirmedBlockCertificate {
        use linera_base::{
            crypto::ValidatorKeypair,
            data_types::{Round, Timestamp},
        };
        use linera_chain::{
            block::ConfirmedBlock,
            data_types::{BlockExecutionOutcome, LiteValue, LiteVote},
            test::{make_first_block, BlockTestExt, VoteTestExt},
        };

        let keypair = ValidatorKeypair::generate();
        let mut proposed_block = make_first_block(chain_id).with_timestamp(Timestamp::from(height));

        // Set the correct height
        proposed_block.height = BlockHeight(height);

        // Create a Block from the proposed block with default execution outcome
        let block = BlockExecutionOutcome::default().with(proposed_block);

        // Create a ConfirmedBlock
        let confirmed_block = ConfirmedBlock::new(block);

        // Create a LiteVote and convert to Vote
        let lite_vote = LiteVote::new(
            LiteValue::new(&confirmed_block),
            Round::MultiLeader(0),
            &keypair.secret_key,
        );

        // Convert to full vote
        let vote = lite_vote.with_value(confirmed_block).unwrap();

        // Convert vote to certificate
        vote.into_certificate(keypair.secret_key.public())
    }

    #[test]
    fn test_try_extract_result_non_certificate_result() {
        use super::RequestResult;

        let chain_id = ChainId(CryptoHash::test_hash("chain1"));
        let req1 = RequestKey::Certificates {
            chain_id,
            heights: vec![BlockHeight(12)],
        };
        let req2 = RequestKey::Certificates {
            chain_id,
            heights: vec![BlockHeight(12)],
        };

        // Non-certificate result should return None
        let blob_result = RequestResult::Blob(None);
        assert!(req1.try_extract_result(&req2, &blob_result).is_none());
    }

    #[test]
    fn test_try_extract_result_empty_request_range() {
        use super::RequestResult;

        let chain_id = ChainId(CryptoHash::test_hash("chain1"));
        let req1 = RequestKey::Certificates {
            chain_id,
            heights: vec![],
        };
        let req2 = RequestKey::Certificates {
            chain_id,
            heights: vec![BlockHeight(10)],
        };

        let certs = vec![make_test_cert(10, chain_id)];
        let result = RequestResult::Certificates(certs);

        // Empty request is always extractable, should return empty result
        match req1.try_extract_result(&req2, &result) {
            Some(RequestResult::Certificates(extracted_certs)) => {
                assert!(extracted_certs.is_empty());
            }
            _ => panic!("Expected Some empty Certificates result"),
        }
    }

    #[test]
    fn test_try_extract_result_empty_result_range() {
        use super::RequestResult;

        let chain_id = ChainId(CryptoHash::test_hash("chain1"));
        let req1 = RequestKey::Certificates {
            chain_id,
            heights: vec![BlockHeight(12)],
        };
        let req2 = RequestKey::Certificates {
            chain_id,
            heights: vec![BlockHeight(12)],
        };

        let result = RequestResult::Certificates(vec![]); // Empty result

        // Empty result should return None
        assert!(req1.try_extract_result(&req2, &result).is_none());
    }

    #[test]
    fn test_try_extract_result_non_overlapping_ranges() {
        use super::RequestResult;

        let chain_id = ChainId(CryptoHash::test_hash("chain1"));
        let new_req = RequestKey::Certificates {
            chain_id,
            heights: vec![BlockHeight(10)],
        };
        let in_flight_req = RequestKey::Certificates {
            chain_id,
            heights: vec![BlockHeight(11)],
        };

        // Result does not contain all requested heights
        let certs = vec![make_test_cert(11, chain_id)];
        let result = RequestResult::Certificates(certs);

        // No overlap, should return None
        assert!(new_req
            .try_extract_result(&in_flight_req, &result)
            .is_none());
    }

    #[test]
    fn test_try_extract_result_partial_overlap_missing_start() {
        use super::RequestResult;

        let chain_id = ChainId(CryptoHash::test_hash("chain1"));
        let req1 = RequestKey::Certificates {
            chain_id,
            heights: vec![BlockHeight(10), BlockHeight(11), BlockHeight(12)],
        };
        let req2 = RequestKey::Certificates {
            chain_id,
            heights: vec![BlockHeight(11), BlockHeight(12)],
        };

        // Result missing the first height (10)
        let certs = vec![make_test_cert(11, chain_id), make_test_cert(12, chain_id)];
        let result = RequestResult::Certificates(certs);

        // Missing start height, should return None
        assert!(req1.try_extract_result(&req2, &result).is_none());
    }

    #[test]
    fn test_try_extract_result_partial_overlap_missing_end() {
        use super::RequestResult;

        let chain_id = ChainId(CryptoHash::test_hash("chain1"));
        let req1 = RequestKey::Certificates {
            chain_id,
            heights: vec![BlockHeight(10), BlockHeight(11), BlockHeight(12)],
        };
        let req2 = RequestKey::Certificates {
            chain_id,
            heights: vec![BlockHeight(10), BlockHeight(11)],
        };

        // Result missing the last height (14)
        let certs = vec![make_test_cert(10, chain_id), make_test_cert(11, chain_id)];
        let result = RequestResult::Certificates(certs);

        // Missing end height, should return None
        assert!(req1.try_extract_result(&req2, &result).is_none());
    }

    #[test]
    fn test_try_extract_result_partial_overlap_missing_middle() {
        use super::RequestResult;

        let chain_id = ChainId(CryptoHash::test_hash("chain1"));
        let new_req = RequestKey::Certificates {
            chain_id,
            heights: vec![BlockHeight(10), BlockHeight(12), BlockHeight(13)],
        };
        let in_flight_req = RequestKey::Certificates {
            chain_id,
            heights: vec![
                BlockHeight(10),
                BlockHeight(12),
                BlockHeight(13),
                BlockHeight(14),
            ],
        };

        let certs = vec![
            make_test_cert(10, chain_id),
            make_test_cert(13, chain_id),
            make_test_cert(14, chain_id),
        ];
        let result = RequestResult::Certificates(certs);

        assert!(new_req
            .try_extract_result(&in_flight_req, &result)
            .is_none());
        assert!(in_flight_req
            .try_extract_result(&new_req, &result)
            .is_none());
    }

    #[test]
    fn test_try_extract_result_exact_match() {
        use super::RequestResult;

        let chain_id = ChainId(CryptoHash::test_hash("chain1"));
        let req1 = RequestKey::Certificates {
            chain_id,
            heights: vec![BlockHeight(10), BlockHeight(11), BlockHeight(12)],
        }; // [10, 11, 12]
        let req2 = RequestKey::Certificates {
            chain_id,
            heights: vec![BlockHeight(10), BlockHeight(11), BlockHeight(12)],
        };

        let certs = vec![
            make_test_cert(10, chain_id),
            make_test_cert(11, chain_id),
            make_test_cert(12, chain_id),
        ];
        let result = RequestResult::Certificates(certs.clone());

        // Exact match should return all certificates
        let extracted = req1.try_extract_result(&req2, &result);
        assert!(extracted.is_some());
        match extracted.unwrap() {
            RequestResult::Certificates(extracted_certs) => {
                assert_eq!(extracted_certs, certs);
            }
            _ => panic!("Expected Certificates result"),
        }
    }

    #[test]
    fn test_try_extract_result_superset_extraction() {
        use super::RequestResult;

        let chain_id = ChainId(CryptoHash::test_hash("chain1"));
        let req1 = RequestKey::Certificates {
            chain_id,
            heights: vec![BlockHeight(12), BlockHeight(13)],
        };
        let req2 = RequestKey::Certificates {
            chain_id,
            heights: vec![BlockHeight(12), BlockHeight(13)],
        };

        // Result has more certificates than requested
        let certs = vec![
            make_test_cert(10, chain_id),
            make_test_cert(11, chain_id),
            make_test_cert(12, chain_id),
            make_test_cert(13, chain_id),
            make_test_cert(14, chain_id),
        ];
        let result = RequestResult::Certificates(certs);

        // Should extract only the requested range [12, 13]
        let extracted = req1.try_extract_result(&req2, &result);
        assert!(extracted.is_some());
        match extracted.unwrap() {
            RequestResult::Certificates(extracted_certs) => {
                assert_eq!(extracted_certs.len(), 2);
                assert_eq!(extracted_certs[0].value().height(), BlockHeight(12));
                assert_eq!(extracted_certs[1].value().height(), BlockHeight(13));
            }
            _ => panic!("Expected Certificates result"),
        }
    }

    #[test]
    fn test_try_extract_result_single_height() {
        use super::RequestResult;

        let chain_id = ChainId(CryptoHash::test_hash("chain1"));
        let req1 = RequestKey::Certificates {
            chain_id,
            heights: vec![BlockHeight(15)],
        }; // [15]
        let req2 = RequestKey::Certificates {
            chain_id,
            heights: vec![BlockHeight(10), BlockHeight(15), BlockHeight(20)],
        };

        let certs = vec![
            make_test_cert(10, chain_id),
            make_test_cert(15, chain_id),
            make_test_cert(20, chain_id),
        ];
        let result = RequestResult::Certificates(certs);

        // Should extract only height 15
        let extracted = req1.try_extract_result(&req2, &result);
        assert!(extracted.is_some());
        match extracted.unwrap() {
            RequestResult::Certificates(extracted_certs) => {
                assert_eq!(extracted_certs.len(), 1);
                assert_eq!(extracted_certs[0].value().height(), BlockHeight(15));
            }
            _ => panic!("Expected Certificates result"),
        }
    }

    #[test]
    fn test_try_extract_result_different_chains() {
        use super::RequestResult;

        let chain1 = ChainId(CryptoHash::test_hash("chain1"));
        let chain2 = ChainId(CryptoHash::test_hash("chain2"));
        let req1 = RequestKey::Certificates {
            chain_id: chain1,
            heights: vec![BlockHeight(12)],
        };
        let req2 = RequestKey::Certificates {
            chain_id: chain2,
            heights: vec![BlockHeight(12)],
        };

        let certs = vec![make_test_cert(12, chain1)];
        let result = RequestResult::Certificates(certs);

        // Different chains should return None
        assert!(req1.try_extract_result(&req2, &result).is_none());
    }
}