forked from 0xPolygon/polygon-edge
/
lookup_map.go
46 lines (36 loc) · 882 Bytes
/
lookup_map.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
package txpool
import (
"sync"
"github.com/dogechain-lab/jury/types"
)
// Lookup map used to find transactions present in the pool
type lookupMap struct {
sync.RWMutex
all map[types.Hash]*types.Transaction
}
// add inserts the given transaction into the map. [thread-safe]
func (m *lookupMap) add(txs ...*types.Transaction) {
m.Lock()
defer m.Unlock()
for _, tx := range txs {
m.all[tx.Hash] = tx
}
}
// remove removes the given transactions from the map. [thread-safe]
func (m *lookupMap) remove(txs ...*types.Transaction) {
m.Lock()
defer m.Unlock()
for _, tx := range txs {
delete(m.all, tx.Hash)
}
}
// get returns the transaction associated with the given hash. [thread-safe]
func (m *lookupMap) get(hash types.Hash) (*types.Transaction, bool) {
m.RLock()
defer m.RUnlock()
tx, ok := m.all[hash]
if !ok {
return nil, false
}
return tx, true
}