Skip to content
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
29 changes: 29 additions & 0 deletions internal/config/ladder_provenance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package config
import (
"math"
"math/big"
"strings"
"testing"

"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -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, 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)
}

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")
}
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}

Expand Down
2 changes: 1 addition & 1 deletion internal/config/store_postgres_ladder_lock.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
Loading