Skip to content

Commit 8854bfc

Browse files
committed
Record actions as well as sub-status types in the history summary
Signed-off-by: Matthew Whitehead <[email protected]>
1 parent f4d0af2 commit 8854bfc

File tree

3 files changed

+40
-5
lines changed

3 files changed

+40
-5
lines changed

pkg/apitypes/managed_tx.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,10 @@ type TxHistoryStateTransitionEntry struct {
6262
// subStatus was entered. Because the detailed history might wrap, this means we can retain some basic
6363
// information about the complete history of the transaction beyond the life of the individual history records.
6464
type TxHistorySummaryEntry struct {
65-
Status TxSubStatus `json:"subStatus"`
65+
Status TxSubStatus `json:"subStatus,omitempty"`
66+
Action TxAction `json:"action,omitempty"`
6667
FirstOccurrence *fftypes.FFTime `json:"firstOccurrence"`
68+
LastOccurrence *fftypes.FFTime `json:"lastOccurrence"`
6769
Count int `json:"count"`
6870
}
6971

pkg/txhistory/txhistory.go

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,19 +81,20 @@ func (h *manager) SetSubStatus(ctx context.Context, mtx *apitypes.ManagedTX, sub
8181
mtx.History = mtx.History[1:]
8282
}
8383

84-
// As was as detailed sub-status records (which might be a long list and early entries
84+
// As we have a possibly indefinite list of sub-status records (which might be a long list and early entries
8585
// get purged at some point) we keep a separate list of all the discrete types of sub-status
86-
// we've ever seen for this transaction along with a count of them. This means an early sub-status
86+
// and action we've we've ever seen for this transaction along with a count of them. This means an early sub-status
8787
// (e.g. "queued") followed by 100s of different sub-status types will still be recorded
8888
for _, statusType := range mtx.HistorySummary {
8989
if statusType.Status == subStatus {
90-
// Just increment the counter
90+
// Just increment the counter and last timestamp
91+
statusType.LastOccurrence = fftypes.Now()
9192
statusType.Count++
9293
return
9394
}
9495
}
9596

96-
mtx.HistorySummary = append(mtx.HistorySummary, &apitypes.TxHistorySummaryEntry{Status: subStatus, Count: 1, FirstOccurrence: fftypes.Now()})
97+
mtx.HistorySummary = append(mtx.HistorySummary, &apitypes.TxHistorySummaryEntry{Status: subStatus, Count: 1, FirstOccurrence: fftypes.Now(), LastOccurrence: fftypes.Now()})
9798
}
9899

99100
// Takes a string that might be valid JSON, and returns valid JSON that is either:
@@ -141,6 +142,20 @@ func (h *manager) AddSubStatusAction(ctx context.Context, mtx *apitypes.ManagedT
141142
if info != nil {
142143
entry.LastInfo = jsonOrString(info)
143144
}
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+
}
144159
return
145160
}
146161
}
@@ -163,4 +178,5 @@ func (h *manager) AddSubStatusAction(ctx context.Context, mtx *apitypes.ManagedT
163178
}
164179

165180
currentSubStatus.Actions = append(currentSubStatus.Actions, newAction)
181+
mtx.HistorySummary = append(mtx.HistorySummary, &apitypes.TxHistorySummaryEntry{Action: action, Count: 1, FirstOccurrence: fftypes.Now(), LastOccurrence: fftypes.Now()})
166182
}

pkg/txhistory/txhistory_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,23 @@ func TestManagedTXSubStatusAction(t *testing.T) {
136136
assert.Equal(t, 1, mtx.History[0].Actions[3].Count)
137137
assert.Nil(t, mtx.History[0].Actions[3].LastErrorTime)
138138

139+
// History is the complete list of unique sub-status types and actions
140+
assert.Equal(t, 5, len(mtx.HistorySummary))
141+
142+
// Sanity check the history summary entries
143+
for _, historyEntry := range mtx.HistorySummary {
144+
assert.NotNil(t, historyEntry.FirstOccurrence)
145+
assert.NotNil(t, historyEntry.LastOccurrence)
146+
assert.GreaterOrEqual(t, historyEntry.Count, 1)
147+
148+
if historyEntry.Action == apitypes.TxActionRetrieveGasPrice {
149+
// The first and last occurrence timestamps shoudn't be the same
150+
assert.NotEqual(t, historyEntry.FirstOccurrence, historyEntry.LastOccurrence)
151+
152+
// We should have a count of 3 for this action
153+
assert.Equal(t, historyEntry.Count, 3)
154+
}
155+
}
139156
}
140157

141158
func TestManagedTXSubStatusInvalidJSON(t *testing.T) {

0 commit comments

Comments
 (0)