-
Notifications
You must be signed in to change notification settings - Fork 175
/
transactions.go
52 lines (43 loc) · 1.26 KB
/
transactions.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
// (c) 2019 Dapper Labs - ALL RIGHTS RESERVED
package stdmap
import (
"fmt"
"github.com/onflow/flow-go/model/flow"
)
// Transactions implements the transactions memory pool of the consensus nodes,
// used to store transactions and to generate block payloads.
type Transactions struct {
*Backend
}
// NewTransactions creates a new memory pool for transctions.
func NewTransactions(limit uint) *Transactions {
t := &Transactions{
Backend: NewBackend(WithLimit(limit)),
}
return t
}
// Add adds a transaction to the mempool.
func (t *Transactions) Add(tx *flow.TransactionBody) bool {
return t.Backend.Add(tx)
}
// ByID returns the transaction with the given ID from the mempool.
func (t *Transactions) ByID(txID flow.Identifier) (*flow.TransactionBody, bool) {
entity, exists := t.Backend.ByID(txID)
if !exists {
return nil, false
}
tx, ok := entity.(*flow.TransactionBody)
if !ok {
panic(fmt.Sprintf("invalid entity in transaction pool (%T)", entity))
}
return tx, true
}
// All returns all transactions from the mempool.
func (t *Transactions) All() []*flow.TransactionBody {
entities := t.Backend.All()
txs := make([]*flow.TransactionBody, 0, len(entities))
for _, entity := range entities {
txs = append(txs, entity.(*flow.TransactionBody))
}
return txs
}