Skip to content

Commit

Permalink
SerializableInt: remove calls to copy underlying big.Int (#1573)
Browse files Browse the repository at this point in the history
  • Loading branch information
BrendanChou authored May 23, 2024
1 parent 5f1c22b commit 212456e
Show file tree
Hide file tree
Showing 13 changed files with 24 additions and 17 deletions.
2 changes: 1 addition & 1 deletion protocol/app/upgrades/v5.0.0/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func perpetualsUpgrade(
subaccounts := subaccountsKeeper.GetAllSubaccount(ctx)
for _, sa := range subaccounts {
for _, perpPosition := range sa.PerpetualPositions {
if perpPosition.Quantums.BigInt().Sign() <= 0 {
if perpPosition.Quantums.Sign() <= 0 {
// Only record positive positions for total open interest.
// Total negative position size should be equal to total positive position size.
continue
Expand Down
12 changes: 10 additions & 2 deletions protocol/dtypes/serializable_int.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func (i SerializableInt) String() string {
if i.IsNil() {
return "nil"
}
return i.BigInt().String()
return i.i.String()
}

// Cmp compares x and y and returns:
Expand All @@ -77,7 +77,15 @@ func (i SerializableInt) Cmp(j SerializableInt) int {
if j.IsNil() {
return 1
}
return i.BigInt().Cmp(j.BigInt())
return i.i.Cmp(j.i)
}

// Sign returns zero if nil, otherwise returns the sign of the Int.
func (i SerializableInt) Sign() int {
if i.IsNil() {
return 0
}
return i.i.Sign()
}

// Marshal implements the gogo proto custom type interface.
Expand Down
2 changes: 1 addition & 1 deletion protocol/x/clob/types/equity_tier_limit_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func (l EquityTierLimit) validate(field string, maxOrders uint32) error {
l,
)
}
if l.UsdTncRequired.IsNil() || l.UsdTncRequired.BigInt().Sign() < 0 {
if l.UsdTncRequired.IsNil() || l.UsdTncRequired.Sign() < 0 {
return errorsmod.Wrapf(
ErrInvalidEquityTierLimitConfig,
"%d is not a valid UsdTncRequired for %s equity tier limit %+v",
Expand Down
3 changes: 1 addition & 2 deletions protocol/x/govplus/types/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package types

import (
"fmt"
"math/big"

errorsmod "cosmossdk.io/errors"
"cosmossdk.io/math"
Expand Down Expand Up @@ -33,7 +32,7 @@ func (msg *MsgSlashValidator) ValidateBasic() error {
)
}

if msg.TokensAtInfractionHeight.BigInt().Cmp(big.NewInt(0)) != 1 {
if msg.TokensAtInfractionHeight.Sign() <= 0 {
return ErrInvalidTokensAtInfractionHeight
}

Expand Down
2 changes: 1 addition & 1 deletion protocol/x/perpetuals/simulation/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func TestRandomizedGenState(t *testing.T) {
require.True(t, perp.Params.DefaultFundingPpm > -int32(lib.OneMillion))
require.True(t, perp.Params.DefaultFundingPpm < int32(lib.OneMillion))

require.True(t, perp.FundingIndex.BigInt().Sign() == 0)
require.True(t, perp.FundingIndex.Sign() == 0)
}
}
}
2 changes: 1 addition & 1 deletion protocol/x/ratelimit/types/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func (p *LimitParams) Validate() error {
return ErrInvalidRateLimitPeriod
}

if limiter.BaselineMinimum.BigInt().Sign() <= 0 {
if limiter.BaselineMinimum.Sign() <= 0 {
return ErrInvalidBaselineMinimum
}

Expand Down
2 changes: 1 addition & 1 deletion protocol/x/rewards/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ func (k Keeper) SetRewardShare(
ctx sdk.Context,
rewardShare types.RewardShare,
) error {
if rewardShare.Weight.BigInt().Cmp(lib.BigInt0()) <= 0 {
if rewardShare.Weight.Sign() <= 0 {
return errorsmod.Wrapf(
types.ErrNonpositiveWeight,
"Invalid weight %v",
Expand Down
2 changes: 1 addition & 1 deletion protocol/x/subaccounts/keeper/subaccount_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ func UpdateAssetPositions(
ap.Quantums = dtypes.NewIntFromBigInt(newQuantums)

// Handle the case where the position is now closed.
if ap.Quantums.BigInt().Sign() == 0 {
if ap.Quantums.Sign() == 0 {
delete(assetPositionsMap, au.AssetId)
}
} else {
Expand Down
4 changes: 2 additions & 2 deletions protocol/x/subaccounts/types/position_size.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (m *AssetPosition) GetBigQuantums() *big.Int {
return new(big.Int)
}

if m.Quantums.BigInt().Sign() == 0 {
if m.Quantums.Sign() == 0 {
panic(errorsmod.Wrapf(
ErrAssetPositionZeroQuantum,
"asset position (asset Id: %v) has zero quantum",
Expand Down Expand Up @@ -92,7 +92,7 @@ func (m *PerpetualPosition) GetBigQuantums() *big.Int {
return new(big.Int)
}

if m.Quantums.BigInt().Sign() == 0 {
if m.Quantums.Sign() == 0 {
panic(errorsmod.Wrapf(
ErrPerpPositionZeroQuantum,
"perpetual position (perpetual Id: %v) has zero quantum",
Expand Down
2 changes: 1 addition & 1 deletion protocol/x/vault/keeper/orders.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (k Keeper) RefreshAllVaultOrders(ctx sdk.Context) {
k.cdc.MustUnmarshal(totalSharesIterator.Value(), &totalShares)

// Skip if TotalShares is non-positive.
if totalShares.NumShares.BigInt().Sign() <= 0 {
if totalShares.NumShares.Sign() <= 0 {
continue
}

Expand Down
4 changes: 2 additions & 2 deletions protocol/x/vault/keeper/shares.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (k Keeper) SetTotalShares(
vaultId types.VaultId,
totalShares types.NumShares,
) error {
if totalShares.NumShares.BigInt().Sign() < 0 {
if totalShares.NumShares.Sign() < 0 {
return types.ErrNegativeShares
}

Expand Down Expand Up @@ -81,7 +81,7 @@ func (k Keeper) SetOwnerShares(
owner string,
ownerShares types.NumShares,
) error {
if ownerShares.NumShares.BigInt().Sign() < 0 {
if ownerShares.NumShares.Sign() < 0 {
return types.ErrNegativeShares
}

Expand Down
2 changes: 1 addition & 1 deletion protocol/x/vault/keeper/vault.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (k Keeper) DecommissionNonPositiveEquityVaults(
k.cdc.MustUnmarshal(totalSharesIterator.Value(), &totalShares)

// Skip if TotalShares is non-positive.
if totalShares.NumShares.BigInt().Sign() <= 0 {
if totalShares.NumShares.Sign() <= 0 {
continue
}

Expand Down
2 changes: 1 addition & 1 deletion protocol/x/vault/types/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func (p Params) Validate() error {
return ErrInvalidOrderExpirationSeconds
}
// Activation threshold quote quantums must be non-negative.
if p.ActivationThresholdQuoteQuantums.BigInt().Sign() < 0 {
if p.ActivationThresholdQuoteQuantums.Sign() < 0 {
return ErrInvalidActivationThresholdQuoteQuantums
}

Expand Down

0 comments on commit 212456e

Please sign in to comment.