-
Notifications
You must be signed in to change notification settings - Fork 17
Check change limiter for transmuter #435
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
base: v25.x
Are you sure you want to change the base?
Conversation
WalkthroughThis update significantly enhances the project by integrating Rust functionalities alongside existing Go components. It introduces a Rust workspace, improved error handling, and new conversion utilities while modifying essential data structures. These changes streamline build processes, enhance interoperability between Go and Rust, and ensure robust testing frameworks, ultimately improving the development experience and code reliability. Changes
Sequence Diagram(s)sequenceDiagram
participant Go
participant Rust
participant FFI
Go->>FFI: Call function to create a division
FFI->>Rust: Convert Go decimal to Rust decimal
Rust->>Rust: Perform calculations
Rust->>FFI: Return results
FFI->>Go: Return division details
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? TipsChatThere are 3 ways to chat with CodeRabbit:
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 as PR comments)
Additionally, you can add CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 2
Outside diff range, codebase verification and nitpick comments (2)
rustffi/link.go (1)
1-6: Add documentation for CGO linking.Consider adding comments to explain the purpose of linking the Rust library and how it integrates with the Go code. This will improve maintainability and understanding for future developers.
rustffi/rustsrc/src/option.rs (1)
1-7: Document the use ofunsafecode.The function uses an
unsafeblock to dereference a pointer. Consider adding comments to explain the safety guarantees and assumptions, ensuring that the function is used correctly and safely.
Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Files ignored due to path filters (2)
Cargo.lockis excluded by!**/*.lockgo.sumis excluded by!**/*.sum
Files selected for processing (22)
- .gitignore (1 hunks)
- Cargo.toml (1 hunks)
- Makefile (1 hunks)
- domain/errors.go (1 hunks)
- go.mod (1 hunks)
- router/usecase/pools/export_test.go (2 hunks)
- router/usecase/pools/routable_cw_alloy_transmuter_pool.go (5 hunks)
- router/usecase/pools/routable_cw_alloy_transmuter_pool_test.go (3 hunks)
- rustffi/link.go (1 hunks)
- rustffi/numbers.go (1 hunks)
- rustffi/numbers_test.go (1 hunks)
- rustffi/rustsrc/Cargo.toml (1 hunks)
- rustffi/rustsrc/build.rs (1 hunks)
- rustffi/rustsrc/src/lib.rs (1 hunks)
- rustffi/rustsrc/src/numbers.rs (1 hunks)
- rustffi/rustsrc/src/option.rs (1 hunks)
- rustffi/rustsrc/src/result.rs (1 hunks)
- rustffi/rustsrc/src/slice.rs (1 hunks)
- rustffi/rustsrc/src/transmuter.rs (1 hunks)
- rustffi/transmuter.go (1 hunks)
- rustffi/transmuter_test.go (1 hunks)
- sqsdomain/cosmwasmpool/alloy_transmuter.go (1 hunks)
Files skipped from review due to trivial changes (4)
- .gitignore
- Cargo.toml
- go.mod
- rustffi/rustsrc/Cargo.toml
Additional comments not posted (50)
rustffi/rustsrc/src/lib.rs (1)
1-5: Well-organized module structure.The module declarations provide a clear and organized entry point for the library. Ensure that each module is well-documented and tested.
rustffi/rustsrc/src/slice.rs (2)
3-6: Ensure safety when using raw pointers.The
FFISlicestruct uses raw pointers, which require careful handling to avoid undefined behavior. Ensure that the pointers are valid and that the length is correct when creating slices.
8-11: Use of unsafe code requires caution.The
as_slicemethod usesunsafeto convert a raw pointer to a slice. Ensure that the pointer is valid and the length is correct to prevent memory safety issues.rustffi/rustsrc/build.rs (2)
6-12: Verify the correctness of the path construction.The path construction logic assumes a specific directory structure. Ensure this matches the project's actual structure to avoid issues during the build process.
14-22: Ensurecbindgenconfiguration is correct.The
cbindgenconfiguration specifies generating C bindings. Ensure that the configuration aligns with the project's requirements and that the generated bindings are correct.rustffi/rustsrc/src/result.rs (4)
4-7: Ensure proper memory management for raw pointers.The
FFIResultstruct uses raw pointers for both the OK value and error message. Ensure that these pointers are managed correctly to avoid memory leaks or undefined behavior.
10-15: Check for potential memory leaks inokmethod.The
okmethod converts aBoxto a raw pointer, which requires manual deallocation. Ensure that the memory is freed appropriately to prevent leaks.
17-25: Handle potential errors inerrmethod safely.The
errmethod creates aCStringfrom a string. Ensure that the conversion handles errors gracefully and that the raw pointer is managed correctly.
28-34: Ensure conversion fromResultis safe and correct.The
Fromimplementation forResultconverts it into anFFIResult. Verify that this conversion is handled correctly and safely, especially with respect to memory management.rustffi/numbers.go (4)
23-25: LGTM! Conversion function is straightforward.The
FFIDecimalToDecfunction correctly converts an FFI decimal back toosmomath.Dec. The use ofNewDecFromBigIntWithPrecis appropriate here.
50-54: LGTM! Conversion function is straightforward.The
FFIU128ToBigIntfunction correctly converts aC.struct_FFIU128back to abig.Int. The use ofSetBitsis appropriate.
15-21: Ensure proper error handling for FFI conversions.The
NewFFIDecimalfunction correctly converts aosmomath.DectoC.struct_FFIDecimalusingNewFFIU128. Ensure that the error returned byNewFFIU128is properly logged or handled at a higher level to aid debugging.Verification successful
Proper error handling for
NewFFIDecimalis confirmed.The
NewFFIDecimalfunction's usage inrustffi/transmuter.goincludes checks for errors, and appropriate actions are taken if an error is encountered. This ensures that errors are managed effectively. No further action is needed.Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash # Description: Verify usage of `NewFFIDecimal` to ensure proper error handling. # Test: Search for `NewFFIDecimal` usage. Expect: Proper error handling or logging. rg --type go -A 5 $'NewFFIDecimal'Length of output: 1459
27-48: Check for integer overflow inNewFFIU128.The function
NewFFIU128converts abig.Intto aC.struct_FFIU128. Ensure that the conversion handles large numbers correctly and logs any overflow errors.Verification successful
Integer Overflow Handling in
NewFFIU128is Adequately ManagedThe
NewFFIU128function checks for integer overflow by ensuring the input fits within a 128-bit unsigned integer and returns an error if it does not. This behavior is verified in the tests. No additional logging for overflow is necessary as the error handling covers this scenario.
- Usage and error handling are present in
rustffi/numbers.go.- Tests in
rustffi/numbers_test.goconfirm correct behavior.Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash # Description: Verify usage of `NewFFIU128` to ensure proper handling of large integers. # Test: Search for `NewFFIU128` usage. Expect: Proper handling or logging of potential overflow. rg --type go -A 5 $'NewFFIU128'Length of output: 1158
router/usecase/pools/export_test.go (3)
37-39: New methodCheckChangeRateLimiterlooks good.The method
CheckChangeRateLimiterintroduces time-based rate limiting. Ensure thatcurrentTimeis used correctly in the underlying function.
41-43: New methodComputeResultedWeightslooks good.The method
ComputeResultedWeightscomputes weights based ontokenInCoin. Ensure that the underlying logic incomputeResultedWeightsis correct.
33-35: Ensure correct parameter usage inCheckStaticRateLimiter.The method
CheckStaticRateLimiternow takestokenInDenomandtokenInWeight. Ensure that these parameters are correctly used in the underlyingcheckStaticRateLimiterfunction.Verification successful
Parameters correctly used in
CheckStaticRateLimiter.The parameters
tokenInDenomandtokenInWeightare correctly utilized in thecheckStaticRateLimiterfunction, ensuring the intended functionality of checking the static rate limiter. No issues found.Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash # Description: Verify correct usage of parameters in `checkStaticRateLimiter`. # Test: Search for `checkStaticRateLimiter` implementation. Expect: Correct usage of `tokenInDenom` and `tokenInWeight`. rg --type go -A 5 $'checkStaticRateLimiter'Length of output: 2474
rustffi/rustsrc/src/transmuter.rs (4)
22-31: Conversion methodinto_divisionlooks good.The method
into_divisioncorrectly convertsFFIDivisionintoDivision. Ensure that theunchecked_newmethod is used safely.
33-59: Functioncompressed_moving_averagelooks good.The function
compressed_moving_averagehandles FFI safely and correctly maps Rust logic to the C interface. Ensure that error handling is robust.
61-76: Functionis_division_outdatedlooks good.The function
is_division_outdatedchecks division status correctly. Ensure that the conversion and logic are consistent with the Rust library.
6-20: Ensure FFI structFFIDivisionis correctly defined.The
FFIDivisionstruct is defined with fields for timestamps and decimals. Ensure that these fields align with the expected structure in the Rust library.Verification successful
FFIDivision Struct is Correctly Defined and Used
The
FFIDivisionstruct is correctly defined and consistently used within the Rust library. It is effectively integrated into methods and functions, aligning with expected FFI practices. No issues found.Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash # Description: Verify `FFIDivision` struct definition aligns with Rust library expectations. # Test: Search for `FFIDivision` usage in Rust library. Expect: Consistent field usage. rg --type rust -A 5 $'FFIDivision'Length of output: 1711
rustffi/rustsrc/src/numbers.rs (7)
1-4: Ensure FFI safety and alignment.The
FFIU128struct is marked with#[repr(C)]to ensure C-compatible memory layout, which is crucial for FFI. Ensure that any changes maintain this alignment.
6-10: Correctness of conversion fromFFIU128tou128.The conversion logic correctly reconstructs a
u128from twou64values. This is an efficient and correct approach.
12-16: Correctness of conversion fromu128toFFIU128.The conversion logic accurately splits a
u128into twou64values, which is necessary for FFI compatibility.
18-20: Ensure FFI safety forFFIDecimal.Similar to
FFIU128,FFIDecimalis also marked with#[repr(C)]. Ensure that any changes maintain this alignment.
22-26: Conversion fromFFIDecimaltotransmuter_math::Decimal.The conversion uses the
transmuter_math::Decimal::rawmethod, which is appropriate for handling raw values. Ensure that thetransmuter_mathcrate is correctly handling these values.
28-32: Conversion fromtransmuter_math::DecimaltoFFIDecimal.The conversion method uses
atomics().u128()to retrieve the underlyingu128representation, which is suitable for FFI purposes.
34-94: Comprehensive unit tests for conversions.The tests cover a wide range of scenarios, including edge cases for
u128andDecimalconversions. This ensures robustness and correctness.rustffi/numbers_test.go (2)
13-78: Comprehensive testing forNewFFIU128.The test cases cover a wide range of scenarios, including edge cases like zero, large numbers, and negative numbers. This ensures robustness and correctness of the
NewFFIU128function.
80-131: Comprehensive testing forNewFFIDecimal.The test cases thoroughly cover different decimal values, including zero, one, large numbers, and negative numbers. This ensures robustness and correctness of the
NewFFIDecimalfunction.rustffi/transmuter.go (7)
23-27: Correct calculation ofintegralinNewFFIDivision.The calculation of
integralusingprevValue.MulIntis appropriate for determining the integral part of the division over elapsed time.
29-45: Ensure correct conversion inNewFFIDivisionRaw.The function correctly converts
osmomath.Decvalues to FFI-compatible decimals. Ensure that the conversion functions handle all edge cases.
47-62: Efficient creation of multiple FFI divisions.The loop efficiently creates multiple
FFIDivisioninstances, handling errors appropriately. This is crucial for batch processing.
64-74: Efficient batch creation inNewFFIDivisionsRaw.Similar to
NewFFIDivisions, this function efficiently handles batch creation while managing errors.
76-96: Memory management inCompressedMovingAverage.The function handles memory management with
defer C.freefor pointers, ensuring no leaks occur. This is crucial for FFI operations.
98-116: Correctness ofIsDivisionOutdated.The function correctly checks if a division is outdated, with proper error handling and conversion of results.
118-127: Efficient pointer handling innewFFIDivisionSlice.The function efficiently handles pointers for FFI slices, ensuring correct memory alignment and safety.
sqsdomain/cosmwasmpool/alloy_transmuter.go (1)
85-88: Verify the impact of changingint64touint64for timestamps.The change from
int64touint64forStartedAtandUpdatedAtfields ensures positive timestamps, which is suitable for Unix timestamps. However, verify that other parts of the codebase handle this change correctly, especially if they involve arithmetic operations or comparisons.Verification successful
No issues found with the change to
uint64for timestamps. The change fromint64touint64forStartedAtandUpdatedAtdoes not affect any arithmetic operations or comparisons in the codebase, as none were found. This ensures that timestamps remain positive, aligning with Unix timestamp requirements.Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash # Description: Verify the usage of `StartedAt` and `UpdatedAt` fields in the codebase. # Test: Search for the usage of `StartedAt` and `UpdatedAt`. Expect: No issues due to type change. rg --type go 'StartedAt|UpdatedAt'Length of output: 4000
Script:
#!/bin/bash # Search for arithmetic operations involving StartedAt and UpdatedAt rg --type go 'StartedAt\s*[-+*/%]|[-+*/%]\s*StartedAt|UpdatedAt\s*[-+*/%]|[-+*/%]\s*UpdatedAt' # Search for comparisons involving StartedAt and UpdatedAt rg --type go 'StartedAt\s*[<>=!]=?|[<>=!]=?\s*StartedAt|UpdatedAt\s*[<>=!]=?|[<>=!]=?\s*UpdatedAt'Length of output: 445
Makefile (1)
70-72: LGTM! Verify integration ofbuild-rust-libwith the build process.The addition of the
build-rust-libtarget enhances the build process by compiling Rust components. Ensure this target is integrated correctly with any CI/CD pipelines or build scripts.rustffi/transmuter_test.go (4)
11-24: LGTM! Comprehensive test forCompressedMovingAveragewith no divisions.The test case for
CompressedMovingAveragewith no divisions is well-implemented, checking for error handling and expected results.
26-54: LGTM! Thorough test forCompressedMovingAveragewith 2 divisions.The test case for
CompressedMovingAveragewith 2 divisions accurately verifies the calculation logic and expected outcomes.
56-163: LGTM! Detailed test forCompressedMovingAveragewith division skipping.The test case for
CompressedMovingAveragewhen divisions are skipped is detailed and covers various scenarios, ensuring robustness.
166-205: LGTM! Well-structured test forIsDivisionOutdated.The test case for
IsDivisionOutdatedis well-structured, covering edge cases and ensuring correct behavior.domain/errors.go (1)
328-336: Addition ofChangeRateLimiterInvalidUpperLimitErroris well-structured.The new error type and its
Error()method are consistent with the existing error handling patterns in the file. This addition enhances error specificity for rate limiting configurations.router/usecase/pools/routable_cw_alloy_transmuter_pool.go (4)
191-214: Enhanced rate limiting logic with static and change limiters.The updated logic effectively checks both static and change rate limiters, allowing for more nuanced control over token transactions. This enhancement ensures compliance with both static and dynamic limits.
233-253: RefactoredcheckStaticRateLimiterfunction.The function now accepts
tokenInDenomandtokenInWeight, aligning with the updated logic for static rate limiter checks. This change improves clarity and functionality.
255-340: Introduction ofcheckChangeRateLimiterfunction.This new function efficiently handles dynamic rate limiting by leveraging historical data and Rust FFI for calculations. It enhances the flexibility and accuracy of rate limiting.
Line range hint
396-452: UpdatedcomputeResultedWeightsfunction.The function now returns weights alongside potential error messages, improving error handling and ensuring accurate weight computation for rate limiting checks.
router/usecase/pools/routable_cw_alloy_transmuter_pool_test.go (3)
414-419: UpdatedCheckStaticRateLimitermethod signature.The method now includes
tokenInWeightas a parameter, aligning with the changes in the main code. This update ensures that the static rate limiter is tested accurately.
431-511: Comprehensive test cases forCleanUpOutdatedDivision.The new test cases cover scenarios with no outdated divisions, one outdated division, and all divisions outdated. These tests ensure the correct functionality of division cleanup logic.
513-622: Thorough testing ofCheckChangeRateLimiter.The test cases evaluate the function under various conditions, including limits being exceeded and within bounds. This thorough testing ensures the reliability of the change rate limiter logic.
| let crate_dir = env::var("CARGO_MANIFEST_DIR").unwrap(); | ||
| let create_name = env::var("CARGO_PKG_NAME").unwrap(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Handle potential errors when retrieving environment variables.
Using unwrap on environment variables can cause the build to panic if the variables are not set. Consider using expect with a descriptive error message or handling the error more gracefully.
- let crate_dir = env::var("CARGO_MANIFEST_DIR").unwrap();
- let create_name = env::var("CARGO_PKG_NAME").unwrap();
+ let crate_dir = env::var("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR not set");
+ let create_name = env::var("CARGO_PKG_NAME").expect("CARGO_PKG_NAME not set");Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| let crate_dir = env::var("CARGO_MANIFEST_DIR").unwrap(); | |
| let create_name = env::var("CARGO_PKG_NAME").unwrap(); | |
| let crate_dir = env::var("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR not set"); | |
| let create_name = env::var("CARGO_PKG_NAME").expect("CARGO_PKG_NAME not set"); |
router/usecase/pools/export_test.go
Outdated
| func CleanUpOutdatedDivision(changeLimier cosmwasmpool.ChangeLimiter, time time.Time) (*cosmwasmpool.Division, []cosmwasmpool.Division, error) { | ||
| return cleanUpOutdatedDivision(changeLimier, time) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix typo in CleanUpOutdatedDivision.
There's a typo in the parameter name changeLimier. It should be changeLimiter.
- func CleanUpOutdatedDivision(changeLimier cosmwasmpool.ChangeLimiter, time time.Time) (*cosmwasmpool.Division, []cosmwasmpool.Division, error) {
+ func CleanUpOutdatedDivision(changeLimiter cosmwasmpool.ChangeLimiter, time time.Time) (*cosmwasmpool.Division, []cosmwasmpool.Division, error) {Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| func CleanUpOutdatedDivision(changeLimier cosmwasmpool.ChangeLimiter, time time.Time) (*cosmwasmpool.Division, []cosmwasmpool.Division, error) { | |
| return cleanUpOutdatedDivision(changeLimier, time) | |
| func CleanUpOutdatedDivision(changeLimiter cosmwasmpool.ChangeLimiter, time time.Time) (*cosmwasmpool.Division, []cosmwasmpool.Division, error) { | |
| return cleanUpOutdatedDivision(changeLimier, time) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Files selected for processing (2)
- router/usecase/pools/export_test.go (2 hunks)
- router/usecase/pools/routable_cw_alloy_transmuter_pool.go (5 hunks)
Files skipped from review as they are similar to previous changes (1)
- router/usecase/pools/export_test.go
Additional comments not posted (5)
router/usecase/pools/routable_cw_alloy_transmuter_pool.go (5)
233-253: Validate error handling incheckStaticRateLimiter.The function correctly checks the static rate limit and returns an error if the weight exceeds the upper limit. Ensure that the error message provides sufficient context for debugging and that all edge cases are considered.
342-394: Ensure robustness incleanUpOutdatedDivision.The function processes divisions to identify outdated ones. Ensure that the logic is robust and handles all edge cases, such as divisions being out of order or errors in calculations. Consider adding logging for better traceability.
191-214: Ensure proper handling of rate limiters.The logic for checking both static and change rate limiters is well-structured. However, ensure that
computeResultedWeightsaccurately computes weights, as this impacts the limiter checks. Verify thatcheckStaticRateLimiterandcheckChangeRateLimiterhandle errors appropriately and that the token weights are correctly validated against the limits.Verification successful
Rate limiter checks and weight computation are correctly implemented.
The functions
computeResultedWeights,checkStaticRateLimiter, andcheckChangeRateLimiterare implemented with appropriate logic and error handling. The code ensures that weights are accurately computed and validated against the rate limiters. No issues were found.
computeResultedWeightscalculates the weights needed for rate limiter checks.checkStaticRateLimiterandcheckChangeRateLimiterhandle the validation and error reporting effectively.Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash # Description: Verify the correctness of rate limiter checks and weight computation. # Test: Search for the function usage and ensure proper error handling and weight computation. rg --type go -A 5 $'computeResultedWeights|checkStaticRateLimiter|checkChangeRateLimiter'Length of output: 5553
255-340: Thoroughly testcheckChangeRateLimiter.This function introduces complex logic to handle change rate limiting, including historical data processing and moving average calculations. Ensure comprehensive tests cover all scenarios, including edge cases related to outdated divisions and upper limit calculations.
Line range hint
396-452:
Verify accuracy incomputeResultedWeights.The function calculates weights using normalization factors, which are critical for rate limiting. Ensure that the function accurately handles missing normalization factors and that the weight calculations are precise. Consider adding validation to prevent potential errors.
Creates FFI against rust code to compute change limit for transmuter.
Summary by CodeRabbit
New Features
Bug Fixes
Divisionstruct fromint64touint64for improved timestamp handling.Tests