node/ledger/
ledger_event.rs

1use serde::{Deserialize, Serialize};
2
3use super::{
4    read::{LedgerReadId, LedgerReadResponse},
5    write::LedgerWriteResponse,
6};
7
8#[derive(Serialize, Deserialize, Debug, Clone)]
9pub enum LedgerEvent {
10    Write(LedgerWriteResponse),
11    Read(LedgerReadId, LedgerReadResponse),
12}
13
14impl std::fmt::Display for LedgerEvent {
15    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
16        write!(f, "Ledger, ")?;
17
18        match self {
19            Self::Write(resp) => {
20                write!(f, "Write, {:?}", resp.kind())?;
21                match resp {
22                    LedgerWriteResponse::StagedLedgerReconstruct {
23                        staged_ledger_hash,
24                        result,
25                    } => {
26                        write!(f, ", {staged_ledger_hash}, {}", res_kind_str(result))
27                    }
28                    LedgerWriteResponse::StagedLedgerDiffCreate {
29                        pred_block_hash,
30                        global_slot_since_genesis,
31                        result,
32                    } => {
33                        write!(
34                            f,
35                            ", {pred_block_hash}, {}, {}",
36                            global_slot_since_genesis.as_u32(),
37                            res_kind_str(result)
38                        )
39                    }
40                    LedgerWriteResponse::BlockApply { block_hash, result } => {
41                        write!(f, ", {block_hash}, {}", res_kind_str(result))
42                    }
43                    LedgerWriteResponse::Commit { best_tip_hash, .. } => {
44                        write!(f, ", {best_tip_hash}")
45                    }
46                }
47            }
48            Self::Read(id, resp) => {
49                write!(f, "Read, {:?}, {id}", resp.kind())
50            }
51        }
52    }
53}
54
55fn res_kind_str<T, E>(res: &Result<T, E>) -> &'static str {
56    match res {
57        Err(_) => "Err",
58        Ok(_) => "Ok",
59    }
60}