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
use sha2::{Digest as _, Sha256};
use tendermint::v0_34::abci::{
    request::{
        ApplySnapshotChunk,
        BeginBlock,
        CheckTx,
        DeliverTx,
        EndBlock,
        InitChain,
        LoadSnapshotChunk,
        OfferSnapshot,
        Query,
    },
    ConsensusRequest,
    InfoRequest,
    MempoolRequest,
    SnapshotRequest,
};
use tracing::{error_span, Span};

pub trait RequestExt {
    fn create_span(&self) -> Span;
}

impl RequestExt for ConsensusRequest {
    fn create_span(&self) -> Span {
        let p = error_span!("abci");

        match self {
            Self::InitChain(InitChain { chain_id, .. }) => {
                error_span!(parent: &p, "InitChain", ?chain_id)
            },
            Self::BeginBlock(BeginBlock { header, .. }) => {
                error_span!(parent: &p, "BeginBlock", height = ?header.height.value())
            },
            Self::DeliverTx(DeliverTx { tx }) => {
                error_span!(parent: &p, "DeliverTx", tx_id = ?hex::encode(Sha256::digest(tx.as_ref())))
            },
            Self::EndBlock(EndBlock { height }) => {
                error_span!(parent: &p, "EndBlock", ?height)
            },
            Self::Commit => {
                error_span!(parent: &p, "Commit")
            },
        }
    }
}

impl RequestExt for InfoRequest {
    fn create_span(&self) -> Span {
        let p = error_span!("abci");

        match self {
            Self::Info(_) => error_span!(parent: &p, "Info"),
            Self::Query(Query {
                path,
                height,
                prove,
                ..
            }) => {
                error_span!(parent: &p, "Query", ?path, ?height, prove)
            },
            Self::Echo(_) => error_span!(parent: &p, "Echo"),
            Self::SetOption(_) => todo!("not implemented"),
        }
    }
}

impl RequestExt for MempoolRequest {
    fn create_span(&self) -> Span {
        let p = error_span!("abci");

        match self {
            Self::CheckTx(CheckTx { kind, tx }) => {
                error_span!(parent: &p, "CheckTx", ?kind, tx_id = ?hex::encode(Sha256::digest(tx.as_ref())))
            },
        }
    }
}

impl RequestExt for SnapshotRequest {
    fn create_span(&self) -> Span {
        let p = error_span!("abci");

        match self {
            Self::ListSnapshots => {
                error_span!(parent: &p, "ListSnapshots")
            },
            Self::OfferSnapshot(OfferSnapshot { snapshot, app_hash }) => {
                error_span!(parent: &p, "OfferSnapshot", app_hash = ?app_hash, height = snapshot.height.value(), chunks = snapshot.chunks)
            },
            Self::LoadSnapshotChunk(LoadSnapshotChunk { height, chunk, .. }) => {
                error_span!(
                    parent: &p,
                    "LoadSnapshotChunk",
                    height = height.value(),
                    chunk = chunk
                )
            },
            Self::ApplySnapshotChunk(ApplySnapshotChunk { index, sender, .. }) => {
                error_span!(
                    parent: &p,
                    "ApplySnapshotChunk",
                    index = index,
                    sender = sender
                )
            },
        }
    }
}