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 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
Missing Logging The save_block function spawns multiple Redis commands but lacks logging for these operations. Ensure that each Redis command (e.g., mset, zadd) has appropriate logging for better traceability.
Use spawn_blocking_named The save_block function performs potentially blocking Redis operations within an async context. Consider using tokio::task::spawn_blocking_named to avoid blocking the Tokio runtime.
Use expect instead of unwrap The code should use expect instead of unwrap to provide context in case of a panic. This is particularly important in the save_block function when calling self.conn().
Instrumentation Missing Fields The read_account and read_slot functions are instrumented with tracing::instrument, but they do not record any identifiers as span fields. Consider adding relevant fields for better traceability.
Reduce memory usage by cloning block_json once and using references
Instead of cloning block_json multiple times, consider cloning it once and using references to the cloned value. This will reduce memory usage and improve performance.
for changes in block.compact_account_changes() {
// account
let mut account = Account {
address: changes.address,
..Account::default()
};
if let Some(nonce) = changes.nonce.take() {
account.nonce = nonce;
}
if let Some(bytecode) = changes.bytecode.take() {
account.bytecode = bytecode;
}
let account_value = to_json_string(&account);
mset_values.push((key_account(&account.address), account_value.clone()));
zadd_values.push((key_account_history(&account.address), account_value, block.number().as_u64()));
// slot
- for slot in changes.slots.into_values() {- if let Some(slot) = slot.take() {- let slot_value = to_json_string(&slot);- mset_values.push((key_slot(&account.address, &slot.index), slot_value.clone()));- zadd_values.push((key_slot_history(&account.address, &slot.index), slot_value, block.number().as_u64()));- }+ for slot in changes.slots.into_values().flatten() {+ let slot_value = to_json_string(&slot);+ mset_values.push((key_slot(&account.address, &slot.index), slot_value.clone()));+ zadd_values.push((key_slot_history(&account.address, &slot.index), slot_value, block.number().as_u64()));
}
}
Suggestion importance[1-10]: 7
Why: Combining the loops improves readability and performance by reducing the number of iterations. However, the improvement is minor as the loops are already nested.
7
Best practice
Add a check to ensure block.transactions is not empty before iterating
Add a check to ensure that block.transactions is not empty before iterating over it. This will avoid unnecessary iterations and potential errors.
-for tx in &block.transactions {- let tx_key = key_tx(&tx.input.hash);- let tx_value = to_json_string(&tx);- mset_values.push((tx_key, tx_value));+if !block.transactions.is_empty() {+ for tx in &block.transactions {+ let tx_key = key_tx(&tx.input.hash);+ let tx_value = to_json_string(&tx);+ mset_values.push((tx_key, tx_value));+ }
}
Suggestion importance[1-10]: 6
Why: Adding a check to ensure block.transactions is not empty before iterating is a good practice to avoid unnecessary iterations, though it is not critical since iterating over an empty collection is not harmful.
6
Maintainability
Use a constant for the string "ZRANGE" to avoid potential typos
Use a constant for the string "ZRANGE" to avoid potential typos and make future modifications easier.
Why: Using a constant for the string "ZRANGE" improves maintainability by reducing the risk of typos and making future modifications easier. However, the impact is relatively small.
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.
PR Type
Enhancement
Description
key_account_history
andkey_slot_history
for generating keys to access historical data.save_block
function to usezadd
for storing historical data in Redis.read_account
andread_slot
functions to handle historical data retrieval based on theStoragePointInTime
parameter.Changes walkthrough 📝
redis_permanent.rs
Add support for historical values in Redis storage
src/eth/storage/redis/redis_permanent.rs
and slots.
key_account_history
andkey_slot_history
forgenerating history keys.
save_block
to usezadd
for storing historical data.read_account
andread_slot
to handle historical dataretrieval.