casper-node 2.0.3

The Casper blockchain node
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
#[cfg(test)]
mod tests;

use std::{
    collections::HashMap,
    fmt::{self, Display, Formatter},
};

use datasize::DataSize;
use serde::{Deserialize, Serialize};
use tracing::{debug, error};

use casper_types::{
    bytesrepr, execution::ExecutionResult, BlockHash, ChunkWithProof, Digest, TransactionHash,
};

use super::block_acquisition::Acceptance;
use crate::types::{BlockExecutionResultsOrChunk, BlockExecutionResultsOrChunkId, ValueOrChunk};

#[derive(Clone, Copy, PartialEq, Eq, DataSize, Debug, Serialize, Deserialize)]
pub(crate) enum ExecutionResultsChecksum {
    // due to historical reasons, pre-1.5 chunks do not support Merkle proof checking
    Uncheckable,
    // can be Merkle proof checked
    Checkable(Digest),
}

impl Display for ExecutionResultsChecksum {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        match self {
            Self::Uncheckable => write!(f, "uncheckable execution results"),
            Self::Checkable(digest) => write!(f, "execution results checksum {}", digest),
        }
    }
}

impl ExecutionResultsChecksum {
    pub(super) fn is_checkable(&self) -> bool {
        matches!(self, ExecutionResultsChecksum::Checkable(_))
    }
}

#[derive(Clone, Copy, PartialEq, Eq, DataSize, Debug)]
pub(crate) enum Error {
    BlockHashMismatch {
        expected: BlockHash,
        actual: BlockHash,
    },
    ChunkCountMismatch {
        block_hash: BlockHash,
        expected: u64,
        actual: u64,
    },
    InvalidChunkCount {
        block_hash: BlockHash,
    },
    InvalidAttemptToApplyChecksum {
        block_hash: BlockHash,
    },
    AttemptToApplyDataAfterCompleted {
        block_hash: BlockHash,
    },
    AttemptToApplyDataWhenMissingChecksum {
        block_hash: BlockHash,
    },
    ChecksumMismatch {
        block_hash: BlockHash,
        expected: Digest,
        actual: Digest,
    },
    ChunksWithDifferentChecksum {
        block_hash: BlockHash,
        expected: Digest,
        actual: Digest,
    },
    FailedToDeserialize {
        block_hash: BlockHash,
    },
    ExecutionResultToDeployHashLengthDiscrepancy {
        block_hash: BlockHash,
        expected: usize,
        actual: usize,
    },
    InvalidOutcomeFromApplyingChunk {
        block_hash: BlockHash,
    },
}

impl Display for Error {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        match self {
            Error::BlockHashMismatch { expected, actual } => {
                write!(
                    f,
                    "block hash mismatch: expected {} actual: {}",
                    expected, actual
                )
            }
            Error::ExecutionResultToDeployHashLengthDiscrepancy {
                block_hash,
                expected,
                actual,
            } => {
                write!(
                    f,
                    "discrepancy between the number of deploys and corresponding execution results for block_hash: {}; expected {} actual: {}",
                    block_hash, expected, actual
                )
            }
            Error::ChunkCountMismatch {
                block_hash,
                expected,
                actual,
            } => {
                write!(
                    f,
                    "chunk count mismatch for block_hash: {}; expected {} actual: {}",
                    block_hash, expected, actual
                )
            }
            Error::InvalidChunkCount { block_hash } => {
                write!(
                    f,
                    "invalid chunk count for block_hash: {}; execution results should either be a complete single value or come in 2 or more chunks",
                    block_hash
                )
            }
            Error::InvalidAttemptToApplyChecksum { block_hash } => {
                write!(
                    f,
                    "attempt to apply checksum to a non-pending item, block_hash: {}",
                    block_hash
                )
            }
            Error::AttemptToApplyDataAfterCompleted { block_hash } => {
                write!(
                    f,
                    "attempt to apply execution results for already completed block_hash: {}",
                    block_hash
                )
            }
            Error::AttemptToApplyDataWhenMissingChecksum { block_hash } => {
                write!(
                    f,
                    "attempt to apply execution results before check sum for block_hash: {}",
                    block_hash
                )
            }
            Error::ChecksumMismatch {
                block_hash,
                expected,
                actual,
            } => {
                write!(
                    f,
                    "root hash mismatch for block_hash: {}; expected {} actual: {}",
                    block_hash, expected, actual
                )
            }
            Error::FailedToDeserialize { block_hash } => {
                write!(
                    f,
                    "failed to deserialize execution effects for block_hash: {}",
                    block_hash,
                )
            }
            Error::ChunksWithDifferentChecksum {
                block_hash,
                expected,
                actual,
            } => write!(
                f,
                "chunks with different checksum for block_hash: {}; expected {} actual: {}",
                block_hash, expected, actual
            ),
            Error::InvalidOutcomeFromApplyingChunk { block_hash } => write!(
                f,
                "cannot have already had chunk if in pending mode for block hash: {}",
                block_hash
            ),
        }
    }
}

