2019-11-27 12:22:23 +00:00
|
|
|
// +build !nimbus
|
|
|
|
|
2018-04-11 15:41:51 +00:00
|
|
|
package shhext
|
|
|
|
|
|
|
|
import (
|
2019-06-26 16:17:41 +00:00
|
|
|
"context"
|
|
|
|
"fmt"
|
2018-12-12 09:39:00 +00:00
|
|
|
"time"
|
2018-04-11 15:41:51 +00:00
|
|
|
|
2020-01-20 20:56:06 +00:00
|
|
|
"github.com/syndtr/goleveldb/leveldb"
|
2019-10-09 14:22:53 +00:00
|
|
|
|
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/rpc"
|
2019-07-01 09:39:51 +00:00
|
|
|
|
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/params"
|
|
|
|
"github.com/status-im/status-go/services/ext"
|
2018-04-11 15:41:51 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Service struct {
|
2020-01-20 20:56:06 +00:00
|
|
|
*ext.Service
|
|
|
|
w types.Whisper
|
2018-09-24 18:07:34 +00:00
|
|
|
}
|
|
|
|
|
2020-01-20 20:56:06 +00:00
|
|
|
func New(config params.ShhextConfig, n types.Node, ctx interface{}, handler ext.EnvelopeEventsHandler, ldb *leveldb.DB) *Service {
|
2019-11-23 17:57:05 +00:00
|
|
|
w, err := n.GetWhisper(ctx)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2020-01-20 20:56:06 +00:00
|
|
|
delay := ext.DefaultRequestsDelay
|
2019-01-15 09:21:33 +00:00
|
|
|
if config.RequestsDelay != 0 {
|
|
|
|
delay = config.RequestsDelay
|
|
|
|
}
|
2020-01-20 20:56:06 +00:00
|
|
|
requestsRegistry := ext.NewRequestsRegistry(delay)
|
|
|
|
mailMonitor := ext.NewMailRequestMonitor(w, handler, requestsRegistry)
|
2018-04-11 15:41:51 +00:00
|
|
|
return &Service{
|
2020-01-20 20:56:06 +00:00
|
|
|
Service: ext.New(config, n, ldb, mailMonitor, requestsRegistry, w),
|
|
|
|
w: w,
|
2020-01-17 12:39:09 +00:00
|
|
|
}
|
2020-01-10 18:59:01 +00:00
|
|
|
}
|
|
|
|
|
2020-01-20 20:56:06 +00:00
|
|
|
func (s *Service) PublicWhisperAPI() types.PublicWhisperAPI {
|
|
|
|
return s.w.PublicWhisperAPI()
|
2018-04-11 15:41:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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-20 20:56:06 +00:00
|
|
|
Namespace: "shhext",
|
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
|
|
|
}
|
|
|
|
|
2020-01-20 20:56:06 +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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|