2020-01-13 19:17:30 +00:00
|
|
|
package transport
|
|
|
|
|
|
|
|
import (
|
2021-04-22 13:36:18 +00:00
|
|
|
"bytes"
|
2020-01-13 19:17:30 +00:00
|
|
|
"context"
|
|
|
|
"crypto/ecdsa"
|
2021-04-22 13:36:18 +00:00
|
|
|
"database/sql"
|
|
|
|
"encoding/hex"
|
2021-07-21 19:02:50 +00:00
|
|
|
"fmt"
|
2021-04-22 13:36:18 +00:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/google/uuid"
|
2023-02-22 21:58:17 +00:00
|
|
|
"github.com/libp2p/go-libp2p/core/peer"
|
2021-04-22 13:36:18 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
"go.uber.org/zap"
|
2024-06-26 18:25:44 +00:00
|
|
|
"golang.org/x/exp/maps"
|
2020-01-13 19:17:30 +00:00
|
|
|
|
2021-11-26 12:30:35 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2022-12-09 16:16:21 +00:00
|
|
|
"github.com/status-im/status-go/connection"
|
2021-01-11 10:32:51 +00:00
|
|
|
"github.com/status-im/status-go/eth-node/crypto"
|
2020-01-13 19:17:30 +00:00
|
|
|
"github.com/status-im/status-go/eth-node/types"
|
|
|
|
)
|
|
|
|
|
2021-04-22 13:36:18 +00:00
|
|
|
var (
|
|
|
|
// ErrNoMailservers returned if there is no configured mailservers that can be used.
|
|
|
|
ErrNoMailservers = errors.New("no configured mailservers")
|
|
|
|
)
|
|
|
|
|
|
|
|
type transportKeysManager struct {
|
|
|
|
waku types.Waku
|
|
|
|
|
|
|
|
// Identity of the current user.
|
|
|
|
privateKey *ecdsa.PrivateKey
|
|
|
|
|
|
|
|
passToSymKeyMutex sync.RWMutex
|
|
|
|
passToSymKeyCache map[string]string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *transportKeysManager) AddOrGetKeyPair(priv *ecdsa.PrivateKey) (string, error) {
|
|
|
|
// caching is handled in waku
|
|
|
|
return m.waku.AddKeyPair(priv)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *transportKeysManager) AddOrGetSymKeyFromPassword(password string) (string, error) {
|
|
|
|
m.passToSymKeyMutex.Lock()
|
|
|
|
defer m.passToSymKeyMutex.Unlock()
|
|
|
|
|
|
|
|
if val, ok := m.passToSymKeyCache[password]; ok {
|
|
|
|
return val, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
id, err := m.waku.AddSymKeyFromPassword(password)
|
|
|
|
if err != nil {
|
|
|
|
return id, err
|
|
|
|
}
|
|
|
|
|
|
|
|
m.passToSymKeyCache[password] = id
|
|
|
|
|
|
|
|
return id, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *transportKeysManager) RawSymKey(id string) ([]byte, error) {
|
|
|
|
return m.waku.GetSymKey(id)
|
|
|
|
}
|
|
|
|
|
|
|
|
type Option func(*Transport) error
|
|
|
|
|
|
|
|
// Transport is a transport based on Whisper service.
|
|
|
|
type Transport struct {
|
|
|
|
waku types.Waku
|
|
|
|
api types.PublicWakuAPI // only PublicWakuAPI implements logic to send messages
|
|
|
|
keysManager *transportKeysManager
|
|
|
|
filters *FiltersManager
|
|
|
|
logger *zap.Logger
|
|
|
|
cache *ProcessedMessageIDsCache
|
|
|
|
|
|
|
|
mailservers []string
|
|
|
|
envelopesMonitor *EnvelopesMonitor
|
|
|
|
quit chan struct{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewTransport returns a new Transport.
|
|
|
|
// TODO: leaving a chat should verify that for a given public key
|
2022-12-09 16:16:21 +00:00
|
|
|
//
|
|
|
|
// there are no other chats. It may happen that we leave a private chat
|
|
|
|
// but still have a public chat for a given public key.
|
2021-04-22 13:36:18 +00:00
|
|
|
func NewTransport(
|
|
|
|
waku types.Waku,
|
|
|
|
privateKey *ecdsa.PrivateKey,
|
|
|
|
db *sql.DB,
|
2021-06-16 20:19:45 +00:00
|
|
|
sqlitePersistenceTableName string,
|
2021-04-22 13:36:18 +00:00
|
|
|
mailservers []string,
|
|
|
|
envelopesMonitorConfig *EnvelopesMonitorConfig,
|
|
|
|
logger *zap.Logger,
|
|
|
|
opts ...Option,
|
|
|
|
) (*Transport, error) {
|
2021-06-16 20:19:45 +00:00
|
|
|
filtersManager, err := NewFiltersManager(newSQLitePersistence(db, sqlitePersistenceTableName), waku, privateKey, logger)
|
2021-04-22 13:36:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var envelopesMonitor *EnvelopesMonitor
|
|
|
|
if envelopesMonitorConfig != nil {
|
|
|
|
envelopesMonitor = NewEnvelopesMonitor(waku, *envelopesMonitorConfig)
|
|
|
|
envelopesMonitor.Start()
|
|
|
|
}
|
|
|
|
|
|
|
|
var api types.PublicWhisperAPI
|
|
|
|
if waku != nil {
|
|
|
|
api = waku.PublicWakuAPI()
|
|
|
|
}
|
|
|
|
t := &Transport{
|
|
|
|
waku: waku,
|
|
|
|
api: api,
|
|
|
|
cache: NewProcessedMessageIDsCache(db),
|
|
|
|
envelopesMonitor: envelopesMonitor,
|
|
|
|
quit: make(chan struct{}),
|
|
|
|
keysManager: &transportKeysManager{
|
|
|
|
waku: waku,
|
|
|
|
privateKey: privateKey,
|
|
|
|
passToSymKeyCache: make(map[string]string),
|
|
|
|
},
|
|
|
|
filters: filtersManager,
|
|
|
|
mailservers: mailservers,
|
|
|
|
logger: logger.With(zap.Namespace("Transport")),
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, opt := range opts {
|
|
|
|
if err := opt(t); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
t.cleanFiltersLoop()
|
|
|
|
|
|
|
|
return t, nil
|
|
|
|
}
|
|
|
|
|
2023-05-22 21:38:02 +00:00
|
|
|
func (t *Transport) InitFilters(chatIDs []FiltersToInitialize, publicKeys []*ecdsa.PublicKey) ([]*Filter, error) {
|
2021-04-22 13:36:18 +00:00
|
|
|
return t.filters.Init(chatIDs, publicKeys)
|
|
|
|
}
|
|
|
|
|
2023-05-22 21:38:02 +00:00
|
|
|
func (t *Transport) InitPublicFilters(filtersToInit []FiltersToInitialize) ([]*Filter, error) {
|
|
|
|
return t.filters.InitPublicFilters(filtersToInit)
|
2021-04-22 13:36:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Transport) Filters() []*Filter {
|
|
|
|
return t.filters.Filters()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Transport) FilterByChatID(chatID string) *Filter {
|
|
|
|
return t.filters.FilterByChatID(chatID)
|
|
|
|
}
|
|
|
|
|
2022-04-22 07:42:22 +00:00
|
|
|
func (t *Transport) FilterByTopic(topic []byte) *Filter {
|
|
|
|
return t.filters.FilterByTopic(topic)
|
|
|
|
}
|
|
|
|
|
2021-03-25 15:15:22 +00:00
|
|
|
func (t *Transport) FiltersByIdentities(identities []string) []*Filter {
|
|
|
|
return t.filters.FiltersByIdentities(identities)
|
|
|
|
}
|
|
|
|
|
2021-04-22 13:36:18 +00:00
|
|
|
func (t *Transport) LoadFilters(filters []*Filter) ([]*Filter, error) {
|
|
|
|
return t.filters.InitWithFilters(filters)
|
|
|
|
}
|
|
|
|
|
2023-11-15 15:58:15 +00:00
|
|
|
func (t *Transport) InitCommunityFilters(communityFiltersToInitialize []CommunityFilterToInitialize, useShards bool) ([]*Filter, error) {
|
|
|
|
return t.filters.InitCommunityFilters(communityFiltersToInitialize, useShards)
|
2021-04-22 13:36:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Transport) RemoveFilters(filters []*Filter) error {
|
2023-07-14 13:42:02 +00:00
|
|
|
return t.filters.Remove(context.Background(), filters...)
|
2021-04-22 13:36:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Transport) RemoveFilterByChatID(chatID string) (*Filter, error) {
|
|
|
|
return t.filters.RemoveFilterByChatID(chatID)
|
|
|
|
}
|
|
|
|
|
2023-07-14 13:42:02 +00:00
|
|
|
func (t *Transport) ResetFilters(ctx context.Context) error {
|
|
|
|
return t.filters.Reset(ctx)
|
2021-04-22 13:36:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Transport) ProcessNegotiatedSecret(secret types.NegotiatedSecret) (*Filter, error) {
|
|
|
|
filter, err := t.filters.LoadNegotiated(secret)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return filter, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Transport) JoinPublic(chatID string) (*Filter, error) {
|
2023-11-15 15:58:15 +00:00
|
|
|
return t.filters.LoadPublic(chatID, "")
|
2021-04-22 13:36:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Transport) LeavePublic(chatID string) error {
|
|
|
|
chat := t.filters.Filter(chatID)
|
|
|
|
if chat != nil {
|
|
|
|
return nil
|
|
|
|
}
|
2023-07-14 13:42:02 +00:00
|
|
|
return t.filters.Remove(context.Background(), chat)
|
2021-04-22 13:36:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Transport) JoinPrivate(publicKey *ecdsa.PublicKey) (*Filter, error) {
|
|
|
|
return t.filters.LoadContactCode(publicKey)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Transport) JoinGroup(publicKeys []*ecdsa.PublicKey) ([]*Filter, error) {
|
|
|
|
var filters []*Filter
|
|
|
|
for _, pk := range publicKeys {
|
|
|
|
f, err := t.filters.LoadContactCode(pk)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
filters = append(filters, f)
|
|
|
|
|
|
|
|
}
|
|
|
|
return filters, nil
|
|
|
|
}
|
|
|
|
|
2021-08-03 19:27:15 +00:00
|
|
|
func (t *Transport) GetStats() types.StatsSummary {
|
|
|
|
return t.waku.GetStats()
|
|
|
|
}
|
|
|
|
|
2021-04-22 13:36:18 +00:00
|
|
|
func (t *Transport) RetrieveRawAll() (map[Filter][]*types.Message, error) {
|
|
|
|
result := make(map[Filter][]*types.Message)
|
2021-10-29 14:29:28 +00:00
|
|
|
logger := t.logger.With(zap.String("site", "retrieveRawAll"))
|
2021-04-22 13:36:18 +00:00
|
|
|
|
2021-11-26 12:30:35 +00:00
|
|
|
for _, filter := range t.filters.Filters() {
|
2021-04-22 13:36:18 +00:00
|
|
|
msgs, err := t.api.GetFilterMessages(filter.FilterID)
|
|
|
|
if err != nil {
|
2021-10-29 14:29:28 +00:00
|
|
|
logger.Warn("failed to fetch messages", zap.Error(err))
|
2021-04-22 13:36:18 +00:00
|
|
|
continue
|
|
|
|
}
|
2021-11-26 12:30:35 +00:00
|
|
|
// Don't pull from filters we don't listen to
|
|
|
|
if !filter.Listen {
|
|
|
|
for _, msg := range msgs {
|
|
|
|
t.waku.MarkP2PMessageAsProcessed(common.BytesToHash(msg.Hash))
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-04-22 13:36:18 +00:00
|
|
|
if len(msgs) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
ids := make([]string, len(msgs))
|
|
|
|
for i := range msgs {
|
|
|
|
id := types.EncodeHex(msgs[i].Hash)
|
|
|
|
ids[i] = id
|
|
|
|
}
|
|
|
|
|
|
|
|
hits, err := t.cache.Hits(ids)
|
|
|
|
if err != nil {
|
2021-10-29 14:29:28 +00:00
|
|
|
logger.Error("failed to check messages exists", zap.Error(err))
|
2021-04-22 13:36:18 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range msgs {
|
|
|
|
// Exclude anything that is a cache hit
|
|
|
|
if !hits[types.EncodeHex(msgs[i].Hash)] {
|
|
|
|
result[*filter] = append(result[*filter], msgs[i])
|
2021-10-29 14:29:28 +00:00
|
|
|
logger.Debug("message not cached", zap.String("hash", types.EncodeHex(msgs[i].Hash)))
|
|
|
|
} else {
|
|
|
|
logger.Debug("message cached", zap.String("hash", types.EncodeHex(msgs[i].Hash)))
|
2021-11-26 12:30:35 +00:00
|
|
|
t.waku.MarkP2PMessageAsProcessed(common.BytesToHash(msgs[i].Hash))
|
2021-04-22 13:36:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SendPublic sends a new message using the Whisper service.
|
|
|
|
// For public filters, chat name is used as an ID as well as
|
|
|
|
// a topic.
|
|
|
|
func (t *Transport) SendPublic(ctx context.Context, newMessage *types.NewMessage, chatName string) ([]byte, error) {
|
|
|
|
if err := t.addSig(newMessage); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-05-22 21:38:02 +00:00
|
|
|
filter, err := t.filters.LoadPublic(chatName, newMessage.PubsubTopic)
|
2021-04-22 13:36:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
newMessage.SymKeyID = filter.SymKeyID
|
2023-05-22 21:38:02 +00:00
|
|
|
newMessage.Topic = filter.ContentTopic
|
2023-11-15 15:58:15 +00:00
|
|
|
newMessage.PubsubTopic = filter.PubsubTopic
|
2021-04-22 13:36:18 +00:00
|
|
|
|
|
|
|
return t.api.Post(ctx, *newMessage)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Transport) SendPrivateWithSharedSecret(ctx context.Context, newMessage *types.NewMessage, publicKey *ecdsa.PublicKey, secret []byte) ([]byte, error) {
|
|
|
|
if err := t.addSig(newMessage); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
filter, err := t.filters.LoadNegotiated(types.NegotiatedSecret{
|
|
|
|
PublicKey: publicKey,
|
|
|
|
Key: secret,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
newMessage.SymKeyID = filter.SymKeyID
|
2023-05-22 21:38:02 +00:00
|
|
|
newMessage.Topic = filter.ContentTopic
|
2023-11-15 15:58:15 +00:00
|
|
|
newMessage.PubsubTopic = filter.PubsubTopic
|
2021-04-22 13:36:18 +00:00
|
|
|
newMessage.PublicKey = nil
|
|
|
|
|
|
|
|
return t.api.Post(ctx, *newMessage)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Transport) SendPrivateWithPartitioned(ctx context.Context, newMessage *types.NewMessage, publicKey *ecdsa.PublicKey) ([]byte, error) {
|
|
|
|
if err := t.addSig(newMessage); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
filter, err := t.filters.LoadPartitioned(publicKey, t.keysManager.privateKey, false)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-11-15 15:58:15 +00:00
|
|
|
newMessage.PubsubTopic = filter.PubsubTopic
|
2023-05-22 21:38:02 +00:00
|
|
|
newMessage.Topic = filter.ContentTopic
|
2021-04-22 13:36:18 +00:00
|
|
|
newMessage.PublicKey = crypto.FromECDSAPub(publicKey)
|
|
|
|
|
|
|
|
return t.api.Post(ctx, *newMessage)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Transport) SendPrivateOnPersonalTopic(ctx context.Context, newMessage *types.NewMessage, publicKey *ecdsa.PublicKey) ([]byte, error) {
|
|
|
|
if err := t.addSig(newMessage); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
filter, err := t.filters.LoadPersonal(publicKey, t.keysManager.privateKey, false)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-11-15 15:58:15 +00:00
|
|
|
newMessage.PubsubTopic = filter.PubsubTopic
|
2023-05-22 21:38:02 +00:00
|
|
|
newMessage.Topic = filter.ContentTopic
|
2021-04-22 13:36:18 +00:00
|
|
|
newMessage.PublicKey = crypto.FromECDSAPub(publicKey)
|
|
|
|
|
|
|
|
return t.api.Post(ctx, *newMessage)
|
|
|
|
}
|
|
|
|
|
2021-10-27 10:59:43 +00:00
|
|
|
func (t *Transport) PersonalTopicFilter() *Filter {
|
|
|
|
return t.filters.PersonalTopicFilter()
|
|
|
|
}
|
|
|
|
|
2021-04-22 13:36:18 +00:00
|
|
|
func (t *Transport) LoadKeyFilters(key *ecdsa.PrivateKey) (*Filter, error) {
|
|
|
|
return t.filters.LoadEphemeral(&key.PublicKey, key, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Transport) SendCommunityMessage(ctx context.Context, newMessage *types.NewMessage, publicKey *ecdsa.PublicKey) ([]byte, error) {
|
|
|
|
if err := t.addSig(newMessage); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// We load the filter to make sure we can post on it
|
2023-05-22 21:38:02 +00:00
|
|
|
filter, err := t.filters.LoadPublic(PubkeyToHex(publicKey)[2:], newMessage.PubsubTopic)
|
2021-04-22 13:36:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-11-15 15:58:15 +00:00
|
|
|
newMessage.PubsubTopic = filter.PubsubTopic
|
2023-05-22 21:38:02 +00:00
|
|
|
newMessage.Topic = filter.ContentTopic
|
2021-04-22 13:36:18 +00:00
|
|
|
newMessage.PublicKey = crypto.FromECDSAPub(publicKey)
|
|
|
|
|
2023-05-22 21:38:02 +00:00
|
|
|
t.logger.Debug("SENDING message", zap.Binary("topic", filter.ContentTopic[:]))
|
2021-04-22 13:36:18 +00:00
|
|
|
|
|
|
|
return t.api.Post(ctx, *newMessage)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Transport) cleanFilters() error {
|
|
|
|
return t.filters.RemoveNoListenFilters()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Transport) addSig(newMessage *types.NewMessage) error {
|
|
|
|
sigID, err := t.keysManager.AddOrGetKeyPair(t.keysManager.privateKey)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
newMessage.SigID = sigID
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-11-17 14:45:19 +00:00
|
|
|
func (t *Transport) Track(identifier []byte, hashes [][]byte, newMessages []*types.NewMessage) {
|
|
|
|
t.TrackMany([][]byte{identifier}, hashes, newMessages)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Transport) TrackMany(identifiers [][]byte, hashes [][]byte, newMessages []*types.NewMessage) {
|
|
|
|
if t.envelopesMonitor == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
envelopeHashes := make([]types.Hash, len(hashes))
|
|
|
|
for i, hash := range hashes {
|
|
|
|
envelopeHashes[i] = types.BytesToHash(hash)
|
|
|
|
}
|
|
|
|
|
|
|
|
err := t.envelopesMonitor.Add(identifiers, envelopeHashes, newMessages)
|
|
|
|
if err != nil {
|
|
|
|
t.logger.Error("failed to track messages", zap.Error(err))
|
2021-04-22 13:36:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetCurrentTime returns the current unix timestamp in milliseconds
|
|
|
|
func (t *Transport) GetCurrentTime() uint64 {
|
|
|
|
return uint64(t.waku.GetCurrentTime().UnixNano() / int64(time.Millisecond))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Transport) MaxMessageSize() uint32 {
|
|
|
|
return t.waku.MaxMessageSize()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Transport) Stop() error {
|
|
|
|
close(t.quit)
|
|
|
|
if t.envelopesMonitor != nil {
|
|
|
|
t.envelopesMonitor.Stop()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// cleanFiltersLoop cleans up the topic we create for the only purpose
|
|
|
|
// of sending messages.
|
|
|
|
// Whenever we send a message we also need to listen to that particular topic
|
|
|
|
// but in case of asymettric topics, we are not interested in listening to them.
|
|
|
|
// We therefore periodically clean them up so we don't receive unnecessary data.
|
|
|
|
|
|
|
|
func (t *Transport) cleanFiltersLoop() {
|
|
|
|
|
|
|
|
ticker := time.NewTicker(5 * time.Minute)
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-t.quit:
|
|
|
|
ticker.Stop()
|
|
|
|
return
|
|
|
|
case <-ticker.C:
|
|
|
|
err := t.cleanFilters()
|
|
|
|
if err != nil {
|
|
|
|
t.logger.Error("failed to clean up topics", zap.Error(err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2021-07-21 19:02:50 +00:00
|
|
|
func (t *Transport) WakuVersion() uint {
|
|
|
|
return t.waku.Version()
|
|
|
|
}
|
|
|
|
|
2021-08-30 14:57:28 +00:00
|
|
|
func (t *Transport) PeerCount() int {
|
|
|
|
return t.waku.PeerCount()
|
|
|
|
}
|
|
|
|
|
2022-11-24 15:00:44 +00:00
|
|
|
func (t *Transport) Peers() map[string]types.WakuV2Peer {
|
2021-08-30 14:57:28 +00:00
|
|
|
return t.waku.Peers()
|
|
|
|
}
|
|
|
|
|
2021-07-21 19:02:50 +00:00
|
|
|
func (t *Transport) createMessagesRequestV1(
|
2021-04-22 13:36:18 +00:00
|
|
|
ctx context.Context,
|
|
|
|
peerID []byte,
|
|
|
|
from, to uint32,
|
|
|
|
previousCursor []byte,
|
|
|
|
topics []types.TopicType,
|
|
|
|
waitForResponse bool,
|
|
|
|
) (cursor []byte, err error) {
|
2023-12-15 19:50:12 +00:00
|
|
|
r := createMessagesRequest(from, to, previousCursor, nil, "", topics, 1000)
|
2021-04-22 13:36:18 +00:00
|
|
|
|
|
|
|
events := make(chan types.EnvelopeEvent, 10)
|
|
|
|
sub := t.waku.SubscribeEnvelopeEvents(events)
|
|
|
|
defer sub.Unsubscribe()
|
|
|
|
|
|
|
|
err = t.waku.SendMessagesRequest(peerID, r)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !waitForResponse {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-07-21 19:02:50 +00:00
|
|
|
var resp *types.MailServerResponse
|
|
|
|
resp, err = t.waitForRequestCompleted(ctx, r.ID, events)
|
2021-04-22 13:36:18 +00:00
|
|
|
if err == nil && resp != nil && resp.Error != nil {
|
|
|
|
err = resp.Error
|
|
|
|
} else if err == nil && resp != nil {
|
|
|
|
cursor = resp.Cursor
|
|
|
|
}
|
2021-07-21 19:02:50 +00:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Transport) createMessagesRequestV2(
|
2023-01-10 20:59:10 +00:00
|
|
|
ctx context.Context,
|
2021-07-21 19:02:50 +00:00
|
|
|
peerID []byte,
|
|
|
|
from, to uint32,
|
|
|
|
previousStoreCursor *types.StoreRequestCursor,
|
2023-05-22 21:38:02 +00:00
|
|
|
pubsubTopic string,
|
|
|
|
contentTopics []types.TopicType,
|
2023-12-15 19:50:12 +00:00
|
|
|
limit uint32,
|
2023-01-10 20:59:10 +00:00
|
|
|
waitForResponse bool,
|
2023-12-15 19:50:12 +00:00
|
|
|
processEnvelopes bool,
|
|
|
|
) (storeCursor *types.StoreRequestCursor, envelopesCount int, err error) {
|
|
|
|
r := createMessagesRequest(from, to, nil, previousStoreCursor, pubsubTopic, contentTopics, limit)
|
2023-01-10 20:59:10 +00:00
|
|
|
|
|
|
|
if waitForResponse {
|
|
|
|
resultCh := make(chan struct {
|
2023-12-15 19:50:12 +00:00
|
|
|
storeCursor *types.StoreRequestCursor
|
|
|
|
envelopesCount int
|
|
|
|
err error
|
2023-01-10 20:59:10 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
go func() {
|
2023-12-15 19:50:12 +00:00
|
|
|
storeCursor, envelopesCount, err = t.waku.RequestStoreMessages(ctx, peerID, r, processEnvelopes)
|
2023-01-10 20:59:10 +00:00
|
|
|
resultCh <- struct {
|
2023-12-15 19:50:12 +00:00
|
|
|
storeCursor *types.StoreRequestCursor
|
|
|
|
envelopesCount int
|
|
|
|
err error
|
|
|
|
}{storeCursor, envelopesCount, err}
|
2023-01-10 20:59:10 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case result := <-resultCh:
|
2023-12-15 19:50:12 +00:00
|
|
|
return result.storeCursor, result.envelopesCount, result.err
|
2023-01-10 20:59:10 +00:00
|
|
|
case <-ctx.Done():
|
2023-12-15 19:50:12 +00:00
|
|
|
return nil, 0, ctx.Err()
|
2023-01-10 20:59:10 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
go func() {
|
2023-12-15 19:50:12 +00:00
|
|
|
_, _, err = t.waku.RequestStoreMessages(ctx, peerID, r, false)
|
2023-01-10 20:59:10 +00:00
|
|
|
if err != nil {
|
|
|
|
t.logger.Error("failed to request store messages", zap.Error(err))
|
|
|
|
}
|
|
|
|
}()
|
2021-07-21 19:02:50 +00:00
|
|
|
}
|
2023-01-10 20:59:10 +00:00
|
|
|
|
2021-07-21 19:02:50 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Transport) SendMessagesRequestForTopics(
|
|
|
|
ctx context.Context,
|
|
|
|
peerID []byte,
|
|
|
|
from, to uint32,
|
|
|
|
previousCursor []byte,
|
|
|
|
previousStoreCursor *types.StoreRequestCursor,
|
2023-05-22 21:38:02 +00:00
|
|
|
pubsubTopic string,
|
|
|
|
contentTopics []types.TopicType,
|
2023-12-15 19:50:12 +00:00
|
|
|
limit uint32,
|
2021-07-21 19:02:50 +00:00
|
|
|
waitForResponse bool,
|
2023-12-15 19:50:12 +00:00
|
|
|
processEnvelopes bool,
|
|
|
|
) (cursor []byte, storeCursor *types.StoreRequestCursor, envelopesCount int, err error) {
|
2021-07-21 19:02:50 +00:00
|
|
|
switch t.waku.Version() {
|
|
|
|
case 2:
|
2023-12-15 19:50:12 +00:00
|
|
|
storeCursor, envelopesCount, err = t.createMessagesRequestV2(ctx, peerID, from, to, previousStoreCursor, pubsubTopic, contentTopics, limit, waitForResponse, processEnvelopes)
|
2021-07-21 19:02:50 +00:00
|
|
|
case 1:
|
2023-05-22 21:38:02 +00:00
|
|
|
cursor, err = t.createMessagesRequestV1(ctx, peerID, from, to, previousCursor, contentTopics, waitForResponse)
|
2021-07-21 19:02:50 +00:00
|
|
|
default:
|
|
|
|
err = fmt.Errorf("unsupported version %d", t.waku.Version())
|
|
|
|
}
|
2021-04-22 13:36:18 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-12-15 19:50:12 +00:00
|
|
|
func createMessagesRequest(from, to uint32, cursor []byte, storeCursor *types.StoreRequestCursor, pubsubTopic string, topics []types.TopicType, limit uint32) types.MessagesRequest {
|
2021-04-22 13:36:18 +00:00
|
|
|
aUUID := uuid.New()
|
|
|
|
// uuid is 16 bytes, converted to hex it's 32 bytes as expected by types.MessagesRequest
|
|
|
|
id := []byte(hex.EncodeToString(aUUID[:]))
|
2021-05-13 13:30:33 +00:00
|
|
|
var topicBytes [][]byte
|
2021-05-14 10:55:42 +00:00
|
|
|
for idx := range topics {
|
|
|
|
topicBytes = append(topicBytes, topics[idx][:])
|
2021-05-13 13:30:33 +00:00
|
|
|
}
|
2021-04-22 13:36:18 +00:00
|
|
|
return types.MessagesRequest{
|
2023-05-22 21:38:02 +00:00
|
|
|
ID: id,
|
|
|
|
From: from,
|
|
|
|
To: to,
|
2023-12-15 19:50:12 +00:00
|
|
|
Limit: limit,
|
2023-05-22 21:38:02 +00:00
|
|
|
Cursor: cursor,
|
|
|
|
PubsubTopic: pubsubTopic,
|
|
|
|
ContentTopics: topicBytes,
|
|
|
|
StoreCursor: storeCursor,
|
2021-04-22 13:36:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Transport) waitForRequestCompleted(ctx context.Context, requestID []byte, events chan types.EnvelopeEvent) (*types.MailServerResponse, error) {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case ev := <-events:
|
|
|
|
if !bytes.Equal(ev.Hash.Bytes(), requestID) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if ev.Event != types.EventMailServerRequestCompleted {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
data, ok := ev.Data.(*types.MailServerResponse)
|
|
|
|
if ok {
|
|
|
|
return data, nil
|
|
|
|
}
|
|
|
|
case <-ctx.Done():
|
|
|
|
return nil, ctx.Err()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ConfirmMessagesProcessed marks the messages as processed in the cache so
|
|
|
|
// they won't be passed to the next layer anymore
|
|
|
|
func (t *Transport) ConfirmMessagesProcessed(ids []string, timestamp uint64) error {
|
2024-01-30 13:43:34 +00:00
|
|
|
t.logger.Debug("confirming message processed", zap.Any("ids", ids), zap.Any("timestamp", timestamp))
|
2021-04-22 13:36:18 +00:00
|
|
|
return t.cache.Add(ids, timestamp)
|
|
|
|
}
|
|
|
|
|
|
|
|
// CleanMessagesProcessed clears the messages that are older than timestamp
|
|
|
|
func (t *Transport) CleanMessagesProcessed(timestamp uint64) error {
|
|
|
|
return t.cache.Clean(timestamp)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Transport) SetEnvelopeEventsHandler(handler EnvelopeEventsHandler) error {
|
|
|
|
if t.envelopesMonitor == nil {
|
|
|
|
return errors.New("Current transport has no envelopes monitor")
|
|
|
|
}
|
|
|
|
t.envelopesMonitor.handler = handler
|
|
|
|
return nil
|
2020-01-13 19:17:30 +00:00
|
|
|
}
|
2021-01-11 10:32:51 +00:00
|
|
|
|
2021-03-25 15:15:22 +00:00
|
|
|
func (t *Transport) ClearProcessedMessageIDsCache() error {
|
2024-01-30 13:43:34 +00:00
|
|
|
t.logger.Debug("clearing processed messages cache")
|
|
|
|
t.waku.ClearEnvelopesCache()
|
2021-03-25 15:15:22 +00:00
|
|
|
return t.cache.Clear()
|
|
|
|
}
|
|
|
|
|
2021-08-26 20:25:43 +00:00
|
|
|
func (t *Transport) BloomFilter() []byte {
|
|
|
|
return t.api.BloomFilter()
|
|
|
|
}
|
|
|
|
|
2021-01-11 10:32:51 +00:00
|
|
|
func PubkeyToHex(key *ecdsa.PublicKey) string {
|
|
|
|
return types.EncodeHex(crypto.FromECDSAPub(key))
|
|
|
|
}
|
2021-08-30 14:57:28 +00:00
|
|
|
|
2021-11-22 13:40:14 +00:00
|
|
|
func (t *Transport) StartDiscV5() error {
|
|
|
|
return t.waku.StartDiscV5()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Transport) StopDiscV5() error {
|
|
|
|
return t.waku.StopDiscV5()
|
|
|
|
}
|
|
|
|
|
2022-11-24 21:27:46 +00:00
|
|
|
func (t *Transport) ListenAddresses() ([]string, error) {
|
|
|
|
return t.waku.ListenAddresses()
|
|
|
|
}
|
|
|
|
|
2024-07-01 15:37:54 +00:00
|
|
|
func (t *Transport) RelayPeersByTopic(topic string) (*types.PeerList, error) {
|
|
|
|
return t.waku.RelayPeersByTopic(topic)
|
|
|
|
}
|
|
|
|
|
2024-06-18 19:48:49 +00:00
|
|
|
func (t *Transport) ENR() (string, error) {
|
|
|
|
return t.waku.ENR()
|
|
|
|
}
|
|
|
|
|
2023-02-22 21:58:17 +00:00
|
|
|
func (t *Transport) AddStorePeer(address string) (peer.ID, error) {
|
2021-08-30 14:57:28 +00:00
|
|
|
return t.waku.AddStorePeer(address)
|
|
|
|
}
|
|
|
|
|
2023-02-22 21:58:17 +00:00
|
|
|
func (t *Transport) AddRelayPeer(address string) (peer.ID, error) {
|
2021-08-30 14:57:28 +00:00
|
|
|
return t.waku.AddRelayPeer(address)
|
|
|
|
}
|
|
|
|
|
2021-09-10 17:06:06 +00:00
|
|
|
func (t *Transport) DialPeer(address string) error {
|
|
|
|
return t.waku.DialPeer(address)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Transport) DialPeerByID(peerID string) error {
|
|
|
|
return t.waku.DialPeerByID(peerID)
|
|
|
|
}
|
|
|
|
|
2021-08-30 14:57:28 +00:00
|
|
|
func (t *Transport) DropPeer(peerID string) error {
|
|
|
|
return t.waku.DropPeer(peerID)
|
|
|
|
}
|
2021-11-26 12:30:35 +00:00
|
|
|
|
|
|
|
func (t *Transport) ProcessingP2PMessages() bool {
|
|
|
|
return t.waku.ProcessingP2PMessages()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Transport) MarkP2PMessageAsProcessed(hash common.Hash) {
|
|
|
|
t.waku.MarkP2PMessageAsProcessed(hash)
|
|
|
|
}
|
2022-01-12 16:02:01 +00:00
|
|
|
|
|
|
|
func (t *Transport) SubscribeToConnStatusChanges() (*types.ConnStatusSubscription, error) {
|
|
|
|
return t.waku.SubscribeToConnStatusChanges()
|
|
|
|
}
|
2022-12-09 16:16:21 +00:00
|
|
|
|
|
|
|
func (t *Transport) ConnectionChanged(state connection.State) {
|
|
|
|
t.waku.ConnectionChanged(state)
|
|
|
|
}
|
2023-05-22 21:38:02 +00:00
|
|
|
|
|
|
|
// Subscribe to a pubsub topic, passing an optional public key if the pubsub topic is protected
|
|
|
|
func (t *Transport) SubscribeToPubsubTopic(topic string, optPublicKey *ecdsa.PublicKey) error {
|
|
|
|
if t.waku.Version() == 2 {
|
|
|
|
return t.waku.SubscribeToPubsubTopic(topic, optPublicKey)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-01-30 17:56:59 +00:00
|
|
|
// Unsubscribe from a pubsub topic
|
|
|
|
func (t *Transport) UnsubscribeFromPubsubTopic(topic string) error {
|
|
|
|
if t.waku.Version() == 2 {
|
|
|
|
return t.waku.UnsubscribeFromPubsubTopic(topic)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-05-22 21:38:02 +00:00
|
|
|
func (t *Transport) StorePubsubTopicKey(topic string, privKey *ecdsa.PrivateKey) error {
|
|
|
|
return t.waku.StorePubsubTopicKey(topic, privKey)
|
|
|
|
}
|
|
|
|
|
2023-10-12 19:21:49 +00:00
|
|
|
func (t *Transport) RetrievePubsubTopicKey(topic string) (*ecdsa.PrivateKey, error) {
|
|
|
|
return t.waku.RetrievePubsubTopicKey(topic)
|
|
|
|
}
|
2024-01-30 17:56:59 +00:00
|
|
|
|
|
|
|
func (t *Transport) RemovePubsubTopicKey(topic string) error {
|
|
|
|
if t.waku.Version() == 2 {
|
|
|
|
return t.waku.RemovePubsubTopicKey(topic)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2024-06-11 07:45:01 +00:00
|
|
|
|
|
|
|
func (t *Transport) ConfirmMessageDelivered(messageID string) {
|
|
|
|
if t.envelopesMonitor == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
hashes, ok := t.envelopesMonitor.messageEnvelopeHashes[messageID]
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
commHashes := make([]common.Hash, len(hashes))
|
|
|
|
for i, h := range hashes {
|
|
|
|
commHashes[i] = common.BytesToHash(h[:])
|
|
|
|
}
|
|
|
|
t.waku.ConfirmMessageDelivered(commHashes)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Transport) SetStorePeerID(peerID peer.ID) {
|
|
|
|
t.waku.SetStorePeerID(peerID)
|
|
|
|
}
|
2024-06-26 18:25:44 +00:00
|
|
|
|
|
|
|
func (t *Transport) SetCriteriaForMissingMessageVerification(peerID peer.ID, filters []*Filter) {
|
|
|
|
if t.waku.Version() != 2 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
topicMap := make(map[string]map[string]struct{})
|
|
|
|
for _, f := range filters {
|
|
|
|
if !f.Listen || f.Ephemeral {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
_, ok := topicMap[f.PubsubTopic]
|
|
|
|
if !ok {
|
|
|
|
topicMap[f.PubsubTopic] = make(map[string]struct{})
|
|
|
|
}
|
|
|
|
|
|
|
|
topicMap[f.PubsubTopic][f.ContentTopic.String()] = struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
for pubsubTopic, contentTopics := range topicMap {
|
|
|
|
ctList := maps.Keys(contentTopics)
|
|
|
|
err := t.waku.SetCriteriaForMissingMessageVerification(peerID, pubsubTopic, ctList)
|
|
|
|
if err != nil {
|
|
|
|
t.logger.Error("could not check for missing messages",
|
|
|
|
zap.Error(err),
|
|
|
|
zap.Stringer("peerID", peerID),
|
|
|
|
zap.String("pubsubTopic", pubsubTopic),
|
|
|
|
zap.Strings("contentTopics", ctList))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|