feat: store timestamp validity checks (#215)

This commit is contained in:
Richard Ramos 2022-03-22 08:34:45 -04:00 committed by GitHub
parent df235db6b7
commit e85d112828
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 15 additions and 0 deletions

View File

@ -8,6 +8,9 @@ import (
"github.com/status-im/go-waku/waku/v2/utils"
)
// MaxTimeVariance is the maximum duration in the future allowed for a message timestamp
const MaxTimeVariance = time.Duration(20) * time.Second
type MessageQueue struct {
sync.RWMutex
@ -21,6 +24,8 @@ type MessageQueue struct {
}
var ErrDuplicatedMessage = errors.New("duplicated message")
var ErrFutureMessage = errors.New("message timestamp in the future")
var ErrTooOld = errors.New("message is too old")
func (self *MessageQueue) Push(msg IndexedWakuMessage) error {
self.Lock()
@ -33,10 +38,20 @@ func (self *MessageQueue) Push(msg IndexedWakuMessage) error {
return ErrDuplicatedMessage
}
// Ensure that messages don't "jump" to the front of the queue with future timestamps
if msg.index.SenderTime-msg.index.ReceiverTime > int64(MaxTimeVariance) {
return ErrFutureMessage
}
self.seen[k] = struct{}{}
self.messages = append(self.messages, msg)
if self.maxMessages != 0 && len(self.messages) > self.maxMessages {
if indexComparison(msg.index, self.messages[0].index) < 0 {
return ErrTooOld // :(
}
numToPop := len(self.messages) - self.maxMessages
self.messages = self.messages[numToPop:len(self.messages)]
}