fix(rln): keep track of last loaded index

This commit is contained in:
Richard Ramos 2022-10-21 15:32:51 -04:00 committed by RichΛrd
parent 665968deea
commit 9b5922bc74
2 changed files with 13 additions and 5 deletions

View File

@ -51,8 +51,9 @@ type WakuRLNRelay struct {
RLN *r.RLN RLN *r.RLN
// pubsubTopic is the topic for which rln relay is mounted // pubsubTopic is the topic for which rln relay is mounted
pubsubTopic string pubsubTopic string
contentTopic string contentTopic string
lastIndexLoaded r.MembershipIndex
validMerkleRoots []r.MerkleNode validMerkleRoots []r.MerkleNode

View File

@ -107,7 +107,7 @@ func (rln *WakuRLNRelay) Register(ctx context.Context) (*r.MembershipIndex, erro
// the types of inputs to this handler matches the MemberRegistered event/proc defined in the MembershipContract interface // the types of inputs to this handler matches the MemberRegistered event/proc defined in the MembershipContract interface
type RegistrationEventHandler = func(pubkey r.IDCommitment, index r.MembershipIndex) error type RegistrationEventHandler = func(pubkey r.IDCommitment, index r.MembershipIndex) error
func processLogs(evt *contracts.RLNMemberRegistered, handler RegistrationEventHandler) error { func (rln *WakuRLNRelay) processLogs(evt *contracts.RLNMemberRegistered, handler RegistrationEventHandler) error {
if evt == nil { if evt == nil {
return nil return nil
} }
@ -115,7 +115,11 @@ func processLogs(evt *contracts.RLNMemberRegistered, handler RegistrationEventHa
var pubkey r.IDCommitment = r.Bytes32(evt.Pubkey.Bytes()) var pubkey r.IDCommitment = r.Bytes32(evt.Pubkey.Bytes())
index := r.MembershipIndex(uint(evt.Index.Int64())) index := r.MembershipIndex(uint(evt.Index.Int64()))
if index <= rln.lastIndexLoaded {
return nil
}
rln.lastIndexLoaded = index
return handler(pubkey, index) return handler(pubkey, index)
} }
@ -171,7 +175,7 @@ func (rln *WakuRLNRelay) loadOldEvents(rlnContract *contracts.RLN, handler Regis
return logIterator.Error() return logIterator.Error()
} }
err = processLogs(logIterator.Event, handler) err = rln.processLogs(logIterator.Event, handler)
if err != nil { if err != nil {
return err return err
} }
@ -192,6 +196,9 @@ func (rln *WakuRLNRelay) watchNewEvents(rlnContract *contracts.RLN, handler Regi
errCh <- err errCh <- err
subs.Unsubscribe() subs.Unsubscribe()
} }
rln.log.Error("subscribing to rln events", zap.Error(err))
return subs, err return subs, err
}) })
defer subs.Unsubscribe() defer subs.Unsubscribe()
@ -201,7 +208,7 @@ func (rln *WakuRLNRelay) watchNewEvents(rlnContract *contracts.RLN, handler Regi
for { for {
select { select {
case evt := <-logSink: case evt := <-logSink:
err := processLogs(evt, handler) err := rln.processLogs(evt, handler)
if err != nil { if err != nil {
rln.log.Error("processing rln log", zap.Error(err)) rln.log.Error("processing rln log", zap.Error(err))
} }