Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: introduce cache layer for fee denom and decimals #110

Merged
merged 5 commits into from
Nov 13, 2024
Merged

Conversation

beer-1
Copy link
Collaborator

@beer-1 beer-1 commented Nov 13, 2024

Description

currently fee decimals should be fetched by calling EVM static call, so it is degrading performance on json-rpc. to avoid this performance issue, we introduce fee cache layer by fetch fee denominated and decimals in a go-rountine periodically.


Author Checklist

All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.

I have...

  • included the correct type prefix in the PR title, you can find examples of the prefixes below:
  • confirmed ! in the type prefix if API or client breaking change
  • targeted the correct branch
  • provided a link to the relevant issue or specification
  • reviewed "Files changed" and left comments if necessary
  • included the necessary unit and integration tests
  • updated the relevant documentation or specification, including comments for documenting Go code
  • confirmed all CI checks have passed

Reviewers Checklist

All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.

I have...

  • confirmed the correct type prefix in the PR title
  • confirmed all author checklist items have been addressed
  • reviewed state machine logic, API design and naming, documentation is accurate, tests and test coverage

Summary by CodeRabbit

Release Notes

  • New Features

    • Introduced a new method GetBlockReceipts for retrieving block receipts in the Ethereum API.
    • Added fee management capabilities to the JSON-RPC backend, including new fields for fee denomination and decimals.
  • Improvements

    • Enhanced error handling in various methods to ensure proper readiness checks for the JSON-RPC service.
    • Streamlined context management across multiple API services for better lifecycle control.
    • Improved the EstimateGas method for more efficient fee denomination retrieval.
    • Updated balance retrieval logic to utilize consolidated fee information for enhanced accuracy.
  • Bug Fixes

    • Resolved issues related to fee retrieval and gas price calculations.
  • Documentation

    • Updated function signatures to reflect new context parameters across various API constructors.

@beer-1 beer-1 requested a review from a team as a code owner November 13, 2024 09:23
Copy link

coderabbitai bot commented Nov 13, 2024

Caution

Review failed

The pull request is closed.

Walkthrough

The pull request introduces significant modifications to the JSONRPCBackend structure and its methods across multiple files. Key changes include the addition of feeDenom and feeDecimals fields, an updated constructor to accept a context parameter, and the implementation of a new feeFetcher method for asynchronous fee management. Additionally, several API constructors are updated to utilize context parameters, enhancing context management throughout the codebase. These changes streamline error handling and improve the overall functionality of the JSON-RPC backend.

Changes

File Change Summary
jsonrpc/backend/backend.go - Added fields: feeDenom string, feeDecimals uint8 to JSONRPCBackend struct.
- Updated constructor NewJSONRPCBackend to accept context.Context.
- Introduced method feeFetcher() for periodic fee fetching in a goroutine.
jsonrpc/backend/eth.go - Updated GetBalance method to retrieve feeDenom and feeDecimals using b.feeInfo().
jsonrpc/backend/gas.go - Modified EstimateGas and GasPrice methods to utilize feeInfo() for fee denomination and decimals.
- Removed methods: feeDenom(), feeDenomWithDecimals().
jsonrpc/jsonrpc.go - Updated StartJSONRPC function to pass ctx instead of app to various API service constructors.
jsonrpc/namespaces/cosmos/api.go - Updated NewCosmosAPI to accept ctx context.Context instead of context.TODO().
jsonrpc/namespaces/eth/api.go - Updated NewEthAPI to accept ctx context.Context.
- Added new method GetBlockReceipts.
jsonrpc/namespaces/eth/filters/api.go - Updated NewFilterAPI to accept ctx context.Context.
- Enhanced methods to handle ctx.Done() for graceful termination.
jsonrpc/namespaces/net/api.go - Updated NewNetAPI to accept ctx context.Context instead of context.TODO().
jsonrpc/namespaces/txpool/api.go - Updated NewTxPoolAPI to accept ctx context.Context instead of context.TODO().
jsonrpc/namespaces/web3/api.go - Updated NewWeb3API to accept ctx context.Context instead of context.TODO().

