From 65d091d458ee2a621558a55fda6cda8dd0efa5f0 Mon Sep 17 00:00:00 2001 From: drklee3 Date: Wed, 21 Aug 2024 17:29:04 -0700 Subject: [PATCH] fix(x/precisebank): Avoid blocked addr error on SendCoinsFromAccountToModule (#2012) --- x/precisebank/keeper/send.go | 11 +++- x/precisebank/keeper/send_integration_test.go | 60 +++++++++++++++++++ 2 files changed, 69 insertions(+), 2 deletions(-) diff --git a/x/precisebank/keeper/send.go b/x/precisebank/keeper/send.go index 6d8c04bd..fd383419 100644 --- a/x/precisebank/keeper/send.go +++ b/x/precisebank/keeper/send.go @@ -179,10 +179,17 @@ func (k Keeper) sendExtendedCoins( // Always send carry from reserve before receiving borrow from sender to // ensure reserve always has sufficient balance starting from 0. if !senderNeedsBorrow && recipientNeedsCarry { + reserveAddr := k.ak.GetModuleAddress(types.ModuleName) + + // We use SendCoins instead of SendCoinsFromModuleToAccount to avoid + // the blocked addrs check. Blocked accounts should not be checked in + // a SendCoins operation. Only SendCoinsFromModuleToAccount should check + // blocked addrs which is done by the parent SendCoinsFromModuleToAccount + // method. carryCoin := sdk.NewCoin(types.IntegerCoinDenom, sdk.NewInt(1)) - if err := k.bk.SendCoinsFromModuleToAccount( + if err := k.bk.SendCoins( ctx, - types.ModuleName, + reserveAddr, to, // recipient carrying sdk.NewCoins(carryCoin), ); err != nil { diff --git a/x/precisebank/keeper/send_integration_test.go b/x/precisebank/keeper/send_integration_test.go index 8174c76b..08c17421 100644 --- a/x/precisebank/keeper/send_integration_test.go +++ b/x/precisebank/keeper/send_integration_test.go @@ -5,6 +5,7 @@ import ( "testing" sdk "github.com/cosmos/cosmos-sdk/types" + authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" minttypes "github.com/cosmos/cosmos-sdk/x/mint/types" "github.com/kava-labs/kava/app" @@ -622,6 +623,65 @@ func (suite *sendIntegrationTestSuite) TestSendCoinsFromAccountToModule() { ) } +func (suite *sendIntegrationTestSuite) TestSendCoinsFromAccountToModule_BlockedRecipientCarry() { + // Carrying to module account balance. This tests that SendCoinsFromAccountToModule + // does not fail when sending to a blocked module account. + + sender := sdk.AccAddress([]byte{1}) + + sendAmt := cs(c(types.ExtendedCoinDenom, 1000)) + sendAmt2 := cs(ci(types.ExtendedCoinDenom, types.ConversionFactor().SubRaw(10))) + + suite.MintToAccount(sender, sendAmt.Add(sendAmt2...)) + + err := suite.Keeper.SendCoinsFromAccountToModule( + suite.Ctx, + sender, + authtypes.FeeCollectorName, + sendAmt, + ) + suite.Require().NoError(err) + + // Trigger carry for fee_collector module account + err = suite.Keeper.SendCoinsFromAccountToModule( + suite.Ctx, + sender, + authtypes.FeeCollectorName, + sendAmt2, + ) + suite.Require().NoError(err) +} + +func (suite *sendIntegrationTestSuite) TestSendCoins_BlockedRecipientCarry() { + // Same test as TestSendCoinsFromModuleToAccount_Blocked, but with SendCoins + // which also should not fail when sending to a blocked module account. + sender := sdk.AccAddress([]byte{1}) + + sendAmt := cs(c(types.ExtendedCoinDenom, 1000)) + sendAmt2 := cs(ci(types.ExtendedCoinDenom, types.ConversionFactor().SubRaw(10))) + + suite.MintToAccount(sender, sendAmt.Add(sendAmt2...)) + + recipient := suite.App.GetAccountKeeper().GetModuleAddress(authtypes.FeeCollectorName) + + err := suite.Keeper.SendCoins( + suite.Ctx, + sender, + recipient, + sendAmt, + ) + suite.Require().NoError(err) + + // Trigger carry for fee_collector module account + err = suite.Keeper.SendCoins( + suite.Ctx, + sender, + recipient, + sendAmt2, + ) + suite.Require().NoError(err) +} + func (suite *sendIntegrationTestSuite) TestSendCoinsFromModuleToAccount() { // Ensure sender correctly matches the specified module account. Opposite // of SendCoinsFromAccountToModule, so we are only checking the correct