Skip to content

Commit

Permalink
Enable rustfmt (polkadot-evm#432)
Browse files Browse the repository at this point in the history
* rustfmt: add lint

* rustfmt: run format on all code
  • Loading branch information
sorpaas authored Jul 30, 2021
1 parent 4268d00 commit d37ded9
Show file tree
Hide file tree
Showing 60 changed files with 2,343 additions and 1,848 deletions.
18 changes: 18 additions & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,21 @@ jobs:
run: cd ts-tests && npm install
- name: Run functional tests
run: cd ts-tests && npm run build && npm run test
lint:
name: 'Run lints'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Submodules
run: git submodule update --init --recursive
- name: Init
run: |
scripts/init.sh
cargo --version
rustc --version
cargo +$WASM_BUILD_TOOLCHAIN --version
rustc +$WASM_BUILD_TOOLCHAIN --version
env:
WASM_BUILD_TOOLCHAIN: nightly-2020-08-29
- name: Rustfmt
run: cargo fmt --all -- --check
40 changes: 18 additions & 22 deletions client/consensus/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,20 @@
// 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 std::sync::Arc;
use std::collections::HashMap;
use std::marker::PhantomData;
use fp_consensus::{ensure_log, FindLogError};
use fp_rpc::EthereumRuntimeRPCApi;
use sc_client_api::{BlockOf, backend::AuxStore};
use sp_blockchain::{HeaderBackend, ProvideCache, well_known_cache_keys::Id as CacheKeyId};
use sp_block_builder::BlockBuilder as BlockBuilderApi;
use sp_runtime::traits::{Block as BlockT, Header as HeaderT};
use sc_client_api;
use sc_client_api::{backend::AuxStore, BlockOf};
use sp_api::ProvideRuntimeApi;
use sp_block_builder::BlockBuilder as BlockBuilderApi;
use sp_blockchain::{well_known_cache_keys::Id as CacheKeyId, HeaderBackend, ProvideCache};
use sp_consensus::{
BlockImportParams, Error as ConsensusError, BlockImport,
BlockCheckParams, ImportResult,
BlockCheckParams, BlockImport, BlockImportParams, Error as ConsensusError, ImportResult,
};
use sc_client_api;
use sp_runtime::traits::{Block as BlockT, Header as HeaderT};
use std::collections::HashMap;
use std::marker::PhantomData;
use std::sync::Arc;

#[derive(derive_more::Display, Debug)]
pub enum Error {
Expand Down Expand Up @@ -81,19 +80,16 @@ impl<Block: BlockT, I: Clone + BlockImport<Block>, C> Clone for FrontierBlockImp
}
}

