Skip to content

Commit

Permalink
Standalone backend database for Frontier (#295)
Browse files Browse the repository at this point in the history
* Init fc-db crate

* Intergrate fc_db into rpc and consensus module
  • Loading branch information
sorpaas authored Feb 24, 2021
1 parent 57bca6b commit 918c11b
Show file tree
Hide file tree
Showing 13 changed files with 299 additions and 163 deletions.
16 changes: 16 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ members = [
"client/consensus",
"client/rpc-core",
"client/rpc",
"client/db",
"primitives/consensus",
"primitives/evm",
"primitives/rpc",
Expand Down
1 change: 1 addition & 0 deletions client/consensus/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ sp-block-builder = { version = "2.0.0", git = "https://github.com/paritytech/sub
sp-inherents = { version = "2.0.0", git = "https://github.com/paritytech/substrate.git", branch = "frontier" }
fp-consensus = { version = "0.1.0", path = "../../primitives/consensus" }
fp-rpc = { path = "../../primitives/rpc" }
fc-db = { path = "../db" }
sp-consensus = { version = "0.8.0", git = "https://github.com/paritytech/substrate.git", branch = "frontier" }
log = "0.4.8"
futures = { version = "0.3.1", features = ["compat"] }
Expand Down
108 changes: 0 additions & 108 deletions client/consensus/src/aux_schema.rs

This file was deleted.

39 changes: 17 additions & 22 deletions client/consensus/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

mod aux_schema;

pub use crate::aux_schema::{load_block_hash, load_transaction_metadata};

use std::sync::Arc;
use std::collections::HashMap;
use std::marker::PhantomData;
Expand Down Expand Up @@ -63,6 +59,7 @@ impl std::convert::From<Error> for ConsensusError {
pub struct FrontierBlockImport<B: BlockT, I, C> {
inner: I,
client: Arc<C>,
backend: Arc<fc_db::Backend<B>>,
enabled: bool,
_marker: PhantomData<B>,
}
Expand All @@ -72,6 +69,7 @@ impl<Block: BlockT, I: Clone + BlockImport<Block>, C> Clone for FrontierBlockImp
FrontierBlockImport {
inner: self.inner.clone(),
client: self.client.clone(),
backend: self.backend.clone(),
enabled: self.enabled,
_marker: PhantomData,
}
Expand All @@ -89,11 +87,13 @@ impl<B, I, C> FrontierBlockImport<B, I, C> where
pub fn new(
inner: I,
client: Arc<C>,
backend: Arc<fc_db::Backend::<B>>,
enabled: bool,
) -> Self {
Self {
inner,
client,
backend,
enabled,
_marker: PhantomData,
}
Expand Down Expand Up @@ -142,32 +142,27 @@ impl<B, I, C> BlockImport<B> for FrontierBlockImport<B, I, C> where
ConsensusLog::PostBlock(block) => fp_consensus::PostHashes::from_block(block),
};

let res = aux_schema::write_block_hash(client.as_ref(), post_hashes.block_hash, hash, insert_closure!());
let mapping_commitment = fc_db::MappingCommitment {
block_hash: hash,
ethereum_block_hash: post_hashes.block_hash,
ethereum_transaction_hashes: post_hashes.transaction_hashes,
};
let res = self.backend.mapping_db().write_hashes(mapping_commitment);
if res.is_err() { trace!(target: "frontier-consensus", "{:?}", res); }

for (index, transaction_hash) in post_hashes.transaction_hashes.into_iter().enumerate() {
let res = aux_schema::write_transaction_metadata(
client.as_ref(),
transaction_hash,
(post_hashes.block_hash, index as u32),
insert_closure!(),
);
if res.is_err() { trace!(target: "frontier-consensus", "{:?}", res); }
}

// On importing block 1 we also map the genesis block in the auxiliary.
if block.header.number().clone() == One::one() {
let id = BlockId::Number(Zero::zero());
if let Ok(Some(header)) = client.header(id) {
let block = self.client.runtime_api().current_block(&id)
.map_err(|_| Error::RuntimeApiCallFailed)?;
let block_hash = block.unwrap().header.hash();
let res = aux_schema::write_block_hash(
client.as_ref(),
block_hash,
header.hash(),
insert_closure!()
);
let block_hash = block.unwrap().header.hash(); // TODO: shouldn't use unwrap
let mapping_commitment = fc_db::MappingCommitment::<B> {
block_hash: header.hash(),
ethereum_block_hash: block_hash,
ethereum_transaction_hashes: Vec::new(),
};
let res = self.backend.mapping_db().write_hashes(mapping_commitment);
if res.is_err() { trace!(target: "frontier-consensus", "{:?}", res); }
}
}
Expand Down
17 changes: 17 additions & 0 deletions client/db/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[package]
name = "fc-db"
version = "0.1.0"
authors = ["Parity Technologies <admin@parity.io>"]
description = "Frontier database backend"
edition = "2018"
license = "GPL-3.0-or-later WITH Classpath-exception-2.0"
repository = "https://github.com/paritytech/frontier/"

[dependencies]
sp-core = { version = "2.0.0", git = "https://github.com/paritytech/substrate.git", branch = "frontier" }
sp-database = { version = "2.0.0", git = "https://github.com/paritytech/substrate.git", branch = "frontier" }
sp-runtime = { version = "2.0.0", git = "https://github.com/paritytech/substrate.git", branch = "frontier" }
kvdb = "0.8.0"
kvdb-rocksdb = "0.10.0"
codec = { package = "parity-scale-codec", version = "1.3.6", features = ["derive"] }
parking_lot = "0.11.1"
Loading

0 comments on commit 918c11b

Please sign in to comment.