Skip to content

Commit

Permalink
Merge pull request #51 from chaindexing/re-introduce--clippy
Browse files Browse the repository at this point in the history
Re-introduce Clippy
  • Loading branch information
Jurshsmith authored Dec 20, 2023
2 parents 87a5081 + bf849a0 commit 549dfb8
Show file tree
Hide file tree
Showing 20 changed files with 113 additions and 118 deletions.
19 changes: 10 additions & 9 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,16 @@ jobs:
- name: Run Tests
run: cargo test

# clippy:
# name: Clippy
# runs-on: ubuntu-latest
# if: github.event_name != 'pull_request'
# timeout-minutes: 45
# steps:
# - uses: actions/checkout@v3
# - uses: dtolnay/rust-toolchain@clippy
# - run: cargo clippy --tests -- -Dclippy::all
clippy:
name: Clippy
runs-on: ubuntu-latest
if: github.event_name != 'pull_request'
timeout-minutes: 45
steps:
- uses: actions/checkout@v3
- uses: dtolnay/rust-toolchain@clippy
# - run: cargo clippy --tests -- -Dclippy::all
- run: cargo clippy

fmt:
name: fmt
Expand Down
4 changes: 2 additions & 2 deletions chaindexing-tests/src/factory/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ pub fn transfer_event_with_contract(contract: Contract) -> Event {
let contract_address = BAYC_CONTRACT_ADDRESS;
let transfer_log = transfer_log(contract_address);
let blocks_by_number = HashMap::from([(
transfer_log.block_number.clone().unwrap(),
transfer_log.block_number.unwrap(),
Block {
..Default::default()
},
)]);
Events::new(&vec![transfer_log], &vec![contract], &blocks_by_number)
Events::get(&vec![transfer_log], &vec![contract], &blocks_by_number)
.first()
.cloned()
.unwrap()
Expand Down
4 changes: 2 additions & 2 deletions chaindexing-tests/src/factory/json_rpcs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub fn empty_json_rpc() -> impl EventsIngesterJsonRpc {
}
}

return JsonRpc;
JsonRpc
}