Possibly related PRs

  • fix: audit #59: The changes in the main PR regarding the JSONRPCBackend struct and its methods may relate to transaction handling, which is also a focus in the marshalReceipt function modifications in this PR.
  • feat: update slinky => connect/v2 #63: The updates in the main PR that introduce new fields for fee management in the JSONRPCBackend struct could connect with the changes in the jsonrpc/backend/gas.go file, where the EstimateGas method was modified to utilize the new fee information.
  • fix: support accesslist type tx and handle rpc transaction #73: The enhancements to the JSONRPCBackend methods for transaction handling and error management could be relevant to the updates in handling access list transactions in this PR.
  • fix: tx pool endpoint to return queued txs and apply mempool wrapper #77: The modifications to the transaction pool and how transactions are queued in the JSONRPCBackend may connect with the changes made to ensure the transaction pool endpoint returns queued transactions.
  • fix: async filter & subscription system #82: The improvements in the asynchronous filtering and subscription system may relate to the changes in the JSONRPCBackend that enhance its functionality and error handling.
  • fix: use snapshot to support revert #90: The changes in the Keeper and its context management may connect with the updates in the JSONRPCBackend that involve state management and error handling.
  • fix: handle upgrade empty params #101: The changes in the main PR that enhance the JSONRPCBackend with fee management features could connect with the updates in the app/upgrade.go file, where the upgrade process was modified to handle ERC20 contract deployments, potentially impacting fee management during upgrades.
  • feat: introduce cache layer for fee denom and decimals #110: The enhancements in the main PR that introduce a caching layer for fee denomination and decimals directly relate to the changes in the jsonrpc/backend/backend.go file, where fee management was significantly modified.

Suggested reviewers

  • djm07073

🐰 "In the code where the rabbits play,
New fields and methods come to stay.
Fetching fees with a hop and a skip,
Context now flows on every trip.
JSON-RPC, now bright and spry,
With every change, we reach for the sky!" 🐇


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 5

🧹 Outside diff range and nitpick comments (5)
jsonrpc/namespaces/cosmos/api.go (1)

20-20: Good improvement in context propagation!

The addition of the context parameter aligns with Go's best practices for context propagation and will enable proper lifecycle management for the cache layer being introduced.

Ensure that this context is properly used for managing the lifecycle of any background operations, especially the periodic fee information fetching mentioned in the PR objectives.

jsonrpc/backend/gas.go (1)

42-44: Consider adding a retry mechanism for readiness check.

While the readiness check is good, consider adding a retry mechanism with timeout for cases where the fee cache is temporarily unavailable. This would improve resilience and user experience.

 	// jsonrpc is not ready for querying
 	if b.feeDenom == "" {
+		// Retry a few times with backoff before giving up
+		if err := b.waitForFeeDenom(b.ctx); err != nil {
 			return hexutil.Uint64(0), NewInternalError("jsonrpc is not ready")
+		}
 	}
jsonrpc/backend/eth.go (1)

33-33: Consider enhancing error handling

The use of cached values improves performance, but consider adding more context to error scenarios:

  1. When ERC20Keeper().GetBalance fails
  2. When balance conversion fails
 balance, err := b.app.EVMKeeper.ERC20Keeper().GetBalance(queryCtx, sdk.AccAddress(address[:]), b.feeDenom)
 if err != nil {
-    return nil, err
+    return nil, fmt.Errorf("failed to get balance for address %s: %w", address.Hex(), err)
 }

-return (*hexutil.Big)(types.ToEthersUint(b.feeDecimals, balance.BigInt())), nil
+ethBalance := types.ToEthersUint(b.feeDecimals, balance.BigInt())
+if ethBalance == nil {
+    return nil, fmt.Errorf("failed to convert balance to ethers unit with decimals %d", b.feeDecimals)
+}
+return (*hexutil.Big)(ethBalance), nil

Also applies to: 38-38

jsonrpc/jsonrpc.go (1)

Line range hint 67-106: Well-structured architecture for cache implementation.

The changes introduce context propagation in a clean and consistent manner, which is essential for the new caching mechanism. The implementation:

  • Maintains clear separation of concerns
  • Preserves existing error handling and shutdown logic
  • Uses error groups for proper cleanup

