Skip to content

Commit

Permalink
Add sql impl
Browse files Browse the repository at this point in the history
  • Loading branch information
boundless-forest committed Apr 26, 2024
1 parent b37678d commit 439d85e
Show file tree
Hide file tree
Showing 20 changed files with 109 additions and 155 deletions.
2 changes: 0 additions & 2 deletions Cargo.lock

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

1 change: 0 additions & 1 deletion client/api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,3 @@ sp-core = { workspace = true, features = ["default"] }
sp-runtime = { workspace = true, features = ["default"] }
# Frontier
fp-storage = { workspace = true, features = ["default"] }
fc-rpc-core = { workspace = true }
15 changes: 4 additions & 11 deletions client/api/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@
use scale_codec::{Decode, Encode};
// Substrate
use sp_core::{H160, H256};
use sp_runtime::{generic::BlockId, traits::Block as BlockT};
use sp_runtime::traits::Block as BlockT;
// Frontier
use fc_rpc_core::types::BlockNumberOrHash;
use fp_storage::EthereumStorageSchema;

#[derive(Clone, Debug, Eq, PartialEq, Encode, Decode)]
Expand All @@ -34,15 +33,6 @@ pub struct TransactionMetadata<Block: BlockT> {
/// The frontier backend interface.
#[async_trait::async_trait]
pub trait Backend<Block: BlockT>: Send + Sync {
/// Get the substrate block id with the given number or hash.
async fn block_id(
&self,
number_or_hash: Option<BlockNumberOrHash>,
) -> Result<Option<BlockId<Block>>, String>;

/// Check if the block is canon.
async fn is_canon(&self, target_hash: Block::Hash) -> bool;

/// Get the substrate hash with the given ethereum block hash.
async fn block_hash(
&self,
Expand All @@ -62,6 +52,9 @@ pub trait Backend<Block: BlockT>: Send + Sync {
fn is_indexed(&self) -> bool {
self.log_indexer().is_indexed()
}

/// Get the latest substrate block hash in the sql database.
async fn best_hash(&self) -> Result<Block::Hash, String>;
}

#[derive(Debug, Eq, PartialEq)]
Expand Down
9 changes: 4 additions & 5 deletions client/cli/src/frontier_db_cmd/mapping_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,16 @@ pub enum MappingKey {
EthBlockOrTransactionHash(H256),
}

pub struct MappingDb<'a, C: HeaderBackend<B>, B: BlockT> {
pub struct MappingDb<'a, B: BlockT, C: HeaderBackend<B>> {
cmd: &'a FrontierDbCmd,
client: Arc<C>,
backend: Arc<fc_db::kv::Backend<B, C>>,
}

impl<'a, C, B: BlockT> MappingDb<'a, C, B>
impl<'a, B: BlockT, C> MappingDb<'a, B, C>
where
C: ProvideRuntimeApi<B>,
C: HeaderBackend<B> + ProvideRuntimeApi<B>,
C::Api: EthereumRuntimeRPCApi<B>,
C: HeaderBackend<B>,
{
pub fn new(
cmd: &'a FrontierDbCmd,
Expand Down Expand Up @@ -176,4 +175,4 @@ where
}
}

impl<'a, C: HeaderBackend<B>, B: BlockT> FrontierDbMessage for MappingDb<'a, C, B> {}
impl<'a, B: BlockT, C: HeaderBackend<B>> FrontierDbMessage for MappingDb<'a, B, C> {}
2 changes: 1 addition & 1 deletion client/cli/src/frontier_db_cmd/meta_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ use std::{

use ethereum_types::H256;
use serde::Deserialize;
use sp_blockchain::HeaderBackend;
// Substrate
use sp_blockchain::HeaderBackend;
use sp_runtime::traits::Block as BlockT;

use super::{utils::FrontierDbMessage, FrontierDbCmd, Operation};
Expand Down
5 changes: 2 additions & 3 deletions client/cli/src/frontier_db_cmd/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,14 @@ pub enum DbValue<H> {
}

impl FrontierDbCmd {
pub fn run<C, B: BlockT>(
pub fn run<B: BlockT, C>(
&self,
client: Arc<C>,
backend: Arc<fc_db::kv::Backend<B, C>>,
) -> sc_cli::Result<()>
where
C: ProvideRuntimeApi<B>,
C: HeaderBackend<B> + ProvideRuntimeApi<B>,
C::Api: fp_rpc::EthereumRuntimeRPCApi<B>,
C: HeaderBackend<B>,
{
match self.column {
Column::Meta => {
Expand Down
4 changes: 2 additions & 2 deletions client/cli/src/frontier_db_cmd/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ type OpaqueBlock =
pub fn open_frontier_backend<Block: BlockT, C: HeaderBackend<Block>>(
client: Arc<C>,
path: PathBuf,
) -> Result<Arc<fc_db::kv::Backend<Block>>, String> {
Ok(Arc::new(fc_db::kv::Backend::<Block>::new(
) -> Result<Arc<fc_db::kv::Backend<Block, C>>, String> {
Ok(Arc::new(fc_db::kv::Backend::<Block, C>::new(
client,
&fc_db::kv::DatabaseSettings {
source: sc_client_db::DatabaseSource::RocksDb {
Expand Down
1 change: 0 additions & 1 deletion client/db/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ sp-runtime = { workspace = true }
sp-storage = { workspace = true, optional = true }
# Frontier
fc-api = { workspace = true }
fc-rpc-core = { 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
43 changes: 5 additions & 38 deletions client/db/src/kv/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,10 @@ pub use sc_client_db::DatabaseSource;
use sp_blockchain::HeaderBackend;
use sp_core::{H160, H256};
pub use sp_database::Database;
use sp_runtime::{
generic::BlockId,
traits::{Block as BlockT, UniqueSaturatedInto, Zero},
};
use sp_runtime::traits::Block as BlockT;

// Frontier
use fc_api::{FilteredLog, TransactionMetadata};
use fc_rpc_core::types::BlockNumberOrHash;
use fp_storage::{EthereumStorageSchema, PALLET_ETHEREUM_SCHEMA_CACHE};

const DB_HASH_LEN: usize = 32;
Expand Down Expand Up @@ -76,39 +72,6 @@ pub struct Backend<Block: BlockT, C: HeaderBackend<Block>> {

#[async_trait::async_trait]
impl<Block: BlockT, C: HeaderBackend<Block>> fc_api::Backend<Block> for Backend<Block, C> {
async fn block_id(
&self,
number_or_hash: Option<BlockNumberOrHash>,
) -> Result<Option<BlockId<Block>>, String> {
Ok(match number_or_hash.unwrap_or(BlockNumberOrHash::Latest) {
BlockNumberOrHash::Hash { hash, .. } => {
if let Ok(Some(substrate_hashes)) = self.block_hash(&hash).await {
for hash in substrate_hashes {
if self.is_canon(hash).await {
return Ok(Some(BlockId::Hash(hash)));
}
}
}
None
}
BlockNumberOrHash::Num(number) => Some(BlockId::Number(number.unique_saturated_into())),
BlockNumberOrHash::Latest => Some(BlockId::Hash(self.client.info().best_hash)),
BlockNumberOrHash::Earliest => Some(BlockId::Number(Zero::zero())),
BlockNumberOrHash::Pending => None,
BlockNumberOrHash::Safe => Some(BlockId::Hash(self.client.info().finalized_hash)),
BlockNumberOrHash::Finalized => Some(BlockId::Hash(self.client.info().finalized_hash)),
})
}

async fn is_canon(&self, target_hash: Block::Hash) -> bool {
if let Ok(Some(number)) = self.client.number(target_hash) {
if let Ok(Some(hash)) = self.client.hash(number) {
return hash == target_hash;
}
}
false
}

async fn block_hash(
&self,
ethereum_block_hash: &H256,
Expand All @@ -127,6 +90,10 @@ impl<Block: BlockT, C: HeaderBackend<Block>> fc_api::Backend<Block> for Backend<
fn log_indexer(&self) -> &dyn fc_api::LogIndexerBackend<Block> {
&self.log_indexer
}

async fn best_hash(&self) -> Result<Block::Hash, String> {
Ok(self.client.info().best_hash)
}
}

#[derive(Clone, Default)]
Expand Down
6 changes: 4 additions & 2 deletions client/db/src/kv/upgrade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -348,8 +348,10 @@ mod tests {
pub fn open_frontier_backend<Block: BlockT, C: HeaderBackend<Block>>(
client: Arc<C>,
setting: &crate::kv::DatabaseSettings,
) -> Result<Arc<crate::kv::Backend<Block>>, String> {
Ok(Arc::new(crate::kv::Backend::<Block>::new(client, setting)?))
) -> Result<Arc<crate::kv::Backend<Block, C>>, String> {
Ok(Arc::new(crate::kv::Backend::<Block, C>::new(
client, setting,
)?))
}

#[cfg_attr(not(feature = "rocksdb"), ignore)]
Expand Down
5 changes: 3 additions & 2 deletions client/db/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@
// 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)]
// #![deny(unused_crate_dependencies)]

use std::sync::Arc;

// Substrate
pub use sc_client_db::DatabaseSource;
use sp_blockchain::HeaderBackend;
use sp_runtime::traits::Block as BlockT;
use std::sync::Arc;

pub mod kv;
#[cfg(feature = "sql")]
Expand Down
Loading

0 comments on commit 439d85e

Please sign in to comment.