Skip to content
This repository has been archived by the owner on Mar 13, 2023. It is now read-only.

Update dvm from 426 to 435 #817

Merged
merged 11 commits into from
Sep 18, 2021
4 changes: 4 additions & 0 deletions Cargo.lock

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

3 changes: 3 additions & 0 deletions client/dvm/db/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,6 @@ parking_lot = { version = "0.11.1" }
sp-core = { git = "https://github.com/darwinia-network/substrate", branch = "main" }
sp-database = { git = "https://github.com/darwinia-network/substrate", branch = "main" }
sp-runtime = { git = "https://github.com/darwinia-network/substrate", branch = "main" }
# darwinia-network
dp-storage = { path = "../../../primitives/storage/" }
dvm-ethereum = { path = "../../../frame/dvm"}
47 changes: 41 additions & 6 deletions client/dvm/db/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,23 @@

mod utils;

// --- paritytech ---
pub use sp_database::Database;
// --- darwinia-network ---
use codec::{Decode, Encode};
use parking_lot::Mutex;
use sp_core::H256;
use sp_runtime::traits::Block as BlockT;

// --- std ---
use std::{
marker::PhantomData,
path::{Path, PathBuf},
sync::Arc,
};
// --- crates.io ---
use codec::{Decode, Encode};
use parking_lot::Mutex;
// --- paritytech ---
use sp_core::H256;
use sp_runtime::traits::Block as BlockT;
// --- darwinia-network ---
use dp_storage::PALLET_ETHEREUM_SCHEMA_CACHE;
use dvm_ethereum::EthereumStorageSchema;

const DB_HASH_LEN: usize = 32;
/// Hash type that this backend uses for the database.
Expand Down Expand Up @@ -140,6 +144,37 @@ impl<Block: BlockT> MetaDb<Block> {

Ok(())
}

pub fn ethereum_schema(&self) -> Result<Option<Vec<(EthereumStorageSchema, H256)>>, String> {
match self
.db
.get(crate::columns::META, &PALLET_ETHEREUM_SCHEMA_CACHE.encode())
{
Some(raw) => Ok(Some(
Decode::decode(&mut &raw[..]).map_err(|e| format!("{:?}", e))?,
)),
None => Ok(None),
}
}

pub fn write_ethereum_schema(
&self,
new_cache: Vec<(EthereumStorageSchema, H256)>,
) -> Result<(), String> {
let mut transaction = sp_database::Transaction::new();

transaction.set(
crate::columns::META,
&PALLET_ETHEREUM_SCHEMA_CACHE.encode(),
&new_cache.encode(),
);

self.db
.commit(transaction)
.map_err(|e| format!("{:?}", e))?;

Ok(())
}
}

pub struct MappingCommitment<Block: BlockT> {
Expand Down
12 changes: 10 additions & 2 deletions client/dvm/mapping-sync/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

mod worker;

pub use worker::MappingSyncWorker;
pub use worker::{MappingSyncWorker, SyncStrategy};

// --- darwinia-network ---
use dp_consensus::FindLogError;
Expand Down Expand Up @@ -99,6 +99,7 @@ pub fn sync_one_block<Block: BlockT, C, B>(
client: &C,
substrate_backend: &B,
frontier_backend: &dc_db::Backend<Block>,
strategy: SyncStrategy,
) -> Result<bool, String>
where
C: ProvideRuntimeApi<Block> + Send + Sync + HeaderBackend<Block> + BlockOf,
Expand Down Expand Up @@ -152,6 +153,11 @@ where
.write_current_syncing_tips(current_syncing_tips)?;
Ok(true)
} else {
if SyncStrategy::Parachain == strategy
&& operating_header.number() > &client.info().best_number
{
return Ok(false);
}
sync_block(frontier_backend, &operating_header)?;

current_syncing_tips.push(*operating_header.parent_hash());
Expand All @@ -167,6 +173,7 @@ pub fn sync_blocks<Block: BlockT, C, B>(
substrate_backend: &B,
frontier_backend: &dc_db::Backend<Block>,
limit: usize,
strategy: SyncStrategy,
) -> Result<bool, String>
where
C: ProvideRuntimeApi<Block> + Send + Sync + HeaderBackend<Block> + BlockOf,
Expand All @@ -176,7 +183,8 @@ where
let mut synced_any = false;

for _ in 0..limit {
synced_any = synced_any || sync_one_block(client, substrate_backend, frontier_backend)?;
synced_any =
synced_any || sync_one_block(client, substrate_backend, frontier_backend, strategy)?;
}

Ok(synced_any)
Expand Down
10 changes: 10 additions & 0 deletions client/dvm/mapping-sync/src/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ use log::debug;

const LIMIT: usize = 8;

#[derive(PartialEq, Copy, Clone)]
pub enum SyncStrategy {
Normal,
Parachain,
}

pub struct MappingSyncWorker<Block: BlockT, C, B> {
import_notifications: ImportNotifications<Block>,
timeout: Duration,
Expand All @@ -44,6 +50,7 @@ pub struct MappingSyncWorker<Block: BlockT, C, B> {
frontier_backend: Arc<dc_db::Backend<Block>>,

have_next: bool,
strategy: SyncStrategy,
}

impl<Block: BlockT, C, B> MappingSyncWorker<Block, C, B> {
Expand All @@ -53,6 +60,7 @@ impl<Block: BlockT, C, B> MappingSyncWorker<Block, C, B> {
client: Arc<C>,
substrate_backend: Arc<B>,
frontier_backend: Arc<dc_db::Backend<Block>>,
strategy: SyncStrategy,
) -> Self {
Self {
import_notifications,
Expand All @@ -64,6 +72,7 @@ impl<Block: BlockT, C, B> MappingSyncWorker<Block, C, B> {
frontier_backend,

have_next: true,
strategy,
}
}
}
Expand Down Expand Up @@ -111,6 +120,7 @@ where
self.substrate_backend.blockchain(),
self.frontier_backend.as_ref(),
LIMIT,
self.strategy,
) {
Ok(have_next) => {
self.have_next = have_next;
Expand Down
Loading