Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: export InitTimeoutTimestamps and VscSendTimestamps to genesis #1076

Merged
merged 11 commits into from
Jul 19, 2023
2 changes: 1 addition & 1 deletion proto/interchain_security/ccv/consumer/v1/genesis.proto
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,4 @@ message HeightToValsetUpdateID {

// OutstandingDowntime defines the genesis information for each validator
// flagged with an outstanding downtime slashing.
message OutstandingDowntime { string validator_consensus_address = 1; }
message OutstandingDowntime { string validator_consensus_address = 1; }
shaspitz marked this conversation as resolved.
Show resolved Hide resolved
6 changes: 6 additions & 0 deletions proto/interchain_security/ccv/provider/v1/genesis.proto
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ message GenesisState {
// empty for a new chain
repeated ConsumerAddrsToPrune consumer_addrs_to_prune = 11
[ (gogoproto.nullable) = false ];

repeated interchain_security.ccv.provider.v1.InitTimeoutTimestamp init_timeout_timestamps = 12
[ (gogoproto.nullable) = false ];

repeated interchain_security.ccv.provider.v1.ExportedVscSendTimestamp exported_vsc_send_timestamps = 13
[ (gogoproto.nullable) = false ];
}

// consumer chain
Expand Down
7 changes: 7 additions & 0 deletions proto/interchain_security/ccv/provider/v1/provider.proto
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,13 @@ message VscSendTimestamp {
[ (gogoproto.stdtime) = true, (gogoproto.nullable) = false ];
}

// ExportedVscSendTimestamps is VscSendTimestamp with chainID info for exporting to genesis
message ExportedVscSendTimestamp {
shaspitz marked this conversation as resolved.
Show resolved Hide resolved
string chain_id = 1;
VscSendTimestamp vsc_send_timestamp = 2
[ (gogoproto.nullable) = false ];
}

//
// Key assignment section
//
Expand Down
2 changes: 1 addition & 1 deletion proto/interchain_security/ccv/provider/v1/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,4 @@ message MsgRegisterConsumerRewardDenom {

// MsgRegisterConsumerRewardDenomResponse defines the
// Msg/RegisterConsumerRewardDenom response type.
message MsgRegisterConsumerRewardDenomResponse {}
message MsgRegisterConsumerRewardDenomResponse {}
2 changes: 1 addition & 1 deletion proto/interchain_security/ccv/v1/ccv.proto
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,4 @@ enum ConsumerPacketDataType {
// VSCMatured packet
CONSUMER_PACKET_TYPE_VSCM = 2
[ (gogoproto.enumvalue_customname) = "VscMaturedPacket" ];
}
}
15 changes: 15 additions & 0 deletions x/ccv/provider/keeper/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,14 @@ func (k Keeper) InitGenesis(ctx sdk.Context, genState *types.GenesisState) {
}
}

for _, item := range genState.InitTimeoutTimestamps {
k.SetInitTimeoutTimestamp(ctx, item.ChainId, item.Timestamp)
}

for _, item := range genState.ExportedVscSendTimestamps {
k.SetVscSendTimestamp(ctx, item.ChainId, item.VscSendTimestamp.VscId, item.VscSendTimestamp.Timestamp)
yaruwangway marked this conversation as resolved.
Show resolved Hide resolved
}

