2020-01-20 20:56:06 +00:00
|
|
|
package ext
|
2019-02-20 06:57:57 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/log"
|
2020-01-02 09:10:19 +00:00
|
|
|
|
2024-09-26 22:37:32 +00:00
|
|
|
"github.com/status-im/status-go/common"
|
2019-11-23 17:57:05 +00:00
|
|
|
"github.com/status-im/status-go/eth-node/types"
|
2020-01-20 20:56:06 +00:00
|
|
|
"github.com/status-im/status-go/services/ext/mailservers"
|
2019-02-20 06:57:57 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// EnvelopeState in local tracker
|
|
|
|
type EnvelopeState int
|
|
|
|
|
|
|
|
const (
|
|
|
|
// NotRegistered returned if asked hash wasn't registered in the tracker.
|
|
|
|
NotRegistered EnvelopeState = -1
|
|
|
|
// MailServerRequestSent is set when p2p request is sent to the mailserver
|
|
|
|
MailServerRequestSent
|
|
|
|
)
|
|
|
|
|
|
|
|
// MailRequestMonitor is responsible for monitoring history request to mailservers.
|
|
|
|
type MailRequestMonitor struct {
|
2020-01-20 20:56:06 +00:00
|
|
|
eventSub mailservers.EnvelopeEventSubscriber
|
|
|
|
handler EnvelopeEventsHandler
|
2019-02-20 06:57:57 +00:00
|
|
|
|
|
|
|
mu sync.Mutex
|
2019-11-23 17:57:05 +00:00
|
|
|
cache map[types.Hash]EnvelopeState
|
2019-02-20 06:57:57 +00:00
|
|
|
|
|
|
|
requestsRegistry *RequestsRegistry
|
|
|
|
|
|
|
|
wg sync.WaitGroup
|
|
|
|
quit chan struct{}
|
|
|
|
}
|
|
|
|
|
2020-01-20 20:56:06 +00:00
|
|
|
func NewMailRequestMonitor(eventSub mailservers.EnvelopeEventSubscriber, h EnvelopeEventsHandler, reg *RequestsRegistry) *MailRequestMonitor {
|
|
|
|
return &MailRequestMonitor{
|
|
|
|
eventSub: eventSub,
|
|
|
|
handler: h,
|
|
|
|
cache: make(map[types.Hash]EnvelopeState),
|
|
|
|
requestsRegistry: reg,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-20 06:57:57 +00:00
|
|
|
// Start processing events.
|
|
|
|
func (m *MailRequestMonitor) Start() {
|
|
|
|
m.quit = make(chan struct{})
|
|
|
|
m.wg.Add(1)
|
|
|
|
go func() {
|
2024-09-26 22:37:32 +00:00
|
|
|
defer common.LogOnPanic()
|
2019-02-20 06:57:57 +00:00
|
|
|
m.handleEnvelopeEvents()
|
|
|
|
m.wg.Done()
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop process events.
|
|
|
|
func (m *MailRequestMonitor) Stop() {
|
|
|
|
close(m.quit)
|
|
|
|
m.wg.Wait()
|
|
|
|
}
|
|
|
|
|
2019-11-23 17:57:05 +00:00
|
|
|
func (m *MailRequestMonitor) GetState(hash types.Hash) EnvelopeState {
|
2019-02-20 06:57:57 +00:00
|
|
|
m.mu.Lock()
|
|
|
|
defer m.mu.Unlock()
|
|
|
|
state, exist := m.cache[hash]
|
|
|
|
if !exist {
|
|
|
|
return NotRegistered
|
|
|
|
}
|
|
|
|
return state
|
|
|
|
}
|
|
|
|
|
|
|
|
// handleEnvelopeEvents processes whisper envelope events
|
|
|
|
func (m *MailRequestMonitor) handleEnvelopeEvents() {
|
2019-11-23 17:57:05 +00:00
|
|
|
events := make(chan types.EnvelopeEvent, 100) // must be buffered to prevent blocking whisper
|
2020-01-20 20:56:06 +00:00
|
|
|
sub := m.eventSub.SubscribeEnvelopeEvents(events)
|
2019-02-20 06:57:57 +00:00
|
|
|
defer sub.Unsubscribe()
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-m.quit:
|
|
|
|
return
|
|
|
|
case event := <-events:
|
|
|
|
m.handleEvent(event)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// handleEvent based on type of the event either triggers
|
|
|
|
// confirmation handler or removes hash from MailRequestMonitor
|
2019-11-23 17:57:05 +00:00
|
|
|
func (m *MailRequestMonitor) handleEvent(event types.EnvelopeEvent) {
|
|
|
|
handlers := map[types.EventType]func(types.EnvelopeEvent){
|
|
|
|
types.EventMailServerRequestSent: m.handleRequestSent,
|
|
|
|
types.EventMailServerRequestCompleted: m.handleEventMailServerRequestCompleted,
|
|
|
|
types.EventMailServerRequestExpired: m.handleEventMailServerRequestExpired,
|
2019-02-20 06:57:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if handler, ok := handlers[event.Event]; ok {
|
|
|
|
handler(event)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-23 17:57:05 +00:00
|
|
|
func (m *MailRequestMonitor) handleRequestSent(event types.EnvelopeEvent) {
|
2019-02-20 06:57:57 +00:00
|
|
|
m.mu.Lock()
|
|
|
|
defer m.mu.Unlock()
|
|
|
|
m.cache[event.Hash] = MailServerRequestSent
|
|
|
|
}
|
|
|
|
|
2019-11-23 17:57:05 +00:00
|
|
|
func (m *MailRequestMonitor) handleEventMailServerRequestCompleted(event types.EnvelopeEvent) {
|
2019-02-20 06:57:57 +00:00
|
|
|
m.mu.Lock()
|
|
|
|
defer m.mu.Unlock()
|
|
|
|
m.requestsRegistry.Unregister(event.Hash)
|
|
|
|
state, ok := m.cache[event.Hash]
|
|
|
|
if !ok || state != MailServerRequestSent {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
log.Debug("mailserver response received", "hash", event.Hash)
|
|
|
|
delete(m.cache, event.Hash)
|
|
|
|
if m.handler != nil {
|
2019-11-23 17:57:05 +00:00
|
|
|
if resp, ok := event.Data.(*types.MailServerResponse); ok {
|
2019-02-20 06:57:57 +00:00
|
|
|
m.handler.MailServerRequestCompleted(event.Hash, resp.LastEnvelopeHash, resp.Cursor, resp.Error)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-23 17:57:05 +00:00
|
|
|
func (m *MailRequestMonitor) handleEventMailServerRequestExpired(event types.EnvelopeEvent) {
|
2019-02-20 06:57:57 +00:00
|
|
|
m.mu.Lock()
|
|
|
|
defer m.mu.Unlock()
|
|
|
|
m.requestsRegistry.Unregister(event.Hash)
|
|
|
|
state, ok := m.cache[event.Hash]
|
|
|
|
if !ok || state != MailServerRequestSent {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
log.Debug("mailserver response expired", "hash", event.Hash)
|
|
|
|
delete(m.cache, event.Hash)
|
|
|
|
if m.handler != nil {
|
|
|
|
m.handler.MailServerRequestExpired(event.Hash)
|
|
|
|
}
|
|
|
|
}
|