+3
@@ -0,0 +1,3 @@
|
||||
# Waku Common
|
||||
|
||||
[See here](../README.md#common)
|
||||
+52
@@ -0,0 +1,52 @@
|
||||
// Copyright 2019 The Waku Library Authors.
|
||||
//
|
||||
// The Waku 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 Waku library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty off
|
||||
// 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 Waku library. If not, see <http://www.gnu.org/licenses/>.
|
||||
//
|
||||
// This software uses the go-ethereum library, which is licensed
|
||||
// under the GNU Lesser General Public Library, version 3 or any later.
|
||||
|
||||
package common
|
||||
|
||||
import (
|
||||
"time"
|
||||
)
|
||||
|
||||
// Waku protocol parameters
|
||||
const (
|
||||
SizeMask = byte(3) // mask used to extract the size of payload size field from the flags
|
||||
|
||||
TopicLength = 4 // in bytes
|
||||
AESKeyLength = 32 // in bytes
|
||||
KeyIDSize = 32 // in bytes
|
||||
BloomFilterSize = 64 // in bytes
|
||||
MaxTopicInterest = 10000
|
||||
|
||||
EnvelopeHeaderLength = 20
|
||||
|
||||
MaxMessageSize = uint32(10 * 1024 * 1024) // maximum accepted size of a message.
|
||||
DefaultMaxMessageSize = uint32(1 << 20) // DefaultMaximumMessageSize is 1mb.
|
||||
|
||||
ExpirationCycle = time.Second
|
||||
TransmissionCycle = 300 * time.Millisecond
|
||||
|
||||
DefaultTTL = 50 // seconds
|
||||
DefaultSyncAllowance = 10 // seconds
|
||||
|
||||
MaxLimitInSyncMailRequest = 1000
|
||||
|
||||
EnvelopeTimeNotSynced uint = iota + 1
|
||||
EnvelopeOtherError
|
||||
|
||||
MaxLimitInMessagesRequest = 1000
|
||||
)
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
package common
|
||||
|
||||
// TimeSyncError error for clock skew errors.
|
||||
type TimeSyncError error
|
||||
+73
@@ -0,0 +1,73 @@
|
||||
// Copyright 2019 The Waku Library Authors.
|
||||
//
|
||||
// The Waku 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 Waku library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty off
|
||||
// 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 Waku library. If not, see <http://www.gnu.org/licenses/>.
|
||||
//
|
||||
// This software uses the go-ethereum library, which is licensed
|
||||
// under the GNU Lesser General Public Library, version 3 or any later.
|
||||
|
||||
package common
|
||||
|
||||
import (
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
"github.com/ethereum/go-ethereum/p2p/enode"
|
||||
)
|
||||
|
||||
// EventType used to define known waku events.
|
||||
type EventType string
|
||||
|
||||
const (
|
||||
// EventEnvelopeSent fires when envelope was sent to a peer.
|
||||
EventEnvelopeSent EventType = "envelope.sent"
|
||||
|
||||
// EventEnvelopeExpired fires when envelop expired
|
||||
EventEnvelopeExpired EventType = "envelope.expired"
|
||||
|
||||
// EventEnvelopeReceived is sent once envelope was received from a peer.
|
||||
// EventEnvelopeReceived must be sent to the feed even if envelope was previously in the cache.
|
||||
// And event, ideally, should contain information about peer that sent envelope to us.
|
||||
EventEnvelopeReceived EventType = "envelope.received"
|
||||
|
||||
// EventBatchAcknowledged is sent when batch of envelopes was acknowledged by a peer.
|
||||
EventBatchAcknowledged EventType = "batch.acknowledged"
|
||||
|
||||
// EventEnvelopeAvailable fires when envelop is available for filters
|
||||
EventEnvelopeAvailable EventType = "envelope.available"
|
||||
|
||||
// EventMailServerRequestSent fires when such request is sent.
|
||||
EventMailServerRequestSent EventType = "mailserver.request.sent"
|
||||
|
||||
// EventMailServerRequestCompleted fires after mailserver sends all the requested messages
|
||||
EventMailServerRequestCompleted EventType = "mailserver.request.completed"
|
||||
|
||||
// EventMailServerRequestExpired fires after mailserver the request TTL ends.
|
||||
// This event is independent and concurrent to EventMailServerRequestCompleted.
|
||||
// Request should be considered as expired only if expiry event was received first.
|
||||
EventMailServerRequestExpired EventType = "mailserver.request.expired"
|
||||
|
||||
// EventMailServerEnvelopeArchived fires after an envelope has been archived
|
||||
EventMailServerEnvelopeArchived EventType = "mailserver.envelope.archived"
|
||||
|
||||
// EventMailServerSyncFinished fires when the sync of messages is finished.
|
||||
EventMailServerSyncFinished EventType = "mailserver.sync.finished"
|
||||
)
|
||||
|
||||
// EnvelopeEvent represents an envelope event.
|
||||
type EnvelopeEvent struct {
|
||||
Event EventType
|
||||
Topic TopicType
|
||||
Hash common.Hash
|
||||
Batch common.Hash
|
||||
Peer enode.ID
|
||||
Data interface{}
|
||||
}
|
||||
+297
@@ -0,0 +1,297 @@
|
||||
// Copyright 2019 The Waku Library Authors.
|
||||
//
|
||||
// The Waku 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 Waku library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty off
|
||||
// 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 Waku library. If not, see <http://www.gnu.org/licenses/>.
|
||||
//
|
||||
// This software uses the go-ethereum library, which is licensed
|
||||
// under the GNU Lesser General Public Library, version 3 or any later.
|
||||
|
||||
package common
|
||||
|
||||
import (
|
||||
"crypto/ecdsa"
|
||||
"fmt"
|
||||
"sync"
|
||||
|
||||
"go.uber.org/zap"
|
||||
"golang.org/x/exp/maps"
|
||||
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
"github.com/ethereum/go-ethereum/crypto"
|
||||
"github.com/ethereum/go-ethereum/log"
|
||||
)
|
||||
|
||||
// Filter represents a Waku message filter
|
||||
type Filter struct {
|
||||
Src *ecdsa.PublicKey // Sender of the message
|
||||
KeyAsym *ecdsa.PrivateKey // Private Key of recipient
|
||||
KeySym []byte // Key associated with the Topic
|
||||
PubsubTopic string // Pubsub topic used to filter messages with
|
||||
ContentTopics TopicSet // ContentTopics to filter messages with
|
||||
SymKeyHash common.Hash // The Keccak256Hash of the symmetric key, needed for optimization
|
||||
id string // unique identifier
|
||||
|
||||
Messages MessageStore
|
||||
}
|
||||
|
||||
type FilterSet = map[*Filter]struct{}
|
||||
type ContentTopicToFilter = map[TopicType]FilterSet
|
||||
type PubsubTopicToContentTopic = map[string]ContentTopicToFilter
|
||||
|
||||
// Filters represents a collection of filters
|
||||
type Filters struct {
|
||||
// Map of random ID to Filter
|
||||
watchers map[string]*Filter
|
||||
|
||||
// Pubsub topic to use when no pubsub topic is specified on a filter
|
||||
defaultPubsubTopic string
|
||||
|
||||
// map a topic to the filters that are interested in being notified when a message matches that topic
|
||||
topicMatcher PubsubTopicToContentTopic
|
||||
|
||||
// list all the filters that will be notified of a new message, no matter what its topic is
|
||||
allTopicsMatcher map[*Filter]struct{}
|
||||
|
||||
logger *zap.Logger
|
||||
|
||||
sync.RWMutex
|
||||
}
|
||||
|
||||
// NewFilters returns a newly created filter collection
|
||||
func NewFilters(defaultPubsubTopic string, logger *zap.Logger) *Filters {
|
||||
return &Filters{
|
||||
watchers: make(map[string]*Filter),
|
||||
topicMatcher: make(PubsubTopicToContentTopic),
|
||||
allTopicsMatcher: make(map[*Filter]struct{}),
|
||||
defaultPubsubTopic: defaultPubsubTopic,
|
||||
logger: logger,
|
||||
}
|
||||
}
|
||||
|
||||
// Install will add a new filter to the filter collection
|
||||
func (fs *Filters) Install(watcher *Filter) (string, error) {
|
||||
if watcher.KeySym != nil && watcher.KeyAsym != nil {
|
||||
return "", fmt.Errorf("filters must choose between symmetric and asymmetric keys")
|
||||
}
|
||||
|
||||
id, err := GenerateRandomID()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
fs.Lock()
|
||||
defer fs.Unlock()
|
||||
|
||||
if fs.watchers[id] != nil {
|
||||
return "", fmt.Errorf("failed to generate unique ID")
|
||||
}
|
||||
|
||||
if watcher.expectsSymmetricEncryption() {
|
||||
watcher.SymKeyHash = crypto.Keccak256Hash(watcher.KeySym)
|
||||
}
|
||||
|
||||
watcher.id = id
|
||||
|
||||
fs.watchers[id] = watcher
|
||||
fs.addTopicMatcher(watcher)
|
||||
|
||||
fs.logger.Debug("filters install", zap.String("id", id))
|
||||
return id, err
|
||||
}
|
||||
|
||||
// Uninstall will remove a filter whose id has been specified from
|
||||
// the filter collection
|
||||
func (fs *Filters) Uninstall(id string) bool {
|
||||
fs.Lock()
|
||||
defer fs.Unlock()
|
||||
watcher := fs.watchers[id]
|
||||
if watcher != nil {
|
||||
fs.removeFromTopicMatchers(watcher)
|
||||
delete(fs.watchers, id)
|
||||
|
||||
fs.logger.Debug("filters uninstall", zap.String("id", id))
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (fs *Filters) AllTopics() []TopicType {
|
||||
var topics []TopicType
|
||||
fs.Lock()
|
||||
defer fs.Unlock()
|
||||
for _, topicsPerPubsubTopic := range fs.topicMatcher {
|
||||
for t := range topicsPerPubsubTopic {
|
||||
topics = append(topics, t)
|
||||
}
|
||||
}
|
||||
|
||||
return topics
|
||||
}
|
||||
|
||||
// addTopicMatcher adds a filter to the topic matchers.
|
||||
// If the filter's Topics array is empty, it will be tried on every topic.
|
||||
// Otherwise, it will be tried on the topics specified.
|
||||
func (fs *Filters) addTopicMatcher(watcher *Filter) {
|
||||
if len(watcher.ContentTopics) == 0 && (watcher.PubsubTopic == fs.defaultPubsubTopic || watcher.PubsubTopic == "") {
|
||||
fs.allTopicsMatcher[watcher] = struct{}{}
|
||||
} else {
|
||||
filtersPerContentTopic, ok := fs.topicMatcher[watcher.PubsubTopic]
|
||||
if !ok {
|
||||
filtersPerContentTopic = make(ContentTopicToFilter)
|
||||
}
|
||||
|
||||
for topic := range watcher.ContentTopics {
|
||||
if filtersPerContentTopic[topic] == nil {
|
||||
filtersPerContentTopic[topic] = make(FilterSet)
|
||||
}
|
||||
filtersPerContentTopic[topic][watcher] = struct{}{}
|
||||
}
|
||||
|
||||
fs.topicMatcher[watcher.PubsubTopic] = filtersPerContentTopic
|
||||
}
|
||||
}
|
||||
|
||||
// removeFromTopicMatchers removes a filter from the topic matchers
|
||||
func (fs *Filters) removeFromTopicMatchers(watcher *Filter) {
|
||||
delete(fs.allTopicsMatcher, watcher)
|
||||
|
||||
filtersPerContentTopic, ok := fs.topicMatcher[watcher.PubsubTopic]
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
|
||||
for topic := range watcher.ContentTopics {
|
||||
delete(filtersPerContentTopic[topic], watcher)
|
||||
}
|
||||
|
||||
fs.topicMatcher[watcher.PubsubTopic] = filtersPerContentTopic
|
||||
}
|
||||
|
||||
// GetWatchersByTopic returns a slice containing the filters that
|
||||
// match a specific topic
|
||||
func (fs *Filters) GetWatchersByTopic(pubsubTopic string, contentTopic TopicType) []*Filter {
|
||||
res := make([]*Filter, 0, len(fs.allTopicsMatcher))
|
||||
for watcher := range fs.allTopicsMatcher {
|
||||
res = append(res, watcher)
|
||||
}
|
||||
|
||||
filtersPerContentTopic, ok := fs.topicMatcher[pubsubTopic]
|
||||
if !ok {
|
||||
return res
|
||||
}
|
||||
|
||||
for watcher := range filtersPerContentTopic[contentTopic] {
|
||||
res = append(res, watcher)
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
// Get returns a filter from the collection with a specific ID
|
||||
func (fs *Filters) Get(id string) *Filter {
|
||||
fs.RLock()
|
||||
defer fs.RUnlock()
|
||||
return fs.watchers[id]
|
||||
}
|
||||
|
||||
func (fs *Filters) All() []*Filter {
|
||||
fs.RLock()
|
||||
defer fs.RUnlock()
|
||||
var filters []*Filter
|
||||
for _, f := range fs.watchers {
|
||||
filters = append(filters, f)
|
||||
}
|
||||
return filters
|
||||
}
|
||||
|
||||
func (fs *Filters) GetFilters() map[string]*Filter {
|
||||
fs.RLock()
|
||||
defer fs.RUnlock()
|
||||
return maps.Clone(fs.watchers)
|
||||
}
|
||||
|
||||
// NotifyWatchers notifies any filter that has declared interest
|
||||
// for the envelope's topic.
|
||||
func (fs *Filters) NotifyWatchers(recvMessage *ReceivedMessage) bool {
|
||||
var decodedMsg *ReceivedMessage
|
||||
|
||||
fs.RLock()
|
||||
defer fs.RUnlock()
|
||||
|
||||
var matched bool
|
||||
candidates := fs.GetWatchersByTopic(recvMessage.PubsubTopic, recvMessage.ContentTopic)
|
||||
|
||||
if len(candidates) == 0 {
|
||||
log.Debug("no filters available for this topic", "message", recvMessage.Hash().Hex(), "pubsubTopic", recvMessage.PubsubTopic, "contentTopic", recvMessage.ContentTopic.String())
|
||||
}
|
||||
|
||||
for _, watcher := range candidates {
|
||||
matched = true
|
||||
if decodedMsg == nil {
|
||||
decodedMsg = recvMessage.Open(watcher)
|
||||
if decodedMsg == nil {
|
||||
log.Debug("processing message: failed to open", "message", recvMessage.Hash().Hex(), "filter", watcher.id)
|
||||
}
|
||||
} else {
|
||||
matched = watcher.MatchMessage(decodedMsg)
|
||||
}
|
||||
|
||||
if matched && decodedMsg != nil {
|
||||
log.Debug("processing message: decrypted", "envelopeHash", recvMessage.Hash().Hex())
|
||||
if watcher.Src == nil || IsPubKeyEqual(decodedMsg.Src, watcher.Src) {
|
||||
watcher.Trigger(decodedMsg)
|
||||
}
|
||||
}
|
||||
}
|
||||
return matched
|
||||
}
|
||||
|
||||
func (f *Filter) expectsAsymmetricEncryption() bool {
|
||||
return f.KeyAsym != nil
|
||||
}
|
||||
|
||||
func (f *Filter) expectsSymmetricEncryption() bool {
|
||||
return f.KeySym != nil
|
||||
}
|
||||
|
||||
// Trigger adds a yet-unknown message to the filter's list of
|
||||
// received messages.
|
||||
func (f *Filter) Trigger(msg *ReceivedMessage) {
|
||||
err := f.Messages.Add(msg)
|
||||
if err != nil {
|
||||
log.Error("failed to add msg into the filters store", "hash", msg.Hash(), "error", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Retrieve will return the list of all received messages associated
|
||||
// to a filter.
|
||||
func (f *Filter) Retrieve() []*ReceivedMessage {
|
||||
msgs, err := f.Messages.Pop()
|
||||
if err != nil {
|
||||
log.Error("failed to retrieve messages from filter store", "error", err)
|
||||
return nil
|
||||
}
|
||||
return msgs
|
||||
}
|
||||
|
||||
// MatchMessage checks if the filter matches an already decrypted
|
||||
// message (i.e. a Message that has already been handled by
|
||||
// MatchEnvelope when checked by a previous filter).
|
||||
// Topics are not checked here, since this is done by topic matchers.
|
||||
func (f *Filter) MatchMessage(msg *ReceivedMessage) bool {
|
||||
if f.expectsAsymmetricEncryption() && msg.isAsymmetricEncryption() {
|
||||
return IsPubKeyEqual(&f.KeyAsym.PublicKey, msg.Dst)
|
||||
} else if f.expectsSymmetricEncryption() && msg.isSymmetricEncryption() {
|
||||
return f.SymKeyHash == msg.SymKeyHash
|
||||
}
|
||||
return false
|
||||
}
|
||||
+112
@@ -0,0 +1,112 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"crypto/ecdsa"
|
||||
crand "crypto/rand"
|
||||
"errors"
|
||||
"fmt"
|
||||
mrand "math/rand"
|
||||
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
)
|
||||
|
||||
// IsPubKeyEqual checks that two public keys are equal
|
||||
func IsPubKeyEqual(a, b *ecdsa.PublicKey) bool {
|
||||
if !ValidatePublicKey(a) {
|
||||
return false
|
||||
} else if !ValidatePublicKey(b) {
|
||||
return false
|
||||
}
|
||||
// the curve is always the same, just compare the points
|
||||
return a.X.Cmp(b.X) == 0 && a.Y.Cmp(b.Y) == 0
|
||||
}
|
||||
|
||||
// ValidatePublicKey checks the format of the given public key.
|
||||
func ValidatePublicKey(k *ecdsa.PublicKey) bool {
|
||||
return k != nil && k.X != nil && k.Y != nil && k.X.Sign() != 0 && k.Y.Sign() != 0
|
||||
}
|
||||
|
||||
// BytesToUintLittleEndian converts the slice to 64-bit unsigned integer.
|
||||
func BytesToUintLittleEndian(b []byte) (res uint64) {
|
||||
mul := uint64(1)
|
||||
for i := 0; i < len(b); i++ {
|
||||
res += uint64(b[i]) * mul
|
||||
mul *= 256
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
// BytesToUintBigEndian converts the slice to 64-bit unsigned integer.
|
||||
func BytesToUintBigEndian(b []byte) (res uint64) {
|
||||
for i := 0; i < len(b); i++ {
|
||||
res *= 256
|
||||
res += uint64(b[i])
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
// ContainsOnlyZeros checks if the data contain only zeros.
|
||||
func ContainsOnlyZeros(data []byte) bool {
|
||||
for _, b := range data {
|
||||
if b != 0 {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// GenerateSecureRandomData generates random data where extra security is required.
|
||||
// The purpose of this function is to prevent some bugs in software or in hardware
|
||||
// from delivering not-very-random data. This is especially useful for AES nonce,
|
||||
// where true randomness does not really matter, but it is very important to have
|
||||
// a unique nonce for every message.
|
||||
func GenerateSecureRandomData(length int) ([]byte, error) {
|
||||
x := make([]byte, length)
|
||||
y := make([]byte, length)
|
||||
res := make([]byte, length)
|
||||
|
||||
_, err := crand.Read(x)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
} else if !ValidateDataIntegrity(x, length) {
|
||||
return nil, errors.New("crypto/rand failed to generate secure random data")
|
||||
}
|
||||
_, err = mrand.Read(y) // nolint: gosec
|
||||
if err != nil {
|
||||
return nil, err
|
||||
} else if !ValidateDataIntegrity(y, length) {
|
||||
return nil, errors.New("math/rand failed to generate secure random data")
|
||||
}
|
||||
for i := 0; i < length; i++ {
|
||||
res[i] = x[i] ^ y[i]
|
||||
}
|
||||
if !ValidateDataIntegrity(res, length) {
|
||||
return nil, errors.New("failed to generate secure random data")
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
|
||||
// GenerateRandomID generates a random string, which is then returned to be used as a key id
|
||||
func GenerateRandomID() (id string, err error) {
|
||||
buf, err := GenerateSecureRandomData(KeyIDSize)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if !ValidateDataIntegrity(buf, KeyIDSize) {
|
||||
return "", fmt.Errorf("error in generateRandomID: crypto/rand failed to generate random data")
|
||||
}
|
||||
id = common.Bytes2Hex(buf)
|
||||
return id, err
|
||||
}
|
||||
|
||||
// ValidateDataIntegrity returns false if the data have the wrong or contains all zeros,
|
||||
// which is the simplest and the most common bug.
|
||||
func ValidateDataIntegrity(k []byte, expectedSize int) bool {
|
||||
if len(k) != expectedSize {
|
||||
return false
|
||||
}
|
||||
if expectedSize > 3 && ContainsOnlyZeros(k) {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
+255
@@ -0,0 +1,255 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"crypto/ecdsa"
|
||||
"errors"
|
||||
"fmt"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"github.com/waku-org/go-waku/waku/v2/payload"
|
||||
"github.com/waku-org/go-waku/waku/v2/protocol"
|
||||
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
"github.com/ethereum/go-ethereum/crypto"
|
||||
"github.com/ethereum/go-ethereum/log"
|
||||
)
|
||||
|
||||
// MessageType represents where this message comes from
|
||||
type MessageType int
|
||||
|
||||
const (
|
||||
RelayedMessageType MessageType = iota
|
||||
StoreMessageType
|
||||
)
|
||||
|
||||
// MessageParams specifies the exact way a message should be wrapped
|
||||
// into an Envelope.
|
||||
type MessageParams struct {
|
||||
Src *ecdsa.PrivateKey
|
||||
Dst *ecdsa.PublicKey
|
||||
KeySym []byte
|
||||
Topic TopicType
|
||||
Payload []byte
|
||||
Padding []byte
|
||||
}
|
||||
|
||||
// ReceivedMessage represents a data packet to be received through the
|
||||
// WakuV2 protocol and successfully decrypted.
|
||||
type ReceivedMessage struct {
|
||||
Envelope *protocol.Envelope // Wrapped Waku Message
|
||||
|
||||
MsgType MessageType
|
||||
|
||||
Data []byte
|
||||
Padding []byte
|
||||
Signature []byte
|
||||
|
||||
Sent uint32 // Time when the message was posted into the network
|
||||
Src *ecdsa.PublicKey // Message recipient (identity used to decode the message)
|
||||
Dst *ecdsa.PublicKey // Message recipient (identity used to decode the message)
|
||||
|
||||
PubsubTopic string
|
||||
ContentTopic TopicType
|
||||
|
||||
SymKeyHash common.Hash // The Keccak256Hash of the key
|
||||
|
||||
hash common.Hash
|
||||
|
||||
Processed atomic.Bool
|
||||
}
|
||||
|
||||
// MessagesRequest contains details of a request for historic messages.
|
||||
type MessagesRequest struct {
|
||||
// ID of the request. The current implementation requires ID to be 32-byte array,
|
||||
// however, it's not enforced for future implementation.
|
||||
ID []byte `json:"id"`
|
||||
|
||||
// From is a lower bound of time range.
|
||||
From uint32 `json:"from"`
|
||||
|
||||
// To is a upper bound of time range.
|
||||
To uint32 `json:"to"`
|
||||
|
||||
// Limit determines the number of messages sent by the mail server
|
||||
// for the current paginated request.
|
||||
Limit uint32 `json:"limit"`
|
||||
|
||||
// Cursor is used as starting point for paginated requests.
|
||||
Cursor []byte `json:"cursor"`
|
||||
|
||||
// Topics is a list of topics. A returned message should
|
||||
// belong to one of the topics from the list.
|
||||
Topics [][]byte `json:"topics"`
|
||||
}
|
||||
|
||||
func (r MessagesRequest) Validate() error {
|
||||
if len(r.ID) != common.HashLength {
|
||||
return errors.New("invalid 'ID', expected a 32-byte slice")
|
||||
}
|
||||
|
||||
if r.From > r.To {
|
||||
return errors.New("invalid 'From' value which is greater than To")
|
||||
}
|
||||
|
||||
if r.Limit > MaxLimitInMessagesRequest {
|
||||
return fmt.Errorf("invalid 'Limit' value, expected value lower than %d", MaxLimitInMessagesRequest)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// EnvelopeError code and optional description of the error.
|
||||
type EnvelopeError struct {
|
||||
Hash common.Hash
|
||||
Code uint
|
||||
Description string
|
||||
}
|
||||
|
||||
// ErrorToEnvelopeError converts common golang error into EnvelopeError with a code.
|
||||
func ErrorToEnvelopeError(hash common.Hash, err error) EnvelopeError {
|
||||
code := EnvelopeOtherError
|
||||
switch err.(type) {
|
||||
case TimeSyncError:
|
||||
code = EnvelopeTimeNotSynced
|
||||
}
|
||||
return EnvelopeError{
|
||||
Hash: hash,
|
||||
Code: code,
|
||||
Description: err.Error(),
|
||||
}
|
||||
}
|
||||
|
||||
// MessagesResponse sent as a response after processing batch of envelopes.
|
||||
type MessagesResponse struct {
|
||||
// Hash is a hash of all envelopes sent in the single batch.
|
||||
Hash common.Hash
|
||||
// Per envelope error.
|
||||
Errors []EnvelopeError
|
||||
}
|
||||
|
||||
func (msg *ReceivedMessage) isSymmetricEncryption() bool {
|
||||
return msg.SymKeyHash != common.Hash{}
|
||||
}
|
||||
|
||||
func (msg *ReceivedMessage) isAsymmetricEncryption() bool {
|
||||
return msg.Dst != nil
|
||||
}
|
||||
|
||||
// MessageStore defines interface for temporary message store.
|
||||
type MessageStore interface {
|
||||
Add(*ReceivedMessage) error
|
||||
Pop() ([]*ReceivedMessage, error)
|
||||
}
|
||||
|
||||
// NewMemoryMessageStore returns pointer to an instance of the MemoryMessageStore.
|
||||
func NewMemoryMessageStore() *MemoryMessageStore {
|
||||
return &MemoryMessageStore{
|
||||
messages: map[common.Hash]*ReceivedMessage{},
|
||||
}
|
||||
}
|
||||
|
||||
// MemoryMessageStore represents messages stored in a memory hash table.
|
||||
type MemoryMessageStore struct {
|
||||
mu sync.Mutex
|
||||
messages map[common.Hash]*ReceivedMessage
|
||||
}
|
||||
|
||||
func NewReceivedMessage(env *protocol.Envelope, msgType MessageType) *ReceivedMessage {
|
||||
ct, err := ExtractTopicFromContentTopic(env.Message().ContentTopic)
|
||||
if err != nil {
|
||||
log.Debug("failed to extract content topic from message", "topic", env.Message().ContentTopic, "err", err)
|
||||
return nil
|
||||
}
|
||||
|
||||
return &ReceivedMessage{
|
||||
Envelope: env,
|
||||
MsgType: msgType,
|
||||
Sent: uint32(env.Message().GetTimestamp() / int64(time.Second)),
|
||||
ContentTopic: ct,
|
||||
PubsubTopic: env.PubsubTopic(),
|
||||
}
|
||||
}
|
||||
|
||||
// Hash returns the SHA3 hash of the envelope, calculating it if not yet done.
|
||||
func (msg *ReceivedMessage) Hash() common.Hash {
|
||||
if (msg.hash == common.Hash{}) {
|
||||
msg.hash = common.BytesToHash(msg.Envelope.Hash())
|
||||
}
|
||||
return msg.hash
|
||||
}
|
||||
|
||||
// Add adds message to store.
|
||||
func (store *MemoryMessageStore) Add(msg *ReceivedMessage) error {
|
||||
store.mu.Lock()
|
||||
defer store.mu.Unlock()
|
||||
if _, exist := store.messages[msg.Hash()]; !exist {
|
||||
store.messages[msg.Hash()] = msg
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Pop returns all available messages and cleans the store.
|
||||
func (store *MemoryMessageStore) Pop() ([]*ReceivedMessage, error) {
|
||||
store.mu.Lock()
|
||||
defer store.mu.Unlock()
|
||||
all := make([]*ReceivedMessage, 0, len(store.messages))
|
||||
for hash, msg := range store.messages {
|
||||
delete(store.messages, hash)
|
||||
all = append(all, msg)
|
||||
}
|
||||
return all, nil
|
||||
}
|
||||
|
||||
// Open tries to decrypt an message, and populates the message fields in case of success.
|
||||
func (msg *ReceivedMessage) Open(watcher *Filter) (result *ReceivedMessage) {
|
||||
if watcher == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
// The API interface forbids filters doing both symmetric and asymmetric encryption.
|
||||
if watcher.expectsAsymmetricEncryption() && watcher.expectsSymmetricEncryption() {
|
||||
return nil
|
||||
}
|
||||
|
||||
// TODO: should we update msg instead of creating a new received message?
|
||||
result = new(ReceivedMessage)
|
||||
|
||||
keyInfo := new(payload.KeyInfo)
|
||||
if watcher.expectsAsymmetricEncryption() {
|
||||
keyInfo.Kind = payload.Asymmetric
|
||||
keyInfo.PrivKey = watcher.KeyAsym
|
||||
msg.Dst = &watcher.KeyAsym.PublicKey
|
||||
} else if watcher.expectsSymmetricEncryption() {
|
||||
keyInfo.Kind = payload.Symmetric
|
||||
keyInfo.SymKey = watcher.KeySym
|
||||
msg.SymKeyHash = crypto.Keccak256Hash(watcher.KeySym)
|
||||
}
|
||||
|
||||
raw, err := payload.DecodePayload(msg.Envelope.Message(), keyInfo)
|
||||
|
||||
if err != nil {
|
||||
log.Error("failed to decode message", "err", err)
|
||||
return nil
|
||||
}
|
||||
|
||||
result.Envelope = msg.Envelope
|
||||
result.Data = raw.Data
|
||||
result.Padding = raw.Padding
|
||||
result.Signature = raw.Signature
|
||||
result.Src = raw.PubKey
|
||||
|
||||
result.Sent = uint32(msg.Envelope.Message().GetTimestamp() / int64(time.Second))
|
||||
|
||||
ct, err := ExtractTopicFromContentTopic(msg.Envelope.Message().ContentTopic)
|
||||
if err != nil {
|
||||
log.Error("failed to decode message", "err", err)
|
||||
return nil
|
||||
}
|
||||
|
||||
result.PubsubTopic = watcher.PubsubTopic
|
||||
result.ContentTopic = ct
|
||||
|
||||
return result
|
||||
}
|
||||
+84
@@ -0,0 +1,84 @@
|
||||
// Copyright 2019 The Waku Library Authors.
|
||||
//
|
||||
// The Waku 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 Waku library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty off
|
||||
// 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 Waku library. If not, see <http://www.gnu.org/licenses/>.
|
||||
//
|
||||
// This software uses the go-ethereum library, which is licensed
|
||||
// under the GNU Lesser General Public Library, version 3 or any later.
|
||||
|
||||
package common
|
||||
|
||||
import (
|
||||
prom "github.com/prometheus/client_golang/prometheus"
|
||||
)
|
||||
|
||||
var (
|
||||
EnvelopesReceivedCounter = prom.NewCounter(prom.CounterOpts{
|
||||
Name: "waku2_envelopes_received_total",
|
||||
Help: "Number of envelopes received.",
|
||||
})
|
||||
EnvelopesValidatedCounter = prom.NewCounter(prom.CounterOpts{
|
||||
Name: "waku2_envelopes_validated_total",
|
||||
Help: "Number of envelopes processed successfully.",
|
||||
})
|
||||
EnvelopesRejectedCounter = prom.NewCounterVec(prom.CounterOpts{
|
||||
Name: "waku2_envelopes_rejected_total",
|
||||
Help: "Number of envelopes rejected.",
|
||||
}, []string{"reason"})
|
||||
EnvelopesCacheFailedCounter = prom.NewCounterVec(prom.CounterOpts{
|
||||
Name: "waku2_envelopes_cache_failures_total",
|
||||
Help: "Number of envelopes which failed to be cached.",
|
||||
}, []string{"type"})
|
||||
EnvelopesCachedCounter = prom.NewCounterVec(prom.CounterOpts{
|
||||
Name: "waku2_envelopes_cached_total",
|
||||
Help: "Number of envelopes cached.",
|
||||
}, []string{"cache"})
|
||||
EnvelopesSizeMeter = prom.NewHistogram(prom.HistogramOpts{
|
||||
Name: "waku2_envelopes_size_bytes",
|
||||
Help: "Size of processed Waku envelopes in bytes.",
|
||||
Buckets: prom.ExponentialBuckets(256, 4, 10),
|
||||
})
|
||||
RateLimitsProcessed = prom.NewCounter(prom.CounterOpts{
|
||||
Name: "waku2_rate_limits_processed_total",
|
||||
Help: "Number of packets Waku rate limiter processed.",
|
||||
})
|
||||
RateLimitsExceeded = prom.NewCounterVec(prom.CounterOpts{
|
||||
Name: "waku2_rate_limits_exceeded_total",
|
||||
Help: "Number of times the Waku rate limits were exceeded",
|
||||
}, []string{"type"})
|
||||
BridgeSent = prom.NewCounter(prom.CounterOpts{
|
||||
Name: "waku2_bridge_sent_total",
|
||||
Help: "Number of envelopes bridged from Waku",
|
||||
})
|
||||
BridgeReceivedSucceed = prom.NewCounter(prom.CounterOpts{
|
||||
Name: "waku2_bridge_received_success_total",
|
||||
Help: "Number of envelopes bridged to Waku and successfully added",
|
||||
})
|
||||
BridgeReceivedFailed = prom.NewCounter(prom.CounterOpts{
|
||||
Name: "waku2_bridge_received_failure_total",
|
||||
Help: "Number of envelopes bridged to Waku and failed to be added",
|
||||
})
|
||||
)
|
||||
|
||||
func init() {
|
||||
prom.MustRegister(EnvelopesReceivedCounter)
|
||||
prom.MustRegister(EnvelopesRejectedCounter)
|
||||
prom.MustRegister(EnvelopesCacheFailedCounter)
|
||||
prom.MustRegister(EnvelopesCachedCounter)
|
||||
prom.MustRegister(EnvelopesSizeMeter)
|
||||
prom.MustRegister(RateLimitsProcessed)
|
||||
prom.MustRegister(RateLimitsExceeded)
|
||||
prom.MustRegister(BridgeSent)
|
||||
prom.MustRegister(BridgeReceivedSucceed)
|
||||
prom.MustRegister(BridgeReceivedFailed)
|
||||
}
|
||||
+118
@@ -0,0 +1,118 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/ethereum/go-ethereum/rlp"
|
||||
"github.com/status-im/status-go/eth-node/types"
|
||||
)
|
||||
|
||||
type Measure struct {
|
||||
Timestamp int64
|
||||
Size uint64
|
||||
}
|
||||
|
||||
type StatsTracker struct {
|
||||
Uploads []Measure
|
||||
Downloads []Measure
|
||||
|
||||
statsMutex sync.Mutex
|
||||
}
|
||||
|
||||
const measurementPeriod = 15 * time.Second
|
||||
|
||||
func measure(input interface{}) (*Measure, error) {
|
||||
b, err := rlp.EncodeToBytes(input)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &Measure{
|
||||
Timestamp: time.Now().UnixNano(),
|
||||
Size: uint64(len(b)),
|
||||
}, nil
|
||||
|
||||
}
|
||||
|
||||
func (s *StatsTracker) AddUpload(input interface{}) {
|
||||
go func(input interface{}) {
|
||||
m, err := measure(input)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
s.statsMutex.Lock()
|
||||
defer s.statsMutex.Unlock()
|
||||
s.Uploads = append(s.Uploads, *m)
|
||||
}(input)
|
||||
}
|
||||
|
||||
func (s *StatsTracker) AddDownload(input interface{}) {
|
||||
go func(input interface{}) {
|
||||
m, err := measure(input)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
s.statsMutex.Lock()
|
||||
defer s.statsMutex.Unlock()
|
||||
s.Downloads = append(s.Downloads, *m)
|
||||
}(input)
|
||||
}
|
||||
|
||||
func (s *StatsTracker) AddUploadBytes(size uint64) {
|
||||
go func(size uint64) {
|
||||
m := Measure{
|
||||
Timestamp: time.Now().UnixNano(),
|
||||
Size: size,
|
||||
}
|
||||
|
||||
s.statsMutex.Lock()
|
||||
defer s.statsMutex.Unlock()
|
||||
s.Uploads = append(s.Uploads, m)
|
||||
}(size)
|
||||
}
|
||||
|
||||
func (s *StatsTracker) AddDownloadBytes(size uint64) {
|
||||
go func(size uint64) {
|
||||
m := Measure{
|
||||
Timestamp: time.Now().UnixNano(),
|
||||
Size: size,
|
||||
}
|
||||
|
||||
s.statsMutex.Lock()
|
||||
defer s.statsMutex.Unlock()
|
||||
s.Downloads = append(s.Downloads, m)
|
||||
}(size)
|
||||
}
|
||||
|
||||
func calculateAverage(measures []Measure, minTime int64) (validMeasures []Measure, rate uint64) {
|
||||
for _, m := range measures {
|
||||
if m.Timestamp > minTime {
|
||||
// Only use recent measures
|
||||
validMeasures = append(validMeasures, m)
|
||||
rate += m.Size
|
||||
}
|
||||
}
|
||||
|
||||
rate /= (uint64(measurementPeriod) / uint64(1*time.Second))
|
||||
return
|
||||
}
|
||||
|
||||
func (s *StatsTracker) GetRatePerSecond() (uploadRate uint64, downloadRate uint64) {
|
||||
s.statsMutex.Lock()
|
||||
defer s.statsMutex.Unlock()
|
||||
minTime := time.Now().Add(-measurementPeriod).UnixNano()
|
||||
s.Uploads, uploadRate = calculateAverage(s.Uploads, minTime)
|
||||
s.Downloads, downloadRate = calculateAverage(s.Downloads, minTime)
|
||||
return
|
||||
}
|
||||
|
||||
func (s *StatsTracker) GetStats() types.StatsSummary {
|
||||
uploadRate, downloadRate := s.GetRatePerSecond()
|
||||
summary := types.StatsSummary{
|
||||
UploadRate: uploadRate,
|
||||
DownloadRate: downloadRate,
|
||||
}
|
||||
return summary
|
||||
}
|
||||
+104
@@ -0,0 +1,104 @@
|
||||
// Copyright 2019 The Waku Library Authors.
|
||||
//
|
||||
// The Waku 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 Waku library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty off
|
||||
// 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 Waku library. If not, see <http://www.gnu.org/licenses/>.
|
||||
//
|
||||
// This software uses the go-ethereum library, which is licensed
|
||||
// under the GNU Lesser General Public Library, version 3 or any later.
|
||||
|
||||
package common
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"strings"
|
||||
|
||||
"github.com/ethereum/go-ethereum/common/hexutil"
|
||||
)
|
||||
|
||||
// TopicType represents a cryptographically secure, probabilistic partial
|
||||
// classifications of a message, determined as the first (leftmost) 4 bytes of the
|
||||
// SHA3 hash of some arbitrary data given by the original author of the message.
|
||||
type TopicType [TopicLength]byte
|
||||
|
||||
type TopicSet map[TopicType]struct{}
|
||||
|
||||
func NewTopicSet(topics []TopicType) TopicSet {
|
||||
s := make(TopicSet, len(topics))
|
||||
for _, t := range topics {
|
||||
s[t] = struct{}{}
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
func NewTopicSetFromBytes(byteArrays [][]byte) TopicSet {
|
||||
topics := make([]TopicType, len(byteArrays))
|
||||
for i, byteArr := range byteArrays {
|
||||
topics[i] = BytesToTopic(byteArr)
|
||||
}
|
||||
return NewTopicSet(topics)
|
||||
}
|
||||
|
||||
// BytesToTopic converts from the byte array representation of a topic
|
||||
// into the TopicType type.
|
||||
func BytesToTopic(b []byte) (t TopicType) {
|
||||
sz := TopicLength
|
||||
if x := len(b); x < TopicLength {
|
||||
sz = x
|
||||
}
|
||||
for i := 0; i < sz; i++ {
|
||||
t[i] = b[i]
|
||||
}
|
||||
return t
|
||||
}
|
||||
|
||||
func StringToTopic(s string) (t TopicType) {
|
||||
str, _ := hexutil.Decode(s)
|
||||
return BytesToTopic(str)
|
||||
}
|
||||
|
||||
// String converts a topic byte array to a string representation.
|
||||
func (t *TopicType) String() string {
|
||||
return hexutil.Encode(t[:])
|
||||
}
|
||||
|
||||
// MarshalText returns the hex representation of t.
|
||||
func (t TopicType) MarshalText() ([]byte, error) {
|
||||
return hexutil.Bytes(t[:]).MarshalText()
|
||||
}
|
||||
|
||||
// UnmarshalText parses a hex representation to a topic.
|
||||
func (t *TopicType) UnmarshalText(input []byte) error {
|
||||
return hexutil.UnmarshalFixedText("Topic", input, t[:])
|
||||
}
|
||||
|
||||
// Converts a topic to its 23/WAKU2-TOPICS representation
|
||||
func (t TopicType) ContentTopic() string {
|
||||
enc := hexutil.Encode(t[:])
|
||||
return "/waku/1/" + enc + "/rfc26"
|
||||
}
|
||||
|
||||
func ExtractTopicFromContentTopic(s string) (TopicType, error) {
|
||||
p := strings.Split(s, "/")
|
||||
|
||||
if len(p) != 5 || p[1] != "waku" || p[2] != "1" || p[4] != "rfc26" {
|
||||
return TopicType{}, errors.New("invalid content topic format")
|
||||
}
|
||||
|
||||
str, err := hexutil.Decode(p[3])
|
||||
if err != nil {
|
||||
return TopicType{}, err
|
||||
}
|
||||
|
||||
result := BytesToTopic(str)
|
||||
return result, nil
|
||||
}
|
||||
Reference in New Issue
Block a user