Skip to content

Commit

Permalink
refactor: use a map to store credentials instead of an array
Browse files Browse the repository at this point in the history
  • Loading branch information
richard-ramos committed Aug 31, 2023
1 parent fdb2b6d commit bbe4b12
Show file tree
Hide file tree
Showing 19 changed files with 214 additions and 316 deletions.
17 changes: 6 additions & 11 deletions cmd/waku/flags_rln.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package main
import (
cli "github.com/urfave/cli/v2"
wcli "github.com/waku-org/go-waku/waku/cliutils"
"github.com/waku-org/go-waku/waku/v2/protocol/rln/keystore"
)

func rlnFlags() []cli.Flag {
Expand All @@ -17,10 +18,10 @@ func rlnFlags() []cli.Flag {
Destination: &options.RLNRelay.Enable,
},
&cli.UintFlag{
Name: "rln-relay-membership-group-index",
Name: "rln-relay-membership-index",
Value: 0,
Usage: "the index of credentials to use, within a specific rln membership set",
Destination: &options.RLNRelay.MembershipGroupIndex,
Usage: "the index of credentials to use",
Destination: &options.RLNRelay.MembershipIndex,
},
&cli.BoolFlag{
Name: "rln-relay-dynamic",
Expand All @@ -30,12 +31,12 @@ func rlnFlags() []cli.Flag {
&cli.PathFlag{
Name: "rln-relay-cred-path",
Usage: "RLN relay membership credentials file",
Value: "",
Value: keystore.DefaultCredentialsFilename,
Destination: &options.RLNRelay.CredentialsPath,
},
&cli.StringFlag{
Name: "rln-relay-cred-password",
Value: "",
Value: keystore.DefaultCredentialsPassword,
Usage: "Password for encrypting RLN credentials",
Destination: &options.RLNRelay.CredentialsPassword,
},
Expand All @@ -45,12 +46,6 @@ func rlnFlags() []cli.Flag {
Usage: "Path to the RLN merkle tree sled db (https://github.com/spacejam/sled)",
Destination: &options.RLNRelay.TreePath,
},
&cli.UintFlag{
Name: "rln-relay-membership-index",
Value: 0,
Usage: "the index of credentials to use",
Destination: &options.RLNRelay.CredentialsIndex,
},
&cli.StringFlag{
Name: "rln-relay-eth-client-address",
Usage: "Ethereum testnet client address",
Expand Down
5 changes: 2 additions & 3 deletions cmd/waku/node_rln.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,16 @@ 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.MembershipGroupIndex), 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
*nodeOpts = append(*nodeOpts, node.WithDynamicRLNRelay(
options.RLNRelay.CredentialsPath,
options.RLNRelay.CredentialsPassword,
options.RLNRelay.CredentialsIndex,
options.RLNRelay.TreePath,
options.RLNRelay.MembershipContractAddress,
rln.MembershipIndex(options.RLNRelay.MembershipGroupIndex),
rln.MembershipIndex(options.RLNRelay.MembershipIndex),
nil,
options.RLNRelay.ETHClientAddress,
))
Expand Down
3 changes: 1 addition & 2 deletions cmd/waku/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,8 @@ type RLNRelayOptions struct {
Enable bool
CredentialsPath string
CredentialsPassword string
CredentialsIndex uint
TreePath string
MembershipGroupIndex uint
MembershipIndex uint
Dynamic bool
ETHClientAddress string
MembershipContractAddress common.Address
Expand Down
17 changes: 7 additions & 10 deletions cmd/waku/rlngenerate/command_rln.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,27 +103,24 @@ func execute(ctx context.Context) error {
return nil
}

func persistCredentials(identityCredential *rln.IdentityCredential, membershipIndex rln.MembershipIndex, chainID *big.Int) error {
func persistCredentials(identityCredential *rln.IdentityCredential, treeIndex rln.MembershipIndex, chainID *big.Int) error {
appKeystore, err := keystore.New(options.CredentialsPath, dynamic.RLNAppInfo, logger)
if err != nil {
return err
}

membershipGroup := keystore.MembershipGroup{
TreeIndex: membershipIndex,
MembershipContract: keystore.MembershipContract{
ChainID: fmt.Sprintf("0x%X", chainID.Int64()),
Address: options.MembershipContractAddress.String(),
},
membershipCredential := keystore.MembershipCredentials{
IdentityCredential: identityCredential,
TreeIndex: treeIndex,
MembershipContractInfo: keystore.NewMembershipContractInfo(chainID, options.MembershipContractAddress),
}

membershipGroupIndex, err := appKeystore.AddMembershipCredentials(identityCredential, membershipGroup, options.CredentialsPassword)
err = appKeystore.AddMembershipCredentials(membershipCredential, options.CredentialsPassword)
if err != nil {
return fmt.Errorf("failed to persist credentials: %w", err)
}

// TODO: obtain keystore index?
logger.Info("persisted credentials succesfully", zap.Uint("membershipGroupIndex", membershipGroupIndex))
logger.Info("persisted credentials succesfully")

return nil
}
6 changes: 1 addition & 5 deletions examples/chat2/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -468,11 +468,7 @@ func (c *Chat) welcomeMessage() {
fmt.Println(err.Error())
}

idx, err := c.node.RLNRelay().MembershipIndex()
if err != nil {
c.ui.Quit()
fmt.Println(err.Error())
}
idx := c.node.RLNRelay().MembershipIndex()

idTrapdoor := credential.IDTrapdoor
idNullifier := credential.IDSecretHash
Expand Down
1 change: 0 additions & 1 deletion examples/chat2/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ func execute(options Options) {
opts = append(opts, node.WithDynamicRLNRelay(
options.RLNRelay.CredentialsPath,
options.RLNRelay.CredentialsPassword,
options.RLNRelay.CredentialsIndex,
"", // Will use default tree path
options.RLNRelay.MembershipContractAddress,
uint(options.RLNRelay.MembershipIndex),
Expand Down
8 changes: 1 addition & 7 deletions examples/chat2/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,17 +185,11 @@ func getFlags() []cli.Flag {
Usage: "Enable spam protection through rln-relay",
Destination: &options.RLNRelay.Enable,
},
&cli.UintFlag{
Name: "rln-relay-membership-group-index",
Value: 0,
Usage: "the index of credentials to use, within a specific rln membership set",
Destination: &options.RLNRelay.MembershipGroupIndex,
},
&cli.UintFlag{
Name: "rln-relay-membership-index",
Value: 0,
Usage: "the index of credentials to use",
Destination: &options.RLNRelay.CredentialsIndex,
Destination: &options.RLNRelay.MembershipIndex,
},
&cli.BoolFlag{
Name: "rln-relay-dynamic",
Expand Down
2 changes: 0 additions & 2 deletions examples/chat2/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ type RLNRelayOptions struct {
Enable bool
CredentialsPath string
CredentialsPassword string
CredentialsIndex uint
MembershipGroupIndex uint
MembershipIndex uint
Dynamic bool
ETHClientAddress string
Expand Down
8 changes: 2 additions & 6 deletions examples/rln/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ const ethClientAddress = "wss://sepolia.infura.io/ws/v3/API_KEY_GOES_HERE"
const contractAddress = "0x9C09146844C1326c2dBC41c451766C7138F88155"
const keystorePath = "" // Empty to store in current folder
const keystorePassword = "" // Empty to use default
const keystoreIndex = 0
const membershipGroupIndex = 0
const membershipIndex = 0

var contentTopic = protocol.NewContentTopic("rln", 1, "test", "proto").String()
var pubsubTopic = protocol.DefaultPubsubTopic()
Expand Down Expand Up @@ -63,14 +62,11 @@ func main() {
node.WithNTP(),
node.WithWakuRelay(),
node.WithDynamicRLNRelay(
pubsubTopic.String(),
contentTopic,
keystorePath,
keystorePassword,
keystoreIndex,
"", // Will use default tree path
common.HexToAddress(contractAddress),
membershipGroupIndex,
membershipIndex,
spamHandler,
ethClientAddress,
),
Expand Down
2 changes: 1 addition & 1 deletion waku/v2/node/wakunode2.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ type SpamHandler = func(message *pb.WakuMessage) error

type RLNRelay interface {
IdentityCredential() (IdentityCredential, error)
MembershipIndex() (uint, error)
MembershipIndex() uint
AppendRLNProof(msg *pb.WakuMessage, senderEpochTime time.Time) error
Validator(spamHandler SpamHandler) func(ctx context.Context, peerID peer.ID, message *pubsub.Message) bool
Start(ctx context.Context) error
Expand Down
1 change: 0 additions & 1 deletion waku/v2/node/wakunode2_rln.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ func (w *WakuNode) setupRLNRelay() error {
w.opts.rlnRelayMemIndex,
appKeystore,
w.opts.keystorePassword,
w.opts.keystoreIndex,
w.opts.prometheusReg,
w.log,
)
Expand Down
1 change: 0 additions & 1 deletion waku/v2/node/wakuoptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ type WakuNodeParameters struct {
rlnETHClientAddress string
keystorePath string
keystorePassword string
keystoreIndex uint
rlnTreePath string
rlnMembershipContractAddress common.Address

Expand Down
5 changes: 2 additions & 3 deletions waku/v2/node/wakuoptions_rln.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,16 @@ 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, keystoreIndex uint, treePath string, membershipContract common.Address, membershipGroupIndex 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
params.keystorePassword = keystorePassword
params.keystorePath = keystorePath
params.keystoreIndex = keystoreIndex
params.rlnSpamHandler = spamHandler
params.rlnETHClientAddress = ethClientAddress
params.rlnMembershipContractAddress = membershipContract
params.rlnRelayMemIndex = membershipGroupIndex
params.rlnRelayMemIndex = membershipIndex
params.rlnTreePath = treePath
return nil
}
Expand Down
41 changes: 10 additions & 31 deletions waku/v2/protocol/rln/group_manager/dynamic/dynamic.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package dynamic
import (
"context"
"errors"
"fmt"
"math/big"
"sync"
"time"
Expand All @@ -25,7 +24,7 @@ import (
var RLNAppInfo = keystore.AppInfo{
Application: "waku-rln-relay",
AppIdentifier: "01234567890abcdef",
Version: "0.1",
Version: "0.2",
}

type DynamicGroupManager struct {
Expand All @@ -37,10 +36,9 @@ type DynamicGroupManager struct {
wg sync.WaitGroup

identityCredential *rln.IdentityCredential
membershipIndex *rln.MembershipIndex
membershipIndex rln.MembershipIndex

membershipContractAddress common.Address
membershipGroupIndex uint
ethClientAddress string
ethClient *ethclient.Client

Expand All @@ -53,7 +51,6 @@ type DynamicGroupManager struct {

appKeystore *keystore.AppKeystore
keystorePassword string
keystoreIndex uint

rootTracker *group_manager.MerkleRootTracker
}
Expand Down Expand Up @@ -120,23 +117,21 @@ type RegistrationHandler = func(tx *types.Transaction)
func NewDynamicGroupManager(
ethClientAddr string,
memContractAddr common.Address,
membershipGroupIndex uint,
membershipIndex uint,
appKeystore *keystore.AppKeystore,
keystorePassword string,
keystoreIndex uint,
reg prometheus.Registerer,
log *zap.Logger,
) (*DynamicGroupManager, error) {
log = log.Named("rln-dynamic")

return &DynamicGroupManager{
membershipGroupIndex: membershipGroupIndex,
membershipIndex: membershipIndex,
membershipContractAddress: memContractAddr,
ethClientAddress: ethClientAddr,
eventHandler: handler,
appKeystore: appKeystore,
keystorePassword: keystorePassword,
keystoreIndex: keystoreIndex,
log: log,
metrics: newMetrics(reg),
}, nil
Expand Down Expand Up @@ -198,30 +193,18 @@ func (gm *DynamicGroupManager) loadCredential() error {

credentials, err := gm.appKeystore.GetMembershipCredentials(
gm.keystorePassword,
nil,
[]keystore.MembershipContract{{
ChainID: fmt.Sprintf("0x%X", gm.chainId),
Address: gm.membershipContractAddress.Hex(),
}})
gm.membershipIndex,
keystore.NewMembershipContractInfo(gm.chainId, gm.membershipContractAddress))
if err != nil {
return err
}
gm.metrics.RecordMembershipCredentialsImportDuration(time.Since(start))

if len(credentials) == 0 {
if credentials == nil {
return errors.New("no credentials available")
}

if int(gm.keystoreIndex) > len(credentials)-1 {
return errors.New("invalid keystore index")
}

if int(gm.membershipGroupIndex) > len(credentials[gm.keystoreIndex].MembershipGroups)-1 {
return errors.New("invalid membership group index")
}

gm.identityCredential = credentials[gm.keystoreIndex].IdentityCredential
gm.membershipIndex = &credentials[gm.keystoreIndex].MembershipGroups[gm.membershipGroupIndex].TreeIndex
gm.identityCredential = credentials.IdentityCredential

return nil
}
Expand Down Expand Up @@ -282,12 +265,8 @@ func (gm *DynamicGroupManager) IdentityCredentials() (rln.IdentityCredential, er
return *gm.identityCredential, nil
}

func (gm *DynamicGroupManager) MembershipIndex() (rln.MembershipIndex, error) {
if gm.membershipIndex == nil {
return 0, errors.New("membership index has not been setup")
}

return *gm.membershipIndex, nil
func (gm *DynamicGroupManager) MembershipIndex() rln.MembershipIndex {
return gm.membershipIndex
}

// Stop stops all go-routines, eth client and closes the rln database
Expand Down
12 changes: 4 additions & 8 deletions waku/v2/protocol/rln/group_manager/static/static.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type StaticGroupManager struct {
log *zap.Logger

identityCredential *rln.IdentityCredential
membershipIndex *rln.MembershipIndex
membershipIndex rln.MembershipIndex

group []rln.IDCommitment
rootTracker *group_manager.MerkleRootTracker
Expand All @@ -36,7 +36,7 @@ func NewStaticGroupManager(
log: log.Named("rln-static"),
group: group,
identityCredential: &identityCredential,
membershipIndex: &index,
membershipIndex: index,
}, nil
}

Expand Down Expand Up @@ -85,12 +85,8 @@ func (gm *StaticGroupManager) IdentityCredentials() (rln.IdentityCredential, err
return *gm.identityCredential, nil
}

func (gm *StaticGroupManager) MembershipIndex() (rln.MembershipIndex, error) {
if gm.membershipIndex == nil {
return 0, errors.New("membership index has not been setup")
}

return *gm.membershipIndex, nil
func (gm *StaticGroupManager) MembershipIndex() rln.MembershipIndex {
return gm.membershipIndex
}

// Stop is a function created just to comply with the GroupManager interface (it does nothing)
Expand Down
Loading

0 comments on commit bbe4b12

Please sign in to comment.