2020-01-13 19:17:30 +00:00
|
|
|
package types
|
|
|
|
|
|
|
|
import (
|
2023-01-26 16:29:00 +00:00
|
|
|
"context"
|
2020-01-13 19:17:30 +00:00
|
|
|
"crypto/ecdsa"
|
2024-09-19 20:16:53 +00:00
|
|
|
"crypto/sha256"
|
|
|
|
"encoding/hex"
|
2024-08-16 18:24:21 +00:00
|
|
|
"encoding/json"
|
2024-09-19 20:16:53 +00:00
|
|
|
"fmt"
|
2022-01-12 16:02:01 +00:00
|
|
|
"sync"
|
2020-01-13 19:17:30 +00:00
|
|
|
"time"
|
2021-11-26 12:30:35 +00:00
|
|
|
|
2023-02-22 21:58:17 +00:00
|
|
|
"github.com/libp2p/go-libp2p/core/peer"
|
|
|
|
"github.com/libp2p/go-libp2p/core/protocol"
|
2024-08-16 18:24:21 +00:00
|
|
|
"github.com/multiformats/go-multiaddr"
|
2022-01-12 16:02:01 +00:00
|
|
|
"github.com/pborman/uuid"
|
|
|
|
|
2024-09-19 20:16:53 +00:00
|
|
|
"github.com/waku-org/go-waku/waku/v2/api/history"
|
|
|
|
|
2021-11-26 12:30:35 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2024-08-16 18:24:21 +00:00
|
|
|
"github.com/ethereum/go-ethereum/p2p/enode"
|
2022-12-09 16:16:21 +00:00
|
|
|
"github.com/status-im/status-go/connection"
|
2020-01-13 19:17:30 +00:00
|
|
|
)
|
|
|
|
|
2022-01-12 16:02:01 +00:00
|
|
|
type ConnStatus struct {
|
2024-08-16 18:24:21 +00:00
|
|
|
IsOnline bool `json:"isOnline"`
|
|
|
|
Peers PeerStats `json:"peers"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type PeerStats map[peer.ID]WakuV2Peer
|
|
|
|
|
|
|
|
func (m PeerStats) MarshalJSON() ([]byte, error) {
|
|
|
|
tmpMap := make(map[string]WakuV2Peer)
|
|
|
|
for k, v := range m {
|
|
|
|
tmpMap[k.String()] = v
|
|
|
|
}
|
|
|
|
return json.Marshal(tmpMap)
|
2022-11-24 15:00:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type WakuV2Peer struct {
|
2024-08-16 18:24:21 +00:00
|
|
|
Protocols []protocol.ID `json:"protocols"`
|
|
|
|
Addresses []multiaddr.Multiaddr `json:"addresses"`
|
2022-01-12 16:02:01 +00:00
|
|
|
}
|
|
|
|
|
2024-07-01 15:37:54 +00:00
|
|
|
type PeerList struct {
|
2024-08-16 18:24:21 +00:00
|
|
|
FullMeshPeers peer.IDSlice `json:"fullMesh"`
|
|
|
|
AllPeers peer.IDSlice `json:"all"`
|
2024-07-01 15:37:54 +00:00
|
|
|
}
|
|
|
|
|
2022-01-12 16:02:01 +00:00
|
|
|
type ConnStatusSubscription struct {
|
|
|
|
sync.RWMutex
|
|
|
|
|
|
|
|
ID string
|
|
|
|
C chan ConnStatus
|
|
|
|
active bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewConnStatusSubscription() *ConnStatusSubscription {
|
|
|
|
return &ConnStatusSubscription{
|
|
|
|
ID: uuid.NewRandom().String(),
|
|
|
|
C: make(chan ConnStatus, 100),
|
|
|
|
active: true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *ConnStatusSubscription) Active() bool {
|
|
|
|
u.RLock()
|
|
|
|
defer u.RUnlock()
|
|
|
|
return u.active
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *ConnStatusSubscription) Unsubscribe() {
|
|
|
|
u.Lock()
|
|
|
|
defer u.Unlock()
|
|
|
|
close(u.C)
|
|
|
|
u.active = false
|
|
|
|
}
|
|
|
|
|
2024-02-19 23:44:38 +00:00
|
|
|
func (u *ConnStatusSubscription) Send(s ConnStatus) bool {
|
|
|
|
u.RLock()
|
|
|
|
defer u.RUnlock()
|
|
|
|
if !u.active {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
u.C <- s
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2022-02-10 15:15:27 +00:00
|
|
|
type WakuKeyManager interface {
|
|
|
|
// GetPrivateKey retrieves the private key of the specified identity.
|
|
|
|
GetPrivateKey(id string) (*ecdsa.PrivateKey, error)
|
|
|
|
// AddKeyPair imports a asymmetric private key and returns a deterministic identifier.
|
|
|
|
AddKeyPair(key *ecdsa.PrivateKey) (string, error)
|
|
|
|
// DeleteKeyPair deletes the key with the specified ID if it exists.
|
|
|
|
DeleteKeyPair(keyID string) bool
|
|
|
|
// DeleteKeyPairs deletes all the keys
|
|
|
|
DeleteKeyPairs() error
|
|
|
|
AddSymKeyDirect(key []byte) (string, error)
|
|
|
|
AddSymKeyFromPassword(password string) (string, error)
|
|
|
|
DeleteSymKey(id string) bool
|
|
|
|
GetSymKey(id string) ([]byte, error)
|
|
|
|
}
|
|
|
|
|
2020-01-13 19:17:30 +00:00
|
|
|
// Whisper represents a dark communication interface through the Ethereum
|
|
|
|
// network, using its very own P2P communication layer.
|
|
|
|
type Waku interface {
|
|
|
|
PublicWakuAPI() PublicWakuAPI
|
|
|
|
|
2021-07-21 19:02:50 +00:00
|
|
|
// Waku protocol version
|
|
|
|
Version() uint
|
|
|
|
|
2021-08-30 14:57:28 +00:00
|
|
|
// PeerCount
|
|
|
|
PeerCount() int
|
|
|
|
|
2024-08-16 18:24:21 +00:00
|
|
|
ListenAddresses() ([]multiaddr.Multiaddr, error)
|
2022-11-24 21:27:46 +00:00
|
|
|
|
2024-07-01 15:37:54 +00:00
|
|
|
RelayPeersByTopic(topic string) (*PeerList, error)
|
|
|
|
|
2024-08-16 18:24:21 +00:00
|
|
|
ENR() (*enode.Node, error)
|
2024-06-18 19:48:49 +00:00
|
|
|
|
2024-08-16 18:24:21 +00:00
|
|
|
Peers() PeerStats
|
2021-08-30 14:57:28 +00:00
|
|
|
|
2021-11-22 13:40:14 +00:00
|
|
|
StartDiscV5() error
|
|
|
|
|
|
|
|
StopDiscV5() error
|
|
|
|
|
2023-05-22 21:38:02 +00:00
|
|
|
SubscribeToPubsubTopic(topic string, optPublicKey *ecdsa.PublicKey) error
|
|
|
|
|
2024-01-30 17:56:59 +00:00
|
|
|
UnsubscribeFromPubsubTopic(topic string) error
|
|
|
|
|
2023-05-22 21:38:02 +00:00
|
|
|
StorePubsubTopicKey(topic string, privKey *ecdsa.PrivateKey) error
|
|
|
|
|
2023-10-12 19:21:49 +00:00
|
|
|
RetrievePubsubTopicKey(topic string) (*ecdsa.PrivateKey, error)
|
|
|
|
|
2024-01-30 17:56:59 +00:00
|
|
|
RemovePubsubTopicKey(topic string) error
|
|
|
|
|
2024-08-16 18:24:21 +00:00
|
|
|
AddRelayPeer(address multiaddr.Multiaddr) (peer.ID, error)
|
2021-09-10 17:06:06 +00:00
|
|
|
|
2024-08-16 18:24:21 +00:00
|
|
|
DialPeer(address multiaddr.Multiaddr) error
|
2021-09-10 17:06:06 +00:00
|
|
|
|
2024-08-16 18:24:21 +00:00
|
|
|
DialPeerByID(peerID peer.ID) error
|
2021-08-30 14:57:28 +00:00
|
|
|
|
2024-08-16 18:24:21 +00:00
|
|
|
DropPeer(peerID peer.ID) error
|
2021-08-30 14:57:28 +00:00
|
|
|
|
2022-01-12 16:02:01 +00:00
|
|
|
SubscribeToConnStatusChanges() (*ConnStatusSubscription, error)
|
|
|
|
|
2024-08-21 17:45:32 +00:00
|
|
|
SetCriteriaForMissingMessageVerification(peerID peer.ID, pubsubTopic string, contentTopics []TopicType) error
|
2024-06-26 18:25:44 +00:00
|
|
|
|
2020-01-13 19:17:30 +00:00
|
|
|
// MinPow returns the PoW value required by this node.
|
|
|
|
MinPow() float64
|
|
|
|
// BloomFilter returns the aggregated bloom filter for all the topics of interest.
|
|
|
|
// The nodes are required to send only messages that match the advertised bloom filter.
|
|
|
|
// If a message does not match the bloom, it will tantamount to spam, and the peer will
|
|
|
|
// be disconnected.
|
|
|
|
BloomFilter() []byte
|
2023-03-24 12:05:42 +00:00
|
|
|
|
2020-01-13 19:17:30 +00:00
|
|
|
// GetCurrentTime returns current time.
|
|
|
|
GetCurrentTime() time.Time
|
|
|
|
|
|
|
|
// GetPrivateKey retrieves the private key of the specified identity.
|
|
|
|
GetPrivateKey(id string) (*ecdsa.PrivateKey, error)
|
|
|
|
|
|
|
|
SubscribeEnvelopeEvents(events chan<- EnvelopeEvent) Subscription
|
|
|
|
|
|
|
|
// AddKeyPair imports a asymmetric private key and returns a deterministic identifier.
|
|
|
|
AddKeyPair(key *ecdsa.PrivateKey) (string, error)
|
|
|
|
// DeleteKeyPair deletes the key with the specified ID if it exists.
|
|
|
|
DeleteKeyPair(keyID string) bool
|
|
|
|
AddSymKeyDirect(key []byte) (string, error)
|
|
|
|
AddSymKeyFromPassword(password string) (string, error)
|
|
|
|
DeleteSymKey(id string) bool
|
|
|
|
GetSymKey(id string) ([]byte, error)
|
2020-11-03 12:42:42 +00:00
|
|
|
MaxMessageSize() uint32
|
2020-01-13 19:17:30 +00:00
|
|
|
|
2021-08-03 19:27:15 +00:00
|
|
|
GetStats() StatsSummary
|
|
|
|
|
2020-01-13 19:17:30 +00:00
|
|
|
Subscribe(opts *SubscriptionOptions) (string, error)
|
|
|
|
GetFilter(id string) Filter
|
2023-07-14 13:42:02 +00:00
|
|
|
Unsubscribe(ctx context.Context, id string) error
|
2021-01-14 22:15:13 +00:00
|
|
|
UnsubscribeMany(ids []string) error
|
2020-01-13 19:17:30 +00:00
|
|
|
|
2021-11-26 12:30:35 +00:00
|
|
|
// ProcessingP2PMessages indicates whether there are in-flight p2p messages
|
|
|
|
ProcessingP2PMessages() bool
|
|
|
|
|
|
|
|
// MarkP2PMessageAsProcessed tells the waku layer that a P2P message has been processed
|
|
|
|
MarkP2PMessageAsProcessed(common.Hash)
|
2022-12-09 16:16:21 +00:00
|
|
|
|
|
|
|
// ConnectionChanged is called whenever the client knows its connection status has changed
|
|
|
|
ConnectionChanged(connection.State)
|
2024-01-30 13:43:34 +00:00
|
|
|
|
|
|
|
// ClearEnvelopesCache clears waku envelopes cache
|
|
|
|
ClearEnvelopesCache()
|
2024-06-11 07:45:01 +00:00
|
|
|
|
|
|
|
// ConfirmMessageDelivered updates a message has been delivered in waku
|
|
|
|
ConfirmMessageDelivered(hash []common.Hash)
|
|
|
|
|
2024-08-01 03:27:43 +00:00
|
|
|
// PeerID returns node's PeerID
|
|
|
|
PeerID() peer.ID
|
2024-09-05 14:25:26 +00:00
|
|
|
|
2024-09-19 20:16:53 +00:00
|
|
|
// GetActiveStorenode returns the peer ID of the currently active storenode. It will be empty if no storenode is active
|
|
|
|
GetActiveStorenode() peer.ID
|
|
|
|
|
|
|
|
// OnStorenodeAvailableOneShot returns a channel that will be triggered only once when a storenode becomes available
|
|
|
|
OnStorenodeAvailableOneShot() <-chan struct{}
|
|
|
|
|
|
|
|
// OnStorenodeChanged is triggered when a new storenode is promoted to become the active storenode or when the active storenode is removed
|
|
|
|
OnStorenodeChanged() <-chan peer.ID
|
|
|
|
|
|
|
|
// OnStorenodeNotWorking is triggered when the last active storenode fails to return results consistently
|
|
|
|
OnStorenodeNotWorking() <-chan struct{}
|
|
|
|
|
|
|
|
// OnStorenodeAvailable is triggered when there is a new active storenode selected
|
|
|
|
OnStorenodeAvailable() <-chan peer.ID
|
|
|
|
|
|
|
|
// WaitForAvailableStoreNode will wait for a storenode to be available until `timeout` happens
|
|
|
|
WaitForAvailableStoreNode(timeout time.Duration) bool
|
|
|
|
|
|
|
|
// SetStorenodeConfigProvider will set the configuration provider for the storenode cycle
|
|
|
|
SetStorenodeConfigProvider(c history.StorenodeConfigProvider)
|
|
|
|
|
|
|
|
// ProcessMailserverBatch will receive a criteria and storenode and execute a query
|
|
|
|
ProcessMailserverBatch(
|
|
|
|
ctx context.Context,
|
|
|
|
batch MailserverBatch,
|
|
|
|
storenodeID peer.ID,
|
|
|
|
pageLimit uint64,
|
|
|
|
shouldProcessNextPage func(int) (bool, uint64),
|
|
|
|
processEnvelopes bool,
|
|
|
|
) error
|
|
|
|
|
|
|
|
// IsStorenodeAvailable is used to determine whether a storenode is available or not
|
|
|
|
IsStorenodeAvailable(peerID peer.ID) bool
|
|
|
|
|
|
|
|
PerformStorenodeTask(fn func() error, opts ...history.StorenodeTaskOption) error
|
|
|
|
|
|
|
|
// DisconnectActiveStorenode will trigger a disconnection of the active storenode, and potentially execute a cycling so a new storenode is promoted
|
|
|
|
DisconnectActiveStorenode(ctx context.Context, backoff time.Duration, shouldCycle bool)
|
|
|
|
}
|
|
|
|
|
|
|
|
type MailserverBatch struct {
|
|
|
|
From uint32
|
|
|
|
To uint32
|
|
|
|
Cursor string
|
|
|
|
PubsubTopic string
|
|
|
|
Topics []TopicType
|
|
|
|
ChatIDs []string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mb *MailserverBatch) Hash() string {
|
|
|
|
data := fmt.Sprintf("%d%d%s%s%v%v", mb.From, mb.To, mb.Cursor, mb.PubsubTopic, mb.Topics, mb.ChatIDs)
|
|
|
|
hash := sha256.Sum256([]byte(data))
|
|
|
|
return hex.EncodeToString(hash[:4])
|
2020-01-13 19:17:30 +00:00
|
|
|
}
|