Skip to content

Commit

Permalink
feat: add exportedVscSendTimestamps
Browse files Browse the repository at this point in the history
  • Loading branch information
Yaru Wang committed Jun 26, 2023
1 parent b845cbb commit cb6588b
Show file tree
Hide file tree
Showing 9 changed files with 430 additions and 232 deletions.
2 changes: 1 addition & 1 deletion proto/interchain_security/ccv/provider/v1/genesis.proto
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ message GenesisState {
repeated interchain_security.ccv.provider.v1.InitTimeoutTimestamp init_timeout_timestamps = 12
[ (gogoproto.nullable) = false ];

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

Expand Down
12 changes: 9 additions & 3 deletions proto/interchain_security/ccv/provider/v1/provider.proto
Original file line number Diff line number Diff line change
Expand Up @@ -216,12 +216,18 @@ message InitTimeoutTimestamp {
}

message VscSendTimestamp {
string chain_id = 1;
uint64 vsc_id = 2;
google.protobuf.Timestamp timestamp = 3
uint64 vsc_id = 1;
google.protobuf.Timestamp timestamp = 2
[ (gogoproto.stdtime) = true, (gogoproto.nullable) = false ];
}

// ExportedVscSendTimestamps is VscSendTimestamp with chainID info for exporting to genesis
message ExportedVscSendTimestamp {
string chain_id = 1;
VscSendTimestamp vsc_send_timestamp = 2
[ (gogoproto.nullable) = false ];
}

//
// Key assignment section
//
Expand Down
13 changes: 8 additions & 5 deletions x/ccv/provider/keeper/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ func (k Keeper) InitGenesis(ctx sdk.Context, genState *types.GenesisState) {
k.SetInitTimeoutTimestamp(ctx, item.ChainId, item.Timestamp)
}

for _, item := range genState.VscSendTimestamps {
k.SetVscSendTimestamp(ctx, item.ChainId, item.VscId, item.Timestamp)
for _, item := range genState.ExportedVscSendTimestamps {
k.SetVscSendTimestamp(ctx, item.ChainId, item.VscSendTimestamp.VscId, item.VscSendTimestamp.Timestamp)
}

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

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

vscSendTimestamps = append(vscSendTimestamps, k.GetAllVscSendTimestamps(ctx, chain.ChainId)...)
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 @@ -162,6 +165,6 @@ func (k Keeper) ExportGenesis(ctx sdk.Context) *types.GenesisState {
k.GetAllValidatorsByConsumerAddr(ctx, nil),
consumerAddrsToPrune,
k.GetAllInitTimeoutTimestamps(ctx),
vscSendTimestamps,
exportedVscSendTimestamps,
)
}
34 changes: 24 additions & 10 deletions x/ccv/provider/keeper/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,16 @@ func TestInitAndExportGenesis(t *testing.T) {
}

now := time.Now().UTC()
vscSendTimeStampsC0 := []providertypes.VscSendTimestamp{
{ChainId: "c0", VscId: 1, Timestamp: now.Add(time.Hour)},
{ChainId: "c0", VscId: 2, Timestamp: now.Add(2 * time.Hour)},
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)}},
}

vscSendTimeStampsC1 := []providertypes.VscSendTimestamp{
{ChainId: "c1", VscId: 1, Timestamp: now.Add(-time.Hour)},
{ChainId: "c1", VscId: 2, Timestamp: now.Add(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)}},
}
vscSendTimeStampsAll := append(vscSendTimeStampsC0, vscSendTimeStampsC1...)
exportedVscSendTimeStampsAll := append(exportedVscSendTimeStampsC0, exportedVscSendTimeStampsC1...)
// create genesis struct
provGenesis := providertypes.NewGenesisState(vscID,
[]providertypes.ValsetUpdateIdToHeight{{ValsetUpdateId: vscID, Height: initHeight}},
Expand Down Expand Up @@ -115,7 +115,7 @@ func TestInitAndExportGenesis(t *testing.T) {
},
},
initTimeoutTimeStamps,
vscSendTimeStampsAll,
exportedVscSendTimeStampsAll,
)

// Instantiate in-mem provider keeper with mocks
Expand Down Expand Up @@ -193,13 +193,27 @@ func TestInitAndExportGenesis(t *testing.T) {
sort.Slice(vscSendTimestampsC0InStore, func(i, j int) bool {
return vscSendTimestampsC0InStore[i].VscId < vscSendTimestampsC0InStore[j].VscId
})
require.Equal(t, vscSendTimestampsC0InStore, vscSendTimeStampsC0)

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, vscSendTimeStampsC1)
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: 0 additions & 2 deletions x/ccv/provider/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -986,7 +986,6 @@ func (k Keeper) GetAllVscSendTimestamps(ctx sdk.Context, chainID string) (vscSen
}

vscSendTimestamps = append(vscSendTimestamps, types.VscSendTimestamp{
ChainId: chainID,
VscId: vscID,
Timestamp: ts,
})
Expand Down Expand Up @@ -1033,7 +1032,6 @@ func (k Keeper) GetFirstVscSendTimestamp(ctx sdk.Context, chainID string) (vscSe
}

return types.VscSendTimestamp{
ChainId: chainID,
VscId: vscID,
Timestamp: ts,
}, true
Expand Down
2 changes: 1 addition & 1 deletion x/ccv/provider/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ func TestVscSendTimestamp(t *testing.T) {
expectedGetAllOrder := []types.VscSendTimestamp{}
for _, tc := range testCases {
if tc.chainID == chainID {
expectedGetAllOrder = append(expectedGetAllOrder, types.VscSendTimestamp{ChainId: tc.chainID, VscId: tc.vscID, Timestamp: tc.ts})
expectedGetAllOrder = append(expectedGetAllOrder, types.VscSendTimestamp{VscId: tc.vscID, Timestamp: tc.ts})
}
}
// sorting by vscID
Expand Down
4 changes: 2 additions & 2 deletions x/ccv/provider/types/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func NewGenesisState(
validatorsByConsumerAddr []ValidatorByConsumerAddr,
consumerAddrsToPrune []ConsumerAddrsToPrune,
initTimeoutTimestamps []InitTimeoutTimestamp,
vscSendTimestamps []VscSendTimestamp,
exportedVscSendTimestamps []ExportedVscSendTimestamp,
) *GenesisState {
return &GenesisState{
ValsetUpdateId: vscID,
Expand All @@ -38,7 +38,7 @@ func NewGenesisState(
ValidatorsByConsumerAddr: validatorsByConsumerAddr,
ConsumerAddrsToPrune: consumerAddrsToPrune,
InitTimeoutTimestamps: initTimeoutTimestamps,
VscSendTimestamps: vscSendTimestamps,
ExportedVscSendTimestamps: exportedVscSendTimestamps,
}
}

Expand Down
Loading

0 comments on commit cb6588b

Please sign in to comment.