Consider documenting the caching behavior and cleanup mechanisms in the package documentation to help future maintainers understand the system's lifecycle.

jsonrpc/namespaces/eth/filters/api.go (1)

166-167: Consider adding proper resource cleanup on shutdown.

While the context cancellation is handled correctly, consider closing channels and cleaning up resources when the event loop terminates to prevent potential resource leaks.

Consider adding cleanup code:

 case <-api.ctx.Done():
+    // Close channels
+    for _, s := range api.subscriptions {
+        close(s.headerChan)
+        close(s.logsChan)
+        close(s.txChan)
+        close(s.hashChan)
+    }
     return
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 63aa650 and f714dff.

📒 Files selected for processing (10)
  • jsonrpc/backend/backend.go (5 hunks)
  • jsonrpc/backend/eth.go (1 hunks)
  • jsonrpc/backend/gas.go (2 hunks)
  • jsonrpc/jsonrpc.go (2 hunks)
  • jsonrpc/namespaces/cosmos/api.go (1 hunks)
  • jsonrpc/namespaces/eth/api.go (1 hunks)
  • jsonrpc/namespaces/eth/filters/api.go (4 hunks)
  • jsonrpc/namespaces/net/api.go (1 hunks)
  • jsonrpc/namespaces/txpool/api.go (1 hunks)
  • jsonrpc/namespaces/web3/api.go (1 hunks)
🔇 Additional comments (19)
jsonrpc/namespaces/cosmos/api.go (2)

22-24: LGTM! Clean struct initialization with proper context assignment.

The initialization is clean and maintains good practices with both context handling and structured logging.


20-24: Verify context usage in the fee cache implementation.

Since this context will be used for the fee cache layer, let's verify its proper usage in the implementation.

jsonrpc/namespaces/net/api.go (2)

27-27: LGTM! Good improvement in context management.

The addition of the context parameter to NewNetAPI is a positive change that allows for better request lifecycle management and aligns with best practices for context propagation.


29-29: Verify context usage in API methods.

While the context is now properly stored in the struct, none of the API methods (Listening, PeerCount, Version) currently utilize it. Consider whether these methods should respect context cancellation.

Consider propagating the context to backend calls if they involve network operations or other cancellable operations. This would improve request cancellation handling.

jsonrpc/namespaces/web3/api.go (2)

30-30: Verify context usage in API methods.

While storing the context is correct, neither ClientVersion() nor Sha3() currently utilize it. If these methods will interact with the new cache layer, they should use the stored context.

Let's check if other API methods in the codebase use context for cache interactions:


28-28: LGTM! Verify consistent context propagation.

The addition of context parameter aligns with Go best practices and supports the PR's caching objectives.

Let's verify that context is consistently propagated across related API constructors:

✅ Verification successful

Context propagation is consistently implemented across API constructors

The context parameter has been consistently added to all API constructors in the jsonrpc namespace:

  • web3.NewWeb3API
  • txpool.NewTxPoolAPI
  • cosmos.NewCosmosAPI
  • net.NewNetAPI
  • eth.filters.NewFilterAPI
  • eth.NewEthAPI

Each constructor properly stores the context in its API struct, following the same pattern.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check for consistent context parameter usage in API constructors
# Expected: All API constructors should accept context.Context

# Search for API constructors
ast-grep --pattern 'func New$_API($$$) $_' 

# Search for any remaining context.TODO() usage that might need updating
rg 'context\.TODO\(\)'

Length of output: 11103


Script:

#!/bin/bash
# Let's try a different approach to verify context propagation

# Search for API constructors and their context usage
rg -A 3 "func New.*API.*\(" 

# Search for context parameter in function signatures
rg -A 3 "context\.Context.*\)"

Length of output: 129164


Script:

#!/bin/bash
# Let's search for API constructors and their context usage more specifically
rg -A 3 "func New.*API.*\(" jsonrpc/namespaces/

Length of output: 1865

jsonrpc/namespaces/txpool/api.go (1)

Line range hint 35-41: LGTM! Good improvement in context management.

