Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove floating point math #145

Merged
merged 8 commits into from
Dec 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion x/storage/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,8 @@ import (
func BeginBlocker(ctx sdk.Context, k keeper.Keeper) {
defer telemetry.ModuleMeasureSince(types.ModuleName, time.Now(), telemetry.MetricKeyBeginBlocker)

k.HandleBlock(ctx)
err := k.HandleRewardBlock(ctx)

Check warning

Code scanning / CodeQL

Panic in BeginBock or EndBlock consensus methods

Possible panics in BeginBock- or EndBlock-related consensus methods could cause a chain halt
if err != nil {
ctx.Logger().Error(err.Error())
}
}
16 changes: 15 additions & 1 deletion x/storage/keeper/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
storetypes "github.com/cosmos/cosmos-sdk/store/types"
typesparams "github.com/cosmos/cosmos-sdk/x/params/types"

authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"

moduletestutil "github.com/jackalLabs/canine-chain/types/module/testutil" // when importing from sdk,'go mod tidy' keeps trying to import from v0.46.
Expand Down Expand Up @@ -41,6 +42,7 @@ func setupStorageKeeper(t *testing.T) (
encCfg := moduletestutil.MakeTestEncodingConfig()
types.RegisterInterfaces(encCfg.InterfaceRegistry)
banktypes.RegisterInterfaces(encCfg.InterfaceRegistry)
authtypes.RegisterInterfaces(encCfg.InterfaceRegistry)

// Create MsgServiceRouter, but don't populate it before creating the storage keeper.
msr := baseapp.NewMsgServiceRouter()
Expand All @@ -50,6 +52,7 @@ func setupStorageKeeper(t *testing.T) (
bankKeeper := storagetestutil.NewMockBankKeeper(ctrl)
accountKeeper := storagetestutil.NewMockAccountKeeper(ctrl)
trackMockBalances(bankKeeper)
accountKeeper.EXPECT().GetModuleAddress(types.ModuleName).Return(modAccount).AnyTimes()

paramsSubspace := typesparams.NewSubspace(encCfg.Codec,
types.Amino,
Expand Down Expand Up @@ -77,7 +80,14 @@ func trackMockBalances(bankKeeper *storagetestutil.MockBankKeeper) {

// We don't track module account balances.
bankKeeper.EXPECT().MintCoins(gomock.Any(), minttypes.ModuleName, gomock.Any()).AnyTimes()
bankKeeper.EXPECT().BurnCoins(gomock.Any(), types.ModuleName, gomock.Any()).AnyTimes()
bankKeeper.EXPECT().BurnCoins(gomock.Any(), types.ModuleName, gomock.Any()).DoAndReturn(func(_ sdk.Context, moduleName string, coins sdk.Coins) error {
newBalance, negative := balances[modAccount.String()].SafeSub(coins)
if negative {
return fmt.Errorf("not enough balance")
}
balances[modAccount.String()] = newBalance
return nil
}).AnyTimes()
bankKeeper.EXPECT().SendCoinsFromModuleToModule(gomock.Any(), minttypes.ModuleName, types.ModuleName, gomock.Any()).AnyTimes()

// But we do track normal account balances.
Expand All @@ -96,4 +106,8 @@ func trackMockBalances(bankKeeper *storagetestutil.MockBankKeeper) {
bankKeeper.EXPECT().GetAllBalances(gomock.Any(), gomock.Any()).DoAndReturn(func(_ sdk.Context, addr sdk.AccAddress) sdk.Coins {
return balances[addr.String()]
}).AnyTimes()
bankKeeper.EXPECT().GetBalance(gomock.Any(), gomock.Any(), gomock.Any()).DoAndReturn(func(_ sdk.Context, addr sdk.AccAddress, denom string) sdk.Coin {
amt := balances[addr.String()].AmountOf(denom)
return sdk.NewCoin(denom, amt)
}).AnyTimes()
}
8 changes: 6 additions & 2 deletions x/storage/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
minttypes "github.com/cosmos/cosmos-sdk/x/mint/types"
storage "github.com/jackalLabs/canine-chain/x/storage"
"github.com/jackalLabs/canine-chain/x/storage/keeper"
Expand All @@ -15,13 +16,16 @@ import (
"github.com/stretchr/testify/suite"
)

var modAccount = authtypes.NewModuleAddress(types.ModuleName)

type KeeperTestSuite struct {
suite.Suite

cdc codec.Codec
ctx sdk.Context
storageKeeper *keeper.Keeper
bankKeeper *storagetestutil.MockBankKeeper
accountKeeper *storagetestutil.MockAccountKeeper
queryClient types.QueryClient
msgSrvr types.MsgServer
}
Expand All @@ -37,8 +41,7 @@ func (suite *KeeperTestSuite) reset() {
types.RegisterQueryServer(queryHelper, storageKeeper)
queryClient := types.NewQueryClient(queryHelper)

// TODO: make accounts in account keeper
_ = accountKeeper
// accountKeeper.EXPECT().GetModuleAccount(gomock.Any(), types.ModuleName).Return(authtypes.NewEmptyModuleAccount(types.ModuleName)).AnyTimes()

coins := sdk.NewCoins(sdk.NewCoin("stake", sdk.NewInt(1000000000)))
err := bankKeeper.MintCoins(ctx, minttypes.ModuleName, coins)
Expand All @@ -49,6 +52,7 @@ func (suite *KeeperTestSuite) reset() {
suite.ctx = ctx
suite.storageKeeper = storageKeeper
suite.bankKeeper = bankKeeper
suite.accountKeeper = accountKeeper
suite.cdc = encCfg.Codec
suite.queryClient = queryClient
suite.msgSrvr = keeper.NewMsgServerImpl(*suite.storageKeeper)
Expand Down
Loading