Register slashing keeper, set startHeight, testcases
This commit is contained in:
parent
d309abb4b9
commit
3bea85abb3
|
@ -82,7 +82,8 @@ func NewGaiaApp(logger log.Logger, db dbm.DB) *GaiaApp {
|
||||||
app.Router().
|
app.Router().
|
||||||
AddRoute("bank", bank.NewHandler(app.coinKeeper)).
|
AddRoute("bank", bank.NewHandler(app.coinKeeper)).
|
||||||
AddRoute("ibc", ibc.NewHandler(app.ibcMapper, app.coinKeeper)).
|
AddRoute("ibc", ibc.NewHandler(app.ibcMapper, app.coinKeeper)).
|
||||||
AddRoute("stake", stake.NewHandler(app.stakeKeeper))
|
AddRoute("stake", stake.NewHandler(app.stakeKeeper)).
|
||||||
|
AddRoute("slashing", slashing.NewHandler(app.slashingKeeper))
|
||||||
|
|
||||||
// initialize BaseApp
|
// initialize BaseApp
|
||||||
app.SetInitChainer(app.initChainer)
|
app.SetInitChainer(app.initChainer)
|
||||||
|
|
|
@ -0,0 +1,124 @@
|
||||||
|
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()
|
||||||
|
priv2 = crypto.GenPrivKeyEd25519()
|
||||||
|
addr2 = priv2.PubKey().Address()
|
||||||
|
addr3 = crypto.GenPrivKeyEd25519().PubKey().Address()
|
||||||
|
priv4 = crypto.GenPrivKeyEd25519()
|
||||||
|
addr4 = priv4.PubKey().Address()
|
||||||
|
coins = sdk.Coins{{"foocoin", 10}}
|
||||||
|
fee = auth.StdFee{
|
||||||
|
sdk.Coins{{"foocoin", 0}},
|
||||||
|
100000,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
// 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(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},
|
||||||
|
}
|
||||||
|
acc2 := &auth.BaseAccount{
|
||||||
|
Address: addr2,
|
||||||
|
Coins: sdk.Coins{genCoin},
|
||||||
|
}
|
||||||
|
accs := []auth.Account{acc1, acc2}
|
||||||
|
|
||||||
|
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
|
||||||
|
mock.SignCheckDeliver(t, mapp.BaseApp, unrevokeMsg, []int64{1}, false, priv1)
|
||||||
|
}
|
|
@ -56,7 +56,11 @@ func (k Keeper) handleValidatorSignature(ctx sdk.Context, pubkey crypto.PubKey,
|
||||||
|
|
||||||
// Local index, so counts blocks validator *should* have signed
|
// Local index, so counts blocks validator *should* have signed
|
||||||
// Will use the 0-value default signing info if not present
|
// Will use the 0-value default signing info if not present
|
||||||
signInfo, _ := k.getValidatorSigningInfo(ctx, address)
|
signInfo, found := k.getValidatorSigningInfo(ctx, address)
|
||||||
|
if !found {
|
||||||
|
// If this validator has never been seen before, set the start height
|
||||||
|
signInfo.StartHeight = height
|
||||||
|
}
|
||||||
index := signInfo.IndexOffset % SignedBlocksWindow
|
index := signInfo.IndexOffset % SignedBlocksWindow
|
||||||
signInfo.IndexOffset++
|
signInfo.IndexOffset++
|
||||||
|
|
||||||
|
|
|
@ -129,3 +129,33 @@ func TestHandleAbsentValidator(t *testing.T) {
|
||||||
validator, _ = sk.GetValidatorByPubKey(ctx, val)
|
validator, _ = sk.GetValidatorByPubKey(ctx, val)
|
||||||
require.Equal(t, sdk.Unbonded, validator.GetStatus())
|
require.Equal(t, sdk.Unbonded, validator.GetStatus())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestHandleNewValidator(t *testing.T) {
|
||||||
|
// initial setup
|
||||||
|
ctx, ck, sk, keeper := createTestInput(t)
|
||||||
|
addr, val, amt := addrs[0], pks[0], int64(100)
|
||||||
|
sh := stake.NewHandler(sk)
|
||||||
|
got := sh(ctx, newTestMsgCreateValidator(addr, val, amt))
|
||||||
|
require.True(t, got.IsOK())
|
||||||
|
stake.EndBlocker(ctx, sk)
|
||||||
|
require.Equal(t, ck.GetCoins(ctx, addr), sdk.Coins{{sk.GetParams(ctx).BondDenom, initCoins - amt}})
|
||||||
|
require.Equal(t, sdk.NewRat(amt), sk.Validator(ctx, addr).GetPower())
|
||||||
|
|
||||||
|
// 1000 first blocks not a validator
|
||||||
|
ctx = ctx.WithBlockHeight(1001)
|
||||||
|
|
||||||
|
// Now a validator
|
||||||
|
keeper.handleValidatorSignature(ctx, val, true)
|
||||||
|
info, found := keeper.getValidatorSigningInfo(ctx, val.Address())
|
||||||
|
require.True(t, found)
|
||||||
|
require.Equal(t, int64(1001), info.StartHeight)
|
||||||
|
require.Equal(t, int64(1), info.IndexOffset)
|
||||||
|
require.Equal(t, int64(1), info.SignedBlocksCounter)
|
||||||
|
require.Equal(t, int64(0), info.JailedUntil)
|
||||||
|
|
||||||
|
// validator should be bonded still, should not have been revoked or slashed
|
||||||
|
validator, _ := sk.GetValidatorByPubKey(ctx, val)
|
||||||
|
require.Equal(t, sdk.Bonded, validator.GetStatus())
|
||||||
|
pool := sk.GetPool(ctx)
|
||||||
|
require.Equal(t, int64(100), pool.BondedTokens)
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue