Skip to content

Commit 48d05c4

Browse files
authored
all: get rid of custom MaxUint64 and MaxUint64 (#30636)
1 parent babd5d8 commit 48d05c4

19 files changed

+36
-122
lines changed

common/math/big.go

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -234,38 +234,3 @@ func U256(x *big.Int) *big.Int {
234234
func U256Bytes(n *big.Int) []byte {
235235
return PaddedBigBytes(U256(n), 32)
236236
}
237-
238-
// S256 interprets x as a two's complement number.
239-
// x must not exceed 256 bits (the result is undefined if it does) and is not modified.
240-
//
241-
// S256(0) = 0
242-
// S256(1) = 1
243-
// S256(2**255) = -2**255
244-
// S256(2**256-1) = -1
245-
func S256(x *big.Int) *big.Int {
246-
if x.Cmp(tt255) < 0 {
247-
return x
248-
}
249-
return new(big.Int).Sub(x, tt256)
250-
}
251-
252-
// Exp implements exponentiation by squaring.
253-
// Exp returns a newly-allocated big integer and does not change
254-
// base or exponent. The result is truncated to 256 bits.
255-
//
256-
// Courtesy @karalabe and @chfast
257-
func Exp(base, exponent *big.Int) *big.Int {
258-
copyBase := new(big.Int).Set(base)
259-
result := big.NewInt(1)
260-
261-
for _, word := range exponent.Bits() {
262-
for i := 0; i < wordBits; i++ {
263-
if word&1 == 1 {
264-
U256(result.Mul(result, copyBase))
265-
}
266-
U256(copyBase.Mul(copyBase, copyBase))
267-
word >>= 1
268-
}
269-
}
270-
return result
271-
}

common/math/big_test.go

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -277,48 +277,3 @@ func TestLittleEndianByteAt(t *testing.T) {
277277
}
278278
}
279279
}
280-
281-
func TestS256(t *testing.T) {
282-
tests := []struct{ x, y *big.Int }{
283-
{x: big.NewInt(0), y: big.NewInt(0)},
284-
{x: big.NewInt(1), y: big.NewInt(1)},
285-
{x: big.NewInt(2), y: big.NewInt(2)},
286-
{
287-
x: new(big.Int).Sub(BigPow(2, 255), big.NewInt(1)),
288-
y: new(big.Int).Sub(BigPow(2, 255), big.NewInt(1)),
289-
},
290-
{
291-
x: BigPow(2, 255),
292-
y: new(big.Int).Neg(BigPow(2, 255)),
293-
},
294-
{
295-
x: new(big.Int).Sub(BigPow(2, 256), big.NewInt(1)),
296-
y: big.NewInt(-1),
297-
},
298-
{
299-
x: new(big.Int).Sub(BigPow(2, 256), big.NewInt(2)),
300-
y: big.NewInt(-2),
301-
},
302-
}
303-
for _, test := range tests {
304-
if y := S256(test.x); y.Cmp(test.y) != 0 {
305-
t.Errorf("S256(%x) = %x, want %x", test.x, y, test.y)
306-
}
307-
}
308-
}
309-
310-
func TestExp(t *testing.T) {
311-
tests := []struct{ base, exponent, result *big.Int }{
312-
{base: big.NewInt(0), exponent: big.NewInt(0), result: big.NewInt(1)},
313-
{base: big.NewInt(1), exponent: big.NewInt(0), result: big.NewInt(1)},
314-
{base: big.NewInt(1), exponent: big.NewInt(1), result: big.NewInt(1)},
315-
{base: big.NewInt(1), exponent: big.NewInt(2), result: big.NewInt(1)},
316-
{base: big.NewInt(3), exponent: big.NewInt(144), result: MustParseBig256("507528786056415600719754159741696356908742250191663887263627442114881")},
317-
{base: big.NewInt(2), exponent: big.NewInt(255), result: MustParseBig256("57896044618658097711785492504343953926634992332820282019728792003956564819968")},
318-
}
319-
for _, test := range tests {
320-
if result := Exp(test.base, test.exponent); result.Cmp(test.result) != 0 {
321-
t.Errorf("Exp(%d, %d) = %d, want %d", test.base, test.exponent, result, test.result)
322-
}
323-
}
324-
}

