Slashing genesis types proto migration (#6881)

* slashing genesis types migrated to proto

* review changes

* fixed lint

* review change

* review change

* review changes

* Update proto messgaes

* Add test for ExportGenesis

* Address naming suggestions

* Update genesis

* Fix genesis test

* Add GetMissedBlocks

* Update genesis test

* Address review suggestions

Co-authored-by: Anil Kumar Kammari <anil@vitwit.com>
Co-authored-by: SaReN <sahithnarahari@gmail.com>
This commit is contained in:
atheeshp 2020-08-04 18:28:36 +05:30 committed by GitHub
parent cb5c93213c
commit 662aa7fa01
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 1870 additions and 640 deletions

View File

@ -0,0 +1,49 @@
syntax = "proto3";
package cosmos.slashing;
option go_package = "github.com/cosmos/cosmos-sdk/x/slashing/types";
import "gogoproto/gogo.proto";
import "cosmos/slashing/slashing.proto";
// GenesisState - all slashing state that must be provided at genesis
message GenesisState {
Params params = 1 [
(gogoproto.casttype) = "Params",
(gogoproto.nullable) = false
];
repeated SigningInfo signing_infos = 2 [
(gogoproto.moretags) = "yaml:\"signing_infos\"",
(gogoproto.nullable) = false
];
repeated ValidatorMissedBlocks missed_blocks = 3 [
(gogoproto.moretags) = "yaml:\"missed_blocks\"",
(gogoproto.nullable) = false
];
}
// SigningInfo stores validator signing info of corresponding address
message SigningInfo {
string address = 1;
ValidatorSigningInfo validator_signing_info = 2 [
(gogoproto.nullable) = false,
(gogoproto.moretags) = "yaml:\"validator_signing_info\""
];
}
// ValidatorMissedBlocks contains array of missed blocks of corresponding address
message ValidatorMissedBlocks {
string address = 1;
repeated MissedBlock missed_blocks = 2 [
(gogoproto.nullable) = false,
(gogoproto.moretags) = "yaml:\"missed_blocks\""
];
}
// MissedBlock contains height and missed status as boolean
message MissedBlock {
int64 index = 1;
bool missed = 2;
}

View File

@ -17,20 +17,20 @@ func InitGenesis(ctx sdk.Context, keeper keeper.Keeper, stakingKeeper types.Stak
},
)
for addr, info := range data.SigningInfos {
address, err := sdk.ConsAddressFromBech32(addr)
for _, info := range data.SigningInfos {
address, err := sdk.ConsAddressFromBech32(info.Address)
if err != nil {
panic(err)
}
keeper.SetValidatorSigningInfo(ctx, address, info)
keeper.SetValidatorSigningInfo(ctx, address, info.ValidatorSigningInfo)
}
for addr, array := range data.MissedBlocks {
address, err := sdk.ConsAddressFromBech32(addr)
for _, array := range data.MissedBlocks {
address, err := sdk.ConsAddressFromBech32(array.Address)
if err != nil {
panic(err)
}
for _, missed := range array {
for _, missed := range array.MissedBlocks {
keeper.SetValidatorMissedBlockBitArray(ctx, address, missed.Index, missed.Missed)
}
}
@ -43,18 +43,21 @@ func InitGenesis(ctx sdk.Context, keeper keeper.Keeper, stakingKeeper types.Stak
// with InitGenesis
func ExportGenesis(ctx sdk.Context, keeper keeper.Keeper) (data types.GenesisState) {
params := keeper.GetParams(ctx)
signingInfos := make(map[string]types.ValidatorSigningInfo)
missedBlocks := make(map[string][]types.MissedBlock)
signingInfos := make([]types.SigningInfo, 0)
missedBlocks := make([]types.ValidatorMissedBlocks, 0)
keeper.IterateValidatorSigningInfos(ctx, func(address sdk.ConsAddress, info types.ValidatorSigningInfo) (stop bool) {
bechAddr := address.String()
signingInfos[bechAddr] = info
localMissedBlocks := []types.MissedBlock{}
keeper.IterateValidatorMissedBlockBitArray(ctx, address, func(index int64, missed bool) (stop bool) {
localMissedBlocks = append(localMissedBlocks, types.NewMissedBlock(index, missed))
return false
signingInfos = append(signingInfos, types.SigningInfo{
Address: bechAddr,
ValidatorSigningInfo: info,
})
localMissedBlocks := keeper.GetValidatorMissedBlocks(ctx, address)
missedBlocks = append(missedBlocks, types.ValidatorMissedBlocks{
Address: bechAddr,
MissedBlocks: localMissedBlocks,
})
missedBlocks[bechAddr] = localMissedBlocks
return false
})

View File

@ -0,0 +1,59 @@
package slashing_test
import (
"testing"
"time"
"github.com/stretchr/testify/require"
abci "github.com/tendermint/tendermint/abci/types"
"github.com/cosmos/cosmos-sdk/simapp"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/slashing"
"github.com/cosmos/cosmos-sdk/x/slashing/keeper"
"github.com/cosmos/cosmos-sdk/x/slashing/types"
)
func TestExportAndInitGenesis(t *testing.T) {
app := simapp.Setup(false)
ctx := app.BaseApp.NewContext(false, abci.Header{})
app.SlashingKeeper.SetParams(ctx, keeper.TestParams())
addrDels := simapp.AddTestAddrsIncremental(app, ctx, 2, sdk.TokensFromConsensusPower(200))
info1 := types.NewValidatorSigningInfo(sdk.ConsAddress(addrDels[0]), int64(4), int64(3),
time.Now().UTC().Add(100000000000), false, int64(10))
info2 := types.NewValidatorSigningInfo(sdk.ConsAddress(addrDels[1]), int64(5), int64(4),
time.Now().UTC().Add(10000000000), false, int64(10))
app.SlashingKeeper.SetValidatorSigningInfo(ctx, sdk.ConsAddress(addrDels[0]), info1)
app.SlashingKeeper.SetValidatorSigningInfo(ctx, sdk.ConsAddress(addrDels[1]), info2)
genesisState := slashing.ExportGenesis(ctx, app.SlashingKeeper)
require.Equal(t, genesisState.Params, keeper.TestParams())
require.Len(t, genesisState.SigningInfos, 2)
require.Equal(t, genesisState.SigningInfos[0].ValidatorSigningInfo, info1)
// Tombstone validators after genesis shouldn't effect genesis state
app.SlashingKeeper.Tombstone(ctx, sdk.ConsAddress(addrDels[0]))
app.SlashingKeeper.Tombstone(ctx, sdk.ConsAddress(addrDels[1]))
ok := app.SlashingKeeper.IsTombstoned(ctx, sdk.ConsAddress(addrDels[0]))
require.True(t, ok)
newInfo1, ok := app.SlashingKeeper.GetValidatorSigningInfo(ctx, sdk.ConsAddress(addrDels[0]))
require.NotEqual(t, info1, newInfo1)
// Initialise genesis with genesis state before tombstone
slashing.InitGenesis(ctx, app.SlashingKeeper, app.StakingKeeper, genesisState)
// Validator isTombstoned should return false as GenesisState is initialised
ok = app.SlashingKeeper.IsTombstoned(ctx, sdk.ConsAddress(addrDels[0]))
require.False(t, ok)
newInfo1, ok = app.SlashingKeeper.GetValidatorSigningInfo(ctx, sdk.ConsAddress(addrDels[0]))
newInfo2, ok := app.SlashingKeeper.GetValidatorSigningInfo(ctx, sdk.ConsAddress(addrDels[1]))
require.True(t, ok)
require.Equal(t, info1, newInfo1)
require.Equal(t, info2, newInfo2)
}

View File

@ -90,6 +90,17 @@ func (k Keeper) IterateValidatorMissedBlockBitArray(ctx sdk.Context,
}
}
// GetValidatorMissedBlocks returns array of missed blocks for given validator Cons address
func (k Keeper) GetValidatorMissedBlocks(ctx sdk.Context, address sdk.ConsAddress) []types.MissedBlock {
missedBlocks := []types.MissedBlock{}
k.IterateValidatorMissedBlockBitArray(ctx, address, func(index int64, missed bool) (stop bool) {
missedBlocks = append(missedBlocks, types.NewMissedBlock(index, missed))
return false
})
return missedBlocks
}
// JailUntil attempts to set a validator's JailedUntil attribute in its signing
// info. It will panic if the signing info does not exist for the validator.
func (k Keeper) JailUntil(ctx sdk.Context, consAddr sdk.ConsAddress, jailTime time.Time) {

View File

@ -86,7 +86,7 @@ func RandomizedGenState(simState *module.SimulationState) {
slashFractionDoubleSign, slashFractionDowntime,
)
slashingGenesis := types.NewGenesisState(params, nil, nil)
slashingGenesis := types.NewGenesisState(params, []types.SigningInfo{}, []types.ValidatorMissedBlocks{})
fmt.Printf("Selected randomly generated slashing parameters:\n%s\n", codec.MustMarshalJSONIndent(simState.Cdc, slashingGenesis.Params))
simState.GenState[types.ModuleName] = simState.Cdc.MustMarshalJSON(slashingGenesis)

View File

@ -7,16 +7,9 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
)
// GenesisState - all slashing state that must be provided at genesis
type GenesisState struct {
Params Params `json:"params" yaml:"params"`
SigningInfos map[string]ValidatorSigningInfo `json:"signing_infos" yaml:"signing_infos"`
MissedBlocks map[string][]MissedBlock `json:"missed_blocks" yaml:"missed_blocks"`
}
// NewGenesisState creates a new GenesisState object
func NewGenesisState(
params Params, signingInfos map[string]ValidatorSigningInfo, missedBlocks map[string][]MissedBlock,
params Params, signingInfos []SigningInfo, missedBlocks []ValidatorMissedBlocks,
) GenesisState {
return GenesisState{
@ -26,12 +19,6 @@ func NewGenesisState(
}
}
// MissedBlock
type MissedBlock struct {
Index int64 `json:"index" yaml:"index"`
Missed bool `json:"missed" yaml:"missed"`
}
// NewMissedBlock creates a new MissedBlock instance
func NewMissedBlock(index int64, missed bool) MissedBlock {
return MissedBlock{
@ -44,8 +31,8 @@ func NewMissedBlock(index int64, missed bool) MissedBlock {
func DefaultGenesisState() GenesisState {
return GenesisState{
Params: DefaultParams(),
SigningInfos: make(map[string]ValidatorSigningInfo),
MissedBlocks: make(map[string][]MissedBlock),
SigningInfos: []SigningInfo{},
MissedBlocks: []ValidatorMissedBlocks{},
}
}

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff