2024-02-12 12:53:15 +00:00
|
|
|
package protocol
|
|
|
|
|
|
|
|
import (
|
2024-07-11 18:36:34 +00:00
|
|
|
"crypto/ecdsa"
|
2024-02-12 12:53:15 +00:00
|
|
|
"database/sql"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/suite"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
|
2024-07-11 18:36:34 +00:00
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
|
|
|
|
2024-02-12 12:53:15 +00:00
|
|
|
"github.com/status-im/status-go/appdatabase"
|
|
|
|
gethbridge "github.com/status-im/status-go/eth-node/bridge/geth"
|
|
|
|
"github.com/status-im/status-go/eth-node/types"
|
|
|
|
"github.com/status-im/status-go/protocol/common/shard"
|
|
|
|
"github.com/status-im/status-go/t/helpers"
|
|
|
|
waku2 "github.com/status-im/status-go/wakuv2"
|
|
|
|
)
|
|
|
|
|
|
|
|
type testWakuV2Config struct {
|
2024-07-15 15:25:12 +00:00
|
|
|
logger *zap.Logger
|
|
|
|
enableStore bool
|
|
|
|
clusterID uint16
|
|
|
|
nodekey []byte
|
2024-02-12 12:53:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewTestWakuV2(s *suite.Suite, cfg testWakuV2Config) *waku2.Waku {
|
|
|
|
wakuConfig := &waku2.Config{
|
2024-03-20 18:10:02 +00:00
|
|
|
ClusterID: cfg.clusterID,
|
|
|
|
LightClient: false,
|
|
|
|
EnablePeerExchangeServer: true,
|
|
|
|
EnablePeerExchangeClient: false,
|
2024-05-07 14:06:45 +00:00
|
|
|
EnableDiscV5: false,
|
2024-02-12 12:53:15 +00:00
|
|
|
}
|
|
|
|
|
2024-07-11 18:36:34 +00:00
|
|
|
var nodeKey *ecdsa.PrivateKey
|
|
|
|
if len(cfg.nodekey) != 0 {
|
|
|
|
nodeKey, _ = crypto.ToECDSA(cfg.nodekey)
|
|
|
|
}
|
|
|
|
|
2024-02-12 12:53:15 +00:00
|
|
|
var db *sql.DB
|
2024-07-15 15:25:12 +00:00
|
|
|
db, err := helpers.SetupTestMemorySQLDB(appdatabase.DbInitializer{})
|
|
|
|
s.Require().NoError(err)
|
2024-02-12 12:53:15 +00:00
|
|
|
|
|
|
|
if cfg.enableStore {
|
|
|
|
wakuConfig.EnableStore = true
|
|
|
|
wakuConfig.StoreCapacity = 200
|
|
|
|
wakuConfig.StoreSeconds = 200
|
|
|
|
}
|
|
|
|
|
|
|
|
wakuNode, err := waku2.New(
|
2024-07-11 18:36:34 +00:00
|
|
|
nodeKey,
|
2024-02-12 12:53:15 +00:00
|
|
|
"",
|
|
|
|
wakuConfig,
|
|
|
|
cfg.logger,
|
|
|
|
db,
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
nil)
|
|
|
|
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
err = wakuNode.Start()
|
2024-07-15 15:25:12 +00:00
|
|
|
if cfg.enableStore {
|
|
|
|
err := wakuNode.SubscribeToPubsubTopic(shard.DefaultNonProtectedPubsubTopic(), nil)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
}
|
2024-02-12 12:53:15 +00:00
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
return wakuNode
|
|
|
|
}
|
|
|
|
|
2024-07-15 15:25:12 +00:00
|
|
|
func CreateWakuV2Network(s *suite.Suite, parentLogger *zap.Logger, nodeNames []string) []types.Waku {
|
2024-02-12 12:53:15 +00:00
|
|
|
nodes := make([]*waku2.Waku, len(nodeNames))
|
|
|
|
wrappers := make([]types.Waku, len(nodes))
|
|
|
|
|
|
|
|
for i, name := range nodeNames {
|
|
|
|
nodes[i] = NewTestWakuV2(s, testWakuV2Config{
|
2024-07-15 15:25:12 +00:00
|
|
|
logger: parentLogger.Named("waku-" + name),
|
|
|
|
enableStore: false,
|
|
|
|
clusterID: shard.MainStatusShardCluster,
|
2024-02-12 12:53:15 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Setup local network graph
|
|
|
|
for i := 0; i < len(nodes); i++ {
|
|
|
|
for j := 0; j < len(nodes); j++ {
|
|
|
|
if i == j {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
addrs := nodes[j].ListenAddresses()
|
|
|
|
s.Require().Greater(len(addrs), 0)
|
|
|
|
_, err := nodes[i].AddRelayPeer(addrs[0])
|
|
|
|
s.Require().NoError(err)
|
|
|
|
err = nodes[i].DialPeer(addrs[0])
|
|
|
|
s.Require().NoError(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for i, n := range nodes {
|
|
|
|
wrappers[i] = gethbridge.NewGethWakuV2Wrapper(n)
|
|
|
|
}
|
|
|
|
return wrappers
|
|
|
|
}
|