From 94d01f04a623af9742f0782d40433a4e356c73b3 Mon Sep 17 00:00:00 2001 From: Matthias <5011972+fasmat@users.noreply.github.com> Date: Mon, 4 Sep 2023 13:12:32 +0000 Subject: [PATCH] Fix failing tests and linter --- Makefile | 6 +++--- common/types/layer.go | 12 ------------ mesh/mesh.go | 12 ++++++++---- tortoise/tortoise.go | 35 +++++++++++++++++++++++------------ 4 files changed, 34 insertions(+), 31 deletions(-) diff --git a/Makefile b/Makefile index c5712b21f3..840ac1ced5 100644 --- a/Makefile +++ b/Makefile @@ -45,11 +45,11 @@ all: install build install: git lfs install go mod download - curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s v1.53.3 + curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s v1.54.2 go install github.com/spacemeshos/go-scale/scalegen@v1.1.10 go install go.uber.org/mock/mockgen@v0.2.0 - go install gotest.tools/gotestsum@v1.10.0 - go install honnef.co/go/tools/cmd/staticcheck@v0.4.3 + go install gotest.tools/gotestsum@v1.10.1 + go install honnef.co/go/tools/cmd/staticcheck@v0.4.5 .PHONY: install build: go-spacemesh get-profiler diff --git a/common/types/layer.go b/common/types/layer.go index 2d92702745..b7b66f077e 100644 --- a/common/types/layer.go +++ b/common/types/layer.go @@ -248,15 +248,3 @@ func NewLayer(layerIndex LayerID) *Layer { blocks: make([]*Block, 0, 3), } } - -// MinLayer returns minimal nonzero layer. -func MinLayer(i, j LayerID) LayerID { - switch { - case i == 0: - return j - case j == 0: - return i - default: - return min(i, j) - } -} diff --git a/mesh/mesh.go b/mesh/mesh.go index f55f074d20..368f6770e6 100644 --- a/mesh/mesh.go +++ b/mesh/mesh.go @@ -70,6 +70,10 @@ func NewMesh(cdb *datastore.CachedDB, c layerClock, trtl system.Tortoise, exec * conState: state, nextProcessedLayers: make(map[types.LayerID]struct{}), missingBlocks: make(chan []types.BlockID, 32), + + pendingUpdates: struct { + min, max types.LayerID + }{min: math.MaxUint32}, } msh.latestLayer.Store(types.LayerID(0)) msh.latestLayerInState.Store(types.LayerID(0)) @@ -289,13 +293,13 @@ func (msh *Mesh) ProcessLayer(ctx context.Context, lid types.LayerID) error { return err } results := msh.trtl.Updates() - pending := msh.pendingUpdates.min != 0 + pending := msh.pendingUpdates.min != math.MaxUint32 if len(results) > 0 { - msh.pendingUpdates.min = types.MinLayer(msh.pendingUpdates.min, results[0].Layer) + msh.pendingUpdates.min = min(msh.pendingUpdates.min, results[0].Layer) msh.pendingUpdates.max = max(msh.pendingUpdates.max, results[len(results)-1].Layer) } next := msh.LatestLayerInState() + 1 - if next < msh.pendingUpdates.min { + if msh.pendingUpdates.min != math.MaxUint32 && next < msh.pendingUpdates.min { msh.pendingUpdates.min = next pending = true } @@ -340,7 +344,7 @@ func (msh *Mesh) ProcessLayer(ctx context.Context, lid types.LayerID) error { msh.pendingUpdates.min = applicable[len(applicable)-1].Layer msh.pendingUpdates.max = max(msh.pendingUpdates.min, msh.pendingUpdates.max) } else { - msh.pendingUpdates.min = 0 + msh.pendingUpdates.min = math.MaxUint32 msh.pendingUpdates.max = 0 } return nil diff --git a/tortoise/tortoise.go b/tortoise/tortoise.go index 5ef247f990..59e82719bc 100644 --- a/tortoise/tortoise.go +++ b/tortoise/tortoise.go @@ -360,7 +360,10 @@ func (t *turtle) onLayer(ctx context.Context, last types.LayerID) { opinion := layer.opinion layer.computeOpinion(t.Hdist, t.last) if opinion != layer.opinion { - t.pending = types.MinLayer(t.pending, t.last) + if t.pending == 0 { + t.pending = t.last + } + t.pending = min(t.pending, t.last) } t.logger.Debug("initial local opinion", @@ -423,7 +426,7 @@ func (t *turtle) verifyLayers() { vverified, vchanged := t.runVerifying() if nverified == t.processed-1 && nverified == vverified { t.switchModes() - changed = types.MinLayer(changed, vchanged) + changed = min(changed, vchanged) } } else { nverified, changed = t.runVerifying() @@ -431,17 +434,17 @@ func (t *turtle) verifyLayers() { if !withinDistance(t.Hdist, nverified+1, t.last) { fverified, fchanged := t.runFull() nverified = fverified - changed = types.MinLayer(changed, fchanged) + changed = min(changed, fchanged) } } for target := t.evicted.Add(1); target.Before(t.processed); target = target.Add(1) { if nverified < target { if target < t.verified { - changed = types.MinLayer(changed, target) + changed = min(changed, target) } break } else if target > t.verified { - changed = types.MinLayer(changed, target) + changed = min(changed, target) } verified = target } @@ -451,8 +454,11 @@ func (t *turtle) verifyLayers() { zap.Uint32("verified", verified.Uint32()), zap.Uint32("changed", changed.Uint32()), ) - if changed != 0 { - t.pending = types.MinLayer(t.pending, changed) + if changed != math.MaxUint32 { + if t.pending == 0 { + t.pending = changed + } + t.pending = min(t.pending, changed) t.onOpinionChange(changed, false) } t.verified = verified @@ -461,13 +467,14 @@ func (t *turtle) verifyLayers() { func (t *turtle) runVerifying() (verified, changed types.LayerID) { verified = t.evicted + changed = math.MaxUint32 for target := t.evicted.Add(1); target.Before(t.processed); target = target.Add(1) { v, c := t.verifying.verify(t.logger, target) if !v { return verified, changed } if c { - changed = types.MinLayer(changed, target) + changed = min(changed, target) } verified = target } @@ -486,13 +493,14 @@ func (t *turtle) runFull() (verified, changed types.LayerID) { } } verified = t.evicted + changed = math.MaxUint32 for target := t.evicted.Add(1); target.Before(t.processed); target = target.Add(1) { v, c := t.full.verify(t.logger, target) if !v { return verified, changed } if c { - changed = types.MinLayer(changed, target) + changed = min(changed, target) } verified = target } @@ -598,13 +606,16 @@ func (t *turtle) onOpinionChange(lid types.LayerID, early bool) { zapBlocks(layer.blocks), ) if opinion != layer.opinion { - changed = types.MinLayer(changed, recompute) + changed = min(changed, recompute) } else if early { break } } - if changed != 0 { - t.pending = types.MinLayer(t.pending, changed) + if changed != math.MaxUint32 { + if t.pending == 0 { + t.pending = changed + } + t.pending = min(t.pending, changed) t.verifying.resetWeights(lid) for target := lid.Add(1); !target.After(t.processed); target = target.Add(1) { t.verifying.countVotes(t.logger, t.ballots[target])