Skip to content

Commit

Permalink
chore(rln-relay): add logs for fetching events
Browse files Browse the repository at this point in the history
  • Loading branch information
richard-ramos committed Sep 12, 2023
1 parent dcc87cf commit 75486f8
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
13 changes: 11 additions & 2 deletions waku/v2/protocol/rln/group_manager/dynamic/dynamic.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package dynamic
import (
"context"
"errors"
"fmt"
"math/big"
"time"

Expand Down Expand Up @@ -126,11 +127,19 @@ func NewDynamicGroupManager(
}

func (gm *DynamicGroupManager) getMembershipFee(ctx context.Context) (*big.Int, error) {
return gm.web3Config.RLNContract.MEMBERSHIPDEPOSIT(&bind.CallOpts{Context: ctx})
fee, err := gm.web3Config.RLNContract.MEMBERSHIPDEPOSIT(&bind.CallOpts{Context: ctx})
if err != nil {
return nil, fmt.Errorf("could not check if credential exits in contract: %w", err)
}
return fee, nil
}

func (gm *DynamicGroupManager) memberExists(ctx context.Context, idCommitment rln.IDCommitment) (bool, error) {
return gm.web3Config.RLNContract.MemberExists(&bind.CallOpts{Context: ctx}, rln.Bytes32ToBigInt(idCommitment))
exists, err := gm.web3Config.RLNContract.MemberExists(&bind.CallOpts{Context: ctx}, rln.Bytes32ToBigInt(idCommitment))
if err != nil {
return false, fmt.Errorf("could not check if credential exits in contract: %w", err)
}
return exists, nil
}

func (gm *DynamicGroupManager) Start(ctx context.Context) error {
Expand Down
21 changes: 19 additions & 2 deletions waku/v2/protocol/rln/group_manager/dynamic/membership_fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,14 @@ func (mf *MembershipFetcher) HandleGroupUpdates(ctx context.Context, handler Reg
return err
}
//

mf.log.Info("loading old events...")
t := time.Now()
err = mf.loadOldEvents(ctx, fromBlock, latestBlockNumber, handler)
if err != nil {
return err
}
mf.log.Info("events loaded", zap.Duration("timeToLoad", time.Since(t)))

errCh := make(chan error)

Expand All @@ -81,22 +85,35 @@ func (mf *MembershipFetcher) HandleGroupUpdates(ctx context.Context, handler Reg

func (mf *MembershipFetcher) loadOldEvents(ctx context.Context, fromBlock, toBlock uint64, handler RegistrationEventHandler) error {
for ; fromBlock+maxBatchSize < toBlock; fromBlock += maxBatchSize + 1 { // check if the end of the batch is within the toBlock range
t1 := time.Now()
events, err := mf.getEvents(ctx, fromBlock, fromBlock+maxBatchSize)
if err != nil {
return err
}

t2 := time.Now()
if err := handler(events); err != nil {
return err
}

mf.log.Info("fetching events", zap.Uint64("from", fromBlock), zap.Uint64("to", fromBlock+maxBatchSize), zap.Int("numEvents", len(events)), zap.Duration("timeToFetch", time.Since(t1)), zap.Duration("timeToProcess", time.Since(t2)))
}

//
t1 := time.Now()
events, err := mf.getEvents(ctx, fromBlock, toBlock)
if err != nil {
return err
}

// process all the fetched events
return handler(events)
t2 := time.Now()
err = handler(events)
if err != nil {
return err
}

mf.log.Info("fetching events", zap.Uint64("from", fromBlock), zap.Uint64("to", fromBlock+maxBatchSize), zap.Int("numEvents", len(events)), zap.Duration("timeToFetch", time.Since(t1)), zap.Duration("timeToProcess", time.Since(t2)))
return nil
}

func (mf *MembershipFetcher) watchNewEvents(ctx context.Context, fromBlock uint64, handler RegistrationEventHandler, errCh chan<- error) {
Expand Down

0 comments on commit 75486f8

Please sign in to comment.