Skip to content

Commit

Permalink
feat: add fc-api to improve fc-db (#1076)
Browse files Browse the repository at this point in the history
* feat: add fc-api to improve fc-db

* fix tests

* fix
  • Loading branch information
koushiro authored Jul 31, 2023
1 parent 194f6bb commit ff8bd85
Show file tree
Hide file tree
Showing 19 changed files with 268 additions and 160 deletions.
15 changes: 15 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ members = [
"frame/evm/precompile/bls12377",
"frame/evm/precompile/dispatch",
"frame/evm/precompile/curve25519",
"client/api",
"client/consensus",
"client/rpc-core",
"client/rpc",
Expand Down Expand Up @@ -134,6 +135,7 @@ substrate-frame-rpc-system = { version = "4.0.0-dev", git = "https://github.com/
substrate-test-runtime-client = { version = "2.0.0", git = "https://github.com/paritytech/substrate", branch = "master" }
substrate-wasm-builder = { version = "5.0.0-dev", git = "https://github.com/paritytech/substrate", branch = "master" }
# Frontier Client
fc-api = { version = "1.0.0-dev", path = "client/api" }
fc-cli = { version = "1.0.0-dev", path = "client/cli", default-features = false }
fc-consensus = { version = "2.0.0-dev", path = "client/consensus" }
fc-db = { version = "2.0.0-dev", path = "client/db", default-features = false }
Expand Down
20 changes: 20 additions & 0 deletions client/api/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[package]
name = "fc-api"
version = "1.0.0-dev"
license = "GPL-3.0-or-later WITH Classpath-exception-2.0"
description = "Frontier client interfaces"
authors = { workspace = true }
edition = { workspace = true }
repository = { workspace = true }

[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]

[dependencies]
async-trait = { workspace = true }
scale-codec = { package = "parity-scale-codec", workspace = true }
# Substrate
sp-core = { workspace = true, features = ["default"] }
sp-runtime = { workspace = true, features = ["default"] }
# Frontier
fp-storage = { workspace = true, features = ["default"] }
81 changes: 81 additions & 0 deletions client/api/src/backend.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
// This file is part of Frontier.
//
// Copyright (c) 2023 Parity Technologies (UK) Ltd.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

use scale_codec::{Decode, Encode};
// Substrate
use sp_core::{H160, H256};
use sp_runtime::traits::Block as BlockT;
// Frontier
use fp_storage::EthereumStorageSchema;

#[derive(Clone, Debug, Eq, PartialEq, Encode, Decode)]
pub struct TransactionMetadata<Block: BlockT> {
pub substrate_block_hash: Block::Hash,
pub ethereum_block_hash: H256,
pub ethereum_index: u32,
}

/// The frontier backend interface.
#[async_trait::async_trait]
pub trait Backend<Block: BlockT>: Send + Sync {
/// Get the substrate hash with the given ethereum block hash.
async fn block_hash(
&self,
ethereum_block_hash: &H256,
) -> Result<Option<Vec<Block::Hash>>, String>;

/// Get the transaction metadata with the given ethereum block hash.
async fn transaction_metadata(
&self,
ethereum_transaction_hash: &H256,
) -> Result<Vec<TransactionMetadata<Block>>, String>;

/// Returns reference to log indexer backend.
fn log_indexer(&self) -> &dyn LogIndexerBackend<Block>;

/// Indicate whether the log indexing feature is supported.
fn is_indexed(&self) -> bool {
self.log_indexer().is_indexed()
}
}

#[derive(Debug, Eq, PartialEq)]
pub struct FilteredLog<Block: BlockT> {
pub substrate_block_hash: Block::Hash,
pub ethereum_block_hash: H256,
pub block_number: u32,
pub ethereum_storage_schema: EthereumStorageSchema,
pub transaction_index: u32,
pub log_index: u32,
}

/// The log indexer backend interface.
#[async_trait::async_trait]
pub trait LogIndexerBackend<Block: BlockT>: Send + Sync {
/// Indicate whether the log indexing feature is supported.
fn is_indexed(&self) -> bool;

/// Filter the logs by the parameters.
async fn filter_logs(
&self,
from_block: u64,
to_block: u64,
addresses: Vec<H160>,
topics: Vec<Vec<Option<H256>>>,
) -> Result<Vec<FilteredLog<Block>>, String>;
}
23 changes: 23 additions & 0 deletions client/api/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
// This file is part of Frontier.
//
// Copyright (c) 2023 Parity Technologies (UK) Ltd.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

#![deny(unused_crate_dependencies)]

pub mod backend;

pub use self::backend::*;
1 change: 1 addition & 0 deletions client/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ sp-consensus = { workspace = true }
sp-io = { workspace = true }
substrate-test-runtime-client = { workspace = true }
# Frontier
fc-api = { workspace = true }
fc-db = { workspace = true, features = ["rocksdb"] }
frontier-template-runtime = { workspace = true, features = ["default"] }

Expand Down
16 changes: 8 additions & 8 deletions client/cli/src/frontier_db_cmd/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -579,8 +579,8 @@ fn commitment_create() {
);

// Expect the offchain-stored transaction metadata to match the one we stored in the runtime.
let expected_transaction_metadata = fc_db::TransactionMetadata {
block_hash,
let expected_transaction_metadata = fc_api::TransactionMetadata {
substrate_block_hash: block_hash,
ethereum_block_hash,
ethereum_index: 0,
};
Expand Down Expand Up @@ -660,8 +660,8 @@ fn commitment_update() {
);

// Expect the offchain-stored transaction metadata to match the one we stored in the runtime.
let expected_transaction_metadata_a1_t1 = fc_db::TransactionMetadata {
block_hash: block_a1_hash,
let expected_transaction_metadata_a1_t1 = fc_api::TransactionMetadata {
substrate_block_hash: block_a1_hash,
ethereum_block_hash,
ethereum_index: 0,
};
Expand Down Expand Up @@ -706,13 +706,13 @@ fn commitment_update() {
);

// Expect the offchain-stored transaction metadata to have data for both blocks.
let expected_transaction_metadata_a2_t1 = fc_db::TransactionMetadata {
block_hash: block_a2_hash,
let expected_transaction_metadata_a2_t1 = fc_api::TransactionMetadata {
substrate_block_hash: block_a2_hash,
ethereum_block_hash,
ethereum_index: 0,
};
let expected_transaction_metadata_a2_t2 = fc_db::TransactionMetadata {
block_hash: block_a2_hash,
let expected_transaction_metadata_a2_t2 = fc_api::TransactionMetadata {
substrate_block_hash: block_a2_hash,
ethereum_block_hash,
ethereum_index: 1,
};
Expand Down
1 change: 1 addition & 0 deletions client/db/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ sp-database = { workspace = true }
sp-runtime = { workspace = true }
sp-storage = { workspace = true, optional = true }
# Frontier
fc-api = { workspace = true }
fc-storage = { workspace = true, optional = true }
fp-consensus = { workspace = true, features = ["default"], optional = true }
fp-rpc = { workspace = true, features = ["default"], optional = true }
Expand Down
Loading

0 comments on commit ff8bd85

Please sign in to comment.