-
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.
- Loading branch information
Showing
14 changed files
with
457 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: 100, | ||
} | ||
|
||
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.