reth_primitives_traits/
log.rs

1#[cfg(test)]
2mod tests {
3    use alloy_primitives::{Address, Bytes, Log as AlloyLog, B256};
4    use alloy_rlp::{RlpDecodable, RlpEncodable};
5    use proptest::proptest;
6    use proptest_arbitrary_interop::arb;
7    use reth_codecs::{add_arbitrary_tests, Compact};
8    use serde::{Deserialize, Serialize};
9
10    /// This type is kept for compatibility tests after the codec support was added to
11    /// alloy-primitives Log type natively
12    #[derive(
13        Clone,
14        Debug,
15        PartialEq,
16        Eq,
17        RlpDecodable,
18        RlpEncodable,
19        Default,
20        Serialize,
21        Deserialize,
22        Compact,
23    )]
24    #[cfg_attr(any(test, feature = "arbitrary"), derive(arbitrary::Arbitrary))]
25    #[add_arbitrary_tests(compact, rlp)]
26    struct Log {
27        /// Contract that emitted this log.
28        address: Address,
29        /// Topics of the log. The number of logs depends on what `LOG` opcode is used.
30        topics: Vec<B256>,
31        /// Arbitrary length data.
32        data: Bytes,
33    }
34
35    impl From<AlloyLog> for Log {
36        fn from(mut log: AlloyLog) -> Self {
37            Self {
38                address: log.address,
39                topics: std::mem::take(log.data.topics_mut_unchecked()),
40                data: log.data.data,
41            }
42        }
43    }
44
45    impl From<Log> for AlloyLog {
46        fn from(log: Log) -> Self {
47            Self::new_unchecked(log.address, log.topics, log.data)
48        }
49    }
50
51    proptest! {
52        #[test]
53        fn test_roundtrip_conversion_between_log_and_alloy_log(log in arb::<Log>()) {
54            // Convert log to buffer and then create alloy_log from buffer and compare
55            let mut compacted_log = Vec::<u8>::new();
56            let len = log.to_compact(&mut compacted_log);
57
58            let alloy_log = AlloyLog::from_compact(&compacted_log, len).0;
59            assert_eq!(log, alloy_log.into());
60
61            // Create alloy_log from log and then convert it to buffer and compare compacted_alloy_log and compacted_log
62            let alloy_log = AlloyLog::new_unchecked(log.address, log.topics, log.data);
63            let mut compacted_alloy_log = Vec::<u8>::new();
64            let alloy_len = alloy_log.to_compact(&mut compacted_alloy_log);
65            assert_eq!(len, alloy_len);
66            assert_eq!(compacted_log, compacted_alloy_log);
67        }
68    }
69}