Skip to content

Commit

Permalink
feat: update GetAllValidatorsByConsumerAddr for fast find consensus k…
Browse files Browse the repository at this point in the history
…ey in use (#1279)

* update GetAllValidatorsByConsumerAddr

* fix test

* update ValidatorConsensusKeyInUse
  • Loading branch information
yaruwangway committed Oct 2, 2023
1 parent 3a3e157 commit 9fb3871
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 8 deletions.
2 changes: 1 addition & 1 deletion tests/e2e/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ func (s *TestConfig) SetRelayerConfig(useRly bool) {
}

// validateStringLiterals enforces that configs follow the constraints
// necessary to to execute the tests
// necessary to execute the tests
//
// Note: Network interfaces (name of virtual ethernet interfaces for ip link)
// within the container will be named as "$CHAIN_ID-$VAL_ID-out" etc.
Expand Down
26 changes: 19 additions & 7 deletions x/ccv/provider/keeper/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,16 +98,28 @@ func ValidatorConsensusKeyInUse(k *Keeper, ctx sdk.Context, valAddr sdk.ValAddre
panic("could not get validator cons addr ")
}

inUse := false

for _, validatorConsumerAddrs := range k.GetAllValidatorsByConsumerAddr(ctx, nil) {
if sdk.ConsAddress(validatorConsumerAddrs.ConsumerAddr).Equals(consensusAddr) {
inUse = true
break
allConsumerChains := []string{}
consumerChains := k.GetAllConsumerChains(ctx)
for _, consumerChain := range consumerChains {
allConsumerChains = append(allConsumerChains, consumerChain.ChainId)
}
proposedChains := k.GetAllProposedConsumerChainIDs(ctx)
for _, proposedChain := range proposedChains {
allConsumerChains = append(allConsumerChains, proposedChain.ChainID)
}
pendingChainIDs := k.GetAllPendingConsumerChainIDs(ctx)
allConsumerChains = append(allConsumerChains, pendingChainIDs...)

for _, c := range allConsumerChains {
if _, exist := k.GetValidatorByConsumerAddr(
ctx,
c,
providertypes.NewConsumerConsAddress(consensusAddr)); exist {
return true
}
}

return inUse
return false
}

func (h Hooks) AfterValidatorCreated(ctx sdk.Context, valAddr sdk.ValAddress) error {
Expand Down
4 changes: 4 additions & 0 deletions x/ccv/provider/keeper/hooks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func TestValidatorConsensusKeyInUse(t *testing.T) {
newValidator.ConsumerConsAddress(),
anotherValidator0.ProviderConsAddress(),
)
k.SetConsumerClientId(ctx, "chainid", "clientID")
},
expect: true,
},
Expand All @@ -50,10 +51,13 @@ func TestValidatorConsensusKeyInUse(t *testing.T) {
newValidator.ConsumerConsAddress(),
anotherValidator0.ProviderConsAddress(),
)
k.SetConsumerClientId(ctx, "chainid0", "clientID0")

k.SetValidatorByConsumerAddr(ctx, "chainid1",
anotherValidator1.ConsumerConsAddress(),
anotherValidator1.ProviderConsAddress(),
)
k.SetConsumerClientId(ctx, "chainid1", "clientID1")
},
expect: true,
},
Expand Down
11 changes: 11 additions & 0 deletions x/ccv/provider/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,17 @@ func (k Keeper) GetAllProposedConsumerChainIDs(ctx sdk.Context) []types.Proposed
return proposedChains
}

// GetAllPendingConsumerChainIDs gets pending consumer chains have not reach spawn time
func (k Keeper) GetAllPendingConsumerChainIDs(ctx sdk.Context) []string {
chainIDs := []string{}
props := k.GetAllPendingConsumerAdditionProps(ctx)
for _, prop := range props {
chainIDs = append(chainIDs, prop.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

0 comments on commit 9fb3871

Please sign in to comment.