#[derive(Clone, PartialEq, Eq, DataSize, Debug)]
pub(super) enum ExecutionResultsAcquisition {
    Needed {
        block_hash: BlockHash,
    },
    Pending {
        block_hash: BlockHash,
        checksum: ExecutionResultsChecksum,
    },
    Acquiring {
        block_hash: BlockHash,
        checksum: ExecutionResultsChecksum,
        chunks: HashMap<u64, ChunkWithProof>,
        chunk_count: u64,
        next: u64,
    },
    Complete {
        block_hash: BlockHash,
        checksum: ExecutionResultsChecksum,
        results: HashMap<TransactionHash, ExecutionResult>,
    },
}

impl Display for ExecutionResultsAcquisition {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        match self {
            ExecutionResultsAcquisition::Needed { block_hash } => {
                write!(f, "Needed: {}", block_hash)
            }
            ExecutionResultsAcquisition::Pending {
                block_hash,
                checksum: _,
            } => write!(f, "Pending: {}", block_hash),
            ExecutionResultsAcquisition::Acquiring {
                block_hash,
                checksum: _,
                chunks: _,
                chunk_count,
                next,
            } => write!(
                f,
                "Acquiring: {}, chunk_count={}, next={}",
                block_hash, chunk_count, next
            ),
            ExecutionResultsAcquisition::Complete {
                block_hash,
                checksum: _,
                results: _,
            } => write!(f, "Complete: {}", block_hash),
        }
    }
}

impl ExecutionResultsAcquisition {
    pub(super) fn needs_value_or_chunk(
        &self,
    ) -> Option<(BlockExecutionResultsOrChunkId, ExecutionResultsChecksum)> {
        match self {
            ExecutionResultsAcquisition::Needed { .. }
            | ExecutionResultsAcquisition::Complete { .. } => None,
            ExecutionResultsAcquisition::Pending {
                block_hash,
                checksum,
            } => Some((BlockExecutionResultsOrChunkId::new(*block_hash), *checksum)),
            ExecutionResultsAcquisition::Acquiring {
                block_hash,
                checksum,
                next,
                ..
            } => Some((
                BlockExecutionResultsOrChunkId::new(*block_hash).next_chunk(*next),
                *checksum,
            )),
        }
    }

    pub(super) fn apply_checksum(self, checksum: ExecutionResultsChecksum) -> Result<Self, Error> {
        match self {
            ExecutionResultsAcquisition::Needed { block_hash } => {
                debug!("apply_checksum - Needed");
                Ok(ExecutionResultsAcquisition::Pending {
                    block_hash,
                    checksum,
                })
            }
            ExecutionResultsAcquisition::Pending { block_hash, .. }
            | ExecutionResultsAcquisition::Acquiring { block_hash, .. }
            | ExecutionResultsAcquisition::Complete { block_hash, .. } => {
                debug!("apply_checksum - Pending | Acquiring | Complete");
                Err(Error::InvalidAttemptToApplyChecksum { block_hash })
            }
        }
    }

