2018-04-11 15:41:51 +00:00
|
|
|
package shhext
|
|
|
|
|
|
|
|
import (
|
2018-04-26 05:56:19 +00:00
|
|
|
"crypto/ecdsa"
|
2018-04-11 15:41:51 +00:00
|
|
|
"sync"
|
2018-06-15 15:12:31 +00:00
|
|
|
"time"
|
2018-04-11 15:41:51 +00:00
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/log"
|
|
|
|
"github.com/ethereum/go-ethereum/node"
|
|
|
|
"github.com/ethereum/go-ethereum/p2p"
|
|
|
|
"github.com/ethereum/go-ethereum/rpc"
|
|
|
|
whisper "github.com/ethereum/go-ethereum/whisper/whisperv6"
|
2018-04-20 11:26:54 +00:00
|
|
|
"github.com/status-im/status-go/services/shhext/dedup"
|
2018-05-02 12:14:08 +00:00
|
|
|
"github.com/syndtr/goleveldb/leveldb"
|
2018-04-11 15:41:51 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// EnvelopeState in local tracker
|
|
|
|
type EnvelopeState int
|
|
|
|
|
|
|
|
const (
|
|
|
|
// EnvelopePosted is set when envelope was added to a local whisper queue.
|
|
|
|
EnvelopePosted EnvelopeState = iota
|
|
|
|
// EnvelopeSent is set when envelope is sent to atleast one peer.
|
|
|
|
EnvelopeSent
|
2018-06-15 15:12:31 +00:00
|
|
|
// MailServerRequestSent is set when p2p request is sent to the mailserver
|
|
|
|
MailServerRequestSent
|
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 {
|
|
|
|
EnvelopeSent(common.Hash)
|
|
|
|
EnvelopeExpired(common.Hash)
|
2018-07-02 07:38:10 +00:00
|
|
|
MailServerRequestCompleted(common.Hash, common.Hash, []byte)
|
2018-06-15 15:12:31 +00:00
|
|
|
MailServerRequestExpired(common.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 {
|
2018-04-20 11:26:54 +00:00
|
|
|
w *whisper.Whisper
|
|
|
|
tracker *tracker
|
|
|
|
nodeID *ecdsa.PrivateKey
|
2018-05-02 12:14:08 +00:00
|
|
|
deduplicator *dedup.Deduplicator
|
2018-06-25 13:27:17 +00:00
|
|
|
debug bool
|
2018-04-11 15:41:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure that Service implements node.Service interface.
|
|
|
|
var _ node.Service = (*Service)(nil)
|
|
|
|
|
|
|
|
// New returns a new Service.
|
2018-06-25 13:27:17 +00:00
|
|
|
func New(w *whisper.Whisper, handler EnvelopeEventsHandler, db *leveldb.DB, debug bool) *Service {
|
2018-04-11 15:41:51 +00:00
|
|
|
track := &tracker{
|
|
|
|
w: w,
|
|
|
|
handler: handler,
|
|
|
|
cache: map[common.Hash]EnvelopeState{},
|
|
|
|
}
|
|
|
|
return &Service{
|
2018-04-20 11:26:54 +00:00
|
|
|
w: w,
|
|
|
|
tracker: track,
|
2018-05-02 12:14:08 +00:00
|
|
|
deduplicator: dedup.NewDeduplicator(w, db),
|
2018-06-25 13:27:17 +00:00
|
|
|
debug: debug,
|
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
|
|
|
{
|
|
|
|
Namespace: "shhext",
|
|
|
|
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
|
|
|
|
|
|
|
if s.debug {
|
|
|
|
apis = append(apis, rpc.API{
|
|
|
|
Namespace: "debug",
|
|
|
|
Version: "1.0",
|
|
|
|
Service: NewDebugAPI(s),
|
|
|
|
Public: true,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
|
|
|
s.tracker.Start()
|
2018-04-26 05:56:19 +00:00
|
|
|
s.nodeID = server.PrivateKey
|
2018-04-11 15:41:51 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop is run when a service is stopped.
|
|
|
|
// It does nothing in this case but is required by `node.Service` interface.
|
|
|
|
func (s *Service) Stop() error {
|
|
|
|
s.tracker.Stop()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// tracker responsible for processing events for envelopes that we are interested in
|
|
|
|
// and calling specified handler.
|
|
|
|
type tracker struct {
|
|
|
|
w *whisper.Whisper
|
2018-04-13 05:52:22 +00:00
|
|
|
handler EnvelopeEventsHandler
|
2018-04-11 15:41:51 +00:00
|
|
|
|
|
|
|
mu sync.Mutex
|
|
|
|
cache map[common.Hash]EnvelopeState
|
|
|
|
|
|
|
|
wg sync.WaitGroup
|
|
|
|
quit chan struct{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start processing events.
|
|
|
|
func (t *tracker) Start() {
|
|
|
|
t.quit = make(chan struct{})
|
|
|
|
t.wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
t.handleEnvelopeEvents()
|
|
|
|
t.wg.Done()
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop process events.
|
|
|
|
func (t *tracker) Stop() {
|
|
|
|
close(t.quit)
|
|
|
|
t.wg.Wait()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add hash to a tracker.
|
|
|
|
func (t *tracker) Add(hash common.Hash) {
|
|
|
|
t.mu.Lock()
|
|
|
|
defer t.mu.Unlock()
|
|
|
|
t.cache[hash] = EnvelopePosted
|
|
|
|
}
|
|
|
|
|
2018-06-15 15:12:31 +00:00
|
|
|
// Add request hash to a tracker.
|
|
|
|
func (t *tracker) AddRequest(hash common.Hash, timerC <-chan time.Time) {
|
|
|
|
t.mu.Lock()
|
|
|
|
defer t.mu.Unlock()
|
|
|
|
t.cache[hash] = MailServerRequestSent
|
|
|
|
go t.expireRequest(hash, timerC)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *tracker) expireRequest(hash common.Hash, timerC <-chan time.Time) {
|
|
|
|
select {
|
|
|
|
case <-t.quit:
|
|
|
|
return
|
|
|
|
case <-timerC:
|
|
|
|
t.handleEvent(whisper.EnvelopeEvent{
|
|
|
|
Event: whisper.EventMailServerRequestExpired,
|
|
|
|
Hash: hash,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-11 15:41:51 +00:00
|
|
|
// handleEnvelopeEvents processes whisper envelope events
|
|
|
|
func (t *tracker) handleEnvelopeEvents() {
|
|
|
|
events := make(chan whisper.EnvelopeEvent, 100) // must be buffered to prevent blocking whisper
|
|
|
|
sub := t.w.SubscribeEnvelopeEvents(events)
|
|
|
|
defer sub.Unsubscribe()
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-t.quit:
|
|
|
|
return
|
|
|
|
case event := <-events:
|
|
|
|
t.handleEvent(event)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// handleEvent based on type of the event either triggers
|
|
|
|
// confirmation handler or removes hash from tracker
|
|
|
|
func (t *tracker) handleEvent(event whisper.EnvelopeEvent) {
|
2018-06-15 15:12:31 +00:00
|
|
|
handlers := map[whisper.EventType]func(whisper.EnvelopeEvent){
|
|
|
|
whisper.EventEnvelopeSent: t.handleEventEnvelopeSent,
|
|
|
|
whisper.EventEnvelopeExpired: t.handleEventEnvelopeExpired,
|
|
|
|
whisper.EventMailServerRequestCompleted: t.handleEventMailServerRequestCompleted,
|
|
|
|
whisper.EventMailServerRequestExpired: t.handleEventMailServerRequestExpired,
|
|
|
|
}
|
|
|
|
|
|
|
|
if handler, ok := handlers[event.Event]; ok {
|
|
|
|
handler(event)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *tracker) handleEventEnvelopeSent(event whisper.EnvelopeEvent) {
|
2018-04-11 15:41:51 +00:00
|
|
|
t.mu.Lock()
|
|
|
|
defer t.mu.Unlock()
|
2018-06-15 15:12:31 +00:00
|
|
|
|
|
|
|
state, ok := t.cache[event.Hash]
|
|
|
|
// if we didn't send a message using extension - skip it
|
|
|
|
// if message was already confirmed - skip it
|
|
|
|
if !ok || state == EnvelopeSent {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
log.Debug("envelope is sent", "hash", event.Hash, "peer", event.Peer)
|
|
|
|
t.cache[event.Hash] = EnvelopeSent
|
|
|
|
if t.handler != nil {
|
|
|
|
t.handler.EnvelopeSent(event.Hash)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *tracker) handleEventEnvelopeExpired(event whisper.EnvelopeEvent) {
|
|
|
|
t.mu.Lock()
|
|
|
|
defer t.mu.Unlock()
|
|
|
|
|
|
|
|
if state, ok := t.cache[event.Hash]; ok {
|
|
|
|
log.Debug("envelope expired", "hash", event.Hash, "state", state)
|
|
|
|
delete(t.cache, event.Hash)
|
|
|
|
if state == EnvelopeSent {
|
2018-04-11 15:41:51 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if t.handler != nil {
|
2018-06-15 15:12:31 +00:00
|
|
|
t.handler.EnvelopeExpired(event.Hash)
|
2018-04-11 15:41:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-06-15 15:12:31 +00:00
|
|
|
|
|
|
|
func (t *tracker) handleEventMailServerRequestCompleted(event whisper.EnvelopeEvent) {
|
|
|
|
t.mu.Lock()
|
|
|
|
defer t.mu.Unlock()
|
|
|
|
|
|
|
|
state, ok := t.cache[event.Hash]
|
|
|
|
if !ok || state != MailServerRequestSent {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
log.Debug("mailserver response received", "hash", event.Hash)
|
|
|
|
delete(t.cache, event.Hash)
|
|
|
|
if t.handler != nil {
|
2018-07-02 07:38:10 +00:00
|
|
|
if resp, ok := event.Data.(*whisper.MailServerResponse); ok {
|
|
|
|
t.handler.MailServerRequestCompleted(event.Hash, resp.LastEnvelopeHash, resp.Cursor)
|
|
|
|
}
|
2018-06-15 15:12:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *tracker) handleEventMailServerRequestExpired(event whisper.EnvelopeEvent) {
|
|
|
|
t.mu.Lock()
|
|
|
|
defer t.mu.Unlock()
|
|
|
|
|
|
|
|
state, ok := t.cache[event.Hash]
|
|
|
|
if !ok || state != MailServerRequestSent {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
log.Debug("mailserver response expired", "hash", event.Hash)
|
|
|
|
delete(t.cache, event.Hash)
|
|
|
|
if t.handler != nil {
|
|
|
|
t.handler.MailServerRequestExpired(event.Hash)
|
|
|
|
}
|
|
|
|
}
|