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

feat!: store proposed chainID before voting finishes #1289

Merged
merged 38 commits into from
Oct 2, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
ac7e1b9
feat: store chain in proposal
Sep 12, 2023
0eea515
add govHook
Sep 12, 2023
a79f384
delete GetChainsInProposal
Sep 14, 2023
690c61c
check proposal type
Sep 14, 2023
658b5f5
update key
Sep 15, 2023
b0a3721
feat: add query proposed chainIDs
Sep 19, 2023
fbb74fc
feat: set govhook
Sep 19, 2023
b61bd85
feat: parse key
Sep 19, 2023
d13c124
refactor: names
Sep 19, 2023
1c6d099
feat: add list proposed consumer chains
Sep 20, 2023
2f007f5
test: add e2e test
Sep 20, 2023
402fcd4
merge main
Sep 20, 2023
27daf3a
add e2e test
Sep 20, 2023
114db15
update comments
Sep 20, 2023
74b7d91
update ProposeConsumerChains in e2e test
Sep 21, 2023
8db40a8
remove wait for block
Sep 21, 2023
5a26420
docs: update changelog
Sep 21, 2023
956b4cc
fix: lint
Sep 21, 2023
f193361
add TestParseProposedConsumerChainKey
Sep 21, 2023
c2cb529
refactor gov hook
Sep 21, 2023
8253ee7
Update proto/interchain_security/ccv/provider/v1/query.proto
yaruwangway Sep 21, 2023
f0c591e
update proto
Sep 21, 2023
4aa274a
add test for set kv
Sep 21, 2023
979b383
refactor key to be prefix_proposalID
Sep 21, 2023
b020e49
formatting
Sep 21, 2023
a90f87c
update e2e test
Sep 21, 2023
c159764
format
Sep 21, 2023
1f2f388
Update x/ccv/provider/keeper/gov_hook.go
yaruwangway Sep 22, 2023
39ca877
Update x/ccv/provider/keeper/keeper.go
yaruwangway Sep 22, 2023
85d0f33
Update x/ccv/provider/keeper/keeper.go
yaruwangway Sep 22, 2023
e094e42
fix e2e test
Sep 22, 2023
c7cda12
fix gosec
Sep 22, 2023
6bf0f39
remove type url check
Oct 2, 2023
dc7371b
test: add unit test
Oct 2, 2023
5ee3116
lint
Oct 2, 2023
d31f4c8
fix lint
Oct 2, 2023
533ea06
fix err
Oct 2, 2023
834a2af
Merge branch 'feat/refactor-key-assignment' into chain-in-proposal
yaruwangway Oct 2, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 82 additions & 0 deletions x/ccv/provider/keeper/gov_hook.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package keeper

import (
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
govkeeper "github.com/cosmos/cosmos-sdk/x/gov/keeper"
sdkgov "github.com/cosmos/cosmos-sdk/x/gov/types"
v1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1"

"github.com/cosmos/gogoproto/proto"
"github.com/cosmos/interchain-security/v3/x/ccv/provider/types"
)

type GovHooks struct {
gk govkeeper.Keeper
k *Keeper
}

// Implements GovHooks interface
// GovHooks exist in cosmos-sdk/x/gov/keeper/hooks.go of v0.45.16-lsm-ics and on
var _ sdkgov.GovHooks = GovHooks{}

func (k *Keeper) Hooks() Hooks {
return Hooks{k}
}

// AfterProposalSubmission - call hook if registered
// After consumerAddition proposal submission, the consumer chainID is stored
func (gh GovHooks) AfterProposalSubmission(ctx sdk.Context, proposalID uint64) {
p, ok := gh.gk.GetProposal(ctx, proposalID)
if !ok {
panic(fmt.Errorf("failed to get proposal %d in gov hook", proposalID))
}
msgs := p.GetMessages()

var msgLegacyContent v1.MsgExecLegacyContent
err := proto.Unmarshal(msgs[0].Value, &msgLegacyContent)
if err != nil {
panic(fmt.Errorf("failed to unmarshal proposal content in gov hook: %w", err))
}

// if the proposal is not ConsumerAdditionProposal, return
var consadditionProp types.ConsumerAdditionProposal
if err := proto.Unmarshal(msgLegacyContent.Content.Value, &consadditionProp); err != nil {
return
}

if consadditionProp.ProposalType() == types.ProposalTypeConsumerRemoval {
gh.k.SetChainsInProposal(ctx, consadditionProp.ChainId, proposalID)
}
}

