From 19e6128d035e65c21df2d7d70d829b9ca1a38179 Mon Sep 17 00:00:00 2001 From: james Date: Thu, 30 Nov 2023 12:17:53 +0800 Subject: [PATCH] restruct --- common/{util/math/min.go => math/maxmin.go} | 2 +- common/util/math/min_test.go | 64 ------------------- .../difficultymanager/difficultymanager.go | 4 +- 3 files changed, 3 insertions(+), 67 deletions(-) rename common/{util/math/min.go => math/maxmin.go} (89%) delete mode 100644 common/util/math/min_test.go diff --git a/common/util/math/min.go b/common/math/maxmin.go similarity index 89% rename from common/util/math/min.go rename to common/math/maxmin.go index edbeac11..7d13dbfd 100644 --- a/common/util/math/min.go +++ b/common/math/maxmin.go @@ -9,7 +9,7 @@ func MinInt(x, y int) int { } // MaxInt64 returns the bigger of x or y. -func MaxInt64(x, y int64) int64 { +func MaxInt64Val(x, y int64) int64 { if x > y { return x } diff --git a/common/util/math/min_test.go b/common/util/math/min_test.go deleted file mode 100644 index 62d46f4d..00000000 --- a/common/util/math/min_test.go +++ /dev/null @@ -1,64 +0,0 @@ -package math_test - -import ( - utilMath "github.com/kaspanet/kaspad/util/math" - "math" - "testing" -) - -const ( - MaxInt = int(^uint(0) >> 1) - MinInt = -MaxInt - 1 -) - -func TestMinInt(t *testing.T) { - tests := []struct { - inputs [2]int - expected int - }{ - {[2]int{MaxInt, 0}, 0}, - {[2]int{1, 2}, 1}, - {[2]int{MaxInt, MaxInt}, MaxInt}, - {[2]int{MaxInt, MaxInt - 1}, MaxInt - 1}, - {[2]int{MaxInt, MinInt}, MinInt}, - {[2]int{MinInt, 0}, MinInt}, - {[2]int{MinInt, MinInt}, MinInt}, - {[2]int{0, MinInt + 1}, MinInt + 1}, - {[2]int{0, MinInt}, MinInt}, - } - - for i, test := range tests { - result := utilMath.MinInt(test.inputs[0], test.inputs[1]) - if result != test.expected { - t.Fatalf("%d: Expected %d, instead found: %d", i, test.expected, result) - } - reverseResult := utilMath.MinInt(test.inputs[1], test.inputs[0]) - if result != reverseResult { - t.Fatalf("%d: Expected result and reverseResult to be the same, instead: %d!=%d", i, result, reverseResult) - } - } -} - -func TestMinUint32(t *testing.T) { - tests := []struct { - inputs [2]uint32 - expected uint32 - }{ - {[2]uint32{math.MaxUint32, 0}, 0}, - {[2]uint32{1, 2}, 1}, - {[2]uint32{math.MaxUint32, math.MaxUint32}, math.MaxUint32}, - {[2]uint32{math.MaxUint32, math.MaxUint32 - 1}, math.MaxUint32 - 1}, - } - - for _, test := range tests { - result := utilMath.MinUint32(test.inputs[0], test.inputs[1]) - if result != test.expected { - t.Fatalf("Expected %d, instead found: %d", test.expected, result) - - } - reverseResult := utilMath.MinUint32(test.inputs[1], test.inputs[0]) - if result != reverseResult { - t.Fatalf("Expected result and reverseResult to be the same, instead: %d!=%d", result, reverseResult) - } - } -} diff --git a/core/types/pow/difficultymanager/difficultymanager.go b/core/types/pow/difficultymanager/difficultymanager.go index dea65a36..84f96149 100644 --- a/core/types/pow/difficultymanager/difficultymanager.go +++ b/core/types/pow/difficultymanager/difficultymanager.go @@ -4,7 +4,7 @@ import ( "math/big" "time" - "github.com/Qitmeer/qng/common/util/math" + "github.com/Qitmeer/qng/common/math" "github.com/Qitmeer/qng/core/types/pow" "github.com/Qitmeer/qng/params" ) @@ -66,7 +66,7 @@ func (dm *difficultyManager) requiredDifficultyFromTargetsWindow(targetsWindow B newTarget := targetsWindow.AverageTarget() newTarget. // We need to clamp the timestamp difference to 1 so that we'll never get a 0 target. - Mul(newTarget, div.SetInt64(math.MaxInt64(windowMaxTimeStamp-windowMinTimestamp, 1))). + Mul(newTarget, div.SetInt64(math.MaxInt64Val(windowMaxTimeStamp-windowMinTimestamp, 1))). Div(newTarget, div.SetInt64(dm.targetTimePerBlock.Milliseconds())). Div(newTarget, div.SetUint64(uint64(len(targetsWindow)))) if newTarget.Cmp(dm.powMax) > 0 {