From 5026fa0ccef67e3e58b323a9c161f406659760a6 Mon Sep 17 00:00:00 2001 From: Peter Broadhurst Date: Mon, 3 Jul 2023 16:28:40 -0400 Subject: [PATCH] Provide a way for policy engines to pass additional state to callbacks Signed-off-by: Peter Broadhurst --- pkg/apitypes/managed_tx.go | 4 ++++ pkg/fftm/transaction_events_handler.go | 14 ++++++++++++-- pkg/fftm/transaction_events_handler_test.go | 1 + 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/pkg/apitypes/managed_tx.go b/pkg/apitypes/managed_tx.go index 1a7ee1de..cdc13080 100644 --- a/pkg/apitypes/managed_tx.go +++ b/pkg/apitypes/managed_tx.go @@ -273,6 +273,10 @@ type ManagedTransactionEvent struct { Type ManagedTransactionEventType Tx *ManagedTX Receipt *ffcapi.TransactionReceiptResponse + // ReceiptHandler can be passed on the event as a closure with extra variables + ReceiptHandler func(ctx context.Context, txID string, receipt *ffcapi.TransactionReceiptResponse) error + // ConfirmationHandler can be passed on the event as a closure with extra variables + ConfirmationHandler func(ctx context.Context, txID string, notification *ConfirmationsNotification) error } type ReceiptRecord struct { diff --git a/pkg/fftm/transaction_events_handler.go b/pkg/fftm/transaction_events_handler.go index 70bc0592..316f0be5 100644 --- a/pkg/fftm/transaction_events_handler.go +++ b/pkg/fftm/transaction_events_handler.go @@ -53,18 +53,28 @@ func (eh *ManagedTransactionEventHandler) HandleEvent(_ context.Context, e apity case apitypes.ManagedTXDeleted: eh.sendWSReply(e.Tx, nil /* receipt never sent with delete */) case apitypes.ManagedTXTransactionHashAdded: + if e.ConfirmationHandler == nil { + // e.ConfirmationHandler was added to support extra variable passing to callbacks. + // Default to the previous interface on the TxHandler + e.ConfirmationHandler = eh.TxHandler.HandleTransactionConfirmations + } + if e.ReceiptHandler == nil { + // e.ReceiptHandler was added to support extra variable passing to callbacks. + // Default to the previous interface on the TxHandler + e.ReceiptHandler = eh.TxHandler.HandleTransactionReceiptReceived + } txID := e.Tx.ID return eh.ConfirmationManager.Notify(&confirmations.Notification{ NotificationType: confirmations.NewTransaction, Transaction: &confirmations.TransactionInfo{ TransactionHash: e.Tx.TransactionHash, Receipt: func(ctx context.Context, receipt *ffcapi.TransactionReceiptResponse) { - if err := eh.TxHandler.HandleTransactionReceiptReceived(ctx, txID, receipt); err != nil { + if err := e.ReceiptHandler(ctx, txID, receipt); err != nil { log.L(ctx).Errorf("Receipt for transaction %s at nonce %s / %d - hash: %s was not handled due to %s", e.Tx.ID, e.Tx.TransactionHeaders.From, e.Tx.Nonce.Int64(), e.Tx.TransactionHash, err.Error()) } }, Confirmations: func(ctx context.Context, notification *apitypes.ConfirmationsNotification) { - if err := eh.TxHandler.HandleTransactionConfirmations(ctx, txID, notification); err != nil { + if err := e.ConfirmationHandler(ctx, txID, notification); err != nil { log.L(ctx).Errorf("Confirmation for transaction %s at nonce %s / %d - hash: %s was not handled due to %s", e.Tx.ID, e.Tx.TransactionHeaders.From, e.Tx.Nonce.Int64(), e.Tx.TransactionHash, err.Error()) } }, diff --git a/pkg/fftm/transaction_events_handler_test.go b/pkg/fftm/transaction_events_handler_test.go index 22084d87..d0ea6bcc 100644 --- a/pkg/fftm/transaction_events_handler_test.go +++ b/pkg/fftm/transaction_events_handler_test.go @@ -112,6 +112,7 @@ func TestHandleTransactionHashUpdateEventAddHash(t *testing.T) { return n.NotificationType == confirmations.NewTransaction })).Return(nil).Once() eh.ConfirmationManager = mcm + eh.TxHandler = &txhandlermocks.TransactionHandler{} testTx := &apitypes.ManagedTX{ ID: fmt.Sprintf("ns1:%s", fftypes.NewUUID()), Created: fftypes.Now(),