-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(rln-relay): clean up nullifier table every MaxEpochGap
- Loading branch information
1 parent
76b0071
commit 27f05fb
Showing
4 changed files
with
116 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package rln | ||
|
||
import ( | ||
"bytes" | ||
"errors" | ||
"sync" | ||
|
||
"github.com/waku-org/go-zerokit-rln/rln" | ||
"go.uber.org/zap" | ||
) | ||
|
||
// NullifierLog is the log of nullifiers and Shamir shares of the past messages grouped per epoch | ||
type NullifierLog struct { | ||
sync.RWMutex | ||
|
||
log *zap.Logger | ||
nullifierLog map[rln.Nullifier][]rln.ProofMetadata // Might make sense to replace this map by a shrinkable map due to https://github.com/golang/go/issues/20135. | ||
nullifierQueue []rln.Nullifier | ||
} | ||
|
||
func NewNullifierLog(log *zap.Logger) *NullifierLog { | ||
result := &NullifierLog{ | ||
nullifierLog: make(map[rln.Nullifier][]rln.ProofMetadata), | ||
log: log, | ||
} | ||
|
||
return result | ||
} | ||
|
||
var errAlreadyExists = errors.New("proof already exists") | ||
|
||
func (n *NullifierLog) Insert(proofMD rln.ProofMetadata) error { | ||
n.Lock() | ||
defer n.Unlock() | ||
|
||
proofs, ok := n.nullifierLog[proofMD.ExternalNullifier] | ||
if ok { | ||
// check if an identical record exists | ||
for _, p := range proofs { | ||
if p.Equals(proofMD) { | ||
// TODO: slashing logic | ||
return errAlreadyExists | ||
} | ||
} | ||
} | ||
|
||
n.nullifierLog[proofMD.ExternalNullifier] = append(proofs, proofMD) | ||
n.nullifierQueue = append(n.nullifierQueue, proofMD.ExternalNullifier) | ||
return nil | ||
} | ||
|
||
// HasDuplicate returns true if there is another message in the `nullifierLog` with the same | ||
// epoch and nullifier as `msg`'s epoch and nullifier but different Shamir secret shares | ||
// otherwise, returns false | ||
func (n *NullifierLog) HasDuplicate(proofMD rln.ProofMetadata) (bool, error) { | ||
n.RLock() | ||
defer n.RUnlock() | ||
|
||
proofs, ok := n.nullifierLog[proofMD.ExternalNullifier] | ||
if !ok { | ||
// epoch does not exist | ||
return false, nil | ||
} | ||
|
||
for _, p := range proofs { | ||
if p.Equals(proofMD) { | ||
// there is an identical record, ignore the msg | ||
return true, nil | ||
} | ||
} | ||
|
||
// check for a message with the same nullifier but different secret shares | ||
matched := false | ||
for _, it := range proofs { | ||
if bytes.Equal(it.Nullifier[:], proofMD.Nullifier[:]) && (!bytes.Equal(it.ShareX[:], proofMD.ShareX[:]) || !bytes.Equal(it.ShareY[:], proofMD.ShareY[:])) { | ||
matched = true | ||
break | ||
} | ||
} | ||
|
||
return matched, nil | ||
} | ||
|
||
// Cleanup cleans up the log every time there are more than MaxEpochGap epochs stored in it | ||
func (n *NullifierLog) Cleanup() { | ||
n.Lock() | ||
defer n.Unlock() | ||
|
||
if int64(len(n.nullifierQueue)) < maxEpochGap { | ||
return | ||
} | ||
|
||
n.log.Debug("clearing epochs from the nullifier log", zap.Int64("count", maxEpochGap)) | ||
|
||
toDelete := n.nullifierQueue[0:maxEpochGap] | ||
for _, l := range toDelete { | ||
delete(n.nullifierLog, l) | ||
} | ||
n.nullifierQueue = n.nullifierQueue[maxEpochGap:] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters