-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
### TL;DR - Added database support for tracking unsettled payer usage - Handles usage tracking in a transaction alongside writing the gateway envelope ### What changed? - Created new `unsettled_usage` table to track spending between payers and originators - Added SQL queries for incrementing and retrieving unsettled usage - Implemented `MinutesSinceEpoch` utility functions for time tracking - Added `RunInTxWithResult` for transaction handling with return values - Created database migration scripts for the new table structure ### How to test? - Run the new test cases in `queries_test.go`: - `TestIncrementUnsettledUsage`: Verifies incremental spending updates - `TestGetUnsettledUsage`: Validates retrieval of usage data with time filtering - Execute database migrations to verify table creation and index setup ### Why make this change? This change enables tracking of real-time usage and spending between payers and originators at a granular level, which is essential for accurate billing and usage monitoring in the system. The minute-level tracking allows for precise usage accounting and settlement processes. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **New Features** - Introduced database operations to update and retrieve unsettled usage data. - Added a new data structure for representing unsettled usage records. - Enhanced transaction handling to return query results. - Provided utilities for time calculations and random number generation. - Delivered migration scripts to create the unsettled usage table with performance improvements. - Integrated new methods for inserting gateway envelopes and managing unsettled usage. - Added a method to convert nanoseconds to a time object. - **Tests** - Added automated tests to verify the functionality of unsettled usage updates and retrievals. - Implemented tests for inserting gateway envelopes and handling concurrent operations. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
- Loading branch information
Showing
14 changed files
with
448 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package db | ||
|
||
import ( | ||
"context" | ||
"database/sql" | ||
|
||
"github.com/xmtp/xmtpd/pkg/db/queries" | ||
) | ||
|
||
// InsertGatewayEnvelopeAndIncrementUnsettledUsage inserts a gateway envelope and increments the unsettled usage for the payer. | ||
// It returns the number of rows inserted. | ||
func InsertGatewayEnvelopeAndIncrementUnsettledUsage( | ||
ctx context.Context, | ||
db *sql.DB, | ||
insertParams queries.InsertGatewayEnvelopeParams, | ||
incrementParams queries.IncrementUnsettledUsageParams, | ||
) (int64, error) { | ||
return RunInTxWithResult( | ||
ctx, | ||
db, | ||
&sql.TxOptions{}, | ||
func(ctx context.Context, txQueries *queries.Queries) (int64, error) { | ||
numInserted, err := txQueries.InsertGatewayEnvelope(ctx, insertParams) | ||
if err != nil { | ||
return 0, err | ||
} | ||
// If the numInserted is 0 it means the envelope already exists | ||
// and we don't need to increment the unsettled usage | ||
if numInserted == 0 { | ||
return 0, nil | ||
} | ||
|
||
err = txQueries.IncrementUnsettledUsage(ctx, incrementParams) | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
return numInserted, nil | ||
}, | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
package db | ||
|
||
import ( | ||
"context" | ||
"sync" | ||
"sync/atomic" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
"github.com/xmtp/xmtpd/pkg/db/queries" | ||
"github.com/xmtp/xmtpd/pkg/testutils" | ||
) | ||
|
||
func buildParams( | ||
payerID int32, | ||
originatorID int32, | ||
sequenceID int64, | ||
spendPicodollars int64, | ||
) (queries.InsertGatewayEnvelopeParams, queries.IncrementUnsettledUsageParams) { | ||
insertParams := queries.InsertGatewayEnvelopeParams{ | ||
OriginatorNodeID: originatorID, | ||
OriginatorSequenceID: sequenceID, | ||
Topic: testutils.RandomBytes(32), | ||
OriginatorEnvelope: testutils.RandomBytes(100), | ||
PayerID: NullInt32(payerID), | ||
} | ||
|
||
incrementParams := queries.IncrementUnsettledUsageParams{ | ||
PayerID: payerID, | ||
OriginatorID: originatorID, | ||
MinutesSinceEpoch: 1, | ||
SpendPicodollars: spendPicodollars, | ||
} | ||
|
||
return insertParams, incrementParams | ||
} | ||
|
||
func TestInsertAndIncrement(t *testing.T) { | ||
ctx := context.Background() | ||
db, _, cleanup := testutils.NewDB(t, ctx) | ||
defer cleanup() | ||
|
||
querier := queries.New(db) | ||
// Create a payer | ||
payerID := testutils.CreatePayer(t, db, testutils.RandomAddress().Hex()) | ||
originatorID := testutils.RandomInt32() | ||
sequenceID := int64(10) | ||
|
||
insertParams, incrementParams := buildParams(payerID, originatorID, sequenceID, 100) | ||
|
||
numInserted, err := InsertGatewayEnvelopeAndIncrementUnsettledUsage( | ||
ctx, | ||
db, | ||
insertParams, | ||
incrementParams, | ||
) | ||
require.NoError(t, err) | ||
require.Equal(t, numInserted, int64(1)) | ||
|
||
payerSpend, err := querier.GetPayerUnsettledUsage( | ||
ctx, | ||
queries.GetPayerUnsettledUsageParams{PayerID: payerID}, | ||
) | ||
require.NoError(t, err) | ||
require.Equal(t, payerSpend, int64(100)) | ||
} | ||
|
||
func TestPayerMustExist(t *testing.T) { | ||
ctx := context.Background() | ||
db, _, cleanup := testutils.NewDB(t, ctx) | ||
defer cleanup() | ||
|
||
payerID := testutils.RandomInt32() | ||
originatorID := testutils.RandomInt32() | ||
sequenceID := int64(10) | ||
|
||
insertParams, incrementParams := buildParams(payerID, originatorID, sequenceID, 100) | ||
|
||
_, err := InsertGatewayEnvelopeAndIncrementUnsettledUsage( | ||
ctx, | ||
db, | ||
insertParams, | ||
incrementParams, | ||
) | ||
require.Error(t, err) | ||
} | ||
|
||
func TestInsertAndIncrementParallel(t *testing.T) { | ||
ctx := context.Background() | ||
db, _, cleanup := testutils.NewDB(t, ctx) | ||
defer cleanup() | ||
|
||
querier := queries.New(db) | ||
// Create a payer | ||
payerID := testutils.CreatePayer(t, db, testutils.RandomAddress().Hex()) | ||
originatorID := testutils.RandomInt32() | ||
sequenceID := int64(10) | ||
numberOfInserts := 20 | ||
|
||
insertParams, incrementParams := buildParams(payerID, originatorID, sequenceID, 100) | ||
|
||
var wg sync.WaitGroup | ||
|
||
totalInserted := int64(0) | ||
|
||
attemptInsert := func() { | ||
defer wg.Done() | ||
numInserted, err := InsertGatewayEnvelopeAndIncrementUnsettledUsage( | ||
ctx, | ||
db, | ||
insertParams, | ||
incrementParams, | ||
) | ||
require.NoError(t, err) | ||
atomic.AddInt64(&totalInserted, numInserted) | ||
} | ||
|
||
for range numberOfInserts { | ||
wg.Add(1) | ||
go attemptInsert() | ||
} | ||
|
||
wg.Wait() | ||
|
||
require.Equal(t, totalInserted, int64(1)) | ||
|
||
payerSpend, err := querier.GetPayerUnsettledUsage( | ||
ctx, | ||
queries.GetPayerUnsettledUsageParams{PayerID: payerID}, | ||
) | ||
require.NoError(t, err) | ||
require.Equal(t, payerSpend, int64(100)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.