-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1200 from cosmos/cwgoes/slashing-bugfixes
Slashing bugfixes (start height, handler registration)
- Loading branch information
Showing
7 changed files
with
178 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
package slashing | ||
|
||
import ( | ||
"testing" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/cosmos/cosmos-sdk/x/auth" | ||
"github.com/cosmos/cosmos-sdk/x/auth/mock" | ||
"github.com/cosmos/cosmos-sdk/x/bank" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/cosmos/cosmos-sdk/x/stake" | ||
abci "github.com/tendermint/abci/types" | ||
crypto "github.com/tendermint/go-crypto" | ||
) | ||
|
||
var ( | ||
priv1 = crypto.GenPrivKeyEd25519() | ||
addr1 = priv1.PubKey().Address() | ||
coins = sdk.Coins{{"foocoin", 10}} | ||
) | ||
|
||
// initialize the mock application for this module | ||
func getMockApp(t *testing.T) (*mock.App, stake.Keeper, Keeper) { | ||
mapp := mock.NewApp() | ||
|
||
RegisterWire(mapp.Cdc) | ||
keyStake := sdk.NewKVStoreKey("stake") | ||
keySlashing := sdk.NewKVStoreKey("slashing") | ||
coinKeeper := bank.NewKeeper(mapp.AccountMapper) | ||
stakeKeeper := stake.NewKeeper(mapp.Cdc, keyStake, coinKeeper, mapp.RegisterCodespace(stake.DefaultCodespace)) | ||
keeper := NewKeeper(mapp.Cdc, keySlashing, stakeKeeper, mapp.RegisterCodespace(DefaultCodespace)) | ||
mapp.Router().AddRoute("stake", stake.NewHandler(stakeKeeper)) | ||
mapp.Router().AddRoute("slashing", NewHandler(keeper)) | ||
|
||
mapp.SetEndBlocker(getEndBlocker(stakeKeeper)) | ||
mapp.SetInitChainer(getInitChainer(mapp, stakeKeeper)) | ||
mapp.CompleteSetup(t, []*sdk.KVStoreKey{keyStake, keySlashing}) | ||
|
||
return mapp, stakeKeeper, keeper | ||
} | ||
|
||
// stake endblocker | ||
func getEndBlocker(keeper stake.Keeper) sdk.EndBlocker { | ||
return func(ctx sdk.Context, req abci.RequestEndBlock) abci.ResponseEndBlock { | ||
validatorUpdates := stake.EndBlocker(ctx, keeper) | ||
return abci.ResponseEndBlock{ | ||
ValidatorUpdates: validatorUpdates, | ||
} | ||
} | ||
} | ||
|
||
// overwrite the mock init chainer | ||
func getInitChainer(mapp *mock.App, keeper stake.Keeper) sdk.InitChainer { | ||
return func(ctx sdk.Context, req abci.RequestInitChain) abci.ResponseInitChain { | ||
mapp.InitChainer(ctx, req) | ||
stake.InitGenesis(ctx, keeper, stake.DefaultGenesisState()) | ||
return abci.ResponseInitChain{} | ||
} | ||
} | ||
|
||
func checkValidator(t *testing.T, mapp *mock.App, keeper stake.Keeper, | ||
addr sdk.Address, expFound bool) stake.Validator { | ||
ctxCheck := mapp.BaseApp.NewContext(true, abci.Header{}) | ||
validator, found := keeper.GetValidator(ctxCheck, addr1) | ||
assert.Equal(t, expFound, found) | ||
return validator | ||
} | ||
|
||
func checkValidatorSigningInfo(t *testing.T, mapp *mock.App, keeper Keeper, | ||
addr sdk.Address, expFound bool) ValidatorSigningInfo { | ||
ctxCheck := mapp.BaseApp.NewContext(true, abci.Header{}) | ||
signingInfo, found := keeper.getValidatorSigningInfo(ctxCheck, addr) | ||
assert.Equal(t, expFound, found) | ||
return signingInfo | ||
} | ||
|
||
func TestSlashingMsgs(t *testing.T) { | ||
mapp, stakeKeeper, keeper := getMockApp(t) | ||
|
||
genCoin := sdk.Coin{"steak", 42} | ||
bondCoin := sdk.Coin{"steak", 10} | ||
|
||
acc1 := &auth.BaseAccount{ | ||
Address: addr1, | ||
Coins: sdk.Coins{genCoin}, | ||
} | ||
accs := []auth.Account{acc1} | ||
mock.SetGenesis(mapp, accs) | ||
description := stake.NewDescription("foo_moniker", "", "", "") | ||
createValidatorMsg := stake.NewMsgCreateValidator( | ||
addr1, priv1.PubKey(), bondCoin, description, | ||
) | ||
mock.SignCheckDeliver(t, mapp.BaseApp, createValidatorMsg, []int64{0}, true, priv1) | ||
mock.CheckBalance(t, mapp, addr1, sdk.Coins{genCoin.Minus(bondCoin)}) | ||
mapp.BeginBlock(abci.RequestBeginBlock{}) | ||
|
||
validator := checkValidator(t, mapp, stakeKeeper, addr1, true) | ||
require.Equal(t, addr1, validator.Owner) | ||
require.Equal(t, sdk.Bonded, validator.Status()) | ||
require.True(sdk.RatEq(t, sdk.NewRat(10), validator.PoolShares.Bonded())) | ||
unrevokeMsg := MsgUnrevoke{ValidatorAddr: validator.PubKey.Address()} | ||
|
||
// no signing info yet | ||
checkValidatorSigningInfo(t, mapp, keeper, addr1, false) | ||
|
||
// unrevoke should fail with unknown validator | ||
res := mock.SignCheck(t, mapp.BaseApp, unrevokeMsg, []int64{1}, priv1) | ||
require.Equal(t, sdk.ToABCICode(DefaultCodespace, CodeInvalidValidator), res.Code) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters