-
Notifications
You must be signed in to change notification settings - Fork 4
/
mempool.go
201 lines (163 loc) · 3.95 KB
/
mempool.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package node
import (
"errors"
"github.com/vulpemventures/go-elements/block"
"github.com/vulpemventures/go-elements/transaction"
"github.com/vulpemventures/neutrino-elements/pkg/protocol"
"sync"
"time"
log "github.com/sirupsen/logrus"
)
const (
TxConfirmed TxEventType = iota
TxUnConfirmed
)
type MemPool struct {
//txChan receives new transactions from the network
txChan chan protocol.MsgTx
//quitChan receives a quit signal
quitChan chan struct{}
//txMap is a map of transactions that are in the memPool
txs map[string]txData
//txLock is a mutex to protect the txs map
txsMutex *sync.RWMutex
//txSubscribers is a list of subscribers listening for new transactions
txSubscribers []txSubscriber
}
func NewMemPool(
logLevel log.Level,
) MemPool {
log.SetLevel(logLevel)
return MemPool{
txChan: make(chan protocol.MsgTx),
txs: make(map[string]txData),
quitChan: make(chan struct{}),
txsMutex: new(sync.RWMutex),
}
}
type TxEventType int
type TxEvent interface {
Type() TxEventType
TxID() string
}
type TxConfirmedEvent struct {
txID string
tx transaction.Transaction
}
func (t TxConfirmedEvent) Type() TxEventType {
return TxConfirmed
}
func (t TxConfirmedEvent) TxID() string {
return t.txID
}
type TxUnConfirmedEvent struct {
txID string
tx transaction.Transaction
}
func (t TxUnConfirmedEvent) Type() TxEventType {
return TxUnConfirmed
}
func (t TxUnConfirmedEvent) TxID() string {
return t.txID
}
type txData struct {
tx transaction.Transaction
timeReceived time.Time
}
type txSubscriber struct {
id string
txEvent chan<- TxEvent
}
func (m *MemPool) Start() {
log.Infoln("memPool started")
go m.listenForNewTxs()
}
func (m *MemPool) Stop() {
close(m.txChan)
close(m.quitChan)
for _, v := range m.txSubscribers {
close(v.txEvent)
}
}
func (m *MemPool) GetMemPool() map[string]transaction.Transaction {
m.txsMutex.RLock()
defer m.txsMutex.RUnlock()
txMap := make(map[string]transaction.Transaction)
for k, v := range m.txs {
txMap[k] = v.tx
}
return txMap
}
func (m *MemPool) AddTx(tx protocol.MsgTx) {
m.txChan <- tx
}
func (m *MemPool) AddSubscriber(id string) <-chan TxEvent {
txEvent := make(chan TxEvent)
m.txSubscribers = append(m.txSubscribers, txSubscriber{
id: id,
txEvent: txEvent,
})
return txEvent
}
func (m *MemPool) CheckTxConfirmed(block block.Block) {
go m.checkTxConfirmed(block)
}
func (m *MemPool) checkTxConfirmed(block block.Block) {
//TODO: check if this is the best way to do this
for _, v := range m.txs {
for _, tx := range block.TransactionsData.Transactions {
if tx.TxHash().String() == v.tx.TxHash().String() {
if err := m.removeTxFromMemPool(v.tx.TxHash().String()); err != nil {
log.Errorln("failed to remove tx from memPool")
}
m.notifySubscribers(TxConfirmedEvent{
txID: tx.TxHash().String(),
tx: *tx,
})
}
}
}
}
func (m *MemPool) removeTxFromMemPool(txID string) error {
m.txsMutex.Lock()
defer m.txsMutex.Unlock()
if _, ok := m.txs[txID]; ok {
delete(m.txs, txID)
return nil
}
return errors.New("tx not found")
}
func (m *MemPool) addTx(txID string, txData txData) {
m.txsMutex.Lock()
defer m.txsMutex.Unlock()
if _, ok := m.txs[txID]; !ok {
m.txs[txID] = txData
}
}
func (m *MemPool) listenForNewTxs() {
log.Infoln("listening for new transactions")
for tx := range m.txChan {
m.addTx(
tx.HashStr(),
txData{
tx: tx.Transaction,
timeReceived: time.Now(),
},
)
m.notifySubscribers(TxUnConfirmedEvent{
txID: tx.HashStr(),
tx: tx.Transaction,
})
log.Debugf("tx %s added to memPool", tx.HashStr())
}
log.Infoln("memPool listener stopped")
}
func (m *MemPool) notifySubscribers(txEvent TxEvent) {
for _, v := range m.txSubscribers {
go func(subscriber txSubscriber) {
log.Debugf("notifying subscriber %s of new tx started", subscriber.id)
subscriber.txEvent <- txEvent
log.Debugf("notifying subscriber %s of new tx done", subscriber.id)
}(v)
}
}