Skip to content
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

rollback invalid payments #1372

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
16 changes: 8 additions & 8 deletions core/meterer/meterer.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,22 +214,22 @@ func (m *Meterer) ServeOnDemandRequest(ctx context.Context, header core.PaymentM

// Compute paymentCharged at payment time
paymentCharged := PaymentCharged(symbolsCharged, m.ChainPaymentState.GetPricePerSymbol())

err = m.OffchainStore.AddOnDemandPayment(ctx, header, paymentCharged)
if err != nil {
return fmt.Errorf("failed to update cumulative payment: %w", err)
}
// Validate payments attached
err = m.ValidatePayment(ctx, header, onDemandPayment, paymentCharged)
Copy link
Contributor

Choose a reason for hiding this comment

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

comment: ValidatePayment is executed after AddOnDemandPayment to prevent race condition

if err != nil {
// No tolerance for incorrect payment amounts; no rollbacks
dbErr := m.OffchainStore.RemoveOnDemandPayment(ctx, header.AccountID, header.CumulativePayment)
if dbErr != nil {
return dbErr
}
return fmt.Errorf("invalid on-demand payment: %w", err)
}

err = m.OffchainStore.AddOnDemandPayment(ctx, header, paymentCharged)
if err != nil {
return fmt.Errorf("failed to update cumulative payment: %w", err)
}

// Update bin usage atomically and check against bin capacity
if err := m.IncrementGlobalBinUsage(ctx, uint64(symbolsCharged), receivedAt); err != nil {
//TODO: conditionally remove the payment based on the error type (maybe if the error is store-op related)
dbErr := m.OffchainStore.RemoveOnDemandPayment(ctx, header.AccountID, header.CumulativePayment)
if dbErr != nil {
return dbErr
Expand Down
13 changes: 10 additions & 3 deletions core/meterer/meterer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ func TestMetererOnDemand(t *testing.T) {
header = createPaymentHeader(now.UnixNano(), big.NewInt(1), accountID1)
_, err = mt.MeterRequest(ctx, *header, 1000, quorumNumbers, now)
assert.ErrorContains(t, err, "insufficient cumulative payment increment")
// Not record for invalid payment
// rollback for invalid payments
result, err := dynamoClient.Query(ctx, ondemandTableName, "AccountID = :account", commondynamodb.ExpressionValues{
":account": &types.AttributeValueMemberS{
Value: accountID1.Hex(),
Expand Down Expand Up @@ -361,7 +361,7 @@ func TestMetererOnDemand(t *testing.T) {
// test cannot insert cumulative payment in out of order
symbolsCharged = mt.SymbolsCharged(uint64(50))
header = createPaymentHeader(now.UnixNano(), meterer.PaymentCharged(symbolsCharged, mt.ChainPaymentState.GetPricePerSymbol()), accountID2)
_, err = mt.MeterRequest(ctx, *header, 50, quorumNumbers, now)
_, err = mt.MeterRequest(ctx, *header, 1, quorumNumbers, now)
assert.ErrorContains(t, err, "invalid on-demand payment: breaking cumulative payment invariants")

result, err = dynamoClient.Query(ctx, ondemandTableName, "AccountID = :account", commondynamodb.ExpressionValues{
Expand All @@ -370,11 +370,18 @@ func TestMetererOnDemand(t *testing.T) {
}})
assert.NoError(t, err)
assert.Equal(t, numValidPayments, len(result))

// with rollback of invalid payments, users cannot cheat by inserting an invalid cumulative payment
symbolsCharged = mt.SymbolsCharged(uint64(30))
header = createPaymentHeader(now.UnixNano(), meterer.PaymentCharged(symbolsCharged, mt.ChainPaymentState.GetPricePerSymbol()), accountID2)
_, err = mt.MeterRequest(ctx, *header, 30, quorumNumbers, now)
assert.ErrorContains(t, err, "invalid on-demand payment: breaking cumulative payment invariants")

// test failed global rate limit (previously payment recorded: 2, global limit: 1009)
header = createPaymentHeader(now.UnixNano(), big.NewInt(0).Add(previousCumulativePayment, meterer.PaymentCharged(1010, mt.ChainPaymentState.GetPricePerSymbol())), accountID1)
_, err = mt.MeterRequest(ctx, *header, 1010, quorumNumbers, now)
assert.ErrorContains(t, err, "failed global rate limiting")
// Correct rollback
// Correct rollback when exceeding global rate limit
result, err = dynamoClient.Query(ctx, ondemandTableName, "AccountID = :account", commondynamodb.ExpressionValues{
":account": &types.AttributeValueMemberS{
Value: accountID2.Hex(),
Expand Down
Loading