-
Notifications
You must be signed in to change notification settings - Fork 175
/
transaction_timings.go
76 lines (66 loc) · 2.11 KB
/
transaction_timings.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// (c) 2019 Dapper Labs - ALL RIGHTS RESERVED
package stdmap
import (
"github.com/onflow/flow-go/model/flow"
)
// TransactionTimings implements the transaction timings memory pool of access nodes,
// used to store transaction timings to report the timing of individual transactions
type TransactionTimings struct {
*Backend
}
// NewTransactionTimings creates a new memory pool for transaction timings
func NewTransactionTimings(limit uint) (*TransactionTimings, error) {
t := &TransactionTimings{
Backend: NewBackend(WithLimit(limit)),
}
return t, nil
}
// Add adds a transaction timing to the mempool.
func (t *TransactionTimings) Add(tx *flow.TransactionTiming) bool {
return t.Backend.Add(tx)
}
// ByID returns the transaction timing with the given ID from the mempool.
func (t *TransactionTimings) ByID(txID flow.Identifier) (*flow.TransactionTiming, bool) {
entity, exists := t.Backend.ByID(txID)
if !exists {
return nil, false
}
tt, ok := entity.(*flow.TransactionTiming)
if !ok {
return nil, false
}
return tt, true
}
// Adjust will adjust the transaction timing using the given function if the given key can be found.
// Returns a bool which indicates whether the value was updated as well as the updated value.
func (t *TransactionTimings) Adjust(txID flow.Identifier, f func(*flow.TransactionTiming) *flow.TransactionTiming) (
*flow.TransactionTiming, bool) {
e, updated := t.Backend.Adjust(txID, func(e flow.Entity) flow.Entity {
tt, ok := e.(*flow.TransactionTiming)
if !ok {
return nil
}
return f(tt)
})
if !updated {
return nil, false
}
tt, ok := e.(*flow.TransactionTiming)
if !ok {
return nil, false
}
return tt, updated
}
// All returns all transaction timings from the mempool.
func (t *TransactionTimings) All() []*flow.TransactionTiming {
entities := t.Backend.All()
txs := make([]*flow.TransactionTiming, 0, len(entities))
for _, entity := range entities {
txs = append(txs, entity.(*flow.TransactionTiming))
}
return txs
}
// Remove removes the transaction timing with the given ID.
func (t *TransactionTimings) Remove(txID flow.Identifier) bool {
return t.Backend.Remove(txID)
}