Skip to content

Commit bf326ac

Browse files
committed
Improve cleanup of auto-return actions to free up nonces more quickly
1 parent 8c96823 commit bf326ac

File tree

7 files changed

+85
-23
lines changed

7 files changed

+85
-23
lines changed

pkg/code/async/account/gift_card.go

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -98,14 +98,14 @@ func (p *service) maybeInitiateGiftCardAutoReturn(ctx context.Context, accountIn
9898
log.Trace("gift card is claimed and will be removed from worker queue")
9999

100100
// Cleanup anything related to gift card auto-return, since it cannot be scheduled
101-
err = initiateProcessToCleanupGiftCardAutoReturn(ctx, p.data, giftCardVaultAccount)
101+
err = InitiateProcessToCleanupGiftCardAutoReturn(ctx, p.data, giftCardVaultAccount)
102102
if err != nil {
103103
log.WithError(err).Warn("failure cleaning up auto-return action")
104104
return err
105105
}
106106

107107
// Gift card is claimed, so take it out of the worker queue.
108-
return markAutoReturnCheckComplete(ctx, p.data, accountInfoRecord)
108+
return MarkAutoReturnCheckComplete(ctx, p.data, accountInfoRecord)
109109
} else if err != action.ErrActionNotFound {
110110
return err
111111
}
@@ -129,7 +129,9 @@ func (p *service) maybeInitiateGiftCardAutoReturn(ctx context.Context, accountIn
129129
log.WithError(err).Warn("failure initiating process to return gift card balance to issuer")
130130
return err
131131
}
132-
return markAutoReturnCheckComplete(ctx, p.data, accountInfoRecord)
132+
133+
// Gift card is auto-returned, so take it out of the worker queue
134+
return MarkAutoReturnCheckComplete(ctx, p.data, accountInfoRecord)
133135
}
134136

135137
// Note: This is the first instance of handling a conditional action, and could be
@@ -201,27 +203,30 @@ func InitiateProcessToAutoReturnGiftCard(ctx context.Context, data code_data.Pro
201203
})
202204
}
203205

