-
Notifications
You must be signed in to change notification settings - Fork 201
/
currentBlockTransactionPool.go
60 lines (48 loc) · 1.51 KB
/
currentBlockTransactionPool.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
package dataPool
import (
"sync"
"github.com/multiversx/mx-chain-core-go/core/check"
"github.com/multiversx/mx-chain-core-go/data"
"github.com/multiversx/mx-chain-go/dataRetriever"
)
var _ dataRetriever.TransactionCacher = (*transactionMapCacher)(nil)
type transactionMapCacher struct {
mutTxs sync.RWMutex
txsForBlock map[string]data.TransactionHandler
}
// NewCurrentBlockTransactionsPool returns a new transactions pool to be used for the current block
func NewCurrentBlockTransactionsPool() *transactionMapCacher {
return &transactionMapCacher{
mutTxs: sync.RWMutex{},
txsForBlock: make(map[string]data.TransactionHandler),
}
}
// Clean creates a new transaction pool
func (tmc *transactionMapCacher) Clean() {
tmc.mutTxs.Lock()
tmc.txsForBlock = make(map[string]data.TransactionHandler)
tmc.mutTxs.Unlock()
}
// GetTx gets the transaction for the given hash
func (tmc *transactionMapCacher) GetTx(txHash []byte) (data.TransactionHandler, error) {
tmc.mutTxs.RLock()
defer tmc.mutTxs.RUnlock()
tx, ok := tmc.txsForBlock[string(txHash)]
if !ok {
return nil, dataRetriever.ErrTxNotFoundInBlockPool
}
return tx, nil
}
// AddTx adds the transaction for the given hash
func (tmc *transactionMapCacher) AddTx(txHash []byte, tx data.TransactionHandler) {
if check.IfNil(tx) {
return
}
tmc.mutTxs.Lock()
tmc.txsForBlock[string(txHash)] = tx
tmc.mutTxs.Unlock()
}
// IsInterfaceNil returns true if underlying object is nil
func (tmc *transactionMapCacher) IsInterfaceNil() bool {
return tmc == nil
}