The change to accept a context parameter instead of using context.TODO() is a good practice as it:

  • Allows proper context propagation for concurrent operations
  • Enables better control over cancellation and deadlines
  • Aligns with the broader initiative to standardize context handling across the codebase

Let's verify that all callers of this constructor have been updated:

✅ Verification successful

All callers have been updated to pass context parameter

The verification shows that NewTxPoolAPI is called in only two locations:

  • jsonrpc/jsonrpc.go: The caller correctly passes the ctx parameter
  • jsonrpc/namespaces/txpool/api.go: This is the constructor definition itself

The changes are consistent across the codebase with no missing updates.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Find all instances where NewTxPoolAPI is called to ensure they've been updated
# to pass a context parameter

# Search for NewTxPoolAPI calls
rg -A 2 "NewTxPoolAPI\(" --type go

Length of output: 415

jsonrpc/backend/gas.go (1)

52-52: Add validation for feeDecimals.

The cached feeDecimals is used for value conversion without validation. Consider adding bounds checking to prevent potential overflow or invalid conversions.

Let's verify the current validation of feeDecimals:

Also applies to: 59-59

jsonrpc/backend/eth.go (2)

Line range hint 41-186: LGTM! Good isolation of cache-related changes

The changes are well-isolated to fee-related functionality, with no impact on other methods. This demonstrates good separation of concerns.


28-30: Verify cache initialization and thread safety

The guard clause for uninitialized state is good. However, we should ensure proper cache initialization and thread safety.

jsonrpc/jsonrpc.go (2)

76-106: Consistent context propagation across API services.

The changes consistently propagate context to all API services, which is good practice for lifecycle management. However, we should verify proper context handling in each service implementation.

#!/bin/bash
# Description: Verify context usage in API service implementations
# Expected: Each service should properly handle context cancellation

# Check context usage in each namespace
for ns in eth filters net web3 txpool cosmos; do
  echo "Checking $ns namespace..."
  
  # Look for constructor implementation
  ast-grep --pattern 'func New${_}API(ctx context.Context, $$$) {
    $$$
  }'
  
  # Look for goroutines that should use this context
  rg -A 5 "go func" "namespaces/$ns"
done

67-70: Verify context propagation in backend implementation.

The addition of context to backend initialization aligns with the PR's objective of implementing a cache layer. However, we should ensure proper context handling in the backend.

✅ Verification successful

Context is properly propagated and utilized in the backend implementation

The verification shows that the context is correctly used in the backend implementation:

  1. The context is stored in the JSONRPCBackend struct and properly initialized in NewJSONRPCBackend
  2. The context is actively used for controlling the lifecycle of background operations, specifically in the feeFetcher goroutine which monitors the context's cancellation via <-b.ctx.Done()
  3. The context is used for fee denom and decimals caching, which aligns with the PR's objective
  4. The implementation includes proper cache initialization and management using the provided context
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify context usage in backend implementation
# Expected: The context should be used for cache management and cleanup

# Check backend implementation for context usage
ast-grep --pattern 'func NewJSONRPCBackend(ctx context.Context, $$$) {
  $$$
}'

# Look for cache-related goroutines that should use this context
rg -A 5 "go func" backend/

Length of output: 1705


Script:

#!/bin/bash
# Let's try a different approach to find the backend implementation and context usage

# Find the backend package location first
fd -t f "backend.*go$"

# Look for context usage in backend files
rg -l "context.Context" .

# Search for NewJSONRPCBackend implementation
rg -A 10 "func NewJSONRPCBackend" .

Length of output: 2589


Script:

#!/bin/bash
# Let's examine the backend implementation to verify context usage

# Check the full implementation of backend.go
cat jsonrpc/backend/backend.go

# Look for any cache-related code in the backend
rg -A 5 "cache" jsonrpc/backend/

Length of output: 12083

jsonrpc/namespaces/eth/filters/api.go (3)

45-46: LGTM! Context field addition follows best practices.

The addition of the ctx field to FilterAPI struct is well-placed and follows Go conventions for field ordering.


68-72: LGTM! Constructor properly initializes context.

