Skip to content
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
7 changes: 6 additions & 1 deletion internal/events/operation_update.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright © 2021 Kaleido, Inc.
// Copyright © 2022 Kaleido, Inc.
//
// SPDX-License-Identifier: Apache-2.0
//
Expand Down Expand Up @@ -39,6 +39,11 @@ func (em *eventManager) OperationUpdate(plugin fftypes.Named, operationID *fftyp

// Special handling for OpTypeTokenTransfer, which writes an event when it fails
if op.Type == fftypes.OpTypeTokenTransfer && txState == fftypes.OpStatusFailed {
txUpdate := database.TransactionQueryFactory.NewUpdate(em.ctx).Set("status", txState)
if err := em.database.UpdateTransaction(em.ctx, op.Transaction, txUpdate); err != nil {
return err
}

event := fftypes.NewEvent(fftypes.EventTypeTransferOpFailed, op.Namespace, op.ID)
if err := em.database.InsertEvent(em.ctx, event); err != nil {
return err
Expand Down
46 changes: 35 additions & 11 deletions internal/events/operation_update_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright © 2021 Kaleido, Inc.
// Copyright © 2022 Kaleido, Inc.
//
// SPDX-License-Identifier: Apache-2.0
//
Expand Down Expand Up @@ -86,48 +86,72 @@ func TestOperationUpdateTransferFail(t *testing.T) {
mdi := em.database.(*databasemocks.Plugin)
mbi := &blockchainmocks.Plugin{}

opID := fftypes.NewUUID()
op := &fftypes.Operation{
ID: opID,
ID: fftypes.NewUUID(),
Type: fftypes.OpTypeTokenTransfer,
Namespace: "ns1",
}

mdi.On("GetOperationByID", em.ctx, opID).Return(op, nil)
mdi.On("UpdateOperation", em.ctx, opID, mock.Anything).Return(nil)
mdi.On("GetOperationByID", em.ctx, op.ID).Return(op, nil)
mdi.On("UpdateOperation", em.ctx, op.ID, mock.Anything).Return(nil)
mdi.On("UpdateTransaction", em.ctx, op.Transaction, mock.Anything).Return(nil)
mdi.On("InsertEvent", em.ctx, mock.MatchedBy(func(e *fftypes.Event) bool {
return e.Type == fftypes.EventTypeTransferOpFailed && e.Namespace == "ns1"
})).Return(nil)

info := fftypes.JSONObject{"some": "info"}
err := em.OperationUpdate(mbi, opID, fftypes.OpStatusFailed, "some error", info)
err := em.OperationUpdate(mbi, op.ID, fftypes.OpStatusFailed, "some error", info)
assert.NoError(t, err)

mdi.AssertExpectations(t)
mbi.AssertExpectations(t)
}

func TestOperationUpdateTransferTransactionFail(t *testing.T) {
em, cancel := newTestEventManager(t)
defer cancel()
mdi := em.database.(*databasemocks.Plugin)
mbi := &blockchainmocks.Plugin{}

op := &fftypes.Operation{
ID: fftypes.NewUUID(),
Type: fftypes.OpTypeTokenTransfer,
Namespace: "ns1",
}

mdi.On("GetOperationByID", em.ctx, op.ID).Return(op, nil)
mdi.On("UpdateOperation", em.ctx, op.ID, mock.Anything).Return(nil)
mdi.On("UpdateTransaction", em.ctx, op.Transaction, mock.Anything).Return(fmt.Errorf("pop"))

info := fftypes.JSONObject{"some": "info"}
err := em.OperationUpdate(mbi, op.ID, fftypes.OpStatusFailed, "some error", info)
assert.EqualError(t, err, "pop")

mdi.AssertExpectations(t)
mbi.AssertExpectations(t)
}

func TestOperationUpdateTransferEventFail(t *testing.T) {
em, cancel := newTestEventManager(t)
defer cancel()
mdi := em.database.(*databasemocks.Plugin)
mbi := &blockchainmocks.Plugin{}

opID := fftypes.NewUUID()
op := &fftypes.Operation{
ID: opID,
ID: fftypes.NewUUID(),
Type: fftypes.OpTypeTokenTransfer,
Namespace: "ns1",
}

mdi.On("GetOperationByID", em.ctx, opID).Return(op, nil)
mdi.On("UpdateOperation", em.ctx, opID, mock.Anything).Return(nil)
mdi.On("GetOperationByID", em.ctx, op.ID).Return(op, nil)
mdi.On("UpdateOperation", em.ctx, op.ID, mock.Anything).Return(nil)
mdi.On("UpdateTransaction", em.ctx, op.Transaction, mock.Anything).Return(nil)
mdi.On("InsertEvent", em.ctx, mock.MatchedBy(func(e *fftypes.Event) bool {
return e.Type == fftypes.EventTypeTransferOpFailed && e.Namespace == "ns1"
})).Return(fmt.Errorf("pop"))

info := fftypes.JSONObject{"some": "info"}
err := em.OperationUpdate(mbi, opID, fftypes.OpStatusFailed, "some error", info)
err := em.OperationUpdate(mbi, op.ID, fftypes.OpStatusFailed, "some error", info)
assert.EqualError(t, err, "pop")

mdi.AssertExpectations(t)
Expand Down