status-go/signal/events_shhext.go
Andrea Maria Piana d60a6713fe Enable mailserver cycle by default
This commit enables mailserver cycle logic by default and make a few
changes:

1) Nodes are graylisted instead of being blacklisted for a set amount of
   time. The reason is that if we blacklist, any cut in connectivity
   might result in long delays before reconnecting, especially on spotty
   connections.

2) Fixes an issue on the devp2p server, whereby the node would not
   connect to one of the static nodes since all the connection slots
   where filled. The fix is a bit inelegant, it always connects to
   static nodes, ignoring maxpeers, but it's tricky to get it to work
   since the code is clearly not written to select a specific node.

3) Adds support to pinned mailservers

4) Add retries to mailservers requests. It uses a closure for now, I
   think we should eventually have a channel etc, but I'd leave that for
   later.
2022-03-19 08:56:22 +00:00

229 lines
7.4 KiB
Go

package signal
import (
"encoding/hex"
"encoding/json"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/status-im/status-go/eth-node/types"
)
const (
// EventEnvelopeSent is triggered when envelope was sent at least to a one peer.
EventEnvelopeSent = "envelope.sent"
// EventEnvelopeExpired is triggered when envelop was dropped by a whisper without being sent
// to any peer
EventEnvelopeExpired = "envelope.expired"
// EventMailServerRequestCompleted is triggered when whisper receives a message ack from the mailserver
EventMailServerRequestCompleted = "mailserver.request.completed"
// EventMailServerRequestExpired is triggered when request TTL ends
EventMailServerRequestExpired = "mailserver.request.expired"
// EventEnodeDiscovered is tiggered when enode has been discovered.
EventEnodeDiscovered = "enode.discovered"
// EventDecryptMessageFailed is triggered when we receive a message from a bundle we don't have
EventDecryptMessageFailed = "messages.decrypt.failed"
// EventBundleAdded is triggered when we receive a bundle
EventBundleAdded = "bundles.added"
// EventNewMessages is triggered when we receive new messages
EventNewMessages = "messages.new"
// EventHistoryRequestStarted is triggered before processing a mailserver batch
EventHistoryRequestStarted = "history.request.started"
// EventHistoryBatchProcessed is triggered after processing a mailserver batch
EventHistoryBatchProcessed = "history.request.batch.processed"
// EventHistoryRequestCompleted is triggered after processing all mailserver batches
EventHistoryRequestCompleted = "history.request.completed"
// EventHistoryRequestFailed is triggered when requesting history messages fails
EventHistoryRequestFailed = "history.request.failed"
// EventBackupPerformed is triggered when a backup has been performed
EventBackupPerformed = "backup.performed"
// EventMailserverAvailable is triggered when a mailserver becomes available
EventMailserverAvailable = "mailserver.available"
// EventMailserverChanged is triggered when switching the active mailserver
EventMailserverChanged = "mailserver.changed"
// EventMailserverNotWorking is triggered when the mailserver has failed to connect or failed to respond to requests
EventMailserverNotWorking = "mailserver.not.working"
)
// EnvelopeSignal includes hash of the envelope.
type EnvelopeSignal struct {
IDs []hexutil.Bytes `json:"ids"`
Hash types.Hash `json:"hash"`
Message string `json:"message"`
}
// MailServerResponseSignal holds the data received in the response from the mailserver.
type MailServerResponseSignal struct {
RequestID types.Hash `json:"requestID"`
LastEnvelopeHash types.Hash `json:"lastEnvelopeHash"`
Cursor string `json:"cursor"`
ErrorMsg string `json:"errorMessage"`
}
type HistoryMessagesSignal struct {
RequestID string `json:"requestId"`
BatchIndex int `json:"batchIndex"`
NumBatches int `json:"numBatches,omitempty"`
ErrorMsg string `json:"errorMessage,omitempty"`
}
// DecryptMessageFailedSignal holds the sender of the message that could not be decrypted
type DecryptMessageFailedSignal struct {
Sender string `json:"sender"`
}
// BundleAddedSignal holds the identity and installation id of the user
type BundleAddedSignal struct {
Identity string `json:"identity"`
InstallationID string `json:"installationID"`
}
type MailserverSignal struct {
Address string `json:"address"`
ID string `json:"id"`
}
type Filter struct {
// ChatID is the identifier of the chat
ChatID string `json:"chatId"`
// SymKeyID is the symmetric key id used for symmetric chats
SymKeyID string `json:"symKeyId"`
// OneToOne tells us if we need to use asymmetric encryption for this chat
Listen bool `json:"listen"`
// FilterID the whisper filter id generated
FilterID string `json:"filterId"`
// Identity is the public key of the other recipient for non-public chats
Identity string `json:"identity"`
// Topic is the whisper topic
Topic types.TopicType `json:"topic"`
}
// SendEnvelopeSent triggered when envelope delivered at least to 1 peer.
func SendEnvelopeSent(identifiers [][]byte) {
var hexIdentifiers []hexutil.Bytes
for _, i := range identifiers {
hexIdentifiers = append(hexIdentifiers, i)
}
send(EventEnvelopeSent, EnvelopeSignal{
IDs: hexIdentifiers,
})
}
// SendEnvelopeExpired triggered when envelope delivered at least to 1 peer.
func SendEnvelopeExpired(identifiers [][]byte, err error) {
var message string
if err != nil {
message = err.Error()
}
var hexIdentifiers []hexutil.Bytes
for _, i := range identifiers {
hexIdentifiers = append(hexIdentifiers, i)
}
send(EventEnvelopeExpired, EnvelopeSignal{IDs: hexIdentifiers, Message: message})
}
func SendHistoricMessagesRequestStarted(requestID string, numBatches int) {
send(EventHistoryRequestStarted, HistoryMessagesSignal{RequestID: requestID, NumBatches: numBatches})
}
func SendHistoricMessagesRequestBatchProcessed(requestID string, batchIndex int, numBatches int) {
send(EventHistoryBatchProcessed, HistoryMessagesSignal{RequestID: requestID, BatchIndex: batchIndex, NumBatches: numBatches})
}
func SendHistoricMessagesRequestFailed(requestID string, err error) {
send(EventHistoryRequestFailed, HistoryMessagesSignal{RequestID: requestID, ErrorMsg: err.Error()})
}
func SendHistoricMessagesRequestCompleted(requestID string) {
send(EventHistoryRequestCompleted, HistoryMessagesSignal{RequestID: requestID})
}
// SendMailServerRequestCompleted triggered when mail server response has been received
func SendMailServerRequestCompleted(requestID types.Hash, lastEnvelopeHash types.Hash, cursor []byte, err error) {
errorMsg := ""
if err != nil {
errorMsg = err.Error()
}
sig := MailServerResponseSignal{
RequestID: requestID,
LastEnvelopeHash: lastEnvelopeHash,
Cursor: hex.EncodeToString(cursor),
ErrorMsg: errorMsg,
}
send(EventMailServerRequestCompleted, sig)
}
// SendMailServerRequestExpired triggered when mail server request expires
func SendMailServerRequestExpired(hash types.Hash) {
send(EventMailServerRequestExpired, EnvelopeSignal{Hash: hash})
}
// EnodeDiscoveredSignal includes enode address and topic
type EnodeDiscoveredSignal struct {
Enode string `json:"enode"`
Topic string `json:"topic"`
}
// BackupPerformedSignal signals that a backup has been performed
type BackupPerformedSignal struct {
LastBackup uint64 `json:"lastBackup"`
}
// SendEnodeDiscovered tiggered when an enode is discovered.
// finds a new enode.
func SendEnodeDiscovered(enode, topic string) {
send(EventEnodeDiscovered, EnodeDiscoveredSignal{
Enode: enode,
Topic: topic,
})
}
func SendBackupPerformed(lastBackup uint64) {
send(EventBackupPerformed, BackupPerformedSignal{lastBackup})
}
func SendDecryptMessageFailed(sender string) {
send(EventDecryptMessageFailed, DecryptMessageFailedSignal{sender})
}
func SendBundleAdded(identity string, installationID string) {
send(EventBundleAdded, BundleAddedSignal{Identity: identity, InstallationID: installationID})
}
func SendNewMessages(obj json.Marshaler) {
send(EventNewMessages, obj)
}
func SendMailserverAvailable(nodeAddress, id string) {
send(EventMailserverAvailable, MailserverSignal{
Address: nodeAddress,
ID: id,
})
}
func SendMailserverChanged(nodeAddress, id string) {
send(EventMailserverChanged, MailserverSignal{
Address: nodeAddress,
ID: id,
})
}
func SendMailserverNotWorking() {
send(EventMailserverNotWorking, MailserverSignal{})
}