Skip to content

Commit

Permalink
refactor: use cosmossdk.io/math (tendermint#941)
Browse files Browse the repository at this point in the history
* replace usages

* remaining imports

* format

* update go mod

* re-format
  • Loading branch information
Alex Johnson authored Aug 30, 2022
1 parent 7a5771d commit 10a2cfe
Show file tree
Hide file tree
Showing 71 changed files with 360 additions and 305 deletions.
7 changes: 4 additions & 3 deletions app/simutil/app_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"math/rand"
"time"

sdkmath "cosmossdk.io/math"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/simapp"
simappparams "github.com/cosmos/cosmos-sdk/simapp/params"
Expand Down Expand Up @@ -68,7 +69,7 @@ func CustomAppStateFn(cdc codec.JSONCodec, simManager *module.SimulationManager)
Rand: r,
GenState: genesisState,
Accounts: accs,
InitialStake: sdk.NewInt(initialStake),
InitialStake: sdkmath.NewInt(initialStake),
NumBonded: numInitiallyBonded,
GenTimestamp: genesisTimestamp,
}
Expand Down Expand Up @@ -96,7 +97,7 @@ func CustomAppStateFn(cdc codec.JSONCodec, simManager *module.SimulationManager)
panic(err)
}
// compute not bonded balance
notBondedTokens := sdk.ZeroInt()
notBondedTokens := sdkmath.ZeroInt()
for _, val := range stakingState.Validators {
if val.Status != stakingtypes.Unbonded {
continue
Expand All @@ -120,7 +121,7 @@ func CustomAppStateFn(cdc codec.JSONCodec, simManager *module.SimulationManager)
for i, balance := range bankState.Balances {
if r.Int63n(100) < 20 {
auctionCoinAmt := r.Int63n(maxInitialCoin)
auctionCoin := sdk.NewCoin(AuctionCoinDenom, sdk.NewInt(auctionCoinAmt))
auctionCoin := sdk.NewCoin(AuctionCoinDenom, sdkmath.NewInt(auctionCoinAmt))
newBalance := balance.Coins.Add(auctionCoin)
bankState.Balances[i].Coins = newBalance
totalNewCoins = totalNewCoins.Add(auctionCoin)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/tendermint/spn
go 1.18

require (
cosmossdk.io/math v1.0.0-beta.3
cosmossdk.io/errors v1.0.0-beta.7
github.com/cosmos/cosmos-sdk v0.46.1
github.com/cosmos/ibc-go/v5 v5.0.0-rc0
Expand Down Expand Up @@ -30,7 +31,6 @@ require (
cloud.google.com/go/compute v1.7.0 // indirect
cloud.google.com/go/iam v0.3.0 // indirect
cloud.google.com/go/storage v1.22.1 // indirect
cosmossdk.io/math v1.0.0-beta.3 // indirect
filippo.io/edwards25519 v1.0.0-rc.1 // indirect
github.com/99designs/go-keychain v0.0.0-20191008050251-8e49817e8af4 // indirect
github.com/99designs/keyring v1.2.1 // indirect
Expand Down
5 changes: 3 additions & 2 deletions pkg/types/signature_counts.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package types
import (
"fmt"

sdkmath "cosmossdk.io/math"
"github.com/cosmos/cosmos-sdk/types/bech32"
"github.com/pkg/errors"

Expand All @@ -18,7 +19,7 @@ func NewSignatureCounts() SignatureCounts {
// AddSignature adds a signature for the consensus address at a specific validator set size
func (m *SignatureCounts) AddSignature(opAddress string, validatorSetSize int64) {
// relative signature is the signature relative to the validator set size
relSignature := sdk.OneDec().QuoInt(sdk.NewInt(validatorSetSize))
relSignature := sdk.OneDec().QuoInt(sdkmath.NewInt(validatorSetSize))

// search for the consensus address
for i, c := range m.Counts {
Expand Down Expand Up @@ -57,7 +58,7 @@ func (m SignatureCounts) Validate() error {
sumSig = sumSig.Add(sc.RelativeSignatures)
}

blockCountDec := sdk.NewDecFromInt(sdk.NewIntFromUint64(m.BlockCount))
blockCountDec := sdk.NewDecFromInt(sdkmath.NewIntFromUint64(m.BlockCount))
if sumSig.GT(blockCountDec) {
return fmt.Errorf(
"sum of relative signatures is higher than block number %s > %s",
Expand Down
5 changes: 3 additions & 2 deletions testutil/gen_app.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package testutil
import (
"time"

sdkmath "cosmossdk.io/math"
"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
Expand Down Expand Up @@ -49,7 +50,7 @@ func GenApp(withGenesis bool, invCheckPeriod uint) (*spnapp.App, spnapp.GenesisS
acc := authtypes.NewBaseAccount(senderPrivKey.PubKey().Address().Bytes(), senderPrivKey.PubKey(), 0, 0)
balances := banktypes.Balance{
Address: acc.GetAddress().String(),
Coins: sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(100000000000000))),
Coins: sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdkmath.NewInt(100000000000000))),
}
genesisState = genesisStateWithValSet(encCdc.Marshaler, genesisState, valSet, []authtypes.GenesisAccount{acc}, balances)
return originalApp, genesisState
Expand Down Expand Up @@ -87,7 +88,7 @@ func genesisStateWithValSet(
UnbondingHeight: int64(0),
UnbondingTime: time.Unix(0, 0).UTC(),
Commission: stakingtypes.NewCommission(sdk.ZeroDec(), sdk.ZeroDec(), sdk.ZeroDec()),
MinSelfDelegation: sdk.ZeroInt(),
MinSelfDelegation: sdkmath.ZeroInt(),
}
validators = append(validators, validator)
delegations = append(delegations, stakingtypes.NewDelegation(genAccs[0].GetAddress(), val.Address.Bytes(), sdk.OneDec()))
Expand Down
3 changes: 2 additions & 1 deletion testutil/sample/campaign.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package sample
import (
"math/rand"

sdkmath "cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"

spntypes "github.com/tendermint/spn/pkg/types"
Expand All @@ -29,7 +30,7 @@ func ShareVestingOptions(r *rand.Rand) campaign.ShareVestingOptions {
// Voucher returns a sample voucher structure
func Voucher(r *rand.Rand, campaignID uint64) sdk.Coin {
denom := campaign.VoucherDenom(campaignID, AlphaString(r, 5))
return sdk.NewCoin(denom, sdk.NewInt(int64(r.Intn(10000)+1)))
return sdk.NewCoin(denom, sdkmath.NewInt(int64(r.Intn(10000)+1)))
}

// Vouchers returns a sample vouchers structure
Expand Down
3 changes: 2 additions & 1 deletion testutil/sample/claim.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package sample
import (
"math/rand"

sdkmath "cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"

claim "github.com/tendermint/spn/x/claim/types"
Expand All @@ -11,7 +12,7 @@ import (
func ClaimRecord(r *rand.Rand) claim.ClaimRecord {
return claim.ClaimRecord{
Address: Address(r),
Claimable: sdk.NewInt(r.Int63n(100000)),
Claimable: sdkmath.NewInt(r.Int63n(100000)),
CompletedMissions: uint64Sequence(r),
}
}
Expand Down
16 changes: 8 additions & 8 deletions testutil/sample/participation.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@ import (
"math/rand"
"time"

sdk "github.com/cosmos/cosmos-sdk/types"
sdkmath "cosmossdk.io/math"

participation "github.com/tendermint/spn/x/participation/types"
)

// ParticipationParams returns a sample of params for the participation module
func ParticipationParams(r *rand.Rand) participation.Params {
allocationPrice := participation.AllocationPrice{
Bonded: sdk.NewInt(r.Int63n(10000) + 1),
Bonded: sdkmath.NewInt(r.Int63n(10000) + 1),
}

tiers := make([]participation.Tier, 0)
numTiers := uint64(r.Int63n(10) + 1)
allocCnt := sdk.NewInt(r.Int63n(5) + 1)
maxBidCnt := sdk.NewInt(r.Int63n(10000) + 1)
allocCnt := sdkmath.NewInt(r.Int63n(5) + 1)
maxBidCnt := sdkmath.NewInt(r.Int63n(10000) + 1)
for i := uint64(1); i <= numTiers; i++ {
tier := participation.Tier{
TierID: i,
Expand All @@ -30,7 +30,7 @@ func ParticipationParams(r *rand.Rand) participation.Params {
tiers = append(tiers, tier)

// increment values for next tier
allocCnt = allocCnt.Add(sdk.NewInt(r.Int63n(5) + 1))
allocCnt = allocCnt.Add(sdkmath.NewInt(r.Int63n(5) + 1))
maxBidCnt = maxBidCnt.AddRaw(r.Int63n(10000) + 1)
}

Expand All @@ -57,17 +57,17 @@ func ParticipationGenesisStateWithAllocations(r *rand.Rand) participation.Genesi
addr := Address(r)
usedAllocs := participation.UsedAllocations{
Address: addr,
NumAllocations: sdk.ZeroInt(),
NumAllocations: sdkmath.ZeroInt(),
}
for j := 0; j < 3; j++ {
auctionUsedAllocs := participation.AuctionUsedAllocations{
Address: addr,
AuctionID: uint64(j),
NumAllocations: sdk.NewInt(r.Int63n(5) + 1),
NumAllocations: sdkmath.NewInt(r.Int63n(5) + 1),
Withdrawn: false,
}
genState.AuctionUsedAllocationsList = append(genState.AuctionUsedAllocationsList, auctionUsedAllocs)
usedAllocs.NumAllocations = usedAllocs.NumAllocations.Mul(sdk.NewInt(2))
usedAllocs.NumAllocations = usedAllocs.NumAllocations.Mul(sdkmath.NewInt(2))
}
genState.UsedAllocationsList = append(genState.UsedAllocationsList, usedAllocs)
}
Expand Down
13 changes: 7 additions & 6 deletions testutil/sample/sample.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"testing"
"time"

sdkmath "cosmossdk.io/math"
"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
Expand Down Expand Up @@ -146,19 +147,19 @@ func Delegation(t testing.TB, r *rand.Rand, addr string) stakingtypes.Delegation

// Coin returns a sample coin structure
func Coin(r *rand.Rand) sdk.Coin {
return sdk.NewCoin(AlphaString(r, 5), sdk.NewInt(r.Int63n(10000)+1))
return sdk.NewCoin(AlphaString(r, 5), sdkmath.NewInt(r.Int63n(10000)+1))
}

// CoinWithRange returns a sample coin structure where the amount is a random number between provided min and max values
// with a random denom
func CoinWithRange(r *rand.Rand, min, max int64) sdk.Coin {
return sdk.NewCoin(AlphaString(r, 5), sdk.NewInt(r.Int63n(max-min)+min))
return sdk.NewCoin(AlphaString(r, 5), sdkmath.NewInt(r.Int63n(max-min)+min))
}

// CoinWithRangeAmount returns a sample coin structure where the amount is a random number between provided min and max values
// with a given denom
func CoinWithRangeAmount(r *rand.Rand, denom string, min, max int64) sdk.Coin {
return sdk.NewCoin(denom, sdk.NewInt(r.Int63n(max-min)+min))
return sdk.NewCoin(denom, sdkmath.NewInt(r.Int63n(max-min)+min))
}

// Coins returns a sample coins structure
Expand Down Expand Up @@ -193,7 +194,7 @@ func DurationFromRange(r *rand.Rand, min, max time.Duration) time.Duration {
return time.Duration(r.Int63n(int64(max-min))) + min
}

// Int returns a sample sdk.Int
func Int(r *rand.Rand) sdk.Int {
return sdk.NewInt(r.Int63())
// Int returns a sample sdkmath.Int
func Int(r *rand.Rand) sdkmath.Int {
return sdkmath.NewInt(r.Int63())
}
7 changes: 4 additions & 3 deletions x/campaign/keeper/campaign_auction_event_hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"time"

sdkerrors "cosmossdk.io/errors"
sdkmath "cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"

fundraisingtypes "github.com/tendermint/fundraising/x/fundraising/types"
Expand Down Expand Up @@ -186,15 +187,15 @@ func (h CampaignAuctionEventHooks) BeforeAllowedBidderUpdated(
_ sdk.Context,
_ uint64,
_ sdk.AccAddress,
_ sdk.Int,
_ sdkmath.Int,
) {
}

// BeforeSellingCoinsAllocated implements FundraisingHooks
func (h CampaignAuctionEventHooks) BeforeSellingCoinsAllocated(
_ sdk.Context,
_ uint64,
_ map[string]sdk.Int,
_ map[string]sdk.Int,
_ map[string]sdkmath.Int,
_ map[string]sdkmath.Int,
) {
}
3 changes: 2 additions & 1 deletion x/campaign/keeper/msg_burn_vouchers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package keeper_test
import (
"testing"

sdkmath "cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/require"

Expand All @@ -21,7 +22,7 @@ func TestMsgBurnVouchers(t *testing.T) {
campaign = sample.Campaign(r, 0)
addr = sample.AccAddress(r)
vouchersTooBig = sdk.NewCoins(
sdk.NewCoin("v/0/foo", sdk.NewInt(spntypes.TotalShareNumber+1)),
sdk.NewCoin("v/0/foo", sdkmath.NewInt(spntypes.TotalShareNumber+1)),
)
)

Expand Down
3 changes: 2 additions & 1 deletion x/campaign/keeper/msg_mint_vouchers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package keeper_test
import (
"testing"

sdkmath "cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/require"

Expand All @@ -23,7 +24,7 @@ func TestMsgMintVouchers(t *testing.T) {

shares, _ = types.NewShares("1000foo,500bar,300foobar")
sharesTooBig = types.NewSharesFromCoins(sdk.NewCoins(
sdk.NewCoin("foo", sdk.NewInt(spntypes.TotalShareNumber+1)),
sdk.NewCoin("foo", sdkmath.NewInt(spntypes.TotalShareNumber+1)),
))
)

Expand Down
4 changes: 3 additions & 1 deletion x/campaign/keeper/msg_redeem_vouchers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package keeper_test
import (
"testing"

sdkmath "cosmossdk.io/math"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/require"

Expand All @@ -23,7 +25,7 @@ func TestMsgRedeemVouchers(t *testing.T) {
campaign = sample.Campaign(r, 0)
campaignMainnetLaunched = sample.Campaign(r, 1)
vouchersTooBig = sdk.NewCoins(
sdk.NewCoin("v/0/foo", sdk.NewInt(spntypes.TotalShareNumber+1)),
sdk.NewCoin("v/0/foo", sdkmath.NewInt(spntypes.TotalShareNumber+1)),
)
)

Expand Down
3 changes: 2 additions & 1 deletion x/campaign/simulation/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package simulation
import (
"math/rand"

sdkmath "cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"

Expand Down Expand Up @@ -122,7 +123,7 @@ func GetSharesFromCampaign(r *rand.Rand, ctx sdk.Context, k keeper.Keeper, campI
if shareNb > remaining {
shareNb = remaining
}
shares = append(shares, sdk.NewCoin(share, sdk.NewInt(shareNb)))
shares = append(shares, sdk.NewCoin(share, sdkmath.NewInt(shareNb)))
}

// No shares can be distributed
Expand Down
11 changes: 6 additions & 5 deletions x/campaign/types/campaign_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package types_test
import (
"testing"

sdkmath "cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/require"

Expand All @@ -13,8 +14,8 @@ import (

var (
invalidCampaignName = "not_valid"
invalidCoins = sdk.Coins{sdk.Coin{Denom: "invalid denom", Amount: sdk.ZeroInt()}}
invalidShares = campaign.Shares{sdk.Coin{Denom: "invalid denom", Amount: sdk.ZeroInt()}}
invalidCoins = sdk.Coins{sdk.Coin{Denom: "invalid denom", Amount: sdkmath.ZeroInt()}}
invalidShares = campaign.Shares{sdk.Coin{Denom: "invalid denom", Amount: sdkmath.ZeroInt()}}
)

func TestNewCampaign(t *testing.T) {
Expand Down Expand Up @@ -50,7 +51,7 @@ func TestCampaign_Validate(t *testing.T) {

totalSharesReached := sample.Campaign(r, 0)
totalSharesReached.AllocatedShares = campaign.NewSharesFromCoins(sdk.NewCoins(
sdk.NewCoin("foo", sdk.NewInt(spntypes.TotalShareNumber+1)),
sdk.NewCoin("foo", sdkmath.NewInt(spntypes.TotalShareNumber+1)),
))
reached, err := campaign.IsTotalSharesReached(totalSharesReached.AllocatedShares, spntypes.TotalShareNumber)
require.NoError(t, err)
Expand All @@ -59,8 +60,8 @@ func TestCampaign_Validate(t *testing.T) {
invalidSpecialAllocations := campaign.NewSpecialAllocations(
sample.Shares(r),
campaign.Shares(sdk.NewCoins(
sdk.NewCoin("foo", sdk.NewInt(100)),
sdk.NewCoin("s/bar", sdk.NewInt(200)),
sdk.NewCoin("foo", sdkmath.NewInt(100)),
sdk.NewCoin("s/bar", sdkmath.NewInt(200)),
)),
)
require.Error(t, invalidSpecialAllocations.Validate())
Expand Down
6 changes: 3 additions & 3 deletions x/campaign/types/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
"testing"

sdk "github.com/cosmos/cosmos-sdk/types"
sdkmath "cosmossdk.io/math"
"github.com/stretchr/testify/require"

spntypes "github.com/tendermint/spn/pkg/types"
Expand Down Expand Up @@ -274,14 +274,14 @@ func TestGenesisState_ValidateParams(t *testing.T) {
{
desc: "max total supply below min total supply",
genState: types.GenesisState{
Params: types.NewParams(types.DefaultMinTotalSupply, types.DefaultMinTotalSupply.Sub(sdk.OneInt()), types.DefaultCampaignCreationFee),
Params: types.NewParams(types.DefaultMinTotalSupply, types.DefaultMinTotalSupply.Sub(sdkmath.OneInt()), types.DefaultCampaignCreationFee),
},
shouldBeValid: false,
},
{
desc: "valid parameters",
genState: types.GenesisState{
Params: types.NewParams(types.DefaultMinTotalSupply, types.DefaultMinTotalSupply.Add(sdk.OneInt()), types.DefaultCampaignCreationFee),
Params: types.NewParams(types.DefaultMinTotalSupply, types.DefaultMinTotalSupply.Add(sdkmath.OneInt()), types.DefaultCampaignCreationFee),
},
shouldBeValid: true,
},
Expand Down
Loading

0 comments on commit 10a2cfe

Please sign in to comment.