impl<B, I, C> FrontierBlockImport<B, I, C> where
impl<B, I, C> FrontierBlockImport<B, I, C>
where
B: BlockT,
I: BlockImport<B, Transaction = sp_api::TransactionFor<C, B>> + Send + Sync,
I::Error: Into<ConsensusError>,
C: ProvideRuntimeApi<B> + Send + Sync + HeaderBackend<B> + AuxStore + ProvideCache<B> + BlockOf,
C::Api: EthereumRuntimeRPCApi<B>,
C::Api: BlockBuilderApi<B>,
{
pub fn new(
inner: I,
client: Arc<C>,
backend: Arc<fc_db::Backend::<B>>,
) -> Self {
pub fn new(inner: I, client: Arc<C>, backend: Arc<fc_db::Backend<B>>) -> Self {
Self {
inner,
client,
Expand All @@ -103,7 +99,8 @@ impl<B, I, C> FrontierBlockImport<B, I, C> where
}
}

impl<B, I, C> BlockImport<B> for FrontierBlockImport<B, I, C> where
impl<B, I, C> BlockImport<B> for FrontierBlockImport<B, I, C>
where
B: BlockT,
I: BlockImport<B, Transaction = sp_api::TransactionFor<C, B>> + Send + Sync,
I::Error: Into<ConsensusError>,
Expand All @@ -114,10 +111,7 @@ impl<B, I, C> BlockImport<B> for FrontierBlockImport<B, I, C> where
type Error = ConsensusError;
type Transaction = sp_api::TransactionFor<C, B>;

fn check_block(
&mut self,
block: BlockCheckParams<B>,
) -> Result<ImportResult, Self::Error> {
fn check_block(&mut self, block: BlockCheckParams<B>) -> Result<ImportResult, Self::Error> {
self.inner.check_block(block).map_err(Into::into)
}

Expand All @@ -131,6 +125,8 @@ impl<B, I, C> BlockImport<B> for FrontierBlockImport<B, I, C> where
// worker.
ensure_log(&block.header.digest()).map_err(|e| Error::from(e))?;

self.inner.import_block(block, new_cache).map_err(Into::into)
self.inner
.import_block(block, new_cache)
.map_err(Into::into)
}
}
81 changes: 50 additions & 31 deletions client/db/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,15 @@ mod utils;

pub use sp_database::Database;

use std::{sync::Arc, path::{Path, PathBuf}, marker::PhantomData};
use codec::{Decode, Encode};
use parking_lot::Mutex;
use sp_core::H256;
use sp_runtime::traits::Block as BlockT;
use parking_lot::Mutex;
use codec::{Encode, Decode};
use std::{
marker::PhantomData,
path::{Path, PathBuf},
sync::Arc,
};

const DB_HASH_LEN: usize = 32;
/// Hash type that this backend uses for the database.
Expand Down Expand Up @@ -108,8 +112,13 @@ pub struct MetaDb<Block: BlockT> {

impl<Block: BlockT> MetaDb<Block> {
pub fn current_syncing_tips(&self) -> Result<Vec<Block::Hash>, String> {
match self.db.get(crate::columns::META, &crate::static_keys::CURRENT_SYNCING_TIPS) {
Some(raw) => Ok(Vec::<Block::Hash>::decode(&mut &raw[..]).map_err(|e| format!("{:?}", e))?),
match self.db.get(
crate::columns::META,
&crate::static_keys::CURRENT_SYNCING_TIPS,
) {
Some(raw) => {
Ok(Vec::<Block::Hash>::decode(&mut &raw[..]).map_err(|e| format!("{:?}", e))?)
}
None => Ok(Vec::new()),
}
}
Expand All @@ -123,7 +132,9 @@ impl<Block: BlockT> MetaDb<Block> {
&tips.encode(),
);

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

Ok(())
}
Expand All @@ -149,22 +160,24 @@ pub struct MappingDb<Block: BlockT> {
}

impl<Block: BlockT> MappingDb<Block> {
pub fn is_synced(
&self,
block_hash: &Block::Hash,
) -> Result<bool, String> {
match self.db.get(crate::columns::SYNCED_MAPPING, &block_hash.encode()) {
pub fn is_synced(&self, block_hash: &Block::Hash) -> Result<bool, String> {
match self
.db
.get(crate::columns::SYNCED_MAPPING, &block_hash.encode())
{
Some(raw) => Ok(bool::decode(&mut &raw[..]).map_err(|e| format!("{:?}", e))?),
None => Ok(false),
}
}

pub fn block_hash(
&self,
ethereum_block_hash: &H256,
) -> Result<Option<Block::Hash>, String> {
match self.db.get(crate::columns::BLOCK_MAPPING, &ethereum_block_hash.encode()) {
Some(raw) => Ok(Some(Block::Hash::decode(&mut &raw[..]).map_err(|e| format!("{:?}", e))?)),
pub fn block_hash(&self, ethereum_block_hash: &H256) -> Result<Option<Block::Hash>, String> {
match self
.db
.get(crate::columns::BLOCK_MAPPING, &ethereum_block_hash.encode())
{
Some(raw) => Ok(Some(
Block::Hash::decode(&mut &raw[..]).map_err(|e| format!("{:?}", e))?,
)),
None => Ok(None),
}
}
Expand All @@ -173,16 +186,17 @@ impl<Block: BlockT> MappingDb<Block> {
&self,
ethereum_transaction_hash: &H256,
) -> Result<Vec<TransactionMetadata<Block>>, String> {
match self.db.get(crate::columns::TRANSACTION_MAPPING, &ethereum_transaction_hash.encode()) {
Some(raw) => Ok(Vec::<TransactionMetadata<Block>>::decode(&mut &raw[..]).map_err(|e| format!("{:?}", e))?),
match self.db.get(
crate::columns::TRANSACTION_MAPPING,
&ethereum_transaction_hash.encode(),
) {
Some(raw) => Ok(Vec::<TransactionMetadata<Block>>::decode(&mut &raw[..])
.map_err(|e| format!("{:?}", e))?),
None => Ok(Vec::new()),
}
}

pub fn write_none(
&self,
block_hash: Block::Hash
) -> Result<(), String> {
pub fn write_none(&self, block_hash: Block::Hash) -> Result<(), String> {
let _lock = self.write_lock.lock();

let mut transaction = sp_database::Transaction::new();
Expand All @@ -193,26 +207,29 @@ impl<Block: BlockT> MappingDb<Block> {
&true.encode(),
);

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

Ok(())
}

pub fn write_hashes(
&self,
commitment: MappingCommitment<Block>,
) -> Result<(), String> {
pub fn write_hashes(&self, commitment: MappingCommitment<Block>) -> Result<(), String> {
let _lock = self.write_lock.lock();

let mut transaction = sp_database::Transaction::new();

transaction.set(
crate::columns::BLOCK_MAPPING,
&commitment.ethereum_block_hash.encode(),
&commitment.block_hash.encode()
&commitment.block_hash.encode(),
);

for (i, ethereum_transaction_hash) in commitment.ethereum_transaction_hashes.into_iter().enumerate() {
for (i, ethereum_transaction_hash) in commitment
.ethereum_transaction_hashes
.into_iter()
.enumerate()
{
let mut metadata = self.transaction_metadata(&ethereum_transaction_hash)?;
metadata.push(TransactionMetadata::<Block> {
block_hash: commitment.block_hash,
Expand All @@ -232,7 +249,9 @@ impl<Block: BlockT> MappingDb<Block> {
&true.encode(),
);

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

Ok(())
}
Expand Down
14 changes: 8 additions & 6 deletions client/db/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,18 @@
// 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 crate::{Database, DatabaseSettings, DatabaseSettingsSrc, DbHash};
use std::sync::Arc;
use crate::{Database, DbHash, DatabaseSettings, DatabaseSettingsSrc};

pub fn open_database(
config: &DatabaseSettings,
) -> Result<Arc<dyn Database<DbHash>>, String> {
pub fn open_database(config: &DatabaseSettings) -> Result<Arc<dyn Database<DbHash>>, String> {
let db: Arc<dyn Database<DbHash>> = match &config.source {
DatabaseSettingsSrc::RocksDb { path, cache_size: _ } => {
DatabaseSettingsSrc::RocksDb {
path,
cache_size: _,
} => {
let db_config = kvdb_rocksdb::DatabaseConfig::with_columns(crate::columns::NUM_COLUMNS);
let path = path.to_str()
let path = path
.to_str()
.ok_or_else(|| "Invalid database path".to_string())?;

let db = kvdb_rocksdb::Database::open(&db_config, &path)
Expand Down
Loading

0 comments on commit d37ded9

Please sign in to comment.