You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Just taking a moment to brush the dust of this sweet sweet EVM event indexer. This PR should address all open security alerts.
PR Type
enhancement, dependencies
Description
Introduced new utility functions and structures in common.rs to handle event descriptors and SQL operations, enhancing code modularity and reusability.
Refactored postgres.rs and sqlite.rs to utilize new utility functions, improving error handling and reducing code duplication.
Updated Cargo.toml to upgrade several dependencies, ensuring compatibility with newer versions and leveraging new features.
Improved documentation by clarifying comments in the indexer module.
Changes walkthrough 📝
Relevant files
Enhancement
common.rs
Introduce event handling utilities and validation functions
src/database/common.rs
Added a new PreparedEvent struct to handle event descriptors and SQL statements.
Implemented check_descriptor function to validate event fields against descriptors.
Added event_exists_with_same_signature function to check for existing events with the same signature.
Introduced push_sql_value function to manage SQL value insertion.
Error Handling The function check_descriptor at lines 23-33 uses a loop to compare event field types with descriptor types and returns an error if they don't match. This approach is correct, but the error message "event field {i} doesn't match event descriptor" does not provide enough context about what specifically is mismatched, which could make debugging more difficult. Consider enhancing the error message to include more details about the expected and actual types.
Possible Bug The function event_exists_with_same_signature at lines 42-57 checks if an event with the same name has a different signature in the database and returns an error if so. However, the error message uses {name} which might not be replaced correctly because it's inside a string literal without formatting applied. This could lead to confusing error messages in the logs.
Replace the unwrap() calls with proper error handling to avoid potential runtime panics. Using ? operator will propagate the error, allowing for cleaner error handling in the calling functions.
Why: The suggestion correctly replaces unwrap() with ?, which is a safer approach to handle potential conversion errors and prevent runtime panics. This change aligns with the existing PR code diff, which already implements this improvement.
9
Replace unwrap() with ? for error propagation in type conversions
Replace the try_into().unwrap() calls with ? to handle potential conversion errors gracefully.
Why: The suggestion correctly replaces unwrap() with ?, improving error handling by propagating errors instead of causing potential panics. This aligns with the changes made in the PR code diff.
9
Enhancement
Improve error message for type mismatch in event fields
Implement a more detailed error message that includes the expected and actual types when the event field does not match the descriptor, to aid in debugging.
if value.kind() != kind.field.kind {
- return Err(anyhow!("event field {i} doesn't match event descriptor"));+ return Err(anyhow!("event field {i} expected type {:?} but got type {:?}", kind.field.kind, value.kind()));
}
Suggestion importance[1-10]: 8
Why: The suggestion enhances the error message by including expected and actual types, which aids in debugging. This is a valuable improvement for developers, making error messages more informative.
8
Add error handling for the write! macro
Consider handling the potential error from the write! macro to ensure that any issues with writing to the string are properly managed.
-write!(&mut sql, "CREATE TABLE IF NOT EXISTS {} (", table.name)?;-write!(&mut sql, "{FIXED_COLUMNS}, ")?;-write!(&mut sql, "{ARRAY_COLUMN}, ")?;-write!(&mut sql, "{}", column.name)?;-write!(&mut sql, " {type_} NOT NULL, ")?;-write!(&mut sql, "PRIMARY KEY({primary_key}));")?;+write!(&mut sql, "CREATE TABLE IF NOT EXISTS {} (", table.name).map_err(|e| anyhow!("Failed to write SQL: {}", e))?;+write!(&mut sql, "{FIXED_COLUMNS}, ").map_err(|e| anyhow!("Failed to write SQL: {}", e))?;+write!(&mut sql, "{ARRAY_COLUMN}, ").map_err(|e| anyhow!("Failed to write SQL: {}", e))?;+write!(&mut sql, "{}", column.name).map_err(|e| anyhow!("Failed to write SQL: {}", e))?;+write!(&mut sql, " {type_} NOT NULL, ").map_err(|e| anyhow!("Failed to write SQL: {}", e))?;+write!(&mut sql, "PRIMARY KEY({primary_key}));").map_err(|e| anyhow!("Failed to write SQL: {}", e))?;
Suggestion importance[1-10]: 7
Why: The suggestion adds error handling to the write! macro, which is a good practice to ensure robustness. However, this is a minor enhancement and not critical, as the existing code already uses the ? operator for error propagation.
7
Make the chrono dependency optional
Consider specifying the optional attribute for the chrono dependency to allow users to opt-in or out based on their needs. This can be particularly useful for libraries to reduce the number of mandatory dependencies.
-chrono = { version = "0.4.40", default-features = false, features = ["std"] }+chrono = { version = "0.4.40", default-features = false, features = ["std"], optional = true }
Suggestion importance[1-10]: 7
Why: Making the chrono dependency optional can improve flexibility for users who may not need it, reducing unnecessary dependencies. However, this change is not critical and depends on the specific use case of the library.
7
Best practice
Lock the serde dependency to a specific minor version
Consider locking the serde dependency to a more specific minor version to avoid potential breaking changes in future updates, ensuring more predictable behavior and compatibility.
-serde = { version = "1", features = ["derive"] }+serde = { version = "1.0.130", features = ["derive"] }
Suggestion importance[1-10]: 8
Why: Locking the serde dependency to a specific minor version can prevent unexpected breaking changes, ensuring stability and predictability, which is a good practice for maintaining compatibility.
8
Maintainability
Specify a version range for hex-literal to allow minor updates
For the hex-literal dependency, consider specifying a more specific version range instead of pinning to a single version to allow for minor updates and patches, which can include important fixes and improvements.
Why: Allowing minor updates for hex-literal can provide important fixes and improvements without risking major breaking changes, enhancing maintainability.
7
Possible issue
Update the time feature version used in tokio-postgres
Update the tokio-postgres dependency to use the latest compatible version of time instead of the older 0.3 version, which may not be compatible with other dependencies or may lack recent improvements and bug fixes.
-tokio-postgres = { version = "0.7.13", features = ["with-time-0_3"] }+tokio-postgres = { version = "0.7.13", features = ["with-time"] }
Suggestion importance[1-10]: 6
Why: Updating the time feature to a more recent version could improve compatibility and include recent improvements, but it may also introduce compatibility issues with existing code if not tested properly.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
User description
Just taking a moment to brush the dust of this sweet sweet EVM event indexer. This PR should address all open security alerts.
PR Type
enhancement, dependencies
Description
common.rsto handle event descriptors and SQL operations, enhancing code modularity and reusability.postgres.rsandsqlite.rsto utilize new utility functions, improving error handling and reducing code duplication.Cargo.tomlto upgrade several dependencies, ensuring compatibility with newer versions and leveraging new features.Changes walkthrough 📝
common.rs
Introduce event handling utilities and validation functionssrc/database/common.rs
PreparedEventstruct to handle event descriptors and SQLstatements.
check_descriptorfunction to validate event fields againstdescriptors.
event_exists_with_same_signaturefunction to check for existingevents with the same signature.
push_sql_valuefunction to manage SQL value insertion.mod.rs
Update module imports and improve EventBlock implementationsrc/database/mod.rs
commonmodule import.EventBlockimplementation to use a more generic lifetime.postgres.rs
Refactor Postgres event handling with improved error managementsrc/database/postgres.rs
commonfor event validation and SQLvalue handling.
PreparedEventto use generic types for SQL statements.unwrapcalls with?for error handling.sqlite.rs
Refactor SQLite event handling with improved error managementsrc/database/sqlite.rs
commonfor event validation and SQLvalue handling.
PreparedEventto use generic types for SQL statements.unwrapcalls with?for error handling.mod.rs
Improve comment clarity in indexer modulesrc/indexer/mod.rs
Cargo.toml
Upgrade dependencies for improved compatibility and featuresCargo.toml
rusqlitedependency to version0.34.0.tokioandtokio-postgresto newer versions.chronoandhex-literalto latest versions.