go-waku/waku/v2/protocol/rln/group_manager/static/static.go

99 lines
2.4 KiB
Go
Raw Normal View History

2023-04-03 21:45:19 +00:00
package static
import (
"context"
"errors"
"github.com/waku-org/go-waku/waku/v2/protocol/rln/group_manager"
2023-04-03 21:45:19 +00:00
"github.com/waku-org/go-zerokit-rln/rln"
"go.uber.org/zap"
)
type StaticGroupManager struct {
rln *rln.RLN
log *zap.Logger
identityCredential *rln.IdentityCredential
membershipIndex rln.MembershipIndex
group []rln.IDCommitment
rootTracker *group_manager.MerkleRootTracker
nextIndex uint64
2023-04-03 21:45:19 +00:00
}
func NewStaticGroupManager(
group []rln.IDCommitment,
identityCredential rln.IdentityCredential,
index rln.MembershipIndex,
rlnInstance *rln.RLN,
rootTracker *group_manager.MerkleRootTracker,
2023-04-03 21:45:19 +00:00
log *zap.Logger,
) (*StaticGroupManager, error) {
// check the peer's index and the inclusion of user's identity commitment in the group
if identityCredential.IDCommitment != group[int(index)] {
2023-04-03 21:45:19 +00:00
return nil, errors.New("peer's IDCommitment does not match commitment in group")
}
return &StaticGroupManager{
log: log.Named("rln-static"),
group: group,
identityCredential: &identityCredential,
membershipIndex: index,
rln: rlnInstance,
rootTracker: rootTracker,
2023-04-03 21:45:19 +00:00
}, nil
}
func (gm *StaticGroupManager) Start(ctx context.Context) error {
2023-04-03 21:45:19 +00:00
gm.log.Info("mounting rln-relay in off-chain/static mode")
// add members to the Merkle tree
err := gm.insertMembers(gm.group)
if err != nil {
return err
2023-04-03 21:45:19 +00:00
}
gm.group = nil // Deleting group to release memory
return nil
}
func (gm *StaticGroupManager) insertMembers(idCommitments []rln.IDCommitment) error {
err := gm.rln.InsertMembers(rln.MembershipIndex(gm.nextIndex), idCommitments)
if err != nil {
gm.log.Error("inserting members into merkletree", zap.Error(err))
return err
2023-04-03 21:45:19 +00:00
}
latestIndex := gm.nextIndex + uint64(len(idCommitments))
gm.rootTracker.UpdateLatestRoot(latestIndex)
2023-04-03 21:45:19 +00:00
gm.nextIndex = latestIndex + 1
return nil
}
func (gm *StaticGroupManager) IdentityCredentials() (rln.IdentityCredential, error) {
if gm.identityCredential == nil {
return rln.IdentityCredential{}, errors.New("identity credential has not been setup")
2023-04-03 21:45:19 +00:00
}
return *gm.identityCredential, nil
2023-04-03 21:45:19 +00:00
}
func (gm *StaticGroupManager) MembershipIndex() rln.MembershipIndex {
return gm.membershipIndex
2023-04-03 21:45:19 +00:00
}
// Stop is a function created just to comply with the GroupManager interface (it does nothing)
func (gm *StaticGroupManager) Stop() error {
// Do nothing
return nil
2023-04-03 21:45:19 +00:00
}
2023-09-11 21:34:56 +00:00
func (gm *StaticGroupManager) IsReady(ctx context.Context) (bool, error) {
return true, nil
}