Skip to content

Commit

Permalink
fix(x/precisebank): Avoid blocked addr error on SendCoinsFromAccountT…
Browse files Browse the repository at this point in the history
…oModule (#2012)
  • Loading branch information
drklee3 authored Aug 22, 2024
1 parent 8023be0 commit 65d091d
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 2 deletions.
11 changes: 9 additions & 2 deletions x/precisebank/keeper/send.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
60 changes: 60 additions & 0 deletions x/precisebank/keeper/send_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 65d091d

Please sign in to comment.