2018-05-11 19:43:07 +00:00
|
|
|
// Copyright 2017 The go-ethereum Authors
|
|
|
|
// This file is part of the go-ethereum library.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package mailserver
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/binary"
|
2018-05-21 11:30:37 +00:00
|
|
|
"errors"
|
2018-05-11 19:43:07 +00:00
|
|
|
"fmt"
|
2019-02-27 14:30:08 +00:00
|
|
|
"math/rand"
|
2018-07-04 09:30:57 +00:00
|
|
|
"sync"
|
2018-05-21 11:30:37 +00:00
|
|
|
|
2018-05-17 11:21:04 +00:00
|
|
|
"time"
|
2018-05-11 19:43:07 +00:00
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
|
|
|
"github.com/ethereum/go-ethereum/log"
|
|
|
|
"github.com/ethereum/go-ethereum/rlp"
|
2018-06-08 11:29:50 +00:00
|
|
|
"github.com/status-im/status-go/params"
|
2018-09-25 07:05:38 +00:00
|
|
|
whisper "github.com/status-im/whisper/whisperv6"
|
2018-05-11 19:43:07 +00:00
|
|
|
)
|
|
|
|
|
2018-05-17 11:21:04 +00:00
|
|
|
const (
|
|
|
|
maxQueryRange = 24 * time.Hour
|
2019-03-12 11:14:35 +00:00
|
|
|
defaultLimit = 2000
|
|
|
|
// When we default the upper limit, we want to extend the range a bit
|
|
|
|
// to accommodate for envelopes with slightly higher timestamp, in seconds
|
|
|
|
whisperTTLSafeThreshold = 60
|
2018-05-17 11:21:04 +00:00
|
|
|
)
|
|
|
|
|
2018-05-21 11:30:37 +00:00
|
|
|
var (
|
2018-07-04 09:30:57 +00:00
|
|
|
errDirectoryNotProvided = errors.New("data directory not provided")
|
|
|
|
errDecryptionMethodNotProvided = errors.New("decryption method is not provided")
|
2018-05-21 11:30:37 +00:00
|
|
|
)
|
|
|
|
|
2018-07-02 07:38:10 +00:00
|
|
|
const (
|
|
|
|
timestampLength = 4
|
|
|
|
requestLimitLength = 4
|
|
|
|
requestTimeRangeLength = timestampLength * 2
|
2019-02-22 08:55:37 +00:00
|
|
|
processRequestTimeout = time.Minute
|
2018-07-02 07:38:10 +00:00
|
|
|
)
|
|
|
|
|
2018-05-21 11:30:37 +00:00
|
|
|
// WMailServer whisper mailserver.
|
2018-05-11 19:43:07 +00:00
|
|
|
type WMailServer struct {
|
2019-05-13 09:25:46 +00:00
|
|
|
db DB
|
2018-07-16 09:07:17 +00:00
|
|
|
w *whisper.Whisper
|
|
|
|
pow float64
|
|
|
|
symFilter *whisper.Filter
|
|
|
|
asymFilter *whisper.Filter
|
2018-07-04 09:30:57 +00:00
|
|
|
|
2019-01-10 16:07:16 +00:00
|
|
|
muRateLimiter sync.RWMutex
|
|
|
|
rateLimiter *rateLimiter
|
2019-01-23 13:32:45 +00:00
|
|
|
|
|
|
|
cleaner *dbCleaner // removes old envelopes
|
2018-12-11 10:23:47 +00:00
|
|
|
}
|
|
|
|
|
2018-05-21 11:30:37 +00:00
|
|
|
// Init initializes mailServer.
|
2018-05-17 11:21:04 +00:00
|
|
|
func (s *WMailServer) Init(shh *whisper.Whisper, config *params.WhisperConfig) error {
|
|
|
|
if len(config.DataDir) == 0 {
|
2018-05-21 11:30:37 +00:00
|
|
|
return errDirectoryNotProvided
|
2018-05-11 19:43:07 +00:00
|
|
|
}
|
|
|
|
|
2018-09-13 16:31:29 +00:00
|
|
|
if len(config.MailServerPassword) == 0 && len(config.MailServerAsymKey) == 0 {
|
2018-07-04 09:30:57 +00:00
|
|
|
return errDecryptionMethodNotProvided
|
2018-05-11 19:43:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
s.w = shh
|
2018-05-17 11:21:04 +00:00
|
|
|
s.pow = config.MinimumPoW
|
2018-05-11 19:43:07 +00:00
|
|
|
|
2018-07-04 09:30:57 +00:00
|
|
|
if err := s.setupRequestMessageDecryptor(config); err != nil {
|
2018-05-21 11:30:37 +00:00
|
|
|
return err
|
|
|
|
}
|
2019-01-23 13:32:45 +00:00
|
|
|
|
|
|
|
if config.MailServerRateLimit > 0 {
|
|
|
|
s.setupRateLimiter(time.Duration(config.MailServerRateLimit) * time.Second)
|
|
|
|
}
|
2018-05-21 11:30:37 +00:00
|
|
|
|
2018-07-04 09:30:57 +00:00
|
|
|
// Open database in the last step in order not to init with error
|
|
|
|
// and leave the database open by accident.
|
2019-05-13 09:25:46 +00:00
|
|
|
if config.DatabaseConfig.PGConfig.Enabled {
|
|
|
|
log.Info("Connecting to postgres database")
|
|
|
|
database, err := NewPostgresDB(config)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("open DB: %s", err)
|
|
|
|
}
|
|
|
|
s.db = database
|
|
|
|
log.Info("Connected to postgres database")
|
|
|
|
} else {
|
|
|
|
// Defaults to LevelDB
|
|
|
|
database, err := NewLevelDB(config)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("open DB: %s", err)
|
|
|
|
}
|
|
|
|
s.db = database
|
2018-07-04 09:30:57 +00:00
|
|
|
}
|
|
|
|
|
2019-01-23 13:32:45 +00:00
|
|
|
if config.MailServerDataRetention > 0 {
|
|
|
|
// MailServerDataRetention is a number of days.
|
|
|
|
s.setupCleaner(time.Duration(config.MailServerDataRetention) * time.Hour * 24)
|
|
|
|
}
|
|
|
|
|
2018-05-21 11:30:37 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-01-10 16:07:16 +00:00
|
|
|
// setupRateLimiter in case limit is bigger than 0 it will setup an automated
|
2018-05-21 11:30:37 +00:00
|
|
|
// limit db cleanup.
|
2019-01-10 16:07:16 +00:00
|
|
|
func (s *WMailServer) setupRateLimiter(limit time.Duration) {
|
2019-01-23 13:32:45 +00:00
|
|
|
s.rateLimiter = newRateLimiter(limit)
|
|
|
|
s.rateLimiter.Start()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *WMailServer) setupCleaner(retention time.Duration) {
|
|
|
|
s.cleaner = newDBCleaner(s.db, retention)
|
|
|
|
s.cleaner.Start()
|
2018-05-21 11:30:37 +00:00
|
|
|
}
|
|
|
|
|
2018-07-04 09:30:57 +00:00
|
|
|
// setupRequestMessageDecryptor setup a Whisper filter to decrypt
|
|
|
|
// incoming Whisper requests.
|
|
|
|
func (s *WMailServer) setupRequestMessageDecryptor(config *params.WhisperConfig) error {
|
2018-07-16 09:07:17 +00:00
|
|
|
s.symFilter = nil
|
|
|
|
s.asymFilter = nil
|
2018-05-21 11:30:37 +00:00
|
|
|
|
2018-07-04 09:30:57 +00:00
|
|
|
if config.MailServerPassword != "" {
|
|
|
|
keyID, err := s.w.AddSymKeyFromPassword(config.MailServerPassword)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("create symmetric key: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
symKey, err := s.w.GetSymKey(keyID)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("save symmetric key: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-07-16 09:07:17 +00:00
|
|
|
s.symFilter = &whisper.Filter{KeySym: symKey}
|
2018-05-11 19:43:07 +00:00
|
|
|
}
|
2018-05-17 11:21:04 +00:00
|
|
|
|
2018-09-13 16:31:29 +00:00
|
|
|
if config.MailServerAsymKey != "" {
|
|
|
|
keyAsym, err := crypto.HexToECDSA(config.MailServerAsymKey)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
s.asymFilter = &whisper.Filter{KeyAsym: keyAsym}
|
2018-07-16 09:07:17 +00:00
|
|
|
}
|
2018-07-04 09:30:57 +00:00
|
|
|
|
2018-05-11 19:43:07 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-05-21 11:30:37 +00:00
|
|
|
// Close the mailserver and its associated db connection.
|
2018-05-11 19:43:07 +00:00
|
|
|
func (s *WMailServer) Close() {
|
|
|
|
if s.db != nil {
|
2018-05-17 11:21:04 +00:00
|
|
|
if err := s.db.Close(); err != nil {
|
|
|
|
log.Error(fmt.Sprintf("s.db.Close failed: %s", err))
|
|
|
|
}
|
|
|
|
}
|
2019-01-10 16:07:16 +00:00
|
|
|
if s.rateLimiter != nil {
|
|
|
|
s.rateLimiter.Stop()
|
2018-05-11 19:43:07 +00:00
|
|
|
}
|
2019-01-23 13:32:45 +00:00
|
|
|
if s.cleaner != nil {
|
|
|
|
s.cleaner.Stop()
|
|
|
|
}
|
2018-05-11 19:43:07 +00:00
|
|
|
}
|
|
|
|
|
2018-05-21 11:30:37 +00:00
|
|
|
// Archive a whisper envelope.
|
2018-05-11 19:43:07 +00:00
|
|
|
func (s *WMailServer) Archive(env *whisper.Envelope) {
|
2019-05-10 10:26:57 +00:00
|
|
|
if err := s.db.SaveEnvelope(env); err != nil {
|
|
|
|
log.Error("Could not save envelope", "hash", env.Hash())
|
2018-05-11 19:43:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-21 11:30:37 +00:00
|
|
|
// DeliverMail sends mail to specified whisper peer.
|
2018-05-11 19:43:07 +00:00
|
|
|
func (s *WMailServer) DeliverMail(peer *whisper.Peer, request *whisper.Envelope) {
|
2018-12-06 09:48:28 +00:00
|
|
|
|
2019-02-27 14:30:08 +00:00
|
|
|
startMethod := time.Now()
|
|
|
|
defer deliverMailTimer.UpdateSince(startMethod)
|
2018-12-06 09:48:28 +00:00
|
|
|
|
2018-06-15 09:19:17 +00:00
|
|
|
requestsMeter.Mark(1)
|
2018-06-11 10:48:42 +00:00
|
|
|
|
2018-05-11 19:43:07 +00:00
|
|
|
if peer == nil {
|
2018-05-30 12:31:47 +00:00
|
|
|
requestErrorsCounter.Inc(1)
|
2019-02-22 08:55:37 +00:00
|
|
|
log.Error("[mailserver:DeliverMail] peer is nil")
|
2018-05-11 19:43:07 +00:00
|
|
|
return
|
|
|
|
}
|
2019-02-27 14:30:08 +00:00
|
|
|
|
|
|
|
requestID := request.Hash().String()
|
|
|
|
peerID := peerIDString(peer)
|
|
|
|
|
|
|
|
log.Info("[mailserver:DeliverMail] delivering mail",
|
|
|
|
"peerID", peerID,
|
|
|
|
"requestID", requestID)
|
|
|
|
|
2018-05-22 15:51:05 +00:00
|
|
|
if s.exceedsPeerRequests(peer.ID()) {
|
2018-05-30 12:31:47 +00:00
|
|
|
requestErrorsCounter.Inc(1)
|
2019-02-27 14:30:08 +00:00
|
|
|
log.Error("[mailserver:DeliverMail] peer exceeded the limit",
|
|
|
|
"peerID", peerID,
|
|
|
|
"requestID", requestID)
|
2018-10-18 10:25:00 +00:00
|
|
|
s.trySendHistoricMessageErrorResponse(peer, request, fmt.Errorf("rate limit exceeded"))
|
2018-05-22 15:51:05 +00:00
|
|
|
return
|
|
|
|
}
|
2018-05-21 11:30:37 +00:00
|
|
|
|
2018-10-19 09:09:13 +00:00
|
|
|
var (
|
|
|
|
lower, upper uint32
|
|
|
|
bloom []byte
|
|
|
|
limit uint32
|
2018-12-11 10:23:47 +00:00
|
|
|
cursor []byte
|
2018-10-19 09:09:13 +00:00
|
|
|
batch bool
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
|
|
|
payload, err := s.decodeRequest(peer.ID(), request)
|
|
|
|
if err == nil {
|
|
|
|
lower, upper = payload.Lower, payload.Upper
|
|
|
|
bloom = payload.Bloom
|
2018-12-11 10:23:47 +00:00
|
|
|
cursor = payload.Cursor
|
2018-10-19 09:09:13 +00:00
|
|
|
limit = payload.Limit
|
|
|
|
batch = payload.Batch
|
|
|
|
} else {
|
2019-02-27 14:30:08 +00:00
|
|
|
log.Debug("[mailserver:DeliverMail] failed to decode request",
|
|
|
|
"err", err,
|
|
|
|
"peerID", peerID,
|
|
|
|
"requestID", requestID)
|
2018-10-19 09:09:13 +00:00
|
|
|
lower, upper, bloom, limit, cursor, err = s.validateRequest(peer.ID(), request)
|
|
|
|
}
|
|
|
|
|
2019-03-12 11:14:35 +00:00
|
|
|
if limit == 0 {
|
|
|
|
limit = defaultLimit
|
|
|
|
}
|
|
|
|
|
2018-10-19 09:09:13 +00:00
|
|
|
if err != nil {
|
2018-10-18 10:25:00 +00:00
|
|
|
requestValidationErrorsCounter.Inc(1)
|
2019-02-27 14:30:08 +00:00
|
|
|
log.Error("[mailserver:DeliverMail] request failed validaton",
|
|
|
|
"peerID", peerID,
|
|
|
|
"requestID", requestID)
|
2018-10-18 10:25:00 +00:00
|
|
|
s.trySendHistoricMessageErrorResponse(peer, request, err)
|
2018-10-19 09:09:13 +00:00
|
|
|
return
|
|
|
|
}
|
2018-10-18 10:25:00 +00:00
|
|
|
|
2019-02-27 14:30:08 +00:00
|
|
|
log.Info("[mailserver:DeliverMail] processing request",
|
|
|
|
"peerID", peerID,
|
|
|
|
"requestID", requestID,
|
2018-12-06 09:48:28 +00:00
|
|
|
"lower", lower,
|
|
|
|
"upper", upper,
|
2018-10-19 09:09:13 +00:00
|
|
|
"bloom", bloom,
|
|
|
|
"limit", limit,
|
|
|
|
"cursor", cursor,
|
2018-12-06 09:48:28 +00:00
|
|
|
"batch", batch,
|
|
|
|
)
|
2018-10-19 09:09:13 +00:00
|
|
|
|
2018-11-19 09:14:03 +00:00
|
|
|
if batch {
|
|
|
|
requestsBatchedCounter.Inc(1)
|
|
|
|
}
|
|
|
|
|
2019-05-13 09:25:46 +00:00
|
|
|
iter, err := s.createIterator(lower, upper, cursor, bloom, limit)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("[mailserver:DeliverMail] request failed",
|
|
|
|
"peerID", peerID,
|
|
|
|
"requestID", requestID)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-12-06 09:48:28 +00:00
|
|
|
defer iter.Release()
|
|
|
|
|
2019-05-09 10:58:02 +00:00
|
|
|
bundles := make(chan []rlp.RawValue, 5)
|
2018-12-06 09:48:28 +00:00
|
|
|
errCh := make(chan error)
|
2019-02-22 08:55:37 +00:00
|
|
|
cancelProcessing := make(chan struct{})
|
2018-12-06 09:48:28 +00:00
|
|
|
|
|
|
|
go func() {
|
2019-02-22 08:55:37 +00:00
|
|
|
counter := 0
|
2018-12-06 09:48:28 +00:00
|
|
|
for bundle := range bundles {
|
2019-05-09 10:58:02 +00:00
|
|
|
if err := s.sendRawEnvelopes(peer, bundle, batch); err != nil {
|
2019-02-22 08:55:37 +00:00
|
|
|
close(cancelProcessing)
|
2018-12-06 09:48:28 +00:00
|
|
|
errCh <- err
|
|
|
|
break
|
|
|
|
}
|
2019-02-22 08:55:37 +00:00
|
|
|
counter++
|
2018-12-06 09:48:28 +00:00
|
|
|
}
|
|
|
|
close(errCh)
|
2019-02-27 14:30:08 +00:00
|
|
|
log.Info("[mailserver:DeliverMail] finished sending bundles",
|
|
|
|
"peerID", peerID,
|
|
|
|
"requestID", requestID,
|
|
|
|
"counter", counter)
|
2018-12-06 09:48:28 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
start := time.Now()
|
|
|
|
nextPageCursor, lastEnvelopeHash := s.processRequestInBundles(
|
|
|
|
iter,
|
2018-10-19 09:09:13 +00:00
|
|
|
bloom,
|
2018-12-06 09:48:28 +00:00
|
|
|
int(limit),
|
2019-02-22 08:55:37 +00:00
|
|
|
processRequestTimeout,
|
2019-02-27 14:30:08 +00:00
|
|
|
requestID,
|
2018-12-06 09:48:28 +00:00
|
|
|
bundles,
|
2019-02-22 08:55:37 +00:00
|
|
|
cancelProcessing,
|
2018-12-06 09:48:28 +00:00
|
|
|
)
|
|
|
|
requestProcessTimer.UpdateSince(start)
|
|
|
|
|
|
|
|
// Wait for the goroutine to finish the work. It may return an error.
|
|
|
|
if err := <-errCh; err != nil {
|
|
|
|
processRequestErrorsCounter.Inc(1)
|
2019-02-27 14:30:08 +00:00
|
|
|
log.Error("[mailserver:DeliverMail] error while processing",
|
|
|
|
"err", err,
|
|
|
|
"peerID", peerID,
|
|
|
|
"requestID", requestID)
|
2018-12-06 09:48:28 +00:00
|
|
|
s.trySendHistoricMessageErrorResponse(peer, request, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Processing of the request could be finished earlier due to iterator error.
|
|
|
|
if err := iter.Error(); err != nil {
|
2018-10-19 09:09:13 +00:00
|
|
|
processRequestErrorsCounter.Inc(1)
|
2019-02-27 14:30:08 +00:00
|
|
|
log.Error("[mailserver:DeliverMail] iterator failed",
|
|
|
|
"err", err,
|
|
|
|
"peerID", peerID,
|
|
|
|
"requestID", requestID)
|
2018-10-19 09:09:13 +00:00
|
|
|
s.trySendHistoricMessageErrorResponse(peer, request, err)
|
|
|
|
return
|
|
|
|
}
|
2018-06-27 12:22:09 +00:00
|
|
|
|
2019-02-27 14:30:08 +00:00
|
|
|
log.Info("[mailserver:DeliverMail] sending historic message response",
|
|
|
|
"peerID", peerID,
|
|
|
|
"requestID", requestID,
|
|
|
|
"last", lastEnvelopeHash,
|
|
|
|
"next", nextPageCursor)
|
2018-10-19 09:09:13 +00:00
|
|
|
|
|
|
|
if err := s.sendHistoricMessageResponse(peer, request, lastEnvelopeHash, nextPageCursor); err != nil {
|
|
|
|
historicResponseErrorsCounter.Inc(1)
|
2019-02-27 14:30:08 +00:00
|
|
|
log.Error("[mailserver:DeliverMail] error sending historic message response",
|
|
|
|
"err", err,
|
|
|
|
"peerID", peerID,
|
|
|
|
"requestID", requestID)
|
2018-10-19 09:09:13 +00:00
|
|
|
// we still want to try to report error even it it is a p2p error and it is unlikely
|
|
|
|
s.trySendHistoricMessageErrorResponse(peer, request, err)
|
2018-05-21 11:30:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-06 09:48:28 +00:00
|
|
|
// SyncMail syncs mail servers between two Mail Servers.
|
|
|
|
func (s *WMailServer) SyncMail(peer *whisper.Peer, request whisper.SyncMailRequest) error {
|
|
|
|
log.Info("Started syncing envelopes", "peer", peerIDString(peer), "req", request)
|
|
|
|
|
2019-02-27 14:30:08 +00:00
|
|
|
requestID := fmt.Sprintf("%d-%d", time.Now().UnixNano(), rand.Intn(1000))
|
|
|
|
|
2018-12-06 09:48:28 +00:00
|
|
|
syncRequestsMeter.Mark(1)
|
|
|
|
|
|
|
|
// Check rate limiting for a requesting peer.
|
|
|
|
if s.exceedsPeerRequests(peer.ID()) {
|
|
|
|
requestErrorsCounter.Inc(1)
|
|
|
|
log.Error("Peer exceeded request per seconds limit", "peerID", peerIDString(peer))
|
|
|
|
return fmt.Errorf("requests per seconds limit exceeded")
|
|
|
|
}
|
|
|
|
|
2018-12-14 11:21:34 +00:00
|
|
|
if err := request.Validate(); err != nil {
|
|
|
|
return fmt.Errorf("request is invalid: %v", err)
|
|
|
|
}
|
|
|
|
|
2019-05-13 09:25:46 +00:00
|
|
|
iter, err := s.createIterator(request.Lower, request.Upper, request.Cursor, nil, 0)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-12-06 09:48:28 +00:00
|
|
|
defer iter.Release()
|
|
|
|
|
2019-05-09 10:58:02 +00:00
|
|
|
bundles := make(chan []rlp.RawValue, 5)
|
2018-12-06 09:48:28 +00:00
|
|
|
errCh := make(chan error)
|
2019-02-22 08:55:37 +00:00
|
|
|
cancelProcessing := make(chan struct{})
|
2018-12-06 09:48:28 +00:00
|
|
|
|
|
|
|
go func() {
|
|
|
|
for bundle := range bundles {
|
2019-05-09 10:58:02 +00:00
|
|
|
resp := whisper.RawSyncResponse{Envelopes: bundle}
|
|
|
|
if err := s.w.SendRawSyncResponse(peer, resp); err != nil {
|
2019-02-22 08:55:37 +00:00
|
|
|
close(cancelProcessing)
|
2018-12-06 09:48:28 +00:00
|
|
|
errCh <- fmt.Errorf("failed to send sync response: %v", err)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
close(errCh)
|
|
|
|
}()
|
|
|
|
|
|
|
|
start := time.Now()
|
|
|
|
nextCursor, _ := s.processRequestInBundles(
|
|
|
|
iter,
|
|
|
|
request.Bloom,
|
|
|
|
int(request.Limit),
|
2019-02-22 08:55:37 +00:00
|
|
|
processRequestTimeout,
|
2019-02-27 14:30:08 +00:00
|
|
|
requestID,
|
2018-12-06 09:48:28 +00:00
|
|
|
bundles,
|
2019-02-22 08:55:37 +00:00
|
|
|
cancelProcessing,
|
2018-12-06 09:48:28 +00:00
|
|
|
)
|
|
|
|
requestProcessTimer.UpdateSince(start)
|
|
|
|
|
|
|
|
// Wait for the goroutine to finish the work. It may return an error.
|
|
|
|
if err := <-errCh; err != nil {
|
|
|
|
_ = s.w.SendSyncResponse(
|
|
|
|
peer,
|
|
|
|
whisper.SyncResponse{Error: "failed to send a response"},
|
|
|
|
)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Processing of the request could be finished earlier due to iterator error.
|
|
|
|
if err := iter.Error(); err != nil {
|
|
|
|
_ = s.w.SendSyncResponse(
|
|
|
|
peer,
|
|
|
|
whisper.SyncResponse{Error: "failed to process all envelopes"},
|
|
|
|
)
|
2019-05-13 09:25:46 +00:00
|
|
|
return fmt.Errorf("LevelDB iterator failed: %v", err)
|
2018-12-06 09:48:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
log.Info("Finished syncing envelopes", "peer", peerIDString(peer))
|
|
|
|
|
|
|
|
if err := s.w.SendSyncResponse(peer, whisper.SyncResponse{
|
|
|
|
Cursor: nextCursor,
|
|
|
|
Final: true,
|
|
|
|
}); err != nil {
|
|
|
|
return fmt.Errorf("failed to send the final sync response: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-05-22 15:51:05 +00:00
|
|
|
// exceedsPeerRequests in case limit its been setup on the current server and limit
|
2018-05-21 11:30:37 +00:00
|
|
|
// allows the query, it will store/update new query time for the current peer.
|
2018-05-22 15:51:05 +00:00
|
|
|
func (s *WMailServer) exceedsPeerRequests(peer []byte) bool {
|
2019-01-10 16:07:16 +00:00
|
|
|
s.muRateLimiter.RLock()
|
|
|
|
defer s.muRateLimiter.RUnlock()
|
|
|
|
|
|
|
|
if s.rateLimiter == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
peerID := string(peer)
|
|
|
|
if s.rateLimiter.IsAllowed(peerID) {
|
|
|
|
s.rateLimiter.Add(peerID)
|
|
|
|
return false
|
2018-05-17 11:21:04 +00:00
|
|
|
}
|
2019-01-10 16:07:16 +00:00
|
|
|
|
|
|
|
log.Info("peerID exceeded the number of requests per second")
|
|
|
|
return true
|
2018-05-11 19:43:07 +00:00
|
|
|
}
|
|
|
|
|
2019-05-13 09:25:46 +00:00
|
|
|
func (s *WMailServer) createIterator(lower, upper uint32, cursor []byte, bloom []byte, limit uint32) (Iterator, error) {
|
2018-10-19 09:09:13 +00:00
|
|
|
var (
|
2019-05-09 10:58:02 +00:00
|
|
|
emptyHash common.Hash
|
|
|
|
emptyTopic whisper.TopicType
|
|
|
|
ku, kl *DBKey
|
2018-10-19 09:09:13 +00:00
|
|
|
)
|
|
|
|
|
2019-05-09 10:58:02 +00:00
|
|
|
ku = NewDBKey(upper+1, emptyTopic, emptyHash)
|
|
|
|
kl = NewDBKey(lower, emptyTopic, emptyHash)
|
2018-10-19 09:09:13 +00:00
|
|
|
|
2019-05-10 10:26:57 +00:00
|
|
|
query := CursorQuery{
|
|
|
|
start: kl.Bytes(),
|
|
|
|
end: ku.Bytes(),
|
|
|
|
cursor: cursor,
|
|
|
|
bloom: bloom,
|
|
|
|
limit: limit,
|
2019-04-23 07:04:58 +00:00
|
|
|
}
|
2019-05-10 10:26:57 +00:00
|
|
|
return s.db.BuildIterator(query)
|
2018-10-19 09:09:13 +00:00
|
|
|
}
|
|
|
|
|
2018-12-06 09:48:28 +00:00
|
|
|
// processRequestInBundles processes envelopes using an iterator and passes them
|
|
|
|
// to the output channel in bundles.
|
|
|
|
func (s *WMailServer) processRequestInBundles(
|
2019-05-10 10:26:57 +00:00
|
|
|
iter Iterator,
|
2019-02-08 15:39:24 +00:00
|
|
|
bloom []byte,
|
|
|
|
limit int,
|
2019-02-22 08:55:37 +00:00
|
|
|
timeout time.Duration,
|
2019-02-27 14:30:08 +00:00
|
|
|
requestID string,
|
2019-05-09 10:58:02 +00:00
|
|
|
output chan<- []rlp.RawValue,
|
2019-02-22 08:55:37 +00:00
|
|
|
cancel <-chan struct{},
|
2018-12-11 10:23:47 +00:00
|
|
|
) ([]byte, common.Hash) {
|
2018-10-19 09:09:13 +00:00
|
|
|
var (
|
2019-05-09 10:58:02 +00:00
|
|
|
bundle []rlp.RawValue
|
2018-12-06 09:48:28 +00:00
|
|
|
bundleSize uint32
|
2019-05-09 10:58:02 +00:00
|
|
|
batches [][]rlp.RawValue
|
2018-12-06 09:48:28 +00:00
|
|
|
processedEnvelopes int
|
|
|
|
processedEnvelopesSize int64
|
2018-12-11 10:23:47 +00:00
|
|
|
nextCursor []byte
|
2018-12-06 09:48:28 +00:00
|
|
|
lastEnvelopeHash common.Hash
|
2018-10-19 09:09:13 +00:00
|
|
|
)
|
|
|
|
|
2019-02-22 08:55:37 +00:00
|
|
|
log.Info("[mailserver:processRequestInBundles] processing request",
|
2019-02-27 14:30:08 +00:00
|
|
|
"requestID", requestID,
|
|
|
|
"limit", limit)
|
2019-02-08 15:39:24 +00:00
|
|
|
|
|
|
|
// We iterate over the envelopes.
|
|
|
|
// We collect envelopes in batches.
|
|
|
|
// If there still room and we haven't reached the limit
|
|
|
|
// append and continue.
|
|
|
|
// Otherwise publish what you have so far, reset the bundle to the
|
|
|
|
// current envelope, and leave if we hit the limit
|
2019-04-23 07:04:58 +00:00
|
|
|
for iter.Next() {
|
2018-12-06 09:48:28 +00:00
|
|
|
|
2019-05-10 10:26:57 +00:00
|
|
|
rawValue, err := iter.GetEnvelope(bloom)
|
2019-05-09 10:58:02 +00:00
|
|
|
|
2019-05-10 10:26:57 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Error("Failed to get envelope from iterator",
|
|
|
|
"err", err,
|
|
|
|
"requestID", requestID)
|
|
|
|
continue
|
2018-10-19 09:09:13 +00:00
|
|
|
|
2019-05-09 10:58:02 +00:00
|
|
|
}
|
2019-05-10 10:26:57 +00:00
|
|
|
|
|
|
|
if rawValue == nil {
|
2018-10-19 09:09:13 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-05-13 09:25:46 +00:00
|
|
|
key, err := iter.DBKey()
|
|
|
|
if err != nil {
|
|
|
|
log.Error("[mailserver:processRequestInBundles] failed getting key",
|
|
|
|
"requestID", requestID)
|
|
|
|
break
|
|
|
|
|
|
|
|
}
|
2019-05-10 10:26:57 +00:00
|
|
|
|
2019-05-09 10:58:02 +00:00
|
|
|
lastEnvelopeHash = key.EnvelopeHash()
|
2019-02-08 15:39:24 +00:00
|
|
|
processedEnvelopes++
|
2019-05-09 10:58:02 +00:00
|
|
|
envelopeSize := uint32(len(rawValue))
|
2019-03-12 11:14:35 +00:00
|
|
|
limitReached := processedEnvelopes == limit
|
2019-02-08 15:39:24 +00:00
|
|
|
newSize := bundleSize + envelopeSize
|
|
|
|
|
|
|
|
// If we still have some room for messages, add and continue
|
2018-10-19 09:09:13 +00:00
|
|
|
if !limitReached && newSize < s.w.MaxMessageSize() {
|
2019-05-09 10:58:02 +00:00
|
|
|
bundle = append(bundle, rawValue)
|
2018-10-19 09:09:13 +00:00
|
|
|
bundleSize = newSize
|
2018-06-27 12:22:09 +00:00
|
|
|
continue
|
2018-05-11 19:43:07 +00:00
|
|
|
}
|
|
|
|
|
2019-02-08 15:39:24 +00:00
|
|
|
// Publish if anything is in the bundle (there should always be
|
|
|
|
// something unless limit = 1)
|
|
|
|
if len(bundle) != 0 {
|
|
|
|
batches = append(batches, bundle)
|
|
|
|
processedEnvelopesSize += int64(bundleSize)
|
|
|
|
}
|
2018-10-19 09:09:13 +00:00
|
|
|
|
2019-02-08 15:39:24 +00:00
|
|
|
// Reset the bundle with the current envelope
|
2019-05-09 10:58:02 +00:00
|
|
|
bundle = []rlp.RawValue{rawValue}
|
2019-02-08 15:39:24 +00:00
|
|
|
bundleSize = envelopeSize
|
2018-10-19 09:09:13 +00:00
|
|
|
|
2019-02-08 15:39:24 +00:00
|
|
|
// Leave if we reached the limit
|
2018-10-19 09:09:13 +00:00
|
|
|
if limitReached {
|
2019-05-09 10:58:02 +00:00
|
|
|
nextCursor = key.Cursor()
|
2018-10-19 09:09:13 +00:00
|
|
|
break
|
|
|
|
}
|
2019-02-08 15:39:24 +00:00
|
|
|
}
|
2018-07-02 07:38:10 +00:00
|
|
|
|
2019-02-08 15:39:24 +00:00
|
|
|
if len(bundle) > 0 {
|
|
|
|
batches = append(batches, bundle)
|
|
|
|
processedEnvelopesSize += int64(bundleSize)
|
2018-10-19 09:09:13 +00:00
|
|
|
}
|
|
|
|
|
2019-02-22 08:55:37 +00:00
|
|
|
log.Info("[mailserver:processRequestInBundles] publishing envelopes",
|
2019-02-27 14:30:08 +00:00
|
|
|
"requestID", requestID,
|
|
|
|
"batchesCount", len(batches),
|
|
|
|
"envelopeCount", processedEnvelopes,
|
|
|
|
"processedEnvelopesSize", processedEnvelopesSize,
|
|
|
|
"cursor", nextCursor)
|
2019-02-08 15:39:24 +00:00
|
|
|
|
|
|
|
// Publish
|
|
|
|
for _, batch := range batches {
|
2019-02-22 08:55:37 +00:00
|
|
|
select {
|
|
|
|
case output <- batch:
|
|
|
|
// It might happen that during producing the batches,
|
|
|
|
// the connection with the peer goes down and
|
|
|
|
// the consumer of `output` channel exits prematurely.
|
|
|
|
// In such a case, we should stop pushing batches and exit.
|
|
|
|
case <-cancel:
|
|
|
|
log.Info("[mailserver:processRequestInBundles] failed to push all batches",
|
|
|
|
"requestID", requestID)
|
|
|
|
break
|
|
|
|
case <-time.After(timeout):
|
|
|
|
log.Error("[mailserver:processRequestInBundles] timed out pushing a batch",
|
|
|
|
"requestID", requestID)
|
|
|
|
break
|
|
|
|
}
|
2018-05-11 19:43:07 +00:00
|
|
|
}
|
|
|
|
|
2018-12-06 09:48:28 +00:00
|
|
|
sentEnvelopesMeter.Mark(int64(processedEnvelopes))
|
|
|
|
sentEnvelopesSizeMeter.Mark(processedEnvelopesSize)
|
2018-05-11 19:43:07 +00:00
|
|
|
|
2019-02-22 08:55:37 +00:00
|
|
|
log.Info("[mailserver:processRequestInBundles] envelopes published",
|
|
|
|
"requestID", requestID)
|
2019-02-08 15:39:24 +00:00
|
|
|
close(output)
|
|
|
|
|
2018-12-06 09:48:28 +00:00
|
|
|
return nextCursor, lastEnvelopeHash
|
2018-05-11 19:43:07 +00:00
|
|
|
}
|
|
|
|
|
2019-05-09 10:58:02 +00:00
|
|
|
func (s *WMailServer) sendRawEnvelopes(peer *whisper.Peer, envelopes []rlp.RawValue, batch bool) error {
|
2018-11-19 09:14:03 +00:00
|
|
|
start := time.Now()
|
|
|
|
defer requestProcessNetTimer.UpdateSince(start)
|
|
|
|
|
2018-10-19 09:09:13 +00:00
|
|
|
if batch {
|
2019-05-09 10:58:02 +00:00
|
|
|
return s.w.SendRawP2PDirect(peer, envelopes...)
|
2018-10-19 09:09:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, env := range envelopes {
|
2019-05-09 10:58:02 +00:00
|
|
|
if err := s.w.SendRawP2PDirect(peer, env); err != nil {
|
2018-10-19 09:09:13 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-12-11 10:23:47 +00:00
|
|
|
func (s *WMailServer) sendHistoricMessageResponse(peer *whisper.Peer, request *whisper.Envelope, lastEnvelopeHash common.Hash, cursor []byte) error {
|
2018-10-18 10:25:00 +00:00
|
|
|
payload := whisper.CreateMailServerRequestCompletedPayload(request.Hash(), lastEnvelopeHash, cursor)
|
2018-07-02 07:38:10 +00:00
|
|
|
return s.w.SendHistoricMessageResponse(peer, payload)
|
2018-06-15 15:12:31 +00:00
|
|
|
}
|
|
|
|
|
2018-10-18 10:25:00 +00:00
|
|
|
// this method doesn't return an error because it is already in the error handling chain
|
|
|
|
func (s *WMailServer) trySendHistoricMessageErrorResponse(peer *whisper.Peer, request *whisper.Envelope, errorToReport error) {
|
|
|
|
payload := whisper.CreateMailServerRequestFailedPayload(request.Hash(), errorToReport)
|
|
|
|
|
|
|
|
err := s.w.SendHistoricMessageResponse(peer, payload)
|
|
|
|
// if we can't report an error, probably something is wrong with p2p connection,
|
|
|
|
// so we just print a log entry to document this sad fact
|
|
|
|
if err != nil {
|
2018-10-28 22:33:58 +00:00
|
|
|
log.Error("Error while reporting error response", "err", err, "peerID", peerIDString(peer))
|
2018-10-18 10:25:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-16 09:07:17 +00:00
|
|
|
// openEnvelope tries to decrypt an envelope, first based on asymetric key (if
|
|
|
|
// provided) and second on the symetric key (if provided)
|
|
|
|
func (s *WMailServer) openEnvelope(request *whisper.Envelope) *whisper.ReceivedMessage {
|
|
|
|
if s.asymFilter != nil {
|
|
|
|
if d := request.Open(s.asymFilter); d != nil {
|
|
|
|
return d
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if s.symFilter != nil {
|
|
|
|
if d := request.Open(s.symFilter); d != nil {
|
|
|
|
return d
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-12-11 10:23:47 +00:00
|
|
|
func (s *WMailServer) decodeRequest(peerID []byte, request *whisper.Envelope) (MessagesRequestPayload, error) {
|
|
|
|
var payload MessagesRequestPayload
|
2018-10-19 09:09:13 +00:00
|
|
|
|
|
|
|
if s.pow > 0.0 && request.PoW() < s.pow {
|
|
|
|
return payload, errors.New("PoW too low")
|
|
|
|
}
|
|
|
|
|
|
|
|
decrypted := s.openEnvelope(request)
|
|
|
|
if decrypted == nil {
|
|
|
|
log.Warn("Failed to decrypt p2p request")
|
|
|
|
return payload, errors.New("failed to decrypt p2p request")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := s.checkMsgSignature(decrypted, peerID); err != nil {
|
|
|
|
log.Warn("Check message signature failed: %s", "err", err.Error())
|
|
|
|
return payload, fmt.Errorf("check message signature failed: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := rlp.DecodeBytes(decrypted.Payload, &payload); err != nil {
|
|
|
|
return payload, fmt.Errorf("failed to decode data: %v", err)
|
|
|
|
}
|
|
|
|
|
2019-03-12 11:14:35 +00:00
|
|
|
if payload.Upper == 0 {
|
|
|
|
payload.Upper = uint32(time.Now().Unix() + whisperTTLSafeThreshold)
|
|
|
|
}
|
|
|
|
|
2018-10-19 09:09:13 +00:00
|
|
|
if payload.Upper < payload.Lower {
|
|
|
|
log.Error("Query range is invalid: lower > upper", "lower", payload.Lower, "upper", payload.Upper)
|
|
|
|
return payload, errors.New("query range is invalid: lower > upper")
|
|
|
|
}
|
|
|
|
|
|
|
|
return payload, nil
|
|
|
|
}
|
|
|
|
|
2018-05-21 11:30:37 +00:00
|
|
|
// validateRequest runs different validations on the current request.
|
2018-10-19 09:09:13 +00:00
|
|
|
// DEPRECATED
|
|
|
|
func (s *WMailServer) validateRequest(
|
|
|
|
peerID []byte,
|
|
|
|
request *whisper.Envelope,
|
2018-12-11 10:23:47 +00:00
|
|
|
) (uint32, uint32, []byte, uint32, []byte, error) {
|
2018-05-11 19:43:07 +00:00
|
|
|
if s.pow > 0.0 && request.PoW() < s.pow {
|
2018-10-18 10:25:00 +00:00
|
|
|
return 0, 0, nil, 0, nil, fmt.Errorf("PoW() is too low")
|
2018-05-11 19:43:07 +00:00
|
|
|
}
|
|
|
|
|
2018-07-16 09:07:17 +00:00
|
|
|
decrypted := s.openEnvelope(request)
|
2018-05-11 19:43:07 +00:00
|
|
|
if decrypted == nil {
|
2018-10-18 10:25:00 +00:00
|
|
|
return 0, 0, nil, 0, nil, fmt.Errorf("failed to decrypt p2p request")
|
2018-05-11 19:43:07 +00:00
|
|
|
}
|
|
|
|
|
2018-05-21 11:30:37 +00:00
|
|
|
if err := s.checkMsgSignature(decrypted, peerID); err != nil {
|
2018-10-18 10:25:00 +00:00
|
|
|
return 0, 0, nil, 0, nil, err
|
2018-05-11 19:43:07 +00:00
|
|
|
}
|
|
|
|
|
2018-05-21 11:30:37 +00:00
|
|
|
bloom, err := s.bloomFromReceivedMessage(decrypted)
|
|
|
|
if err != nil {
|
2018-10-18 10:25:00 +00:00
|
|
|
return 0, 0, nil, 0, nil, err
|
2018-05-11 19:43:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
lower := binary.BigEndian.Uint32(decrypted.Payload[:4])
|
|
|
|
upper := binary.BigEndian.Uint32(decrypted.Payload[4:8])
|
2018-05-17 11:21:04 +00:00
|
|
|
|
2018-06-26 07:33:05 +00:00
|
|
|
if upper < lower {
|
2018-10-18 10:25:00 +00:00
|
|
|
err := fmt.Errorf("query range is invalid: from > to (%d > %d)", lower, upper)
|
|
|
|
return 0, 0, nil, 0, nil, err
|
2018-06-26 07:33:05 +00:00
|
|
|
}
|
|
|
|
|
2018-05-17 11:21:04 +00:00
|
|
|
lowerTime := time.Unix(int64(lower), 0)
|
|
|
|
upperTime := time.Unix(int64(upper), 0)
|
|
|
|
if upperTime.Sub(lowerTime) > maxQueryRange {
|
2018-10-18 10:25:00 +00:00
|
|
|
err := fmt.Errorf("query range too big for peer %s", string(peerID))
|
|
|
|
return 0, 0, nil, 0, nil, err
|
2018-07-02 07:38:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var limit uint32
|
|
|
|
if len(decrypted.Payload) >= requestTimeRangeLength+whisper.BloomFilterSize+requestLimitLength {
|
|
|
|
limit = binary.BigEndian.Uint32(decrypted.Payload[requestTimeRangeLength+whisper.BloomFilterSize:])
|
|
|
|
}
|
|
|
|
|
2018-12-11 10:23:47 +00:00
|
|
|
var cursor []byte
|
|
|
|
if len(decrypted.Payload) == requestTimeRangeLength+whisper.BloomFilterSize+requestLimitLength+DBKeyLength {
|
2018-07-02 07:38:10 +00:00
|
|
|
cursor = decrypted.Payload[requestTimeRangeLength+whisper.BloomFilterSize+requestLimitLength:]
|
2018-05-17 11:21:04 +00:00
|
|
|
}
|
|
|
|
|
2018-10-18 10:25:00 +00:00
|
|
|
err = nil
|
|
|
|
return lower, upper, bloom, limit, cursor, err
|
2018-05-11 19:43:07 +00:00
|
|
|
}
|
2018-05-17 11:21:04 +00:00
|
|
|
|
2018-05-21 11:30:37 +00:00
|
|
|
// checkMsgSignature returns an error in case the message is not correcly signed
|
|
|
|
func (s *WMailServer) checkMsgSignature(msg *whisper.ReceivedMessage, id []byte) error {
|
|
|
|
src := crypto.FromECDSAPub(msg.Src)
|
|
|
|
if len(src)-len(id) == 1 {
|
|
|
|
src = src[1:]
|
2018-05-17 11:21:04 +00:00
|
|
|
}
|
|
|
|
|
2018-05-21 11:30:37 +00:00
|
|
|
// if you want to check the signature, you can do it here. e.g.:
|
|
|
|
// if !bytes.Equal(peerID, src) {
|
|
|
|
if src == nil {
|
|
|
|
return errors.New("Wrong signature of p2p request")
|
2018-05-17 11:21:04 +00:00
|
|
|
}
|
|
|
|
|
2018-05-21 11:30:37 +00:00
|
|
|
return nil
|
2018-05-17 11:21:04 +00:00
|
|
|
}
|
|
|
|
|
2018-12-14 11:21:34 +00:00
|
|
|
// bloomFromReceivedMessage for a given whisper.ReceivedMessage it extracts the
|
|
|
|
// used bloom filter.
|
2018-05-21 11:30:37 +00:00
|
|
|
func (s *WMailServer) bloomFromReceivedMessage(msg *whisper.ReceivedMessage) ([]byte, error) {
|
|
|
|
payloadSize := len(msg.Payload)
|
2018-05-17 11:21:04 +00:00
|
|
|
|
2018-05-21 11:30:37 +00:00
|
|
|
if payloadSize < 8 {
|
|
|
|
return nil, errors.New("Undersized p2p request")
|
|
|
|
} else if payloadSize == 8 {
|
|
|
|
return whisper.MakeFullNodeBloom(), nil
|
|
|
|
} else if payloadSize < 8+whisper.BloomFilterSize {
|
|
|
|
return nil, errors.New("Undersized bloom filter in p2p request")
|
2018-05-17 11:21:04 +00:00
|
|
|
}
|
2018-05-21 11:30:37 +00:00
|
|
|
|
|
|
|
return msg.Payload[8 : 8+whisper.BloomFilterSize], nil
|
2018-05-17 11:21:04 +00:00
|
|
|
}
|
2018-10-28 22:33:58 +00:00
|
|
|
|
|
|
|
// peerWithID is a generalization of whisper.Peer.
|
|
|
|
// whisper.Peer has all fields and methods, except for ID(), unexported.
|
|
|
|
// It makes it impossible to create an instance of it
|
|
|
|
// outside of whisper package and test properly.
|
|
|
|
type peerWithID interface {
|
|
|
|
ID() []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
func peerIDString(peer peerWithID) string {
|
|
|
|
return fmt.Sprintf("%x", peer.ID())
|
|
|
|
}
|
|
|
|
|
|
|
|
func peerIDBytesString(id []byte) string {
|
|
|
|
return fmt.Sprintf("%x", id)
|
|
|
|
}
|
2019-05-09 10:58:02 +00:00
|
|
|
|
|
|
|
func extractBloomFromEncodedEnvelope(rawValue rlp.RawValue) ([]byte, error) {
|
|
|
|
var envelope whisper.Envelope
|
|
|
|
decodeErr := rlp.DecodeBytes(rawValue, &envelope)
|
|
|
|
if decodeErr != nil {
|
|
|
|
return nil, decodeErr
|
|
|
|
}
|
|
|
|
return envelope.Bloom(), nil
|
|
|
|
}
|