2021-11-12 09:19:42 +00:00
|
|
|
package node
|
|
|
|
|
|
|
|
import (
|
2024-02-08 09:54:58 +00:00
|
|
|
"net"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2024-01-21 03:55:27 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2024-02-08 09:54:58 +00:00
|
|
|
"github.com/waku-org/go-waku/waku/v2/peermanager"
|
2024-05-03 16:07:03 +00:00
|
|
|
"github.com/waku-org/go-waku/waku/v2/protocol/legacy_store"
|
2024-01-21 03:55:27 +00:00
|
|
|
"github.com/waku-org/go-waku/waku/v2/protocol/pb"
|
|
|
|
r "github.com/waku-org/go-zerokit-rln/rln"
|
|
|
|
"go.uber.org/zap"
|
2021-11-12 09:19:42 +00:00
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
|
|
|
"github.com/multiformats/go-multiaddr"
|
2023-08-16 01:40:00 +00:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2021-11-12 09:19:42 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2022-11-09 19:53:01 +00:00
|
|
|
"github.com/waku-org/go-waku/tests"
|
|
|
|
"github.com/waku-org/go-waku/waku/persistence"
|
2021-11-12 09:19:42 +00:00
|
|
|
)
|
|
|
|
|
2024-01-21 03:55:27 +00:00
|
|
|
func handleSpam(msg *pb.WakuMessage, topic string) error {
|
|
|
|
|
|
|
|
logger := new(zap.Logger)
|
|
|
|
|
|
|
|
logger.Log(zap.InfoLevel, "Spam has been detected!")
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-11-12 09:19:42 +00:00
|
|
|
func TestWakuOptions(t *testing.T) {
|
2024-02-08 09:54:58 +00:00
|
|
|
topicHealthStatusChan := make(chan peermanager.TopicHealthStatus, 100)
|
2021-11-12 09:19:42 +00:00
|
|
|
|
|
|
|
key, err := tests.RandomHex(32)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
prvKey, err := crypto.HexToECDSA(key)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
hostAddr, _ := net.ResolveTCPAddr("tcp", "0.0.0.0:0")
|
|
|
|
|
|
|
|
addr, err := multiaddr.NewMultiaddr("/ip4/0.0.0.0/tcp/4000/ws")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2024-05-03 16:07:03 +00:00
|
|
|
storeFactory := func(w *WakuNode) legacy_store.Store {
|
|
|
|
return legacy_store.NewWakuStore(w.opts.messageProvider, w.peermanager, w.timesource, prometheus.DefaultRegisterer, w.log)
|
2022-03-18 19:56:34 +00:00
|
|
|
}
|
|
|
|
|
2021-11-12 09:19:42 +00:00
|
|
|
options := []WakuNodeOption{
|
2021-11-17 16:19:42 +00:00
|
|
|
WithHostAddress(hostAddr),
|
2023-02-08 16:02:06 +00:00
|
|
|
WithAdvertiseAddresses(addr),
|
2023-05-15 18:44:36 +00:00
|
|
|
WithMultiaddress(addr),
|
2021-11-12 09:19:42 +00:00
|
|
|
WithPrivateKey(prvKey),
|
|
|
|
WithLibP2POptions(),
|
|
|
|
WithWakuRelay(),
|
2021-11-17 16:19:42 +00:00
|
|
|
WithDiscoveryV5(123, nil, false),
|
2023-01-03 15:17:25 +00:00
|
|
|
WithWakuStore(),
|
2022-05-30 18:48:22 +00:00
|
|
|
WithMessageProvider(&persistence.DBStore{}),
|
2021-11-12 09:19:42 +00:00
|
|
|
WithLightPush(),
|
|
|
|
WithKeepAlive(time.Hour),
|
2024-02-08 09:54:58 +00:00
|
|
|
WithTopicHealthStatusChannel(topicHealthStatusChan),
|
2022-03-18 19:56:34 +00:00
|
|
|
WithWakuStoreFactory(storeFactory),
|
2021-11-12 09:19:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
params := new(WakuNodeParameters)
|
|
|
|
|
|
|
|
for _, opt := range options {
|
|
|
|
require.NoError(t, opt(params))
|
|
|
|
}
|
|
|
|
|
|
|
|
require.NotNil(t, params.multiAddr)
|
|
|
|
require.NotNil(t, params.privKey)
|
2024-02-08 09:54:58 +00:00
|
|
|
require.NotNil(t, params.topicHealthNotifCh)
|
2021-11-12 09:19:42 +00:00
|
|
|
}
|
2024-01-21 03:55:27 +00:00
|
|
|
|
|
|
|
func TestWakuRLNOptions(t *testing.T) {
|
2024-02-08 09:54:58 +00:00
|
|
|
topicHealthStatusChan := make(chan peermanager.TopicHealthStatus, 100)
|
2024-01-21 03:55:27 +00:00
|
|
|
|
|
|
|
key, err := tests.RandomHex(32)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
prvKey, err := crypto.HexToECDSA(key)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
hostAddr, _ := net.ResolveTCPAddr("tcp", "0.0.0.0:0")
|
|
|
|
|
|
|
|
addr, err := multiaddr.NewMultiaddr("/ip4/0.0.0.0/tcp/4000/ws")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2024-05-03 16:07:03 +00:00
|
|
|
storeFactory := func(w *WakuNode) legacy_store.Store {
|
|
|
|
return legacy_store.NewWakuStore(w.opts.messageProvider, w.peermanager, w.timesource, prometheus.DefaultRegisterer, w.log)
|
2024-01-21 03:55:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
index := r.MembershipIndex(5)
|
|
|
|
|
|
|
|
// Test WithStaticRLNRelay
|
|
|
|
|
|
|
|
options := []WakuNodeOption{
|
|
|
|
WithHostAddress(hostAddr),
|
|
|
|
WithAdvertiseAddresses(addr),
|
|
|
|
WithMultiaddress(addr),
|
|
|
|
WithPrivateKey(prvKey),
|
|
|
|
WithLibP2POptions(),
|
|
|
|
WithWakuRelay(),
|
|
|
|
WithDiscoveryV5(123, nil, false),
|
|
|
|
WithWakuStore(),
|
|
|
|
WithMessageProvider(&persistence.DBStore{}),
|
|
|
|
WithLightPush(),
|
|
|
|
WithKeepAlive(time.Hour),
|
2024-02-08 09:54:58 +00:00
|
|
|
WithTopicHealthStatusChannel(topicHealthStatusChan),
|
2024-01-21 03:55:27 +00:00
|
|
|
WithWakuStoreFactory(storeFactory),
|
|
|
|
WithStaticRLNRelay(&index, handleSpam),
|
|
|
|
}
|
|
|
|
|
|
|
|
params := new(WakuNodeParameters)
|
|
|
|
|
|
|
|
for _, opt := range options {
|
|
|
|
require.NoError(t, opt(params))
|
|
|
|
}
|
|
|
|
|
|
|
|
require.True(t, params.enableRLN)
|
|
|
|
require.False(t, params.rlnRelayDynamic)
|
|
|
|
require.Equal(t, uint(5), *params.rlnRelayMemIndex)
|
|
|
|
require.NotNil(t, params.rlnSpamHandler)
|
|
|
|
|
|
|
|
// Test WithDynamicRLNRelay
|
|
|
|
|
|
|
|
var (
|
|
|
|
keystorePath = "./rlnKeystore.json"
|
|
|
|
keystorePassword = "password"
|
|
|
|
rlnTreePath = "root"
|
|
|
|
contractAddress = "0xF471d71E9b1455bBF4b85d475afb9BB0954A29c4"
|
|
|
|
ethClientAddress = "wss://sepolia.infura.io/ws/v3/API_KEY_GOES_HERE"
|
|
|
|
)
|
|
|
|
|
|
|
|
index = uint(0)
|
|
|
|
|
|
|
|
options2 := []WakuNodeOption{
|
|
|
|
WithHostAddress(hostAddr),
|
|
|
|
WithAdvertiseAddresses(addr),
|
|
|
|
WithMultiaddress(addr),
|
|
|
|
WithPrivateKey(prvKey),
|
|
|
|
WithLibP2POptions(),
|
|
|
|
WithWakuRelay(),
|
|
|
|
WithDiscoveryV5(123, nil, false),
|
|
|
|
WithWakuStore(),
|
|
|
|
WithMessageProvider(&persistence.DBStore{}),
|
|
|
|
WithLightPush(),
|
|
|
|
WithKeepAlive(time.Hour),
|
2024-02-08 09:54:58 +00:00
|
|
|
WithTopicHealthStatusChannel(topicHealthStatusChan),
|
2024-01-21 03:55:27 +00:00
|
|
|
WithWakuStoreFactory(storeFactory),
|
|
|
|
WithDynamicRLNRelay(keystorePath, keystorePassword, rlnTreePath, common.HexToAddress(contractAddress), &index, handleSpam, ethClientAddress),
|
|
|
|
}
|
|
|
|
|
|
|
|
params2 := new(WakuNodeParameters)
|
|
|
|
|
|
|
|
for _, opt := range options2 {
|
|
|
|
require.NoError(t, opt(params2))
|
|
|
|
}
|
|
|
|
|
|
|
|
require.True(t, params2.enableRLN)
|
|
|
|
require.True(t, params2.rlnRelayDynamic)
|
|
|
|
require.Equal(t, keystorePassword, params2.keystorePassword)
|
|
|
|
require.Equal(t, uint(0), *params2.rlnRelayMemIndex)
|
|
|
|
require.NotNil(t, params2.rlnSpamHandler)
|
|
|
|
require.Equal(t, ethClientAddress, params2.rlnETHClientAddress)
|
|
|
|
require.Equal(t, common.HexToAddress(contractAddress), params2.rlnMembershipContractAddress)
|
|
|
|
require.Equal(t, rlnTreePath, params2.rlnTreePath)
|
|
|
|
|
|
|
|
}
|