2023-04-03 21:45:19 +00:00
|
|
|
package static
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
|
2023-04-04 15:44:28 +00:00
|
|
|
"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
|
|
|
|
|
2023-04-04 15:44:28 +00:00
|
|
|
identityCredential *rln.IdentityCredential
|
2023-08-24 18:42:50 +00:00
|
|
|
membershipIndex rln.MembershipIndex
|
2023-04-04 15:44:28 +00:00
|
|
|
|
|
|
|
group []rln.IDCommitment
|
|
|
|
rootTracker *group_manager.MerkleRootTracker
|
2023-07-28 17:25:33 +00:00
|
|
|
nextIndex uint64
|
2023-04-03 21:45:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewStaticGroupManager(
|
|
|
|
group []rln.IDCommitment,
|
2023-04-04 15:44:28 +00:00
|
|
|
identityCredential rln.IdentityCredential,
|
|
|
|
index rln.MembershipIndex,
|
2023-09-07 16:23:48 +00:00
|
|
|
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
|
2023-04-04 15:44:28 +00:00
|
|
|
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{
|
2023-04-04 15:44:28 +00:00
|
|
|
log: log.Named("rln-static"),
|
|
|
|
group: group,
|
|
|
|
identityCredential: &identityCredential,
|
2023-08-24 18:42:50 +00:00
|
|
|
membershipIndex: index,
|
2023-09-07 16:23:48 +00:00
|
|
|
rln: rlnInstance,
|
|
|
|
rootTracker: rootTracker,
|
2023-04-03 21:45:19 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2023-09-07 16:23:48 +00:00
|
|
|
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
|
2023-07-28 17:25:33 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-07-28 17:25:33 +00:00
|
|
|
func (gm *StaticGroupManager) insertMembers(idCommitments []rln.IDCommitment) error {
|
|
|
|
err := gm.rln.InsertMembers(rln.MembershipIndex(gm.nextIndex), idCommitments)
|
2023-04-04 15:44:28 +00:00
|
|
|
if err != nil {
|
2023-07-28 17:25:33 +00:00
|
|
|
gm.log.Error("inserting members into merkletree", zap.Error(err))
|
2023-04-04 15:44:28 +00:00
|
|
|
return err
|
2023-04-03 21:45:19 +00:00
|
|
|
}
|
|
|
|
|
2023-07-28 17:25:33 +00:00
|
|
|
latestIndex := gm.nextIndex + uint64(len(idCommitments))
|
|
|
|
|
2023-11-09 20:17:41 +00:00
|
|
|
gm.rootTracker.UpdateLatestRoot(latestIndex)
|
2023-04-03 21:45:19 +00:00
|
|
|
|
2023-07-28 17:25:33 +00:00
|
|
|
gm.nextIndex = latestIndex + 1
|
|
|
|
|
2023-04-04 15:44:28 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-04-04 15:44:28 +00:00
|
|
|
return *gm.identityCredential, nil
|
2023-04-03 21:45:19 +00:00
|
|
|
}
|
|
|
|
|
2023-08-24 18:42:50 +00:00
|
|
|
func (gm *StaticGroupManager) MembershipIndex() rln.MembershipIndex {
|
|
|
|
return gm.membershipIndex
|
2023-04-03 21:45:19 +00:00
|
|
|
}
|
|
|
|
|
2023-08-18 13:59:37 +00:00
|
|
|
// Stop is a function created just to comply with the GroupManager interface (it does nothing)
|
|
|
|
func (gm *StaticGroupManager) Stop() error {
|
2023-04-04 15:44:28 +00:00
|
|
|
// Do nothing
|
2023-08-18 13:59:37 +00:00
|
|
|
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
|
|
|
|
}
|