2021-01-15 12:40:15 +00:00
|
|
|
package mempool
|
|
|
|
|
2021-05-28 11:47:33 +00:00
|
|
|
import "github.com/nspcc-dev/neo-go/pkg/core/mempoolevent"
|
2021-01-15 12:40:15 +00:00
|
|
|
|
|
|
|
// RunSubscriptions runs subscriptions goroutine if mempool subscriptions are enabled.
|
|
|
|
// You should manually free the resources by calling StopSubscriptions on mempool shutdown.
|
|
|
|
func (mp *Pool) RunSubscriptions() {
|
|
|
|
if !mp.subscriptionsEnabled {
|
|
|
|
panic("subscriptions are disabled")
|
|
|
|
}
|
|
|
|
if !mp.subscriptionsOn.Load() {
|
|
|
|
mp.subscriptionsOn.Store(true)
|
|
|
|
go mp.notificationDispatcher()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// StopSubscriptions stops mempool events loop.
|
|
|
|
func (mp *Pool) StopSubscriptions() {
|
|
|
|
if !mp.subscriptionsEnabled {
|
|
|
|
panic("subscriptions are disabled")
|
|
|
|
}
|
|
|
|
if mp.subscriptionsOn.Load() {
|
|
|
|
mp.subscriptionsOn.Store(false)
|
|
|
|
close(mp.stopCh)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// SubscribeForTransactions adds the given channel to the new mempool event broadcasting, so when
|
|
|
|
// there is a new transactions added to the mempool or an existing transaction removed from
|
|
|
|
// the mempool, you'll receive it via this channel.
|
2021-05-28 11:47:33 +00:00
|
|
|
func (mp *Pool) SubscribeForTransactions(ch chan<- mempoolevent.Event) {
|
2021-01-15 12:40:15 +00:00
|
|
|
if mp.subscriptionsOn.Load() {
|
|
|
|
mp.subCh <- ch
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// UnsubscribeFromTransactions unsubscribes the given channel from new mempool notifications,
|
2021-01-15 12:40:15 +00:00
|
|
|
// you can close it afterwards. Passing non-subscribed channel is a no-op.
|
2021-05-28 11:47:33 +00:00
|
|
|
func (mp *Pool) UnsubscribeFromTransactions(ch chan<- mempoolevent.Event) {
|
2021-01-15 12:40:15 +00:00
|
|
|
if mp.subscriptionsOn.Load() {
|
|
|
|
mp.unsubCh <- ch
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// notificationDispatcher manages subscription to events and broadcasts new events.
|
|
|
|
func (mp *Pool) notificationDispatcher() {
|
|
|
|
var (
|
|
|
|
// These are just sets of subscribers, though modelled as maps
|
|
|
|
// for ease of management (not a lot of subscriptions is really
|
|
|
|
// expected, but maps are convenient for adding/deleting elements).
|
2021-05-28 11:47:33 +00:00
|
|
|
txFeed = make(map[chan<- mempoolevent.Event]bool)
|
2021-01-15 12:40:15 +00:00
|
|
|
)
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-mp.stopCh:
|
|
|
|
return
|
|
|
|
case sub := <-mp.subCh:
|
|
|
|
txFeed[sub] = true
|
|
|
|
case unsub := <-mp.unsubCh:
|
|
|
|
delete(txFeed, unsub)
|
|
|
|
case event := <-mp.events:
|
|
|
|
for ch := range txFeed {
|
|
|
|
ch <- event
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|