-
Notifications
You must be signed in to change notification settings - Fork 13
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
Check change limiter for transmuter #435
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 ofunsafe
code.The function uses an
unsafe
block 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.lock
is excluded by!**/*.lock
go.sum
is 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
FFISlice
struct 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_slice
method usesunsafe
to 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
: Ensurecbindgen
configuration is correct.The
cbindgen
configuration 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
FFIResult
struct 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 inok
method.The
ok
method converts aBox
to a raw pointer, which requires manual deallocation. Ensure that the memory is freed appropriately to prevent leaks.
17-25
: Handle potential errors inerr
method safely.The
err
method creates aCString
from a string. Ensure that the conversion handles errors gracefully and that the raw pointer is managed correctly.
28-34
: Ensure conversion fromResult
is safe and correct.The
From
implementation forResult
converts 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
FFIDecimalToDec
function correctly converts an FFI decimal back toosmomath.Dec
. The use ofNewDecFromBigIntWithPrec
is appropriate here.
50-54
: LGTM! Conversion function is straightforward.The
FFIU128ToBigInt
function correctly converts aC.struct_FFIU128
back to abig.Int
. The use ofSetBits
is appropriate.
15-21
: Ensure proper error handling for FFI conversions.The
NewFFIDecimal
function correctly converts aosmomath.Dec
toC.struct_FFIDecimal
usingNewFFIU128
. Ensure that the error returned byNewFFIU128
is properly logged or handled at a higher level to aid debugging.Verification successful
Proper error handling for
NewFFIDecimal
is confirmed.The
NewFFIDecimal
function's usage inrustffi/transmuter.go
includes 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
NewFFIU128
converts abig.Int
to aC.struct_FFIU128
. Ensure that the conversion handles large numbers correctly and logs any overflow errors.Verification successful
Integer Overflow Handling in
NewFFIU128
is Adequately ManagedThe
NewFFIU128
function 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.go
confirm 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 methodCheckChangeRateLimiter
looks good.The method
CheckChangeRateLimiter
introduces time-based rate limiting. Ensure thatcurrentTime
is used correctly in the underlying function.
41-43
: New methodComputeResultedWeights
looks good.The method
ComputeResultedWeights
computes weights based ontokenInCoin
. Ensure that the underlying logic incomputeResultedWeights
is correct.
33-35
: Ensure correct parameter usage inCheckStaticRateLimiter
.The method
CheckStaticRateLimiter
now takestokenInDenom
andtokenInWeight
. Ensure that these parameters are correctly used in the underlyingcheckStaticRateLimiter
function.Verification successful
Parameters correctly used in
CheckStaticRateLimiter
.The parameters
tokenInDenom
andtokenInWeight
are correctly utilized in thecheckStaticRateLimiter
function, 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_division
looks good.The method
into_division
correctly convertsFFIDivision
intoDivision
. Ensure that theunchecked_new
method is used safely.
33-59
: Functioncompressed_moving_average
looks good.The function
compressed_moving_average
handles FFI safely and correctly maps Rust logic to the C interface. Ensure that error handling is robust.
61-76
: Functionis_division_outdated
looks good.The function
is_division_outdated
checks division status correctly. Ensure that the conversion and logic are consistent with the Rust library.
6-20
: Ensure FFI structFFIDivision
is correctly defined.The
FFIDivision
struct 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
FFIDivision
struct 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
FFIU128
struct 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 fromFFIU128
tou128
.The conversion logic correctly reconstructs a
u128
from twou64
values. This is an efficient and correct approach.
12-16
: Correctness of conversion fromu128
toFFIU128
.The conversion logic accurately splits a
u128
into twou64
values, which is necessary for FFI compatibility.
18-20
: Ensure FFI safety forFFIDecimal
.Similar to
FFIU128
,FFIDecimal
is also marked with#[repr(C)]
. Ensure that any changes maintain this alignment.
22-26
: Conversion fromFFIDecimal
totransmuter_math::Decimal
.The conversion uses the
transmuter_math::Decimal::raw
method, which is appropriate for handling raw values. Ensure that thetransmuter_math
crate is correctly handling these values.
28-32
: Conversion fromtransmuter_math::Decimal
toFFIDecimal
.The conversion method uses
atomics().u128()
to retrieve the underlyingu128
representation, 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
u128
andDecimal
conversions. 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
NewFFIU128
function.
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
NewFFIDecimal
function.rustffi/transmuter.go (7)
23-27
: Correct calculation ofintegral
inNewFFIDivision
.The calculation of
integral
usingprevValue.MulInt
is appropriate for determining the integral part of the division over elapsed time.
29-45
: Ensure correct conversion inNewFFIDivisionRaw
.The function correctly converts
osmomath.Dec
values 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
FFIDivision
instances, 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.free
for 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 changingint64
touint64
for timestamps.The change from
int64
touint64
forStartedAt
andUpdatedAt
fields 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
uint64
for timestamps. The change fromint64
touint64
forStartedAt
andUpdatedAt
does 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-lib
with the build process.The addition of the
build-rust-lib
target 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 forCompressedMovingAverage
with no divisions.The test case for
CompressedMovingAverage
with no divisions is well-implemented, checking for error handling and expected results.
26-54
: LGTM! Thorough test forCompressedMovingAverage
with 2 divisions.The test case for
CompressedMovingAverage
with 2 divisions accurately verifies the calculation logic and expected outcomes.
56-163
: LGTM! Detailed test forCompressedMovingAverage
with division skipping.The test case for
CompressedMovingAverage
when divisions are skipped is detailed and covers various scenarios, ensuring robustness.
166-205
: LGTM! Well-structured test forIsDivisionOutdated
.The test case for
IsDivisionOutdated
is well-structured, covering edge cases and ensuring correct behavior.domain/errors.go (1)
328-336
: Addition ofChangeRateLimiterInvalidUpperLimitError
is 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
: RefactoredcheckStaticRateLimiter
function.The function now accepts
tokenInDenom
andtokenInWeight
, aligning with the updated logic for static rate limiter checks. This change improves clarity and functionality.
255-340
: Introduction ofcheckChangeRateLimiter
function.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
: UpdatedcomputeResultedWeights
function.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
: UpdatedCheckStaticRateLimiter
method signature.The method now includes
tokenInWeight
as 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
computeResultedWeights
accurately computes weights, as this impacts the limiter checks. Verify thatcheckStaticRateLimiter
andcheckChangeRateLimiter
handle 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
, andcheckChangeRateLimiter
are 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.
computeResultedWeights
calculates the weights needed for rate limiter checks.checkStaticRateLimiter
andcheckChangeRateLimiter
handle 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
Division
struct fromint64
touint64
for improved timestamp handling.Tests