-
Notifications
You must be signed in to change notification settings - Fork 211
/
atx_pool.go
49 lines (41 loc) · 980 Bytes
/
atx_pool.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
package miner
import (
"fmt"
"sync"
"github.com/spacemeshos/go-spacemesh/common/types"
)
type AtxMemPool struct {
mu sync.RWMutex
atxMap map[types.AtxId]*types.ActivationTx
}
func NewAtxMemPool() *AtxMemPool {
return &AtxMemPool{atxMap: make(map[types.AtxId]*types.ActivationTx)}
}
func (mem *AtxMemPool) Get(id types.AtxId) (*types.ActivationTx, error) {
mem.mu.RLock()
defer mem.mu.RUnlock()
atx, ok := mem.atxMap[id]
if !ok {
return nil, fmt.Errorf("cannot find ATX in mempool")
}
return atx, nil
}
func (mem *AtxMemPool) GetAllItems() []*types.ActivationTx {
mem.mu.RLock()
defer mem.mu.RUnlock()
var atxList []*types.ActivationTx
for _, k := range mem.atxMap {
atxList = append(atxList, k)
}
return atxList
}
func (mem *AtxMemPool) Put(atx *types.ActivationTx) {
mem.mu.Lock()
mem.atxMap[atx.Id()] = atx
mem.mu.Unlock()
}
func (mem *AtxMemPool) Invalidate(id types.AtxId) {
mem.mu.Lock()
defer mem.mu.Unlock()
delete(mem.atxMap, id)
}