    pub(super) fn apply_block_execution_results_or_chunk(
        self,
        block_execution_results_or_chunk: BlockExecutionResultsOrChunk,
        transaction_hashes: Vec<TransactionHash>,
    ) -> Result<(Self, Acceptance), Error> {
        let block_hash = *block_execution_results_or_chunk.block_hash();
        let value = block_execution_results_or_chunk.into_value();

        debug!(%block_hash, state=%self, "apply_block_execution_results_or_chunk");

        let expected_block_hash = self.block_hash();
        if expected_block_hash != block_hash {
            debug!(
                %block_hash,
                "apply_block_execution_results_or_chunk: Error::BlockHashMismatch"
            );
            return Err(Error::BlockHashMismatch {
                expected: expected_block_hash,
                actual: block_hash,
            });
        }

        let (checksum, execution_results) = match (self, value) {
            (
                ExecutionResultsAcquisition::Pending { checksum, .. },
                ValueOrChunk::Value(execution_results),
            )
            | (
                ExecutionResultsAcquisition::Acquiring { checksum, .. },
                ValueOrChunk::Value(execution_results),
            ) => {
                debug!(
                    "apply_block_execution_results_or_chunk: (Pending, Value) | (Acquiring, Value)"
                );
                (checksum, execution_results)
            }
            (
                ExecutionResultsAcquisition::Pending { checksum, .. },
                ValueOrChunk::ChunkWithProof(chunk),
            ) => {
                debug!("apply_block_execution_results_or_chunk: (Pending, ChunkWithProof)");
                match apply_chunk(block_hash, checksum, HashMap::new(), chunk, None) {
                    Ok(ApplyChunkOutcome::HadIt { .. }) => {
                        error!("cannot have already had chunk if in pending mode");
                        return Err(Error::InvalidOutcomeFromApplyingChunk { block_hash });
                    }
                    Ok(ApplyChunkOutcome::NeedNext {
                        chunks,
                        chunk_count,
                        next,
                    }) => {
                        let acquisition = ExecutionResultsAcquisition::Acquiring {
                            block_hash,
                            checksum,
                            chunks,
                            chunk_count,
                            next,
                        };
                        let acceptance = Acceptance::NeededIt;
                        return Ok((acquisition, acceptance));
                    }
                    Ok(ApplyChunkOutcome::Complete { execution_results }) => {
                        (checksum, execution_results)
                    }
                    Err(err) => {
                        return Err(err);
                    }
                }
            }
            (
                ExecutionResultsAcquisition::Acquiring {
                    checksum,
                    chunks,
                    chunk_count,
                    next,
                    ..
                },
                ValueOrChunk::ChunkWithProof(chunk),
            ) => {
                debug!("apply_block_execution_results_or_chunk: (Acquiring, ChunkWithProof)");
                match apply_chunk(block_hash, checksum, chunks, chunk, Some(chunk_count)) {
                    Ok(ApplyChunkOutcome::HadIt { chunks }) => {
                        let acquisition = ExecutionResultsAcquisition::Acquiring {
                            block_hash,
                            checksum,
                            chunks,
                            chunk_count,
                            next,
                        };
                        let acceptance = Acceptance::HadIt;
                        return Ok((acquisition, acceptance));
                    }
                    Ok(ApplyChunkOutcome::NeedNext {
                        chunks,
                        chunk_count,
                        next,
                    }) => {
                        let acquisition = ExecutionResultsAcquisition::Acquiring {
                            block_hash,
                            checksum,
                            chunks,
                            chunk_count,
                            next,
                        };
                        let acceptance = Acceptance::NeededIt;
                        return Ok((acquisition, acceptance));
                    }
                    Ok(ApplyChunkOutcome::Complete { execution_results }) => {
                        (checksum, execution_results)
                    }
                    Err(err) => {
                        return Err(err);
                    }
                }
            }
            (ExecutionResultsAcquisition::Needed { block_hash }, _) => {
                debug!("apply_block_execution_results_or_chunk: (Needed, _)");
                return Err(Error::AttemptToApplyDataWhenMissingChecksum { block_hash });
            }
            (ExecutionResultsAcquisition::Complete { .. }, _) => {
                debug!("apply_block_execution_results_or_chunk: (Complete, _)");
                return Err(Error::AttemptToApplyDataAfterCompleted { block_hash });
            }
        };

        if transaction_hashes.len() != execution_results.len() {
            debug!(
                %block_hash,
                "apply_block_execution_results_or_chunk: Error::ExecutionResultToDeployHashLengthDiscrepancy"
            );
            return Err(Error::ExecutionResultToDeployHashLengthDiscrepancy {
                block_hash,
                expected: transaction_hashes.len(),
                actual: execution_results.len(),
            });
        }
        let results = transaction_hashes
            .into_iter()
            .zip(execution_results)
            .collect();
        debug!(
            %block_hash,
            "apply_block_execution_results_or_chunk: returning ExecutionResultsAcquisition::Complete"
        );
        let acceptance = Acceptance::NeededIt;
        let acquisition = ExecutionResultsAcquisition::Complete {
            block_hash,
            results,
            checksum,
        };
        Ok((acquisition, acceptance))
    }

