Skip to content

Commit 4f826e5

Browse files
committed
Fix bug that causes history summary to grow incorrectly
Signed-off-by: Matthew Whitehead <[email protected]>
1 parent fefbd94 commit 4f826e5

File tree

2 files changed

+41
-30
lines changed

2 files changed

+41
-30
lines changed

pkg/txhistory/txhistory.go

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,10 @@ func (h *manager) AddSubStatusAction(ctx context.Context, mtx *apitypes.ManagedT
129129
// See if this action exists in the list already since we only want to update the single entry, not
130130
// add a new one
131131
currentSubStatus := mtx.History[len(mtx.History)-1]
132+
alreadyRecordedAction := false
132133
for _, entry := range currentSubStatus.Actions {
133134
if entry.Action == action {
135+
alreadyRecordedAction = true
134136
entry.Count++
135137
entry.LastOccurrence = fftypes.Now()
136138

@@ -142,41 +144,39 @@ func (h *manager) AddSubStatusAction(ctx context.Context, mtx *apitypes.ManagedT
142144
if info != nil {
143145
entry.LastInfo = jsonOrString(info)
144146
}
145-
146-
// As we have a possibly indefinite list of sub-status records (which might be a long list and early entries
147-
// get purged at some point) we keep a separate list of all the discrete types of sub-status
148-
// and action we've we've ever seen for this transaction along with a count of them. This means an early sub-status
149-
// (e.g. "queued") with an action that never happens again (e.g. "assignNonce") followed by 100s of different sub-status
150-
// types will still be recorded
151-
for _, actionType := range mtx.HistorySummary {
152-
if actionType.Action == action {
153-
// Just increment the counter and last timestamp
154-
actionType.LastOccurrence = fftypes.Now()
155-
actionType.Count++
156-
break
157-
}
158-
}
159-
return
147+
break
160148
}
161149
}
162150

163-
// If this is an entirely new status add it to the list
164-
newAction := &apitypes.TxHistoryActionEntry{
165-
Time: fftypes.Now(),
166-
Action: action,
167-
LastOccurrence: fftypes.Now(),
168-
Count: 1,
169-
}
151+
if !alreadyRecordedAction {
152+
// If this is an entirely new action for this status entry, add it to the list
153+
newAction := &apitypes.TxHistoryActionEntry{
154+
Time: fftypes.Now(),
155+
Action: action,
156+
LastOccurrence: fftypes.Now(),
157+
Count: 1,
158+
}
170159

171-
if err != nil {
172-
newAction.LastError = jsonOrString(err)
173-
newAction.LastErrorTime = fftypes.Now()
174-
}
160+
if err != nil {
161+
newAction.LastError = jsonOrString(err)
162+
newAction.LastErrorTime = fftypes.Now()
163+
}
175164

176-
if info != nil {
177-
newAction.LastInfo = jsonOrString(info)
165+
if info != nil {
166+
newAction.LastInfo = jsonOrString(info)
167+
}
168+
169+
currentSubStatus.Actions = append(currentSubStatus.Actions, newAction)
178170
}
179171

180-
currentSubStatus.Actions = append(currentSubStatus.Actions, newAction)
172+
// Check if the history summary needs updating
173+
for _, actionType := range mtx.HistorySummary {
174+
if actionType.Action == action {
175+
// Just increment the counter and last timestamp
176+
actionType.LastOccurrence = fftypes.Now()
177+
actionType.Count++
178+
return
179+
}
180+
}
181181
mtx.HistorySummary = append(mtx.HistorySummary, &apitypes.TxHistorySummaryEntry{Action: action, Count: 1, FirstOccurrence: fftypes.Now(), LastOccurrence: fftypes.Now()})
182182
}

pkg/txhistory/txhistory_test.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ func TestManagedTXSubStatusAction(t *testing.T) {
102102
assert.Equal(t, 1, len(mtx.History[0].Actions))
103103
assert.Nil(t, mtx.History[0].Actions[0].LastErrorTime)
104104

105-
// Add an action
105+
// Add another action
106106
h.AddSubStatusAction(ctx, mtx, apitypes.TxActionRetrieveGasPrice, nil, fftypes.JSONAnyPtr(`{"gasError":"Acme Gas Oracle RC=12345"}`))
107107
assert.Equal(t, 2, len(mtx.History[0].Actions))
108108
assert.Equal(t, (*mtx.History[0].Actions[1].LastError).String(), `{"gasError":"Acme Gas Oracle RC=12345"}`)
@@ -139,6 +139,17 @@ func TestManagedTXSubStatusAction(t *testing.T) {
139139
// History is the complete list of unique sub-status types and actions
140140
assert.Equal(t, 5, len(mtx.HistorySummary))
141141

142+
// Add some new sub-status and actions to check max lengths are correct
143+
// Seen one of these before - should increase summary length by 1
144+
h.SetSubStatus(ctx, mtx, apitypes.TxSubStatusConfirmed)
145+
h.AddSubStatusAction(ctx, mtx, apitypes.TxActionReceiveReceipt, fftypes.JSONAnyPtr(`{"receiptId":"`+receiptId+`"}`), nil)
146+
assert.Equal(t, 6, len(mtx.HistorySummary))
147+
148+
// Seen both of these before - no change expected
149+
h.SetSubStatus(ctx, mtx, apitypes.TxSubStatusReceived)
150+
h.AddSubStatusAction(ctx, mtx, apitypes.TxActionAssignNonce, nil, nil)
151+
assert.Equal(t, 6, len(mtx.HistorySummary))
152+
142153
// Sanity check the history summary entries
143154
for _, historyEntry := range mtx.HistorySummary {
144155
assert.NotNil(t, historyEntry.FirstOccurrence)

0 commit comments

Comments
 (0)