2021-06-16 20:19:45 +00:00
|
|
|
package gethbridge
|
|
|
|
|
|
|
|
import (
|
2023-01-26 16:29:00 +00:00
|
|
|
"context"
|
2021-06-16 20:19:45 +00:00
|
|
|
"crypto/ecdsa"
|
|
|
|
"time"
|
|
|
|
|
2022-11-04 13:56:45 +00:00
|
|
|
"github.com/libp2p/go-libp2p/core/peer"
|
2024-08-16 18:24:21 +00:00
|
|
|
"github.com/multiformats/go-multiaddr"
|
2024-06-06 13:52:51 +00:00
|
|
|
"google.golang.org/protobuf/proto"
|
2023-10-12 19:21:49 +00:00
|
|
|
|
2024-09-19 20:16:53 +00:00
|
|
|
"github.com/waku-org/go-waku/waku/v2/api/history"
|
2024-07-30 18:28:27 +00:00
|
|
|
"github.com/waku-org/go-waku/waku/v2/protocol"
|
|
|
|
"github.com/waku-org/go-waku/waku/v2/protocol/store"
|
2021-06-16 20:19:45 +00:00
|
|
|
|
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"
|
2024-09-26 22:37:32 +00:00
|
|
|
|
|
|
|
gocommon "github.com/status-im/status-go/common"
|
2022-12-09 16:16:21 +00:00
|
|
|
"github.com/status-im/status-go/connection"
|
2021-06-16 20:19:45 +00:00
|
|
|
"github.com/status-im/status-go/eth-node/types"
|
|
|
|
"github.com/status-im/status-go/wakuv2"
|
|
|
|
wakucommon "github.com/status-im/status-go/wakuv2/common"
|
|
|
|
)
|
|
|
|
|
|
|
|
type gethWakuV2Wrapper struct {
|
|
|
|
waku *wakuv2.Waku
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewGethWakuWrapper returns an object that wraps Geth's Waku in a types interface
|
|
|
|
func NewGethWakuV2Wrapper(w *wakuv2.Waku) types.Waku {
|
|
|
|
if w == nil {
|
|
|
|
panic("waku cannot be nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
return &gethWakuV2Wrapper{
|
|
|
|
waku: w,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetGethWhisperFrom retrieves the underlying whisper Whisper struct from a wrapped Whisper interface
|
|
|
|
func GetGethWakuV2From(m types.Waku) *wakuv2.Waku {
|
|
|
|
return m.(*gethWakuV2Wrapper).waku
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *gethWakuV2Wrapper) PublicWakuAPI() types.PublicWakuAPI {
|
|
|
|
return NewGethPublicWakuV2APIWrapper(wakuv2.NewPublicWakuAPI(w.waku))
|
|
|
|
}
|
|
|
|
|
2021-07-21 19:02:50 +00:00
|
|
|
func (w *gethWakuV2Wrapper) Version() uint {
|
|
|
|
return 2
|
|
|
|
}
|
|
|
|
|
2021-08-30 14:57:28 +00:00
|
|
|
func (w *gethWakuV2Wrapper) PeerCount() int {
|
|
|
|
return w.waku.PeerCount()
|
|
|
|
}
|
|
|
|
|
|
|
|
// DEPRECATED: Not used in WakuV2
|
2021-06-16 20:19:45 +00:00
|
|
|
func (w *gethWakuV2Wrapper) MinPow() float64 {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// MaxMessageSize returns the MaxMessageSize set
|
|
|
|
func (w *gethWakuV2Wrapper) MaxMessageSize() uint32 {
|
|
|
|
return w.waku.MaxMessageSize()
|
|
|
|
}
|
|
|
|
|
2021-08-30 14:57:28 +00:00
|
|
|
// DEPRECATED: not used in WakuV2
|
2021-06-16 20:19:45 +00:00
|
|
|
func (w *gethWakuV2Wrapper) BloomFilter() []byte {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetCurrentTime returns current time.
|
|
|
|
func (w *gethWakuV2Wrapper) GetCurrentTime() time.Time {
|
|
|
|
return w.waku.CurrentTime()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *gethWakuV2Wrapper) SubscribeEnvelopeEvents(eventsProxy chan<- types.EnvelopeEvent) types.Subscription {
|
|
|
|
events := make(chan wakucommon.EnvelopeEvent, 100) // must be buffered to prevent blocking whisper
|
|
|
|
go func() {
|
2024-09-26 22:37:32 +00:00
|
|
|
defer gocommon.LogOnPanic()
|
2021-06-16 20:19:45 +00:00
|
|
|
for e := range events {
|
|
|
|
eventsProxy <- *NewWakuV2EnvelopeEventWrapper(&e)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return NewGethSubscriptionWrapper(w.waku.SubscribeEnvelopeEvents(events))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *gethWakuV2Wrapper) GetPrivateKey(id string) (*ecdsa.PrivateKey, error) {
|
|
|
|
return w.waku.GetPrivateKey(id)
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddKeyPair imports a asymmetric private key and returns a deterministic identifier.
|
|
|
|
func (w *gethWakuV2Wrapper) AddKeyPair(key *ecdsa.PrivateKey) (string, error) {
|
|
|
|
return w.waku.AddKeyPair(key)
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteKeyPair deletes the key with the specified ID if it exists.
|
|
|
|
func (w *gethWakuV2Wrapper) DeleteKeyPair(keyID string) bool {
|
|
|
|
return w.waku.DeleteKeyPair(keyID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *gethWakuV2Wrapper) AddSymKeyDirect(key []byte) (string, error) {
|
|
|
|
return w.waku.AddSymKeyDirect(key)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *gethWakuV2Wrapper) AddSymKeyFromPassword(password string) (string, error) {
|
|
|
|
return w.waku.AddSymKeyFromPassword(password)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *gethWakuV2Wrapper) DeleteSymKey(id string) bool {
|
|
|
|
return w.waku.DeleteSymKey(id)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *gethWakuV2Wrapper) GetSymKey(id string) ([]byte, error) {
|
|
|
|
return w.waku.GetSymKey(id)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *gethWakuV2Wrapper) Subscribe(opts *types.SubscriptionOptions) (string, error) {
|
|
|
|
var (
|
|
|
|
err error
|
|
|
|
keyAsym *ecdsa.PrivateKey
|
|
|
|
keySym []byte
|
|
|
|
)
|
|
|
|
|
|
|
|
if opts.SymKeyID != "" {
|
|
|
|
keySym, err = w.GetSymKey(opts.SymKeyID)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if opts.PrivateKeyID != "" {
|
|
|
|
keyAsym, err = w.GetPrivateKey(opts.PrivateKeyID)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-07 00:18:46 +00:00
|
|
|
f, err := w.createFilterWrapper("", keyAsym, keySym, opts.PubsubTopic, opts.Topics)
|
2021-06-16 20:19:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
id, err := w.waku.Subscribe(GetWakuV2FilterFrom(f))
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
f.(*wakuV2FilterWrapper).id = id
|
|
|
|
return id, nil
|
|
|
|
}
|
|
|
|
|
2021-08-03 19:27:15 +00:00
|
|
|
func (w *gethWakuV2Wrapper) GetStats() types.StatsSummary {
|
|
|
|
return w.waku.GetStats()
|
|
|
|
}
|
|
|
|
|
2021-06-16 20:19:45 +00:00
|
|
|
func (w *gethWakuV2Wrapper) GetFilter(id string) types.Filter {
|
|
|
|
return NewWakuV2FilterWrapper(w.waku.GetFilter(id), id)
|
|
|
|
}
|
|
|
|
|
2023-07-14 13:42:02 +00:00
|
|
|
func (w *gethWakuV2Wrapper) Unsubscribe(ctx context.Context, id string) error {
|
|
|
|
return w.waku.Unsubscribe(ctx, id)
|
2021-06-16 20:19:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (w *gethWakuV2Wrapper) UnsubscribeMany(ids []string) error {
|
|
|
|
return w.waku.UnsubscribeMany(ids)
|
|
|
|
}
|
|
|
|
|
2024-08-07 00:18:46 +00:00
|
|
|
func (w *gethWakuV2Wrapper) createFilterWrapper(id string, keyAsym *ecdsa.PrivateKey, keySym []byte, pubsubTopic string, topics [][]byte) (types.Filter, error) {
|
2021-06-16 20:19:45 +00:00
|
|
|
return NewWakuV2FilterWrapper(&wakucommon.Filter{
|
2023-09-13 10:50:23 +00:00
|
|
|
KeyAsym: keyAsym,
|
|
|
|
KeySym: keySym,
|
|
|
|
ContentTopics: wakucommon.NewTopicSetFromBytes(topics),
|
|
|
|
PubsubTopic: pubsubTopic,
|
|
|
|
Messages: wakucommon.NewMemoryMessageStore(),
|
2021-06-16 20:19:45 +00:00
|
|
|
}, id), nil
|
|
|
|
}
|
|
|
|
|
2021-11-22 13:40:14 +00:00
|
|
|
func (w *gethWakuV2Wrapper) StartDiscV5() error {
|
|
|
|
return w.waku.StartDiscV5()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *gethWakuV2Wrapper) StopDiscV5() error {
|
|
|
|
return w.waku.StopDiscV5()
|
|
|
|
}
|
|
|
|
|
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 (w *gethWakuV2Wrapper) SubscribeToPubsubTopic(topic string, optPublicKey *ecdsa.PublicKey) error {
|
|
|
|
return w.waku.SubscribeToPubsubTopic(topic, optPublicKey)
|
|
|
|
}
|
|
|
|
|
2024-01-30 17:56:59 +00:00
|
|
|
func (w *gethWakuV2Wrapper) UnsubscribeFromPubsubTopic(topic string) error {
|
|
|
|
return w.waku.UnsubscribeFromPubsubTopic(topic)
|
|
|
|
}
|
|
|
|
|
2023-10-12 19:21:49 +00:00
|
|
|
func (w *gethWakuV2Wrapper) RetrievePubsubTopicKey(topic string) (*ecdsa.PrivateKey, error) {
|
|
|
|
return w.waku.RetrievePubsubTopicKey(topic)
|
|
|
|
}
|
|
|
|
|
2023-05-22 21:38:02 +00:00
|
|
|
func (w *gethWakuV2Wrapper) StorePubsubTopicKey(topic string, privKey *ecdsa.PrivateKey) error {
|
|
|
|
return w.waku.StorePubsubTopicKey(topic, privKey)
|
|
|
|
}
|
|
|
|
|
2024-01-30 17:56:59 +00:00
|
|
|
func (w *gethWakuV2Wrapper) RemovePubsubTopicKey(topic string) error {
|
|
|
|
return w.waku.RemovePubsubTopicKey(topic)
|
|
|
|
}
|
|
|
|
|
2024-08-16 18:24:21 +00:00
|
|
|
func (w *gethWakuV2Wrapper) AddStorePeer(address multiaddr.Multiaddr) (peer.ID, error) {
|
2021-08-30 14:57:28 +00:00
|
|
|
return w.waku.AddStorePeer(address)
|
|
|
|
}
|
|
|
|
|
2024-08-16 18:24:21 +00:00
|
|
|
func (w *gethWakuV2Wrapper) AddRelayPeer(address multiaddr.Multiaddr) (peer.ID, error) {
|
2021-08-30 14:57:28 +00:00
|
|
|
return w.waku.AddRelayPeer(address)
|
|
|
|
}
|
|
|
|
|
2024-08-16 18:24:21 +00:00
|
|
|
func (w *gethWakuV2Wrapper) Peers() types.PeerStats {
|
2021-08-30 14:57:28 +00:00
|
|
|
return w.waku.Peers()
|
|
|
|
}
|
|
|
|
|
2024-08-16 18:24:21 +00:00
|
|
|
func (w *gethWakuV2Wrapper) DialPeer(address multiaddr.Multiaddr) error {
|
2021-09-10 17:06:06 +00:00
|
|
|
return w.waku.DialPeer(address)
|
|
|
|
}
|
|
|
|
|
2024-08-16 18:24:21 +00:00
|
|
|
func (w *gethWakuV2Wrapper) DialPeerByID(peerID peer.ID) error {
|
2021-09-10 17:06:06 +00:00
|
|
|
return w.waku.DialPeerByID(peerID)
|
|
|
|
}
|
|
|
|
|
2024-08-16 18:24:21 +00:00
|
|
|
func (w *gethWakuV2Wrapper) ListenAddresses() ([]multiaddr.Multiaddr, error) {
|
2024-08-27 13:14:36 +00:00
|
|
|
return w.waku.ListenAddresses()
|
2022-11-24 21:27:46 +00:00
|
|
|
}
|
|
|
|
|
2024-07-01 15:37:54 +00:00
|
|
|
func (w *gethWakuV2Wrapper) RelayPeersByTopic(topic string) (*types.PeerList, error) {
|
|
|
|
return w.waku.RelayPeersByTopic(topic)
|
|
|
|
}
|
|
|
|
|
2024-08-16 18:24:21 +00:00
|
|
|
func (w *gethWakuV2Wrapper) ENR() (*enode.Node, error) {
|
2024-06-18 19:48:49 +00:00
|
|
|
return w.waku.ENR()
|
|
|
|
}
|
|
|
|
|
2024-08-16 18:24:21 +00:00
|
|
|
func (w *gethWakuV2Wrapper) DropPeer(peerID peer.ID) error {
|
2021-08-30 14:57:28 +00:00
|
|
|
return w.waku.DropPeer(peerID)
|
|
|
|
}
|
|
|
|
|
2021-11-26 12:30:35 +00:00
|
|
|
func (w *gethWakuV2Wrapper) ProcessingP2PMessages() bool {
|
2021-12-01 15:15:18 +00:00
|
|
|
return w.waku.ProcessingP2PMessages()
|
2021-11-26 12:30:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (w *gethWakuV2Wrapper) MarkP2PMessageAsProcessed(hash common.Hash) {
|
2021-12-01 15:15:18 +00:00
|
|
|
w.waku.MarkP2PMessageAsProcessed(hash)
|
2021-11-26 12:30:35 +00:00
|
|
|
}
|
|
|
|
|
2022-01-12 16:02:01 +00:00
|
|
|
func (w *gethWakuV2Wrapper) SubscribeToConnStatusChanges() (*types.ConnStatusSubscription, error) {
|
|
|
|
return w.waku.SubscribeToConnStatusChanges(), nil
|
|
|
|
}
|
|
|
|
|
2024-08-21 17:45:32 +00:00
|
|
|
func (w *gethWakuV2Wrapper) SetCriteriaForMissingMessageVerification(peerID peer.ID, pubsubTopic string, contentTopics []types.TopicType) error {
|
|
|
|
var cTopics []string
|
|
|
|
for _, ct := range contentTopics {
|
2024-09-19 20:16:53 +00:00
|
|
|
cTopics = append(cTopics, wakucommon.BytesToTopic(ct.Bytes()).ContentTopic())
|
2024-08-21 17:45:32 +00:00
|
|
|
}
|
2024-06-26 18:25:44 +00:00
|
|
|
pubsubTopic = w.waku.GetPubsubTopic(pubsubTopic)
|
2024-08-21 17:45:32 +00:00
|
|
|
w.waku.SetTopicsToVerifyForMissingMessages(peerID, pubsubTopic, cTopics)
|
2024-06-26 18:25:44 +00:00
|
|
|
|
|
|
|
// No err can be be generated by this function. The function returns an error
|
|
|
|
// Just so there's compatibility with GethWakuWrapper from V1
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-12-09 16:16:21 +00:00
|
|
|
func (w *gethWakuV2Wrapper) ConnectionChanged(state connection.State) {
|
|
|
|
w.waku.ConnectionChanged(state)
|
|
|
|
}
|
|
|
|
|
2024-01-30 13:43:34 +00:00
|
|
|
func (w *gethWakuV2Wrapper) ClearEnvelopesCache() {
|
|
|
|
w.waku.ClearEnvelopesCache()
|
|
|
|
}
|
|
|
|
|
2021-06-16 20:19:45 +00:00
|
|
|
type wakuV2FilterWrapper struct {
|
|
|
|
filter *wakucommon.Filter
|
|
|
|
id string
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewWakuFilterWrapper returns an object that wraps Geth's Filter in a types interface
|
|
|
|
func NewWakuV2FilterWrapper(f *wakucommon.Filter, id string) types.Filter {
|
|
|
|
if f.Messages == nil {
|
|
|
|
panic("Messages should not be nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
return &wakuV2FilterWrapper{
|
|
|
|
filter: f,
|
|
|
|
id: id,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetWakuFilterFrom retrieves the underlying whisper Filter struct from a wrapped Filter interface
|
|
|
|
func GetWakuV2FilterFrom(f types.Filter) *wakucommon.Filter {
|
|
|
|
return f.(*wakuV2FilterWrapper).filter
|
|
|
|
}
|
|
|
|
|
|
|
|
// ID returns the filter ID
|
|
|
|
func (w *wakuV2FilterWrapper) ID() string {
|
|
|
|
return w.id
|
|
|
|
}
|
2024-06-11 07:45:01 +00:00
|
|
|
|
|
|
|
func (w *gethWakuV2Wrapper) ConfirmMessageDelivered(hashes []common.Hash) {
|
|
|
|
w.waku.ConfirmMessageDelivered(hashes)
|
|
|
|
}
|
|
|
|
|
2024-08-01 03:27:43 +00:00
|
|
|
func (w *gethWakuV2Wrapper) PeerID() peer.ID {
|
|
|
|
return w.waku.PeerID()
|
|
|
|
}
|
2024-09-05 14:25:26 +00:00
|
|
|
|
2024-09-19 20:16:53 +00:00
|
|
|
func (w *gethWakuV2Wrapper) GetActiveStorenode() peer.ID {
|
|
|
|
return w.waku.StorenodeCycle.GetActiveStorenode()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *gethWakuV2Wrapper) OnStorenodeAvailableOneShot() <-chan struct{} {
|
|
|
|
return w.waku.StorenodeCycle.StorenodeAvailableOneshotEmitter.Subscribe()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *gethWakuV2Wrapper) OnStorenodeChanged() <-chan peer.ID {
|
|
|
|
return w.waku.StorenodeCycle.StorenodeChangedEmitter.Subscribe()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *gethWakuV2Wrapper) OnStorenodeNotWorking() <-chan struct{} {
|
|
|
|
return w.waku.StorenodeCycle.StorenodeNotWorkingEmitter.Subscribe()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *gethWakuV2Wrapper) OnStorenodeAvailable() <-chan peer.ID {
|
|
|
|
return w.waku.StorenodeCycle.StorenodeAvailableEmitter.Subscribe()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *gethWakuV2Wrapper) WaitForAvailableStoreNode(timeout time.Duration) bool {
|
|
|
|
return w.waku.StorenodeCycle.WaitForAvailableStoreNode(context.TODO(), timeout)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *gethWakuV2Wrapper) SetStorenodeConfigProvider(c history.StorenodeConfigProvider) {
|
|
|
|
w.waku.StorenodeCycle.SetStorenodeConfigProvider(c)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *gethWakuV2Wrapper) ProcessMailserverBatch(
|
|
|
|
ctx context.Context,
|
|
|
|
batch types.MailserverBatch,
|
|
|
|
storenodeID peer.ID,
|
|
|
|
pageLimit uint64,
|
|
|
|
shouldProcessNextPage func(int) (bool, uint64),
|
|
|
|
processEnvelopes bool,
|
|
|
|
) error {
|
|
|
|
pubsubTopic := w.waku.GetPubsubTopic(batch.PubsubTopic)
|
|
|
|
contentTopics := []string{}
|
|
|
|
for _, topic := range batch.Topics {
|
|
|
|
contentTopics = append(contentTopics, wakucommon.BytesToTopic(topic.Bytes()).ContentTopic())
|
|
|
|
}
|
|
|
|
|
|
|
|
criteria := store.FilterCriteria{
|
|
|
|
TimeStart: proto.Int64(int64(batch.From) * int64(time.Second)),
|
|
|
|
TimeEnd: proto.Int64(int64(batch.To) * int64(time.Second)),
|
|
|
|
ContentFilter: protocol.NewContentFilter(pubsubTopic, contentTopics...),
|
|
|
|
}
|
|
|
|
|
|
|
|
return w.waku.HistoryRetriever.Query(ctx, criteria, storenodeID, pageLimit, shouldProcessNextPage, processEnvelopes)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *gethWakuV2Wrapper) IsStorenodeAvailable(peerID peer.ID) bool {
|
|
|
|
return w.waku.StorenodeCycle.IsStorenodeAvailable(peerID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *gethWakuV2Wrapper) PerformStorenodeTask(fn func() error, opts ...history.StorenodeTaskOption) error {
|
|
|
|
return w.waku.StorenodeCycle.PerformStorenodeTask(fn, opts...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *gethWakuV2Wrapper) DisconnectActiveStorenode(ctx context.Context, backoff time.Duration, shouldCycle bool) {
|
|
|
|
w.waku.StorenodeCycle.Lock()
|
|
|
|
defer w.waku.StorenodeCycle.Unlock()
|
|
|
|
|
|
|
|
w.waku.StorenodeCycle.DisconnectActiveStorenode(backoff)
|
|
|
|
if shouldCycle {
|
|
|
|
w.waku.StorenodeCycle.Cycle(ctx)
|
|
|
|
}
|
2024-09-05 14:25:26 +00:00
|
|
|
}
|