use ethers::types::{Bytes, H160, H256};
Expand Down Expand Up @@ -65,7 +65,7 @@ fn h256(str: &str) -> H256 {
#[macro_export]
macro_rules! json_rpc_with_logs {
($contract_address:expr) => {{
use crate::json_rpc_with_logs;
use $crate::json_rpc_with_logs;

json_rpc_with_logs!($contract_address, 17774490)
}};
Expand Down
2 changes: 1 addition & 1 deletion chaindexing-tests/src/test_runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ where
TestFn: Fn(ChaindexingRepoConn<'a>) -> Fut,
Fut: Future<Output = ()>,
{
let mut conn = ChaindexingRepo::get_conn(&pool).await;
let mut conn = ChaindexingRepo::get_conn(pool).await;

if should_setup_test_db() {
db::setup();
Expand Down
2 changes: 1 addition & 1 deletion chaindexing-tests/src/tests/contract_states.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,5 +114,5 @@ impl ContractStateMigrations for NftStateMigrations {
pub async fn setup() {
let bayc_contract = bayc_contract().add_state_migrations(NftStateMigrations);
let raw_query_client = test_runner::new_repo().get_raw_query_client().await;
Chaindexing::run_migrations_for_contract_states(&raw_query_client, &vec![bayc_contract]).await;
Chaindexing::run_migrations_for_contract_states(&raw_query_client, &[bayc_contract]).await;
}
8 changes: 4 additions & 4 deletions chaindexing/src/chain_reorg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,16 @@ impl UnsavedReorgedBlock {
pub struct ReorgedBlocks;

impl ReorgedBlocks {
pub fn only_earliest_per_chain(reorged_blocks: &Vec<ReorgedBlock>) -> Vec<ReorgedBlock> {
pub fn only_earliest_per_chain(reorged_blocks: &[ReorgedBlock]) -> Vec<ReorgedBlock> {
reorged_blocks
.into_iter()
.iter()
.fold(
HashMap::<i32, ReorgedBlock>::new(),
|mut reorged_blocks_by_chain, reorged_block| {
let ReorgedBlock { chain_id, .. } = reorged_block;

if let Some(earliest_reorged_block) = reorged_blocks_by_chain.get(chain_id) {
if reorged_block.block_number < (*earliest_reorged_block).block_number {
if reorged_block.block_number < earliest_reorged_block.block_number {
reorged_blocks_by_chain.insert(*chain_id, reorged_block.clone());
}
} else {
Expand All @@ -88,7 +88,7 @@ impl ReorgedBlocks {
.collect()
}

pub fn get_ids(reorged_blocks: &Vec<ReorgedBlock>) -> Vec<i32> {
pub fn get_ids(reorged_blocks: &[ReorgedBlock]) -> Vec<i32> {
reorged_blocks.iter().map(|r| r.id).collect()
}
}
14 changes: 7 additions & 7 deletions chaindexing/src/contract_states.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ pub struct ContractStates;

impl ContractStates {
pub async fn backtrack_states<'a>(
state_migrations: &Vec<Arc<dyn ContractStateMigrations>>,
state_migrations: &[Arc<dyn ContractStateMigrations>],
chain_id: i32,
block_number: i64,
client: &ChaindexingRepoRawQueryTxnClient<'a>,
) {
let table_names = Self::get_all_table_names(&state_migrations);
let table_names = Self::get_all_table_names(state_migrations);

for table_name in table_names {
let state_versions =
Expand All @@ -38,7 +38,7 @@ impl ContractStates {
}

pub fn get_all_table_names(
state_migrations: &Vec<Arc<dyn ContractStateMigrations>>,
state_migrations: &[Arc<dyn ContractStateMigrations>],
) -> Vec<String> {
state_migrations
.iter()
Expand Down Expand Up @@ -92,7 +92,7 @@ pub trait ContractState:
let client = context.get_raw_query_client();

let table_name = Self::table_name();
let state_view = self.to_complete_view(&table_name, &client).await;
let state_view = self.to_complete_view(table_name, client).await;

let latest_state_version =
StateVersion::update(&state_view, &updates, table_name, event, client).await;
Expand All @@ -104,7 +104,7 @@ pub trait ContractState:
let client = context.get_raw_query_client();

let table_name = Self::table_name();
let state_view = self.to_complete_view(&table_name, &client).await;
let state_view = self.to_complete_view(table_name, client).await;

let latest_state_version =
StateVersion::delete(&state_view, table_name, event, client).await;
Expand Down Expand Up @@ -137,7 +137,7 @@ pub trait ContractState:
}

pub fn to_columns_and_values(state: &HashMap<String, String>) -> (Vec<String>, Vec<String>) {
state.into_iter().fold(
state.iter().fold(
(vec![], vec![]),
|(mut columns, mut values), (column, value)| {
columns.push(column.to_string());
Expand Down Expand Up @@ -166,7 +166,7 @@ pub fn serde_map_to_string_map(
if value.is_object() {
map.insert(key.to_owned(), value.to_string());
} else {
map.insert(key.to_owned(), value.to_string().replace("\"", ""));
map.insert(key.to_owned(), value.to_string().replace('\"', ""));
}
}

Expand Down
36 changes: 17 additions & 19 deletions chaindexing/src/contract_states/migrations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub trait ContractStateMigrations: Send + Sync {
fn get_table_names(&self) -> Vec<String> {
self.migrations().iter().fold(vec![], |mut table_names, migration| {
if migration.starts_with("CREATE TABLE IF NOT EXISTS") {
let table_name = extract_table_name(&migration);
let table_name = extract_table_name(migration);
table_names.push(table_name)
}

Expand All @@ -27,16 +27,14 @@ pub trait ContractStateMigrations: Send + Sync {

if user_migration.starts_with("CREATE TABLE IF NOT EXISTS") {
let create_state_views_table_migration =
append_migration(&user_migration, &get_remaining_state_views_migration());
append_migration(user_migration, &get_remaining_state_views_migration());
let create_state_views_table_migration =
DefaultMigration::remove_repeating_occurrences(
&create_state_views_table_migration,
);

let create_state_versions_table_migration = append_migration(
&user_migration,
&get_remaining_state_versions_migration(),
);
let create_state_versions_table_migration =
append_migration(user_migration, &get_remaining_state_versions_migration());
let create_state_versions_table_migration =
set_state_versions_table_name(&create_state_versions_table_migration);
let create_state_versions_table_migration =
Expand Down Expand Up @@ -77,7 +75,7 @@ pub trait ContractStateMigrations: Send + Sync {
.iter()
.filter(|m| m.starts_with("CREATE TABLE IF NOT EXISTS"))
.map(|create_migration| {
let table_name = extract_table_name(&create_migration);
let table_name = extract_table_name(create_migration);

format!("DROP TABLE IF EXISTS {table_name}")
})
Expand All @@ -88,7 +86,7 @@ pub trait ContractStateMigrations: Send + Sync {
fn extract_table_name(migration: &str) -> String {
migration
.replace("CREATE TABLE IF NOT EXISTS", "")
.split("(")
.split('(')
.collect::<Vec<&str>>()
.first()
.unwrap()
Expand All @@ -98,12 +96,12 @@ fn extract_table_name(migration: &str) -> String {

fn extract_table_fields(migration: &str, remove_json_fields: bool) -> Vec<String> {
migration
.replace(")", "")
.split("(")
.replace(')', "")
.split('(')
.collect::<Vec<&str>>()
.last()
.unwrap()
.split(",")
.split(',')
.filter(|field| remove_json_fields && !(field.contains("JSON") || field.contains("JSONB")))
.map(|field| {
field
Expand Down Expand Up @@ -140,8 +138,8 @@ fn validate_migration(migration: &str) {
}

fn append_migration(migration: &str, migration_to_append: &str) -> String {
let mut migration = migration.replace("\n", "");
migration.push_str(",");
let mut migration = migration.replace('\n', "");
migration.push(',');
migration.push_str(migration_to_append);
migration
.split_ascii_whitespace()
Expand Down Expand Up @@ -176,12 +174,12 @@ fn set_state_versions_table_name(migration: &str) -> String {
}

fn extract_migration_columns(migration: &str) -> Vec<String> {
let migration_tokens = migration.split("(");
let migration_tokens = migration.split('(');
let migration = migration_tokens.last().unwrap();
let mut migration_tokens = migration.split(")");
let mut migration_tokens = migration.split(')');
let migration = migration_tokens.next().unwrap();

migration.split(",").fold(vec![], |mut migration_columns, migration_column| {
migration.split(',').fold(vec![], |mut migration_columns, migration_column| {
migration_columns.push(migration_column.to_string());
migration_columns
})
Expand Down Expand Up @@ -252,14 +250,14 @@ impl DefaultMigration {
let mut repeating_state_fields_count = repeating_state_fields.iter().fold(
HashMap::new(),
|mut repeating_field_count, field| {
repeating_field_count.insert(*field, 0 as u8);
repeating_field_count.insert(*field, 0_u8);

repeating_field_count
},
);

migration
.split(",")
.split(',')
.fold(vec![], |mut unique_migration_tokens, migration_token| {
match repeating_state_fields.iter().find(|field| migration_token.contains(**field))
{
Expand Down Expand Up @@ -307,7 +305,7 @@ mod contract_state_migrations_get_migration_test {
contract_state.migrations().first().unwrap()
);

assert_default_migration(&create_state_migration);
assert_default_migration(create_state_migration);
}

#[test]
Expand Down
14 changes: 7 additions & 7 deletions chaindexing/src/contract_states/state_versions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::{

use super::{serde_map_to_string_map, to_columns_and_values};

pub const STATE_VERSIONS_TABLE_PREFIX: &'static str = "chaindexing_state_versions_for_";
pub const STATE_VERSIONS_TABLE_PREFIX: &str = "chaindexing_state_versions_for_";
pub const STATE_VERSIONS_UNIQUE_FIELDS: [&str; 2] =
["state_version_id", "state_version_is_deleted"];

Expand All @@ -25,7 +25,7 @@ impl StateVersions {
"SELECT * FROM {table_name}
WHERE chain_id = {chain_id}
AND block_number >= {from_block_number}",
table_name = StateVersion::table_name(&state_table_name),
table_name = StateVersion::table_name(state_table_name),
);

ChaindexingRepo::load_data_list_from_raw_query_with_txn_client::<
Expand All @@ -37,15 +37,15 @@ impl StateVersions {
.collect()
}

pub fn get_ids(state_versions: &Vec<HashMap<String, String>>) -> Vec<String> {
pub fn get_ids(state_versions: &[HashMap<String, String>]) -> Vec<String> {
state_versions
.iter()
.map(|state_version| state_version.get("state_version_id").unwrap())
.cloned()
.collect()
}

pub fn get_group_ids(state_versions: &Vec<HashMap<String, String>>) -> Vec<String> {
pub fn get_group_ids(state_versions: &[HashMap<String, String>]) -> Vec<String> {
state_versions
.iter()
.map(|state_version| state_version.get("state_version_group_id").unwrap())
Expand All @@ -54,7 +54,7 @@ impl StateVersions {
}

pub async fn delete_by_ids<'a>(
ids: &Vec<String>,
ids: &[String],
state_table_name: &str,
client: &ChaindexingRepoRawQueryTxnClient<'a>,
) {
Expand All @@ -69,15 +69,15 @@ impl StateVersions {
}

pub async fn get_latest<'a>(
group_ids: &Vec<String>,
group_ids: &[String],
state_table_name: &str,
client: &ChaindexingRepoRawQueryTxnClient<'a>,
) -> Vec<HashMap<String, String>> {
let query = format!(
"SELECT DISTINCT ON (state_version_group_id) * FROM {table_name}
WHERE state_version_group_id IN ({group_ids})
ORDER BY state_version_group_id, block_number, log_index DESC",
table_name = StateVersion::table_name(&state_table_name),
table_name = StateVersion::table_name(state_table_name),
group_ids = group_ids.iter().map(|id| format!("'{id}'")).collect::<Vec<_>>().join(",")
);

Expand Down
10 changes: 5 additions & 5 deletions chaindexing/src/contract_states/state_views.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ pub struct StateViews;

impl StateViews {
pub async fn refresh<'a>(
state_version_group_ids: &Vec<String>,
state_version_group_ids: &[String],
table_name: &str,
client: &ChaindexingRepoRawQueryTxnClient<'a>,
) {
let latest_state_versions =
StateVersions::get_latest(&state_version_group_ids, table_name, client).await;
StateVersions::get_latest(state_version_group_ids, table_name, client).await;

for latest_state_version in latest_state_versions {
StateView::refresh(&latest_state_version, table_name, client).await
Expand Down Expand Up @@ -53,10 +53,10 @@ impl StateView {
) {
let state_version_group_id = StateVersion::get_group_id(latest_state_version);

if StateVersion::was_deleted(&latest_state_version) {
if StateVersion::was_deleted(latest_state_version) {
Self::delete(&state_version_group_id, table_name, client).await;
} else {
let new_state_view = Self::from_latest_state_version(&latest_state_version);
let new_state_view = Self::from_latest_state_version(latest_state_version);

Self::delete(&state_version_group_id, table_name, client).await;
Self::create(&new_state_view, table_name, client).await;
Expand Down Expand Up @@ -90,7 +90,7 @@ impl StateView {
table_name: &str,
client: &ChaindexingRepoRawQueryTxnClient<'a>,
) {
let (columns, values) = to_columns_and_values(&new_state_view);
let (columns, values) = to_columns_and_values(new_state_view);
let query = format!(
"INSERT INTO {table_name} ({columns}) VALUES ({values})",
columns = columns.join(","),
Expand Down
Loading

0 comments on commit 549dfb8

Please sign in to comment.