2022-10-05 04:42:38 +00:00
|
|
|
package shards
|
2020-11-17 19:13:41 +00:00
|
|
|
|
|
|
|
import (
|
2021-05-04 16:12:59 +00:00
|
|
|
"sync"
|
|
|
|
|
2022-07-26 03:37:51 +00:00
|
|
|
"github.com/ledgerwatch/erigon-lib/common"
|
2022-03-17 07:40:18 +00:00
|
|
|
"github.com/ledgerwatch/erigon-lib/gointerfaces/remote"
|
2021-05-20 18:25:53 +00:00
|
|
|
"github.com/ledgerwatch/erigon/core/types"
|
2020-11-17 19:13:41 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type RpcEventType uint64
|
|
|
|
|
2022-03-18 08:06:23 +00:00
|
|
|
type NewSnapshotSubscription func() error
|
2022-02-10 01:25:58 +00:00
|
|
|
type HeaderSubscription func(headerRLP []byte) error
|
2021-03-25 06:42:45 +00:00
|
|
|
type PendingLogsSubscription func(types.Logs) error
|
2021-03-30 07:09:00 +00:00
|
|
|
type PendingBlockSubscription func(*types.Block) error
|
2021-04-26 09:53:38 +00:00
|
|
|
type PendingTxsSubscription func([]types.Transaction) error
|
2022-03-17 07:40:18 +00:00
|
|
|
type LogsSubscription func([]*remote.SubscribeLogsReply) error
|
2020-11-17 19:13:41 +00:00
|
|
|
|
2021-05-04 16:12:59 +00:00
|
|
|
// Events manages event subscriptions and dissimination. Thread-safe
|
2020-11-17 19:13:41 +00:00
|
|
|
type Events struct {
|
2022-02-10 01:25:58 +00:00
|
|
|
id int
|
|
|
|
headerSubscriptions map[int]chan [][]byte
|
2022-03-18 08:06:23 +00:00
|
|
|
newSnapshotSubscription map[int]chan struct{}
|
2021-03-30 07:09:00 +00:00
|
|
|
pendingLogsSubscriptions map[int]PendingLogsSubscription
|
|
|
|
pendingBlockSubscriptions map[int]PendingBlockSubscription
|
2021-04-26 09:53:38 +00:00
|
|
|
pendingTxsSubscriptions map[int]PendingTxsSubscription
|
2022-03-26 18:21:31 +00:00
|
|
|
logsSubscriptions map[int]chan []*remote.SubscribeLogsReply
|
|
|
|
hasLogSubscriptions bool
|
2021-05-04 16:12:59 +00:00
|
|
|
lock sync.RWMutex
|
2020-11-17 19:13:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewEvents() *Events {
|
2021-03-30 07:09:00 +00:00
|
|
|
return &Events{
|
2022-02-10 01:25:58 +00:00
|
|
|
headerSubscriptions: map[int]chan [][]byte{},
|
2021-03-30 07:09:00 +00:00
|
|
|
pendingLogsSubscriptions: map[int]PendingLogsSubscription{},
|
|
|
|
pendingBlockSubscriptions: map[int]PendingBlockSubscription{},
|
2021-04-26 09:53:38 +00:00
|
|
|
pendingTxsSubscriptions: map[int]PendingTxsSubscription{},
|
2022-03-26 18:21:31 +00:00
|
|
|
logsSubscriptions: map[int]chan []*remote.SubscribeLogsReply{},
|
2022-03-18 08:06:23 +00:00
|
|
|
newSnapshotSubscription: map[int]chan struct{}{},
|
2021-03-30 07:09:00 +00:00
|
|
|
}
|
2020-11-17 19:13:41 +00:00
|
|
|
}
|
|
|
|
|
2022-02-10 01:25:58 +00:00
|
|
|
func (e *Events) AddHeaderSubscription() (chan [][]byte, func()) {
|
2021-05-04 16:12:59 +00:00
|
|
|
e.lock.Lock()
|
|
|
|
defer e.lock.Unlock()
|
2022-02-10 01:25:58 +00:00
|
|
|
ch := make(chan [][]byte, 8)
|
|
|
|
e.id++
|
|
|
|
id := e.id
|
|
|
|
e.headerSubscriptions[id] = ch
|
|
|
|
return ch, func() {
|
|
|
|
delete(e.headerSubscriptions, id)
|
|
|
|
close(ch)
|
|
|
|
}
|
2020-11-17 19:13:41 +00:00
|
|
|
}
|
|
|
|
|
2022-03-18 08:06:23 +00:00
|
|
|
func (e *Events) AddNewSnapshotSubscription() (chan struct{}, func()) {
|
|
|
|
e.lock.Lock()
|
|
|
|
defer e.lock.Unlock()
|
|
|
|
ch := make(chan struct{}, 8)
|
|
|
|
e.id++
|
|
|
|
id := e.id
|
|
|
|
e.newSnapshotSubscription[id] = ch
|
|
|
|
return ch, func() {
|
|
|
|
delete(e.newSnapshotSubscription, id)
|
|
|
|
close(ch)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-26 18:21:31 +00:00
|
|
|
func (e *Events) AddLogsSubscription() (chan []*remote.SubscribeLogsReply, func()) {
|
2021-05-04 16:12:59 +00:00
|
|
|
e.lock.Lock()
|
|
|
|
defer e.lock.Unlock()
|
2022-03-26 18:21:31 +00:00
|
|
|
ch := make(chan []*remote.SubscribeLogsReply, 8)
|
|
|
|
e.id++
|
|
|
|
id := e.id
|
|
|
|
e.logsSubscriptions[id] = ch
|
|
|
|
return ch, func() {
|
|
|
|
delete(e.logsSubscriptions, id)
|
|
|
|
close(ch)
|
|
|
|
}
|
2021-03-30 07:09:00 +00:00
|
|
|
}
|
|
|
|
|
2022-03-26 18:21:31 +00:00
|
|
|
func (e *Events) EmptyLogSubsctiption(empty bool) {
|
2021-05-04 16:12:59 +00:00
|
|
|
e.lock.Lock()
|
|
|
|
defer e.lock.Unlock()
|
2022-03-26 18:21:31 +00:00
|
|
|
e.hasLogSubscriptions = !empty
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Events) HasLogSubsriptions() bool {
|
|
|
|
e.lock.RLock()
|
|
|
|
defer e.lock.RUnlock()
|
|
|
|
return e.hasLogSubscriptions
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Events) AddPendingLogsSubscription(s PendingLogsSubscription) {
|
|
|
|
e.lock.Lock()
|
|
|
|
defer e.lock.Unlock()
|
|
|
|
e.pendingLogsSubscriptions[len(e.pendingLogsSubscriptions)] = s
|
2021-03-25 06:42:45 +00:00
|
|
|
}
|
|
|
|
|
2022-03-26 18:21:31 +00:00
|
|
|
func (e *Events) AddPendingBlockSubscription(s PendingBlockSubscription) {
|
2022-03-17 07:40:18 +00:00
|
|
|
e.lock.Lock()
|
|
|
|
defer e.lock.Unlock()
|
2022-03-26 18:21:31 +00:00
|
|
|
e.pendingBlockSubscriptions[len(e.pendingBlockSubscriptions)] = s
|
2022-03-17 07:40:18 +00:00
|
|
|
}
|
|
|
|
|
2022-03-18 08:06:23 +00:00
|
|
|
func (e *Events) OnNewSnapshot() {
|
|
|
|
e.lock.Lock()
|
|
|
|
defer e.lock.Unlock()
|
|
|
|
for _, ch := range e.newSnapshotSubscription {
|
2022-07-26 03:37:51 +00:00
|
|
|
common.PrioritizedSend(ch, struct{}{})
|
2022-03-18 08:06:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-10 01:25:58 +00:00
|
|
|
func (e *Events) OnNewHeader(newHeadersRlp [][]byte) {
|
2021-05-05 03:16:57 +00:00
|
|
|
e.lock.Lock()
|
|
|
|
defer e.lock.Unlock()
|
2022-02-10 01:25:58 +00:00
|
|
|
for _, ch := range e.headerSubscriptions {
|
2022-07-26 03:37:51 +00:00
|
|
|
common.PrioritizedSend(ch, newHeadersRlp)
|
2020-11-17 19:13:41 +00:00
|
|
|
}
|
|
|
|
}
|
2021-03-25 06:42:45 +00:00
|
|
|
|
|
|
|
func (e *Events) OnNewPendingLogs(logs types.Logs) {
|
2021-05-05 03:16:57 +00:00
|
|
|
e.lock.Lock()
|
|
|
|
defer e.lock.Unlock()
|
2021-03-25 06:42:45 +00:00
|
|
|
for i, sub := range e.pendingLogsSubscriptions {
|
|
|
|
if err := sub(logs); err != nil {
|
2021-03-30 07:09:00 +00:00
|
|
|
delete(e.pendingLogsSubscriptions, i)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-03-17 07:40:18 +00:00
|
|
|
|
|
|
|
func (e *Events) OnLogs(logs []*remote.SubscribeLogsReply) {
|
|
|
|
e.lock.Lock()
|
|
|
|
defer e.lock.Unlock()
|
2022-03-26 18:21:31 +00:00
|
|
|
for _, ch := range e.logsSubscriptions {
|
2022-07-26 03:37:51 +00:00
|
|
|
common.PrioritizedSend(ch, logs)
|
2022-03-17 07:40:18 +00:00
|
|
|
}
|
|
|
|
}
|
2022-10-05 04:42:38 +00:00
|
|
|
|
|
|
|
type Notifications struct {
|
|
|
|
Events *Events
|
|
|
|
Accumulator *Accumulator
|
|
|
|
StateChangesConsumer StateChangeConsumer
|
|
|
|
}
|