-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement begin blocker logic (#3)
- Loading branch information
Showing
11 changed files
with
178 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package e2e | ||
|
||
import ( | ||
"testing" | ||
|
||
"cosmossdk.io/math" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/strangelove-ventures/interchaintest/v8/ibc" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
var COLLECTOR = "noble17xpfvakm2amg962yls6f84z3kell8c5lc6kgnn" | ||
|
||
// TestBeginBlocker tests the module's begin blocker logic. | ||
func TestBeginBlocker(t *testing.T) { | ||
t.Parallel() | ||
|
||
var wrapper Wrapper | ||
ctx, _, _ := Suite(t, &wrapper, false) | ||
validator := wrapper.chain.Validators[0] | ||
|
||
oldBalance, err := wrapper.chain.BankQueryAllBalances(ctx, wrapper.owner.FormattedAddress()) | ||
require.NoError(t, err) | ||
|
||
err = validator.BankSend(ctx, wrapper.owner.KeyName(), ibc.WalletAmount{ | ||
Address: wrapper.pendingOwner.FormattedAddress(), | ||
Denom: "uusdc", | ||
Amount: math.NewInt(1_000_000), | ||
}) | ||
require.NoError(t, err) | ||
|
||
balance, err := wrapper.chain.BankQueryAllBalances(ctx, COLLECTOR) | ||
require.NoError(t, err) | ||
require.True(t, balance.IsZero()) | ||
|
||
newBalance, err := wrapper.chain.BankQueryAllBalances(ctx, wrapper.owner.FormattedAddress()) | ||
require.NoError(t, err) | ||
require.Equal(t, oldBalance.Sub(sdk.NewInt64Coin("uusdc", 1_000_000)), newBalance) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package mocks | ||
|
||
import ( | ||
"context" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/cosmos/cosmos-sdk/x/auth/codec" | ||
"github.com/noble-assets/authority/x/authority/types" | ||
) | ||
|
||
var cdc = codec.NewBech32Codec("noble") | ||
|
||
var _ types.BankKeeper = BankKeeper{} | ||
|
||
type BankKeeper struct { | ||
Balances map[string]sdk.Coins | ||
} | ||
|
||
func (k BankKeeper) GetAllBalances(_ context.Context, bz sdk.AccAddress) sdk.Coins { | ||
address, _ := cdc.BytesToString(bz) | ||
return k.Balances[address] | ||
} | ||
|
||
func (k BankKeeper) SendCoins(_ context.Context, _, _ sdk.AccAddress, _ sdk.Coins) error { | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package keeper | ||
|
||
import ( | ||
"context" | ||
|
||
"cosmossdk.io/errors" | ||
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" | ||
) | ||
|
||
// BeginBlock sends all fees collected in the previous block to the module's owner. | ||
func (k *Keeper) BeginBlock(ctx context.Context) error { | ||
collector := k.accountKeeper.GetModuleAddress(authtypes.FeeCollectorName) | ||
balance := k.bankKeeper.GetAllBalances(ctx, collector) | ||
if balance.IsZero() { | ||
return nil | ||
} | ||
|
||
owner, err := k.Owner.Get(ctx) | ||
if err != nil { | ||
return errors.Wrap(err, "failed to get owner from state") | ||
} | ||
address, err := k.accountKeeper.AddressCodec().StringToBytes(owner) | ||
if err != nil { | ||
return errors.Wrap(err, "failed to decode owner address") | ||
} | ||
|
||
return k.bankKeeper.SendCoins(ctx, collector, address, balance) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package keeper_test | ||
|
||
import ( | ||
"testing" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/noble-assets/authority/utils" | ||
"github.com/noble-assets/authority/utils/mocks" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestBeginBlock(t *testing.T) { | ||
bank := mocks.BankKeeper{ | ||
Balances: make(map[string]sdk.Coins), | ||
} | ||
keeper, ctx := mocks.AuthorityKeeperWithBank(t, bank) | ||
|
||
// ACT: Attempt to run begin blocker with empty fee collector. | ||
err := keeper.BeginBlock(ctx) | ||
// ASSERT: The action should've succeeded due to empty account. | ||
require.NoError(t, err) | ||
|
||
// ARRANGE: Give the fee collector some balance. | ||
bank.Balances["noble17xpfvakm2amg962yls6f84z3kell8c5lc6kgnn"] = sdk.NewCoins( | ||
sdk.NewInt64Coin("uusdc", 20_000), | ||
) | ||
|
||
// ACT: Attempt to run begin blocker with no owner set. | ||
err = keeper.BeginBlock(ctx) | ||
// ASSERT: The action should've failed due to no owner set. | ||
require.ErrorContains(t, err, "failed to get owner from state") | ||
|
||
// ARRANGE: Set an invalid owner in state. | ||
require.NoError(t, keeper.Owner.Set(ctx, "cosmos10d07y265gmmuvt4z0w9aw880jnsr700j6zn9kn")) | ||
|
||
// ACT: Attempt to run begin blocker with invalid owner set. | ||
err = keeper.BeginBlock(ctx) | ||
// ASSERT: The action should've failed due to invalid owner set. | ||
require.ErrorContains(t, err, "failed to decode owner address") | ||
|
||
// ARRANGE: Generate an owner account and set in state. | ||
owner := utils.TestAccount() | ||
require.NoError(t, keeper.Owner.Set(ctx, owner.Address)) | ||
|
||
// ACT: Attempt to run begin blocker. | ||
err = keeper.BeginBlock(ctx) | ||
// ASSERT: The action should've succeeded. | ||
require.NoError(t, err) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,18 @@ | ||
package types | ||
|
||
import "cosmossdk.io/core/address" | ||
import ( | ||
"context" | ||
|
||
"cosmossdk.io/core/address" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
) | ||
|
||
type AccountKeeper interface { | ||
AddressCodec() address.Codec | ||
GetModuleAddress(moduleName string) sdk.AccAddress | ||
} | ||
|
||
type BankKeeper interface { | ||
GetAllBalances(ctx context.Context, addr sdk.AccAddress) sdk.Coins | ||
SendCoins(ctx context.Context, fromAddr, toAddr sdk.AccAddress, amt sdk.Coins) error | ||
} |