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
use hedera_proto::services;

use crate::protobuf::{
    FromProtobuf,
    ToProtobuf,
};
use crate::ContractId;

/// The log information for an event returned by a smart contract function call.
/// One function call may return several such events.
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct ContractLogInfo {
    /// Address of the contract that emitted the event.
    pub contract_id: ContractId,

    /// Bloom filter for this log.
    pub bloom: Vec<u8>,

    /// A list of topics this log is relevent to.
    pub topics: Vec<Vec<u8>>,

    /// The log's data payload.
    pub data: Vec<u8>,
}

impl ContractLogInfo {
    /// Create a new `ContractLogInfo` from protobuf-encoded `bytes`.
    ///
    /// # Errors
    /// - [`Error::FromProtobuf`](crate::Error::FromProtobuf) if decoding the bytes fails to produce a valid protobuf.
    /// - [`Error::FromProtobuf`](crate::Error::FromProtobuf) if decoding the protobuf fails.
    pub fn from_bytes(bytes: &[u8]) -> crate::Result<Self> {
        FromProtobuf::from_bytes(bytes)
    }

    /// Convert `self` to a protobuf-encoded [`Vec<u8>`].
    #[must_use]
    pub fn to_bytes(&self) -> Vec<u8> {
        ToProtobuf::to_bytes(self)
    }
}

impl FromProtobuf<services::ContractLoginfo> for ContractLogInfo {
    fn from_protobuf(pb: services::ContractLoginfo) -> crate::Result<Self>
    where
        Self: Sized,
    {
        Ok(Self {
            contract_id: ContractId::from_protobuf(pb_getf!(pb, contract_id)?)?,
            bloom: pb.bloom,
            topics: pb.topic,
            data: pb.data,
        })
    }
}

impl ToProtobuf for ContractLogInfo {
    type Protobuf = services::ContractLoginfo;

    fn to_protobuf(&self) -> Self::Protobuf {
        Self::Protobuf {
            contract_id: Some(self.contract_id.to_protobuf()),
            bloom: self.bloom.clone(),
            topic: self.topics.clone(),
            data: self.data.clone(),
        }
    }
}

#[cfg(test)]
mod tests {
    use expect_test::expect;
    use hedera_proto::services;
    use prost::Message;

    use crate::protobuf::{
        FromProtobuf,
        ToProtobuf,
    };
    use crate::ContractLogInfo;

    fn make_info() -> services::ContractLoginfo {
        services::ContractLoginfo {
            contract_id: Some(services::ContractId {
                shard_num: 0,
                realm_num: 0,
                contract: Some(services::contract_id::Contract::ContractNum(10)),
            }),
            bloom: b"bloom".to_vec(),
            topic: Vec::from([b"bloom".to_vec()]),
            data: b"data".to_vec(),
        }
    }

    #[test]
    fn from_protobuf() {
        expect![[r#"
            ContractLogInfo {
                contract_id: "0.0.10",
                bloom: [
                    98,
                    108,
                    111,
                    111,
                    109,
                ],
                topics: [
                    [
                        98,
                        108,
                        111,
                        111,
                        109,
                    ],
                ],
                data: [
                    100,
                    97,
                    116,
                    97,
                ],
            }
        "#]]
        .assert_debug_eq(&ContractLogInfo::from_protobuf(make_info()).unwrap());
    }

    #[test]
    fn to_protobuf() {
        expect![[r#"
            ContractLoginfo {
                contract_id: Some(
                    ContractId {
                        shard_num: 0,
                        realm_num: 0,
                        contract: Some(
                            ContractNum(
                                10,
                            ),
                        ),
                    },
                ),
                bloom: [
                    98,
                    108,
                    111,
                    111,
                    109,
                ],
                topic: [
                    [
                        98,
                        108,
                        111,
                        111,
                        109,
                    ],
                ],
                data: [
                    100,
                    97,
                    116,
                    97,
                ],
            }
        "#]]
        .assert_debug_eq(&ContractLogInfo::from_protobuf(make_info()).unwrap().to_protobuf())
    }

    #[test]
    fn from_bytes() {
        expect![[r#"
            ContractLogInfo {
                contract_id: "0.0.10",
                bloom: [
                    98,
                    108,
                    111,
                    111,
                    109,
                ],
                topics: [
                    [
                        98,
                        108,
                        111,
                        111,
                        109,
                    ],
                ],
                data: [
                    100,
                    97,
                    116,
                    97,
                ],
            }
        "#]]
        .assert_debug_eq(&ContractLogInfo::from_bytes(&make_info().encode_to_vec()).unwrap());
    }
}