Skip to content

Commit 0879186

Browse files
committed
fix order of magnitude
1 parent 0fd0912 commit 0879186

File tree

2 files changed

+54
-9
lines changed

2 files changed

+54
-9
lines changed

seth/gas_adjuster.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -787,12 +787,16 @@ func calculateMagnitudeDifference(first, second *big.Float) (int, string) {
787787
secondOrderOfMagnitude := math.Log10(secondFloat)
788788

789789
diff := firstOrderOfMagnitude - secondOrderOfMagnitude
790+
absDiff := math.Abs(diff)
791+
792+
// Values within the same order of magnitude (less than 10x difference)
793+
if absDiff < 1 {
794+
return 0, "the same order of magnitude"
795+
}
790796

791797
if diff < 0 {
792798
intDiff := math.Floor(diff)
793799
return int(intDiff), fmt.Sprintf("%d orders of magnitude smaller", int(math.Abs(intDiff)))
794-
} else if diff > 0 && diff <= 1 {
795-
return 0, "the same order of magnitude"
796800
}
797801

798802
intDiff := int(math.Ceil(diff))

seth/gas_adjuster_test.go

Lines changed: 48 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,45 @@ func TestGasAdjuster_CalculateMagnitudeDifference(t *testing.T) {
3232
{
3333
name: "Similar magnitude (within 1 order)",
3434
first: big.NewFloat(30_000_000_000), // 30 gwei
35-
second: big.NewFloat(2_000_000_000), // 2 gwei
35+
second: big.NewFloat(31_000_000_000), // 31 gwei
3636
expectedDiff: 0,
3737
expectedText: "the same order of magnitude",
3838
},
39+
{
40+
name: "Similar magnitude (within 1 order)",
41+
first: big.NewFloat(100_000_000_000), // 100 gwei
42+
second: big.NewFloat(99_999_999_999), // 99.999999999 gwei
43+
expectedDiff: 0,
44+
expectedText: "the same order of magnitude",
45+
},
46+
{
47+
name: "Similar magnitude (within 1 order)",
48+
first: big.NewFloat(30_000_000_000), // 30 gwei
49+
second: big.NewFloat(99_999_999_999), // 99.999999999 gwei
50+
expectedDiff: 0,
51+
expectedText: "the same order of magnitude",
52+
},
53+
{
54+
name: "Similar magnitude (within 1 order)",
55+
first: big.NewFloat(99_999_999_999), // 99.999999999 gwei
56+
second: big.NewFloat(30_000_000_000), // 30 gwei
57+
expectedDiff: 0,
58+
expectedText: "the same order of magnitude",
59+
},
60+
{
61+
name: "Similar magnitude (within 1 order)",
62+
first: big.NewFloat(99_999_999_999), // 99.999999999 gwei
63+
second: big.NewFloat(100_000_000_000), // 100 gwei
64+
expectedDiff: 0,
65+
expectedText: "the same order of magnitude",
66+
},
67+
{
68+
name: "Just under 1 order of magnitude (same order)",
69+
first: big.NewFloat(9_999_999_999), // 9.999... gwei
70+
second: big.NewFloat(1_000_000_000), // 1 gwei
71+
expectedDiff: 0, // Still same order (diff = 0.9999...)
72+
expectedText: "the same order of magnitude",
73+
},
3974
{
4075
name: "Exactly 3 orders larger",
4176
first: big.NewFloat(1_000_000_000), // 1 gwei
@@ -71,11 +106,13 @@ func TestGasAdjuster_CalculateMagnitudeDifference(t *testing.T) {
71106
diff, text := calculateMagnitudeDifference(tt.first, tt.second)
72107

73108
if diff != tt.expectedDiff {
74-
t.Errorf("calculateMagnitudeDifference() diff = %v, want %v", diff, tt.expectedDiff)
109+
t.Errorf("calculateMagnitudeDifference(first=%s wei, second=%s wei)\n diff = %v, want %v",
110+
tt.first.Text('f', 0), tt.second.Text('f', 0), diff, tt.expectedDiff)
75111
}
76112

77113
if text != tt.expectedText {
78-
t.Errorf("calculateMagnitudeDifference() text = %v, want %v", text, tt.expectedText)
114+
t.Errorf("calculateMagnitudeDifference(first=%s wei, second=%s wei)\n text = %q, want %q",
115+
tt.first.Text('f', 0), tt.second.Text('f', 0), text, tt.expectedText)
79116
}
80117
})
81118
}
@@ -226,13 +263,17 @@ func TestGasAdjuster_FeeEqualizerLogic(t *testing.T) {
226263
resultTip := currentGasTip.Int64()
227264

228265
if resultBaseFee != tt.expectedBaseFee {
229-
t.Errorf("Base fee after adjustment = %d, want %d\nDescription: %s",
230-
resultBaseFee, tt.expectedBaseFee, tt.description)
266+
t.Errorf("Base fee after adjustment = %d wei (%.4f gwei), want %d wei (%.4f gwei)\nDescription: %s",
267+
resultBaseFee, float64(resultBaseFee)/1e9,
268+
tt.expectedBaseFee, float64(tt.expectedBaseFee)/1e9,
269+
tt.description)
231270
}
232271

233272
if resultTip != tt.expectedTip {
234-
t.Errorf("Tip after adjustment = %d, want %d\nDescription: %s",
235-
resultTip, tt.expectedTip, tt.description)
273+
t.Errorf("Tip after adjustment = %d wei (%.4f gwei), want %d wei (%.4f gwei)\nDescription: %s",
274+
resultTip, float64(resultTip)/1e9,
275+
tt.expectedTip, float64(tt.expectedTip)/1e9,
276+
tt.description)
236277
}
237278

238279
if baseFeeAdjusted != tt.shouldAdjustBaseFee {

0 commit comments

Comments
 (0)