k.SetParams(ctx, genState.Params)
k.InitializeSlashMeter(ctx)
}
Expand All @@ -99,6 +107,7 @@ func (k Keeper) ExportGenesis(ctx sdk.Context) *types.GenesisState {
// get a list of all registered consumer chains
registeredChains := k.GetAllConsumerChains(ctx)

var exportedVscSendTimestamps []types.ExportedVscSendTimestamp
// export states for each consumer chains
var consumerStates []types.ConsumerState
for _, chain := range registeredChains {
Expand Down Expand Up @@ -129,6 +138,10 @@ func (k Keeper) ExportGenesis(ctx sdk.Context) *types.GenesisState {
cs.PendingValsetChanges = k.GetPendingVSCPackets(ctx, chain.ChainId)
consumerStates = append(consumerStates, cs)

vscSendTimestamps := k.GetAllVscSendTimestamps(ctx, chain.ChainId)
for _, vscSendTimestamp := range vscSendTimestamps {
exportedVscSendTimestamps = append(exportedVscSendTimestamps, types.ExportedVscSendTimestamp{ChainId: chain.ChainId, VscSendTimestamp: vscSendTimestamp})
}
}

// ConsumerAddrsToPrune are added only for registered consumer chains
Expand All @@ -151,5 +164,7 @@ func (k Keeper) ExportGenesis(ctx sdk.Context) *types.GenesisState {
k.GetAllValidatorConsumerPubKeys(ctx, nil),
k.GetAllValidatorsByConsumerAddr(ctx, nil),
consumerAddrsToPrune,
k.GetAllInitTimeoutTimestamps(ctx),
exportedVscSendTimestamps,
)
}
51 changes: 51 additions & 0 deletions x/ccv/provider/keeper/genesis_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package keeper_test

import (
"sort"
"testing"
"time"

Expand Down Expand Up @@ -35,6 +36,22 @@ func TestInitAndExportGenesis(t *testing.T) {
consumerTmPubKey := consumerCryptoId.TMProtoCryptoPublicKey()
consumerConsAddr := consumerCryptoId.ConsumerConsAddress()

initTimeoutTimeStamps := []providertypes.InitTimeoutTimestamp{
{ChainId: cChainIDs[0], Timestamp: uint64(time.Now().UTC().UnixNano()) + 10},
{ChainId: cChainIDs[1], Timestamp: uint64(time.Now().UTC().UnixNano()) + 15},
}

now := time.Now().UTC()
exportedVscSendTimeStampsC0 := []providertypes.ExportedVscSendTimestamp{
{ChainId: "c0", VscSendTimestamp: providertypes.VscSendTimestamp{VscId: 1, Timestamp: now.Add(time.Hour)}},
{ChainId: "c0", VscSendTimestamp: providertypes.VscSendTimestamp{VscId: 2, Timestamp: now.Add(2 * time.Hour)}},
}

exportedVscSendTimeStampsC1 := []providertypes.ExportedVscSendTimestamp{
{ChainId: "c1", VscSendTimestamp: providertypes.VscSendTimestamp{VscId: 1, Timestamp: now.Add(-time.Hour)}},
{ChainId: "c1", VscSendTimestamp: providertypes.VscSendTimestamp{VscId: 2, Timestamp: now.Add(time.Hour)}},
}
exportedVscSendTimeStampsAll := append(exportedVscSendTimeStampsC0, exportedVscSendTimeStampsC1...)
// create genesis struct
provGenesis := providertypes.NewGenesisState(vscID,
[]providertypes.ValsetUpdateIdToHeight{{ValsetUpdateId: vscID, Height: initHeight}},
Expand Down Expand Up @@ -97,6 +114,8 @@ func TestInitAndExportGenesis(t *testing.T) {
ConsumerAddrs: &providertypes.AddressList{Addresses: [][]byte{consumerConsAddr.ToSdkConsAddr()}},
},
},
initTimeoutTimeStamps,
exportedVscSendTimeStampsAll,
)

// Instantiate in-mem provider keeper with mocks
Expand Down Expand Up @@ -163,6 +182,38 @@ func TestInitAndExportGenesis(t *testing.T) {

// check the exported genesis
require.Equal(t, provGenesis, pk.ExportGenesis(ctx))

initTimeoutTimestampInStore := pk.GetAllInitTimeoutTimestamps(ctx)
sort.Slice(initTimeoutTimestampInStore, func(i, j int) bool {
return initTimeoutTimestampInStore[i].Timestamp < initTimeoutTimestampInStore[j].Timestamp
})
require.Equal(t, initTimeoutTimestampInStore, initTimeoutTimeStamps)

vscSendTimestampsC0InStore := pk.GetAllVscSendTimestamps(ctx, cChainIDs[0])
sort.Slice(vscSendTimestampsC0InStore, func(i, j int) bool {
return vscSendTimestampsC0InStore[i].VscId < vscSendTimestampsC0InStore[j].VscId
})

require.Equal(t, vscSendTimestampsC0InStore, exportedVscSendTimeStampsToVscSendTimeStamps(exportedVscSendTimeStampsC0))

vscSendTimestampsC1InStore := pk.GetAllVscSendTimestamps(ctx, cChainIDs[1])
sort.Slice(vscSendTimestampsC1InStore, func(i, j int) bool {
return vscSendTimestampsC1InStore[i].VscId < vscSendTimestampsC1InStore[j].VscId
})
require.Equal(t, vscSendTimestampsC1InStore, exportedVscSendTimeStampsToVscSendTimeStamps(exportedVscSendTimeStampsC1))
}

func exportedVscSendTimeStampsToVscSendTimeStamps(exportedVscSendTimeStamps []providertypes.ExportedVscSendTimestamp) []providertypes.VscSendTimestamp {
vscSendTimeStamps := []providertypes.VscSendTimestamp{}
for _, exportedVscSendTimeStamp := range exportedVscSendTimeStamps {
vscSendTimeStamps = append(vscSendTimeStamps, exportedVscSendTimeStamp.VscSendTimestamp)
}

sort.Slice(vscSendTimeStamps, func(i, j int) bool {
return vscSendTimeStamps[i].VscId < vscSendTimeStamps[j].VscId
})

return vscSendTimeStamps
}

func assertConsumerChainStates(t *testing.T, ctx sdk.Context, pk keeper.Keeper, consumerStates ...providertypes.ConsumerState) {
Expand Down
2 changes: 2 additions & 0 deletions x/ccv/provider/module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ func TestInitGenesis(t *testing.T) {
nil,
nil,
nil,
nil,
nil,
)

cdc := keeperParams.Cdc
Expand Down
4 changes: 4 additions & 0 deletions x/ccv/provider/types/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ func NewGenesisState(
validatorConsumerPubkeys []ValidatorConsumerPubKey,
validatorsByConsumerAddr []ValidatorByConsumerAddr,
consumerAddrsToPrune []ConsumerAddrsToPrune,
initTimeoutTimestamps []InitTimeoutTimestamp,
exportedVscSendTimestamps []ExportedVscSendTimestamp,
) *GenesisState {
return &GenesisState{
ValsetUpdateId: vscID,
Expand All @@ -35,6 +37,8 @@ func NewGenesisState(
ValidatorConsumerPubkeys: validatorConsumerPubkeys,
ValidatorsByConsumerAddr: validatorsByConsumerAddr,
ConsumerAddrsToPrune: consumerAddrsToPrune,
InitTimeoutTimestamps: initTimeoutTimestamps,
ExportedVscSendTimestamps: exportedVscSendTimestamps,
MSalopek marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand Down
Loading