referrerpolicy=no-referrer-when-downgrade

snowbridge_ethereum/
log.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: 2023 Snowfork <hello@snowfork.com>
3use codec::{Decode, Encode};
4use ethereum_types::{H160, H256};
5use sp_std::prelude::*;
6
7#[derive(Clone, Debug, Encode, Decode, PartialEq, Eq)]
8pub struct Log {
9	pub address: H160,
10	pub topics: Vec<H256>,
11	pub data: Vec<u8>,
12}
13
14impl rlp::Decodable for Log {
15	/// We need to implement rlp::Decodable manually as the derive macro RlpDecodable
16	/// didn't seem to generate the correct code for parsing our logs.
17	fn decode(rlp: &rlp::Rlp) -> Result<Self, rlp::DecoderError> {
18		let mut iter = rlp.iter();
19
20		let address: H160 = match iter.next() {
21			Some(data) => data.as_val()?,
22			None => return Err(rlp::DecoderError::Custom("Expected log address")),
23		};
24
25		let topics: Vec<H256> = match iter.next() {
26			Some(data) => data.as_list()?,
27			None => return Err(rlp::DecoderError::Custom("Expected log topics")),
28		};
29
30		let data: Vec<u8> = match iter.next() {
31			Some(data) => data.data()?.to_vec(),
32			None => return Err(rlp::DecoderError::Custom("Expected log data")),
33		};
34
35		Ok(Self { address, topics, data })
36	}
37}
38
39#[cfg(test)]
40mod tests {
41
42	use super::Log;
43	use hex_literal::hex;
44
45	const RAW_LOG: [u8; 605] = hex!(
46		"
47		f9025a941cfd66659d44cfe2e627c5742ba7477a3284cffae1a0266413be5700ce8dd5ac6b9a7dfb
48		abe99b3e45cae9a68ac2757858710b401a38b9022000000000000000000000000000000000000000
49		00000000000000000000000060000000000000000000000000000000000000000000000000000000
50		00000000c00000000000000000000000000000000000000000000000000000000000000100000000
51		00000000000000000000000000000000000000000000000000000000283163466436363635394434
52		34636665324536323763353734324261373437376133323834634666410000000000000000000000
53		00000000000000000000000000000000000000000000000000000000000000000000000000000000
54		000000000773656e6445544800000000000000000000000000000000000000000000000000000000
55		00000000000000000000000000000000000000000000000000000001000000000000000000000000
56		00cffeaaf7681c89285d65cfbe808b80e50269657300000000000000000000000000000000000000
57		000000000000000000000000a0000000000000000000000000000000000000000000000000000000
58		0000000000000000000000000000000000000000000000000000000000000000000000000a000000
59		00000000000000000000000000000000000000000000000000000000020000000000000000000000
60		00000000000000000000000000000000000000002f3146524d4d3850456957585961783772705336
61		5834585a5831614141785357783143724b5479725659685632346667000000000000000000000000
62		0000000000
63	"
64	);
65
66	#[test]
67	fn decode_log() {
68		let log: Log = rlp::decode(&RAW_LOG).unwrap();
69		assert_eq!(log.address.as_bytes(), hex!["1cfd66659d44cfe2e627c5742ba7477a3284cffa"]);
70		assert_eq!(
71			log.topics[0].as_bytes(),
72			hex!["266413be5700ce8dd5ac6b9a7dfbabe99b3e45cae9a68ac2757858710b401a38"]
73		);
74	}
75}