Skip to content

Commit

Permalink
Fix: export InitTimeoutTimestamps and VscSendTimestamps to genesis (#…
Browse files Browse the repository at this point in the history
…1076)

* fix: add InitTimeoutTimestamps and VscSendTimestamps to genesis

* fix: add chainID to vscSendTimestamp

* test: fix tests

* test: fixgenesis test

* test: fix test TestInitAndExportGenesis

* feat: add exportedVscSendTimestamps

* docs: update changelog

* feat: update exportedVscSendTimestamps

* fix: lint
  • Loading branch information
yaruwangway committed Jul 19, 2023
1 parent 6204bd8 commit 064c601
Show file tree
Hide file tree
Showing 12 changed files with 668 additions and 163 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## [Unreleased]

Add an entry to the unreleased section whenever merging a PR to main that is not targeted at a specific release. These entries will eventually be included in a release.
* `[x/ccv/provider]` (fix) [#1076](https://github.com/cosmos/interchain-security/pull/1076) Add `InitTimeoutTimestamps` and `ExportedVscSendTimestamps` to exported genesis.

* (fix!) revert consumer packet data changes from #1037 [#1150](https://github.com/cosmos/interchain-security/pull/1150)
* (fix!) proper deletion of pending packets [#1146](https://github.com/cosmos/interchain-security/pull/1146)
Expand Down
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; }
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 {
string chain_id = 1;
repeated VscSendTimestamp vsc_send_timestamps = 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 {}
15 changes: 15 additions & 0 deletions x/ccv/provider/keeper/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,16 @@ 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 {
for _, vscSendTimestamp := range item.VscSendTimestamps {
k.SetVscSendTimestamp(ctx, item.ChainId, vscSendTimestamp.VscId, vscSendTimestamp.Timestamp)
}
}

k.SetParams(ctx, genState.Params)
k.InitializeSlashMeter(ctx)
}
Expand All @@ -100,6 +110,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 @@ -130,6 +141,8 @@ 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)
exportedVscSendTimestamps = append(exportedVscSendTimestamps, types.ExportedVscSendTimestamp{ChainId: chain.ChainId, VscSendTimestamps: vscSendTimestamps})
}

// ConsumerAddrsToPrune are added only for registered consumer chains
Expand All @@ -152,5 +165,7 @@ func (k Keeper) ExportGenesis(ctx sdk.Context) *types.GenesisState {
k.GetAllValidatorConsumerPubKeys(ctx, nil),
k.GetAllValidatorsByConsumerAddr(ctx, nil),
consumerAddrsToPrune,
k.GetAllInitTimeoutTimestamps(ctx),
exportedVscSendTimestamps,
)
}
47 changes: 47 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 @@ -36,6 +37,32 @@ 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",
VscSendTimestamps: []providertypes.VscSendTimestamp{
{VscId: 1, Timestamp: now.Add(time.Hour)},
{VscId: 2, Timestamp: now.Add(2 * time.Hour)},
},
}

exportedVscSendTimeStampsC1 := providertypes.ExportedVscSendTimestamp{
ChainId: "c1",
VscSendTimestamps: []providertypes.VscSendTimestamp{
{VscId: 1, Timestamp: now.Add(-time.Hour)},
{VscId: 2, Timestamp: now.Add(time.Hour)},
},
}

var exportedVscSendTimeStampsAll []providertypes.ExportedVscSendTimestamp
exportedVscSendTimeStampsAll = append(exportedVscSendTimeStampsAll, exportedVscSendTimeStampsC0)
exportedVscSendTimeStampsAll = append(exportedVscSendTimeStampsAll, exportedVscSendTimeStampsC1)

// create genesis struct
provGenesis := providertypes.NewGenesisState(vscID,
[]providertypes.ValsetUpdateIdToHeight{{ValsetUpdateId: vscID, Height: initHeight}},
Expand Down Expand Up @@ -98,6 +125,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 @@ -164,6 +193,24 @@ 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, exportedVscSendTimeStampsC0.VscSendTimestamps)

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, exportedVscSendTimeStampsC1.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 @@ -108,6 +108,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 @@ -24,6 +24,8 @@ func NewGenesisState(
validatorConsumerPubkeys []ValidatorConsumerPubKey,
validatorsByConsumerAddr []ValidatorByConsumerAddr,
consumerAddrsToPrune []ConsumerAddrsToPrune,
initTimeoutTimestamps []InitTimeoutTimestamp,
exportedVscSendTimestamps []ExportedVscSendTimestamp,
) *GenesisState {
return &GenesisState{
ValsetUpdateId: vscID,
Expand All @@ -37,6 +39,8 @@ func NewGenesisState(
ValidatorConsumerPubkeys: validatorConsumerPubkeys,
ValidatorsByConsumerAddr: validatorsByConsumerAddr,
ConsumerAddrsToPrune: consumerAddrsToPrune,
InitTimeoutTimestamps: initTimeoutTimestamps,
ExportedVscSendTimestamps: exportedVscSendTimestamps,
}
}

Expand Down
Loading

0 comments on commit 064c601

Please sign in to comment.