Some checks failed
Build SimApp / build (amd64) (push) Waiting to run
Build SimApp / build (arm64) (push) Waiting to run
CodeQL / Analyze (push) Waiting to run
Build & Push / build (push) Waiting to run
Run Gosec / Gosec (push) Waiting to run
Lint / golangci-lint (push) Waiting to run
Checks dependencies and mocks generation / Check go mod tidy (push) Waiting to run
Checks dependencies and mocks generation / Check up to date mocks (push) Waiting to run
System Tests / setup (push) Waiting to run
System Tests / test-system (push) Blocked by required conditions
System Tests / test-system-legacy (push) Blocked by required conditions
Tests / Code Coverage / split-test-files (push) Waiting to run
Tests / Code Coverage / tests (00) (push) Blocked by required conditions
Tests / Code Coverage / tests (01) (push) Blocked by required conditions
Tests / Code Coverage / tests (02) (push) Blocked by required conditions
Tests / Code Coverage / tests (03) (push) Blocked by required conditions
Tests / Code Coverage / test-integration (push) Waiting to run
Tests / Code Coverage / test-e2e (push) Waiting to run
Tests / Code Coverage / repo-analysis (push) Blocked by required conditions
Tests / Code Coverage / test-sim-nondeterminism (push) Waiting to run
Tests / Code Coverage / test-clientv2 (push) Waiting to run
Tests / Code Coverage / test-core (push) Waiting to run
Tests / Code Coverage / test-depinject (push) Waiting to run
Tests / Code Coverage / test-errors (push) Waiting to run
Tests / Code Coverage / test-math (push) Waiting to run
Tests / Code Coverage / test-schema (push) Waiting to run
Tests / Code Coverage / test-collections (push) Waiting to run
Tests / Code Coverage / test-cosmovisor (push) Waiting to run
Tests / Code Coverage / test-confix (push) Waiting to run
Tests / Code Coverage / test-store (push) Waiting to run
Tests / Code Coverage / test-log (push) Waiting to run
Tests / Code Coverage / test-x-tx (push) Waiting to run
Tests / Code Coverage / test-x-nft (push) Waiting to run
Tests / Code Coverage / test-x-circuit (push) Waiting to run
Tests / Code Coverage / test-x-feegrant (push) Waiting to run
Tests / Code Coverage / test-x-evidence (push) Waiting to run
Tests / Code Coverage / test-x-upgrade (push) Waiting to run
Tests / Code Coverage / test-tools-benchmark (push) Waiting to run
Build & Push SDK Proto Builder / build (push) Has been cancelled
134 lines
5.4 KiB
Go
134 lines
5.4 KiB
Go
package staking_test
|
|
|
|
import (
|
|
"testing"
|
|
|
|
abci "github.com/cometbft/cometbft/abci/types"
|
|
cmtproto "github.com/cometbft/cometbft/proto/tendermint/types"
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"cosmossdk.io/depinject"
|
|
"cosmossdk.io/log"
|
|
"cosmossdk.io/math"
|
|
|
|
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
|
|
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
|
|
simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims"
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil"
|
|
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
|
|
bankKeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
|
|
stakingKeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper"
|
|
"github.com/cosmos/cosmos-sdk/x/staking/testutil"
|
|
"github.com/cosmos/cosmos-sdk/x/staking/types"
|
|
)
|
|
|
|
var (
|
|
priv1 = secp256k1.GenPrivKey()
|
|
addr1 = sdk.AccAddress(priv1.PubKey().Address())
|
|
priv2 = secp256k1.GenPrivKey()
|
|
addr2 = sdk.AccAddress(priv2.PubKey().Address())
|
|
|
|
valKey = ed25519.GenPrivKey()
|
|
commissionRates = types.NewCommissionRates(math.LegacyZeroDec(), math.LegacyZeroDec(), math.LegacyZeroDec())
|
|
)
|
|
|
|
func TestStakingMsgs(t *testing.T) {
|
|
genTokens := sdk.TokensFromConsensusPower(42, sdk.DefaultPowerReduction)
|
|
bondTokens := sdk.TokensFromConsensusPower(10, sdk.DefaultPowerReduction)
|
|
genCoin := sdk.NewCoin(sdk.DefaultBondDenom, genTokens)
|
|
bondCoin := sdk.NewCoin(sdk.DefaultBondDenom, bondTokens)
|
|
|
|
acc1 := &authtypes.BaseAccount{Address: addr1.String()}
|
|
acc2 := &authtypes.BaseAccount{Address: addr2.String()}
|
|
accs := []simtestutil.GenesisAccount{
|
|
{GenesisAccount: acc1, Coins: sdk.Coins{genCoin}},
|
|
{GenesisAccount: acc2, Coins: sdk.Coins{genCoin}},
|
|
}
|
|
|
|
var (
|
|
bankKeeper bankKeeper.Keeper
|
|
stakingKeeper *stakingKeeper.Keeper
|
|
)
|
|
|
|
startupCfg := simtestutil.DefaultStartUpConfig()
|
|
startupCfg.GenesisAccounts = accs
|
|
|
|
app, err := simtestutil.SetupWithConfiguration(
|
|
depinject.Configs(
|
|
testutil.AppConfig,
|
|
depinject.Supply(log.NewNopLogger()),
|
|
),
|
|
startupCfg, &bankKeeper, &stakingKeeper)
|
|
require.NoError(t, err)
|
|
ctxCheck := app.NewContext(true)
|
|
|
|
require.True(t, sdk.Coins{genCoin}.Equal(bankKeeper.GetAllBalances(ctxCheck, addr1)))
|
|
require.True(t, sdk.Coins{genCoin}.Equal(bankKeeper.GetAllBalances(ctxCheck, addr2)))
|
|
|
|
// create validator
|
|
description := types.NewDescription("foo_moniker", "", "", "", "")
|
|
createValidatorMsg, err := types.NewMsgCreateValidator(
|
|
sdk.ValAddress(addr1).String(), valKey.PubKey(), bondCoin, description, commissionRates, math.OneInt(),
|
|
)
|
|
require.NoError(t, err)
|
|
|
|
header := cmtproto.Header{Height: app.LastBlockHeight() + 1}
|
|
txConfig := moduletestutil.MakeTestTxConfig()
|
|
_, _, err = simtestutil.SignCheckDeliver(t, txConfig, app.BaseApp, header, []sdk.Msg{createValidatorMsg}, "", []uint64{0}, []uint64{0}, true, true, priv1)
|
|
require.NoError(t, err)
|
|
require.True(t, sdk.Coins{genCoin.Sub(bondCoin)}.Equal(bankKeeper.GetAllBalances(ctxCheck, addr1)))
|
|
|
|
_, err = app.FinalizeBlock(&abci.RequestFinalizeBlock{Height: app.LastBlockHeight() + 1})
|
|
require.NoError(t, err)
|
|
ctxCheck = app.NewContext(true)
|
|
validator, err := stakingKeeper.GetValidator(ctxCheck, sdk.ValAddress(addr1))
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, sdk.ValAddress(addr1).String(), validator.OperatorAddress)
|
|
require.Equal(t, types.Bonded, validator.Status)
|
|
require.True(math.IntEq(t, bondTokens, validator.BondedTokens()))
|
|
|
|
_, err = app.FinalizeBlock(&abci.RequestFinalizeBlock{Height: app.LastBlockHeight() + 1})
|
|
require.NoError(t, err)
|
|
|
|
// edit the validator
|
|
description = types.NewDescription("bar_moniker", "", "", "", "")
|
|
editValidatorMsg := types.NewMsgEditValidator(sdk.ValAddress(addr1).String(), description, nil, nil)
|
|
|
|
header = cmtproto.Header{Height: app.LastBlockHeight() + 1}
|
|
_, _, err = simtestutil.SignCheckDeliver(t, txConfig, app.BaseApp, header, []sdk.Msg{editValidatorMsg}, "", []uint64{0}, []uint64{1}, true, true, priv1)
|
|
require.NoError(t, err)
|
|
|
|
ctxCheck = app.NewContext(true)
|
|
validator, err = stakingKeeper.GetValidator(ctxCheck, sdk.ValAddress(addr1))
|
|
require.NoError(t, err)
|
|
require.Equal(t, description, validator.Description)
|
|
|
|
// delegate
|
|
require.True(t, sdk.Coins{genCoin}.Equal(bankKeeper.GetAllBalances(ctxCheck, addr2)))
|
|
delegateMsg := types.NewMsgDelegate(addr2.String(), sdk.ValAddress(addr1).String(), bondCoin)
|
|
|
|
header = cmtproto.Header{Height: app.LastBlockHeight() + 1}
|
|
_, _, err = simtestutil.SignCheckDeliver(t, txConfig, app.BaseApp, header, []sdk.Msg{delegateMsg}, "", []uint64{1}, []uint64{0}, true, true, priv2)
|
|
require.NoError(t, err)
|
|
|
|
ctxCheck = app.NewContext(true)
|
|
require.True(t, sdk.Coins{genCoin.Sub(bondCoin)}.Equal(bankKeeper.GetAllBalances(ctxCheck, addr2)))
|
|
_, err = stakingKeeper.GetDelegation(ctxCheck, addr2, sdk.ValAddress(addr1))
|
|
require.NoError(t, err)
|
|
|
|
// begin unbonding
|
|
beginUnbondingMsg := types.NewMsgUndelegate(addr2.String(), sdk.ValAddress(addr1).String(), bondCoin)
|
|
header = cmtproto.Header{Height: app.LastBlockHeight() + 1}
|
|
_, _, err = simtestutil.SignCheckDeliver(t, txConfig, app.BaseApp, header, []sdk.Msg{beginUnbondingMsg}, "", []uint64{1}, []uint64{1}, true, true, priv2)
|
|
require.NoError(t, err)
|
|
|
|
// delegation should exist anymore
|
|
ctxCheck = app.NewContext(true)
|
|
_, err = stakingKeeper.GetDelegation(ctxCheck, addr2, sdk.ValAddress(addr1))
|
|
require.ErrorIs(t, err, types.ErrNoDelegation)
|
|
|
|
// balance should be the same because bonding not yet complete
|
|
require.True(t, sdk.Coins{genCoin.Sub(bondCoin)}.Equal(bankKeeper.GetAllBalances(ctxCheck, addr2)))
|
|
}
|