Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
MAC authored and MAC committed Jan 8, 2024
1 parent b9e3719 commit b875378
Showing 1 changed file with 16 additions and 98 deletions.
114 changes: 16 additions & 98 deletions x/cudoMint/ante/ante_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,9 @@ import (
"github.com/CudoVentures/cudos-node/app"
"github.com/CudoVentures/cudos-node/app/apptesting"
appparams "github.com/CudoVentures/cudos-node/app/params"
simulation "github.com/CudoVentures/cudos-node/x/simulation"
"github.com/cosmos/cosmos-sdk/client"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
"github.com/cosmos/cosmos-sdk/testutil/testdata"
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
)
Expand Down Expand Up @@ -72,7 +70,6 @@ func (suite *AnteTestSuite) CreateTestTx(privs []cryptotypes.PrivKey, accNums []
if err != nil {
return nil, err
}

// Second round: all signer infos are set, so each signer can sign.
sigsV2 = []signing.SignatureV2{}
for i, priv := range privs {
Expand All @@ -81,6 +78,7 @@ func (suite *AnteTestSuite) CreateTestTx(privs []cryptotypes.PrivKey, accNums []
AccountNumber: accNums[i],
Sequence: accSeqs[i],
}

sigV2, err := tx.SignWithPrivKey(
suite.clientCtx.TxConfig.SignModeHandler().DefaultMode(), signerData,
suite.txBuilder, priv, suite.clientCtx.TxConfig, accSeqs[i])
Expand All @@ -90,6 +88,8 @@ func (suite *AnteTestSuite) CreateTestTx(privs []cryptotypes.PrivKey, accNums []

sigsV2 = append(sigsV2, sigV2)
}
fmt.Printf("sigsV2: %+v \n", sigsV2)

err = suite.txBuilder.SetSignatures(sigsV2...)
if err != nil {
return nil, err
Expand All @@ -106,9 +106,8 @@ func (suite *AnteTestSuite) CreateValidator(tokens sdk.Int) (cryptotypes.PrivKey
suite.Ctx = suite.App.NewContext(false, suite.Ctx.BlockHeader())

priv, pub, addr := testdata.KeyTestPubAddr()
consKey, valPub, _ := suite.Ed25519PubAddr()
_, valPub, _ := suite.Ed25519PubAddr()
valAddr := sdk.ValAddress(addr)
fmt.Println("valAddr", valAddr.String())
sendCoins := sdk.NewCoins(sdk.NewCoin(appparams.BondDenom, tokens.Mul(sdk.NewInt(2))))
suite.FundAcc(addr, sendCoins)

Expand All @@ -117,14 +116,6 @@ func (suite *AnteTestSuite) CreateValidator(tokens sdk.Int) (cryptotypes.PrivKey
account.SetPubKey(pub)
account.SetAccountNumber(0)
account.SetSequence(0)
// create sim account to pass into deliver tx context
// TODO: refactor, why need simAccount?
simAccount := simtypes.Account{
PrivKey: priv,
Address: addr,
PubKey: pub,
ConsKey: consKey,
}
// set account in deliver tx context
suite.App.AccountKeeper.SetAccount(suite.Ctx, account)

Expand All @@ -148,14 +139,20 @@ func (suite *AnteTestSuite) CreateValidator(tokens sdk.Int) (cryptotypes.PrivKey
return nil, nil, stakingtypes.Validator{}, nil, err
}

// deliver Tx
_, _, err = simulation.GenAndDeliverTx(suite.App.BaseApp, suite.clientCtx.TxConfig, msgCreateValidator, sdk.NewCoins(), suite.Ctx, simAccount, suite.App.AccountKeeper, stakingtypes.ModuleName)
err = suite.txBuilder.SetMsgs(msgCreateValidator)
if err != nil {
return nil, nil, stakingtypes.Validator{}, nil, err
}
tx, err := suite.CreateTestTx([]cryptotypes.PrivKey{priv}, []uint64{account.GetAccountNumber()}, []uint64{account.GetSequence()}, suite.Ctx.ChainID())
if err != nil {
return nil, nil, stakingtypes.Validator{}, nil, err
}
_, _, err = suite.App.Deliver(suite.clientCtx.TxConfig.TxEncoder(), tx)

if err != nil {
return nil, nil, stakingtypes.Validator{}, nil, err
}

fmt.Println("deliver tx success")
// turn block for validator updates
suite.App.EndBlock(abci.RequestEndBlock{Height: suite.Ctx.BlockHeight()})
suite.App.Commit()
Expand All @@ -174,10 +171,11 @@ func (suite *AnteTestSuite) CreateValidator(tokens sdk.Int) (cryptotypes.PrivKey
func (suite *AnteTestSuite) TestAnte_CreateAndEditValidator() {
MinSelfDelegation, _ := sdk.NewIntFromString("2000000000000000000000000")
suite.SetupTest() // setup

suite.txBuilder = suite.clientCtx.TxConfig.NewTxBuilder()
suite.txBuilder.SetGasLimit(400_000)
suite.txBuilder.SetGasLimit(400_000_000)

priv1, _, val1, acc1, err := suite.CreateValidator(MinSelfDelegation)
_, _, _, _, err := suite.CreateValidator(MinSelfDelegation)
suite.Require().NoError(err)

suite.Ctx = suite.Ctx.WithBlockHeight(suite.Ctx.BlockHeight() + 1)
Expand All @@ -187,86 +185,6 @@ func (suite *AnteTestSuite) TestAnte_CreateAndEditValidator() {
suite.App.EndBlock(abci.RequestEndBlock{Height: suite.Ctx.BlockHeight()})
suite.App.Commit()

// create validator tx with not enough min self delegation
_, _, _, _, err = suite.CreateValidator(MinSelfDelegation.Sub(sdk.NewInt(1)))
suite.Require().Error(err, "should not be able to create validator with less than min self delegation")

// Try to edit validator with not enough min self delegation

tx, err := suite.CreateTestTx([]cryptotypes.PrivKey{priv1}, []uint64{acc1.GetAccountNumber()}, []uint64{acc1.GetSequence()}, suite.Ctx.ChainID())
suite.Require().NoError(err)
invalidtarget := MinSelfDelegation.Sub(sdk.NewInt(1))
editmsg := stakingtypes.NewMsgEditValidator(
val1.GetOperator(),
val1.Description, &val1.Commission.Rate, &invalidtarget,
)
err = suite.txBuilder.SetMsgs(editmsg)
suite.Require().NoError(err)

suite.App.BeginBlock(abci.RequestBeginBlock{Header: suite.Ctx.BlockHeader()})

suite.Ctx = suite.App.NewContext(false, suite.Ctx.BlockHeader())
_, checkRes, err := suite.App.Check(suite.clientCtx.TxConfig.TxEncoder(), tx)

fmt.Printf("Signer data: %+v \n", tx.GetSigners()[0].String())
fmt.Printf("check response: %+v, error = %v \n", checkRes, err)
suite.Require().Error(err, "should not be able to edit validator with less than min self delegation")

_, deliverRes, err := suite.App.Deliver(suite.clientCtx.TxConfig.TxEncoder(), tx)
fmt.Printf("deliver response: %+v, error = %v \n", deliverRes, err)
suite.App.EndBlock(abci.RequestEndBlock{Height: suite.Ctx.BlockHeight()})
suite.App.Commit()
// check and update account keeper
acc := suite.App.AccountKeeper.GetAccount(suite.CheckCtx, acc1.GetAddress())
checkSeq := acc.GetSequence()

// checkSeq not updated when checkTx fails
suite.Require().Equal(uint64(1), checkSeq)
acc = suite.App.AccountKeeper.GetAccount(suite.Ctx, acc.GetAddress())
deliverSeq := acc.GetSequence()

// deliverSeq not updated when deliverTx fails
suite.Require().Equal(uint64(1), deliverSeq)

// Try to edit validator with enough min self delegation
fmt.Println("try to edit validator with enough min self delegation")
tx, err = suite.CreateTestTx([]cryptotypes.PrivKey{priv1}, []uint64{acc1.GetAccountNumber()}, []uint64{acc1.GetSequence()}, suite.Ctx.ChainID())
suite.Require().NoError(err)
EnoughMinSelfDelegation := MinSelfDelegation.Add(sdk.NewInt(1))
editmsg = stakingtypes.NewMsgEditValidator(
val1.GetOperator(),
val1.Description, &val1.Commission.Rate, &EnoughMinSelfDelegation)
err = suite.txBuilder.SetMsgs(editmsg)
suite.Require().NoError(err)
fmt.Println("block height", suite.Ctx.BlockHeight())
suite.Ctx = suite.Ctx.WithBlockHeight(suite.Ctx.BlockHeight() + 1)
suite.App.BeginBlock(abci.RequestBeginBlock{Header: suite.Ctx.BlockHeader()})

suite.Ctx = suite.App.NewContext(false, suite.Ctx.BlockHeader())
_, checkRes, err = suite.App.Check(suite.clientCtx.TxConfig.TxEncoder(), tx)
fmt.Println("check tx", acc1.GetSequence())

fmt.Printf("check response: %+v, error = %v \n", checkRes, err)
suite.Require().NoError(err, "should be able to edit validator with enough min self delegation")

fmt.Println("deliver tx", suite.App.AccountKeeper.GetAccount(suite.Ctx, acc1.GetAddress()).GetSequence())
_, deliverRes, err = suite.App.Deliver(suite.clientCtx.TxConfig.TxEncoder(), tx)
fmt.Printf("deliver response: %+v, error = %v \n", deliverRes, err)
suite.App.EndBlock(abci.RequestEndBlock{Height: suite.Ctx.BlockHeight()})
suite.App.Commit()
// check and update account keeper
acc = suite.App.AccountKeeper.GetAccount(suite.CheckCtx, acc1.GetAddress())
checkSeq = acc.GetSequence()

// Account sequence updated when checkTx succeeds
suite.Require().Equal(uint64(2), checkSeq)

acc = suite.App.AccountKeeper.GetAccount(suite.Ctx, acc.GetAddress())
deliverSeq = acc.GetSequence()

// Account sequence updated when deliverTx succeeds
suite.Require().Equal(uint64(2), deliverSeq)

}

func TestAnteTestSuite(t *testing.T) {
Expand Down

0 comments on commit b875378

Please sign in to comment.