common

package
v1.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 19, 2024 License: MPL-2.0 Imports: 21 Imported by: 0

README

Waku Common

See here

Documentation

Index

Constants

View Source
const (
	TopicLength  = 4  // in bytes
	AESKeyLength = 32 // in bytes
	KeyIDSize    = 32 // in bytes

	DefaultMaxMessageSize = uint32(1 << 20) // DefaultMaximumMessageSize is 1mb.
)

Waku protocol parameters

Variables

View Source
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),
	})
)

Functions

func BytesToUintBigEndian

func BytesToUintBigEndian(b []byte) (res uint64)

BytesToUintBigEndian converts the slice to 64-bit unsigned integer.

func BytesToUintLittleEndian

func BytesToUintLittleEndian(b []byte) (res uint64)

BytesToUintLittleEndian converts the slice to 64-bit unsigned integer.

func ContainsOnlyZeros

func ContainsOnlyZeros(data []byte) bool

ContainsOnlyZeros checks if the data contain only zeros.

func GenerateRandomID

func GenerateRandomID() (id string, err error)

GenerateRandomID generates a random string, which is then returned to be used as a key id

func GenerateSecureRandomData

func GenerateSecureRandomData(length int) ([]byte, error)

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 IsPubKeyEqual

func IsPubKeyEqual(a, b *ecdsa.PublicKey) bool

IsPubKeyEqual checks that two public keys are equal

func ValidateDataIntegrity

func ValidateDataIntegrity(k []byte, expectedSize int) bool

ValidateDataIntegrity returns false if the data have the wrong or contains all zeros, which is the simplest and the most common bug.

func ValidatePublicKey

func ValidatePublicKey(k *ecdsa.PublicKey) bool

ValidatePublicKey checks the format of the given public key.

Types

type ContentTopicToFilter added in v0.166.1

type ContentTopicToFilter = map[TopicType]FilterSet

type EnvelopeError

type EnvelopeError struct {
	Hash        common.Hash
	Code        uint
	Description string
}

EnvelopeError code and optional description of the error.

type EnvelopeEvent

type EnvelopeEvent struct {
	Event EventType
	Topic TopicType
	Hash  common.Hash
	Batch common.Hash
	Peer  enode.ID
	Data  interface{}
}

EnvelopeEvent represents an envelope event.

type EventType

type EventType string

EventType used to define known waku events.

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"
)

type 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

	Messages MessageStore
	// contains filtered or unexported fields
}

Filter represents a Waku message filter

func (*Filter) MatchMessage

func (f *Filter) MatchMessage(msg *ReceivedMessage) bool

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 (*Filter) Retrieve

func (f *Filter) Retrieve() []*ReceivedMessage

Retrieve will return the list of all received messages associated to a filter.

func (*Filter) Trigger

func (f *Filter) Trigger(msg *ReceivedMessage)

Trigger adds a yet-unknown message to the filter's list of received messages.

type FilterSet added in v0.166.1

type FilterSet = map[*Filter]struct{}

type Filters

type Filters struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

Filters represents a collection of filters

func NewFilters

func NewFilters(defaultPubsubTopic string, logger *zap.Logger) *Filters

NewFilters returns a newly created filter collection

func (*Filters) All added in v0.172.10

func (fs *Filters) All() []*Filter

func (*Filters) AllTopics

func (fs *Filters) AllTopics() []TopicType

func (*Filters) Get

func (fs *Filters) Get(id string) *Filter

Get returns a filter from the collection with a specific ID

func (*Filters) GetFilters added in v0.167.6

func (fs *Filters) GetFilters() map[string]*Filter

func (*Filters) GetWatchersByTopic

func (fs *Filters) GetWatchersByTopic(pubsubTopic string, contentTopic TopicType) []*Filter

GetWatchersByTopic returns a slice containing the filters that match a specific topic

func (*Filters) Install

func (fs *Filters) Install(watcher *Filter) (string, error)

Install will add a new filter to the filter collection

func (*Filters) NotifyWatchers

func (fs *Filters) NotifyWatchers(recvMessage *ReceivedMessage) bool

NotifyWatchers notifies any filter that has declared interest for the envelope's topic.

func (*Filters) Uninstall

func (fs *Filters) Uninstall(id string) bool

Uninstall will remove a filter whose id has been specified from the filter collection

type Measure added in v0.83.8

type Measure struct {
	Timestamp int64
	Size      uint64
}

type MemoryMessageStore

type MemoryMessageStore struct {
	// contains filtered or unexported fields
}

MemoryMessageStore represents messages stored in a memory hash table.

func NewMemoryMessageStore

func NewMemoryMessageStore() *MemoryMessageStore

