Skip to content

Commit

Permalink
Merge pull request #65 from kaleido-io/error-handling-for-tx-history
Browse files Browse the repository at this point in the history
adding error handling for tx history
  • Loading branch information
peterbroadhurst authored Mar 3, 2023
2 parents e0581eb + ea366d0 commit fcfb473
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
16 changes: 16 additions & 0 deletions pkg/txhistory/txhistory.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ func (h *manager) CurrentSubStatus(ctx context.Context, mtx *apitypes.ManagedTX)
// might go through many sub-status changes before being confirmed on chain the list of
// entries is capped at the configured number and FIFO approach used to keep within that cap.
func (h *manager) SetSubStatus(ctx context.Context, mtx *apitypes.ManagedTX, subStatus apitypes.TxSubStatus) {
if h.maxHistoryCount <= 0 {
// if history is turned off, it's a no op
return
}
// See if the status being transitioned to is the same as the current status.
// If so, there's nothing to do.
if len(mtx.History) > 0 {
Expand Down Expand Up @@ -125,6 +129,17 @@ func jsonOrString(value *fftypes.JSONAny) *fftypes.JSONAny {
// HTTP 4xx return code from a gas oracle. There is also an information field to record
// arbitrary data about the action, for example the gas price retrieved from an oracle.
func (h *manager) AddSubStatusAction(ctx context.Context, mtx *apitypes.ManagedTX, action apitypes.TxAction, info *fftypes.JSONAny, err *fftypes.JSONAny) {
if h.maxHistoryCount <= 0 {
// if history is turned off, it's a no op
return
}

// check there is a parent sub status to add actions to
if len(mtx.History) == 0 {
// if there is no sub status, add a sub status with the first possible sub status: received
h.SetSubStatus(ctx, mtx, apitypes.TxSubStatusReceived)

}

// See if this action exists in the list already since we only want to update the single entry, not
// add a new one
Expand Down Expand Up @@ -179,4 +194,5 @@ func (h *manager) AddSubStatusAction(ctx context.Context, mtx *apitypes.ManagedT
}
}
mtx.HistorySummary = append(mtx.HistorySummary, &apitypes.TxHistorySummaryEntry{Action: action, Count: 1, FirstOccurrence: fftypes.Now(), LastOccurrence: fftypes.Now()})

}
29 changes: 29 additions & 0 deletions pkg/txhistory/txhistory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"fmt"
"testing"

"github.com/hyperledger/firefly-common/pkg/config"
"github.com/hyperledger/firefly-common/pkg/fftypes"
"github.com/hyperledger/firefly-transaction-manager/internal/tmconfig"
"github.com/hyperledger/firefly-transaction-manager/pkg/apitypes"
Expand Down Expand Up @@ -201,6 +202,34 @@ func TestManagedTXSubStatusMaxEntries(t *testing.T) {

}

func TestMaxHistoryCountSetToZero(t *testing.T) {
tmconfig.Reset()
config.Set(tmconfig.TransactionsMaxHistoryCount, 0)
ctx, cancelCtx := context.WithCancel(context.Background())
h := NewTxHistoryManager(ctx).(*manager)
defer cancelCtx()
mtx := &apitypes.ManagedTX{}

h.SetSubStatus(ctx, mtx, apitypes.TxSubStatusReceived)
h.AddSubStatusAction(ctx, mtx, apitypes.TxActionSubmitTransaction, nil, nil)
assert.Equal(t, 0, len(mtx.History))
assert.Equal(t, 0, len(mtx.HistorySummary))

}

func TestAddReceivedStatusWhenNothingSet(t *testing.T) {
ctx, h, done := newTestTxHistoryManager(t)

defer done()
mtx := &apitypes.ManagedTX{}

assert.Equal(t, 0, len(mtx.History))
h.AddSubStatusAction(ctx, mtx, apitypes.TxActionSubmitTransaction, nil, nil)
assert.Equal(t, 1, len(mtx.History))
assert.Equal(t, 1, len(mtx.History[0].Actions))
assert.Equal(t, apitypes.TxSubStatusReceived, mtx.History[0].Status)
assert.Equal(t, apitypes.TxActionSubmitTransaction, mtx.History[0].Actions[0].Action)
}
func TestJSONOrStringNull(t *testing.T) {
assert.Nil(t, jsonOrString(nil))
}

0 comments on commit fcfb473

Please sign in to comment.