Skip to content

Commit

Permalink
working on tests
Browse files Browse the repository at this point in the history
  • Loading branch information
robert-zaremba committed Oct 16, 2020
1 parent d527c77 commit 1a29257
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 44 deletions.
3 changes: 1 addition & 2 deletions codec/any_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ import (
"errors"
"testing"

"github.com/cosmos/cosmos-sdk/codec"

"github.com/stretchr/testify/require"

"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/testutil/testdata"
)
Expand Down
6 changes: 3 additions & 3 deletions x/distribution/keeper/delegation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,10 @@ func TestCalculateRewardsBasic(t *testing.T) {
tstaking.Commission = stakingtypes.NewCommissionRates(sdk.NewDecWithPrec(5, 1), sdk.NewDecWithPrec(5, 1), sdk.NewDec(0))
tstaking.CreateValidator(valAddrs[0], valConsPk1, 100, true)

// end block to bond validator
// end block to bond validator and start new block
staking.EndBlocker(ctx, app.StakingKeeper)

// next block
ctx = ctx.WithBlockHeight(ctx.BlockHeight() + 1)
tstaking.Ctx = ctx

// fetch validator and delegation
val := app.StakingKeeper.Validator(ctx, valAddrs[0])
Expand Down Expand Up @@ -234,6 +233,7 @@ func TestCalculateRewardsMultiDelegator(t *testing.T) {
app.DistrKeeper.AllocateTokensToValidator(ctx, val, tokens)

// second delegation
tstaking.Ctx = ctx
tstaking.Delegate(sdk.AccAddress(valAddrs[1]), valAddrs[0], 100)
del2 := app.StakingKeeper.Delegation(ctx, sdk.AccAddress(valAddrs[1]), valAddrs[0])

Expand Down
16 changes: 8 additions & 8 deletions x/evidence/keeper/infraction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/evidence/types"
"github.com/cosmos/cosmos-sdk/x/staking"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
"github.com/cosmos/cosmos-sdk/x/staking/teststaking"
)

func (suite *KeeperTestSuite) TestHandleDoubleSign() {
Expand All @@ -15,11 +15,10 @@ func (suite *KeeperTestSuite) TestHandleDoubleSign() {

power := int64(100)
stakingParams := suite.app.StakingKeeper.GetParams(ctx)
selfDelegation := sdk.TokensFromConsensusPower(power)
operatorAddr, val := valAddresses[0], pubkeys[0]
tstaking := teststaking.NewService(suite.T(), ctx, suite.app.StakingKeeper)

// create validator
suite.createValidator(ctx, operatorAddr, val, selfDelegation)
selfDelegation := tstaking.CreateValidatorWithValPower(operatorAddr, val, power, true)

// execute end-blocker and verify validator attributes
staking.EndBlocker(ctx, suite.app.StakingKeeper)
Expand Down Expand Up @@ -67,8 +66,9 @@ func (suite *KeeperTestSuite) TestHandleDoubleSign() {
del, _ := suite.app.StakingKeeper.GetDelegation(ctx, sdk.AccAddress(operatorAddr), operatorAddr)
validator, _ := suite.app.StakingKeeper.GetValidator(ctx, operatorAddr)
totalBond := validator.TokensFromShares(del.GetShares()).TruncateInt()
msgUnbond := stakingtypes.NewMsgUndelegate(sdk.AccAddress(operatorAddr), operatorAddr, sdk.NewCoin(stakingParams.BondDenom, totalBond))
suite.stakingHandle(ctx, msgUnbond)
tstaking.Ctx = ctx
tstaking.Denom = stakingParams.BondDenom
tstaking.Undelegate(sdk.AccAddress(operatorAddr), operatorAddr, totalBond, true)
}

func (suite *KeeperTestSuite) TestHandleDoubleSign_TooOld() {
Expand All @@ -77,10 +77,10 @@ func (suite *KeeperTestSuite) TestHandleDoubleSign_TooOld() {

power := int64(100)
stakingParams := suite.app.StakingKeeper.GetParams(ctx)
amt := sdk.TokensFromConsensusPower(power)
operatorAddr, val := valAddresses[0], pubkeys[0]
tstaking := teststaking.NewService(suite.T(), ctx, suite.app.StakingKeeper)

suite.createValidator(ctx, operatorAddr, val, amt)
amt := tstaking.CreateValidatorWithValPower(operatorAddr, val, power, true)

// execute end-blocker and verify validator attributes
staking.EndBlocker(ctx, suite.app.StakingKeeper)
Expand Down
19 changes: 1 addition & 18 deletions x/evidence/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"github.com/cosmos/cosmos-sdk/x/evidence/keeper"
"github.com/cosmos/cosmos-sdk/x/evidence/types"
"github.com/cosmos/cosmos-sdk/x/staking"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
)

var (
Expand Down Expand Up @@ -50,7 +49,7 @@ func newPubKey(pk string) (res crypto.PubKey) {
return pubkey
}

func testEquivocationHandler(k interface{}) types.Handler {
func testEquivocationHandler(_ interface{}) types.Handler {
return func(ctx sdk.Context, e exported.Evidence) error {
if err := e.ValidateBasic(); err != nil {
return err
Expand Down Expand Up @@ -108,22 +107,6 @@ func (suite *KeeperTestSuite) SetupTest() {
suite.stakingHdl = staking.NewHandler(app.StakingKeeper)
}

func (suite *KeeperTestSuite) createValidator(ctx sdk.Context, a sdk.ValAddress, pubKey crypto.PubKey, amt sdk.Int) {
commission := stakingtypes.NewCommissionRates(sdk.ZeroDec(), sdk.ZeroDec(), sdk.ZeroDec())
msg, err := stakingtypes.NewMsgCreateValidator(
a, pubKey, sdk.NewCoin(sdk.DefaultBondDenom, amt),
stakingtypes.Description{}, commission, sdk.OneInt(),
)
suite.NoError(err)
suite.stakingHdl(ctx, msg)
}

func (suite *KeeperTestSuite) stakingHandle(ctx sdk.Context, msg sdk.Msg) {
res, err := suite.stakingHdl(ctx, msg)
suite.NoError(err)
suite.NotNil(res)
}

func (suite *KeeperTestSuite) populateEvidence(ctx sdk.Context, numEvidence int) []exported.Evidence {
evidence := make([]exported.Evidence, numEvidence)

Expand Down
12 changes: 4 additions & 8 deletions x/staking/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ func TestIncrementsMsgDelegate(t *testing.T) {
require.Equal(t, bondAmount, validator.DelegatorShares.RoundInt())
require.Equal(t, bondAmount, validator.BondedTokens(), "validator: %v", validator)

tstaking.CheckDelegator(delegatorAddr, validatorAddr)
tstaking.CheckDelegator(delegatorAddr, validatorAddr, false)

bond, found := app.StakingKeeper.GetDelegation(ctx, sdk.AccAddress(validatorAddr), validatorAddr)
require.True(t, found)
Expand All @@ -279,9 +279,6 @@ func TestIncrementsMsgDelegate(t *testing.T) {
bondedTokens := app.StakingKeeper.TotalBondedTokens(ctx)
require.Equal(t, bondAmount.Int64(), bondedTokens.Int64())

// just send the same msgbond multiple times
tstaking.Delegate(delegatorAddr, validatorAddr, bondAmount.Int64())

for i := int64(0); i < 5; i++ {
ctx = ctx.WithBlockHeight(i)
tstaking.Ctx = ctx
Expand Down Expand Up @@ -547,7 +544,7 @@ func TestMultipleMsgDelegate(t *testing.T) {
// delegate multiple parties
for _, delegatorAddr := range delegatorAddrs {
tstaking.Delegate(delegatorAddr, validatorAddr, 10)
tstaking.CheckDelegator(delegatorAddr, validatorAddr)
tstaking.CheckDelegator(delegatorAddr, validatorAddr, true)
}

// unbond them all
Expand Down Expand Up @@ -735,13 +732,12 @@ func TestRedelegationPeriod(t *testing.T) {
params := app.StakingKeeper.GetParams(ctx)
params.UnbondingTime = 7 * time.Second
app.StakingKeeper.SetParams(ctx, params)
// initial balance
amt1 := app.BankKeeper.GetBalance(ctx, sdk.AccAddress(validatorAddr), denom).Amount

// create the validators
tstaking.CreateValidator(validatorAddr, PKs[0], 10, true)

// initial balance
amt1 := app.BankKeeper.GetBalance(ctx, sdk.AccAddress(validatorAddr), denom).Amount

// balance should have been subtracted after creation
amt2 := app.BankKeeper.GetBalance(ctx, sdk.AccAddress(validatorAddr), denom).Amount
require.Equal(t, amt1.Sub(sdk.NewInt(10)).Int64(), amt2.Int64(), "expected coins to be subtracted")
Expand Down
7 changes: 3 additions & 4 deletions x/staking/teststaking/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,9 @@ func (sh *Service) CheckValidator(addr sdk.ValAddress, status stakingtypes.BondS
}

// CheckDelegator asserts that a delegator exists
func (sh *Service) CheckDelegator(delegator sdk.AccAddress, val sdk.ValAddress) {
d, ok := sh.k.GetDelegation(sh.Ctx, delegator, val)
require.True(sh.t, ok)
require.NotNil(sh.t, d)
func (sh *Service) CheckDelegator(delegator sdk.AccAddress, val sdk.ValAddress, found bool) {
_, ok := sh.k.GetDelegation(sh.Ctx, delegator, val)
require.Equal(sh.t, ok, found)
}

// TurnBlock calls EndBlocker and updates the block time
Expand Down
25 changes: 24 additions & 1 deletion x/staking/types/msg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import (
"github.com/stretchr/testify/require"
"github.com/tendermint/tendermint/crypto"

"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
sdk "github.com/cosmos/cosmos-sdk/types"
)

Expand All @@ -14,6 +17,25 @@ var (
coinZero = sdk.NewInt64Coin(sdk.DefaultBondDenom, 0)
)

func TestMsgPkDecode(t *testing.T) {
// description := Description{}
// commission1 := NewCommissionRates(sdk.ZeroDec(), sdk.ZeroDec(), sdk.ZeroDec())
// msg, err := NewMsgCreateValidator(valAddr1, pk1, coinPos, description, commission1, sdk.OneInt())
// require.NoError(t, err)

registry := codectypes.NewInterfaceRegistry()
cdc := codec.NewProtoCodec(registry)

pk1bz, err := codec.MarshalAny(cdc, pk1)
require.NoError(t, err)

var pkUnmarshaled ed25519.PubKey
err = codec.UnmarshalAny(cdc, &pkUnmarshaled, pk1bz)
require.NoError(t, err)

require.True(t, pk1.Equals(&pkUnmarshaled))
}

// test ValidateBasic for MsgCreateValidator
func TestMsgCreateValidator(t *testing.T) {
commission1 := NewCommissionRates(sdk.ZeroDec(), sdk.ZeroDec(), sdk.ZeroDec())
Expand Down Expand Up @@ -41,9 +63,10 @@ func TestMsgCreateValidator(t *testing.T) {
}

for _, tc := range tests {
t.Logf("Test: %s, pk=%t", tc.name, tc.pubkey)
description := NewDescription(tc.moniker, tc.identity, tc.website, tc.securityContact, tc.details)
msg, err := NewMsgCreateValidator(tc.validatorAddr, tc.pubkey, tc.bond, description, tc.CommissionRates, tc.minSelfDelegation)
require.NotNil(t, err)
require.NoError(t, err)
if tc.expectPass {
require.Nil(t, msg.ValidateBasic(), "test: %v", tc.name)
} else {
Expand Down

0 comments on commit 1a29257

Please sign in to comment.