204-
func initiateProcessToCleanupGiftCardAutoReturn(ctx context.Context, data code_data.Provider, giftCardVaultAccount *common.Account) error {
205-
autoReturnAction, err := data.GetGiftCardAutoReturnAction(ctx, giftCardVaultAccount.PublicKey().ToBase58())
206-
if err != nil {
207-
return err
208-
}
206+
// todo: This probably belongs somewhere more common
207+
func InitiateProcessToCleanupGiftCardAutoReturn(ctx context.Context, data code_data.Provider, giftCardVaultAccount *common.Account) error {
208+
return data.ExecuteInTx(ctx, sql.LevelDefault, func(ctx context.Context) error {
209+
autoReturnAction, err := data.GetGiftCardAutoReturnAction(ctx, giftCardVaultAccount.PublicKey().ToBase58())
210+
if err != nil {
211+
return err
212+
}
209213

210-
autoReturnFulfillment, err := data.GetAllFulfillmentsByAction(ctx, autoReturnAction.Intent, autoReturnAction.ActionId)
211-
if err != nil {
212-
return err
213-
}
214+
autoReturnFulfillment, err := data.GetAllFulfillmentsByAction(ctx, autoReturnAction.Intent, autoReturnAction.ActionId)
215+
if err != nil {
216+
return err
217+
}
214218

215-
err = markActionAsRevoked(ctx, data, autoReturnAction)
216-
if err != nil {
217-
return err
218-
}
219+
err = markActionAsRevoked(ctx, data, autoReturnAction)
220+
if err != nil {
221+
return err
222+
}
219223

220-
// The sequencer will handle state transition and any cleanup
221-
return markFulfillmentAsActivelyScheduled(ctx, data, autoReturnFulfillment[0])
224+
// The sequencer will handle state transition and any cleanup
225+
return markFulfillmentAsActivelyScheduled(ctx, data, autoReturnFulfillment[0])
226+
})
222227
}
223228

224-
func markAutoReturnCheckComplete(ctx context.Context, data code_data.Provider, record *account.Record) error {
229+
func MarkAutoReturnCheckComplete(ctx context.Context, data code_data.Provider, record *account.Record) error {
225230
if !record.RequiresAutoReturnCheck {
226231
return nil
227232
}

pkg/code/async/sequencer/fulfillment_handler.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -556,7 +556,12 @@ func (h *NoPrivacyWithdrawFulfillmentHandler) OnSuccess(ctx context.Context, ful
556556
return err
557557
}
558558

559-
return markTimelockClosed(ctx, h.data, fulfillmentRecord.Source, txnRecord.Slot)
559+
err = markTimelockClosed(ctx, h.data, fulfillmentRecord.Source, txnRecord.Slot)
560+
if err != nil {
561+
return err
562+
}
563+
564+
return maybeCleanupAutoReturnAction(ctx, h.data, fulfillmentRecord.Source)
560565
}
561566

562567
func (h *NoPrivacyWithdrawFulfillmentHandler) OnFailure(ctx context.Context, fulfillmentRecord *fulfillment.Record, txnRecord *transaction.Record) (recovered bool, err error) {
@@ -752,7 +757,7 @@ func isTokenAccountOnBlockchain(ctx context.Context, data code_data.Provider, ad
752757
}
753758

754759
// todo: simplify initialization of fulfillment handlers across service and contextual scheduler
755-
func getFulfillmentHandlers(data code_data.Provider, vmIndexerClient indexerpb.IndexerClient, configProvider ConfigProvider) map[fulfillment.Type]FulfillmentHandler {
760+
func getFulfillmentHandlers(data code_data.Provider, vmIndexerClient indexerpb.IndexerClient) map[fulfillment.Type]FulfillmentHandler {
756761
handlersByType := make(map[fulfillment.Type]FulfillmentHandler)
757762
handlersByType[fulfillment.InitializeLockedTimelockAccount] = NewInitializeLockedTimelockAccountFulfillmentHandler(data)
758763
handlersByType[fulfillment.NoPrivacyTransferWithAuthority] = NewNoPrivacyTransferWithAuthorityFulfillmentHandler(data, vmIndexerClient)

pkg/code/async/sequencer/scheduler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ func NewContextualScheduler(data code_data.Provider, indexerClient indexerpb.Ind
5555
log: logrus.StandardLogger().WithField("type", "sequencer/scheduler/contextual"),
5656
data: data,
5757
conf: configProvider(),
58-
handlersByType: getFulfillmentHandlers(data, indexerClient, configProvider),
58+
handlersByType: getFulfillmentHandlers(data, indexerClient),
5959
includeSubsidizerChecks: true,
6060
}
6161
}

pkg/code/async/sequencer/service.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func New(data code_data.Provider, scheduler Scheduler, vmIndexerClient indexerpb
4040
data: data,
4141
scheduler: scheduler,
4242
vmIndexerClient: vmIndexerClient,
43-
fulfillmentHandlersByType: getFulfillmentHandlers(data, vmIndexerClient, configProvider),
43+
fulfillmentHandlersByType: getFulfillmentHandlers(data, vmIndexerClient),
4444
actionHandlersByType: getActionHandlers(data),
4545
intentHandlersByType: getIntentHandlers(data),
4646
}

pkg/code/async/sequencer/timelock.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ func markTimelockLocked(ctx context.Context, data code_data.Provider, vault stri
1919
return err
2020
}
2121

22+
if record.VaultState == timelock_token_v1.StateLocked {
23+
return nil
24+
}
25+
2226
record.VaultState = timelock_token_v1.StateLocked
2327
record.Block = slot
2428

@@ -35,6 +39,10 @@ func markTimelockClosed(ctx context.Context, data code_data.Provider, vault stri
3539
return err
3640
}
3741

42+
if record.VaultState == timelock_token_v1.StateClosed {
43+
return nil
44+
}
45+
3846
record.VaultState = timelock_token_v1.StateClosed
3947
if record.Block > slot {
4048
// Potential conflict with unlock state detection, force a move to close at the next block

pkg/code/async/sequencer/utils.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,12 @@ import (
55

66
"github.com/pkg/errors"
77

8+
commonpb "github.com/code-payments/code-protobuf-api/generated/go/common/v1"
9+
10+
async_account "github.com/code-payments/code-server/pkg/code/async/account"
11+
"github.com/code-payments/code-server/pkg/code/common"
812
code_data "github.com/code-payments/code-server/pkg/code/data"
13+
"github.com/code-payments/code-server/pkg/code/data/action"
914
"github.com/code-payments/code-server/pkg/code/data/fulfillment"
1015
"github.com/code-payments/code-server/pkg/code/data/nonce"
1116
"github.com/code-payments/code-server/pkg/code/data/transaction"
@@ -291,3 +296,38 @@ func (p *service) markVirtualNonceReleasedDueToSubmittedTransaction(ctx context.
291296
nonceRecord.State = nonce.StateReleased
292297
return p.data.SaveNonce(ctx, nonceRecord)
293298
}
299+
300+
func maybeCleanupAutoReturnAction(ctx context.Context, data code_data.Provider, vaultAddress string) error {
301+
vaultAccount, err := common.NewAccountFromPublicKeyString(vaultAddress)
302+
if err != nil {
303+
return err
304+
}
305+
306+
accountInfoRecord, err := data.GetAccountInfoByTokenAddress(ctx, vaultAccount.PublicKey().ToBase58())
307+
if err != nil {
308+
return err
309+
}
310+
311+
if accountInfoRecord.AccountType != commonpb.AccountType_REMOTE_SEND_GIFT_CARD {
312+
return nil
313+
}
314+
315+
_, err = data.GetGiftCardClaimedAction(ctx, vaultAccount.PublicKey().ToBase58())
316+
if err == action.ErrActionNotFound {
317+
// Gift card isn't claimed, so it must've been auto-returned if it was closed
318+
return nil
319+
} else if err != nil {
320+
return err
321+
}
322+
323+
err = async_account.InitiateProcessToCleanupGiftCardAutoReturn(ctx, data, vaultAccount)
324+
if err != nil {
325+
return err
326+
}
327+
328+
// It's ok if this fails, the auto-return worker will just process this account
329+
// idempotently at a later time
330+
async_account.MarkAutoReturnCheckComplete(ctx, data, accountInfoRecord)
331+
332+
return nil
333+
}

pkg/code/server/transaction/intent.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -999,6 +999,10 @@ func (s *transactionServer) VoidGiftCard(ctx context.Context, req *transactionpb
999999
return nil, status.Error(codes.Internal, "")
10001000
}
10011001

1002+
// It's ok if this fails, the auto-return worker will just process this account
1003+
// idempotently at a later time
1004+
async_account.MarkAutoReturnCheckComplete(ctx, s.data, accountInfoRecord)
1005+
10021006
return &transactionpb.VoidGiftCardResponse{
10031007
Result: transactionpb.VoidGiftCardResponse_OK,
10041008
}, nil

0 commit comments

Comments
 (0)