11package herocache
22
33import (
4- "fmt"
5-
64 "github.com/rs/zerolog"
75
86 "github.com/onflow/flow-go/model/flow"
@@ -14,7 +12,7 @@ import (
1412)
1513
1614type Transactions struct {
17- c * stdmap.Backend
15+ * stdmap.Backend [flow. Identifier , * flow. TransactionBody ]
1816 byPayer map [flow.Address ]map [flow.Identifier ]struct {}
1917}
2018
@@ -26,31 +24,26 @@ func NewTransactions(limit uint32, logger zerolog.Logger, collector module.HeroC
2624 }
2725
2826 tracer := & ejectionTracer {transactions : t }
29- t .c = stdmap .NewBackend (stdmap .WithBackData (
30- herocache .NewCache (limit ,
31- herocache .DefaultOversizeFactor ,
32- heropool .LRUEjection ,
33- logger .With ().Str ("mempool" , "transactions" ).Logger (),
34- collector ,
35- herocache .WithTracer (tracer ))))
27+ t .Backend = stdmap .NewBackend (
28+ stdmap.WithMutableBackData [flow.Identifier , * flow.TransactionBody ](
29+ herocache .NewCache [* flow.TransactionBody ](limit ,
30+ herocache .DefaultOversizeFactor ,
31+ heropool .LRUEjection ,
32+ logger .With ().Str ("mempool" , "transactions" ).Logger (),
33+ collector ,
34+ herocache.WithTracer [* flow.TransactionBody ](tracer ))))
3635
3736 return t
3837}
3938
40- // Has checks whether the transaction with the given hash is currently in
41- // the memory pool.
42- func (t * Transactions ) Has (id flow.Identifier ) bool {
43- return t .c .Has (id )
44- }
45-
4639// Add adds a transaction to the mempool.
4740func (t * Transactions ) Add (tx * flow.TransactionBody ) bool {
4841 added := false
49- err := t .c . Run (func (backdata mempool.BackData ) error {
42+ err := t .Run (func (backdata mempool.BackData [flow. Identifier , * flow. TransactionBody ] ) error {
5043 // Warning! reference pointer must be dereferenced before adding to HeroCache.
5144 // This is crucial for its heap object optimizations.
5245 txID := tx .ID ()
53- added = backdata .Add (txID , * tx )
46+ added = backdata .Add (txID , tx )
5447 if ! added {
5548 return nil
5649 }
@@ -68,37 +61,9 @@ func (t *Transactions) Add(tx *flow.TransactionBody) bool {
6861 return added
6962}
7063
71- // ByID returns the transaction with the given ID from the mempool.
72- func (t * Transactions ) ByID (txID flow.Identifier ) (* flow.TransactionBody , bool ) {
73- entity , exists := t .c .ByID (txID )
74- if ! exists {
75- return nil , false
76- }
77- tx , ok := entity .(flow.TransactionBody )
78- if ! ok {
79- panic (fmt .Sprintf ("invalid entity in transaction pool (%T)" , entity ))
80- }
81- return & tx , true
82- }
83-
84- // All returns all transactions from the mempool. Since it is using the HeroCache, All guarantees returning
85- // all transactions in the same order as they are added.
86- func (t * Transactions ) All () []* flow.TransactionBody {
87- entities := t .c .All ()
88- txs := make ([]* flow.TransactionBody , 0 , len (entities ))
89- for _ , entity := range entities {
90- tx , ok := entity .(flow.TransactionBody )
91- if ! ok {
92- panic (fmt .Sprintf ("invalid entity in transaction pool (%T)" , entity ))
93- }
94- txs = append (txs , & tx )
95- }
96- return txs
97- }
98-
9964// Clear removes all transactions stored in this mempool.
10065func (t * Transactions ) Clear () {
101- err := t .c . Run (func (backdata mempool.BackData ) error {
66+ err := t .Run (func (backdata mempool.BackData [flow. Identifier , * flow. TransactionBody ] ) error {
10267 backdata .Clear ()
10368 t .byPayer = make (map [flow.Address ]map [flow.Identifier ]struct {})
10469 return nil
@@ -108,15 +73,10 @@ func (t *Transactions) Clear() {
10873 }
10974}
11075
111- // Size returns total number of stored transactions.
112- func (t * Transactions ) Size () uint {
113- return t .c .Size ()
114- }
115-
11676// Remove removes transaction from mempool.
11777func (t * Transactions ) Remove (id flow.Identifier ) bool {
11878 removed := false
119- err := t .c . Run (func (backdata mempool.BackData ) error {
79+ err := t .Run (func (backdata mempool.BackData [flow. Identifier , * flow. TransactionBody ] ) error {
12080 var entity flow.Entity
12181 entity , removed = backdata .Remove (id )
12282 if ! removed {
@@ -136,15 +96,14 @@ func (t *Transactions) Remove(id flow.Identifier) bool {
13696// by the given payer.
13797func (t * Transactions ) ByPayer (payer flow.Address ) []* flow.TransactionBody {
13898 var result []* flow.TransactionBody
139- err := t .c . Run (func (backdata mempool.BackData ) error {
99+ err := t .Run (func (backdata mempool.BackData [flow. Identifier , * flow. TransactionBody ] ) error {
140100 ids := t .byPayer [payer ]
141101 for id := range ids {
142- entity , exists := backdata .ByID (id )
102+ txBody , exists := backdata .Get (id )
143103 if ! exists {
144104 continue
145105 }
146- txBody := entity .(flow.TransactionBody )
147- result = append (result , & txBody )
106+ result = append (result , txBody )
148107 }
149108 return nil
150109 })
@@ -170,19 +129,16 @@ type ejectionTracer struct {
170129 transactions * Transactions
171130}
172131
173- var _ herocache.Tracer = (* ejectionTracer )(nil )
132+ var _ herocache.Tracer [ * flow. TransactionBody ] = (* ejectionTracer )(nil )
174133
175- func (t * ejectionTracer ) EntityEjectionDueToEmergency (ejectedEntity flow.Entity ) {
176- t .cleanupIndex (ejectedEntity )
177- }
178-
179- func (t * ejectionTracer ) EntityEjectionDueToFullCapacity (ejectedEntity flow.Entity ) {
180- t .cleanupIndex (ejectedEntity )
134+ // EntityEjectionDueToEmergency calls removeFromIndex on the transactions to clean up the index. This is safe since
135+ // backdata is locked by the caller when this function is called.
136+ func (t * ejectionTracer ) EntityEjectionDueToEmergency (txBody * flow.TransactionBody ) {
137+ t .transactions .removeFromIndex (txBody .ID (), txBody .Payer )
181138}
182139
183- // cleanupIndex calls removeFromIndex on the transactions to clean up the index. This is safe since
140+ // EntityEjectionDueToFullCapacity calls removeFromIndex on the transactions to clean up the index. This is safe since
184141// backdata is locked by the caller when this function is called.
185- func (t * ejectionTracer ) cleanupIndex (ejectedEntity flow.Entity ) {
186- txBody := ejectedEntity .(flow.TransactionBody )
142+ func (t * ejectionTracer ) EntityEjectionDueToFullCapacity (txBody * flow.TransactionBody ) {
187143 t .transactions .removeFromIndex (txBody .ID (), txBody .Payer )
188144}
0 commit comments