chore: fix peer exchange and unit testing (#4381)
* chore: peer exchange local test * chore: use local enr resolver * remove light client close peer. * fix: peer exchange not start when enabled * chore: remove ws config * uncomment light client * use resolver option * chore: use option param for dns resolver * chore: fix vendor changes. * chore: lint
This commit is contained in:
parent
3874e47840
commit
9be202be50
|
@ -19,6 +19,7 @@
|
||||||
package wakuv2
|
package wakuv2
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
ethdisc "github.com/ethereum/go-ethereum/p2p/dnsdisc"
|
||||||
"github.com/waku-org/go-waku/waku/v2/protocol/relay"
|
"github.com/waku-org/go-waku/waku/v2/protocol/relay"
|
||||||
|
|
||||||
"github.com/status-im/status-go/wakuv2/common"
|
"github.com/status-im/status-go/wakuv2/common"
|
||||||
|
@ -38,6 +39,7 @@ type Config struct {
|
||||||
Rendezvous bool `toml:",omitempty"`
|
Rendezvous bool `toml:",omitempty"`
|
||||||
DiscV5BootstrapNodes []string `toml:",omitempty"`
|
DiscV5BootstrapNodes []string `toml:",omitempty"`
|
||||||
Nameserver string `toml:",omitempty"`
|
Nameserver string `toml:",omitempty"`
|
||||||
|
Resolver ethdisc.Resolver `toml:",omitempty"`
|
||||||
EnableDiscV5 bool `toml:",omitempty"`
|
EnableDiscV5 bool `toml:",omitempty"`
|
||||||
DiscoveryLimit int `toml:",omitempty"`
|
DiscoveryLimit int `toml:",omitempty"`
|
||||||
AutoUpdate bool `toml:",omitempty"`
|
AutoUpdate bool `toml:",omitempty"`
|
||||||
|
|
|
@ -54,6 +54,7 @@ import (
|
||||||
pubsub "github.com/libp2p/go-libp2p-pubsub"
|
pubsub "github.com/libp2p/go-libp2p-pubsub"
|
||||||
"github.com/libp2p/go-libp2p/core/metrics"
|
"github.com/libp2p/go-libp2p/core/metrics"
|
||||||
|
|
||||||
|
ethdisc "github.com/ethereum/go-ethereum/p2p/dnsdisc"
|
||||||
"github.com/waku-org/go-waku/waku/v2/dnsdisc"
|
"github.com/waku-org/go-waku/waku/v2/dnsdisc"
|
||||||
wps "github.com/waku-org/go-waku/waku/v2/peerstore"
|
wps "github.com/waku-org/go-waku/waku/v2/peerstore"
|
||||||
"github.com/waku-org/go-waku/waku/v2/protocol"
|
"github.com/waku-org/go-waku/waku/v2/protocol"
|
||||||
|
@ -87,6 +88,7 @@ type settings struct {
|
||||||
PeerExchange bool // Enable peer exchange
|
PeerExchange bool // Enable peer exchange
|
||||||
DiscoveryLimit int // Indicates the number of nodes to discover
|
DiscoveryLimit int // Indicates the number of nodes to discover
|
||||||
Nameserver string // Optional nameserver to use for dns discovery
|
Nameserver string // Optional nameserver to use for dns discovery
|
||||||
|
Resolver ethdisc.Resolver // Optional resolver to use for dns discovery
|
||||||
EnableDiscV5 bool // Indicates whether discv5 is enabled or not
|
EnableDiscV5 bool // Indicates whether discv5 is enabled or not
|
||||||
DefaultPubsubTopic string // Pubsub topic to be used by default for messages that do not have a topic assigned (depending whether sharding is used or not)
|
DefaultPubsubTopic string // Pubsub topic to be used by default for messages that do not have a topic assigned (depending whether sharding is used or not)
|
||||||
Options []node.WakuNodeOption
|
Options []node.WakuNodeOption
|
||||||
|
@ -242,6 +244,7 @@ func New(nodeKey string, fleet string, cfg *Config, logger *zap.Logger, appDB *s
|
||||||
PeerExchange: cfg.PeerExchange,
|
PeerExchange: cfg.PeerExchange,
|
||||||
DiscoveryLimit: cfg.DiscoveryLimit,
|
DiscoveryLimit: cfg.DiscoveryLimit,
|
||||||
Nameserver: cfg.Nameserver,
|
Nameserver: cfg.Nameserver,
|
||||||
|
Resolver: cfg.Resolver,
|
||||||
EnableDiscV5: cfg.EnableDiscV5,
|
EnableDiscV5: cfg.EnableDiscV5,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -403,12 +406,16 @@ func (w *Waku) dnsDiscover(ctx context.Context, enrtreeAddress string, apply fnA
|
||||||
if !ok {
|
if !ok {
|
||||||
w.settingsMu.RLock()
|
w.settingsMu.RLock()
|
||||||
nameserver := w.settings.Nameserver
|
nameserver := w.settings.Nameserver
|
||||||
|
resolver := w.settings.Resolver
|
||||||
w.settingsMu.RUnlock()
|
w.settingsMu.RUnlock()
|
||||||
|
|
||||||
var opts []dnsdisc.DNSDiscoveryOption
|
var opts []dnsdisc.DNSDiscoveryOption
|
||||||
if nameserver != "" {
|
if nameserver != "" {
|
||||||
opts = append(opts, dnsdisc.WithNameserver(nameserver))
|
opts = append(opts, dnsdisc.WithNameserver(nameserver))
|
||||||
}
|
}
|
||||||
|
if resolver != nil {
|
||||||
|
opts = append(opts, dnsdisc.WithResolver(resolver))
|
||||||
|
}
|
||||||
|
|
||||||
discoveredNodes, err := dnsdisc.RetrieveNodes(ctx, enrtreeAddress, opts...)
|
discoveredNodes, err := dnsdisc.RetrieveNodes(ctx, enrtreeAddress, opts...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -590,11 +597,7 @@ func (w *Waku) runPeerExchangeLoop() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Obtaining peer ID
|
// Obtaining peer ID
|
||||||
peerIDString, err := discoveredNode.PeerInfo.Addrs[0].ValueForProtocol(multiaddr.P_P2P)
|
peerIDString := discoveredNode.PeerID.String()
|
||||||
if err != nil {
|
|
||||||
w.logger.Warn("multiaddress does not contain peerID", zap.String("multiaddr", discoveredNode.PeerInfo.Addrs[0].String()))
|
|
||||||
continue // No peer ID available somehow
|
|
||||||
}
|
|
||||||
|
|
||||||
peerID, err := peer.Decode(peerIDString)
|
peerID, err := peer.Decode(peerIDString)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -1184,6 +1187,13 @@ func (w *Waku) Start() error {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if w.cfg.PeerExchange {
|
||||||
|
err := w.node.PeerExchange().Start(w.ctx)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
w.wg.Add(2)
|
w.wg.Add(2)
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
|
|
|
@ -10,10 +10,15 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/cenkalti/backoff/v3"
|
"github.com/cenkalti/backoff/v3"
|
||||||
"github.com/golang/protobuf/proto"
|
|
||||||
"github.com/stretchr/testify/require"
|
|
||||||
|
|
||||||
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
||||||
|
"github.com/ethereum/go-ethereum/crypto"
|
||||||
|
ethdnsdisc "github.com/ethereum/go-ethereum/p2p/dnsdisc"
|
||||||
|
"github.com/ethereum/go-ethereum/p2p/enode"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
"golang.org/x/exp/maps"
|
"golang.org/x/exp/maps"
|
||||||
|
"google.golang.org/protobuf/proto"
|
||||||
|
|
||||||
"github.com/waku-org/go-waku/waku/v2/dnsdisc"
|
"github.com/waku-org/go-waku/waku/v2/dnsdisc"
|
||||||
"github.com/waku-org/go-waku/waku/v2/protocol/pb"
|
"github.com/waku-org/go-waku/waku/v2/protocol/pb"
|
||||||
|
@ -192,6 +197,82 @@ func TestBasicWakuV2(t *testing.T) {
|
||||||
require.NoError(t, w.Stop())
|
require.NoError(t, w.Stop())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type mapResolver map[string]string
|
||||||
|
|
||||||
|
func (mr mapResolver) LookupTXT(ctx context.Context, name string) ([]string, error) {
|
||||||
|
if record, ok := mr[name]; ok {
|
||||||
|
return []string{record}, nil
|
||||||
|
}
|
||||||
|
return nil, errors.New("not found")
|
||||||
|
}
|
||||||
|
|
||||||
|
var signingKeyForTesting, _ = crypto.ToECDSA(hexutil.MustDecode("0xdc599867fc513f8f5e2c2c9c489cde5e71362d1d9ec6e693e0de063236ed1240"))
|
||||||
|
|
||||||
|
func makeTestTree(domain string, nodes []*enode.Node, links []string) (*ethdnsdisc.Tree, string) {
|
||||||
|
tree, err := ethdnsdisc.MakeTree(1, nodes, links)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
url, err := tree.Sign(signingKeyForTesting, domain)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
return tree, url
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPeerExchange(t *testing.T) {
|
||||||
|
// start node which serve as PeerExchange server
|
||||||
|
config := &Config{}
|
||||||
|
config.EnableDiscV5 = true
|
||||||
|
config.PeerExchange = true
|
||||||
|
pxServerNode, err := New("", "", config, nil, nil, nil, nil, nil)
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.NoError(t, pxServerNode.Start())
|
||||||
|
|
||||||
|
time.Sleep(1 * time.Second)
|
||||||
|
|
||||||
|
// start node that will be discovered by PeerExchange
|
||||||
|
config = &Config{}
|
||||||
|
config.EnableDiscV5 = true
|
||||||
|
config.DiscV5BootstrapNodes = []string{pxServerNode.node.ENR().String()}
|
||||||
|
node, err := New("", "", config, nil, nil, nil, nil, nil)
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.NoError(t, node.Start())
|
||||||
|
|
||||||
|
time.Sleep(1 * time.Second)
|
||||||
|
|
||||||
|
// start light node which use PeerExchange to discover peers
|
||||||
|
enrNodes := []*enode.Node{pxServerNode.node.ENR()}
|
||||||
|
tree, url := makeTestTree("n", enrNodes, nil)
|
||||||
|
resolver := mapResolver(tree.ToTXT("n"))
|
||||||
|
|
||||||
|
config = &Config{}
|
||||||
|
config.PeerExchange = true
|
||||||
|
config.LightClient = true
|
||||||
|
config.Resolver = resolver
|
||||||
|
|
||||||
|
config.WakuNodes = []string{url}
|
||||||
|
lightNode, err := New("", "", config, nil, nil, nil, nil, nil)
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.NoError(t, lightNode.Start())
|
||||||
|
|
||||||
|
// Sanity check, not great, but it's probably helpful
|
||||||
|
options := func(b *backoff.ExponentialBackOff) {
|
||||||
|
b.MaxElapsedTime = 30 * time.Second
|
||||||
|
}
|
||||||
|
err = tt.RetryWithBackOff(func() error {
|
||||||
|
if len(lightNode.Peers()) == 2 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return errors.New("no peers discovered")
|
||||||
|
}, options)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
require.NoError(t, lightNode.Stop())
|
||||||
|
require.NoError(t, pxServerNode.Stop())
|
||||||
|
require.NoError(t, node.Stop())
|
||||||
|
}
|
||||||
|
|
||||||
func TestWakuV2Filter(t *testing.T) {
|
func TestWakuV2Filter(t *testing.T) {
|
||||||
enrTreeAddress := testENRBootstrap
|
enrTreeAddress := testENRBootstrap
|
||||||
envEnrTreeAddress := os.Getenv("ENRTREE_ADDRESS")
|
envEnrTreeAddress := os.Getenv("ENRTREE_ADDRESS")
|
||||||
|
|
Loading…
Reference in New Issue