2020-11-17 19:13:41 +00:00
|
|
|
package remotedbserver
|
|
|
|
|
|
|
|
import (
|
2021-05-04 16:12:59 +00:00
|
|
|
"sync"
|
|
|
|
|
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
|
|
|
|
|
|
|
|
type HeaderSubscription func(*types.Header) 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
|
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 {
|
2021-03-30 07:09:00 +00:00
|
|
|
headerSubscriptions map[int]HeaderSubscription
|
|
|
|
pendingLogsSubscriptions map[int]PendingLogsSubscription
|
|
|
|
pendingBlockSubscriptions map[int]PendingBlockSubscription
|
2021-04-26 09:53:38 +00:00
|
|
|
pendingTxsSubscriptions map[int]PendingTxsSubscription
|
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{
|
|
|
|
headerSubscriptions: map[int]HeaderSubscription{},
|
|
|
|
pendingLogsSubscriptions: map[int]PendingLogsSubscription{},
|
|
|
|
pendingBlockSubscriptions: map[int]PendingBlockSubscription{},
|
2021-04-26 09:53:38 +00:00
|
|
|
pendingTxsSubscriptions: map[int]PendingTxsSubscription{},
|
2021-03-30 07:09:00 +00:00
|
|
|
}
|
2020-11-17 19:13:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Events) AddHeaderSubscription(s HeaderSubscription) {
|
2021-05-04 16:12:59 +00:00
|
|
|
e.lock.Lock()
|
|
|
|
defer e.lock.Unlock()
|
2021-03-30 07:09:00 +00:00
|
|
|
e.headerSubscriptions[len(e.headerSubscriptions)] = s
|
2020-11-17 19:13:41 +00:00
|
|
|
}
|
|
|
|
|
2021-03-25 06:42:45 +00:00
|
|
|
func (e *Events) AddPendingLogsSubscription(s PendingLogsSubscription) {
|
2021-05-04 16:12:59 +00:00
|
|
|
e.lock.Lock()
|
|
|
|
defer e.lock.Unlock()
|
2021-03-30 07:09:00 +00:00
|
|
|
e.pendingLogsSubscriptions[len(e.pendingLogsSubscriptions)] = s
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Events) AddPendingBlockSubscription(s PendingBlockSubscription) {
|
2021-05-04 16:12:59 +00:00
|
|
|
e.lock.Lock()
|
|
|
|
defer e.lock.Unlock()
|
2021-03-30 07:09:00 +00:00
|
|
|
e.pendingBlockSubscriptions[len(e.pendingBlockSubscriptions)] = s
|
2021-03-25 06:42:45 +00:00
|
|
|
}
|
|
|
|
|
2021-04-26 09:53:38 +00:00
|
|
|
func (e *Events) AddPendingTxsSubscription(s PendingTxsSubscription) {
|
2021-05-04 16:12:59 +00:00
|
|
|
e.lock.Lock()
|
|
|
|
defer e.lock.Unlock()
|
2021-04-26 09:53:38 +00:00
|
|
|
e.pendingTxsSubscriptions[len(e.pendingTxsSubscriptions)] = s
|
|
|
|
}
|
|
|
|
|
2020-11-17 19:13:41 +00:00
|
|
|
func (e *Events) OnNewHeader(newHeader *types.Header) {
|
2021-05-05 03:16:57 +00:00
|
|
|
e.lock.Lock()
|
|
|
|
defer e.lock.Unlock()
|
2021-02-12 16:46:50 +00:00
|
|
|
for i, sub := range e.headerSubscriptions {
|
|
|
|
if err := sub(newHeader); err != nil {
|
2021-03-30 07:09:00 +00:00
|
|
|
delete(e.headerSubscriptions, i)
|
2021-02-12 16:46:50 +00:00
|
|
|
}
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Events) OnNewPendingBlock(block *types.Block) {
|
2021-05-05 03:16:57 +00:00
|
|
|
e.lock.Lock()
|
|
|
|
defer e.lock.Unlock()
|
2021-03-30 07:09:00 +00:00
|
|
|
for i, sub := range e.pendingBlockSubscriptions {
|
|
|
|
if err := sub(block); err != nil {
|
|
|
|
delete(e.pendingBlockSubscriptions, i)
|
2021-03-25 06:42:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-04-26 09:53:38 +00:00
|
|
|
|
|
|
|
func (e *Events) OnNewPendingTxs(txs []types.Transaction) {
|
2021-05-05 03:16:57 +00:00
|
|
|
e.lock.Lock()
|
|
|
|
defer e.lock.Unlock()
|
2021-04-26 09:53:38 +00:00
|
|
|
for i, sub := range e.pendingTxsSubscriptions {
|
|
|
|
if err := sub(txs); err != nil {
|
|
|
|
delete(e.pendingTxsSubscriptions, i)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|