go-waku/waku/v2/node/wakunode2_rln.go

125 lines
3.1 KiB
Go
Raw Normal View History

2022-08-12 12:44:13 +00:00
//go:build gowaku_rln
// +build gowaku_rln
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"
"github.com/waku-org/go-waku/waku/v2/protocol/rln"
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"
r "github.com/waku-org/go-zerokit-rln/rln"
2022-08-12 12:44:13 +00:00
"go.uber.org/zap"
)
// RLNRelay is used to access any operation related to Waku RLN protocol
func (w *WakuNode) RLNRelay() RLNRelay {
return w.rlnRelay
}
2023-01-06 22:37:57 +00:00
func (w *WakuNode) mountRlnRelay(ctx context.Context) error {
2022-08-12 12:44:13 +00:00
// check whether inputs are provided
// relay protocol is the prerequisite of rln-relay
if w.Relay() == nil {
return errors.New("relay protocol is required")
}
// check whether the pubsub topic is supported at the relay level
topicFound := false
for _, t := range w.Relay().Topics() {
if t == w.opts.rlnRelayPubsubTopic {
topicFound = true
break
}
}
if !topicFound {
return errors.New("relay protocol does not support the configured pubsub topic")
}
2023-04-04 21:02:12 +00:00
var err error
var groupManager rln.GroupManager
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
2022-08-12 12:44:13 +00:00
// set up rln relay inputs
2023-04-03 21:45:19 +00:00
groupKeys, idCredential, err := static.Setup(w.opts.rlnRelayMemIndex)
2022-08-12 12:44:13 +00:00
if err != nil {
return err
}
2023-04-04 21:02:12 +00:00
groupManager, err = static.NewStaticGroupManager(groupKeys, idCredential, w.opts.rlnRelayMemIndex, 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-04-04 21:02:12 +00:00
// check if the peer has provided its rln credentials
var memKeyPair *r.IdentityCredential
if w.opts.rlnRelayIDCommitment != nil && w.opts.rlnRelayIDKey != nil {
memKeyPair = &r.IdentityCredential{
IDCommitment: *w.opts.rlnRelayIDCommitment,
IDSecretHash: *w.opts.rlnRelayIDKey,
}
2023-04-03 21:45:19 +00:00
}
2023-04-04 21:02:12 +00:00
groupManager, err = dynamic.NewDynamicGroupManager(
w.opts.rlnETHClientAddress,
w.opts.rlnETHPrivateKey,
w.opts.rlnMembershipContractAddress,
memKeyPair,
w.opts.rlnRelayMemIndex,
w.opts.rlnRegistrationHandler,
w.log,
)
2022-08-12 12:44:13 +00:00
if err != nil {
return err
}
2023-04-04 21:02:12 +00:00
}
rlnRelay, err := rln.New(w.Relay(), groupManager, w.opts.rlnRelayPubsubTopic, w.opts.rlnRelayContentTopic, w.opts.rlnSpamHandler, w.timesource, w.log)
if err != nil {
return err
}
2022-08-12 12:44:13 +00:00
2023-04-04 21:02:12 +00:00
err = rlnRelay.Start(ctx)
if err != nil {
return err
}
2022-08-12 12:44:13 +00:00
2023-04-04 21:02:12 +00:00
w.rlnRelay = rlnRelay
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
}
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")
}
}
w.log.Info("mounted waku RLN relay", zap.String("pubsubTopic", w.opts.rlnRelayPubsubTopic), zap.String("contentTopic", w.opts.rlnRelayContentTopic))
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 {
w.rlnRelay.Stop()
}
2022-08-18 16:27:10 +00:00
return nil
}