Skip to content

Commit

Permalink
Update after #448 merge
Browse files Browse the repository at this point in the history
  • Loading branch information
sainoe committed Nov 18, 2022
1 parent 50ae321 commit af1aebb
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 21 deletions.
10 changes: 7 additions & 3 deletions proto/interchain_security/ccv/consumer/v1/genesis.proto
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,15 @@ message GenesisState {
// HeightToValsetUpdateId nil on new chain, filled in on restart.
repeated HeightToValsetUpdateID height_to_valset_update_id = 9
[ (gogoproto.nullable) = false ];
// OutstandingDowntimes nil on new chain, filled in on restart.
// OutstandingDowntimes nil on new chain, filled in on restart.
repeated OutstandingDowntime outstanding_downtime_slashing = 10
[ (gogoproto.nullable) = false ];
// PendingConsumerPackets nil on new chain, filled in on restart.
ConsumerPackets pending_consumer_packets = 11;
// PendingConsumerPackets nil on new chain, filled in on restart.
ConsumerPackets pending_consumer_packets = 11
[ (gogoproto.nullable) = false ];
// LastTransmissionBlockHeight nil on new chain, filled in on restart.
interchain_security.ccv.consumer.v1.LastTransmissionBlockHeight last_transmission_block_height = 12
[ (gogoproto.nullable) = false ];
}

// MaturingVSCPacket defines the genesis information for the
Expand Down
13 changes: 7 additions & 6 deletions x/ccv/consumer/keeper/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,16 @@ func (k Keeper) InitGenesis(ctx sdk.Context, state *consumertypes.GenesisState)

