all: get rid of custom MaxUint64 and MaxUint64 (#30636)

This commit is contained in:
Péter Szilágyi 2024-10-20 14:41:51 +03:00 committed by GitHub
parent babd5d8026
commit 48d05c43c9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
19 changed files with 36 additions and 122 deletions

View File

@ -234,38 +234,3 @@ func U256(x *big.Int) *big.Int {
func U256Bytes(n *big.Int) []byte { func U256Bytes(n *big.Int) []byte {
return PaddedBigBytes(U256(n), 32) return PaddedBigBytes(U256(n), 32)
} }
// S256 interprets x as a two's complement number.
// x must not exceed 256 bits (the result is undefined if it does) and is not modified.
//
// S256(0) = 0
// S256(1) = 1
// S256(2**255) = -2**255
// S256(2**256-1) = -1
func S256(x *big.Int) *big.Int {
if x.Cmp(tt255) < 0 {
return x
}
return new(big.Int).Sub(x, tt256)
}
// Exp implements exponentiation by squaring.
// Exp returns a newly-allocated big integer and does not change
// base or exponent. The result is truncated to 256 bits.
//
// Courtesy @karalabe and @chfast
func Exp(base, exponent *big.Int) *big.Int {
copyBase := new(big.Int).Set(base)
result := big.NewInt(1)
for _, word := range exponent.Bits() {
for i := 0; i < wordBits; i++ {
if word&1 == 1 {
U256(result.Mul(result, copyBase))
}
U256(copyBase.Mul(copyBase, copyBase))
word >>= 1
}
}
return result
}

View File

@ -277,48 +277,3 @@ func TestLittleEndianByteAt(t *testing.T) {
} }
} }
} }
func TestS256(t *testing.T) {
tests := []struct{ x, y *big.Int }{
{x: big.NewInt(0), y: big.NewInt(0)},
{x: big.NewInt(1), y: big.NewInt(1)},
{x: big.NewInt(2), y: big.NewInt(2)},
{
x: new(big.Int).Sub(BigPow(2, 255), big.NewInt(1)),
y: new(big.Int).Sub(BigPow(2, 255), big.NewInt(1)),
},
{
x: BigPow(2, 255),
y: new(big.Int).Neg(BigPow(2, 255)),
},
{
x: new(big.Int).Sub(BigPow(2, 256), big.NewInt(1)),
y: big.NewInt(-1),
},
{
x: new(big.Int).Sub(BigPow(2, 256), big.NewInt(2)),
y: big.NewInt(-2),
},
}
for _, test := range tests {
if y := S256(test.x); y.Cmp(test.y) != 0 {
t.Errorf("S256(%x) = %x, want %x", test.x, y, test.y)
}
}
}
func TestExp(t *testing.T) {
tests := []struct{ base, exponent, result *big.Int }{
{base: big.NewInt(0), exponent: big.NewInt(0), result: big.NewInt(1)},
{base: big.NewInt(1), exponent: big.NewInt(0), result: big.NewInt(1)},
{base: big.NewInt(1), exponent: big.NewInt(1), result: big.NewInt(1)},
{base: big.NewInt(1), exponent: big.NewInt(2), result: big.NewInt(1)},
{base: big.NewInt(3), exponent: big.NewInt(144), result: MustParseBig256("507528786056415600719754159741696356908742250191663887263627442114881")},
{base: big.NewInt(2), exponent: big.NewInt(255), result: MustParseBig256("57896044618658097711785492504343953926634992332820282019728792003956564819968")},
}
for _, test := range tests {
if result := Exp(test.base, test.exponent); result.Cmp(test.result) != 0 {
t.Errorf("Exp(%d, %d) = %d, want %d", test.base, test.exponent, result, test.result)
}
}
}

View File

@ -22,22 +22,6 @@ import (
"strconv" "strconv"
) )
// Integer limit values.
const (
MaxInt8 = 1<<7 - 1
MinInt8 = -1 << 7
MaxInt16 = 1<<15 - 1
MinInt16 = -1 << 15
MaxInt32 = 1<<31 - 1
MinInt32 = -1 << 31
MaxInt64 = 1<<63 - 1
MinInt64 = -1 << 63
MaxUint8 = 1<<8 - 1
MaxUint16 = 1<<16 - 1
MaxUint32 = 1<<32 - 1
MaxUint64 = 1<<64 - 1
)
// HexOrDecimal64 marshals uint64 as hex or decimal. // HexOrDecimal64 marshals uint64 as hex or decimal.
type HexOrDecimal64 uint64 type HexOrDecimal64 uint64

