From fa2bcf198fe6ad5893704fdf095d3299f3a10d42 Mon Sep 17 00:00:00 2001 From: Alex Vanin Date: Thu, 1 Sep 2022 15:46:29 +0300 Subject: [PATCH] [#62] Fix expiration epoch calculation Previous implementation does not provide 'at least' lifetime guarantee. Signed-off-by: Alex Vanin --- CHANGELOG.md | 3 +++ handlers/util.go | 18 +++++++++++++++--- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a602e46..d239bad 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,9 @@ This document outlines major changes between releases. ## [Unreleased] +### Fixed +- Fix expiration epoch calculation (#62) + ### Changed - Update go version for build to 1.19 (#61) diff --git a/handlers/util.go b/handlers/util.go index c708014..cde81ca 100644 --- a/handlers/util.go +++ b/handlers/util.go @@ -4,6 +4,7 @@ import ( "context" "errors" "fmt" + "math" "strconv" "time" @@ -153,9 +154,20 @@ func prepareExpirationHeader(headers map[string]string, epochDurations *epochDur } func updateExpirationHeader(headers map[string]string, durations *epochDurations, expDuration time.Duration) { - epochDuration := durations.msPerBlock * int64(durations.blockPerEpoch) - numEpoch := expDuration.Milliseconds() / epochDuration - headers[objectv2.SysAttributeExpEpoch] = strconv.FormatInt(int64(durations.currentEpoch)+numEpoch, 10) + epochDuration := uint64(durations.msPerBlock) * durations.blockPerEpoch + currentEpoch := durations.currentEpoch + numEpoch := uint64(expDuration.Milliseconds()) / epochDuration + + if uint64(expDuration.Milliseconds())%epochDuration != 0 { + numEpoch++ + } + + expirationEpoch := uint64(math.MaxUint64) + if numEpoch < math.MaxUint64-currentEpoch { + expirationEpoch = currentEpoch + numEpoch + } + + headers[objectv2.SysAttributeExpEpoch] = strconv.FormatUint(expirationEpoch, 10) } // IsObjectToken check that provided token is for object.