status-go/protocol/v1/clock.go

21 lines
630 B
Go
Raw Permalink Normal View History

package protocol
2019-07-17 22:25:42 +00:00
import "time"
const clockBumpInMs = uint64(time.Minute / time.Millisecond)
2019-07-17 22:25:42 +00:00
// 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 uint64, timeInMs uint64) uint64 {
2019-07-17 22:25:42 +00:00
clock := lastObservedValue
if clock < timeInMs {
2019-07-17 22:25:42 +00:00
// 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 = timeInMs + clockBumpInMs
2019-07-17 22:25:42 +00:00
} else {
clock++
}
return clock
}