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

fix(v2.1): create transaction compatibility #696

Merged
merged 1 commit into from
Feb 20, 2025

Conversation

gfyrag
Copy link
Contributor

@gfyrag gfyrag commented Feb 20, 2025

No description provided.

@gfyrag gfyrag requested a review from a team as a code owner February 20, 2025 12:22
Copy link

coderabbitai bot commented Feb 20, 2025

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

🗂️ Base branches to auto review (1)
  • main

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Walkthrough

This pull request modifies key build and processing components. The Earthfile now explicitly copies source files across all build stages, disables client SDK generation, and ensures the correct binary is included in the final image. Utility functions have been refactored to use pointer types and precise type handling, with corresponding test cases added. Ledger store queries now support PIT and date filtering with improved error handling, while new tests validate account and balance retrieval logic.

Changes

File(s) Change Summary
Earthfile Updated COPY commands to explicitly include (+sources/*) for stages including generate, compile, tests, deploy, lint, and bench. Disabled the generate-client stage and modified the build-image stage to copy (+compile/main).
internal/api/.../utils.go Modified getPITFilter to handle the pit variable as a pointer (*time.Time) and updated time parsing accordingly.
internal/script.go Revised ToCore method in ScriptV1 by introducing a type switch for precise formatting of the "amount" field based on its underlying type (string vs float64).
internal/script_test.go Added new test function TestConvertScriptV1 to validate the conversion behavior of ScriptV1 for both float64 and string representations of "amount".
internal/storage/ledgerstore/accounts.go
internal/storage/ledgerstore/accounts_test.go
Enhanced accountQueryContext by adding PIT and first_usage filtering logic. A new test case ("list using filter on balances and pit") validates the filtering mechanism.
internal/storage/ledgerstore/balances.go
internal/storage/ledgerstore/balances_test.go
Updated GetBalance to build a SQL query that computes the balance from moves, with refined error handling (returning zero for not found accounts). Added a new test (TestGetBalances) to verify behavior for non-existent accounts.

Sequence Diagram(s)

sequenceDiagram
    participant Dev as Developer
    participant EF as Earthfile
    participant BS as Build System
    Dev->>EF: Trigger build process
    EF->>EF: Copy source files to /src for stages (generate, compile, tests, deploy, lint, bench)
    EF-->>EF: Skip generate-client stage (disabled)
    EF->>BS: Initiate build-image stage with compiled binary
    BS-->>Dev: Final image built with correct binary
Loading
sequenceDiagram
    participant App as Client/Application
    participant Store as Ledgerstore
    participant DB as Database
    App->>Store: Call GetBalance(ctx, address, asset)
    Store->>DB: Execute SQL query on MovesTable
    alt Result found
        DB-->>Store: Return balance result
    else Not found (sqlutils.ErrNotFound)
        DB-->>Store: Return error indicating missing record
        Store-->>Store: Set balance to zero
    end
    Store-->>App: Return computed balance
Loading

Possibly related PRs

Suggested labels

build-images, benchmarks

Suggested reviewers

  • flemzord
  • paul-nicolas

Poem

I'm a rabbit in the codefield, light on my feet,
Hopping through Earthfile changes that are neat.
Sources spread like carrots, fresh and bright,
With queries and tests keeping logic just right.
I celebrate these tweaks with a joyful midnight 🐇!


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 generate docstrings to generate docstrings for this PR. (Beta)
  • @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: 0

🧹 Nitpick comments (2)
internal/script_test.go (1)

19-47: Add test cases for edge cases and error scenarios.

The current test cases cover basic float64 and big integer conversions, but consider adding cases for:

  • Negative numbers
  • Zero values
  • Numbers with decimal places
  • Invalid string formats

Here's a suggested expansion of the test cases:

 testCases := []testCase{
     // ... existing cases ...
+    {
+        name: "negative number",
+        inputVars: map[string]any{
+            "amount": map[string]any{
+                "asset":  "USD",
+                "amount": float64(-1000),
+            },
+        },
+        expected: map[string]string{
+            "amount": "USD -1000",
+        },
+    },
+    {
+        name: "zero value",
+        inputVars: map[string]any{
+            "amount": map[string]any{
+                "asset":  "USD",
+                "amount": float64(0),
+            },
+        },
+        expected: map[string]string{
+            "amount": "USD 0",
+        },
+    },
+    {
+        name: "decimal number",
+        inputVars: map[string]any{
+            "amount": map[string]any{
+                "asset":  "USD",
+                "amount": float64(100.50),
+            },
+        },
+        expected: map[string]string{
+            "amount": "USD 100",
+        },
+    },
+    {
+        name: "invalid string format",
+        inputVars: map[string]any{
+            "amount": map[string]any{
+                "asset":  "USD",
+                "amount": "not_a_number",
+            },
+        },
+        expected: map[string]string{
+            "amount": "USD not_a_number",
+        },
+    },
 }
internal/storage/ledgerstore/accounts_test.go (1)

190-201: Consider expanding test coverage for PIT filtering.

While the test case effectively validates negative balance filtering with PIT, consider adding test cases for:

  • Positive balance filtering with PIT
  • Zero balance filtering with PIT
  • Multiple PIT values to verify boundary conditions

Example test case:

+	t.Run("list using filter on positive balances and pit", func(t *testing.T) {
+		t.Parallel()
+		accounts, err := store.GetAccountsWithVolumes(ctx, NewGetAccountsQuery(NewPaginatedQueryOptions(PITFilterWithVolumes{
+			PITFilter: PITFilter{
+				PIT: pointer.For(now.Add(100 * time.Millisecond)),
+			},
+		}).
+			WithQueryBuilder(query.Gt("balance[USD]", 0)),
+		))
+		require.NoError(t, err)
+		require.Len(t, accounts.Data, 1) // account:1
+		require.Equal(t, "account:1", accounts.Data[0].Account.Address)
+	})
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between db39a0f and 7a17f8f.

⛔ Files ignored due to path filters (1)
  • docker-compose.yml is excluded by !**/*.yml
📒 Files selected for processing (8)
  • Earthfile (1 hunks)
  • internal/api/v2/utils.go (1 hunks)
  • internal/script.go (1 hunks)
  • internal/script_test.go (1 hunks)
  • internal/storage/ledgerstore/accounts.go (2 hunks)
  • internal/storage/ledgerstore/accounts_test.go (3 hunks)
  • internal/storage/ledgerstore/balances.go (1 hunks)
  • internal/storage/ledgerstore/balances_test.go (1 hunks)
🔇 Additional comments (10)
internal/script.go (1)

35-40: Verify potential precision loss in float64 to int conversion.

The type switch handles both string and float64 amounts, but the float64 to int conversion could lead to precision loss for large numbers or numbers with decimal places.

Please confirm:

  1. Is precision loss acceptable in this context?
  2. Should we add validation to ensure the float64 value is within safe integer bounds?

Consider adding validation before the conversion:

 case float64:
+    if amount > float64(math.MaxInt64) || amount < float64(math.MinInt64) {
+        // Handle overflow case
+    }
     s.Script.Vars[k] = fmt.Sprintf("%s %d", v["asset"], int(amount))
internal/script_test.go (1)

49-63: LGTM! Well-structured test implementation.

The test implementation is clean and follows best practices:

  • Uses table-driven tests for better maintainability
  • Enables parallel execution for better performance
  • Uses clear test case names
  • Properly validates the conversion results
internal/api/v2/utils.go (1)

50-58: LGTM! Improved pointer handling for PIT filter.

The changes improve memory efficiency and make the code more idiomatic by using pointer types for optional time values.

internal/storage/ledgerstore/balances.go (2)

151-158: LGTM! Improved balance query implementation.

The new implementation:

  • Uses direct SQL query instead of function call, improving maintainability
  • Correctly calculates balance using post-commit volumes
  • Properly orders results by sequence

160-165: LGTM! Added proper handling for non-existent accounts.

The changes ensure that non-existent accounts return a zero balance instead of an error, improving API compatibility.

internal/storage/ledgerstore/balances_test.go (1)

21-31: LGTM! Good test coverage for non-existent accounts.

The test verifies that querying a non-existent account returns a zero balance without error, ensuring proper API behavior.

Earthfile (1)

122-122:

❓ Verification inconclusive

Verify impact of disabling client generation.

The client generation has been disabled. Please ensure this won't impact users who depend on the generated client SDK.


🏁 Script executed:

#!/bin/bash
# Check for any references to generated client code
rg -l "github.com/formancehq/ledger/pkg/client"

Length of output: 47


Manual Verification Required: Client SDK Impact

  • The build command disabling client generation (#BUILD +generate-client) is now in effect.
  • Our automated search using rg -l "github.com/formancehq/ledger/pkg/client" returned no matches, suggesting there are no direct references to the generated client code.
  • However, given the low-confidence inference from the automated script, please manually verify that disabling client generation does not adversely affect any users dependent on the client SDK. Also, review tests, documentation, or other configurations that might reference the client SDK.
internal/storage/ledgerstore/accounts.go (3)

110-118: LGTM! Proper handling of PIT filtering for balances.

The implementation correctly handles date filtering for balance queries with proper SQL injection prevention through parameterized queries.


127-134: LGTM! Consistent implementation of PIT filtering.

The implementation maintains consistency with asset-specific balance filtering while ensuring secure query construction.


152-153: LGTM! Clean implementation of first_usage filtering.

The implementation elegantly handles first_usage filtering using standard comparison operators.

@flemzord flemzord changed the base branch from main to release/v2.1 February 20, 2025 12:36
@gfyrag gfyrag force-pushed the hotfix/v2.1/create-transaction-compatiblity branch from 7a17f8f to fd89662 Compare February 20, 2025 12:39
@gfyrag gfyrag enabled auto-merge (rebase) February 20, 2025 12:41
@gfyrag gfyrag requested a review from Dav-14 February 20, 2025 12:56
@flemzord flemzord disabled auto-merge February 20, 2025 12:59
@flemzord flemzord merged commit 6d9ae35 into release/v2.1 Feb 20, 2025
7 checks passed
@flemzord flemzord deleted the hotfix/v2.1/create-transaction-compatiblity branch February 20, 2025 12:59
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.

2 participants