chore(rln-relay): add logs for fetching events

This commit is contained in:
Richard Ramos 2023-09-12 11:08:15 -04:00 committed by richΛrd
parent dcc87cf24f
commit 75486f8226
2 changed files with 30 additions and 4 deletions

View File

@ -3,6 +3,7 @@ package dynamic
import (
"context"
"errors"
"fmt"
"math/big"
"time"
@ -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 {

View File

@ -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)
@ -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) {