From 8ec1524606801730b8da110b68fd32e153232102 Mon Sep 17 00:00:00 2001 From: Cristian Magherusan-Stanciu Date: Fri, 16 Oct 2026 14:15:38 +0200 Subject: [PATCH 1/3] test(ladder): pin the float fields of netted rows Assert legacy rows carry rate 1 and their amount, the rate rounds down from 1/10, 1/3 and 1/2, a stored rate of 1 is accepted and a rate below float range clamps to the smallest nonzero float. Co-Authored-By: Claude Opus 5.5 --- internal/config/ladder_provenance_test.go | 29 +++++++++++++++++++ ..._postgres_ladder_basis_integration_test.go | 7 ++++- 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/internal/config/ladder_provenance_test.go b/internal/config/ladder_provenance_test.go index 6652b325..8ef9504f 100644 --- a/internal/config/ladder_provenance_test.go +++ b/internal/config/ladder_provenance_test.go @@ -3,6 +3,7 @@ package config import ( "math" "math/big" + "strings" "testing" "github.com/stretchr/testify/require" @@ -80,3 +81,31 @@ func TestAssumeOnDemandIfUnknown_OnlyForUnprovenBasis(t *testing.T) { _, err := nettingOnDemand("1", &LadderTrancheDB{}) require.Error(t, err, "netting still refuses an unknown basis") } + +func TestNewLadderInFlightRow_FloatFieldsFeedAllocate(t *testing.T) { + od, native := LadderAmountOnDemand, LadderAmountCommitment + legacy, err := newLadderInFlightRow("12.5", &LadderTrancheDB{AmountBasis: &od}) + require.NoError(t, err) + require.Equal(t, 1.0, legacy.Rate) + require.Equal(t, 12.5, legacy.CommitmentUSDPerHour) + + // Tight bound: the rate is the largest float not above the exact rate. + for _, text := range []string{"1/10", "1/3", "1/2"} { + exact, ok := new(big.Rat).SetString(text) + require.True(t, ok) + row, err := newLadderInFlightRow("1", &LadderTrancheDB{AmountBasis: &native, CommitmentRate: &text}) + require.NoError(t, err, text) + require.LessOrEqual(t, new(big.Rat).SetFloat64(row.Rate).Cmp(exact), 0, text) + require.Greater(t, new(big.Rat).SetFloat64(math.Nextafter(row.Rate, 2)).Cmp(exact), 0, text) + } + + one := "1" + row, err := newLadderInFlightRow("3", &LadderTrancheDB{AmountBasis: &native, CommitmentRate: &one}) + require.NoError(t, err) + require.Equal(t, 1.0, row.Rate) + + tiny := "1/1" + strings.Repeat("0", 400) + row, err = newLadderInFlightRow("1", &LadderTrancheDB{AmountBasis: &native, CommitmentRate: &tiny}) + require.NoError(t, err) + require.Equal(t, math.SmallestNonzeroFloat64, row.Rate, "a rate below float range must not become 0") +} diff --git a/internal/config/store_postgres_ladder_basis_integration_test.go b/internal/config/store_postgres_ladder_basis_integration_test.go index ec8a9f47..ef70e8ee 100644 --- a/internal/config/store_postgres_ladder_basis_integration_test.go +++ b/internal/config/store_postgres_ladder_basis_integration_test.go @@ -99,7 +99,12 @@ func TestLadderNetting_LegacyOnlyRowsKeepTheExactTotalBitForBit(t *testing.T) { } want, err := nettingFloat(exact) require.NoError(t, err) - got := mustOnDemand(t, plannedRows(t, f)) + rows := plannedRows(t, f) + require.Len(t, rows, len(amounts)) + for _, r := range rows { + require.Equal(t, 1.0, r.Rate, "legacy on-demand rows carry rate 1") + } + got := mustOnDemand(t, rows) require.Equal(t, math.Float64bits(want), math.Float64bits(got)) } From 6122e440c5fbe709a6120fd8ea0bf50f1cc9e4a2 Mon Sep 17 00:00:00 2001 From: Cristian Magherusan-Stanciu Date: Fri, 16 Oct 2026 14:17:01 +0200 Subject: [PATCH 2/3] docs(ladder): name settledLadderRows where comments cited the removed function Co-Authored-By: Claude Opus 5.5 --- internal/config/store_postgres_ladder_lock.go | 2 +- .../config/store_postgres_ladder_settled_integration_test.go | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/internal/config/store_postgres_ladder_lock.go b/internal/config/store_postgres_ladder_lock.go index f0bdeec3..629acd8b 100644 --- a/internal/config/store_postgres_ladder_lock.go +++ b/internal/config/store_postgres_ladder_lock.go @@ -99,7 +99,7 @@ func acquireLadderConfigLock(ctx context.Context, tx pgx.Tx, configID string) er // runs' windows never overlap. const ladderSettleSkewMargin = time.Minute -// ladderSettleAnchor is the anchor every caller passes to settledLadderCommit +// ladderSettleAnchor is the anchor every caller passes to settledLadderRows // for an existing-commitment read taken at or after asOf. func ladderSettleAnchor(asOf time.Time) time.Time { return asOf.Add(-ladderSettleSkewMargin) } diff --git a/internal/config/store_postgres_ladder_settled_integration_test.go b/internal/config/store_postgres_ladder_settled_integration_test.go index 26c1a79b..ddd26410 100644 --- a/internal/config/store_postgres_ladder_settled_integration_test.go +++ b/internal/config/store_postgres_ladder_settled_integration_test.go @@ -21,7 +21,7 @@ import ( // TestPostgresStoreDB_LadderSettledAt covers the stamp written by // TransitionLadderTrancheTx, the CHECK that rejects an unstamped completed -// row, and the boundaries of settledLadderCommit. Migration behavior is in +// row, and the boundaries of settledLadderRows. Migration behavior is in // the migrations package (000117). Set LADDER_SETTLED_TEST_DATABASE_URL to a // dedicated pr842a_* database to run it without Docker. func TestPostgresStoreDB_LadderSettledAt(t *testing.T) { @@ -85,7 +85,7 @@ func TestPostgresStoreDB_LadderSettledAt(t *testing.T) { requireCheckViolation(t, err) }) - t.Run("settledLadderCommit boundaries", func(t *testing.T) { + t.Run("settledLadderRows boundaries", func(t *testing.T) { otherCfg := seedLadderConfigWithExtID(ctx, t, store, "333333333333") anchor := time.Now().UTC().Truncate(time.Microsecond) add := func(status ladder.TrancheStatus, amount float64, settled *time.Time) { From 157cf34169337fb66c11d10728d3e0ed2b6db668 Mon Sep 17 00:00:00 2001 From: Cristian Magherusan-Stanciu Date: Fri, 16 Oct 2026 16:02:57 +0200 Subject: [PATCH 3/3] test(ladder): avoid shadowing err in the rate bound loop Co-Authored-By: Claude Opus 5.5 --- internal/config/ladder_provenance_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/config/ladder_provenance_test.go b/internal/config/ladder_provenance_test.go index 8ef9504f..cb2c16ec 100644 --- a/internal/config/ladder_provenance_test.go +++ b/internal/config/ladder_provenance_test.go @@ -93,8 +93,8 @@ func TestNewLadderInFlightRow_FloatFieldsFeedAllocate(t *testing.T) { for _, text := range []string{"1/10", "1/3", "1/2"} { exact, ok := new(big.Rat).SetString(text) require.True(t, ok) - row, err := newLadderInFlightRow("1", &LadderTrancheDB{AmountBasis: &native, CommitmentRate: &text}) - require.NoError(t, err, text) + row, rowErr := newLadderInFlightRow("1", &LadderTrancheDB{AmountBasis: &native, CommitmentRate: &text}) + require.NoError(t, rowErr, text) require.LessOrEqual(t, new(big.Rat).SetFloat64(row.Rate).Cmp(exact), 0, text) require.Greater(t, new(big.Rat).SetFloat64(math.Nextafter(row.Rate, 2)).Cmp(exact), 0, text) }