The context parameter addition follows Go's context package conventions by making it the first parameter, and it's correctly stored in the struct.


105-124: LGTM! Excellent concurrency handling with proper documentation.

The implementation demonstrates good practices:

  • Proper mutex usage for thread safety
  • Clear documentation explaining the unsubscribe processing outside the lock
  • Correct context cancellation handling
jsonrpc/namespaces/eth/api.go (3)

110-112: Ensure consistent context usage across API methods.

While the constructor now accepts and stores a context, it's currently only used in GetBlockReceipts. Consider propagating the stored context to other API methods, especially those making backend calls, to ensure consistent timeout and cancellation handling.

Let's verify the context usage in API methods:


Line range hint 447-449: Review fee-related methods for cache interaction.

With the introduction of a fee cache layer, the following methods need careful consideration:

  • GasPrice
  • EstimateGas
  • MaxPriorityFeePerGas

Ensure these methods:

  1. Handle cache miss scenarios gracefully
  2. Consider potential race conditions during cache updates
  3. Implement proper error handling for cache invalidation

Let's examine the backend implementation of these methods:

Also applies to: 460-463, 466-469


110-112: Verify completion of caching implementation.

While the context changes prepare for the fee cache layer, the actual caching implementation seems to be missing. This might be part of a larger change set, but please ensure:

  1. The caching logic is implemented in the backend
  2. Cache invalidation strategy is documented
  3. Cache-related configuration (TTL, size limits) is properly defined

Let's check for cache-related implementations:

jsonrpc/backend/backend.go (1)

73-73: Ensure all callers of NewJSONRPCBackend pass the new context parameter

The constructor NewJSONRPCBackend now requires a context.Context parameter. Please verify that all invocations of this constructor throughout the codebase have been updated to pass the appropriate context.

Run the following script to identify any calls to NewJSONRPCBackend that may not have been updated:

Expected result: No matches indicating missing context parameters.

✅ Verification successful

Let me verify the implementation of NewJSONRPCBackend and its usage to ensure we have a complete picture.


All callers of NewJSONRPCBackend are properly updated

The verification shows that there is only one caller of NewJSONRPCBackend in jsonrpc/jsonrpc.go and it correctly passes the context parameter as the first argument: backend.NewJSONRPCBackend(ctx, app, logger, svrCtx, clientCtx, jsonRPCConfig). All parameters match the function signature and are in the correct order.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify that all calls to NewJSONRPCBackend pass the context parameter

# Search for calls to NewJSONRPCBackend with missing context parameter
rg 'NewJSONRPCBackend\s*\(' --glob '!jsonrpc/backend/backend.go' -A 1 | grep -B 1 -E 'NewJSONRPCBackend\s*\(\s*\w+\s*\,'

Length of output: 232


Script:

#!/bin/bash
# Get the implementation of NewJSONRPCBackend
ast-grep --pattern 'func NewJSONRPCBackend($$$) ($$$) {
  $$$
}'

# Get all usages of NewJSONRPCBackend
rg 'NewJSONRPCBackend\s*\(' -A 2

Length of output: 3740

jsonrpc/backend/gas.go Outdated Show resolved Hide resolved
jsonrpc/backend/gas.go Outdated Show resolved Hide resolved
jsonrpc/backend/backend.go Show resolved Hide resolved
jsonrpc/backend/backend.go Show resolved Hide resolved
jsonrpc/backend/backend.go Outdated Show resolved Hide resolved
Copy link

codecov bot commented Nov 13, 2024

Codecov Report

Attention: Patch coverage is 0% with 105 lines in your changes missing coverage. Please review.

