fc_api/
backend.rs

1// This file is part of Frontier.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
5
6// This program is free software: you can redistribute it and/or modify
7// it under the terms of the GNU General Public License as published by
8// the Free Software Foundation, either version 3 of the License, or
9// (at your option) any later version.
10
11// This program is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15
16// You should have received a copy of the GNU General Public License
17// along with this program. If not, see <https://www.gnu.org/licenses/>.
18
19use scale_codec::{Decode, Encode};
20// Substrate
21use sp_core::{H160, H256};
22use sp_runtime::traits::Block as BlockT;
23// Frontier
24use fp_storage::EthereumStorageSchema;
25
26#[derive(Clone, Debug, Eq, PartialEq, Encode, Decode)]
27pub struct TransactionMetadata<Block: BlockT> {
28	pub substrate_block_hash: Block::Hash,
29	pub ethereum_block_hash: H256,
30	pub ethereum_index: u32,
31}
32
33/// The frontier backend interface.
34#[async_trait::async_trait]
35pub trait Backend<Block: BlockT>: Send + Sync {
36	/// Get the substrate hash with the given ethereum block hash.
37	async fn block_hash(
38		&self,
39		ethereum_block_hash: &H256,
40	) -> Result<Option<Vec<Block::Hash>>, String>;
41
42	/// Get the transaction metadata with the given ethereum block hash.
43	async fn transaction_metadata(
44		&self,
45		ethereum_transaction_hash: &H256,
46	) -> Result<Vec<TransactionMetadata<Block>>, String>;
47
48	/// Returns reference to log indexer backend.
49	fn log_indexer(&self) -> &dyn LogIndexerBackend<Block>;
50
51	/// Indicate whether the log indexing feature is supported.
52	fn is_indexed(&self) -> bool {
53		self.log_indexer().is_indexed()
54	}
55
56	/// Get the hash of the oldest substrate block fully indexed by the backend.
57	async fn first_block_hash(&self) -> Result<Block::Hash, String>;
58
59	/// Get the hash of the latest substrate block fully indexed by the backend.
60	async fn latest_block_hash(&self) -> Result<Block::Hash, String>;
61}
62
63#[derive(Debug, Eq, PartialEq)]
64pub struct FilteredLog<Block: BlockT> {
65	pub substrate_block_hash: Block::Hash,
66	pub ethereum_block_hash: H256,
67	pub block_number: u32,
68	pub ethereum_storage_schema: EthereumStorageSchema,
69	pub transaction_index: u32,
70	pub log_index: u32,
71}
72
73/// The log indexer backend interface.
74#[async_trait::async_trait]
75pub trait LogIndexerBackend<Block: BlockT>: Send + Sync {
76	/// Indicate whether the log indexing feature is supported.
77	fn is_indexed(&self) -> bool;
78
79	/// Filter the logs by the parameters.
80	async fn filter_logs(
81		&self,
82		from_block: u64,
83		to_block: u64,
84		addresses: Vec<H160>,
85		topics: Vec<Vec<Option<H256>>>,
86	) -> Result<Vec<FilteredLog<Block>>, String>;
87}