View File

@ -17,6 +17,7 @@
package math package math
import ( import (
"math"
"testing" "testing"
) )
@ -36,8 +37,8 @@ func TestOverflow(t *testing.T) {
op operation op operation
}{ }{
// add operations // add operations
{MaxUint64, 1, true, add}, {math.MaxUint64, 1, true, add},
{MaxUint64 - 1, 1, false, add}, {math.MaxUint64 - 1, 1, false, add},
// sub operations // sub operations
{0, 1, true, sub}, {0, 1, true, sub},
@ -46,8 +47,8 @@ func TestOverflow(t *testing.T) {
// mul operations // mul operations
{0, 0, false, mul}, {0, 0, false, mul},
{10, 10, false, mul}, {10, 10, false, mul},
{MaxUint64, 2, true, mul}, {math.MaxUint64, 2, true, mul},
{MaxUint64, 1, false, mul}, {math.MaxUint64, 1, false, mul},
} { } {
var overflows bool var overflows bool
switch test.op { switch test.op {

View File

@ -19,6 +19,7 @@ package core
import ( import (
"errors" "errors"
"fmt" "fmt"
gomath "math"
"math/big" "math/big"
"math/rand" "math/rand"
"os" "os"
@ -1949,11 +1950,11 @@ func testSideImport(t *testing.T, numCanonBlocksInSidechain, blocksBetweenCommon
gspec = &Genesis{ gspec = &Genesis{
Config: &chainConfig, Config: &chainConfig,
Alloc: types.GenesisAlloc{addr: {Balance: big.NewInt(math.MaxInt64)}}, Alloc: types.GenesisAlloc{addr: {Balance: big.NewInt(gomath.MaxInt64)}},
BaseFee: big.NewInt(params.InitialBaseFee), BaseFee: big.NewInt(params.InitialBaseFee),
} }
signer = types.LatestSigner(gspec.Config) signer = types.LatestSigner(gspec.Config)
mergeBlock = math.MaxInt32 mergeBlock = gomath.MaxInt32
) )
// Generate and import the canonical chain // Generate and import the canonical chain
chain, err := NewBlockChain(rawdb.NewMemoryDatabase(), nil, gspec, nil, engine, vm.Config{}, nil) 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
Config: &chainConfig, Config: &chainConfig,
} }
engine = beacon.New(ethash.NewFaker()) engine = beacon.New(ethash.NewFaker())
mergeBlock = uint64(math.MaxUint64) mergeBlock = uint64(gomath.MaxUint64)
) )
// Apply merging since genesis // Apply merging since genesis
if mergeHeight == 0 { if mergeHeight == 0 {

View File

@ -18,8 +18,8 @@ package rawdb
import ( import (
"fmt" "fmt"
"math"
"github.com/ethereum/go-ethereum/common/math"
"github.com/ethereum/go-ethereum/rlp" "github.com/ethereum/go-ethereum/rlp"
"github.com/golang/snappy" "github.com/golang/snappy"
) )

View File

@ -19,10 +19,10 @@ package rawdb
import ( import (
"errors" "errors"
"fmt" "fmt"
"math"
"sync" "sync"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/math"
"github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/rlp" "github.com/ethereum/go-ethereum/rlp"

View File

@ -20,10 +20,10 @@ import (
"bytes" "bytes"
"encoding/binary" "encoding/binary"
"errors" "errors"
"math"
"time" "time"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/math"
"github.com/ethereum/go-ethereum/core/rawdb" "github.com/ethereum/go-ethereum/core/rawdb"
"github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/ethdb/memorydb" "github.com/ethereum/go-ethereum/ethdb/memorydb"

View File

@ -19,11 +19,11 @@ package core
import ( import (
"crypto/ecdsa" "crypto/ecdsa"
"encoding/binary" "encoding/binary"
"math"
"math/big" "math/big"
"testing" "testing"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/math"
"github.com/ethereum/go-ethereum/consensus" "github.com/ethereum/go-ethereum/consensus"
"github.com/ethereum/go-ethereum/consensus/beacon" "github.com/ethereum/go-ethereum/consensus/beacon"
"github.com/ethereum/go-ethereum/consensus/ethash" "github.com/ethereum/go-ethereum/consensus/ethash"

View File

@ -18,6 +18,7 @@ package types
import ( import (
"bytes" "bytes"
gomath "math"
"math/big" "math/big"
"reflect" "reflect"
"testing" "testing"
@ -277,9 +278,9 @@ func TestRlpDecodeParentHash(t *testing.T) {
if rlpData, err := rlp.EncodeToBytes(&Header{ if rlpData, err := rlp.EncodeToBytes(&Header{
ParentHash: want, ParentHash: want,
Difficulty: mainnetTd, Difficulty: mainnetTd,
Number: new(big.Int).SetUint64(math.MaxUint64), Number: new(big.Int).SetUint64(gomath.MaxUint64),
Extra: make([]byte, 65+32), Extra: make([]byte, 65+32),
BaseFee: new(big.Int).SetUint64(math.MaxUint64), BaseFee: new(big.Int).SetUint64(gomath.MaxUint64),
}); err != nil { }); err != nil {
t.Fatal(err) t.Fatal(err)
} else { } else {

View File

@ -17,8 +17,9 @@
package vm package vm
import ( import (
"math"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/math"
"github.com/holiman/uint256" "github.com/holiman/uint256"
) )

View File

@ -22,6 +22,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"maps" "maps"
gomath "math"
"math/big" "math/big"
"github.com/consensys/gnark-crypto/ecc" "github.com/consensys/gnark-crypto/ecc"
@ -416,7 +417,7 @@ func (c *bigModExp) RequiredGas(input []byte) uint64 {
// 2. Different divisor (`GQUADDIVISOR`) (3) // 2. Different divisor (`GQUADDIVISOR`) (3)
gas.Div(gas, big3) gas.Div(gas, big3)
if gas.BitLen() > 64 { if gas.BitLen() > 64 {
return math.MaxUint64 return gomath.MaxUint64
} }
// 3. Minimum price of 200 gas // 3. Minimum price of 200 gas
if gas.Uint64() < 200 { if gas.Uint64() < 200 {
@ -429,7 +430,7 @@ func (c *bigModExp) RequiredGas(input []byte) uint64 {
gas.Div(gas, big20) gas.Div(gas, big20)
if gas.BitLen() > 64 { if gas.BitLen() > 64 {
return math.MaxUint64 return gomath.MaxUint64
} }
return gas.Uint64() return gas.Uint64()
} }

View File

@ -17,11 +17,11 @@
package vm package vm
import ( import (
"math"
"testing" "testing"
"time" "time"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/math"
"github.com/ethereum/go-ethereum/core/state" "github.com/ethereum/go-ethereum/core/state"
"github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/params" "github.com/ethereum/go-ethereum/params"

View File

@ -17,6 +17,8 @@
package vm package vm
import ( import (
gomath "math"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/math" "github.com/ethereum/go-ethereum/common/math"
"github.com/ethereum/go-ethereum/params" "github.com/ethereum/go-ethereum/params"
@ -126,7 +128,7 @@ func gasCodeCopyEip4762(evm *EVM, contract *Contract, stack *Stack, mem *Memory,
) )
uint64CodeOffset, overflow := codeOffset.Uint64WithOverflow() uint64CodeOffset, overflow := codeOffset.Uint64WithOverflow()
if overflow { if overflow {
uint64CodeOffset = math.MaxUint64 uint64CodeOffset = gomath.MaxUint64
} }
_, copyOffset, nonPaddedCopyLength := getDataAndAdjustedBounds(contract.Code, uint64CodeOffset, length.Uint64()) _, copyOffset, nonPaddedCopyLength := getDataAndAdjustedBounds(contract.Code, uint64CodeOffset, length.Uint64())
if !contract.IsDeployment { if !contract.IsDeployment {

View File

@ -22,6 +22,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"maps" "maps"
gomath "math"
"math/big" "math/big"
"strings" "strings"
"time" "time"
@ -432,7 +433,7 @@ func (api *PersonalAccountAPI) UnlockAccount(ctx context.Context, addr common.Ad
return false, errors.New("account unlock with HTTP access is forbidden") return false, errors.New("account unlock with HTTP access is forbidden")
} }
const max = uint64(time.Duration(math.MaxInt64) / time.Second) const max = uint64(time.Duration(gomath.MaxInt64) / time.Second)
var d time.Duration var d time.Duration
if duration == nil { if duration == nil {
d = 300 * time.Second d = 300 * time.Second
@ -1183,7 +1184,7 @@ func doCall(ctx context.Context, b Backend, args TransactionArgs, state *state.S
defer cancel() defer cancel()
gp := new(core.GasPool) gp := new(core.GasPool)
if globalGasCap == 0 { if globalGasCap == 0 {
gp.AddGas(math.MaxUint64) gp.AddGas(gomath.MaxUint64)
} else { } else {
gp.AddGas(globalGasCap) gp.AddGas(globalGasCap)
} }
@ -1291,7 +1292,7 @@ func (api *BlockChainAPI) SimulateV1(ctx context.Context, opts simOpts, blockNrO
} }
gasCap := api.b.RPCGasCap() gasCap := api.b.RPCGasCap()
if gasCap == 0 { if gasCap == 0 {
gasCap = math.MaxUint64 gasCap = gomath.MaxUint64
} }
sim := &simulator{ sim := &simulator{
b: api.b, b: api.b,

View File

@ -22,6 +22,7 @@ import (
"crypto/sha256" "crypto/sha256"
"errors" "errors"
"fmt" "fmt"
gomath "math"
"math/big" "math/big"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
@ -140,7 +141,7 @@ func (args *TransactionArgs) setDefaults(ctx context.Context, b Backend, skipGas
if skipGasEstimation { // Skip gas usage estimation if a precise gas limit is not critical, e.g., in non-transaction calls. if skipGasEstimation { // Skip gas usage estimation if a precise gas limit is not critical, e.g., in non-transaction calls.
gas := hexutil.Uint64(b.RPCGasCap()) gas := hexutil.Uint64(b.RPCGasCap())
if gas == 0 { if gas == 0 {
gas = hexutil.Uint64(math.MaxUint64 / 2) gas = hexutil.Uint64(gomath.MaxUint64 / 2)
} }
args.Gas = &gas args.Gas = &gas
} else { // Estimate the gas usage otherwise. } else { // Estimate the gas usage otherwise.
@ -378,7 +379,7 @@ func (args *TransactionArgs) CallDefaults(globalGasCap uint64, baseFee *big.Int,
if args.Gas == nil { if args.Gas == nil {
gas := globalGasCap gas := globalGasCap
if gas == 0 { if gas == 0 {
gas = uint64(math.MaxUint64 / 2) gas = uint64(gomath.MaxUint64 / 2)
} }
args.Gas = (*hexutil.Uint64)(&gas) args.Gas = (*hexutil.Uint64)(&gas)
} else { } else {

View File

@ -17,12 +17,12 @@
package params package params
import ( import (
"math"
"math/big" "math/big"
"reflect" "reflect"
"testing" "testing"
"time" "time"
"github.com/ethereum/go-ethereum/common/math"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )

View File

@ -22,6 +22,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"io" "io"
gomath "math"
"math/big" "math/big"
"reflect" "reflect"
"strings" "strings"
@ -555,7 +556,7 @@ var decodeTests = []decodeTest{
// uint256 // uint256
{input: "80", ptr: new(*uint256.Int), value: uint256.NewInt(0)}, {input: "80", ptr: new(*uint256.Int), value: uint256.NewInt(0)},
{input: "01", ptr: new(*uint256.Int), value: uint256.NewInt(1)}, {input: "01", ptr: new(*uint256.Int), value: uint256.NewInt(1)},
{input: "88FFFFFFFFFFFFFFFF", ptr: new(*uint256.Int), value: uint256.NewInt(math.MaxUint64)}, {input: "88FFFFFFFFFFFFFFFF", ptr: new(*uint256.Int), value: uint256.NewInt(gomath.MaxUint64)},
{input: "89FFFFFFFFFFFFFFFFFF", ptr: new(*uint256.Int), value: veryBigInt256}, {input: "89FFFFFFFFFFFFFFFFFF", ptr: new(*uint256.Int), value: veryBigInt256},
{input: "10", ptr: new(uint256.Int), value: *uint256.NewInt(16)}, // non-pointer also works {input: "10", ptr: new(uint256.Int), value: *uint256.NewInt(16)}, // non-pointer also works

View File

@ -18,11 +18,11 @@ package rpc
import ( import (
"encoding/json" "encoding/json"
"math"
"reflect" "reflect"
"testing" "testing"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/math"
) )
func TestBlockNumberJSONUnmarshal(t *testing.T) { func TestBlockNumberJSONUnmarshal(t *testing.T) {