precompile_utils/evm/
logs.rs

1// This file is part of Frontier.
2
3// Copyright (c) Moonsong Labs.
4// Copyright (C) Parity Technologies (UK) Ltd.
5// SPDX-License-Identifier: Apache-2.0
6
7// Licensed under the Apache License, Version 2.0 (the "License");
8// you may not use this file except in compliance with the License.
9// You may obtain a copy of the License at
10//
11// 	http://www.apache.org/licenses/LICENSE-2.0
12//
13// Unless required by applicable law or agreed to in writing, software
14// distributed under the License is distributed on an "AS IS" BASIS,
15// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16// See the License for the specific language governing permissions and
17// limitations under the License.
18
19use crate::EvmResult;
20use alloc::{vec, vec::Vec};
21use pallet_evm::{Log, PrecompileHandle};
22use sp_core::{H160, H256};
23
24/// Create a 0-topic log.
25#[must_use]
26pub fn log0(address: impl Into<H160>, data: impl Into<Vec<u8>>) -> Log {
27	Log {
28		address: address.into(),
29		topics: vec![],
30		data: data.into(),
31	}
32}
33
34/// Create a 1-topic log.
35#[must_use]
36pub fn log1(address: impl Into<H160>, topic0: impl Into<H256>, data: impl Into<Vec<u8>>) -> Log {
37	Log {
38		address: address.into(),
39		topics: vec![topic0.into()],
40		data: data.into(),
41	}
42}
43
44/// Create a 2-topics log.
45#[must_use]
46pub fn log2(
47	address: impl Into<H160>,
48	topic0: impl Into<H256>,
49	topic1: impl Into<H256>,
50	data: impl Into<Vec<u8>>,
51) -> Log {
52	Log {
53		address: address.into(),
54		topics: vec![topic0.into(), topic1.into()],
55		data: data.into(),
56	}
57}
58
59/// Create a 3-topics log.
60#[must_use]
61pub fn log3(
62	address: impl Into<H160>,
63	topic0: impl Into<H256>,
64	topic1: impl Into<H256>,
65	topic2: impl Into<H256>,
66	data: impl Into<Vec<u8>>,
67) -> Log {
68	Log {
69		address: address.into(),
70		topics: vec![topic0.into(), topic1.into(), topic2.into()],
71		data: data.into(),
72	}
73}
74
75/// Create a 4-topics log.
76#[must_use]
77pub fn log4(
78	address: impl Into<H160>,
79	topic0: impl Into<H256>,
80	topic1: impl Into<H256>,
81	topic2: impl Into<H256>,
82	topic3: impl Into<H256>,
83	data: impl Into<Vec<u8>>,
84) -> Log {
85	Log {
86		address: address.into(),
87		topics: vec![topic0.into(), topic1.into(), topic2.into(), topic3.into()],
88		data: data.into(),
89	}
90}
91
92/// Extension trait allowing to record logs into a PrecompileHandle.
93pub trait LogExt {
94	fn record(self, handle: &mut impl PrecompileHandle) -> EvmResult;
95
96	fn compute_cost(&self) -> EvmResult<u64>;
97}
98
99impl LogExt for Log {
100	fn record(self, handle: &mut impl PrecompileHandle) -> EvmResult {
101		handle.log(self.address, self.topics, self.data)?;
102		Ok(())
103	}
104
105	fn compute_cost(&self) -> EvmResult<u64> {
106		crate::evm::costs::log_costs(self.topics.len(), self.data.len())
107	}
108}