2018-04-11 15:41:51 +00:00
|
|
|
package shhext
|
|
|
|
|
|
|
|
import (
|
2019-06-26 16:17:41 +00:00
|
|
|
"context"
|
2018-04-26 05:56:19 +00:00
|
|
|
"crypto/ecdsa"
|
2019-08-27 12:04:15 +00:00
|
|
|
"database/sql"
|
2019-06-26 16:17:41 +00:00
|
|
|
"fmt"
|
2020-01-10 18:59:01 +00:00
|
|
|
"math/big"
|
2019-07-01 09:39:51 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2018-12-12 09:39:00 +00:00
|
|
|
"time"
|
2018-04-11 15:41:51 +00:00
|
|
|
|
2019-10-09 14:22:53 +00:00
|
|
|
"github.com/status-im/status-go/logutils"
|
|
|
|
|
2020-01-10 18:59:01 +00:00
|
|
|
commongethtypes "github.com/ethereum/go-ethereum/common"
|
|
|
|
gethtypes "github.com/ethereum/go-ethereum/core/types"
|
|
|
|
"github.com/ethereum/go-ethereum/ethclient"
|
2019-05-17 11:06:56 +00:00
|
|
|
"github.com/ethereum/go-ethereum/log"
|
2018-04-11 15:41:51 +00:00
|
|
|
"github.com/ethereum/go-ethereum/node"
|
|
|
|
"github.com/ethereum/go-ethereum/p2p"
|
2018-11-21 10:22:30 +00:00
|
|
|
"github.com/ethereum/go-ethereum/p2p/enode"
|
2018-04-11 15:41:51 +00:00
|
|
|
"github.com/ethereum/go-ethereum/rpc"
|
2019-07-01 09:39:51 +00:00
|
|
|
|
2019-04-30 06:46:12 +00:00
|
|
|
"github.com/status-im/status-go/db"
|
2020-01-10 18:59:01 +00:00
|
|
|
"github.com/status-im/status-go/multiaccounts/accounts"
|
2019-01-17 12:56:22 +00:00
|
|
|
"github.com/status-im/status-go/params"
|
2018-12-05 13:57:05 +00:00
|
|
|
"github.com/status-im/status-go/services/shhext/mailservers"
|
2019-07-01 09:39:51 +00:00
|
|
|
"github.com/status-im/status-go/signal"
|
|
|
|
|
2020-01-02 09:10:19 +00:00
|
|
|
"github.com/syndtr/goleveldb/leveldb"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
|
2020-01-10 18:59:01 +00:00
|
|
|
coretypes "github.com/status-im/status-go/eth-node/core/types"
|
2019-11-23 17:57:05 +00:00
|
|
|
"github.com/status-im/status-go/eth-node/types"
|
2020-01-13 19:17:30 +00:00
|
|
|
"github.com/status-im/status-go/protocol"
|
|
|
|
"github.com/status-im/status-go/protocol/transport"
|
2018-04-11 15:41:51 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2018-12-05 13:57:05 +00:00
|
|
|
// defaultConnectionsTarget used in Service.Start if configured connection target is 0.
|
|
|
|
defaultConnectionsTarget = 1
|
2018-12-12 09:39:00 +00:00
|
|
|
// defaultTimeoutWaitAdded is a timeout to use to establish initial connections.
|
|
|
|
defaultTimeoutWaitAdded = 5 * time.Second
|
2018-04-11 15:41:51 +00:00
|
|
|
)
|
|
|
|
|
2018-04-13 05:52:22 +00:00
|
|
|
// EnvelopeEventsHandler used for two different event types.
|
|
|
|
type EnvelopeEventsHandler interface {
|
2019-08-09 07:03:10 +00:00
|
|
|
EnvelopeSent([][]byte)
|
|
|
|
EnvelopeExpired([][]byte, error)
|
2019-11-23 17:57:05 +00:00
|
|
|
MailServerRequestCompleted(types.Hash, types.Hash, []byte, error)
|
|
|
|
MailServerRequestExpired(types.Hash)
|
2018-04-13 05:52:22 +00:00
|
|
|
}
|
2018-04-11 15:41:51 +00:00
|
|
|
|
|
|
|
// Service is a service that provides some additional Whisper API.
|
|
|
|
type Service struct {
|
2020-01-13 19:17:30 +00:00
|
|
|
apiName string
|
|
|
|
messenger *protocol.Messenger
|
|
|
|
identity *ecdsa.PrivateKey
|
|
|
|
cancelMessenger chan struct{}
|
2019-05-06 06:33:19 +00:00
|
|
|
storage db.TransactionalStorage
|
2019-11-23 17:57:05 +00:00
|
|
|
n types.Node
|
|
|
|
w types.Whisper
|
2019-01-15 09:21:33 +00:00
|
|
|
config params.ShhextConfig
|
2019-02-20 06:57:57 +00:00
|
|
|
mailMonitor *MailRequestMonitor
|
2019-01-15 09:21:33 +00:00
|
|
|
requestsRegistry *RequestsRegistry
|
2019-04-30 06:46:12 +00:00
|
|
|
historyUpdates *HistoryUpdateReactor
|
2019-01-15 09:21:33 +00:00
|
|
|
server *p2p.Server
|
|
|
|
nodeID *ecdsa.PrivateKey
|
2019-04-30 06:46:12 +00:00
|
|
|
peerStore *mailservers.PeerStore
|
|
|
|
cache *mailservers.Cache
|
|
|
|
connManager *mailservers.ConnectionManager
|
|
|
|
lastUsedMonitor *mailservers.LastUsedConnectionMonitor
|
2020-01-10 18:59:01 +00:00
|
|
|
accountsDB *accounts.Database
|
2018-09-24 18:07:34 +00:00
|
|
|
}
|
|
|
|
|
2018-04-11 15:41:51 +00:00
|
|
|
// Make sure that Service implements node.Service interface.
|
|
|
|
var _ node.Service = (*Service)(nil)
|
|
|
|
|
2019-11-23 17:57:05 +00:00
|
|
|
// New returns a new shhext Service.
|
2020-01-13 19:17:30 +00:00
|
|
|
func New(n types.Node, ctx interface{}, apiName string, handler EnvelopeEventsHandler, ldb *leveldb.DB, config params.ShhextConfig) *Service {
|
2019-11-23 17:57:05 +00:00
|
|
|
w, err := n.GetWhisper(ctx)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2019-04-30 06:46:12 +00:00
|
|
|
cache := mailservers.NewCache(ldb)
|
2018-12-12 09:39:00 +00:00
|
|
|
ps := mailservers.NewPeerStore(cache)
|
2019-01-15 09:21:33 +00:00
|
|
|
delay := defaultRequestsDelay
|
|
|
|
if config.RequestsDelay != 0 {
|
|
|
|
delay = config.RequestsDelay
|
|
|
|
}
|
|
|
|
requestsRegistry := NewRequestsRegistry(delay)
|
2019-05-06 06:33:19 +00:00
|
|
|
historyUpdates := NewHistoryUpdateReactor()
|
2019-02-20 06:57:57 +00:00
|
|
|
mailMonitor := &MailRequestMonitor{
|
|
|
|
w: w,
|
|
|
|
handler: handler,
|
2019-11-23 17:57:05 +00:00
|
|
|
cache: map[types.Hash]EnvelopeState{},
|
2019-02-20 06:57:57 +00:00
|
|
|
requestsRegistry: requestsRegistry,
|
|
|
|
}
|
2018-04-11 15:41:51 +00:00
|
|
|
return &Service{
|
2020-01-13 19:17:30 +00:00
|
|
|
apiName: apiName,
|
2019-05-06 06:33:19 +00:00
|
|
|
storage: db.NewLevelDBStorage(ldb),
|
2019-11-23 17:57:05 +00:00
|
|
|
n: n,
|
2019-01-15 09:21:33 +00:00
|
|
|
w: w,
|
|
|
|
config: config,
|
2019-02-20 06:57:57 +00:00
|
|
|
mailMonitor: mailMonitor,
|
2019-01-15 09:21:33 +00:00
|
|
|
requestsRegistry: requestsRegistry,
|
2019-04-30 06:46:12 +00:00
|
|
|
historyUpdates: historyUpdates,
|
2019-01-15 09:21:33 +00:00
|
|
|
peerStore: ps,
|
|
|
|
cache: cache,
|
2018-04-11 15:41:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-18 22:24:38 +00:00
|
|
|
func (s *Service) InitProtocol(identity *ecdsa.PrivateKey, db *sql.DB) error { // nolint: gocyclo
|
2019-07-01 09:39:51 +00:00
|
|
|
if !s.config.PFSEnabled {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-12-18 22:24:38 +00:00
|
|
|
// If Messenger has been already set up, we need to shut it down
|
|
|
|
// before we init it again. Otherwise, it will lead to goroutines leakage
|
|
|
|
// due to not stopped filters.
|
|
|
|
if s.messenger != nil {
|
|
|
|
if err := s.messenger.Shutdown(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
s.identity = identity
|
|
|
|
|
2019-07-01 09:39:51 +00:00
|
|
|
dataDir := filepath.Clean(s.config.BackupDisabledDataDir)
|
|
|
|
|
|
|
|
if err := os.MkdirAll(dataDir, os.ModePerm); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-07-17 22:25:42 +00:00
|
|
|
|
2019-11-21 16:19:22 +00:00
|
|
|
// Create a custom zap.Logger which will forward logs from status-go/protocol to status-go logger.
|
2019-08-09 07:03:10 +00:00
|
|
|
zapLogger, err := logutils.NewZapLoggerWithAdapter(logutils.Logger())
|
2019-07-01 09:39:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-01-13 19:17:30 +00:00
|
|
|
envelopesMonitorConfig := &transport.EnvelopesMonitorConfig{
|
2019-08-09 07:03:10 +00:00
|
|
|
MaxAttempts: s.config.MaxMessageDeliveryAttempts,
|
|
|
|
MailserverConfirmationsEnabled: s.config.MailServerConfirmations,
|
2019-11-23 17:57:05 +00:00
|
|
|
IsMailserver: func(peer types.EnodeID) bool {
|
2019-08-09 07:03:10 +00:00
|
|
|
return s.peerStore.Exist(peer)
|
|
|
|
},
|
|
|
|
EnvelopeEventsHandler: EnvelopeSignalHandler{},
|
|
|
|
Logger: zapLogger,
|
|
|
|
}
|
2019-08-27 12:04:15 +00:00
|
|
|
options := buildMessengerOptions(s.config, db, envelopesMonitorConfig, zapLogger)
|
2019-08-09 07:03:10 +00:00
|
|
|
|
2019-07-17 22:25:42 +00:00
|
|
|
messenger, err := protocol.NewMessenger(
|
|
|
|
identity,
|
2019-11-23 17:57:05 +00:00
|
|
|
s.n,
|
2019-07-17 22:25:42 +00:00
|
|
|
s.config.InstallationID,
|
2019-07-26 07:17:29 +00:00
|
|
|
options...,
|
2019-07-17 22:25:42 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-07-01 09:39:51 +00:00
|
|
|
}
|
2020-01-10 18:59:01 +00:00
|
|
|
s.accountsDB = accounts.NewDB(db)
|
2019-07-17 22:25:42 +00:00
|
|
|
s.messenger = messenger
|
|
|
|
// Start a loop that retrieves all messages and propagates them to status-react.
|
|
|
|
s.cancelMessenger = make(chan struct{})
|
|
|
|
go s.retrieveMessagesLoop(time.Second, s.cancelMessenger)
|
2020-01-10 18:59:01 +00:00
|
|
|
go s.verifyTransactionLoop(30*time.Second, s.cancelMessenger)
|
2019-07-01 09:39:51 +00:00
|
|
|
|
2019-12-02 15:34:05 +00:00
|
|
|
return s.messenger.Init()
|
2019-07-17 22:25:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Service) retrieveMessagesLoop(tick time.Duration, cancel <-chan struct{}) {
|
|
|
|
ticker := time.NewTicker(tick)
|
|
|
|
defer ticker.Stop()
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ticker.C:
|
Move to protobuf for Message type (#1706)
* Use a single Message type `v1/message.go` and `message.go` are the same now, and they embed `protobuf.ChatMessage`
* Use `SendChatMessage` for sending chat messages, this is basically the old `Send` but a bit more flexible so we can send different message types (stickers,commands), and not just text.
* Remove dedup from services/shhext. Because now we process in status-protocol, dedup makes less sense, as those messages are going to be processed anyway, so removing for now, we can re-evaluate if bringing it to status-go or not.
* Change the various retrieveX method to a single one:
`RetrieveAll` will be processing those messages that it can process (Currently only `Message`), and return the rest in `RawMessages` (still transit). The format for the response is:
`Chats`: -> The chats updated by receiving the message
`Messages`: -> The messages retrieved (already matched to a chat)
`Contacts`: -> The contacts updated by the messages
`RawMessages` -> Anything else that can't be parsed, eventually as we move everything to status-protocol-go this will go away.
2019-12-05 16:25:34 +00:00
|
|
|
response, err := s.messenger.RetrieveAll()
|
2019-07-05 12:45:47 +00:00
|
|
|
if err != nil {
|
2019-07-17 22:25:42 +00:00
|
|
|
log.Error("failed to retrieve raw messages", "err", err)
|
2019-07-05 12:45:47 +00:00
|
|
|
continue
|
|
|
|
}
|
Move to protobuf for Message type (#1706)
* Use a single Message type `v1/message.go` and `message.go` are the same now, and they embed `protobuf.ChatMessage`
* Use `SendChatMessage` for sending chat messages, this is basically the old `Send` but a bit more flexible so we can send different message types (stickers,commands), and not just text.
* Remove dedup from services/shhext. Because now we process in status-protocol, dedup makes less sense, as those messages are going to be processed anyway, so removing for now, we can re-evaluate if bringing it to status-go or not.
* Change the various retrieveX method to a single one:
`RetrieveAll` will be processing those messages that it can process (Currently only `Message`), and return the rest in `RawMessages` (still transit). The format for the response is:
`Chats`: -> The chats updated by receiving the message
`Messages`: -> The messages retrieved (already matched to a chat)
`Contacts`: -> The contacts updated by the messages
`RawMessages` -> Anything else that can't be parsed, eventually as we move everything to status-protocol-go this will go away.
2019-12-05 16:25:34 +00:00
|
|
|
if !response.IsEmpty() {
|
|
|
|
PublisherSignalHandler{}.NewMessages(response)
|
2019-07-17 22:25:42 +00:00
|
|
|
}
|
|
|
|
case <-cancel:
|
|
|
|
return
|
2019-07-01 09:39:51 +00:00
|
|
|
}
|
|
|
|
}
|
2019-07-17 22:25:42 +00:00
|
|
|
}
|
2019-07-05 12:45:47 +00:00
|
|
|
|
2020-01-10 18:59:01 +00:00
|
|
|
type verifyTransactionClient struct {
|
|
|
|
chainID *big.Int
|
|
|
|
url string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *verifyTransactionClient) TransactionByHash(ctx context.Context, hash types.Hash) (coretypes.Message, bool, error) {
|
|
|
|
signer := gethtypes.NewEIP155Signer(c.chainID)
|
|
|
|
client, err := ethclient.Dial(c.url)
|
|
|
|
if err != nil {
|
|
|
|
return coretypes.Message{}, false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
transaction, pending, err := client.TransactionByHash(ctx, commongethtypes.BytesToHash(hash.Bytes()))
|
|
|
|
if err != nil {
|
|
|
|
return coretypes.Message{}, false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
message, err := transaction.AsMessage(signer)
|
|
|
|
if err != nil {
|
|
|
|
return coretypes.Message{}, false, err
|
|
|
|
}
|
|
|
|
from := types.BytesToAddress(message.From().Bytes())
|
|
|
|
to := types.BytesToAddress(message.To().Bytes())
|
|
|
|
|
|
|
|
return coretypes.NewMessage(
|
|
|
|
from,
|
|
|
|
&to,
|
|
|
|
message.Nonce(),
|
|
|
|
message.Value(),
|
|
|
|
message.Gas(),
|
|
|
|
message.GasPrice(),
|
|
|
|
message.Data(),
|
|
|
|
message.CheckNonce(),
|
|
|
|
), pending, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Service) verifyTransactionLoop(tick time.Duration, cancel <-chan struct{}) {
|
|
|
|
if s.config.VerifyTransactionURL == "" {
|
|
|
|
log.Warn("not starting transaction loop")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ticker := time.NewTicker(tick)
|
|
|
|
defer ticker.Stop()
|
|
|
|
|
|
|
|
ctx, cancelVerifyTransaction := context.WithCancel(context.Background())
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ticker.C:
|
|
|
|
accounts, err := s.accountsDB.GetAccounts()
|
|
|
|
if err != nil {
|
|
|
|
log.Error("failed to retrieve accounts", "err", err)
|
|
|
|
}
|
|
|
|
var wallets []types.Address
|
|
|
|
for _, account := range accounts {
|
|
|
|
if account.Wallet {
|
|
|
|
wallets = append(wallets, types.BytesToAddress(account.Address.Bytes()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
response, err := s.messenger.ValidateTransactions(ctx, wallets)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("failed to validate transactions", "err", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if !response.IsEmpty() {
|
|
|
|
PublisherSignalHandler{}.NewMessages(response)
|
|
|
|
}
|
|
|
|
case <-cancel:
|
|
|
|
cancelVerifyTransaction()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-17 22:25:42 +00:00
|
|
|
func (s *Service) ConfirmMessagesProcessed(messageIDs [][]byte) error {
|
|
|
|
return s.messenger.ConfirmMessagesProcessed(messageIDs)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Service) EnableInstallation(installationID string) error {
|
|
|
|
return s.messenger.EnableInstallation(installationID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// DisableInstallation disables an installation for multi-device sync.
|
|
|
|
func (s *Service) DisableInstallation(installationID string) error {
|
|
|
|
return s.messenger.DisableInstallation(installationID)
|
2019-07-01 09:39:51 +00:00
|
|
|
}
|
|
|
|
|
2018-11-21 10:22:30 +00:00
|
|
|
// UpdateMailservers updates information about selected mail servers.
|
2018-12-12 09:39:00 +00:00
|
|
|
func (s *Service) UpdateMailservers(nodes []*enode.Node) error {
|
|
|
|
if err := s.peerStore.Update(nodes); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-12-05 13:57:05 +00:00
|
|
|
if s.connManager != nil {
|
|
|
|
s.connManager.Notify(nodes)
|
|
|
|
}
|
2018-12-12 09:39:00 +00:00
|
|
|
return nil
|
2018-11-21 10:22:30 +00:00
|
|
|
}
|
|
|
|
|
2018-04-11 15:41:51 +00:00
|
|
|
// Protocols returns a new protocols list. In this case, there are none.
|
|
|
|
func (s *Service) Protocols() []p2p.Protocol {
|
|
|
|
return []p2p.Protocol{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// APIs returns a list of new APIs.
|
|
|
|
func (s *Service) APIs() []rpc.API {
|
2018-06-25 13:27:17 +00:00
|
|
|
apis := []rpc.API{
|
2018-04-11 15:41:51 +00:00
|
|
|
{
|
2020-01-13 19:17:30 +00:00
|
|
|
Namespace: s.apiName,
|
2018-04-11 15:41:51 +00:00
|
|
|
Version: "1.0",
|
2018-04-26 05:56:19 +00:00
|
|
|
Service: NewPublicAPI(s),
|
2018-04-11 15:41:51 +00:00
|
|
|
Public: true,
|
|
|
|
},
|
|
|
|
}
|
2018-06-25 13:27:17 +00:00
|
|
|
return apis
|
2018-04-11 15:41:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Start is run when a service is started.
|
|
|
|
// It does nothing in this case but is required by `node.Service` interface.
|
|
|
|
func (s *Service) Start(server *p2p.Server) error {
|
2018-12-05 13:57:05 +00:00
|
|
|
if s.config.EnableConnectionManager {
|
|
|
|
connectionsTarget := s.config.ConnectionTarget
|
|
|
|
if connectionsTarget == 0 {
|
|
|
|
connectionsTarget = defaultConnectionsTarget
|
|
|
|
}
|
2019-01-21 14:00:10 +00:00
|
|
|
maxFailures := s.config.MaxServerFailures
|
|
|
|
// if not defined change server on first expired event
|
|
|
|
if maxFailures == 0 {
|
|
|
|
maxFailures = 1
|
|
|
|
}
|
|
|
|
s.connManager = mailservers.NewConnectionManager(server, s.w, connectionsTarget, maxFailures, defaultTimeoutWaitAdded)
|
2018-12-05 13:57:05 +00:00
|
|
|
s.connManager.Start()
|
2018-12-12 09:39:00 +00:00
|
|
|
if err := mailservers.EnsureUsedRecordsAddedFirst(s.peerStore, s.connManager); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if s.config.EnableLastUsedMonitor {
|
|
|
|
s.lastUsedMonitor = mailservers.NewLastUsedConnectionMonitor(s.peerStore, s.cache, s.w)
|
|
|
|
s.lastUsedMonitor.Start()
|
2018-12-05 13:57:05 +00:00
|
|
|
}
|
2019-02-20 06:57:57 +00:00
|
|
|
s.mailMonitor.Start()
|
2018-04-26 05:56:19 +00:00
|
|
|
s.nodeID = server.PrivateKey
|
2018-12-05 13:57:05 +00:00
|
|
|
s.server = server
|
2019-07-05 12:45:47 +00:00
|
|
|
return nil
|
2019-06-03 14:29:14 +00:00
|
|
|
}
|
|
|
|
|
2018-04-11 15:41:51 +00:00
|
|
|
// Stop is run when a service is stopped.
|
|
|
|
func (s *Service) Stop() error {
|
2019-06-03 14:29:14 +00:00
|
|
|
log.Info("Stopping shhext service")
|
2018-12-05 13:57:05 +00:00
|
|
|
if s.config.EnableConnectionManager {
|
|
|
|
s.connManager.Stop()
|
|
|
|
}
|
2018-12-12 09:39:00 +00:00
|
|
|
if s.config.EnableLastUsedMonitor {
|
|
|
|
s.lastUsedMonitor.Stop()
|
|
|
|
}
|
2019-02-26 12:55:01 +00:00
|
|
|
s.requestsRegistry.Clear()
|
2019-02-20 06:57:57 +00:00
|
|
|
s.mailMonitor.Stop()
|
2019-07-17 22:25:42 +00:00
|
|
|
|
|
|
|
if s.cancelMessenger != nil {
|
|
|
|
select {
|
|
|
|
case <-s.cancelMessenger:
|
|
|
|
// channel already closed
|
|
|
|
default:
|
|
|
|
close(s.cancelMessenger)
|
|
|
|
s.cancelMessenger = nil
|
2019-05-23 07:54:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-17 22:25:42 +00:00
|
|
|
if s.messenger != nil {
|
|
|
|
if err := s.messenger.Shutdown(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2019-05-17 11:06:56 +00:00
|
|
|
}
|
2019-06-26 16:17:41 +00:00
|
|
|
|
2019-11-23 17:57:05 +00:00
|
|
|
func (s *Service) syncMessages(ctx context.Context, mailServerID []byte, r types.SyncMailRequest) (resp types.SyncEventResponse, err error) {
|
2019-06-26 16:17:41 +00:00
|
|
|
err = s.w.SyncMessages(mailServerID, r)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for the response which is received asynchronously as a p2p packet.
|
|
|
|
// This packet handler will send an event which contains the response payload.
|
2019-11-23 17:57:05 +00:00
|
|
|
events := make(chan types.EnvelopeEvent, 1024)
|
2019-06-26 16:17:41 +00:00
|
|
|
sub := s.w.SubscribeEnvelopeEvents(events)
|
|
|
|
defer sub.Unsubscribe()
|
|
|
|
|
|
|
|
// Add explicit timeout context, otherwise the request
|
|
|
|
// can hang indefinitely if not specified by the sender.
|
|
|
|
// Sender is usually through netcat or some bash tool
|
|
|
|
// so it's not really possible to specify the timeout.
|
|
|
|
timeoutCtx, cancel := context.WithTimeout(ctx, time.Second*30)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case event := <-events:
|
2019-11-23 17:57:05 +00:00
|
|
|
if event.Event != types.EventMailServerSyncFinished {
|
2019-06-26 16:17:41 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Info("received EventMailServerSyncFinished event", "data", event.Data)
|
|
|
|
|
|
|
|
var ok bool
|
|
|
|
|
2019-11-23 17:57:05 +00:00
|
|
|
resp, ok = event.Data.(types.SyncEventResponse)
|
2019-06-26 16:17:41 +00:00
|
|
|
if !ok {
|
|
|
|
err = fmt.Errorf("did not understand the response event data")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return
|
|
|
|
case <-timeoutCtx.Done():
|
|
|
|
err = timeoutCtx.Err()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-07-17 22:25:42 +00:00
|
|
|
|
2020-01-13 19:17:30 +00:00
|
|
|
func onNegotiatedFilters(filters []*transport.Filter) {
|
2019-08-27 12:04:15 +00:00
|
|
|
var signalFilters []*signal.Filter
|
|
|
|
for _, filter := range filters {
|
|
|
|
|
|
|
|
signalFilter := &signal.Filter{
|
|
|
|
ChatID: filter.ChatID,
|
|
|
|
SymKeyID: filter.SymKeyID,
|
|
|
|
Listen: filter.Listen,
|
|
|
|
FilterID: filter.FilterID,
|
|
|
|
Identity: filter.Identity,
|
|
|
|
Topic: filter.Topic,
|
|
|
|
}
|
|
|
|
|
|
|
|
signalFilters = append(signalFilters, signalFilter)
|
|
|
|
}
|
|
|
|
if len(filters) != 0 {
|
|
|
|
handler := PublisherSignalHandler{}
|
|
|
|
handler.WhisperFilterAdded(signalFilters)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-13 19:17:30 +00:00
|
|
|
func buildMessengerOptions(
|
|
|
|
config params.ShhextConfig,
|
|
|
|
db *sql.DB,
|
|
|
|
envelopesMonitorConfig *transport.EnvelopesMonitorConfig,
|
|
|
|
logger *zap.Logger,
|
|
|
|
) []protocol.Option {
|
2019-07-26 07:17:29 +00:00
|
|
|
options := []protocol.Option{
|
2019-08-09 07:03:10 +00:00
|
|
|
protocol.WithCustomLogger(logger),
|
2019-08-27 12:04:15 +00:00
|
|
|
protocol.WithDatabase(db),
|
2019-08-09 07:03:10 +00:00
|
|
|
protocol.WithEnvelopesMonitorConfig(envelopesMonitorConfig),
|
2019-08-27 12:04:15 +00:00
|
|
|
protocol.WithOnNegotiatedFilters(onNegotiatedFilters),
|
2019-07-26 07:17:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if config.DataSyncEnabled {
|
|
|
|
options = append(options, protocol.WithDatasync())
|
|
|
|
}
|
|
|
|
|
2020-01-10 18:59:01 +00:00
|
|
|
if config.VerifyTransactionURL != "" {
|
|
|
|
client := &verifyTransactionClient{
|
|
|
|
url: config.VerifyTransactionURL,
|
|
|
|
chainID: big.NewInt(config.VerifyTransactionChainID),
|
|
|
|
}
|
|
|
|
options = append(options, protocol.WithVerifyTransactionClient(client))
|
|
|
|
}
|
|
|
|
|
2019-08-09 07:03:10 +00:00
|
|
|
return options
|
2019-07-17 22:25:42 +00:00
|
|
|
}
|