Skip to content

Commit cb68a1b

Browse files
mmsqealjo242
andauthored
fix(test): avoid mutation of global int256 variable (#842)
* fix(test): avoid mutation of global int256 variable * cleanup reserve when erc20 precompile acct don't have balance * Revert "test: adjusts precisebank module account balance to expected amount (#837)" This reverts commit 381a354. * cleanup genesis * cleanup --------- Co-authored-by: Alex | Cosmos Labs <[email protected]>
1 parent 5ca1990 commit cb68a1b

File tree

4 files changed

+35
-42
lines changed

4 files changed

+35
-42
lines changed

tests/integration/x/erc20/test_dynamic_precompiles.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ func (s *KeeperTestSuite) TestRegisterERC20CodeHash() {
111111
s.Require().Equal(nonce, acc.Nonce)
112112
if tc.vesting {
113113
totalBalance = s.network.App.GetBankKeeper().GetBalance(ctx, account.Bytes(), s.network.GetBaseDenom()).Amount
114-
s.Require().Equal(totalBalance.BigInt(), common.U2560.Add(balance, balance).ToBig())
114+
s.Require().Equal(totalBalance.BigInt(), uint256.NewInt(0).Add(balance, balance).ToBig())
115115
}
116116
} else {
117117
s.Require().Equal(common.U2560, acc.Balance)
@@ -128,7 +128,7 @@ func (s *KeeperTestSuite) TestRegisterERC20CodeHash() {
128128
s.Require().Equal(nonce, acc.Nonce)
129129
if tc.vesting {
130130
totalBalance = s.network.App.GetBankKeeper().GetBalance(ctx, account.Bytes(), s.network.GetBaseDenom()).Amount
131-
s.Require().Equal(totalBalance.BigInt(), common.U2560.Add(balance, balance).ToBig())
131+
s.Require().Equal(totalBalance.BigInt(), uint256.NewInt(0).Add(balance, balance).ToBig())
132132
}
133133
} else {
134134
s.Require().Equal(common.U2560, acc.Balance)

tests/integration/x/precisebank/test_genesis.go

Lines changed: 29 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -48,29 +48,6 @@ func (s *GenesisTestSuite) SetupTestWithChainID(chainID testconstants.ChainID) {
4848
})
4949
}
5050

51-
func (s *GenesisTestSuite) adjustModuleBalance(expectedAmt sdkmath.Int) {
52-
moduleAddr := s.network.App.GetAccountKeeper().GetModuleAddress(types.ModuleName)
53-
balance := s.network.App.GetBankKeeper().GetBalance(s.network.GetContext(), moduleAddr, types.IntegerCoinDenom())
54-
55-
if balance.Amount.GT(expectedAmt) {
56-
// Burn excess
57-
err := s.network.App.GetBankKeeper().BurnCoins(
58-
s.network.GetContext(),
59-
types.ModuleName,
60-
sdk.NewCoins(sdk.NewCoin(types.IntegerCoinDenom(), balance.Amount.Sub(expectedAmt))),
61-
)
62-
s.Require().NoError(err)
63-
} else if balance.Amount.LT(expectedAmt) {
64-
// Mint deficit
65-
err := s.network.App.GetBankKeeper().MintCoins(
66-
s.network.GetContext(),
67-
types.ModuleName,
68-
sdk.NewCoins(sdk.NewCoin(types.IntegerCoinDenom(), expectedAmt.Sub(balance.Amount))),
69-
)
70-
s.Require().NoError(err)
71-
}
72-
}
73-
7451
func (s *GenesisTestSuite) TestInitGenesis() {
7552
tests := []struct {
7653
name string
@@ -93,9 +70,13 @@ func (s *GenesisTestSuite) TestInitGenesis() {
9370
{
9471
"valid - module balance matches non-zero amount",
9572
func() {
96-
// The network setup creates an initial balance of 1, so we need to mint 1 more
97-
// to get to the expected amount of 2 for this test case
98-
s.adjustModuleBalance(sdkmath.NewInt(2))
73+
// Mint the expected amount of 2 integer coins to back the fractional balances
74+
err := s.network.App.GetBankKeeper().MintCoins(
75+
s.network.GetContext(),
76+
types.ModuleName,
77+
sdk.NewCoins(sdk.NewCoin(types.IntegerCoinDenom(), sdkmath.NewInt(2))),
78+
)
79+
s.Require().NoError(err)
9980
},
10081
types.NewGenesisState(
10182
types.FractionalBalances{
@@ -123,9 +104,7 @@ func (s *GenesisTestSuite) TestInitGenesis() {
123104
{
124105
"invalid - module balance insufficient",
125106
func() {
126-
// The network setup creates an initial balance of 1, so we need to burn that
127-
// to get to 0 balance for this test case
128-
s.adjustModuleBalance(sdkmath.ZeroInt())
107+
// Module account starts with 0 balance (no setup needed)
129108
},
130109
types.NewGenesisState(
131110
types.FractionalBalances{
@@ -141,9 +120,13 @@ func (s *GenesisTestSuite) TestInitGenesis() {
141120
{
142121
"invalid - module balance excessive",
143122
func() {
144-
// The network setup creates an initial balance of 1, so we need to mint 99 more
145-
// to get to 100 total balance for this test case
146-
s.adjustModuleBalance(sdkmath.NewInt(100))
123+
// Mint 100 integer coins (excessive for expected 2)
124+
err := s.network.App.GetBankKeeper().MintCoins(
125+
s.network.GetContext(),
126+
types.ModuleName,
127+
sdk.NewCoins(sdk.NewCoin(types.IntegerCoinDenom(), sdkmath.NewInt(100))),
128+
)
129+
s.Require().NoError(err)
147130
},
148131
types.NewGenesisState(
149132
types.FractionalBalances{
@@ -242,8 +225,13 @@ func (s *GenesisTestSuite) TestExportGenesis() {
242225
{
243226
"balances, no remainder",
244227
func() *types.GenesisState {
245-
// Burn the initial balance created by network setup, then mint the expected amount
246-
s.adjustModuleBalance(sdkmath.NewInt(1))
228+
// Mint the expected amount to back the fractional balances
229+
err := s.network.App.GetBankKeeper().MintCoins(
230+
s.network.GetContext(),
231+
types.ModuleName,
232+
sdk.NewCoins(sdk.NewCoin(types.IntegerCoinDenom(), sdkmath.NewInt(1))),
233+
)
234+
s.Require().NoError(err)
247235

248236
return types.NewGenesisState(
249237
types.FractionalBalances{
@@ -257,8 +245,13 @@ func (s *GenesisTestSuite) TestExportGenesis() {
257245
{
258246
"balances, remainder",
259247
func() *types.GenesisState {
260-
// Burn the initial balance created by network setup, then mint the expected amount
261-
s.adjustModuleBalance(sdkmath.NewInt(1))
248+
// Mint the expected amount to back the fractional balances
249+
err := s.network.App.GetBankKeeper().MintCoins(
250+
s.network.GetContext(),
251+
types.ModuleName,
252+
sdk.NewCoins(sdk.NewCoin(types.IntegerCoinDenom(), sdkmath.NewInt(1))),
253+
)
254+
s.Require().NoError(err)
262255

263256
return types.NewGenesisState(
264257
types.FractionalBalances{

tests/integration/x/precisebank/test_view_integration.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,9 @@ func (s *KeeperIntegrationTestSuite) TestKeeperHiddenReserve() {
139139
// Check underlying x/bank balance for reserve
140140
reserveIntCoin := s.network.App.GetBankKeeper().GetBalance(s.network.GetContext(), moduleAddr, types.IntegerCoinDenom())
141141
s.Require().Equal(
142-
sdkmath.NewInt(2), // Network setup creates 1, test mints 1 more = 2 total
142+
sdkmath.NewInt(1),
143143
reserveIntCoin.Amount,
144-
"reserve should hold 2 integer coins (1 from network setup + 1 from test mint)",
144+
"reserve should hold 1 integer coin from test mint",
145145
)
146146

147147
tests := []struct {
@@ -160,7 +160,7 @@ func (s *KeeperIntegrationTestSuite) TestKeeperHiddenReserve() {
160160
"reserve account - visible integer denom",
161161
moduleAddr,
162162
types.IntegerCoinDenom(),
163-
sdkmath.NewInt(2), // Network setup creates 1, test mints 1 more = 2 total
163+
sdkmath.NewInt(1), // 1 integer coin from test mint
164164
},
165165
{
166166
"user account - visible extended denom",

tests/integration/x/vm/test_statedb.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -984,7 +984,7 @@ func (s *KeeperTestSuite) TestSetBalance() {
984984
},
985985
false,
986986
func() *uint256.Int {
987-
return common.U2560.Add(totalBalance, amount)
987+
return uint256.NewInt(0).Add(totalBalance, amount)
988988
},
989989
},
990990
{

0 commit comments

Comments
 (0)