refactor(tests/integration) Migrate lockup and multisig integration tests to server v2 (#22924)

This commit is contained in:
son trinh 2024-12-19 17:48:37 +07:00 committed by GitHub
parent 578763edcc
commit cb82789871
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
11 changed files with 649 additions and 379 deletions

View File

@ -1,97 +0,0 @@
package lockup
import (
"testing"
"time"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
"cosmossdk.io/core/transaction"
"cosmossdk.io/simapp"
types "cosmossdk.io/x/accounts/defaults/lockup/v1"
"cosmossdk.io/x/bank/testutil"
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
sdk "github.com/cosmos/cosmos-sdk/types"
)
var (
ownerAddr = secp256k1.GenPrivKey().PubKey().Address()
accOwner = sdk.AccAddress(ownerAddr)
)
type IntegrationTestSuite struct {
suite.Suite
app *simapp.SimApp
}
func NewIntegrationTestSuite() *IntegrationTestSuite {
return &IntegrationTestSuite{}
}
func (s *IntegrationTestSuite) SetupSuite() {
s.T().Log("setting up integration test suite")
s.app = setupApp(s.T())
}
func (s *IntegrationTestSuite) TearDownSuite() {
s.T().Log("tearing down integration test suite")
}
func setupApp(t *testing.T) *simapp.SimApp {
t.Helper()
app := simapp.Setup(t, false)
return app
}
func (s *IntegrationTestSuite) executeTx(ctx sdk.Context, msg sdk.Msg, app *simapp.SimApp, accAddr, sender []byte) error {
_, err := app.AccountsKeeper.Execute(ctx, accAddr, sender, msg, nil)
return err
}
func (s *IntegrationTestSuite) queryAcc(ctx sdk.Context, req sdk.Msg, app *simapp.SimApp, accAddr []byte) (transaction.Msg, error) {
resp, err := app.AccountsKeeper.Query(ctx, accAddr, req)
return resp, err
}
func (s *IntegrationTestSuite) fundAccount(app *simapp.SimApp, ctx sdk.Context, addr sdk.AccAddress, amt sdk.Coins) {
require.NoError(s.T(), testutil.FundAccount(ctx, app.BankKeeper, addr, amt))
}
func (s *IntegrationTestSuite) queryLockupAccInfo(ctx sdk.Context, app *simapp.SimApp, accAddr []byte) *types.QueryLockupAccountInfoResponse {
req := &types.QueryLockupAccountInfoRequest{}
resp, err := s.queryAcc(ctx, req, app, accAddr)
require.NoError(s.T(), err)
require.NotNil(s.T(), resp)
lockupAccountInfoResponse, ok := resp.(*types.QueryLockupAccountInfoResponse)
require.True(s.T(), ok)
return lockupAccountInfoResponse
}
func (s *IntegrationTestSuite) queryUnbondingEntries(ctx sdk.Context, app *simapp.SimApp, accAddr []byte, valAddr string) *types.QueryUnbondingEntriesResponse {
req := &types.QueryUnbondingEntriesRequest{
ValidatorAddress: valAddr,
}
resp, err := s.queryAcc(ctx, req, app, accAddr)
require.NoError(s.T(), err)
require.NotNil(s.T(), resp)
unbondingEntriesResponse, ok := resp.(*types.QueryUnbondingEntriesResponse)
require.True(s.T(), ok)
return unbondingEntriesResponse
}
func (s *IntegrationTestSuite) setupStakingParams(ctx sdk.Context, app *simapp.SimApp) {
params, err := app.StakingKeeper.Params.Get(ctx)
require.NoError(s.T(), err)
// update unbonding time
params.UnbondingTime = time.Duration(time.Second * 10)
err = app.StakingKeeper.Params.Set(ctx, params)
require.NoError(s.T(), err)
}

View File

@ -1,115 +0,0 @@
package multisig
import (
"context"
"testing"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
"cosmossdk.io/core/transaction"
"cosmossdk.io/math"
"cosmossdk.io/simapp"
v1 "cosmossdk.io/x/accounts/defaults/multisig/v1"
"cosmossdk.io/x/bank/testutil"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
sdk "github.com/cosmos/cosmos-sdk/types"
)
type IntegrationTestSuite struct {
suite.Suite
app *simapp.SimApp
members []sdk.AccAddress
membersAddr []string
}
func NewIntegrationTestSuite() *IntegrationTestSuite {
return &IntegrationTestSuite{}
}
func (s *IntegrationTestSuite) SetupSuite() {
s.app = setupApp(s.T())
s.members = []sdk.AccAddress{}
for i := 0; i < 10; i++ {
addr := secp256k1.GenPrivKey().PubKey().Address()
addrStr, err := s.app.AuthKeeper.AddressCodec().BytesToString(addr)
require.NoError(s.T(), err)
s.membersAddr = append(s.membersAddr, addrStr)
s.members = append(s.members, sdk.AccAddress(addr))
}
}
func (s *IntegrationTestSuite) TearDownSuite() {}
func setupApp(t *testing.T) *simapp.SimApp {
t.Helper()
app := simapp.Setup(t, false)
return app
}
func (s *IntegrationTestSuite) executeTx(ctx context.Context, msg sdk.Msg, accAddr, sender []byte) error {
_, err := s.app.AccountsKeeper.Execute(ctx, accAddr, sender, msg, nil)
return err
}
func (s *IntegrationTestSuite) queryAcc(ctx context.Context, req sdk.Msg, accAddr []byte) (transaction.Msg, error) {
resp, err := s.app.AccountsKeeper.Query(ctx, accAddr, req)
return resp, err
}
func (s *IntegrationTestSuite) fundAccount(ctx context.Context, addr sdk.AccAddress, amt sdk.Coins) {
require.NoError(s.T(), testutil.FundAccount(ctx, s.app.BankKeeper, addr, amt))
}
// initAccount initializes a multisig account with the given members and powers
// and returns the account address
func (s *IntegrationTestSuite) initAccount(ctx context.Context, sender []byte, membersPowers map[string]uint64) ([]byte, string) {
s.fundAccount(ctx, sender, sdk.Coins{sdk.NewCoin("stake", math.NewInt(1000000))})
members := []*v1.Member{}
for addrStr, power := range membersPowers {
members = append(members, &v1.Member{Address: addrStr, Weight: power})
}
_, accountAddr, err := s.app.AccountsKeeper.Init(ctx, "multisig", sender,
&v1.MsgInit{
Members: members,
Config: &v1.Config{
Threshold: 100,
Quorum: 100,
VotingPeriod: 120,
Revote: false,
EarlyExecution: true,
},
}, sdk.Coins{sdk.NewCoin("stake", math.NewInt(1000))}, nil)
s.NoError(err)
accountAddrStr, err := s.app.AuthKeeper.AddressCodec().BytesToString(accountAddr)
s.NoError(err)
return accountAddr, accountAddrStr
}
// createProposal
func (s *IntegrationTestSuite) createProposal(ctx context.Context, accAddr, sender []byte, msgs ...*codectypes.Any) {
propReq := &v1.MsgCreateProposal{
Proposal: &v1.Proposal{
Title: "test",
Summary: "test",
Messages: msgs,
},
}
err := s.executeTx(ctx, propReq, accAddr, sender)
s.NoError(err)
}
func (s *IntegrationTestSuite) executeProposal(ctx context.Context, accAddr, sender []byte, proposalID uint64) error {
execReq := &v1.MsgExecuteProposal{
ProposalId: proposalID,
}
return s.executeTx(ctx, execReq, accAddr, sender)
}

View File

@ -13,23 +13,23 @@ import (
types "cosmossdk.io/x/accounts/defaults/lockup/v1"
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
"github.com/cosmos/cosmos-sdk/tests/integration/v2"
sdk "github.com/cosmos/cosmos-sdk/types"
)
func (s *IntegrationTestSuite) TestContinuousLockingAccount() {
t := s.T()
app := setupApp(t)
currentTime := time.Now()
ctx := sdk.NewContext(app.CommitMultiStore(), false, app.Logger()).WithHeaderInfo(header.Info{
Time: currentTime,
})
s.setupStakingParams(ctx, app)
ownerAddrStr, err := app.AuthKeeper.AddressCodec().BytesToString(accOwner)
ctx := s.ctx
ctx = integration.SetHeaderInfo(ctx, header.Info{Time: currentTime})
s.setupStakingParams(ctx, s.stakingKeeper)
ownerAddrStr, err := s.authKeeper.AddressCodec().BytesToString(accOwner)
require.NoError(t, err)
s.fundAccount(app, ctx, accOwner, sdk.Coins{sdk.NewCoin("stake", math.NewInt(1000000))})
s.fundAccount(s.bankKeeper, ctx, accOwner, sdk.Coins{sdk.NewCoin("stake", math.NewInt(1000000))})
randAcc := sdk.AccAddress(secp256k1.GenPrivKey().PubKey().Address())
_, accountAddr, err := app.AccountsKeeper.Init(ctx, lockupaccount.CONTINUOUS_LOCKING_ACCOUNT, accOwner, &types.MsgInitLockupAccount{
_, accountAddr, err := s.accountsKeeper.Init(ctx, lockupaccount.CONTINUOUS_LOCKING_ACCOUNT, accOwner, &types.MsgInitLockupAccount{
Owner: ownerAddrStr,
StartTime: currentTime,
// end time in 1 minutes
@ -37,10 +37,10 @@ func (s *IntegrationTestSuite) TestContinuousLockingAccount() {
}, sdk.Coins{sdk.NewCoin("stake", math.NewInt(1000))}, nil)
require.NoError(t, err)
addr, err := app.AuthKeeper.AddressCodec().BytesToString(randAcc)
addr, err := s.authKeeper.AddressCodec().BytesToString(randAcc)
require.NoError(t, err)
vals, err := app.StakingKeeper.GetAllValidators(ctx)
vals, err := s.stakingKeeper.GetAllValidators(ctx)
require.NoError(t, err)
val := vals[0]
@ -50,7 +50,7 @@ func (s *IntegrationTestSuite) TestContinuousLockingAccount() {
ToAddress: addr,
Amount: sdk.Coins{sdk.NewCoin("stake", math.NewInt(100))},
}
err := s.executeTx(ctx, msg, app, accountAddr, accOwner)
err := s.executeTx(ctx, msg, s.accountsKeeper, accountAddr, accOwner)
require.NotNil(t, err)
})
t.Run("error - execute send message, insufficient fund", func(t *testing.T) {
@ -59,15 +59,13 @@ func (s *IntegrationTestSuite) TestContinuousLockingAccount() {
ToAddress: addr,
Amount: sdk.Coins{sdk.NewCoin("stake", math.NewInt(100))},
}
err := s.executeTx(ctx, msg, app, accountAddr, accOwner)
err := s.executeTx(ctx, msg, s.accountsKeeper, accountAddr, accOwner)
require.NotNil(t, err)
})
// Update context time
// 12 sec = 1/5 of a minute so 200stake should be released
ctx = ctx.WithHeaderInfo(header.Info{
Time: currentTime.Add(time.Second * 12),
})
ctx = integration.SetHeaderInfo(ctx, header.Info{Time: currentTime.Add(time.Second * 12)})
// Check if token is sendable
t.Run("ok - execute send message", func(t *testing.T) {
@ -76,33 +74,32 @@ func (s *IntegrationTestSuite) TestContinuousLockingAccount() {
ToAddress: addr,
Amount: sdk.Coins{sdk.NewCoin("stake", math.NewInt(100))},
}
err := s.executeTx(ctx, msg, app, accountAddr, accOwner)
err := s.executeTx(ctx, msg, s.accountsKeeper, accountAddr, accOwner)
require.NoError(t, err)
balance := app.BankKeeper.GetBalance(ctx, randAcc, "stake")
balance := s.bankKeeper.GetBalance(ctx, randAcc, "stake")
require.True(t, balance.Amount.Equal(math.NewInt(100)))
})
t.Run("ok - execute delegate message", func(t *testing.T) {
msg := &types.MsgDelegate{
Sender: ownerAddrStr,
ValidatorAddress: val.OperatorAddress,
Amount: sdk.NewCoin("stake", math.NewInt(100)),
}
err = s.executeTx(ctx, msg, app, accountAddr, accOwner)
err = s.executeTx(ctx, msg, s.accountsKeeper, accountAddr, accOwner)
require.NoError(t, err)
valbz, err := app.StakingKeeper.ValidatorAddressCodec().StringToBytes(val.OperatorAddress)
valbz, err := s.stakingKeeper.ValidatorAddressCodec().StringToBytes(val.OperatorAddress)
require.NoError(t, err)
del, err := app.StakingKeeper.Delegations.Get(
del, err := s.stakingKeeper.Delegations.Get(
ctx, collections.Join(sdk.AccAddress(accountAddr), sdk.ValAddress(valbz)),
)
require.NoError(t, err)
require.NotNil(t, del)
// check if tracking is updated accordingly
lockupAccountInfoResponse := s.queryLockupAccInfo(ctx, app, accountAddr)
lockupAccountInfoResponse := s.queryLockupAccInfo(ctx, s.accountsKeeper, accountAddr)
delLocking := lockupAccountInfoResponse.DelegatedLocking
require.True(t, delLocking.AmountOf("stake").Equal(math.NewInt(100)))
})
@ -111,11 +108,11 @@ func (s *IntegrationTestSuite) TestContinuousLockingAccount() {
Sender: ownerAddrStr,
ValidatorAddress: val.OperatorAddress,
}
err = s.executeTx(ctx, msg, app, accountAddr, accOwner)
err = s.executeTx(ctx, msg, s.accountsKeeper, accountAddr, accOwner)
require.NoError(t, err)
})
t.Run("ok - execute undelegate message", func(t *testing.T) {
vals, err := app.StakingKeeper.GetAllValidators(ctx)
vals, err := s.stakingKeeper.GetAllValidators(ctx)
require.NoError(t, err)
val := vals[0]
msg := &types.MsgUndelegate{
@ -123,31 +120,29 @@ func (s *IntegrationTestSuite) TestContinuousLockingAccount() {
ValidatorAddress: val.OperatorAddress,
Amount: sdk.NewCoin("stake", math.NewInt(100)),
}
err = s.executeTx(ctx, msg, app, accountAddr, accOwner)
err = s.executeTx(ctx, msg, s.accountsKeeper, accountAddr, accOwner)
require.NoError(t, err)
valbz, err := app.StakingKeeper.ValidatorAddressCodec().StringToBytes(val.OperatorAddress)
valbz, err := s.stakingKeeper.ValidatorAddressCodec().StringToBytes(val.OperatorAddress)
require.NoError(t, err)
ubd, err := app.StakingKeeper.GetUnbondingDelegation(
ubd, err := s.stakingKeeper.GetUnbondingDelegation(
ctx, sdk.AccAddress(accountAddr), sdk.ValAddress(valbz),
)
require.NoError(t, err)
require.Equal(t, len(ubd.Entries), 1)
// check if an entry is added
unbondingEntriesResponse := s.queryUnbondingEntries(ctx, app, accountAddr, val.OperatorAddress)
unbondingEntriesResponse := s.queryUnbondingEntries(ctx, s.accountsKeeper, accountAddr, val.OperatorAddress)
entries := unbondingEntriesResponse.UnbondingEntries
require.True(t, entries[0].Amount.Amount.Equal(math.NewInt(100)))
require.True(t, entries[0].ValidatorAddress == val.OperatorAddress)
})
// Update context time to end time
ctx = ctx.WithHeaderInfo(header.Info{
Time: currentTime.Add(time.Minute),
})
ctx = integration.SetHeaderInfo(ctx, header.Info{Time: currentTime.Add(time.Minute)})
// trigger endblock for staking to handle matured unbonding delegation
_, err = app.StakingKeeper.EndBlocker(ctx)
_, err = s.stakingKeeper.EndBlocker(ctx)
require.NoError(t, err)
// test if tracking delegate work perfectly
@ -157,20 +152,20 @@ func (s *IntegrationTestSuite) TestContinuousLockingAccount() {
ValidatorAddress: val.OperatorAddress,
Amount: sdk.NewCoin("stake", math.NewInt(100)),
}
err = s.executeTx(ctx, msg, app, accountAddr, accOwner)
err = s.executeTx(ctx, msg, s.accountsKeeper, accountAddr, accOwner)
require.NoError(t, err)
valbz, err := app.StakingKeeper.ValidatorAddressCodec().StringToBytes(val.OperatorAddress)
valbz, err := s.stakingKeeper.ValidatorAddressCodec().StringToBytes(val.OperatorAddress)
require.NoError(t, err)
del, err := app.StakingKeeper.Delegations.Get(
del, err := s.stakingKeeper.Delegations.Get(
ctx, collections.Join(sdk.AccAddress(accountAddr), sdk.ValAddress(valbz)),
)
require.NoError(t, err)
require.NotNil(t, del)
// check if tracking is updated accordingly
lockupAccountInfoResponse := s.queryLockupAccInfo(ctx, app, accountAddr)
lockupAccountInfoResponse := s.queryLockupAccInfo(ctx, s.accountsKeeper, accountAddr)
delLocking := lockupAccountInfoResponse.DelegatedLocking
// should be update as ubd entry is matured
require.True(t, delLocking.AmountOf("stake").Equal(math.ZeroInt()))
@ -178,7 +173,7 @@ func (s *IntegrationTestSuite) TestContinuousLockingAccount() {
require.True(t, delFree.AmountOf("stake").Equal(math.NewInt(100)))
// check if the entry is removed
unbondingEntriesResponse := s.queryUnbondingEntries(ctx, app, accountAddr, val.OperatorAddress)
unbondingEntriesResponse := s.queryUnbondingEntries(ctx, s.accountsKeeper, accountAddr, val.OperatorAddress)
entries := unbondingEntriesResponse.UnbondingEntries
require.Len(t, entries, 0)
})

View File

@ -13,33 +13,34 @@ import (
types "cosmossdk.io/x/accounts/defaults/lockup/v1"
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
"github.com/cosmos/cosmos-sdk/tests/integration/v2"
sdk "github.com/cosmos/cosmos-sdk/types"
)
func (s *IntegrationTestSuite) TestDelayedLockingAccount() {
t := s.T()
app := setupApp(t)
currentTime := time.Now()
ctx := sdk.NewContext(app.CommitMultiStore(), false, app.Logger()).WithHeaderInfo(header.Info{
Time: currentTime,
})
s.setupStakingParams(ctx, app)
ownerAddrStr, err := app.AuthKeeper.AddressCodec().BytesToString(accOwner)
ctx := s.ctx
ctx = integration.SetHeaderInfo(ctx, header.Info{Time: currentTime})
s.setupStakingParams(ctx, s.stakingKeeper)
ownerAddrStr, err := s.authKeeper.AddressCodec().BytesToString(accOwner)
require.NoError(t, err)
s.fundAccount(app, ctx, accOwner, sdk.Coins{sdk.NewCoin("stake", math.NewInt(1000000))})
s.fundAccount(s.bankKeeper, ctx, accOwner, sdk.Coins{sdk.NewCoin("stake", math.NewInt(1000000))})
randAcc := sdk.AccAddress(secp256k1.GenPrivKey().PubKey().Address())
_, accountAddr, err := app.AccountsKeeper.Init(ctx, lockupaccount.DELAYED_LOCKING_ACCOUNT, accOwner, &types.MsgInitLockupAccount{
_, accountAddr, err := s.accountsKeeper.Init(ctx, lockupaccount.DELAYED_LOCKING_ACCOUNT, accOwner, &types.MsgInitLockupAccount{
Owner: ownerAddrStr,
// end time in 1 minutes
EndTime: currentTime.Add(time.Minute),
}, sdk.Coins{sdk.NewCoin("stake", math.NewInt(1000))}, nil)
require.NoError(t, err)
addr, err := app.AuthKeeper.AddressCodec().BytesToString(randAcc)
addr, err := s.authKeeper.AddressCodec().BytesToString(randAcc)
require.NoError(t, err)
vals, err := app.StakingKeeper.GetAllValidators(ctx)
vals, err := s.stakingKeeper.GetAllValidators(ctx)
require.NoError(t, err)
val := vals[0]
@ -49,7 +50,7 @@ func (s *IntegrationTestSuite) TestDelayedLockingAccount() {
ToAddress: addr,
Amount: sdk.Coins{sdk.NewCoin("stake", math.NewInt(100))},
}
err := s.executeTx(ctx, msg, app, accountAddr, accOwner)
err := s.executeTx(ctx, msg, s.accountsKeeper, accountAddr, accOwner)
require.NotNil(t, err)
})
t.Run("error - execute send message, insufficient fund", func(t *testing.T) {
@ -58,7 +59,7 @@ func (s *IntegrationTestSuite) TestDelayedLockingAccount() {
ToAddress: addr,
Amount: sdk.Coins{sdk.NewCoin("stake", math.NewInt(100))},
}
err := s.executeTx(ctx, msg, app, accountAddr, accOwner)
err := s.executeTx(ctx, msg, s.accountsKeeper, accountAddr, accOwner)
require.NotNil(t, err)
})
t.Run("ok - execute delegate message", func(t *testing.T) {
@ -67,20 +68,20 @@ func (s *IntegrationTestSuite) TestDelayedLockingAccount() {
ValidatorAddress: val.OperatorAddress,
Amount: sdk.NewCoin("stake", math.NewInt(100)),
}
err = s.executeTx(ctx, msg, app, accountAddr, accOwner)
err = s.executeTx(ctx, msg, s.accountsKeeper, accountAddr, accOwner)
require.NoError(t, err)
valbz, err := app.StakingKeeper.ValidatorAddressCodec().StringToBytes(val.OperatorAddress)
valbz, err := s.stakingKeeper.ValidatorAddressCodec().StringToBytes(val.OperatorAddress)
require.NoError(t, err)
del, err := app.StakingKeeper.Delegations.Get(
del, err := s.stakingKeeper.Delegations.Get(
ctx, collections.Join(sdk.AccAddress(accountAddr), sdk.ValAddress(valbz)),
)
require.NoError(t, err)
require.NotNil(t, del)
// check if tracking is updated accordingly
lockupAccountInfoResponse := s.queryLockupAccInfo(ctx, app, accountAddr)
lockupAccountInfoResponse := s.queryLockupAccInfo(ctx, s.accountsKeeper, accountAddr)
delLocking := lockupAccountInfoResponse.DelegatedLocking
require.True(t, delLocking.AmountOf("stake").Equal(math.NewInt(100)))
})
@ -89,11 +90,11 @@ func (s *IntegrationTestSuite) TestDelayedLockingAccount() {
Sender: ownerAddrStr,
ValidatorAddress: val.OperatorAddress,
}
err = s.executeTx(ctx, msg, app, accountAddr, accOwner)
err = s.executeTx(ctx, msg, s.accountsKeeper, accountAddr, accOwner)
require.NoError(t, err)
})
t.Run("ok - execute undelegate message", func(t *testing.T) {
vals, err := app.StakingKeeper.GetAllValidators(ctx)
vals, err := s.stakingKeeper.GetAllValidators(ctx)
require.NoError(t, err)
val := vals[0]
msg := &types.MsgUndelegate{
@ -101,19 +102,19 @@ func (s *IntegrationTestSuite) TestDelayedLockingAccount() {
ValidatorAddress: val.OperatorAddress,
Amount: sdk.NewCoin("stake", math.NewInt(100)),
}
err = s.executeTx(ctx, msg, app, accountAddr, accOwner)
err = s.executeTx(ctx, msg, s.accountsKeeper, accountAddr, accOwner)
require.NoError(t, err)
valbz, err := app.StakingKeeper.ValidatorAddressCodec().StringToBytes(val.OperatorAddress)
valbz, err := s.stakingKeeper.ValidatorAddressCodec().StringToBytes(val.OperatorAddress)
require.NoError(t, err)
ubd, err := app.StakingKeeper.GetUnbondingDelegation(
ubd, err := s.stakingKeeper.GetUnbondingDelegation(
ctx, sdk.AccAddress(accountAddr), sdk.ValAddress(valbz),
)
require.NoError(t, err)
require.Equal(t, len(ubd.Entries), 1)
// check if an entry is added
unbondingEntriesResponse := s.queryUnbondingEntries(ctx, app, accountAddr, val.OperatorAddress)
unbondingEntriesResponse := s.queryUnbondingEntries(ctx, s.accountsKeeper, accountAddr, val.OperatorAddress)
entries := unbondingEntriesResponse.UnbondingEntries
require.True(t, entries[0].Amount.Amount.Equal(math.NewInt(100)))
require.True(t, entries[0].ValidatorAddress == val.OperatorAddress)
@ -121,13 +122,10 @@ func (s *IntegrationTestSuite) TestDelayedLockingAccount() {
// Update context time
// After endtime fund should be unlock
// And unbond time elapsed
ctx = ctx.WithHeaderInfo(header.Info{
Time: currentTime.Add(time.Second * 61),
})
ctx = integration.SetHeaderInfo(ctx, header.Info{Time: currentTime.Add(time.Second * 61)})
// trigger endblock for staking to handle matured unbonding delegation
_, err = app.StakingKeeper.EndBlocker(ctx)
_, err = s.stakingKeeper.EndBlocker(ctx)
require.NoError(t, err)
// Check if token is sendable after unlock
@ -137,19 +135,19 @@ func (s *IntegrationTestSuite) TestDelayedLockingAccount() {
ToAddress: addr,
Amount: sdk.Coins{sdk.NewCoin("stake", math.NewInt(100))},
}
err := s.executeTx(ctx, msg, app, accountAddr, accOwner)
err := s.executeTx(ctx, msg, s.accountsKeeper, accountAddr, accOwner)
require.NoError(t, err)
balance := app.BankKeeper.GetBalance(ctx, randAcc, "stake")
balance := s.bankKeeper.GetBalance(ctx, randAcc, "stake")
require.True(t, balance.Amount.Equal(math.NewInt(100)))
// check if tracking ubd entry is updated accordingly
lockupAccountInfoResponse := s.queryLockupAccInfo(ctx, app, accountAddr)
lockupAccountInfoResponse := s.queryLockupAccInfo(ctx, s.accountsKeeper, accountAddr)
delLocking := lockupAccountInfoResponse.DelegatedLocking
require.True(t, delLocking.AmountOf("stake").Equal(math.ZeroInt()))
// check if the entry is removed
unbondingEntriesResponse := s.queryUnbondingEntries(ctx, app, accountAddr, val.OperatorAddress)
unbondingEntriesResponse := s.queryUnbondingEntries(ctx, s.accountsKeeper, accountAddr, val.OperatorAddress)
entries := unbondingEntriesResponse.UnbondingEntries
require.Len(t, entries, 0)
})

View File

@ -13,23 +13,24 @@ import (
types "cosmossdk.io/x/accounts/defaults/lockup/v1"
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
"github.com/cosmos/cosmos-sdk/tests/integration/v2"
sdk "github.com/cosmos/cosmos-sdk/types"
)
func (s *IntegrationTestSuite) TestPeriodicLockingAccount() {
t := s.T()
app := setupApp(t)
currentTime := time.Now()
ctx := sdk.NewContext(app.CommitMultiStore(), false, app.Logger()).WithHeaderInfo(header.Info{
Time: currentTime,
})
s.setupStakingParams(ctx, app)
ownerAddrStr, err := app.AuthKeeper.AddressCodec().BytesToString(accOwner)
ctx := s.ctx
ctx = integration.SetHeaderInfo(ctx, header.Info{Time: currentTime})
s.setupStakingParams(ctx, s.stakingKeeper)
ownerAddrStr, err := s.authKeeper.AddressCodec().BytesToString(accOwner)
require.NoError(t, err)
s.fundAccount(app, ctx, accOwner, sdk.Coins{sdk.NewCoin("stake", math.NewInt(1000000))})
s.fundAccount(s.bankKeeper, ctx, accOwner, sdk.Coins{sdk.NewCoin("stake", math.NewInt(1000000))})
randAcc := sdk.AccAddress(secp256k1.GenPrivKey().PubKey().Address())
_, accountAddr, err := app.AccountsKeeper.Init(ctx, lockupaccount.PERIODIC_LOCKING_ACCOUNT, accOwner, &types.MsgInitPeriodicLockingAccount{
_, accountAddr, err := s.accountsKeeper.Init(ctx, lockupaccount.PERIODIC_LOCKING_ACCOUNT, accOwner, &types.MsgInitPeriodicLockingAccount{
Owner: ownerAddrStr,
StartTime: currentTime,
LockingPeriods: []types.Period{
@ -49,10 +50,10 @@ func (s *IntegrationTestSuite) TestPeriodicLockingAccount() {
}, sdk.Coins{sdk.NewCoin("stake", math.NewInt(1500))}, nil)
require.NoError(t, err)
addr, err := app.AuthKeeper.AddressCodec().BytesToString(randAcc)
addr, err := s.authKeeper.AddressCodec().BytesToString(randAcc)
require.NoError(t, err)
vals, err := app.StakingKeeper.GetAllValidators(ctx)
vals, err := s.stakingKeeper.GetAllValidators(ctx)
require.NoError(t, err)
val := vals[0]
@ -62,7 +63,7 @@ func (s *IntegrationTestSuite) TestPeriodicLockingAccount() {
ToAddress: addr,
Amount: sdk.Coins{sdk.NewCoin("stake", math.NewInt(100))},
}
err := s.executeTx(ctx, msg, app, accountAddr, accOwner)
err := s.executeTx(ctx, msg, s.accountsKeeper, accountAddr, accOwner)
require.NotNil(t, err)
})
// No token being unlocked yet
@ -72,15 +73,13 @@ func (s *IntegrationTestSuite) TestPeriodicLockingAccount() {
ToAddress: addr,
Amount: sdk.Coins{sdk.NewCoin("stake", math.NewInt(100))},
}
err := s.executeTx(ctx, msg, app, accountAddr, accOwner)
err := s.executeTx(ctx, msg, s.accountsKeeper, accountAddr, accOwner)
require.NotNil(t, err)
})
// Update context time
// After first period 500stake should be unlock
ctx = ctx.WithHeaderInfo(header.Info{
Time: currentTime.Add(time.Minute),
})
ctx = integration.SetHeaderInfo(ctx, header.Info{Time: currentTime.Add(time.Minute)})
// Check if 500 stake is sendable now
t.Run("ok - execute send message", func(t *testing.T) {
@ -89,18 +88,16 @@ func (s *IntegrationTestSuite) TestPeriodicLockingAccount() {
ToAddress: addr,
Amount: sdk.Coins{sdk.NewCoin("stake", math.NewInt(500))},
}
err := s.executeTx(ctx, msg, app, accountAddr, accOwner)
err := s.executeTx(ctx, msg, s.accountsKeeper, accountAddr, accOwner)
require.NoError(t, err)
balance := app.BankKeeper.GetBalance(ctx, randAcc, "stake")
balance := s.bankKeeper.GetBalance(ctx, randAcc, "stake")
require.True(t, balance.Amount.Equal(math.NewInt(500)))
})
// Update context time
// After second period 1000stake should be unlock
ctx = ctx.WithHeaderInfo(header.Info{
Time: currentTime.Add(time.Minute * 2),
})
ctx = integration.SetHeaderInfo(ctx, header.Info{Time: currentTime.Add(time.Minute * 2)})
t.Run("ok - execute delegate message", func(t *testing.T) {
msg := &types.MsgDelegate{
@ -108,20 +105,20 @@ func (s *IntegrationTestSuite) TestPeriodicLockingAccount() {
ValidatorAddress: val.OperatorAddress,
Amount: sdk.NewCoin("stake", math.NewInt(100)),
}
err = s.executeTx(ctx, msg, app, accountAddr, accOwner)
err = s.executeTx(ctx, msg, s.accountsKeeper, accountAddr, accOwner)
require.NoError(t, err)
valbz, err := app.StakingKeeper.ValidatorAddressCodec().StringToBytes(val.OperatorAddress)
valbz, err := s.stakingKeeper.ValidatorAddressCodec().StringToBytes(val.OperatorAddress)
require.NoError(t, err)
del, err := app.StakingKeeper.Delegations.Get(
del, err := s.stakingKeeper.Delegations.Get(
ctx, collections.Join(sdk.AccAddress(accountAddr), sdk.ValAddress(valbz)),
)
require.NoError(t, err)
require.NotNil(t, del)
// check if tracking is updated accordingly
lockupAccountInfoResponse := s.queryLockupAccInfo(ctx, app, accountAddr)
lockupAccountInfoResponse := s.queryLockupAccInfo(ctx, s.accountsKeeper, accountAddr)
delLocking := lockupAccountInfoResponse.DelegatedLocking
require.True(t, delLocking.AmountOf("stake").Equal(math.NewInt(100)))
})
@ -130,11 +127,11 @@ func (s *IntegrationTestSuite) TestPeriodicLockingAccount() {
Sender: ownerAddrStr,
ValidatorAddress: val.OperatorAddress,
}
err = s.executeTx(ctx, msg, app, accountAddr, accOwner)
err = s.executeTx(ctx, msg, s.accountsKeeper, accountAddr, accOwner)
require.NoError(t, err)
})
t.Run("ok - execute undelegate message", func(t *testing.T) {
vals, err := app.StakingKeeper.GetAllValidators(ctx)
vals, err := s.stakingKeeper.GetAllValidators(ctx)
require.NoError(t, err)
val := vals[0]
msg := &types.MsgUndelegate{
@ -142,19 +139,19 @@ func (s *IntegrationTestSuite) TestPeriodicLockingAccount() {
ValidatorAddress: val.OperatorAddress,
Amount: sdk.NewCoin("stake", math.NewInt(100)),
}
err = s.executeTx(ctx, msg, app, accountAddr, accOwner)
err = s.executeTx(ctx, msg, s.accountsKeeper, accountAddr, accOwner)
require.NoError(t, err)
valbz, err := app.StakingKeeper.ValidatorAddressCodec().StringToBytes(val.OperatorAddress)
valbz, err := s.stakingKeeper.ValidatorAddressCodec().StringToBytes(val.OperatorAddress)
require.NoError(t, err)
ubd, err := app.StakingKeeper.GetUnbondingDelegation(
ubd, err := s.stakingKeeper.GetUnbondingDelegation(
ctx, sdk.AccAddress(accountAddr), sdk.ValAddress(valbz),
)
require.NoError(t, err)
require.Equal(t, len(ubd.Entries), 1)
// check if an entry is added
unbondingEntriesResponse := s.queryUnbondingEntries(ctx, app, accountAddr, val.OperatorAddress)
unbondingEntriesResponse := s.queryUnbondingEntries(ctx, s.accountsKeeper, accountAddr, val.OperatorAddress)
entries := unbondingEntriesResponse.UnbondingEntries
require.True(t, entries[0].Amount.Amount.Equal(math.NewInt(100)))
require.True(t, entries[0].ValidatorAddress == val.OperatorAddress)
@ -162,12 +159,10 @@ func (s *IntegrationTestSuite) TestPeriodicLockingAccount() {
// Update context time
// After third period 1500stake should be unlock
ctx = ctx.WithHeaderInfo(header.Info{
Time: currentTime.Add(time.Minute * 3),
})
ctx = integration.SetHeaderInfo(ctx, header.Info{Time: currentTime.Add(time.Minute * 3)})
// trigger endblock for staking to handle matured unbonding delegation
_, err = app.StakingKeeper.EndBlocker(ctx)
_, err = s.stakingKeeper.EndBlocker(ctx)
require.NoError(t, err)
t.Run("ok - execute delegate message", func(t *testing.T) {
@ -176,20 +171,20 @@ func (s *IntegrationTestSuite) TestPeriodicLockingAccount() {
ValidatorAddress: val.OperatorAddress,
Amount: sdk.NewCoin("stake", math.NewInt(100)),
}
err = s.executeTx(ctx, msg, app, accountAddr, accOwner)
err = s.executeTx(ctx, msg, s.accountsKeeper, accountAddr, accOwner)
require.NoError(t, err)
valbz, err := app.StakingKeeper.ValidatorAddressCodec().StringToBytes(val.OperatorAddress)
valbz, err := s.stakingKeeper.ValidatorAddressCodec().StringToBytes(val.OperatorAddress)
require.NoError(t, err)
del, err := app.StakingKeeper.Delegations.Get(
del, err := s.stakingKeeper.Delegations.Get(
ctx, collections.Join(sdk.AccAddress(accountAddr), sdk.ValAddress(valbz)),
)
require.NoError(t, err)
require.NotNil(t, del)
// check if tracking is updated accordingly
lockupAccountInfoResponse := s.queryLockupAccInfo(ctx, app, accountAddr)
lockupAccountInfoResponse := s.queryLockupAccInfo(ctx, s.accountsKeeper, accountAddr)
// check if matured ubd entry cleared
delLocking := lockupAccountInfoResponse.DelegatedLocking
require.True(t, delLocking.AmountOf("stake").Equal(math.ZeroInt()))
@ -197,7 +192,7 @@ func (s *IntegrationTestSuite) TestPeriodicLockingAccount() {
require.True(t, delFree.AmountOf("stake").Equal(math.NewInt(100)))
// check if the entry is removed
unbondingEntriesResponse := s.queryUnbondingEntries(ctx, app, accountAddr, val.OperatorAddress)
unbondingEntriesResponse := s.queryUnbondingEntries(ctx, s.accountsKeeper, accountAddr, val.OperatorAddress)
entries := unbondingEntriesResponse.UnbondingEntries
require.Len(t, entries, 0)
})

View File

@ -13,31 +13,32 @@ import (
types "cosmossdk.io/x/accounts/defaults/lockup/v1"
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
"github.com/cosmos/cosmos-sdk/tests/integration/v2"
sdk "github.com/cosmos/cosmos-sdk/types"
)
func (s *IntegrationTestSuite) TestPermanentLockingAccount() {
t := s.T()
app := setupApp(t)
currentTime := time.Now()
ctx := sdk.NewContext(app.CommitMultiStore(), false, app.Logger()).WithHeaderInfo(header.Info{
Time: currentTime,
})
s.setupStakingParams(ctx, app)
ownerAddrStr, err := app.AuthKeeper.AddressCodec().BytesToString(accOwner)
ctx := s.ctx
ctx = integration.SetHeaderInfo(ctx, header.Info{Time: currentTime})
s.setupStakingParams(ctx, s.stakingKeeper)
ownerAddrStr, err := s.authKeeper.AddressCodec().BytesToString(accOwner)
require.NoError(t, err)
s.fundAccount(app, ctx, accOwner, sdk.Coins{sdk.NewCoin("stake", math.NewInt(1000000))})
s.fundAccount(s.bankKeeper, ctx, accOwner, sdk.Coins{sdk.NewCoin("stake", math.NewInt(1000000))})
randAcc := sdk.AccAddress(secp256k1.GenPrivKey().PubKey().Address())
_, accountAddr, err := app.AccountsKeeper.Init(ctx, lockupaccount.PERMANENT_LOCKING_ACCOUNT, accOwner, &types.MsgInitLockupAccount{
_, accountAddr, err := s.accountsKeeper.Init(ctx, lockupaccount.PERMANENT_LOCKING_ACCOUNT, accOwner, &types.MsgInitLockupAccount{
Owner: ownerAddrStr,
}, sdk.Coins{sdk.NewCoin("stake", math.NewInt(1000))}, nil)
require.NoError(t, err)
addr, err := app.AuthKeeper.AddressCodec().BytesToString(randAcc)
addr, err := s.authKeeper.AddressCodec().BytesToString(randAcc)
require.NoError(t, err)
vals, err := app.StakingKeeper.GetAllValidators(ctx)
vals, err := s.stakingKeeper.GetAllValidators(ctx)
require.NoError(t, err)
val := vals[0]
@ -47,7 +48,7 @@ func (s *IntegrationTestSuite) TestPermanentLockingAccount() {
ToAddress: addr,
Amount: sdk.Coins{sdk.NewCoin("stake", math.NewInt(100))},
}
err := s.executeTx(ctx, msg, app, accountAddr, accOwner)
err := s.executeTx(ctx, msg, s.accountsKeeper, accountAddr, accOwner)
require.NotNil(t, err)
})
t.Run("error - execute send message, insufficient fund", func(t *testing.T) {
@ -56,7 +57,7 @@ func (s *IntegrationTestSuite) TestPermanentLockingAccount() {
ToAddress: addr,
Amount: sdk.Coins{sdk.NewCoin("stake", math.NewInt(100))},
}
err := s.executeTx(ctx, msg, app, accountAddr, accOwner)
err := s.executeTx(ctx, msg, s.accountsKeeper, accountAddr, accOwner)
require.NotNil(t, err)
})
t.Run("ok - execute delegate message", func(t *testing.T) {
@ -65,20 +66,20 @@ func (s *IntegrationTestSuite) TestPermanentLockingAccount() {
ValidatorAddress: val.OperatorAddress,
Amount: sdk.NewCoin("stake", math.NewInt(100)),
}
err = s.executeTx(ctx, msg, app, accountAddr, accOwner)
err = s.executeTx(ctx, msg, s.accountsKeeper, accountAddr, accOwner)
require.NoError(t, err)
valbz, err := app.StakingKeeper.ValidatorAddressCodec().StringToBytes(val.OperatorAddress)
valbz, err := s.stakingKeeper.ValidatorAddressCodec().StringToBytes(val.OperatorAddress)
require.NoError(t, err)
del, err := app.StakingKeeper.Delegations.Get(
del, err := s.stakingKeeper.Delegations.Get(
ctx, collections.Join(sdk.AccAddress(accountAddr), sdk.ValAddress(valbz)),
)
require.NoError(t, err)
require.NotNil(t, del)
// check if tracking is updated accordingly
lockupAccountInfoResponse := s.queryLockupAccInfo(ctx, app, accountAddr)
lockupAccountInfoResponse := s.queryLockupAccInfo(ctx, s.accountsKeeper, accountAddr)
delLocking := lockupAccountInfoResponse.DelegatedLocking
require.True(t, delLocking.AmountOf("stake").Equal(math.NewInt(100)))
})
@ -87,11 +88,11 @@ func (s *IntegrationTestSuite) TestPermanentLockingAccount() {
Sender: ownerAddrStr,
ValidatorAddress: val.OperatorAddress,
}
err = s.executeTx(ctx, msg, app, accountAddr, accOwner)
err = s.executeTx(ctx, msg, s.accountsKeeper, accountAddr, accOwner)
require.NoError(t, err)
})
t.Run("ok - execute undelegate message", func(t *testing.T) {
vals, err := app.StakingKeeper.GetAllValidators(ctx)
vals, err := s.stakingKeeper.GetAllValidators(ctx)
require.NoError(t, err)
val := vals[0]
msg := &types.MsgUndelegate{
@ -99,25 +100,25 @@ func (s *IntegrationTestSuite) TestPermanentLockingAccount() {
ValidatorAddress: val.OperatorAddress,
Amount: sdk.NewCoin("stake", math.NewInt(100)),
}
err = s.executeTx(ctx, msg, app, accountAddr, accOwner)
err = s.executeTx(ctx, msg, s.accountsKeeper, accountAddr, accOwner)
require.NoError(t, err)
valbz, err := app.StakingKeeper.ValidatorAddressCodec().StringToBytes(val.OperatorAddress)
valbz, err := s.stakingKeeper.ValidatorAddressCodec().StringToBytes(val.OperatorAddress)
require.NoError(t, err)
ubd, err := app.StakingKeeper.GetUnbondingDelegation(
ubd, err := s.stakingKeeper.GetUnbondingDelegation(
ctx, sdk.AccAddress(accountAddr), sdk.ValAddress(valbz),
)
require.NoError(t, err)
require.Equal(t, len(ubd.Entries), 1)
// check if an entry is added
unbondingEntriesResponse := s.queryUnbondingEntries(ctx, app, accountAddr, val.OperatorAddress)
unbondingEntriesResponse := s.queryUnbondingEntries(ctx, s.accountsKeeper, accountAddr, val.OperatorAddress)
entries := unbondingEntriesResponse.UnbondingEntries
require.True(t, entries[0].Amount.Amount.Equal(math.NewInt(100)))
require.True(t, entries[0].ValidatorAddress == val.OperatorAddress)
})
s.fundAccount(app, ctx, accountAddr, sdk.Coins{sdk.NewCoin("stake", math.NewInt(1000))})
s.fundAccount(s.bankKeeper, ctx, accountAddr, sdk.Coins{sdk.NewCoin("stake", math.NewInt(1000))})
t.Run("ok - execute send message", func(t *testing.T) {
msg := &types.MsgSend{
@ -125,20 +126,18 @@ func (s *IntegrationTestSuite) TestPermanentLockingAccount() {
ToAddress: addr,
Amount: sdk.Coins{sdk.NewCoin("stake", math.NewInt(100))},
}
err := s.executeTx(ctx, msg, app, accountAddr, accOwner)
err := s.executeTx(ctx, msg, s.accountsKeeper, accountAddr, accOwner)
require.NoError(t, err)
balance := app.BankKeeper.GetBalance(ctx, randAcc, "stake")
balance := s.bankKeeper.GetBalance(ctx, randAcc, "stake")
require.True(t, balance.Amount.Equal(math.NewInt(100)))
})
// Update context time
ctx = ctx.WithHeaderInfo(header.Info{
Time: currentTime.Add(time.Second * 11),
})
ctx = integration.SetHeaderInfo(ctx, header.Info{Time: currentTime.Add(time.Second * 11)})
// trigger endblock for staking to handle matured unbonding delegation
_, err = app.StakingKeeper.EndBlocker(ctx)
_, err = s.stakingKeeper.EndBlocker(ctx)
require.NoError(t, err)
t.Run("ok - execute delegate message", func(t *testing.T) {
@ -147,26 +146,26 @@ func (s *IntegrationTestSuite) TestPermanentLockingAccount() {
ValidatorAddress: val.OperatorAddress,
Amount: sdk.NewCoin("stake", math.NewInt(10)),
}
err = s.executeTx(ctx, msg, app, accountAddr, accOwner)
err = s.executeTx(ctx, msg, s.accountsKeeper, accountAddr, accOwner)
require.NoError(t, err)
valbz, err := app.StakingKeeper.ValidatorAddressCodec().StringToBytes(val.OperatorAddress)
valbz, err := s.stakingKeeper.ValidatorAddressCodec().StringToBytes(val.OperatorAddress)
require.NoError(t, err)
del, err := app.StakingKeeper.Delegations.Get(
del, err := s.stakingKeeper.Delegations.Get(
ctx, collections.Join(sdk.AccAddress(accountAddr), sdk.ValAddress(valbz)),
)
require.NoError(t, err)
require.NotNil(t, del)
// check if tracking is updated accordingly
lockupAccountInfoResponse := s.queryLockupAccInfo(ctx, app, accountAddr)
lockupAccountInfoResponse := s.queryLockupAccInfo(ctx, s.accountsKeeper, accountAddr)
delLocking := lockupAccountInfoResponse.DelegatedLocking
// matured ubd entry should be cleared so del locking should only be 10
require.True(t, delLocking.AmountOf("stake").Equal(math.NewInt(10)))
// check if the entry is removed
unbondingEntriesResponse := s.queryUnbondingEntries(ctx, app, accountAddr, val.OperatorAddress)
unbondingEntriesResponse := s.queryUnbondingEntries(ctx, s.accountsKeeper, accountAddr, val.OperatorAddress)
entries := unbondingEntriesResponse.UnbondingEntries
require.Len(t, entries, 0)
})

View File

@ -0,0 +1,253 @@
package lockup
import (
"context"
"time"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
"cosmossdk.io/core/router"
"cosmossdk.io/core/transaction"
"cosmossdk.io/depinject"
"cosmossdk.io/log"
"cosmossdk.io/runtime/v2"
"cosmossdk.io/x/accounts"
basedepinject "cosmossdk.io/x/accounts/defaults/base/depinject"
lockupdepinject "cosmossdk.io/x/accounts/defaults/lockup/depinject"
types "cosmossdk.io/x/accounts/defaults/lockup/v1"
_ "cosmossdk.io/x/bank" // import as blank for app wiring
bankkeeper "cosmossdk.io/x/bank/keeper"
"cosmossdk.io/x/bank/testutil"
banktypes "cosmossdk.io/x/bank/types"
_ "cosmossdk.io/x/consensus"
_ "cosmossdk.io/x/distribution" // import as blank for app wiring
distrkeeper "cosmossdk.io/x/distribution/keeper"
distrtypes "cosmossdk.io/x/distribution/types"
_ "cosmossdk.io/x/staking" // import as blank for app wiring
stakingkeeper "cosmossdk.io/x/staking/keeper"
stakingtypes "cosmossdk.io/x/staking/types"
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
"github.com/cosmos/cosmos-sdk/tests/integration/v2"
"github.com/cosmos/cosmos-sdk/testutil/configurator"
sdk "github.com/cosmos/cosmos-sdk/types"
authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper"
_ "github.com/cosmos/cosmos-sdk/x/auth/tx/config" // import as blank for app wiring``
_ "github.com/cosmos/cosmos-sdk/x/auth/vesting" // import as blank for app wiring
_ "github.com/cosmos/cosmos-sdk/x/genutil" // import as blank for app wiring
)
var (
ownerAddr = secp256k1.GenPrivKey().PubKey().Address()
accOwner = sdk.AccAddress(ownerAddr)
)
type IntegrationTestSuite struct {
suite.Suite
app *integration.App
ctx context.Context
authKeeper authkeeper.AccountKeeper
accountsKeeper accounts.Keeper
bankKeeper bankkeeper.BaseKeeper
stakingKeeper *stakingkeeper.Keeper
distrKeeper distrkeeper.Keeper
}
func NewIntegrationTestSuite() *IntegrationTestSuite {
return &IntegrationTestSuite{}
}
func (s *IntegrationTestSuite) SetupSuite() {
s.T().Log("setting up integration test suite")
moduleConfigs := []configurator.ModuleOption{
configurator.AccountsModule(),
configurator.AuthModule(),
configurator.BankModule(),
configurator.VestingModule(),
configurator.StakingModule(),
configurator.TxModule(),
configurator.ValidateModule(),
configurator.ConsensusModule(),
configurator.GenutilModule(),
configurator.DistributionModule(),
}
var err error
startupCfg := integration.DefaultStartUpConfig(s.T())
msgRouterService := integration.NewRouterService()
s.registerMsgRouterService(msgRouterService)
var routerFactory runtime.RouterServiceFactory = func(_ []byte) router.Service {
return msgRouterService
}
queryRouterService := integration.NewRouterService()
s.registerQueryRouterService(queryRouterService)
serviceBuilder := runtime.NewRouterBuilder(routerFactory, queryRouterService)
startupCfg.BranchService = &integration.BranchService{}
startupCfg.RouterServiceBuilder = serviceBuilder
startupCfg.HeaderService = &integration.HeaderService{}
startupCfg.GasService = &integration.GasService{}
s.app, err = integration.NewApp(
depinject.Configs(configurator.NewAppV2Config(moduleConfigs...), depinject.Provide(
// inject desired account types:
basedepinject.ProvideAccount,
// provide base account options
basedepinject.ProvideSecp256K1PubKey,
// inject desired account types:
lockupdepinject.ProvideAllLockupAccounts,
), depinject.Supply(log.NewNopLogger())),
startupCfg,
&s.bankKeeper, &s.accountsKeeper, &s.authKeeper, &s.stakingKeeper, &s.distrKeeper)
require.NoError(s.T(), err)
s.ctx = s.app.StateLatestContext(s.T())
}
func (s *IntegrationTestSuite) registerMsgRouterService(router *integration.RouterService) {
// register custom router service
bankSendHandler := func(ctx context.Context, req transaction.Msg) (transaction.Msg, error) {
msg, ok := req.(*banktypes.MsgSend)
if !ok {
return nil, integration.ErrInvalidMsgType
}
msgServer := bankkeeper.NewMsgServerImpl(s.bankKeeper)
resp, err := msgServer.Send(ctx, msg)
return resp, err
}
stakingDelegateHandler := func(ctx context.Context, req transaction.Msg) (transaction.Msg, error) {
msg, ok := req.(*stakingtypes.MsgDelegate)
if !ok {
return nil, integration.ErrInvalidMsgType
}
msgServer := stakingkeeper.NewMsgServerImpl(s.stakingKeeper)
resp, err := msgServer.Delegate(ctx, msg)
return resp, err
}
stakingUndelegateHandler := func(ctx context.Context, req transaction.Msg) (transaction.Msg, error) {
msg, ok := req.(*stakingtypes.MsgUndelegate)
if !ok {
return nil, integration.ErrInvalidMsgType
}
msgServer := stakingkeeper.NewMsgServerImpl(s.stakingKeeper)
resp, err := msgServer.Undelegate(ctx, msg)
return resp, err
}
distrWithdrawRewardHandler := func(ctx context.Context, req transaction.Msg) (transaction.Msg, error) {
msg, ok := req.(*distrtypes.MsgWithdrawDelegatorReward)
if !ok {
return nil, integration.ErrInvalidMsgType
}
msgServer := distrkeeper.NewMsgServerImpl(s.distrKeeper)
resp, err := msgServer.WithdrawDelegatorReward(ctx, msg)
return resp, err
}
router.RegisterHandler(bankSendHandler, "cosmos.bank.v1beta1.MsgSend")
router.RegisterHandler(stakingDelegateHandler, "cosmos.staking.v1beta1.MsgDelegate")
router.RegisterHandler(stakingUndelegateHandler, "cosmos.staking.v1beta1.MsgUndelegate")
router.RegisterHandler(distrWithdrawRewardHandler, "cosmos.distribution.v1beta1.MsgWithdrawDelegatorReward")
}
func (s *IntegrationTestSuite) registerQueryRouterService(router *integration.RouterService) {
// register custom router service
stakingParamsQueryHandler := func(ctx context.Context, msg transaction.Msg) (transaction.Msg, error) {
req, ok := msg.(*stakingtypes.QueryParamsRequest)
if !ok {
return nil, integration.ErrInvalidMsgType
}
qs := stakingkeeper.NewQuerier(s.stakingKeeper)
resp, err := qs.Params(ctx, req)
return resp, err
}
stakingUnbondingQueryHandler := func(ctx context.Context, msg transaction.Msg) (transaction.Msg, error) {
req, ok := msg.(*stakingtypes.QueryUnbondingDelegationRequest)
if !ok {
return nil, integration.ErrInvalidMsgType
}
qs := stakingkeeper.NewQuerier(s.stakingKeeper)
resp, err := qs.UnbondingDelegation(ctx, req)
return resp, err
}
bankBalanceQueryHandler := func(ctx context.Context, msg transaction.Msg) (transaction.Msg, error) {
req, ok := msg.(*banktypes.QueryBalanceRequest)
if !ok {
return nil, integration.ErrInvalidMsgType
}
qs := bankkeeper.NewQuerier(&s.bankKeeper)
resp, err := qs.Balance(ctx, req)
return resp, err
}
router.RegisterHandler(stakingParamsQueryHandler, "cosmos.staking.v1beta1.QueryParamsRequest")
router.RegisterHandler(stakingUnbondingQueryHandler, "cosmos.staking.v1beta1.QueryUnbondingDelegationRequest")
router.RegisterHandler(bankBalanceQueryHandler, "cosmos.bank.v1beta1.QueryBalanceRequest")
}
func (s *IntegrationTestSuite) TearDownSuite() {
s.T().Log("tearing down integration test suite")
}
func (s *IntegrationTestSuite) executeTx(ctx context.Context, msg sdk.Msg, ak accounts.Keeper, accAddr, sender []byte) error {
_, err := ak.Execute(ctx, accAddr, sender, msg, nil)
return err
}
func (s *IntegrationTestSuite) queryAcc(ctx context.Context, req sdk.Msg, ak accounts.Keeper, accAddr []byte) (transaction.Msg, error) {
resp, err := ak.Query(ctx, accAddr, req)
return resp, err
}
func (s *IntegrationTestSuite) fundAccount(bk bankkeeper.Keeper, ctx context.Context, addr sdk.AccAddress, amt sdk.Coins) {
require.NoError(s.T(), testutil.FundAccount(ctx, bk, addr, amt))
}
func (s *IntegrationTestSuite) queryLockupAccInfo(ctx context.Context, ak accounts.Keeper, accAddr []byte) *types.QueryLockupAccountInfoResponse {
req := &types.QueryLockupAccountInfoRequest{}
resp, err := s.queryAcc(ctx, req, ak, accAddr)
require.NoError(s.T(), err)
require.NotNil(s.T(), resp)
lockupAccountInfoResponse, ok := resp.(*types.QueryLockupAccountInfoResponse)
require.True(s.T(), ok)
return lockupAccountInfoResponse
}
func (s *IntegrationTestSuite) queryUnbondingEntries(ctx context.Context, ak accounts.Keeper, accAddr []byte, valAddr string) *types.QueryUnbondingEntriesResponse {
req := &types.QueryUnbondingEntriesRequest{
ValidatorAddress: valAddr,
}
resp, err := s.queryAcc(ctx, req, ak, accAddr)
require.NoError(s.T(), err)
require.NotNil(s.T(), resp)
unbondingEntriesResponse, ok := resp.(*types.QueryUnbondingEntriesResponse)
require.True(s.T(), ok)
return unbondingEntriesResponse
}
func (s *IntegrationTestSuite) setupStakingParams(ctx context.Context, sk *stakingkeeper.Keeper) {
params, err := sk.Params.Get(ctx)
require.NoError(s.T(), err)
// update unbonding time
params.UnbondingTime = time.Duration(time.Second * 10)
err = sk.Params.Set(ctx, params)
require.NoError(s.T(), err)
}

View File

@ -15,6 +15,7 @@ import (
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
"github.com/cosmos/cosmos-sdk/tests/integration/v2"
sdk "github.com/cosmos/cosmos-sdk/types"
)
@ -24,12 +25,12 @@ func TestIntegrationTestSuite(t *testing.T) {
// TestSimpleSendProposal creates a multisig account with 1 member, sends a tx, votes and executes it.
func (s *IntegrationTestSuite) TestSimpleSendProposal() {
ctx := sdk.NewContext(s.app.CommitMultiStore(), false, s.app.Logger()).WithHeaderInfo(header.Info{
Time: time.Now(),
})
ctx := s.app.StateLatestContext(s.T())
ctx = integration.SetHeaderInfo(ctx, header.Info{Time: time.Now()})
randAcc := sdk.AccAddress(secp256k1.GenPrivKey().PubKey().Address())
addr, err := s.app.AuthKeeper.AddressCodec().BytesToString(randAcc)
addr, err := s.authKeeper.AddressCodec().BytesToString(randAcc)
s.NoError(err)
initialMembers := map[string]uint64{
@ -37,7 +38,7 @@ func (s *IntegrationTestSuite) TestSimpleSendProposal() {
}
accountAddr, accAddrStr := s.initAccount(ctx, s.members[0], initialMembers)
balance := s.app.BankKeeper.GetBalance(ctx, randAcc, "stake")
balance := s.bankKeeper.GetBalance(ctx, randAcc, "stake")
s.Equal(math.NewInt(0), balance.Amount)
// do a simple bank send
@ -70,37 +71,39 @@ func (s *IntegrationTestSuite) TestSimpleSendProposal() {
s.NoError(err)
foundPropResult := false
for _, v := range ctx.EventManager().Events() {
if v.Type == "proposal_tally" {
events := integration.EventsFromContext(ctx)
for _, e := range events {
if e.Type == "proposal_tally" {
foundPropResult = true
status, found := v.GetAttribute("status")
attr, found := integration.GetAttribute(e, "status")
s.True(found)
s.Equal(v1.ProposalStatus_PROPOSAL_STATUS_PASSED.String(), status.Value)
s.Equal(v1.ProposalStatus_PROPOSAL_STATUS_PASSED.String(), attr.Value)
yesVotes, found := v.GetAttribute("yes_votes")
yesVotes, found := integration.GetAttribute(e, "yes_votes")
s.True(found)
s.Equal("100", yesVotes.Value)
noVotes, found := v.GetAttribute("no_votes")
noVotes, found := integration.GetAttribute(e, "no_votes")
s.True(found)
s.Equal("0", noVotes.Value)
propID, found := v.GetAttribute("proposal_id")
propID, found := integration.GetAttribute(e, "proposal_id")
s.True(found)
s.Equal("0", propID.Value)
execErr, found := v.GetAttribute("exec_err")
execErr, found := integration.GetAttribute(e, "exec_err")
s.True(found)
s.Equal("<nil>", execErr.Value)
rejectErr, found := v.GetAttribute("reject_err")
rejectErr, found := integration.GetAttribute(e, "reject_err")
s.True(found)
s.Equal("<nil>", rejectErr.Value)
}
}
s.True(foundPropResult)
balance = s.app.BankKeeper.GetBalance(ctx, randAcc, "stake")
balance = s.bankKeeper.GetBalance(ctx, randAcc, "stake")
s.Equal(int64(100), balance.Amount.Int64())
// try to execute again, should fail
@ -111,9 +114,9 @@ func (s *IntegrationTestSuite) TestSimpleSendProposal() {
// TestConfigUpdate creates a multisig with 1 member, adds 2 more members and
// changes the config to require 2/3 majority (also through a proposal).
func (s *IntegrationTestSuite) TestConfigUpdate() {
ctx := sdk.NewContext(s.app.CommitMultiStore(), false, s.app.Logger()).WithHeaderInfo(header.Info{
Time: time.Now(),
})
ctx := s.app.StateLatestContext(s.T())
ctx = integration.SetHeaderInfo(ctx, header.Info{Time: time.Now()})
initialMembers := map[string]uint64{
s.membersAddr[0]: 100,
@ -223,35 +226,37 @@ func (s *IntegrationTestSuite) TestConfigUpdate() {
err = s.executeProposal(ctx, accountAddr, s.members[0], 1)
s.ErrorContains(err, "voting period has not ended yet, and early execution is not enabled")
headerInfo := ctx.HeaderInfo()
headerInfo := integration.HeaderInfoFromContext(ctx)
headerInfo.Time = headerInfo.Time.Add(time.Second * 121)
ctx = ctx.WithHeaderInfo(headerInfo)
ctx = integration.SetHeaderInfo(ctx, headerInfo)
// now it should work, but the proposal will fail
err = s.executeProposal(ctx, accountAddr, s.members[0], 1)
s.NoError(err)
foundPropResult := false
for _, v := range ctx.EventManager().Events() {
if v.Type == "proposal_tally" {
propID, found := v.GetAttribute("proposal_id")
events := integration.EventsFromContext(ctx)
for _, e := range events {
if e.Type == "proposal_tally" {
propID, found := integration.GetAttribute(e, "proposal_id")
s.True(found)
if propID.Value == "1" {
foundPropResult = true
status, found := v.GetAttribute("status")
status, found := integration.GetAttribute(e, "status")
s.True(found)
s.Equal(v1.ProposalStatus_PROPOSAL_STATUS_REJECTED.String(), status.Value)
// exec_err is nil because the proposal didn't execute
execErr, found := v.GetAttribute("exec_err")
execErr, found := integration.GetAttribute(e, "exec_err")
s.True(found)
s.Equal("<nil>", execErr.Value)
rejectErr, found := v.GetAttribute("reject_err")
rejectErr, found := integration.GetAttribute(e, "reject_err")
s.True(found)
s.Equal("threshold not reached", rejectErr.Value)
}
}
}
s.True(foundPropResult)

View File

@ -0,0 +1,223 @@
package multisig
import (
"context"
"github.com/stretchr/testify/suite"
"cosmossdk.io/core/router"
"cosmossdk.io/core/transaction"
"cosmossdk.io/depinject"
"cosmossdk.io/log"
"cosmossdk.io/math"
"cosmossdk.io/runtime/v2"
"cosmossdk.io/x/accounts"
basedepinject "cosmossdk.io/x/accounts/defaults/base/depinject"
multisigdepinject "cosmossdk.io/x/accounts/defaults/multisig/depinject"
v1 "cosmossdk.io/x/accounts/defaults/multisig/v1"
accountsv1 "cosmossdk.io/x/accounts/v1"
_ "cosmossdk.io/x/bank" // import as blank for app wiring
bankkeeper "cosmossdk.io/x/bank/keeper"
"cosmossdk.io/x/bank/testutil"
banktypes "cosmossdk.io/x/bank/types"
_ "cosmossdk.io/x/consensus"
_ "cosmossdk.io/x/distribution" // import as blank for app wiring
distrkeeper "cosmossdk.io/x/distribution/keeper"
_ "cosmossdk.io/x/staking" // import as blank for app wiring
stakingkeeper "cosmossdk.io/x/staking/keeper"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
"github.com/cosmos/cosmos-sdk/tests/integration/v2"
"github.com/cosmos/cosmos-sdk/testutil/configurator"
sdk "github.com/cosmos/cosmos-sdk/types"
authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper"
_ "github.com/cosmos/cosmos-sdk/x/auth/tx/config" // import as blank for app wiring``
_ "github.com/cosmos/cosmos-sdk/x/auth/vesting" // import as blank for app wiring
_ "github.com/cosmos/cosmos-sdk/x/genutil" // import as blank for app wiring
)
type IntegrationTestSuite struct {
suite.Suite
app *integration.App
members []sdk.AccAddress
membersAddr []string
authKeeper authkeeper.AccountKeeper
accountsKeeper accounts.Keeper
bankKeeper bankkeeper.BaseKeeper
stakingKeeper *stakingkeeper.Keeper
distrKeeper distrkeeper.Keeper
}
func NewIntegrationTestSuite() *IntegrationTestSuite {
return &IntegrationTestSuite{}
}
func (s *IntegrationTestSuite) SetupSuite() {
moduleConfigs := []configurator.ModuleOption{
configurator.AccountsModule(),
configurator.AuthModule(),
configurator.BankModule(),
configurator.VestingModule(),
configurator.StakingModule(),
configurator.TxModule(),
configurator.ValidateModule(),
configurator.ConsensusModule(),
configurator.GenutilModule(),
configurator.DistributionModule(),
}
var err error
startupCfg := integration.DefaultStartUpConfig(s.T())
msgRouterService := integration.NewRouterService()
s.registerMsgRouterService(msgRouterService)
var routerFactory runtime.RouterServiceFactory = func(_ []byte) router.Service {
return msgRouterService
}
queryRouterService := integration.NewRouterService()
s.registerQueryRouterService(queryRouterService)
serviceBuilder := runtime.NewRouterBuilder(routerFactory, queryRouterService)
startupCfg.BranchService = &integration.BranchService{}
startupCfg.RouterServiceBuilder = serviceBuilder
startupCfg.HeaderService = &integration.HeaderService{}
startupCfg.GasService = &integration.GasService{}
s.app, err = integration.NewApp(
depinject.Configs(configurator.NewAppV2Config(moduleConfigs...), depinject.Provide(
// inject desired account types:
basedepinject.ProvideAccount,
// provide base account options
basedepinject.ProvideSecp256K1PubKey,
// inject desired account types:
multisigdepinject.ProvideAccount,
), depinject.Supply(log.NewNopLogger())),
startupCfg,
&s.bankKeeper, &s.accountsKeeper, &s.authKeeper, &s.stakingKeeper, &s.distrKeeper)
s.NoError(err)
s.members = []sdk.AccAddress{}
for i := 0; i < 10; i++ {
addr := secp256k1.GenPrivKey().PubKey().Address()
addrStr, err := s.authKeeper.AddressCodec().BytesToString(addr)
s.NoError(err)
s.membersAddr = append(s.membersAddr, addrStr)
s.members = append(s.members, sdk.AccAddress(addr))
}
}
func (s *IntegrationTestSuite) registerMsgRouterService(router *integration.RouterService) {
// register custom router service
bankSendHandler := func(ctx context.Context, req transaction.Msg) (transaction.Msg, error) {
msg, ok := req.(*banktypes.MsgSend)
if !ok {
return nil, integration.ErrInvalidMsgType
}
msgServer := bankkeeper.NewMsgServerImpl(s.bankKeeper)
resp, err := msgServer.Send(ctx, msg)
return resp, err
}
// register custom router service
accountsExeccHandler := func(ctx context.Context, req transaction.Msg) (transaction.Msg, error) {
msg, ok := req.(*accountsv1.MsgExecute)
if !ok {
return nil, integration.ErrInvalidMsgType
}
msgServer := accounts.NewMsgServer(s.accountsKeeper)
resp, err := msgServer.Execute(ctx, msg)
return resp, err
}
router.RegisterHandler(bankSendHandler, "cosmos.bank.v1beta1.MsgSend")
router.RegisterHandler(accountsExeccHandler, "cosmos.accounts.v1.MsgExecute")
}
func (s *IntegrationTestSuite) registerQueryRouterService(router *integration.RouterService) {
// register custom router service
bankBalanceQueryHandler := func(ctx context.Context, msg transaction.Msg) (transaction.Msg, error) {
req, ok := msg.(*banktypes.QueryBalanceRequest)
if !ok {
return nil, integration.ErrInvalidMsgType
}
qs := bankkeeper.NewQuerier(&s.bankKeeper)
resp, err := qs.Balance(ctx, req)
return resp, err
}
router.RegisterHandler(bankBalanceQueryHandler, "cosmos.bank.v1beta1.QueryBalanceRequest")
}
func (s *IntegrationTestSuite) TearDownSuite() {}
func (s *IntegrationTestSuite) executeTx(ctx context.Context, msg sdk.Msg, accAddr, sender []byte) error {
_, err := s.accountsKeeper.Execute(ctx, accAddr, sender, msg, nil)
return err
}
func (s *IntegrationTestSuite) queryAcc(ctx context.Context, req sdk.Msg, accAddr []byte) (transaction.Msg, error) {
resp, err := s.accountsKeeper.Query(ctx, accAddr, req)
return resp, err
}
func (s *IntegrationTestSuite) fundAccount(ctx context.Context, addr sdk.AccAddress, amt sdk.Coins) {
s.NoError(testutil.FundAccount(ctx, s.bankKeeper, addr, amt))
}
// initAccount initializes a multisig account with the given members and powers
// and returns the account address
func (s *IntegrationTestSuite) initAccount(ctx context.Context, sender []byte, membersPowers map[string]uint64) ([]byte, string) {
s.fundAccount(ctx, sender, sdk.Coins{sdk.NewCoin("stake", math.NewInt(1000000))})
members := []*v1.Member{}
for addrStr, power := range membersPowers {
members = append(members, &v1.Member{Address: addrStr, Weight: power})
}
_, accountAddr, err := s.accountsKeeper.Init(ctx, "multisig", sender,
&v1.MsgInit{
Members: members,
Config: &v1.Config{
Threshold: 100,
Quorum: 100,
VotingPeriod: 120,
Revote: false,
EarlyExecution: true,
},
}, sdk.Coins{sdk.NewCoin("stake", math.NewInt(1000))}, nil)
s.NoError(err)
accountAddrStr, err := s.authKeeper.AddressCodec().BytesToString(accountAddr)
s.NoError(err)
return accountAddr, accountAddrStr
}
// createProposal
func (s *IntegrationTestSuite) createProposal(ctx context.Context, accAddr, sender []byte, msgs ...*codectypes.Any) {
propReq := &v1.MsgCreateProposal{
Proposal: &v1.Proposal{
Title: "test",
Summary: "test",
Messages: msgs,
},
}
err := s.executeTx(ctx, propReq, accAddr, sender)
s.NoError(err)
}
func (s *IntegrationTestSuite) executeProposal(ctx context.Context, accAddr, sender []byte, proposalID uint64) error {
execReq := &v1.MsgExecuteProposal{
ProposalId: proposalID,
}
return s.executeTx(ctx, execReq, accAddr, sender)
}

View File

@ -126,6 +126,20 @@ func GetAttributes(e []event.Event, key string) ([]event.Attribute, bool) {
return attrs, len(attrs) > 0
}
func GetAttribute(e event.Event, key string) (event.Attribute, bool) {
attributes, err := e.Attributes()
if err != nil {
return event.Attribute{}, false
}
for _, attr := range attributes {
if attr.Key == key {
return attr, true
}
}
return event.Attribute{}, false
}
func GasMeterFromContext(ctx context.Context) gas.Meter {
iCtx, ok := ctx.Value(contextKey).(*integrationContext)
if !ok {