2019-08-09 07:03:10 +00:00
package whisper
2019-02-20 06:57:57 +00:00
import (
2019-03-01 13:36:21 +00:00
"context"
2019-04-02 10:40:45 +00:00
"errors"
2019-02-20 06:57:57 +00:00
"sync"
2020-01-13 19:17:30 +00:00
"github.com/status-im/status-go/protocol/transport"
2019-08-09 07:03:10 +00:00
"go.uber.org/zap"
2020-01-02 09:10:19 +00:00
"github.com/status-im/status-go/eth-node/types"
2019-02-20 06:57:57 +00:00
)
2019-08-09 07:03:10 +00:00
// EnvelopeState in local tracker
type EnvelopeState int
const (
// NotRegistered returned if asked hash wasn't registered in the tracker.
NotRegistered EnvelopeState = - 1
// EnvelopePosted is set when envelope was added to a local whisper queue.
EnvelopePosted EnvelopeState = iota
2020-01-13 19:17:30 +00:00
// EnvelopeSent is set when envelope is sent to at least one peer.
2019-08-09 07:03:10 +00:00
EnvelopeSent
)
// EnvelopeEventsHandler used for two different event types.
type EnvelopeEventsHandler interface {
EnvelopeSent ( [ ] [ ] byte )
EnvelopeExpired ( [ ] [ ] byte , error )
2019-11-23 17:57:05 +00:00
MailServerRequestCompleted ( types . Hash , types . Hash , [ ] byte , error )
MailServerRequestExpired ( types . Hash )
2019-03-01 13:36:21 +00:00
}
// NewEnvelopesMonitor returns a pointer to an instance of the EnvelopesMonitor.
2020-01-13 19:17:30 +00:00
func NewEnvelopesMonitor ( w types . Whisper , config transport . EnvelopesMonitorConfig ) * EnvelopesMonitor {
2019-08-09 07:03:10 +00:00
logger := config . Logger
if logger == nil {
logger = zap . NewNop ( )
}
2019-11-23 17:57:05 +00:00
var whisperAPI types . PublicWhisperAPI
2019-10-09 14:22:53 +00:00
if w != nil {
whisperAPI = w . PublicWhisperAPI ( )
}
2019-03-01 13:36:21 +00:00
return & EnvelopesMonitor {
w : w ,
2019-10-09 14:22:53 +00:00
whisperAPI : whisperAPI ,
2019-08-09 07:03:10 +00:00
handler : config . EnvelopeEventsHandler ,
mailServerConfirmation : config . MailserverConfirmationsEnabled ,
maxAttempts : config . MaxAttempts ,
isMailserver : config . IsMailserver ,
logger : logger . With ( zap . Namespace ( "EnvelopesMonitor" ) ) ,
2019-03-01 13:36:21 +00:00
// key is envelope hash (event.Hash)
2019-11-23 17:57:05 +00:00
envelopes : map [ types . Hash ] EnvelopeState { } ,
messages : map [ types . Hash ] * types . NewMessage { } ,
attempts : map [ types . Hash ] int { } ,
identifiers : make ( map [ types . Hash ] [ ] [ ] byte ) ,
2019-03-01 13:36:21 +00:00
// key is hash of the batch (event.Batch)
2019-11-23 17:57:05 +00:00
batches : map [ types . Hash ] map [ types . Hash ] struct { } { } ,
2019-03-01 13:36:21 +00:00
}
}
2019-02-20 06:57:57 +00:00
// EnvelopesMonitor is responsible for monitoring whisper envelopes state.
type EnvelopesMonitor struct {
2019-11-23 17:57:05 +00:00
w types . Whisper
whisperAPI types . PublicWhisperAPI
2019-02-20 06:57:57 +00:00
handler EnvelopeEventsHandler
mailServerConfirmation bool
2019-03-01 13:36:21 +00:00
maxAttempts int
mu sync . Mutex
2019-11-23 17:57:05 +00:00
envelopes map [ types . Hash ] EnvelopeState
batches map [ types . Hash ] map [ types . Hash ] struct { }
2019-02-20 06:57:57 +00:00
2019-11-23 17:57:05 +00:00
messages map [ types . Hash ] * types . NewMessage
attempts map [ types . Hash ] int
identifiers map [ types . Hash ] [ ] [ ] byte
2019-02-20 06:57:57 +00:00
2019-08-09 07:03:10 +00:00
wg sync . WaitGroup
quit chan struct { }
2019-11-23 17:57:05 +00:00
isMailserver func ( peer types . EnodeID ) bool
2019-02-20 06:57:57 +00:00
2019-08-09 07:03:10 +00:00
logger * zap . Logger
2019-02-20 06:57:57 +00:00
}
// Start processing events.
func ( m * EnvelopesMonitor ) Start ( ) {
m . quit = make ( chan struct { } )
m . wg . Add ( 1 )
go func ( ) {
m . handleEnvelopeEvents ( )
m . wg . Done ( )
} ( )
}
// Stop process events.
func ( m * EnvelopesMonitor ) Stop ( ) {
close ( m . quit )
m . wg . Wait ( )
}
// Add hash to a tracker.
2019-11-23 17:57:05 +00:00
func ( m * EnvelopesMonitor ) Add ( identifiers [ ] [ ] byte , envelopeHash types . Hash , message types . NewMessage ) {
2019-02-20 06:57:57 +00:00
m . mu . Lock ( )
defer m . mu . Unlock ( )
2019-03-01 13:36:21 +00:00
m . envelopes [ envelopeHash ] = EnvelopePosted
2019-08-09 07:03:10 +00:00
m . identifiers [ envelopeHash ] = identifiers
2019-10-09 14:22:53 +00:00
m . messages [ envelopeHash ] = & message
2019-03-01 13:36:21 +00:00
m . attempts [ envelopeHash ] = 1
2019-02-20 06:57:57 +00:00
}
2019-11-23 17:57:05 +00:00
func ( m * EnvelopesMonitor ) GetState ( hash types . Hash ) EnvelopeState {
2019-02-20 06:57:57 +00:00
m . mu . Lock ( )
defer m . mu . Unlock ( )
2019-03-01 13:36:21 +00:00
state , exist := m . envelopes [ hash ]
if ! exist {
return NotRegistered
}
return state
}
2019-02-20 06:57:57 +00:00
// handleEnvelopeEvents processes whisper envelope events
func ( m * EnvelopesMonitor ) handleEnvelopeEvents ( ) {
2019-11-23 17:57:05 +00:00
events := make ( chan types . EnvelopeEvent , 100 ) // must be buffered to prevent blocking whisper
2019-02-20 06:57:57 +00:00
sub := m . w . SubscribeEnvelopeEvents ( events )
2019-10-09 14:22:53 +00:00
defer func ( ) {
close ( events )
sub . Unsubscribe ( )
} ( )
2019-02-20 06:57:57 +00:00
for {
select {
case <- m . quit :
2019-10-09 14:22:53 +00:00
return
2019-02-20 06:57:57 +00:00
case event := <- events :
m . handleEvent ( event )
}
}
}
// handleEvent based on type of the event either triggers
// confirmation handler or removes hash from tracker
2019-11-23 17:57:05 +00:00
func ( m * EnvelopesMonitor ) handleEvent ( event types . EnvelopeEvent ) {
handlers := map [ types . EventType ] func ( types . EnvelopeEvent ) {
types . EventEnvelopeSent : m . handleEventEnvelopeSent ,
types . EventEnvelopeExpired : m . handleEventEnvelopeExpired ,
types . EventBatchAcknowledged : m . handleAcknowledgedBatch ,
types . EventEnvelopeReceived : m . handleEventEnvelopeReceived ,
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 * EnvelopesMonitor ) handleEventEnvelopeSent ( event types . EnvelopeEvent ) {
2019-02-20 06:57:57 +00:00
if m . mailServerConfirmation {
if ! m . isMailserver ( event . Peer ) {
return
}
}
m . mu . Lock ( )
defer m . mu . Unlock ( )
2019-03-01 13:36:21 +00:00
state , ok := m . envelopes [ event . Hash ]
2019-02-20 06:57:57 +00:00
// if we didn't send a message using extension - skip it
// if message was already confirmed - skip it
if ! ok || state == EnvelopeSent {
return
}
2019-08-09 07:03:10 +00:00
m . logger . Debug ( "envelope is sent" , zap . String ( "hash" , event . Hash . String ( ) ) , zap . String ( "peer" , event . Peer . String ( ) ) )
2019-11-23 17:57:05 +00:00
if event . Batch != ( types . Hash { } ) {
2019-02-20 06:57:57 +00:00
if _ , ok := m . batches [ event . Batch ] ; ! ok {
2019-11-23 17:57:05 +00:00
m . batches [ event . Batch ] = map [ types . Hash ] struct { } { }
2019-02-20 06:57:57 +00:00
}
m . batches [ event . Batch ] [ event . Hash ] = struct { } { }
2019-08-09 07:03:10 +00:00
m . logger . Debug ( "waiting for a confirmation" , zap . String ( "batch" , event . Batch . String ( ) ) )
2019-02-20 06:57:57 +00:00
} else {
2019-03-01 13:36:21 +00:00
m . envelopes [ event . Hash ] = EnvelopeSent
2019-02-20 06:57:57 +00:00
if m . handler != nil {
2019-08-09 07:03:10 +00:00
m . handler . EnvelopeSent ( m . identifiers [ event . Hash ] )
2019-02-20 06:57:57 +00:00
}
}
}
2019-11-23 17:57:05 +00:00
func ( m * EnvelopesMonitor ) handleAcknowledgedBatch ( event types . EnvelopeEvent ) {
2019-02-20 06:57:57 +00:00
if m . mailServerConfirmation {
if ! m . isMailserver ( event . Peer ) {
return
}
}
m . mu . Lock ( )
defer m . mu . Unlock ( )
envelopes , ok := m . batches [ event . Batch ]
if ! ok {
2019-08-09 07:03:10 +00:00
m . logger . Debug ( "batch is not found" , zap . String ( "batch" , event . Batch . String ( ) ) )
2019-02-20 06:57:57 +00:00
}
2019-08-09 07:03:10 +00:00
m . logger . Debug ( "received a confirmation" , zap . String ( "batch" , event . Batch . String ( ) ) , zap . String ( "peer" , event . Peer . String ( ) ) )
2019-11-23 17:57:05 +00:00
envelopeErrors , ok := event . Data . ( [ ] types . EnvelopeError )
2019-04-02 10:40:45 +00:00
if event . Data != nil && ! ok {
2020-01-23 15:12:50 +00:00
m . logger . Error ( "received unexpected data in the the confirmation event" , zap . Any ( "data" , event . Data ) )
2019-04-02 10:40:45 +00:00
}
2019-11-23 17:57:05 +00:00
failedEnvelopes := map [ types . Hash ] struct { } { }
2019-04-02 10:40:45 +00:00
for i := range envelopeErrors {
envelopeError := envelopeErrors [ i ]
_ , exist := m . envelopes [ envelopeError . Hash ]
if exist {
2019-08-09 07:03:10 +00:00
m . logger . Warn ( "envelope that was posted by us is discarded" , zap . String ( "hash" , envelopeError . Hash . String ( ) ) , zap . String ( "peer" , event . Peer . String ( ) ) , zap . String ( "error" , envelopeError . Description ) )
2019-04-02 10:40:45 +00:00
var err error
switch envelopeError . Code {
2019-11-23 17:57:05 +00:00
case types . EnvelopeTimeNotSynced :
2019-04-02 10:40:45 +00:00
err = errors . New ( "envelope wasn't delivered due to time sync issues" )
}
m . handleEnvelopeFailure ( envelopeError . Hash , err )
}
failedEnvelopes [ envelopeError . Hash ] = struct { } { }
}
2019-02-20 06:57:57 +00:00
for hash := range envelopes {
2019-04-02 10:40:45 +00:00
if _ , exist := failedEnvelopes [ hash ] ; exist {
continue
}
2019-03-01 13:36:21 +00:00
state , ok := m . envelopes [ hash ]
2019-02-20 06:57:57 +00:00
if ! ok || state == EnvelopeSent {
continue
}
2019-03-01 13:36:21 +00:00
m . envelopes [ hash ] = EnvelopeSent
2019-02-20 06:57:57 +00:00
if m . handler != nil {
2019-08-09 07:03:10 +00:00
m . handler . EnvelopeSent ( m . identifiers [ hash ] )
2019-02-20 06:57:57 +00:00
}
}
delete ( m . batches , event . Batch )
}
2019-11-23 17:57:05 +00:00
func ( m * EnvelopesMonitor ) handleEventEnvelopeExpired ( event types . EnvelopeEvent ) {
2019-02-20 06:57:57 +00:00
m . mu . Lock ( )
defer m . mu . Unlock ( )
2019-04-02 10:40:45 +00:00
m . handleEnvelopeFailure ( event . Hash , errors . New ( "envelope expired due to connectivity issues" ) )
}
// handleEnvelopeFailure is a common code path for processing envelopes failures. not thread safe, lock
// must be used on a higher level.
2019-11-23 17:57:05 +00:00
func ( m * EnvelopesMonitor ) handleEnvelopeFailure ( hash types . Hash , err error ) {
2019-04-02 10:40:45 +00:00
if state , ok := m . envelopes [ hash ] ; ok {
message , exist := m . messages [ hash ]
2019-03-01 13:36:21 +00:00
if ! exist {
2019-08-09 07:03:10 +00:00
m . logger . Error ( "message was deleted erroneously" , zap . String ( "envelope hash" , hash . String ( ) ) )
2019-03-01 13:36:21 +00:00
}
2019-04-02 10:40:45 +00:00
attempt := m . attempts [ hash ]
2019-08-09 07:03:10 +00:00
identifiers := m . identifiers [ hash ]
2019-04-02 10:40:45 +00:00
m . clearMessageState ( hash )
2019-02-20 06:57:57 +00:00
if state == EnvelopeSent {
return
}
2019-03-01 13:36:21 +00:00
if attempt < m . maxAttempts {
2019-08-09 07:03:10 +00:00
m . logger . Debug ( "retrying to send a message" , zap . String ( "hash" , hash . String ( ) ) , zap . Int ( "attempt" , attempt + 1 ) )
2019-10-09 14:22:53 +00:00
hex , err := m . whisperAPI . Post ( context . TODO ( ) , * message )
2019-03-01 13:36:21 +00:00
if err != nil {
2019-08-09 07:03:10 +00:00
m . logger . Error ( "failed to retry sending message" , zap . String ( "hash" , hash . String ( ) ) , zap . Int ( "attempt" , attempt + 1 ) , zap . Error ( err ) )
if m . handler != nil {
m . handler . EnvelopeExpired ( identifiers , err )
}
2019-03-01 13:36:21 +00:00
}
2019-11-23 17:57:05 +00:00
envelopeID := types . BytesToHash ( hex )
2019-03-01 13:36:21 +00:00
m . envelopes [ envelopeID ] = EnvelopePosted
m . messages [ envelopeID ] = message
m . attempts [ envelopeID ] = attempt + 1
2019-08-06 21:50:13 +00:00
m . identifiers [ envelopeID ] = identifiers
2019-03-01 13:36:21 +00:00
} else {
2019-08-09 07:03:10 +00:00
m . logger . Debug ( "envelope expired" , zap . String ( "hash" , hash . String ( ) ) )
2019-03-01 13:36:21 +00:00
if m . handler != nil {
2019-08-09 07:03:10 +00:00
m . handler . EnvelopeExpired ( identifiers , err )
2019-03-01 13:36:21 +00:00
}
2019-02-20 06:57:57 +00:00
}
}
}
2019-11-23 17:57:05 +00:00
func ( m * EnvelopesMonitor ) handleEventEnvelopeReceived ( event types . EnvelopeEvent ) {
2019-02-20 06:57:57 +00:00
if m . mailServerConfirmation {
if ! m . isMailserver ( event . Peer ) {
return
}
}
m . mu . Lock ( )
defer m . mu . Unlock ( )
2019-03-01 13:36:21 +00:00
state , ok := m . envelopes [ event . Hash ]
2019-02-20 06:57:57 +00:00
if ! ok || state != EnvelopePosted {
return
}
2019-08-09 07:03:10 +00:00
m . logger . Debug ( "expected envelope received" , zap . String ( "hash" , event . Hash . String ( ) ) , zap . String ( "peer" , event . Peer . String ( ) ) )
2019-03-01 13:36:21 +00:00
m . envelopes [ event . Hash ] = EnvelopeSent
2019-02-20 06:57:57 +00:00
if m . handler != nil {
2019-08-09 07:03:10 +00:00
m . handler . EnvelopeSent ( m . identifiers [ event . Hash ] )
2019-02-20 06:57:57 +00:00
}
}
2019-03-01 13:36:21 +00:00
// clearMessageState removes all message and envelope state.
// not thread-safe, should be protected on a higher level.
2019-11-23 17:57:05 +00:00
func ( m * EnvelopesMonitor ) clearMessageState ( envelopeID types . Hash ) {
2019-03-01 13:36:21 +00:00
delete ( m . envelopes , envelopeID )
delete ( m . messages , envelopeID )
delete ( m . attempts , envelopeID )
2019-08-09 07:03:10 +00:00
delete ( m . identifiers , envelopeID )
2019-03-01 13:36:21 +00:00
}