// set default value for valset update ID
k.SetHeightValsetUpdateID(ctx, uint64(ctx.BlockHeight()), uint64(0))

} else {
// verify genesis initial valset against the latest consensus state
// IBC genesis MUST run before CCV consumer genesis
if err := k.verifyGenesisInitValset(ctx, state); err != nil {
panic(err)
}
// chain restarts without the CCV channel established
if state.ProviderChannelId == "" {
k.AppendPendingPacket(ctx, state.PendingConsumerPackets.List...)

// chain restarts with the CCV channel established
} else {
// chain restarts with the CCV channel established
if state.ProviderChannelId != "" {
// set provider channel ID
k.SetProviderChannel(ctx, state.ProviderChannelId)
// set all unbonding sequences
Expand All @@ -80,6 +78,7 @@ func (k Keeper) InitGenesis(ctx sdk.Context, state *consumertypes.GenesisState)
}
k.SetOutstandingDowntime(ctx, consAddr)
}

// set last transmission block height
err := k.SetLastTransmissionBlockHeight(ctx, state.LastTransmissionBlockHeight)
if err != nil {
Expand All @@ -88,7 +87,9 @@ func (k Keeper) InitGenesis(ctx sdk.Context, state *consumertypes.GenesisState)

}

// set pending packet
// set pending consumer pending packets
// note that genesis states embed defined pending mature VSC packets only if the handshake is completed
k.AppendPendingPacket(ctx, state.PendingConsumerPackets.List...)

// set height to valset update id mapping
for _, h2v := range state.HeightToValsetUpdateId {
Expand Down
3 changes: 2 additions & 1 deletion x/ccv/consumer/keeper/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ func TestInitGenesis(t *testing.T) {
List: []consumertypes.ConsumerPacket{
{
Type: consumertypes.SlashPacket,
Data: ccv.NewSlashPacketData(abciValidator, vscID, stakingtypes.Downtime).GetBytes()},
Data: ccv.NewSlashPacketData(abciValidator, vscID, stakingtypes.Downtime).GetBytes(),
},
{
Type: consumertypes.VscMaturedPacket,
Data: ccv.NewVSCMaturedPacketData(1).GetBytes(),
Expand Down
4 changes: 2 additions & 2 deletions x/ccv/consumer/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -429,9 +429,9 @@ func (k Keeper) DeletePendingDataPackets(ctx sdk.Context) {
}

// AppendPendingDataPacket appends the given data packet to the pending data packets in store
func (k Keeper) AppendPendingPacket(ctx sdk.Context, packet types.ConsumerPacket) {
func (k Keeper) AppendPendingPacket(ctx sdk.Context, packet ...types.ConsumerPacket) {
pending := k.GetPendingPackets(ctx)
list := append(pending.GetList(), packet)
list := append(pending.GetList(), packet...)
k.SetPendingPackets(ctx, types.ConsumerPackets{List: list})
}

Expand Down
14 changes: 12 additions & 2 deletions x/ccv/consumer/types/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func NewRestartGenesisState(
maturingPackets []MaturingVSCPacket,
initValSet []abci.ValidatorUpdate,
heightToValsetUpdateIDs []HeightToValsetUpdateID,
pendingSlashRequests SlashRequests,
pendingConsumerPackets ConsumerPackets,
outstandingDowntimes []OutstandingDowntime,
lastTransBlockHeight LastTransmissionBlockHeight,
params Params,
Expand All @@ -44,7 +44,7 @@ func NewRestartGenesisState(
NewChain: false,
InitialValSet: initValSet,
HeightToValsetUpdateId: heightToValsetUpdateIDs,
PendingSlashRequests: pendingSlashRequests,
PendingConsumerPackets: pendingConsumerPackets,
OutstandingDowntimeSlashing: outstandingDowntimes,
LastTransmissionBlockHeight: lastTransBlockHeight,
}
Expand Down Expand Up @@ -108,6 +108,9 @@ func (gs GenesisState) Validate() error {
if len(gs.MaturingPackets) != 0 {
return sdkerrors.Wrap(ccv.ErrInvalidGenesis, "maturing packets must be empty for new chain")
}
if len(gs.PendingConsumerPackets.List) != 0 {
return sdkerrors.Wrap(ccv.ErrInvalidGenesis, "pending consumer packets must be empty for new chain")
}
if gs.LastTransmissionBlockHeight.Height != 0 {
return sdkerrors.Wrap(ccv.ErrInvalidGenesis, "last transmission block height must be empty for new chain")
}
Expand Down Expand Up @@ -142,6 +145,13 @@ func (gs GenesisState) Validate() error {
return sdkerrors.Wrap(
ccv.ErrInvalidGenesis, "last transmission block height must be zero when handshake isn't completed")
}
if len(gs.PendingConsumerPackets.List) != 0 {
for _, packet := range gs.PendingConsumerPackets.List {
if packet.Type == VscMaturedPacket {
return sdkerrors.Wrap(ccv.ErrInvalidGenesis, "pending maturing packets must be empty for new chain")
}
}
}
}
if gs.HeightToValsetUpdateId == nil {
return sdkerrors.Wrap(
Expand Down
79 changes: 73 additions & 6 deletions x/ccv/consumer/types/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,21 @@ import (
"testing"
"time"

sdk "github.com/cosmos/cosmos-sdk/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"

clienttypes "github.com/cosmos/ibc-go/v3/modules/core/02-client/types"
commitmenttypes "github.com/cosmos/ibc-go/v3/modules/core/23-commitment/types"
ibctmtypes "github.com/cosmos/ibc-go/v3/modules/light-clients/07-tendermint/types"
abci "github.com/tendermint/tendermint/abci/types"

"github.com/cosmos/interchain-security/x/ccv/consumer/types"

tmtypes "github.com/tendermint/tendermint/types"

testutil "github.com/cosmos/interchain-security/testutil/keeper"

ccv "github.com/cosmos/interchain-security/x/ccv/types"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -152,6 +157,24 @@ func TestValidateInitialGenesisState(t *testing.T) {
},
true,
},
{
"invalid new consumer genesis state: non-empty pending consumer packets",
&types.GenesisState{
params,
"",
"",
true,
cs,
consensusState,
nil,
valUpdates,
nil,
nil,
types.ConsumerPackets{List: []types.ConsumerPacket{{}}},
types.LastTransmissionBlockHeight{},
},
true,
},
{
"invalid new consumer genesis state: nil initial validator set",
types.NewInitialGenesisState(cs, consensusState, nil, params),
Expand All @@ -173,6 +196,14 @@ func TestValidateInitialGenesisState(t *testing.T) {
valUpdates, params),
true,
},
{
"invalid new consumer genesis state: initial validator set does not match validator set hash",
types.NewInitialGenesisState(
cs, ibctmtypes.NewConsensusState(
time.Now(), commitmenttypes.NewMerkleRoot([]byte("apphash")), []byte("9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08")),
valUpdates, params),
true,
},
{
"invalid new consumer genesis state: invalid params",
types.NewInitialGenesisState(cs, consensusState, valUpdates,
Expand Down Expand Up @@ -214,6 +245,19 @@ func TestValidateRestartGenesisState(t *testing.T) {
valHash := valSet.Hash()
valUpdates := tmtypes.TM2PB.ValidatorUpdates(valSet)

matConsumerPacket := types.ConsumerPacket{
Type: types.VscMaturedPacket,
Data: ccv.NewVSCMaturedPacketData(1).GetBytes(),
}

slashConsumerPacket := types.ConsumerPacket{
Type: types.SlashPacket,
Data: ccv.NewSlashPacketData(
abci.Validator{Address: pubKey.Address(), Power: int64(1)},
1,
stakingtypes.Downtime).GetBytes(),
}

// create default height to validator set update id mapping
heightToValsetUpdateID := []types.HeightToValsetUpdateID{
{Height: 0, ValsetUpdateId: 0},
Expand All @@ -232,7 +276,14 @@ func TestValidateRestartGenesisState(t *testing.T) {
}{
{
"valid restart consumer genesis state: empty maturing packets",
types.NewRestartGenesisState("ccvclient", "ccvchannel", nil, valUpdates, heightToValsetUpdateID, types.SlashRequests{}, nil, types.LastTransmissionBlockHeight{}, params),
types.NewRestartGenesisState("ccvclient", "ccvchannel", nil, valUpdates, heightToValsetUpdateID,
types.ConsumerPackets{List: []types.ConsumerPacket{matConsumerPacket, slashConsumerPacket}}, nil, types.LastTransmissionBlockHeight{Height: 100}, params),
false,
},
{
"valid restart consumer genesis state: handshake in progress ",
types.NewRestartGenesisState("ccvclient", "", nil, valUpdates, heightToValsetUpdateID,
types.ConsumerPackets{List: []types.ConsumerPacket{slashConsumerPacket}}, nil, types.LastTransmissionBlockHeight{}, params),
false,
},
{
Expand All @@ -241,26 +292,28 @@ func TestValidateRestartGenesisState(t *testing.T) {
{1, uint64(time.Now().UnixNano())},
{3, uint64(time.Now().UnixNano())},
{5, uint64(time.Now().UnixNano())},
}, valUpdates, heightToValsetUpdateID, types.SlashRequests{}, nil, types.LastTransmissionBlockHeight{}, params),
}, valUpdates, heightToValsetUpdateID, types.ConsumerPackets{},
[]types.OutstandingDowntime{{ValidatorConsensusAddress: sdk.ConsAddress(validator.Address.Bytes()).String()}},
types.LastTransmissionBlockHeight{}, params),
false,
},
{
"invalid restart consumer genesis state: provider id is empty",
types.NewRestartGenesisState("", "ccvchannel", nil, valUpdates, heightToValsetUpdateID, types.SlashRequests{}, nil, types.LastTransmissionBlockHeight{}, params),
types.NewRestartGenesisState("", "ccvchannel", nil, valUpdates, heightToValsetUpdateID, types.ConsumerPackets{}, nil, types.LastTransmissionBlockHeight{}, params),
true,
},
{
"invalid restart consumer genesis state: maturing packet vscId is invalid",
types.NewRestartGenesisState("ccvclient", "ccvchannel", []types.MaturingVSCPacket{
{0, uint64(time.Now().UnixNano())},
}, valUpdates, nil, types.SlashRequests{}, nil, types.LastTransmissionBlockHeight{}, params),
}, valUpdates, nil, types.ConsumerPackets{}, nil, types.LastTransmissionBlockHeight{}, params),
true,
},
{
"invalid restart consumer genesis state: maturing packet time is invalid",
types.NewRestartGenesisState("ccvclient", "ccvchannel", []types.MaturingVSCPacket{
{1, 0},
}, valUpdates, nil, types.SlashRequests{}, nil, types.LastTransmissionBlockHeight{}, params),
}, valUpdates, nil, types.ConsumerPackets{}, nil, types.LastTransmissionBlockHeight{}, params),
true,
},
{
Expand Down Expand Up @@ -306,7 +359,7 @@ func TestValidateRestartGenesisState(t *testing.T) {
},
{
"invalid restart consumer genesis state: nil height to validator set id mapping",
types.NewRestartGenesisState("ccvclient", "",ConsumerPackets
types.NewRestartGenesisState("ccvclient", "",
[]types.MaturingVSCPacket{{1, 0}}, valUpdates, nil, types.ConsumerPackets{}, nil, types.LastTransmissionBlockHeight{}, params),
true,
}, {
Expand All @@ -328,6 +381,20 @@ func TestValidateRestartGenesisState(t *testing.T) {
nil, valUpdates, heightToValsetUpdateID, types.ConsumerPackets{}, nil, types.LastTransmissionBlockHeight{Height: int64(1)}, params),
true,
},
{
"invalid restart consumer genesis state: pending maturing packets defined when handshake is still in progress",
types.NewRestartGenesisState("ccvclient", "",
nil, valUpdates, heightToValsetUpdateID, types.ConsumerPackets{
List: []types.ConsumerPacket{

{
Type: types.VscMaturedPacket,
Data: ccv.NewVSCMaturedPacketData(1).GetBytes(),
},
},
}, nil, types.LastTransmissionBlockHeight{Height: int64(1)}, params),
true,
},
{
"invalid restart consumer genesis state: invalid params",
types.NewRestartGenesisState("ccvclient", "ccvchannel", nil, valUpdates, nil, types.ConsumerPackets{}, nil, types.LastTransmissionBlockHeight{},
Expand Down
2 changes: 1 addition & 1 deletion x/ccv/provider/types/genesis.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit af1aebb

Please sign in to comment.