Skip to content

feat: add unbounded partial addresses matching over address filters #953

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

Merged
merged 1 commit into from
May 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
module github.com/formancehq/ledger

go 1.23.0
go 1.24

toolchain go1.23.3
toolchain go1.24.2

replace github.com/formancehq/ledger/pkg/client => ./pkg/client

Expand Down
10 changes: 10 additions & 0 deletions internal/storage/ledger/accounts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,16 @@ func TestAccountsList(t *testing.T) {
require.NoError(t, err)
require.Len(t, accounts.Data, 3)
})
t.Run("list using filter on address and unbounded length", func(t *testing.T) {
t.Parallel()
accounts, err := store.Accounts().Paginate(ctx, common.OffsetPaginatedQuery[any]{
Options: common.ResourceQuery[any]{
Builder: query.Match("address", "account:..."),
},
})
require.NoError(t, err)
require.Len(t, accounts.Data, 3)
})
t.Run("list using filter on multiple address", func(t *testing.T) {
t.Parallel()
accounts, err := store.Accounts().Paginate(ctx, common.OffsetPaginatedQuery[any]{
Expand Down
8 changes: 6 additions & 2 deletions internal/storage/ledger/resource_aggregated_balances.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ func (h aggregatedBalancesResourceRepositoryHandler) BuildDataset(query common.R
Where("effective_date <= ?", query.PIT)
}

if query.UseFilter("address", isPartialAddress) {
if query.UseFilter("address", func(value any) bool {
return isPartialAddress(value.(string))
}) {
subQuery := h.store.db.NewSelect().
TableExpr(h.store.GetPrefixedRelationName("accounts")).
Column("address_array").
Expand Down Expand Up @@ -108,7 +110,9 @@ func (h aggregatedBalancesResourceRepositoryHandler) BuildDataset(query common.R
ColumnExpr("(input, output)::"+h.store.GetPrefixedRelationName("volumes")+" as volumes").
Where("ledger = ?", h.store.ledger.Name)

if query.UseFilter("metadata") || query.UseFilter("address", isPartialAddress) {
if query.UseFilter("metadata") || query.UseFilter("address", func(value any) bool {
return isPartialAddress(value.(string))
}) {
subQuery := h.store.db.NewSelect().
TableExpr(h.store.GetPrefixedRelationName("accounts")).
Column("address").
Expand Down
4 changes: 3 additions & 1 deletion internal/storage/ledger/resource_volumes.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ func (h volumesResourceHandler) BuildDataset(query common.RepositoryHandlerBuild

var selectVolumes *bun.SelectQuery

needAddressSegments := query.UseFilter("address", isPartialAddress)
needAddressSegments := query.UseFilter("address", func(value any) bool {
return isPartialAddress(value.(string))
})
if !query.UsePIT() && !query.UseOOT() {
selectVolumes = h.store.db.NewSelect().
Column("asset", "input", "output").
Expand Down
22 changes: 9 additions & 13 deletions internal/storage/ledger/transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,26 +297,22 @@ func (store *Store) DeleteTransactionMetadata(ctx context.Context, id uint64, ke
func filterAccountAddressOnTransactions(address string, source, destination bool) string {
src := strings.Split(address, ":")

needSegmentCheck := false
for _, segment := range src {
needSegmentCheck = segment == ""
if needSegmentCheck {
break
}
}

if needSegmentCheck {
m := map[string]any{
fmt.Sprint(len(src)): nil,
}
if isPartialAddress(address) {
m := map[string]any{}
parts := make([]string, 0)

for i, segment := range src {
if len(segment) == 0 {
continue
}
if i == len(src)-1 && segment == "..." {
break
}
m[fmt.Sprint(i)] = segment
}
if src[len(src)-1] != "..." {
m[fmt.Sprint(len(src))] = nil
}

data, err := json.Marshal([]any{m})
if err != nil {
Expand Down Expand Up @@ -345,4 +341,4 @@ func filterAccountAddressOnTransactions(address string, source, destination bool
parts = append(parts, fmt.Sprintf("destinations @> '%s'", string(data)))
}
return strings.Join(parts, " or ")
}
}
9 changes: 9 additions & 0 deletions internal/storage/ledger/transactions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -777,6 +777,15 @@ func TestTransactionsList(t *testing.T) {
},
expected: []ledger.Transaction{tx5, tx4, tx3},
},
{
name: "address filter using segment and unbounded segment list",
query: common.ColumnPaginatedQuery[any]{
Options: common.ResourceQuery[any]{
Builder: query.Match("account", "users:..."),
},
},
expected: []ledger.Transaction{tx5, tx4, tx3},
},
{
name: "filter using metadata",
query: common.ColumnPaginatedQuery[any]{
Expand Down
17 changes: 9 additions & 8 deletions internal/storage/ledger/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@ import (
"strings"
)

func isSegmentedAddress(address string) bool {
func isPartialAddress(address string) bool {
src := strings.Split(address, ":")

for _, segment := range src {
for index, segment := range src {
if segment == "" {
return true
}
if segment == "..." && index == len(src)-1 {
return true
}
}

return false
Expand All @@ -22,10 +25,12 @@ func filterAccountAddress(address, key string) string {

if isPartialAddress(address) {
src := strings.Split(address, ":")
parts = append(parts, fmt.Sprintf("jsonb_array_length(%s_array) = %d", key, len(src)))
if src[len(src)-1] != "" {
parts = append(parts, fmt.Sprintf("jsonb_array_length(%s_array) = %d", key, len(src)))
}
Comment on lines +28 to +30
Copy link

Choose a reason for hiding this comment

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

💡 Verification agent

🧩 Analysis chain

Verify edge case handling for address ending with ":"

The condition src[len(src)-1] != "" prevents adding the array length constraint when the last segment is "...". However, consider the edge case where an address ends with ":" (e.g., "account:"), which would create an empty last segment.


🏁 Script executed:

#!/bin/bash
# Test edge cases for addresses ending with colon
echo "Testing address patterns that end with colon..."

# Search for test cases that might cover this scenario
rg -A 10 -B 2 ".*:\"?\s*$" --type go

# Look for existing test coverage of edge cases
ast-grep --pattern 'func $_(t *testing.T) {
  $$$
  address := "$_:"
  $$$
}'

Length of output: 72841


#!/bin/bash

Show the implementation of filterAccountAddress in utils.go

rg -n "func filterAccountAddress" -A 50 internal/storage/ledger/utils.go

Also dump the file around the array-length check

sed -n '1,200p' internal/storage/ledger/utils.go | sed -n '1,80p'


#!/bin/bash

Extract the full implementation of filterAccountAddress to inspect edge-case handling

rg -n "func filterAccountAddress" -A50 -B2 internal/storage/ledger/utils.go


Add tests and clarify trailing-colon behavior in filterAccountAddress

The if src[len(src)-1] != "" check in internal/storage/ledger/utils.go (lines ~28–30) intentionally skips the length constraint for addresses ending with “:”, but we have no coverage or documentation for this edge case.

• File: internal/storage/ledger/utils.go, around lines 27–30

if src[len(src)-1] != "" {
    parts = append(parts, fmt.Sprintf("jsonb_array_length(%s_array) = %d", key, len(src)))
}

• Add unit tests for inputs like "account:" to verify the generated SQL (it should only enforce prefix matching, no array-length check).
• Document in code (or GoDoc) that a trailing colon enables prefix/sub-account matching by design.

🤖 Prompt for AI Agents
In internal/storage/ledger/utils.go around lines 27 to 30, the condition
src[len(src)-1] != "" skips adding the array length constraint for addresses
ending with a colon, but this behavior is undocumented and lacks test coverage.
Add unit tests for inputs like "account:" to verify that the generated SQL
enforces only prefix matching without the array-length check. Also, update the
function's comments or GoDoc to clearly document that a trailing colon enables
prefix or sub-account matching by design.


for i, segment := range src {
if len(segment) == 0 {
if len(segment) == 0 || segment == "..." {
continue
}
parts = append(parts, fmt.Sprintf("%s_array @@ ('$[%d] == \"%s\"')::jsonpath", key, i, segment))
Expand All @@ -37,10 +42,6 @@ func filterAccountAddress(address, key string) string {
return strings.Join(parts, " and ")
}

func isPartialAddress(address any) bool {
return isSegmentedAddress(address.(string))
}

func explodeAddress(address string) map[string]any {
parts := strings.Split(address, ":")
ret := make(map[string]any, len(parts)+1)
Expand Down
4 changes: 2 additions & 2 deletions tools/generator/go.mod
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
module github.com/formancehq/ledger/tools/generator

go 1.23.0
go 1.24

toolchain go1.23.3
toolchain go1.24.3

replace github.com/formancehq/ledger => ../..

Expand Down
Loading