Skip to content

Commit ea366d0

Browse files
committed
adding error handling for tx history
Signed-off-by: Chengxuan Xing <[email protected]>
1 parent ae8ecda commit ea366d0

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

pkg/txhistory/txhistory.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ func (h *manager) CurrentSubStatus(ctx context.Context, mtx *apitypes.ManagedTX)
5959
// might go through many sub-status changes before being confirmed on chain the list of
6060
// entries is capped at the configured number and FIFO approach used to keep within that cap.
6161
func (h *manager) SetSubStatus(ctx context.Context, mtx *apitypes.ManagedTX, subStatus apitypes.TxSubStatus) {
62+
if h.maxHistoryCount <= 0 {
63+
// if history is turned off, it's a no op
64+
return
65+
}
6266
// See if the status being transitioned to is the same as the current status.
6367
// If so, there's nothing to do.
6468
if len(mtx.History) > 0 {
@@ -125,6 +129,17 @@ func jsonOrString(value *fftypes.JSONAny) *fftypes.JSONAny {
125129
// HTTP 4xx return code from a gas oracle. There is also an information field to record
126130
// arbitrary data about the action, for example the gas price retrieved from an oracle.
127131
func (h *manager) AddSubStatusAction(ctx context.Context, mtx *apitypes.ManagedTX, action apitypes.TxAction, info *fftypes.JSONAny, err *fftypes.JSONAny) {
132+
if h.maxHistoryCount <= 0 {
133+
// if history is turned off, it's a no op
134+
return
135+
}
136+
137+
// check there is a parent sub status to add actions to
138+
if len(mtx.History) == 0 {
139+
// if there is no sub status, add a sub status with the first possible sub status: received
140+
h.SetSubStatus(ctx, mtx, apitypes.TxSubStatusReceived)
141+
142+
}
128143

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

pkg/txhistory/txhistory_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"fmt"
2323
"testing"
2424

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

202203
}
203204

205+
func TestMaxHistoryCountSetToZero(t *testing.T) {
206+
tmconfig.Reset()
207+
config.Set(tmconfig.TransactionsMaxHistoryCount, 0)
208+
ctx, cancelCtx := context.WithCancel(context.Background())
209+
h := NewTxHistoryManager(ctx).(*manager)
210+
defer cancelCtx()
211+
mtx := &apitypes.ManagedTX{}
212+
213+
h.SetSubStatus(ctx, mtx, apitypes.TxSubStatusReceived)
214+
h.AddSubStatusAction(ctx, mtx, apitypes.TxActionSubmitTransaction, nil, nil)
215+
assert.Equal(t, 0, len(mtx.History))
216+
assert.Equal(t, 0, len(mtx.HistorySummary))
217+
218+
}
219+
220+
func TestAddReceivedStatusWhenNothingSet(t *testing.T) {
221+
ctx, h, done := newTestTxHistoryManager(t)
222+
223+
defer done()
224+
mtx := &apitypes.ManagedTX{}
225+
226+
assert.Equal(t, 0, len(mtx.History))
227+
h.AddSubStatusAction(ctx, mtx, apitypes.TxActionSubmitTransaction, nil, nil)
228+
assert.Equal(t, 1, len(mtx.History))
229+
assert.Equal(t, 1, len(mtx.History[0].Actions))
230+
assert.Equal(t, apitypes.TxSubStatusReceived, mtx.History[0].Status)
231+
assert.Equal(t, apitypes.TxActionSubmitTransaction, mtx.History[0].Actions[0].Action)
232+
}
204233
func TestJSONOrStringNull(t *testing.T) {
205234
assert.Nil(t, jsonOrString(nil))
206235
}

0 commit comments

Comments
 (0)