Project coverage is 28.17%. Comparing base (63aa650) to head (7c23f0a).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
jsonrpc/backend/backend.go 0.00% 51 Missing ⚠️
jsonrpc/namespaces/eth/filters/api.go 0.00% 23 Missing ⚠️
jsonrpc/backend/gas.go 0.00% 12 Missing ⚠️
jsonrpc/jsonrpc.go 0.00% 7 Missing ⚠️
jsonrpc/backend/eth.go 0.00% 2 Missing ⚠️
jsonrpc/namespaces/cosmos/api.go 0.00% 2 Missing ⚠️
jsonrpc/namespaces/eth/api.go 0.00% 2 Missing ⚠️
jsonrpc/namespaces/net/api.go 0.00% 2 Missing ⚠️
jsonrpc/namespaces/txpool/api.go 0.00% 2 Missing ⚠️
jsonrpc/namespaces/web3/api.go 0.00% 2 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #110      +/-   ##
==========================================
- Coverage   28.25%   28.17%   -0.08%     
==========================================
  Files         126      126              
  Lines       14237    14275      +38     
==========================================
  Hits         4022     4022              
- Misses       9642     9680      +38     
  Partials      573      573              
Files with missing lines Coverage Δ
jsonrpc/backend/eth.go 0.00% <0.00%> (ø)
jsonrpc/namespaces/cosmos/api.go 0.00% <0.00%> (ø)
jsonrpc/namespaces/eth/api.go 0.00% <0.00%> (ø)
jsonrpc/namespaces/net/api.go 0.00% <0.00%> (ø)
jsonrpc/namespaces/txpool/api.go 0.00% <0.00%> (ø)
jsonrpc/namespaces/web3/api.go 0.00% <0.00%> (ø)
jsonrpc/jsonrpc.go 0.00% <0.00%> (ø)
jsonrpc/backend/gas.go 0.00% <0.00%> (ø)
jsonrpc/namespaces/eth/filters/api.go 0.00% <0.00%> (ø)
jsonrpc/backend/backend.go 0.00% <0.00%> (ø)

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Outside diff range and nitpick comments (2)
jsonrpc/backend/backend.go (2)

130-139: Ensure appropriate error handling in feeInfo method

The feeInfo method returns NewInternalError("jsonrpc is not ready") when feeDenom is empty. Verify that NewInternalError is the correct error constructor for this context and that it aligns with the overall error handling strategy of the project.


130-139: Consider adding unit tests for the feeInfo method

Implementing unit tests for feeInfo can help ensure it behaves correctly, especially in scenarios where fee data may not be initialized yet.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between f714dff and 99f7684.

📒 Files selected for processing (3)
  • jsonrpc/backend/backend.go (5 hunks)
  • jsonrpc/backend/eth.go (2 hunks)
  • jsonrpc/backend/gas.go (3 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • jsonrpc/backend/eth.go
🔇 Additional comments (5)
jsonrpc/backend/gas.go (1)

111-125: LGTM! Well-documented and secure implementation.

The changes have effectively addressed previous concerns by:

  • Documenting the precision multiplier's purpose
  • Adding overflow protection
  • Using consistent fee information retrieval
jsonrpc/backend/backend.go (4)

43-47: Addition of fee cache fields is appropriate

The introduction of feeDenom, feeDecimals, and feeMutex to the JSONRPCBackend struct effectively sets up the necessary fields for caching fee information with proper synchronization.


74-74: Inclusion of context parameter enhances lifecycle management

Adding ctx context.Context to the NewJSONRPCBackend constructor allows for better control over the backend's lifecycle, enabling cancellation and timeout functionalities.


124-126: Potential goroutine leak due to feeFetcher persists

The issue regarding the potential goroutine leak remains unresolved. The feeFetcher goroutine may continue running if the context b.ctx is not properly canceled when the JSONRPCBackend is no longer needed.


141-185: Initial fetch missing in feeFetcher, may cause startup delays

The feeFetcher function waits for the first ticker interval (5 seconds) before fetching fee data, which leaves feeDenom and feeDecimals unset initially. This can cause errors if other methods access these fields before they are populated.

jsonrpc/backend/gas.go Show resolved Hide resolved
@beer-1 beer-1 merged commit 8be43a3 into main Nov 13, 2024
5 of 6 checks passed
@beer-1 beer-1 deleted the feat/cache-fee branch November 13, 2024 10:36
beer-1 added a commit that referenced this pull request Nov 13, 2024
* loading fee requires executing evm call, so introduce cache layer and periodically fetch it

* decrease update interval

* introduce lock

* decrease fetch interval
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant