Skip to content

Commit

Permalink
Merge pull request #2185 from CortexFoundation/dev
Browse files Browse the repository at this point in the history
get rid of custom MaxUint64 and MaxUint64
  • Loading branch information
ucwong authored Oct 31, 2024
2 parents dd9bf95 + dc3a41c commit aec0024
Show file tree
Hide file tree
Showing 14 changed files with 25 additions and 74 deletions.
45 changes: 0 additions & 45 deletions common/math/big_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,48 +270,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)
}
}
}
16 changes: 0 additions & 16 deletions common/math/integer.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,6 @@ import (
"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.
type HexOrDecimal64 uint64

Expand Down
9 changes: 5 additions & 4 deletions common/math/integer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package math

import (
"math"
"testing"
)

Expand All @@ -36,8 +37,8 @@ func TestOverflow(t *testing.T) {
op operation
}{
// add operations
{MaxUint64, 1, true, add},
{MaxUint64 - 1, 1, false, add},
{math.MaxUint64, 1, true, add},
{math.MaxUint64 - 1, 1, false, add},

// sub operations
{0, 1, true, sub},
Expand All @@ -46,8 +47,8 @@ func TestOverflow(t *testing.T) {
// mul operations
{0, 0, false, mul},
{10, 10, false, mul},
{MaxUint64, 2, true, mul},
{MaxUint64, 1, false, mul},
{math.MaxUint64, 2, true, mul},
{math.MaxUint64, 1, false, mul},
} {
var overflows bool
switch test.op {
Expand Down
3 changes: 3 additions & 0 deletions core/blockchain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
package core

import (
"errors"
"fmt"
gomath "math"
"math/big"
"testing"

Expand Down
2 changes: 1 addition & 1 deletion core/rawdb/freezer_batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ package rawdb

import (
"fmt"
"math"

"github.com/golang/snappy"

"github.com/CortexFoundation/CortexTheseus/common/math"
"github.com/CortexFoundation/CortexTheseus/rlp"
)

Expand Down
2 changes: 1 addition & 1 deletion core/rawdb/freezer_memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ package rawdb
import (
"errors"
"fmt"
"math"
"sync"

"github.com/CortexFoundation/CortexTheseus/common"
"github.com/CortexFoundation/CortexTheseus/common/math"
"github.com/CortexFoundation/CortexTheseus/ctxcdb"
"github.com/CortexFoundation/CortexTheseus/log"
"github.com/CortexFoundation/CortexTheseus/rlp"
Expand Down
2 changes: 1 addition & 1 deletion core/state/snapshot/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ import (
"bytes"
"encoding/binary"
"fmt"
"math"
"math/big"
"time"

"github.com/VictoriaMetrics/fastcache"

"github.com/CortexFoundation/CortexTheseus/common"
"github.com/CortexFoundation/CortexTheseus/common/math"
"github.com/CortexFoundation/CortexTheseus/core/rawdb"
"github.com/CortexFoundation/CortexTheseus/core/types"
"github.com/CortexFoundation/CortexTheseus/ctxcdb"
Expand Down
1 change: 1 addition & 0 deletions core/types/block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"bytes"
"encoding/binary"
"hash"
gomath "math"
"math/big"
"reflect"
"testing"
Expand Down
3 changes: 2 additions & 1 deletion core/vm/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@
package vm

import (
"math"

"github.com/holiman/uint256"

"github.com/CortexFoundation/CortexTheseus/common"
"github.com/CortexFoundation/CortexTheseus/common/math"
)

// calcMemSize64 calculates the required memory size, and returns
Expand Down
5 changes: 3 additions & 2 deletions core/vm/contracts.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"encoding/binary"
"errors"
"fmt"
gomath "math"
"math/big"

"github.com/consensys/gnark-crypto/ecc"
Expand Down Expand Up @@ -379,7 +380,7 @@ func (c *bigModExp) RequiredGas(input []byte) uint64 {
// 2. Different divisor (`GQUADDIVISOR`) (3)
gas.Div(gas, big3)
if gas.BitLen() > 64 {
return math.MaxUint64
return gomath.MaxUint64
}
// 3. Minimum price of 200 gas
if gas.Uint64() < 200 {
Expand All @@ -392,7 +393,7 @@ func (c *bigModExp) RequiredGas(input []byte) uint64 {
gas.Div(gas, big20)

if gas.BitLen() > 64 {
return math.MaxUint64
return gomath.MaxUint64
}
return gas.Uint64()
}
Expand Down
2 changes: 1 addition & 1 deletion internal/ctxcapi/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"context"
"errors"
"fmt"
"math"
"math/big"
"time"

Expand All @@ -29,7 +30,6 @@ import (
"github.com/CortexFoundation/CortexTheseus/accounts/keystore"
"github.com/CortexFoundation/CortexTheseus/common"
"github.com/CortexFoundation/CortexTheseus/common/hexutil"
"github.com/CortexFoundation/CortexTheseus/common/math"
"github.com/CortexFoundation/CortexTheseus/consensus/cuckoo"
"github.com/CortexFoundation/CortexTheseus/core"
"github.com/CortexFoundation/CortexTheseus/core/rawdb"
Expand Down
4 changes: 4 additions & 0 deletions params/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,13 @@
package params

import (
"math"
"math/big"
"reflect"
"testing"
"time"

"github.com/stretchr/testify/require"
)

func TestCheckCompatible(t *testing.T) {
Expand Down
3 changes: 2 additions & 1 deletion rlp/decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"errors"
"fmt"
"io"
gomath "math"
"math/big"
"reflect"
"strings"
Expand Down Expand Up @@ -546,7 +547,7 @@ var decodeTests = []decodeTest{
// uint256
{input: "80", ptr: new(*uint256.Int), value: uint256.NewInt(0)},
{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: "10", ptr: new(uint256.Int), value: *uint256.NewInt(16)}, // non-pointer also works

Expand Down
2 changes: 1 addition & 1 deletion rpc/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ package rpc

import (
"encoding/json"
"math"
"reflect"
"testing"

"github.com/CortexFoundation/CortexTheseus/common"
"github.com/CortexFoundation/CortexTheseus/common/math"
)

func TestBlockNumberJSONUnmarshal(t *testing.T) {
Expand Down

0 comments on commit aec0024

Please sign in to comment.