2023-09-12 19:14:08 +00:00
|
|
|
//go:build !gowaku_no_rln
|
|
|
|
// +build !gowaku_no_rln
|
2022-08-12 12:44:13 +00:00
|
|
|
|
|
|
|
package node
|
|
|
|
|
|
|
|
import (
|
2023-04-03 21:45:19 +00:00
|
|
|
"bytes"
|
2023-01-06 22:37:57 +00:00
|
|
|
"context"
|
2022-08-12 12:44:13 +00:00
|
|
|
"errors"
|
2023-08-18 13:59:37 +00:00
|
|
|
|
2023-08-21 20:54:13 +00:00
|
|
|
pubsub "github.com/libp2p/go-libp2p-pubsub"
|
2022-11-09 19:53:01 +00:00
|
|
|
"github.com/waku-org/go-waku/waku/v2/protocol/rln"
|
2023-09-07 16:23:48 +00:00
|
|
|
"github.com/waku-org/go-waku/waku/v2/protocol/rln/group_manager"
|
2023-04-04 21:02:12 +00:00
|
|
|
"github.com/waku-org/go-waku/waku/v2/protocol/rln/group_manager/dynamic"
|
2023-04-03 21:45:19 +00:00
|
|
|
"github.com/waku-org/go-waku/waku/v2/protocol/rln/group_manager/static"
|
2023-08-23 20:50:25 +00:00
|
|
|
"github.com/waku-org/go-waku/waku/v2/protocol/rln/keystore"
|
2022-10-27 15:23:20 +00:00
|
|
|
r "github.com/waku-org/go-zerokit-rln/rln"
|
2022-08-12 12:44:13 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// RLNRelay is used to access any operation related to Waku RLN protocol
|
|
|
|
func (w *WakuNode) RLNRelay() RLNRelay {
|
|
|
|
return w.rlnRelay
|
|
|
|
}
|
|
|
|
|
2023-08-21 20:54:13 +00:00
|
|
|
func (w *WakuNode) setupRLNRelay() error {
|
2023-04-04 21:02:12 +00:00
|
|
|
var err error
|
|
|
|
|
2023-08-24 14:25:17 +00:00
|
|
|
if !w.opts.enableRLN {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-09-07 16:23:48 +00:00
|
|
|
var groupManager group_manager.GroupManager
|
|
|
|
|
|
|
|
rlnInstance, rootTracker, err := rln.GetRLNInstanceAndRootTracker(w.opts.rlnTreePath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-08-12 12:44:13 +00:00
|
|
|
if !w.opts.rlnRelayDynamic {
|
|
|
|
w.log.Info("setting up waku-rln-relay in off-chain mode")
|
2023-04-04 21:02:12 +00:00
|
|
|
|
2023-09-04 21:44:41 +00:00
|
|
|
index := uint(0)
|
|
|
|
if w.opts.rlnRelayMemIndex != nil {
|
|
|
|
index = *w.opts.rlnRelayMemIndex
|
|
|
|
}
|
|
|
|
|
2022-08-12 12:44:13 +00:00
|
|
|
// set up rln relay inputs
|
2023-09-04 21:44:41 +00:00
|
|
|
groupKeys, idCredential, err := static.Setup(index)
|
2022-08-12 12:44:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-09-04 21:44:41 +00:00
|
|
|
groupManager, err = static.NewStaticGroupManager(groupKeys, idCredential, index, rlnInstance, rootTracker, w.log)
|
2023-04-03 21:45:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-04-04 21:02:12 +00:00
|
|
|
} else {
|
|
|
|
w.log.Info("setting up waku-rln-relay in on-chain mode")
|
2023-04-03 21:45:19 +00:00
|
|
|
|
2023-08-23 20:50:25 +00:00
|
|
|
appKeystore, err := keystore.New(w.opts.keystorePath, dynamic.RLNAppInfo, w.log)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-04-04 21:02:12 +00:00
|
|
|
groupManager, err = dynamic.NewDynamicGroupManager(
|
|
|
|
w.opts.rlnETHClientAddress,
|
|
|
|
w.opts.rlnMembershipContractAddress,
|
2023-07-27 19:51:31 +00:00
|
|
|
w.opts.rlnRelayMemIndex,
|
2023-08-23 20:50:25 +00:00
|
|
|
appKeystore,
|
2023-04-05 19:44:46 +00:00
|
|
|
w.opts.keystorePassword,
|
2023-08-22 19:30:04 +00:00
|
|
|
w.opts.prometheusReg,
|
2023-09-07 16:23:48 +00:00
|
|
|
rlnInstance,
|
|
|
|
rootTracker,
|
2023-04-04 21:02:12 +00:00
|
|
|
w.log,
|
|
|
|
)
|
2022-08-12 12:44:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-04-04 21:02:12 +00:00
|
|
|
}
|
|
|
|
|
2023-09-07 16:23:48 +00:00
|
|
|
rlnRelay := rln.New(group_manager.Details{
|
|
|
|
GroupManager: groupManager,
|
|
|
|
RootTracker: rootTracker,
|
|
|
|
RLN: rlnInstance,
|
|
|
|
}, w.timesource, w.opts.prometheusReg, w.log)
|
2022-08-12 12:44:13 +00:00
|
|
|
|
2023-08-21 20:54:13 +00:00
|
|
|
w.rlnRelay = rlnRelay
|
|
|
|
|
|
|
|
// Adding RLN as a default validator
|
|
|
|
w.opts.pubsubOpts = append(w.opts.pubsubOpts, pubsub.WithDefaultValidator(rlnRelay.Validator(w.opts.rlnSpamHandler)))
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *WakuNode) startRlnRelay(ctx context.Context) error {
|
|
|
|
rlnRelay := w.rlnRelay.(*rln.WakuRLNRelay)
|
|
|
|
|
|
|
|
err := rlnRelay.Start(ctx)
|
2023-04-04 21:02:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-08-12 12:44:13 +00:00
|
|
|
|
2023-04-04 21:02:12 +00:00
|
|
|
if !w.opts.rlnRelayDynamic {
|
2022-08-12 12:44:13 +00:00
|
|
|
// check the correct construction of the tree by comparing the calculated root against the expected root
|
|
|
|
// no error should happen as it is already captured in the unit tests
|
|
|
|
root, err := rlnRelay.RLN.GetMerkleRoot()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-04-04 15:44:28 +00:00
|
|
|
expectedRoot, err := r.ToBytes32LE(r.STATIC_GROUP_MERKLE_ROOT)
|
2023-04-03 21:45:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !bytes.Equal(expectedRoot[:], root[:]) {
|
2022-08-12 12:44:13 +00:00
|
|
|
return errors.New("root mismatch: something went wrong not in Merkle tree construction")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-21 20:54:13 +00:00
|
|
|
w.log.Info("mounted waku RLN relay")
|
2022-08-12 12:44:13 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2022-08-18 16:27:10 +00:00
|
|
|
|
|
|
|
func (w *WakuNode) stopRlnRelay() error {
|
2022-08-27 02:01:26 +00:00
|
|
|
if w.rlnRelay != nil {
|
2023-08-18 13:59:37 +00:00
|
|
|
return w.rlnRelay.Stop()
|
2022-08-27 02:01:26 +00:00
|
|
|
}
|
2022-08-18 16:27:10 +00:00
|
|
|
return nil
|
|
|
|
}
|