From 3ac16db9c79472d29d5cb5af4e0d8a94bea7d569 Mon Sep 17 00:00:00 2001 From: ConvallariaMaj Date: Wed, 14 Aug 2024 11:33:02 +0200 Subject: [PATCH] fix review issues --- CHANGELOG.md | 1 + activation/activation.go | 9 +++++---- activation/poet.go | 23 ++++++----------------- 3 files changed, 12 insertions(+), 21 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5da21a3c98..9ba268ee7d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -55,6 +55,7 @@ such and the node will continue to scan new ATXs for their validity. * [#6142](https://github.com/spacemeshos/go-spacemesh/pull/6142) Fix node not dropping peers that are broadcasting invalid ATXs. + ## Release v1.6.3 ### Improvements diff --git a/activation/activation.go b/activation/activation.go index 53624c2fb1..cea8b0b778 100644 --- a/activation/activation.go +++ b/activation/activation.go @@ -41,11 +41,12 @@ var ( type PoetConfig struct { // Offset from the epoch start when the poet round starts PhaseShift time.Duration `mapstructure:"phase-shift"` - // A gap between end of old PoET round and start of new one + // CycleGap gives the duration between the end of a PoET round and the start of the next CycleGap time.Duration `mapstructure:"cycle-gap"` - // Time duration measured from the end of cycle gap, when we start to build the NiPoST challenge. - // The later we start (the smaller this value is) - // the higher is the chance for getting a good positioning ATX. + // GracePeriod defines the time before the start of the next PoET round until the node + // waits before building its NiPoST challenge. Shorter durations allow the node to + // possibly pick a better positioning ATX, but come with the risk that the node might + // not be able to validate that ATX and has to fall back to using its own previous ATX. GracePeriod time.Duration `mapstructure:"grace-period"` RequestTimeout time.Duration `mapstructure:"poet-request-timeout"` RequestRetryDelay time.Duration `mapstructure:"retry-delay"` diff --git a/activation/poet.go b/activation/poet.go index 888919663d..844cd0038b 100644 --- a/activation/poet.go +++ b/activation/poet.go @@ -6,7 +6,7 @@ import ( "fmt" "io" "math" - "math/rand" + "math/rand/v2" "net/http" "net/url" "sync" @@ -129,22 +129,11 @@ func WithLogger(logger *zap.Logger) PoetClientOpts { } } -func customLinearJitterBackoff(min, max time.Duration, attemptNum int, resp *http.Response) time.Duration { - attemptNum++ +func customLinearJitterBackoff(min, max time.Duration, _ int, _ *http.Response) time.Duration { if max <= min { - return min * time.Duration(attemptNum) + return min } - - source := rand.New(rand.NewSource(int64(time.Now().Nanosecond()))) - - jitter := source.Float64() * float64(max-min) - jitterMin := int64(jitter) + int64(min) - - sleep := time.Duration(jitterMin * int64(attemptNum)) - if sleep > max { - sleep = max - } - return sleep + return min + rand.N(max-min) } // NewHTTPPoetClient returns new instance of HTTPPoetClient connecting to the specified url. @@ -153,7 +142,7 @@ func NewHTTPPoetClient(server types.PoetServer, cfg PoetConfig, opts ...PoetClie RetryMax: cfg.MaxRequestRetries, RetryWaitMin: cfg.RequestRetryDelay, RetryWaitMax: 2 * cfg.RequestRetryDelay, - Backoff: customLinearJitterBackoff, + Backoff: retryablehttp.LinearJitterBackoff, CheckRetry: checkRetry, } @@ -161,7 +150,7 @@ func NewHTTPPoetClient(server types.PoetServer, cfg PoetConfig, opts ...PoetClie RetryMax: math.MaxInt, RetryWaitMin: cfg.RequestRetryDelay, RetryWaitMax: 2 * cfg.RequestRetryDelay, - Backoff: retryablehttp.DefaultBackoff, + Backoff: customLinearJitterBackoff, CheckRetry: retryablehttp.DefaultRetryPolicy, }