status-go/protocol/v1/clock.go

21 lines
648 B
Go
Raw Normal View History

package protocol
2019-07-17 22:25:42 +00:00
import "time"
const clockBumpInMs = int64(time.Minute / time.Millisecond)
// CalcMessageClock calculates a new clock value for Message.
// It is used to properly sort messages and accommodate the fact
2019-07-17 22:25:42 +00:00
// that time might be different on each device.
func CalcMessageClock(lastObservedValue int64, timeInMs TimestampInMs) int64 {
clock := lastObservedValue
if clock < int64(timeInMs) {
// Added time should be larger than time skew tollerance for a message.
// Here, we use 1 minute which is larger than accepted message time skew by Whisper.
clock = int64(timeInMs) + clockBumpInMs
} else {
clock++
}
return clock
}