Skip to content

Commit

Permalink
chore(rln-relay): use the only key from keystore if only 1 exists
Browse files Browse the repository at this point in the history
  • Loading branch information
richard-ramos committed Sep 5, 2023
1 parent e66f0e3 commit adacf87
Show file tree
Hide file tree
Showing 13 changed files with 104 additions and 44 deletions.
11 changes: 6 additions & 5 deletions cmd/waku/flags_rln.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ func rlnFlags() []cli.Flag {
Usage: "Enable spam protection through rln-relay",
Destination: &options.RLNRelay.Enable,
},
&cli.UintFlag{
Name: "rln-relay-membership-index",
Value: 0,
Usage: "the index of credentials to use",
Destination: &options.RLNRelay.MembershipIndex,
&cli.GenericFlag{
Name: "rln-relay-cred-index",
Usage: "the index of the onchain commitment to use",
Value: &wcli.OptionalUint{
Value: &options.RLNRelay.MembershipIndex,
},
},
&cli.BoolFlag{
Name: "rln-relay-dynamic",
Expand Down
4 changes: 2 additions & 2 deletions cmd/waku/node_rln.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func checkForRLN(logger *zap.Logger, options NodeOptions, nodeOpts *[]node.WakuN
failOnErr(errors.New("relay not available"), "Could not enable RLN Relay")
}
if !options.RLNRelay.Dynamic {
*nodeOpts = append(*nodeOpts, node.WithStaticRLNRelay(rln.MembershipIndex(options.RLNRelay.MembershipIndex), nil))
*nodeOpts = append(*nodeOpts, node.WithStaticRLNRelay((*rln.MembershipIndex)(options.RLNRelay.MembershipIndex), nil))
} else {
// TODO: too many parameters in this function
// consider passing a config struct instead
Expand All @@ -26,7 +26,7 @@ func checkForRLN(logger *zap.Logger, options NodeOptions, nodeOpts *[]node.WakuN
options.RLNRelay.CredentialsPassword,
options.RLNRelay.TreePath,
options.RLNRelay.MembershipContractAddress,
rln.MembershipIndex(options.RLNRelay.MembershipIndex),
options.RLNRelay.MembershipIndex,
nil,
options.RLNRelay.ETHClientAddress,
))
Expand Down
2 changes: 1 addition & 1 deletion cmd/waku/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ type RLNRelayOptions struct {
CredentialsPath string
CredentialsPassword string
TreePath string
MembershipIndex uint
MembershipIndex *uint
Dynamic bool
ETHClientAddress string
MembershipContractAddress common.Address
Expand Down
4 changes: 2 additions & 2 deletions examples/chat2/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,13 @@ func execute(options Options) {
options.RLNRelay.CredentialsPassword,
"", // Will use default tree path
options.RLNRelay.MembershipContractAddress,
uint(options.RLNRelay.MembershipIndex),
options.RLNRelay.MembershipIndex,
spamHandler,
options.RLNRelay.ETHClientAddress,
))
} else {
opts = append(opts, node.WithStaticRLNRelay(
uint(options.RLNRelay.MembershipIndex),
options.RLNRelay.MembershipIndex,
spamHandler))
}
}
Expand Down
14 changes: 7 additions & 7 deletions examples/chat2/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ func getFlags() []cli.Flag {

testCT, err := protocol.NewContentTopic("toy-chat", 3, "mingde", "proto")
if err != nil {
fmt.Println("Invalid contentTopic")
return nil
panic("invalid contentTopic")
}
testnetContentTopic := testCT.String()

Expand Down Expand Up @@ -190,11 +189,12 @@ func getFlags() []cli.Flag {
Usage: "Enable spam protection through rln-relay",
Destination: &options.RLNRelay.Enable,
},
&cli.UintFlag{
Name: "rln-relay-membership-index",
Value: 0,
Usage: "the index of credentials to use",
Destination: &options.RLNRelay.MembershipIndex,
&cli.GenericFlag{
Name: "rln-relay-cred-index",
Usage: "the index of the onchain commitment to use",
Value: &wcli.OptionalUint{
Value: &options.RLNRelay.MembershipIndex,
},
},
&cli.BoolFlag{
Name: "rln-relay-dynamic",
Expand Down
2 changes: 1 addition & 1 deletion examples/chat2/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type RLNRelayOptions struct {
Enable bool
CredentialsPath string
CredentialsPassword string
MembershipIndex uint
MembershipIndex *uint
Dynamic bool
ETHClientAddress string
MembershipContractAddress common.Address
Expand Down
31 changes: 31 additions & 0 deletions waku/cliutils/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"crypto/ecdsa"
"errors"
"fmt"
"strconv"
"strings"

"github.com/ethereum/go-ethereum/common"
Expand Down Expand Up @@ -73,3 +74,33 @@ func (v *ChoiceValue) String() string {
}
return *v.Value
}

// OptionalUint represents a urfave/cli flag to store uint values that can be
// optionally set and not have any default value assigned to it
type OptionalUint struct {
Value **uint
}

// Set assigns a value to the flag only if it represents a valid uint value
func (v *OptionalUint) Set(value string) error {
if value != "" {
uintVal, err := strconv.ParseUint(value, 10, 0)
if err != nil {
return err
}
uVal := uint(uintVal)
*v.Value = &uVal
} else {
v.Value = nil
}

return nil
}

// String returns the string representation of the OptionalUint flag, if set
func (v *OptionalUint) String() string {
if v.Value == nil {
return ""
}
return fmt.Sprintf("%d", *v.Value)
}
9 changes: 7 additions & 2 deletions waku/v2/node/wakunode2_rln.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,18 @@ func (w *WakuNode) setupRLNRelay() error {
if !w.opts.rlnRelayDynamic {
w.log.Info("setting up waku-rln-relay in off-chain mode")

index := uint(0)
if w.opts.rlnRelayMemIndex != nil {
index = *w.opts.rlnRelayMemIndex
}

// set up rln relay inputs
groupKeys, idCredential, err := static.Setup(w.opts.rlnRelayMemIndex)
groupKeys, idCredential, err := static.Setup(index)
if err != nil {
return err
}

groupManager, err = static.NewStaticGroupManager(groupKeys, idCredential, w.opts.rlnRelayMemIndex, w.log)
groupManager, err = static.NewStaticGroupManager(groupKeys, idCredential, index, w.log)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion waku/v2/node/wakuoptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ type WakuNodeParameters struct {
enablePeerExchange bool

enableRLN bool
rlnRelayMemIndex uint
rlnRelayMemIndex *uint
rlnRelayDynamic bool
rlnSpamHandler func(message *pb.WakuMessage) error
rlnETHClientAddress string
Expand Down
4 changes: 2 additions & 2 deletions waku/v2/node/wakuoptions_rln.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

// WithStaticRLNRelay enables the Waku V2 RLN protocol in offchain mode
// Requires the `gowaku_rln` build constrain (or the env variable RLN=true if building go-waku)
func WithStaticRLNRelay(memberIndex r.MembershipIndex, spamHandler rln.SpamHandler) WakuNodeOption {
func WithStaticRLNRelay(memberIndex *r.MembershipIndex, spamHandler rln.SpamHandler) WakuNodeOption {
return func(params *WakuNodeParameters) error {
params.enableRLN = true
params.rlnRelayDynamic = false
Expand All @@ -23,7 +23,7 @@ func WithStaticRLNRelay(memberIndex r.MembershipIndex, spamHandler rln.SpamHandl

// WithDynamicRLNRelay enables the Waku V2 RLN protocol in onchain mode.
// Requires the `gowaku_rln` build constrain (or the env variable RLN=true if building go-waku)
func WithDynamicRLNRelay(keystorePath string, keystorePassword string, treePath string, membershipContract common.Address, membershipIndex uint, spamHandler rln.SpamHandler, ethClientAddress string) WakuNodeOption {
func WithDynamicRLNRelay(keystorePath string, keystorePassword string, treePath string, membershipContract common.Address, membershipIndex *uint, spamHandler rln.SpamHandler, ethClientAddress string) WakuNodeOption {
return func(params *WakuNodeParameters) error {
params.enableRLN = true
params.rlnRelayDynamic = true
Expand Down
24 changes: 13 additions & 11 deletions waku/v2/protocol/rln/group_manager/dynamic/dynamic.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,9 @@ type DynamicGroupManager struct {

eventHandler RegistrationEventHandler

appKeystore *keystore.AppKeystore
keystorePassword string
appKeystore *keystore.AppKeystore
keystorePassword string
membershipIndexToLoad *uint

rootTracker *group_manager.MerkleRootTracker
}
Expand Down Expand Up @@ -112,7 +113,7 @@ type RegistrationHandler = func(tx *types.Transaction)
func NewDynamicGroupManager(
ethClientAddr string,
memContractAddr common.Address,
membershipIndex uint,
membershipIndexToLoad *uint,
appKeystore *keystore.AppKeystore,
keystorePassword string,
reg prometheus.Registerer,
Expand All @@ -121,13 +122,13 @@ func NewDynamicGroupManager(
log = log.Named("rln-dynamic")

return &DynamicGroupManager{
membershipIndex: membershipIndex,
web3Config: web3.NewConfig(ethClientAddr, memContractAddr),
eventHandler: handler,
appKeystore: appKeystore,
keystorePassword: keystorePassword,
log: log,
metrics: newMetrics(reg),
membershipIndexToLoad: membershipIndexToLoad,
web3Config: web3.NewConfig(ethClientAddr, memContractAddr),
eventHandler: handler,
appKeystore: appKeystore,
keystorePassword: keystorePassword,
log: log,
metrics: newMetrics(reg),
}, nil
}

Expand Down Expand Up @@ -176,7 +177,7 @@ func (gm *DynamicGroupManager) loadCredential() error {

credentials, err := gm.appKeystore.GetMembershipCredentials(
gm.keystorePassword,
gm.membershipIndex,
gm.membershipIndexToLoad,
keystore.NewMembershipContractInfo(gm.web3Config.ChainID, gm.web3Config.RegistryContract.Address))
if err != nil {
return err
Expand All @@ -188,6 +189,7 @@ func (gm *DynamicGroupManager) loadCredential() error {
}

gm.identityCredential = credentials.IdentityCredential
gm.membershipIndex = credentials.TreeIndex

return nil
}
Expand Down
33 changes: 27 additions & 6 deletions waku/v2/protocol/rln/keystore/keystore.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,31 @@ func getKey(treeIndex rln.MembershipIndex, filterMembershipContract MembershipCo
}

// GetMembershipCredentials decrypts and retrieves membership credentials from the keystore applying filters
func (k *AppKeystore) GetMembershipCredentials(keystorePassword string, treeIndex rln.MembershipIndex, filterMembershipContract MembershipContractInfo) (*MembershipCredentials, error) {
key, err := getKey(treeIndex, filterMembershipContract)
if err != nil {
return nil, err
func (k *AppKeystore) GetMembershipCredentials(keystorePassword string, index *rln.MembershipIndex, filterMembershipContract MembershipContractInfo) (*MembershipCredentials, error) {
// If there is only one, and index to laod nil, assume 0,
// if there is more than one, complain if the index to load is nil

var key Key
var err error
if len(k.Credentials) == 1 {
// Only one credential, the tree index does not matter.
k.logger.Warn("automatically loading the only credential found on the keystore")
for k := range k.Credentials {
key = k // Obtain the first c
break
}
} else {
treeIndex := uint(0)
if index != nil {
treeIndex = *index
} else {
return nil, errors.New("the index of the onchain commitment to use was not specified")
}

key, err = getKey(treeIndex, filterMembershipContract)
if err != nil {
return nil, err
}
}

credential, ok := k.Credentials[key]
Expand All @@ -108,7 +129,7 @@ func (k *AppKeystore) GetMembershipCredentials(keystorePassword string, treeInde

// AddMembershipCredentials inserts a membership credential to the keystore matching the application, appIdentifier and version filters.
func (k *AppKeystore) AddMembershipCredentials(newCredential MembershipCredentials, password string) error {
credentials, err := k.GetMembershipCredentials(password, newCredential.TreeIndex, newCredential.MembershipContractInfo)
credentials, err := k.GetMembershipCredentials(password, &newCredential.TreeIndex, newCredential.MembershipContractInfo)
if err != nil {
return err
}
Expand All @@ -118,7 +139,7 @@ func (k *AppKeystore) AddMembershipCredentials(newCredential MembershipCredentia
return err
}

if credentials != nil {
if credentials != nil && credentials.TreeIndex == newCredential.TreeIndex && credentials.MembershipContractInfo.Equals(newCredential.MembershipContractInfo) {
return errors.New("credential already present")
}

Expand Down
8 changes: 4 additions & 4 deletions waku/v2/protocol/rln/onchain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ func (s *WakuRLNRelayDynamicSuite) TestDynamicGroupManagement() {

membershipIndex := s.register(appKeystore, u1Credentials, s.u1PrivKey)

gm, err := dynamic.NewDynamicGroupManager(s.web3Config.ETHClientAddress, s.web3Config.RegistryContract.Address, membershipIndex, appKeystore, keystorePassword, prometheus.DefaultRegisterer, utils.Logger())
gm, err := dynamic.NewDynamicGroupManager(s.web3Config.ETHClientAddress, s.web3Config.RegistryContract.Address, &membershipIndex, appKeystore, keystorePassword, prometheus.DefaultRegisterer, utils.Logger())
s.Require().NoError(err)

// initialize the WakuRLNRelay
Expand Down Expand Up @@ -232,7 +232,7 @@ func (s *WakuRLNRelayDynamicSuite) TestMerkleTreeConstruction() {
membershipIndex = s.register(appKeystore, credentials2, s.u1PrivKey)

// mount the rln relay protocol in the on-chain/dynamic mode
gm, err := dynamic.NewDynamicGroupManager(s.web3Config.ETHClientAddress, s.web3Config.RegistryContract.Address, membershipIndex, appKeystore, keystorePassword, prometheus.DefaultRegisterer, utils.Logger())
gm, err := dynamic.NewDynamicGroupManager(s.web3Config.ETHClientAddress, s.web3Config.RegistryContract.Address, &membershipIndex, appKeystore, keystorePassword, prometheus.DefaultRegisterer, utils.Logger())
s.Require().NoError(err)

rlnRelay, err := New(gm, s.tmpRLNDBPath(), timesource.NewDefaultClock(), prometheus.DefaultRegisterer, utils.Logger())
Expand Down Expand Up @@ -265,7 +265,7 @@ func (s *WakuRLNRelayDynamicSuite) TestCorrectRegistrationOfPeers() {
membershipGroupIndex := s.register(appKeystore, credentials1, s.u1PrivKey)

// mount the rln relay protocol in the on-chain/dynamic mode
gm1, err := dynamic.NewDynamicGroupManager(s.web3Config.ETHClientAddress, s.web3Config.RegistryContract.Address, membershipGroupIndex, appKeystore, keystorePassword, prometheus.DefaultRegisterer, utils.Logger())
gm1, err := dynamic.NewDynamicGroupManager(s.web3Config.ETHClientAddress, s.web3Config.RegistryContract.Address, &membershipGroupIndex, appKeystore, keystorePassword, prometheus.DefaultRegisterer, utils.Logger())
s.Require().NoError(err)

rlnRelay1, err := New(gm1, s.tmpRLNDBPath(), timesource.NewDefaultClock(), prometheus.DefaultRegisterer, utils.Logger())
Expand All @@ -283,7 +283,7 @@ func (s *WakuRLNRelayDynamicSuite) TestCorrectRegistrationOfPeers() {
membershipGroupIndex = s.register(appKeystore2, credentials2, s.u2PrivKey)

// mount the rln relay protocol in the on-chain/dynamic mode
gm2, err := dynamic.NewDynamicGroupManager(s.web3Config.ETHClientAddress, s.web3Config.RegistryContract.Address, membershipGroupIndex, appKeystore2, keystorePassword, prometheus.DefaultRegisterer, utils.Logger())
gm2, err := dynamic.NewDynamicGroupManager(s.web3Config.ETHClientAddress, s.web3Config.RegistryContract.Address, &membershipGroupIndex, appKeystore2, keystorePassword, prometheus.DefaultRegisterer, utils.Logger())
s.Require().NoError(err)

rlnRelay2, err := New(gm2, s.tmpRLNDBPath(), timesource.NewDefaultClock(), prometheus.DefaultRegisterer, utils.Logger())
Expand Down

0 comments on commit adacf87

Please sign in to comment.