Skip to content

Commit

Permalink
Module account permissions fix (#701)
Browse files Browse the repository at this point in the history
* initial draft

* fix log msg formatting

* fix mod account type

* sync permissions at a block time

* update the update time
  • Loading branch information
rhuairahrighairidh authored Oct 28, 2020
1 parent c02ce61 commit 35a82ac
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 1 deletion.
8 changes: 8 additions & 0 deletions x/bep3/abci.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
package bep3

import (
"fmt"

sdk "github.com/cosmos/cosmos-sdk/types"
)

// BeginBlocker on every block expires outdated atomic swaps and removes closed
// swap from long term storage (default storage time of 1 week)
func BeginBlocker(ctx sdk.Context, k Keeper) {
if ctx.BlockTime().After(ModulePermissionsUpgradeTime) {
err := k.EnsureModuleAccountPermissions(ctx)
if err != nil {
k.Logger(ctx).Error(fmt.Sprintf("couldn't update module account permissions: %v", err))
}
}
k.UpdateTimeBasedSupplyLimits(ctx)
k.UpdateExpiredAtomicSwaps(ctx)
k.DeleteClosedAtomicSwapsFromLongtermStorage(ctx)
Expand Down
1 change: 1 addition & 0 deletions x/bep3/alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ var (
DefaultMinBlockLock = types.DefaultMinBlockLock
DefaultMaxBlockLock = types.DefaultMaxBlockLock
DefaultPreviousBlockTime = types.DefaultPreviousBlockTime
ModulePermissionsUpgradeTime = types.ModulePermissionsUpgradeTime
)

type (
Expand Down
7 changes: 7 additions & 0 deletions x/bep3/keeper/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"time"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth"
authexported "github.com/cosmos/cosmos-sdk/x/auth/exported"

"github.com/tendermint/tendermint/crypto"
tmtime "github.com/tendermint/tendermint/types/time"
Expand All @@ -29,6 +31,11 @@ func c(denom string, amount int64) sdk.Coin { return sdk.NewInt64Coin(denom, amo
func cs(coins ...sdk.Coin) sdk.Coins { return sdk.NewCoins(coins...) }
func ts(minOffset int) int64 { return tmtime.Now().Add(time.Duration(minOffset) * time.Minute).Unix() }

func NewAuthGenStateFromAccs(accounts ...authexported.GenesisAccount) app.GenesisState {
authGenesis := auth.NewGenesisState(auth.DefaultParams(), accounts)
return app.GenesisState{auth.ModuleName: auth.ModuleCdc.MustMarshalJSON(authGenesis)}
}

func NewBep3GenStateMulti(deputyAddress sdk.AccAddress) app.GenesisState {
bep3Genesis := types.GenesisState{
Params: types.Params{
Expand Down
14 changes: 14 additions & 0 deletions x/bep3/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/params/subspace"
"github.com/cosmos/cosmos-sdk/x/supply"
"github.com/tendermint/tendermint/libs/log"

"github.com/kava-labs/kava/x/bep3/types"
Expand Down Expand Up @@ -46,6 +47,19 @@ func (k Keeper) Logger(ctx sdk.Context) log.Logger {
return ctx.Logger().With("module", fmt.Sprintf("x/%s", types.ModuleName))
}

// EnsureModuleAccountPermissions syncs the bep3 module account's permissions with those in the supply keeper.
func (k Keeper) EnsureModuleAccountPermissions(ctx sdk.Context) error {
maccI := k.supplyKeeper.GetModuleAccount(ctx, types.ModuleName)
macc, ok := maccI.(*supply.ModuleAccount)
if !ok {
return fmt.Errorf("expected %s account to be a module account type", types.ModuleName)
}
_, perms := k.supplyKeeper.GetModuleAddressAndPermissions(types.ModuleName)
macc.Permissions = perms
k.supplyKeeper.SetModuleAccount(ctx, macc)
return nil
}

// ------------------------------------------
// Atomic Swaps
// ------------------------------------------
Expand Down
33 changes: 32 additions & 1 deletion x/bep3/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/stretchr/testify/suite"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/supply"

abci "github.com/tendermint/tendermint/abci/types"
tmtime "github.com/tendermint/tendermint/types/time"
Expand All @@ -30,7 +31,6 @@ func (suite *KeeperTestSuite) SetupTest() {
config := sdk.GetConfig()
app.SetBech32AddressPrefixes(config)
suite.ResetChain()
return
}

func (suite *KeeperTestSuite) ResetChain() {
Expand All @@ -43,6 +43,37 @@ func (suite *KeeperTestSuite) ResetChain() {
suite.keeper = keeper
}

func (suite *KeeperTestSuite) TestEnsureModuleAccountPermissions() {
suite.app.InitializeFromGenesisStates(
NewAuthGenStateFromAccs(
supply.NewEmptyModuleAccount(types.ModuleName), // no permisions
),
)
supplyKeeper := suite.app.GetSupplyKeeper()
testCoins := cs(c("busd", 1000_00_000_000))

// Ensure there are no minting and burning permissions.
// This calls mint/burn instead of checking permissions as the supply module can report permissions incorrectly.
// Using a panic check because MintCoins panics when permissions are incorrect.
suite.Panics(func() {
err := supplyKeeper.MintCoins(suite.ctx, types.ModuleName, testCoins)
if err != nil {
panic(err)
}
err = supplyKeeper.BurnCoins(suite.ctx, types.ModuleName, testCoins)
if err != nil {
panic(err)
}
})

err := suite.keeper.EnsureModuleAccountPermissions(suite.ctx)
suite.NoError(err)

err = supplyKeeper.MintCoins(suite.ctx, types.ModuleName, testCoins)
suite.NoError(err)
err = supplyKeeper.BurnCoins(suite.ctx, types.ModuleName, testCoins)
suite.NoError(err)
}
func (suite *KeeperTestSuite) TestGetSetAtomicSwap() {
suite.ResetChain()

Expand Down
2 changes: 2 additions & 0 deletions x/bep3/types/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
type SupplyKeeper interface {
GetModuleAddress(name string) sdk.AccAddress
GetModuleAccount(ctx sdk.Context, moduleName string) supplyexported.ModuleAccountI
GetModuleAddressAndPermissions(moduleName string) (sdk.AccAddress, []string)
SetModuleAccount(ctx sdk.Context, macc supplyexported.ModuleAccountI)

SendCoinsFromModuleToAccount(ctx sdk.Context, senderModule string, recipientAddr sdk.AccAddress, amt sdk.Coins) error
SendCoinsFromAccountToModule(ctx sdk.Context, senderAddr sdk.AccAddress, recipientModule string, amt sdk.Coins) error
Expand Down
5 changes: 5 additions & 0 deletions x/bep3/types/keys.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package types

import (
"time"

sdk "github.com/cosmos/cosmos-sdk/types"
)

Expand All @@ -26,6 +28,9 @@ const (

// Key prefixes
var (
// ModulePermissionsUpgradeTime is the block time after which the bep3 module account's permissions are synced with the supply module.
ModulePermissionsUpgradeTime time.Time = time.Date(2020, 11, 3, 10, 0, 0, 0, time.UTC)

AtomicSwapKeyPrefix = []byte{0x00} // prefix for keys that store AtomicSwaps
AtomicSwapByBlockPrefix = []byte{0x01} // prefix for keys of the AtomicSwapsByBlock index
AtomicSwapLongtermStoragePrefix = []byte{0x02} // prefix for keys of the AtomicSwapLongtermStorage index
Expand Down

0 comments on commit 35a82ac

Please sign in to comment.