// AfterProposalVotingPeriodEnded - call hook if registered
// After proposal voting ends, the consumer chainID in store is deleted.
// When a proposal passes, this chainID will be available in providerKeeper.GetAllPendingConsumerAdditionProps
// or providerKeeper.GetAllConsumerChains(ctx).
func (gh GovHooks) AfterProposalVotingPeriodEnded(ctx sdk.Context, proposalID uint64) {
p, ok := gh.gk.GetProposal(ctx, proposalID)
if !ok {
panic(fmt.Errorf("failed to get proposal %d in gov hook", proposalID))
}
msgs := p.GetMessages()

var msgLegacyContent v1.MsgExecLegacyContent
err := proto.Unmarshal(msgs[0].Value, &msgLegacyContent)
if err != nil {
panic(fmt.Errorf("failed to unmarshal proposal content in gov hook: %w", err))
}
var consadditionProp types.ConsumerAdditionProposal

// if the proposal is not ConsumerAdditionProposal, return
if err := proto.Unmarshal(msgLegacyContent.Content.Value, &consadditionProp); err != nil {
return
}

gh.k.DeleteChainsInProposal(ctx, consadditionProp.ChainId)
}

func (gh GovHooks) AfterProposalDeposit(ctx sdk.Context, proposalID uint64, depositorAddr sdk.AccAddress) {
}
func (gh GovHooks) AfterProposalVote(ctx sdk.Context, proposalID uint64, voterAddr sdk.AccAddress) {}
func (gh GovHooks) AfterProposalFailedMinDeposit(ctx sdk.Context, proposalID uint64) {}
43 changes: 43 additions & 0 deletions x/ccv/provider/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,49 @@ func (k Keeper) DeleteChainToChannel(ctx sdk.Context, chainID string) {
store.Delete(types.ChainToChannelKey(chainID))
}

// GetChainsInProposal gets chainId in gov proposal before voting finishes
func (k Keeper) GetChainsInProposal(ctx sdk.Context, chainID string) (string, bool) {
yaruwangway marked this conversation as resolved.
Show resolved Hide resolved
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.ChainInProposalKey(chainID))
if bz == nil {
return "", false
}

return string(bz), true
}

// SetChainsInProposal sets consumer chainId in gov consumerAddition proposal in store
// the consumer chainId is only added when the voting period for consumerAddition proposal
// does not end.
func (k Keeper) SetChainsInProposal(ctx sdk.Context, chainID string, proposalID uint64) {
yaruwangway marked this conversation as resolved.
Show resolved Hide resolved
yaruwangway marked this conversation as resolved.
Show resolved Hide resolved
store := ctx.KVStore(k.storeKey)
store.Set(types.ChainInProposalKey(chainID), sdk.Uint64ToBigEndian(proposalID))
}

// DeleteChainsInProposal deletes the consumer chainID from store
// which is in gov consumerAddition proposal
func (k Keeper) DeleteChainsInProposal(ctx sdk.Context, chainID string) {
store := ctx.KVStore(k.storeKey)
store.Delete(types.ChainInProposalKey(chainID))
}

// GetAllChainsInProposal get consumer chainId in gov consumerAddition proposal before
// voting period ends.
func (k Keeper) GetAllChainsInProposal(ctx sdk.Context) []string {
yaruwangway marked this conversation as resolved.
Show resolved Hide resolved
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, []byte{types.ChainInProposalByteKey})
defer iterator.Close()

chainIDs := []string{}
for ; iterator.Valid(); iterator.Next() {
// remove 1 byte prefix from key to retrieve chainID
chainID := string(iterator.Key()[1:])
chainIDs = append(chainIDs, chainID)
}

return chainIDs
}

// GetAllConsumerChains gets all of the consumer chains, for which the provider module
// created IBC clients. Consumer chains with created clients are also referred to as registered.
//
Expand Down
7 changes: 7 additions & 0 deletions x/ccv/provider/types/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,8 @@ const (
// handled in the current block
VSCMaturedHandledThisBlockBytePrefix

// ChainInProposalByteKey is the byte prefix storing the consumer chainId in consumerAddition gov proposal submitted before voting finishes
ChainInProposalByteKey
// NOTE: DO NOT ADD NEW BYTE PREFIXES HERE WITHOUT ADDING THEM TO getAllKeyPrefixes() IN keys_test.go
)

Expand Down Expand Up @@ -414,6 +416,11 @@ func ChainIdWithLenKey(prefix byte, chainID string) []byte {
)
}

// ChainInProposalKey returns the consumer chainId in consumerAddition gov proposal submitted before voting finishes
func ChainInProposalKey(chainID string) []byte {
return append([]byte{ChainInProposalByteKey}, []byte(chainID)...)
}

// ParseChainIdAndTsKey returns the chain ID and time for a ChainIdAndTs key
func ParseChainIdAndTsKey(prefix byte, bz []byte) (string, time.Time, error) {
expectedPrefix := []byte{prefix}
Expand Down
1 change: 1 addition & 0 deletions x/ccv/provider/types/keys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ func getAllKeyPrefixes() []byte {
providertypes.ConsumerAddrsToPruneBytePrefix,
providertypes.SlashLogBytePrefix,
providertypes.VSCMaturedHandledThisBlockBytePrefix,
providertypes.ChainInProposalByteKey,
}
}

Expand Down
Loading