    pub(super) fn is_checkable(&self) -> bool {
        match self {
            ExecutionResultsAcquisition::Needed { .. } => false,
            ExecutionResultsAcquisition::Pending { checksum, .. }
            | ExecutionResultsAcquisition::Acquiring { checksum, .. }
            | ExecutionResultsAcquisition::Complete { checksum, .. } => checksum.is_checkable(),
        }
    }

    fn block_hash(&self) -> BlockHash {
        match self {
            ExecutionResultsAcquisition::Needed { block_hash }
            | ExecutionResultsAcquisition::Pending { block_hash, .. }
            | ExecutionResultsAcquisition::Acquiring { block_hash, .. }
            | ExecutionResultsAcquisition::Complete { block_hash, .. } => *block_hash,
        }
    }
}

#[derive(Debug)]
enum ApplyChunkOutcome {
    HadIt {
        chunks: HashMap<u64, ChunkWithProof>,
    },
    NeedNext {
        chunks: HashMap<u64, ChunkWithProof>,
        chunk_count: u64,
        next: u64,
    },
    Complete {
        execution_results: Vec<ExecutionResult>,
    },
}

impl ApplyChunkOutcome {
    fn need_next(chunks: HashMap<u64, ChunkWithProof>, chunk_count: u64, next: u64) -> Self {
        ApplyChunkOutcome::NeedNext {
            chunks,
            chunk_count,
            next,
        }
    }

    fn execution_results(execution_results: Vec<ExecutionResult>) -> Self {
        ApplyChunkOutcome::Complete { execution_results }
    }
}

fn apply_chunk(
    block_hash: BlockHash,
    checksum: ExecutionResultsChecksum,
    mut chunks: HashMap<u64, ChunkWithProof>,
    chunk: ChunkWithProof,
    expected_count: Option<u64>,
) -> Result<ApplyChunkOutcome, Error> {
    let digest = chunk.proof().root_hash();
    let index = chunk.proof().index();
    let chunk_count = chunk.proof().count();
    if chunk_count == 1 {
        debug!(%block_hash, "apply_chunk: Error::InvalidChunkCount");
        return Err(Error::InvalidChunkCount { block_hash });
    }

    if let Some(expected) = expected_count {
        if expected != chunk_count {
            debug!(%block_hash, "apply_chunk: Error::ChunkCountMismatch");
            return Err(Error::ChunkCountMismatch {
                block_hash,
                expected,
                actual: chunk_count,
            });
        }
    }

    // ExecutionResultsChecksum::Uncheckable has no checksum, otherwise check it
    if let ExecutionResultsChecksum::Checkable(expected) = checksum {
        if expected != digest {
            debug!(%block_hash, "apply_chunk: Error::ChecksumMismatch");
            return Err(Error::ChecksumMismatch {
                block_hash,
                expected,
                actual: digest,
            });
        }
    } else if let Some(other_chunk) = chunks.values().next() {
        let existing_chunk_digest = other_chunk.proof().root_hash();
        if existing_chunk_digest != digest {
            debug!(%block_hash, "apply_chunk: Error::ChunksWithDifferentChecksum");
            return Err(Error::ChunksWithDifferentChecksum {
                block_hash,
                expected: existing_chunk_digest,
                actual: digest,
            });
        }
    }

    if chunks.insert(index, chunk).is_some() {
        debug!(%block_hash, index, "apply_chunk: already had it");
        return Ok(ApplyChunkOutcome::HadIt { chunks });
    };

    match (0..chunk_count).find(|idx| !chunks.contains_key(idx)) {
        Some(next) => Ok(ApplyChunkOutcome::need_next(chunks, chunk_count, next)),
        None => {
            let serialized: Vec<u8> = (0..chunk_count)
                .filter_map(|index| chunks.get(&index))
                .flat_map(|c| c.chunk())
                .copied()
                .collect();
            match bytesrepr::deserialize(serialized) {
                Ok(results) => {
                    debug!(%block_hash, "apply_chunk: ApplyChunkOutcome::execution_results");
                    Ok(ApplyChunkOutcome::execution_results(results))
                }
                Err(error) => {
                    error!(%error, "failed to deserialize execution results");
                    Err(Error::FailedToDeserialize { block_hash })
                }
            }
        }
    }
}