NewMemoryMessageStore returns pointer to an instance of the MemoryMessageStore.

func (*MemoryMessageStore) Add

func (store *MemoryMessageStore) Add(msg *ReceivedMessage) error

Add adds message to store.

func (*MemoryMessageStore) Pop

func (store *MemoryMessageStore) Pop() ([]*ReceivedMessage, error)

Pop returns all available messages and cleans the store.

type MessageParams

type MessageParams struct {
	Src     *ecdsa.PrivateKey
	Dst     *ecdsa.PublicKey
	KeySym  []byte
	Topic   TopicType
	Payload []byte
	Padding []byte
}

MessageParams specifies the exact way a message should be wrapped into an Envelope.

type MessageStore

type MessageStore interface {
	Add(*ReceivedMessage) error
	Pop() ([]*ReceivedMessage, error)
}

MessageStore defines interface for temporary message store.

type MessageType added in v0.91.13

type MessageType = string

MessageType represents where this message comes from

const (
	RelayedMessageType MessageType = "relay"
	StoreMessageType   MessageType = "store"
	SendMessageType    MessageType = "send"
	MissingMessageType MessageType = "missing"
)

type MessagesResponse

type MessagesResponse struct {
	// Hash is a hash of all envelopes sent in the single batch.
	Hash common.Hash
	// Per envelope error.
	Errors []EnvelopeError
}

MessagesResponse sent as a response after processing batch of envelopes.

type PubsubTopicToContentTopic added in v0.166.1

type PubsubTopicToContentTopic = map[string]ContentTopicToFilter

type ReceivedMessage

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 in seconds
	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

	Processed atomic.Bool
	// contains filtered or unexported fields
}

ReceivedMessage represents a data packet to be received through the WakuV2 protocol and successfully decrypted.

func NewReceivedMessage

func NewReceivedMessage(env *protocol.Envelope, msgType MessageType) *ReceivedMessage

func (*ReceivedMessage) Hash

func (msg *ReceivedMessage) Hash() common.Hash

Hash returns the SHA3 hash of the envelope, calculating it if not yet done.

func (*ReceivedMessage) Open

func (msg *ReceivedMessage) Open(watcher *Filter) (result *ReceivedMessage)

Open tries to decrypt an message, and populates the message fields in case of success.

type StatsTracker added in v0.83.8

type StatsTracker struct {
	Uploads   []Measure
	Downloads []Measure
	// contains filtered or unexported fields
}

func (*StatsTracker) AddDownload added in v0.83.8

func (s *StatsTracker) AddDownload(input interface{})

func (*StatsTracker) AddDownloadBytes added in v0.83.8

func (s *StatsTracker) AddDownloadBytes(size uint64)

func (*StatsTracker) AddUpload added in v0.83.8

func (s *StatsTracker) AddUpload(input interface{})

func (*StatsTracker) AddUploadBytes added in v0.83.8

func (s *StatsTracker) AddUploadBytes(size uint64)

func (*StatsTracker) GetRatePerSecond added in v0.83.8

func (s *StatsTracker) GetRatePerSecond() (uploadRate uint64, downloadRate uint64)

func (*StatsTracker) GetStats added in v0.83.8

func (s *StatsTracker) GetStats() types.StatsSummary

type TopicSet added in v0.167.6

type TopicSet map[TopicType]struct{}

func NewTopicSet added in v0.167.6

func NewTopicSet(topics []TopicType) TopicSet

func NewTopicSetFromBytes added in v0.167.6

func NewTopicSetFromBytes(byteArrays [][]byte) TopicSet

func (TopicSet) ContentTopics added in v0.182.41

func (t TopicSet) ContentTopics() []string

type TopicType

type TopicType [TopicLength]byte

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.

func BytesToTopic

func BytesToTopic(b []byte) (t TopicType)

BytesToTopic converts from the byte array representation of a topic into the TopicType type.

func ExtractTopicFromContentTopic added in v0.83.12

func ExtractTopicFromContentTopic(s string) (TopicType, error)

func StringToTopic

func StringToTopic(s string) (t TopicType)

func (TopicType) ContentTopic added in v0.83.12

func (t TopicType) ContentTopic() string

Converts a topic to its 23/WAKU2-TOPICS representation

func (TopicType) MarshalText

func (t TopicType) MarshalText() ([]byte, error)

MarshalText returns the hex representation of t.

func (*TopicType) String

func (t *TopicType) String() string

String converts a topic byte array to a string representation.

func (*TopicType) UnmarshalText

func (t *TopicType) UnmarshalText(input []byte) error

UnmarshalText parses a hex representation to a topic.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL