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

Provide a way for policy engines to pass additional state to callbacks #92

Merged
merged 1 commit into from
Jul 10, 2023
Merged
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
4 changes: 4 additions & 0 deletions pkg/apitypes/managed_tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
14 changes: 12 additions & 2 deletions pkg/fftm/transaction_events_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
},
Expand Down
1 change: 1 addition & 0 deletions pkg/fftm/transaction_events_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down