added EndBlocker and started implementing e2e test suite

Signed-off-by: Lorenz Herzberger <lorenzherzberger@gmail.com>
This commit is contained in:
Lorenz Herzberger 2023-08-28 17:24:00 +02:00
parent e612ae78f4
commit f9de021deb
No known key found for this signature in database
GPG Key ID: FA5EE906EB55316A
6 changed files with 151 additions and 4 deletions

14
tests/e2e/dao/cli_test.go Normal file
View File

@ -0,0 +1,14 @@
package dao
import (
"planetmint-go/testutil/network"
"testing"
"github.com/stretchr/testify/suite"
)
func TestE2ETestSuite(t *testing.T) {
cfg := network.DefaultConfig()
cfg.NumValidators = 1
suite.Run(t, NewE2ETestSuite(cfg))
}

66
tests/e2e/dao/suite.go Normal file
View File

@ -0,0 +1,66 @@
package dao
import (
"fmt"
"planetmint-go/testutil/network"
"planetmint-go/testutil/sample"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/crypto/hd"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
clitestutil "planetmint-go/testutil/cli"
auth "github.com/cosmos/cosmos-sdk/x/auth/client/cli"
bank "github.com/cosmos/cosmos-sdk/x/bank/client/cli"
"github.com/stretchr/testify/suite"
)
// E2ETestSuite struct definition of dao suite
type E2ETestSuite struct {
suite.Suite
cfg network.Config
network *network.Network
}
// NewE2ETestSuite returns configured dao E2ETestSuite
func NewE2ETestSuite(cfg network.Config) *E2ETestSuite {
return &E2ETestSuite{cfg: cfg}
}
// SetupSuite initializes dao E2ETestSuite
func (s *E2ETestSuite) SetupSuite() {
s.T().Log("setting up e2e test suite")
s.network = network.New(s.T())
}
// TearDownSuite clean up after testing
func (s *E2ETestSuite) TearDownSuite() {
s.T().Log("tearing down e2e test suite")
}
func (s *E2ETestSuite) TestDistributeCollectedFees() {
val := s.network.Validators[0]
kb := val.ClientCtx.Keyring
account, err := kb.NewAccount(sample.Name, sample.Mnemonic, keyring.DefaultBIP39Passphrase, sample.DefaultDerivationPath, hd.Secp256k1)
s.Require().NoError(err)
addr, _ := account.GetAddress()
// sending funds to machine to initialize account on chain
args := []string{
val.Moniker,
addr.String(),
sample.Amount,
"--yes",
fmt.Sprintf("--%s=%s", flags.FlagFees, sample.Fees),
}
_, err = clitestutil.ExecTestCLICmd(val.ClientCtx, bank.NewSendTxCmd(), args)
s.Require().NoError(err)
out, err := clitestutil.ExecTestCLICmd(val.ClientCtx, auth.GetAccountsCmd(), []string{})
fmt.Println(out)
}

12
x/dao/abci.go Normal file
View File

@ -0,0 +1,12 @@
package dao
import (
"planetmint-go/x/dao/keeper"
abci "github.com/cometbft/cometbft/abci/types"
sdk "github.com/cosmos/cosmos-sdk/types"
)
func EndBlocker(ctx sdk.Context, req abci.RequestEndBlock, k keeper.Keeper) {
k.DistributeCollectedFees(ctx)
}

View File

@ -3,10 +3,13 @@ package keeper
import (
"fmt"
"cosmossdk.io/math"
"github.com/cometbft/cometbft/libs/log"
"github.com/cosmos/cosmos-sdk/codec"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
disttypes "github.com/cosmos/cosmos-sdk/x/distribution/types"
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
"planetmint-go/x/dao/types"
@ -52,3 +55,48 @@ func NewKeeper(
func (k Keeper) Logger(ctx sdk.Context) log.Logger {
return ctx.Logger().With("module", fmt.Sprintf("x/%s", types.ModuleName))
}
func (k Keeper) DistributeCollectedFees(ctx sdk.Context) {
ctx = sdk.UnwrapSDKContext(ctx)
balances := make(map[string]math.Int)
totalStake := math.ZeroInt()
k.accountKeeper.IterateAccounts(ctx, func(acc authtypes.AccountI) bool {
addr := acc.GetAddress()
balance := k.bankKeeper.SpendableCoins(ctx, addr)
found, stake := balance.Find("stake")
if found {
totalStake = totalStake.Add(stake.Amount)
balances[addr.String()] = stake.Amount
}
return false
})
distAddr := k.accountKeeper.GetModuleAddress(disttypes.ModuleName)
distSpendableCoins := k.bankKeeper.SpendableCoins(ctx, distAddr)
found, coinToDistribute := distSpendableCoins.Find("token")
if found {
decTotalAmountToDistribute := sdk.NewDecFromInt(coinToDistribute.Amount)
decTotalStake := sdk.NewDecFromInt(totalStake)
for addr, stake := range balances {
decStake := sdk.NewDecFromInt(stake)
share := decStake.Quo(decTotalStake)
claim := decTotalAmountToDistribute.Mul(share)
if claim.GTE(sdk.OneDec()) {
intClaim := claim.TruncateInt()
coinClaim := sdk.NewCoin("token", intClaim)
accAddress, err := sdk.AccAddressFromBech32(addr)
if err != nil {
panic(err)
}
if !k.bankKeeper.BlockedAddr(accAddress) {
err = k.bankKeeper.SendCoinsFromModuleToAccount(ctx, disttypes.ModuleName, accAddress, sdk.NewCoins(coinClaim))
if err != nil {
panic(err)
}
}
}
}
}
}

View File

@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
// this line is used by starport scaffolding # 1
"github.com/grpc-ecosystem/grpc-gateway/runtime"
@ -11,14 +12,15 @@ import (
abci "github.com/cometbft/cometbft/abci/types"
"planetmint-go/x/dao/client/cli"
"planetmint-go/x/dao/keeper"
"planetmint-go/x/dao/types"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/codec"
cdctypes "github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
"planetmint-go/x/dao/client/cli"
"planetmint-go/x/dao/keeper"
"planetmint-go/x/dao/types"
)
var (
@ -143,6 +145,7 @@ func (AppModule) ConsensusVersion() uint64 { return 1 }
func (am AppModule) BeginBlock(_ sdk.Context, _ abci.RequestBeginBlock) {}
// EndBlock contains the logic that is automatically triggered at the end of each block
func (am AppModule) EndBlock(_ sdk.Context, _ abci.RequestEndBlock) []abci.ValidatorUpdate {
func (am AppModule) EndBlock(ctx sdk.Context, req abci.RequestEndBlock) []abci.ValidatorUpdate {
EndBlocker(ctx, req, am.keeper)
return []abci.ValidatorUpdate{}
}

View File

@ -8,11 +8,15 @@ import (
// AccountKeeper defines the expected account keeper used for simulations (noalias)
type AccountKeeper interface {
GetAccount(ctx sdk.Context, addr sdk.AccAddress) types.AccountI
GetModuleAddress(module string) sdk.AccAddress
IterateAccounts(sdk.Context, func(types.AccountI) bool)
// Methods imported from account should be defined here
}
// BankKeeper defines the expected interface needed to retrieve account balances.
type BankKeeper interface {
SpendableCoins(ctx sdk.Context, addr sdk.AccAddress) sdk.Coins
SendCoinsFromModuleToAccount(ctx sdk.Context, senderModule string, recipientAddr sdk.AccAddress, amt sdk.Coins) error
BlockedAddr(addr sdk.AccAddress) bool
// Methods imported from bank should be defined here
}