Skip to content

Commit

Permalink
Merge pull request #92 from kaleido-io/closures-for-context
Browse files Browse the repository at this point in the history
Provide a way for policy engines to pass additional state to callbacks
  • Loading branch information
peterbroadhurst committed Jul 10, 2023
2 parents a68dc16 + 5026fa0 commit 6ff003a
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
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

0 comments on commit 6ff003a

Please sign in to comment.