fix(whisper expiry): current time + work + ttl (#10587)

This commit is contained in:
Niklas Adolfsson 2019-04-15 15:05:40 +02:00 committed by TriplEight
parent 6fcd775d48
commit 92e770e916

View File

@ -264,17 +264,17 @@ impl Message {
let mut rng = { let mut rng = {
let mut thread_rng = ::rand::thread_rng(); let mut thread_rng = ::rand::thread_rng();
XorShiftRng::from_seed(thread_rng.gen::<[u32; 4]>()) XorShiftRng::from_seed(thread_rng.gen::<[u32; 4]>())
}; };
assert!(params.ttl > 0); assert!(params.ttl > 0);
let expiry = { let expiry = {
let after_mining = SystemTime::now().checked_sub(Duration::from_millis(params.work)) let since_epoch = SystemTime::now()
.ok_or(Error::TimestampOverflow)?; .checked_add(Duration::from_secs(params.ttl))
let since_epoch = after_mining.duration_since(time::UNIX_EPOCH) .and_then(|t| t.checked_add(Duration::from_millis(params.work)))
.expect("time after now is after unix epoch; qed"); .ok_or(Error::TimestampOverflow)?
.duration_since(time::UNIX_EPOCH).expect("time after now is after unix epoch; qed");
// round up the sub-second to next whole second. // round up the sub-second to next whole second.
since_epoch.as_secs() + if since_epoch.subsec_nanos() == 0 { 0 } else { 1 } since_epoch.as_secs() + if since_epoch.subsec_nanos() == 0 { 0 } else { 1 }