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
4 changes: 2 additions & 2 deletions internal/txcommon/txcommon.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ func (t *transactionHelper) PersistTransaction(ctx context.Context, ns string, i
return true, nil
}

// AddBlockchainTX is called when we know the tranasction should exist, and we don't need any validation
// just want to bolt an extra blockchain TXID on - if it's not there already.
// AddBlockchainTX is called when we know the transaction should exist, and we don't need any validation
// but just want to bolt on an extra blockchain TXID (if it's not there already).
func (t *transactionHelper) AddBlockchainTX(ctx context.Context, id *fftypes.UUID, blockchainTXID string) error {

tx, err := t.database.GetTransactionByID(ctx, id)
Expand Down
92 changes: 92 additions & 0 deletions internal/txcommon/txcommon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"testing"

"github.com/hyperledger/firefly/mocks/databasemocks"
"github.com/hyperledger/firefly/pkg/database"
"github.com/hyperledger/firefly/pkg/fftypes"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
Expand Down Expand Up @@ -283,3 +284,94 @@ func TestPersistTransactionExistingMismatchType(t *testing.T) {
mdi.AssertExpectations(t)

}

func TestAddBlockchainTX(t *testing.T) {

mdi := &databasemocks.Plugin{}
txHelper := NewTransactionHelper(mdi)
ctx := context.Background()

txid := fftypes.NewUUID()
mdi.On("GetTransactionByID", ctx, txid).Return(&fftypes.Transaction{
ID: txid,
Namespace: "ns1",
Type: fftypes.TransactionTypeContractInvoke,
Created: fftypes.Now(),
BlockchainIDs: fftypes.FFStringArray{"0x111111"},
}, nil)
mdi.On("UpdateTransaction", ctx, txid, mock.MatchedBy(func(u database.Update) bool {
info, _ := u.Finalize()
assert.Equal(t, 1, len(info.SetOperations))
assert.Equal(t, "blockchainids", info.SetOperations[0].Field)
val, _ := info.SetOperations[0].Value.Value()
assert.Equal(t, "0x111111,abc", val)
return true
})).Return(nil)

err := txHelper.AddBlockchainTX(ctx, txid, "abc")
assert.NoError(t, err)

mdi.AssertExpectations(t)

}

func TestAddBlockchainTXGetFail(t *testing.T) {

mdi := &databasemocks.Plugin{}
txHelper := NewTransactionHelper(mdi)
ctx := context.Background()

txid := fftypes.NewUUID()
mdi.On("GetTransactionByID", ctx, txid).Return(nil, fmt.Errorf("pop"))

err := txHelper.AddBlockchainTX(ctx, txid, "abc")
assert.EqualError(t, err, "pop")

mdi.AssertExpectations(t)

}

func TestAddBlockchainTXUpdateFail(t *testing.T) {

mdi := &databasemocks.Plugin{}
txHelper := NewTransactionHelper(mdi)
ctx := context.Background()

txid := fftypes.NewUUID()
mdi.On("GetTransactionByID", ctx, txid).Return(&fftypes.Transaction{
ID: txid,
Namespace: "ns1",
Type: fftypes.TransactionTypeContractInvoke,
Created: fftypes.Now(),
BlockchainIDs: fftypes.FFStringArray{"0x111111"},
}, nil)
mdi.On("UpdateTransaction", ctx, txid, mock.Anything).Return(fmt.Errorf("pop"))

err := txHelper.AddBlockchainTX(ctx, txid, "abc")
assert.EqualError(t, err, "pop")

mdi.AssertExpectations(t)

}

func TestAddBlockchainTXUnchanged(t *testing.T) {

mdi := &databasemocks.Plugin{}
txHelper := NewTransactionHelper(mdi)
ctx := context.Background()

txid := fftypes.NewUUID()
mdi.On("GetTransactionByID", ctx, txid).Return(&fftypes.Transaction{
ID: txid,
Namespace: "ns1",
Type: fftypes.TransactionTypeContractInvoke,
Created: fftypes.Now(),
BlockchainIDs: fftypes.FFStringArray{"0x111111"},
}, nil)

err := txHelper.AddBlockchainTX(ctx, txid, "0x111111")
assert.NoError(t, err)

mdi.AssertExpectations(t)

}