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
// Copyright (c) The Diem Core Contributors
// SPDX-License-Identifier: Apache-2.0

use crate::{
    chunk_request::GetChunkRequest, chunk_response::GetChunkResponse, error::Error,
    request_manager::ChunkRequestInfo,
};
use diem_config::{config::PeerNetworkId, network_id::NetworkId};
use diem_logger::Schema;
use diem_types::{
    contract_event::ContractEvent, ledger_info::LedgerInfoWithSignatures, waypoint::Waypoint,
};
use serde::Serialize;

#[derive(Clone, Schema)]
pub struct LogSchema<'a> {
    name: LogEntry,
    event: Option<LogEvent>,
    #[schema(debug)]
    error: Option<&'a Error>,
    #[schema(display)]
    peer: Option<&'a PeerNetworkId>,
    is_valid_peer: Option<bool>,
    #[schema(display)]
    chunk_request: Option<GetChunkRequest>,
    version: Option<u64>,
    #[schema(display)]
    chunk_response: Option<GetChunkResponse>,
    #[schema(display)]
    waypoint: Option<Waypoint>,
    subscription_name: Option<String>,
    count: Option<usize>,
    reconfig_events: Option<Vec<ContractEvent>>,
    local_li_version: Option<u64>,
    local_synced_version: Option<u64>,
    local_epoch: Option<u64>,
    #[schema(display)]
    ledger_info: Option<LedgerInfoWithSignatures>,
    old_epoch: Option<u64>,
    new_epoch: Option<u64>,
    request_version: Option<u64>,
    target_version: Option<u64>,
    old_multicast_level: Option<NetworkId>,
    new_multicast_level: Option<NetworkId>,
    #[schema(debug)]
    chunk_req_info: Option<&'a ChunkRequestInfo>,
}

impl<'a> LogSchema<'a> {
    pub fn new(name: LogEntry) -> Self {
        Self::new_event(name, None)
    }

    pub fn event_log(name: LogEntry, event: LogEvent) -> Self {
        Self::new_event(name, Some(event))
    }

    fn new_event(name: LogEntry, event: Option<LogEvent>) -> Self {
        Self {
            name,
            event,
            peer: None,
            is_valid_peer: None,
            error: None,
            chunk_request: None,
            chunk_response: None,
            version: None,
            waypoint: None,
            subscription_name: None,
            reconfig_events: None,
            count: None,
            local_li_version: None,
            local_synced_version: None,
            local_epoch: None,
            ledger_info: None,
            new_epoch: None,
            old_epoch: None,
            request_version: None,
            target_version: None,
            old_multicast_level: None,
            new_multicast_level: None,
            chunk_req_info: None,
        }
    }
}

#[derive(Clone, Copy, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum LogEntry {
    Reconfig,
    NewPeer,
    NewPeerAlreadyExists,
    LostPeer,
    LostPeerNotKnown,
    Waypoint,
    RuntimeStart,
    ConsensusCommit,
    SyncRequest,
    Timeout,
    LocalState,
    SendChunkRequest,
    ProcessChunkRequest,
    ProcessChunkResponse,
    ProcessChunkMessage,
    NetworkError,
    EpochChange,
    CommitFlow,
    Multicast,
    SubscriptionDeliveryFail,
    ProgressCheck,
}

#[derive(Clone, Copy, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum LogEvent {
    // Generic events
    CallbackFail,
    Complete,
    Fail,
    Initialize,
    Timeout,
    PublishError,
    Received,

    // SendChunkRequest events
    MissingPeers,
    NetworkSendError,
    Success,
    ChunkRequestInfo,

    // ProcessChunkResponse events
    ReceivedChunkWithoutRequest,
    SendChunkRequestFail,
    ApplyChunkSuccess,
    ApplyChunkFail,
    PostCommitFail,
    OldResponseLI,

    // ProcessChunkRequest events
    PastEpochRequested,
    DeliverChunk,

    // Multicast network events
    Failover,
    Recover,
}