common/math/integer.go

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,6 @@ import (
2222
"strconv"
2323
)
2424

25-
// Integer limit values.
26-
const (
27-
MaxInt8 = 1<<7 - 1
28-
MinInt8 = -1 << 7
29-
MaxInt16 = 1<<15 - 1
30-
MinInt16 = -1 << 15
31-
MaxInt32 = 1<<31 - 1
32-
MinInt32 = -1 << 31
33-
MaxInt64 = 1<<63 - 1
34-
MinInt64 = -1 << 63
35-
MaxUint8 = 1<<8 - 1
36-
MaxUint16 = 1<<16 - 1
37-
MaxUint32 = 1<<32 - 1
38-
MaxUint64 = 1<<64 - 1
39-
)
40-
4125
// HexOrDecimal64 marshals uint64 as hex or decimal.
4226
type HexOrDecimal64 uint64
4327

common/math/integer_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package math
1818

1919
import (
20+
"math"
2021
"testing"
2122
)
2223

@@ -36,8 +37,8 @@ func TestOverflow(t *testing.T) {
3637
op operation
3738
}{
3839
// add operations
39-
{MaxUint64, 1, true, add},
40-
{MaxUint64 - 1, 1, false, add},
40+
{math.MaxUint64, 1, true, add},
41+
{math.MaxUint64 - 1, 1, false, add},
4142

4243
// sub operations
4344
{0, 1, true, sub},
@@ -46,8 +47,8 @@ func TestOverflow(t *testing.T) {
4647
// mul operations
4748
{0, 0, false, mul},
4849
{10, 10, false, mul},
49-
{MaxUint64, 2, true, mul},
50-
{MaxUint64, 1, false, mul},
50+
{math.MaxUint64, 2, true, mul},
51+
{math.MaxUint64, 1, false, mul},
5152
} {
5253
var overflows bool
5354
switch test.op {

core/blockchain_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package core
1919
import (
2020
"errors"
2121
"fmt"
22+
gomath "math"
2223
"math/big"
2324
"math/rand"
2425
"os"
@@ -1949,11 +1950,11 @@ func testSideImport(t *testing.T, numCanonBlocksInSidechain, blocksBetweenCommon
19491950

19501951
gspec = &Genesis{
19511952
Config: &chainConfig,
1952-
Alloc: types.GenesisAlloc{addr: {Balance: big.NewInt(math.MaxInt64)}},
1953+
Alloc: types.GenesisAlloc{addr: {Balance: big.NewInt(gomath.MaxInt64)}},
19531954
BaseFee: big.NewInt(params.InitialBaseFee),
19541955
}
19551956
signer = types.LatestSigner(gspec.Config)
1956-
mergeBlock = math.MaxInt32
1957+
mergeBlock = gomath.MaxInt32
19571958
)
19581959
// Generate and import the canonical chain
19591960
chain, err := NewBlockChain(rawdb.NewMemoryDatabase(), nil, gspec, nil, engine, vm.Config{}, nil)
@@ -2236,7 +2237,7 @@ func testInsertKnownChainDataWithMerging(t *testing.T, typ string, mergeHeight i
22362237
Config: &chainConfig,
22372238
}
22382239
engine = beacon.New(ethash.NewFaker())
2239-
mergeBlock = uint64(math.MaxUint64)
2240+
mergeBlock = uint64(gomath.MaxUint64)
22402241
)
22412242
// Apply merging since genesis
22422243
if mergeHeight == 0 {

core/rawdb/freezer_batch.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ package rawdb
1818

1919
import (
2020
"fmt"
21+
"math"
2122

22-
"github.com/ethereum/go-ethereum/common/math"
2323
"github.com/ethereum/go-ethereum/rlp"
2424
"github.com/golang/snappy"
2525
)

core/rawdb/freezer_memory.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ package rawdb
1919
import (
2020
"errors"
2121
"fmt"
22+
"math"
2223
"sync"
2324

2425
"github.com/ethereum/go-ethereum/common"
25-
"github.com/ethereum/go-ethereum/common/math"
2626
"github.com/ethereum/go-ethereum/ethdb"
2727
"github.com/ethereum/go-ethereum/log"
2828
"github.com/ethereum/go-ethereum/rlp"

core/state/snapshot/context.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ import (
2020
"bytes"
2121
"encoding/binary"
2222
"errors"
23+
"math"
2324
"time"
2425

2526
"github.com/ethereum/go-ethereum/common"
26-
"github.com/ethereum/go-ethereum/common/math"
2727
"github.com/ethereum/go-ethereum/core/rawdb"
2828
"github.com/ethereum/go-ethereum/ethdb"
2929
"github.com/ethereum/go-ethereum/ethdb/memorydb"

core/state_processor_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ package core
1919
import (
2020
"crypto/ecdsa"
2121
"encoding/binary"
22+
"math"
2223
"math/big"
2324
"testing"
2425

2526
"github.com/ethereum/go-ethereum/common"
26-
"github.com/ethereum/go-ethereum/common/math"
2727
"github.com/ethereum/go-ethereum/consensus"
2828
"github.com/ethereum/go-ethereum/consensus/beacon"
2929
"github.com/ethereum/go-ethereum/consensus/ethash"

core/types/block_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package types
1818

1919
import (
2020
"bytes"
21+
gomath "math"
2122
"math/big"
2223
"reflect"
2324
"testing"
@@ -277,9 +278,9 @@ func TestRlpDecodeParentHash(t *testing.T) {
277278
if rlpData, err := rlp.EncodeToBytes(&Header{
278279
ParentHash: want,
279280
Difficulty: mainnetTd,
280-
Number: new(big.Int).SetUint64(math.MaxUint64),
281+
Number: new(big.Int).SetUint64(gomath.MaxUint64),
281282
Extra: make([]byte, 65+32),
282-
BaseFee: new(big.Int).SetUint64(math.MaxUint64),
283+
BaseFee: new(big.Int).SetUint64(gomath.MaxUint64),
283284
}); err != nil {
284285
t.Fatal(err)
285286
} else {

core/vm/common.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@
1717
package vm
1818

1919
import (
20+
"math"
21+
2022
"github.com/ethereum/go-ethereum/common"
21-
"github.com/ethereum/go-ethereum/common/math"
2223
"github.com/holiman/uint256"
2324
)
2425

core/vm/contracts.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"errors"
2323
"fmt"
2424
"maps"
25+
gomath "math"
2526
"math/big"
2627

2728
"github.com/consensys/gnark-crypto/ecc"
@@ -416,7 +417,7 @@ func (c *bigModExp) RequiredGas(input []byte) uint64 {
416417
// 2. Different divisor (`GQUADDIVISOR`) (3)
417418
gas.Div(gas, big3)
418419
if gas.BitLen() > 64 {
419-
return math.MaxUint64
420+
return gomath.MaxUint64
420421
}
421422
// 3. Minimum price of 200 gas
422423
if gas.Uint64() < 200 {
@@ -429,7 +430,7 @@ func (c *bigModExp) RequiredGas(input []byte) uint64 {
429430
gas.Div(gas, big20)
430431

431432
if gas.BitLen() > 64 {
432-
return math.MaxUint64
433+
return gomath.MaxUint64
433434
}
434435
return gas.Uint64()
435436
}

core/vm/interpreter_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@
1717
package vm
1818

1919
import (
20+
"math"
2021
"testing"
2122
"time"
2223

2324
"github.com/ethereum/go-ethereum/common"
24-
"github.com/ethereum/go-ethereum/common/math"
2525
"github.com/ethereum/go-ethereum/core/state"
2626
"github.com/ethereum/go-ethereum/core/types"
2727
"github.com/ethereum/go-ethereum/params"

core/vm/operations_verkle.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
package vm
1818

1919
import (
20+
gomath "math"
21+
2022
"github.com/ethereum/go-ethereum/common"
2123
"github.com/ethereum/go-ethereum/common/math"
2224
"github.com/ethereum/go-ethereum/params"
@@ -126,7 +128,7 @@ func gasCodeCopyEip4762(evm *EVM, contract *Contract, stack *Stack, mem *Memory,
126128
)
127129
uint64CodeOffset, overflow := codeOffset.Uint64WithOverflow()
128130
if overflow {
129-
uint64CodeOffset = math.MaxUint64
131+
uint64CodeOffset = gomath.MaxUint64
130132
}
131133
_, copyOffset, nonPaddedCopyLength := getDataAndAdjustedBounds(contract.Code, uint64CodeOffset, length.Uint64())
132134
if !contract.IsDeployment {

internal/ethapi/api.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"errors"
2323
"fmt"
2424
"maps"
25+
gomath "math"
2526
"math/big"
2627
"strings"
2728
"time"
@@ -432,7 +433,7 @@ func (api *PersonalAccountAPI) UnlockAccount(ctx context.Context, addr common.Ad
432433
return false, errors.New("account unlock with HTTP access is forbidden")
433434
}
434435

435-
const max = uint64(time.Duration(math.MaxInt64) / time.Second)
436+
const max = uint64(time.Duration(gomath.MaxInt64) / time.Second)
436437
var d time.Duration
437438
if duration == nil {
438439
d = 300 * time.Second
@@ -1183,7 +1184,7 @@ func doCall(ctx context.Context, b Backend, args TransactionArgs, state *state.S
11831184
defer cancel()
11841185
gp := new(core.GasPool)
11851186
if globalGasCap == 0 {
1186-
gp.AddGas(math.MaxUint64)
1187+
gp.AddGas(gomath.MaxUint64)
11871188
} else {
11881189
gp.AddGas(globalGasCap)
11891190
}
@@ -1291,7 +1292,7 @@ func (api *BlockChainAPI) SimulateV1(ctx context.Context, opts simOpts, blockNrO
12911292
}
12921293
gasCap := api.b.RPCGasCap()
12931294
if gasCap == 0 {
1294-
gasCap = math.MaxUint64
1295+
gasCap = gomath.MaxUint64
12951296
}
12961297
sim := &simulator{
12971298
b: api.b,

internal/ethapi/transaction_args.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"crypto/sha256"
2323
"errors"
2424
"fmt"
25+
gomath "math"
2526
"math/big"
2627

2728
"github.com/ethereum/go-ethereum/common"
@@ -140,7 +141,7 @@ func (args *TransactionArgs) setDefaults(ctx context.Context, b Backend, skipGas
140141
if skipGasEstimation { // Skip gas usage estimation if a precise gas limit is not critical, e.g., in non-transaction calls.
141142
gas := hexutil.Uint64(b.RPCGasCap())
142143
if gas == 0 {
143-
gas = hexutil.Uint64(math.MaxUint64 / 2)
144+
gas = hexutil.Uint64(gomath.MaxUint64 / 2)
144145
}
145146
args.Gas = &gas
146147
} else { // Estimate the gas usage otherwise.
@@ -378,7 +379,7 @@ func (args *TransactionArgs) CallDefaults(globalGasCap uint64, baseFee *big.Int,
378379
if args.Gas == nil {
379380
gas := globalGasCap
380381
if gas == 0 {
381-
gas = uint64(math.MaxUint64 / 2)
382+
gas = uint64(gomath.MaxUint64 / 2)
382383
}
383384
args.Gas = (*hexutil.Uint64)(&gas)
384385
} else {

params/config_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@
1717
package params
1818

1919
import (
20+
"math"
2021
"math/big"
2122
"reflect"
2223
"testing"
2324
"time"
2425

25-
"github.com/ethereum/go-ethereum/common/math"
2626
"github.com/stretchr/testify/require"
2727
)
2828

rlp/decode_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"errors"
2323
"fmt"
2424
"io"
25+
gomath "math"
2526
"math/big"
2627
"reflect"
2728
"strings"
@@ -555,7 +556,7 @@ var decodeTests = []decodeTest{
555556
// uint256
556557
{input: "80", ptr: new(*uint256.Int), value: uint256.NewInt(0)},
557558
{input: "01", ptr: new(*uint256.Int), value: uint256.NewInt(1)},
558-
{input: "88FFFFFFFFFFFFFFFF", ptr: new(*uint256.Int), value: uint256.NewInt(math.MaxUint64)},
559+
{input: "88FFFFFFFFFFFFFFFF", ptr: new(*uint256.Int), value: uint256.NewInt(gomath.MaxUint64)},
559560
{input: "89FFFFFFFFFFFFFFFFFF", ptr: new(*uint256.Int), value: veryBigInt256},
560561
{input: "10", ptr: new(uint256.Int), value: *uint256.NewInt(16)}, // non-pointer also works
561562

0 commit comments

Comments
 (0)