Skip to content

Commit

Permalink
feat(x/staking): add app wiring support for StakingHooks (#12291)
Browse files Browse the repository at this point in the history
## Description

Ref #12036 
Depends on app wiring for x/distribution getting merged first (#12292)



---

### Author Checklist

*All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.*

I have...

- [ ] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] added `!` to the type prefix if API or client breaking change
- [ ] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#pr-targeting))
- [ ] provided a link to the relevant issue or specification
- [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/main/docs/building-modules)
- [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#testing)
- [ ] added a changelog entry to `CHANGELOG.md`
- [ ] included comments for [documenting Go code](https://blog.golang.org/godoc)
- [ ] updated the relevant documentation or specification
- [ ] reviewed "Files changed" and left comments if necessary
- [ ] confirmed all CI checks have passed

### Reviewers Checklist

*All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.*

I have...

- [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] confirmed `!` in the type prefix if API or client breaking change
- [ ] confirmed all author checklist items have been addressed 
- [ ] reviewed state machine logic
- [ ] reviewed API design and naming
- [ ] reviewed documentation is accurate
- [ ] reviewed tests and test coverage
- [ ] manually tested (if applicable)
  • Loading branch information
aaronc committed Jun 22, 2022
1 parent 91b1d83 commit 165e612
Show file tree
Hide file tree
Showing 15 changed files with 437 additions and 43 deletions.
178 changes: 158 additions & 20 deletions api/cosmos/staking/module/v1/module.pulsar.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions core/appconfig/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"sigs.k8s.io/yaml"

appv1alpha1 "cosmossdk.io/api/cosmos/app/v1alpha1"

"github.com/cosmos/cosmos-sdk/depinject"

"cosmossdk.io/core/internal"
Expand Down Expand Up @@ -95,6 +96,10 @@ func Compose(appConfig *appv1alpha1.Config) depinject.Config {
for _, provider := range init.Providers {
opts = append(opts, depinject.ProvideInModule(module.Name, provider))
}

for _, invoker := range init.Invokers {
opts = append(opts, depinject.InvokeInModule(module.Name, invoker))
}
}

return depinject.Configs(opts...)
Expand Down
18 changes: 18 additions & 0 deletions core/appmodule/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,21 @@ func Provide(providers ...interface{}) Option {
return nil
})
}

// Invoke registers invokers to run with depinject. Each invoker will be called
// at the end of dependency graph configuration in the order in which it was defined. Invokers may not define output
// parameters, although they may return an error, and all of their input parameters will be marked as optional so that
// invokers impose no additional constraints on the dependency graph. Invoker functions should nil-check all inputs.
func Invoke(invokers ...interface{}) Option {
return funcOption(func(initializer *internal.ModuleInitializer) error {
for _, invoker := range invokers {
desc, err := depinject.ExtractProviderDescriptor(invoker)
if err != nil {
return err
}

initializer.Invokers = append(initializer.Invokers, desc)
}
return nil
})
}
2 changes: 2 additions & 0 deletions core/internal/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"google.golang.org/protobuf/reflect/protoreflect"

appv1alpha1 "cosmossdk.io/api/cosmos/app/v1alpha1"

"github.com/cosmos/cosmos-sdk/depinject"
)

Expand All @@ -21,6 +22,7 @@ type ModuleInitializer struct {
ConfigProtoMessage proto.Message
Error error
Providers []depinject.ProviderDescriptor
Invokers []depinject.ProviderDescriptor
}

// ModulesByProtoMessageName should be used to retrieve modules by their protobuf name.
Expand Down
39 changes: 39 additions & 0 deletions depinject/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,45 @@ func provide(ctr *container, key *moduleKey, providers []interface{}) error {
return nil
}

// Invoke defines a container configuration which registers the provided invoker functions. Each invoker will be called
// at the end of dependency graph configuration in the order in which it was defined. Invokers may not define output
// parameters, although they may return an error, and all of their input parameters will be marked as optional so that
// invokers impose no additional constraints on the dependency graph. Invoker functions should nil-check all inputs.
func Invoke(invokers ...interface{}) Config {
return containerConfig(func(ctr *container) error {
return invoke(ctr, nil, invokers)
})
}

// InvokeInModule defines a container configuration which registers the provided invoker functions to run in the
// provided module scope. Each invoker will be called
// at the end of dependency graph configuration in the order in which it was defined. Invokers may not define output
// parameters, although they may return an error, and all of their input parameters will be marked as optional so that
// invokers impose no additional constraints on the dependency graph. Invoker functions should nil-check all inputs.
func InvokeInModule(moduleName string, invokers ...interface{}) Config {
return containerConfig(func(ctr *container) error {
if moduleName == "" {
return errors.Errorf("expected non-empty module name")
}

return invoke(ctr, ctr.createOrGetModuleKey(moduleName), invokers)
})
}

func invoke(ctr *container, key *moduleKey, invokers []interface{}) error {
for _, c := range invokers {
rc, err := ExtractProviderDescriptor(c)
if err != nil {
return errors.WithStack(err)
}
err = ctr.addInvoker(&rc, key)
if err != nil {
return err
}
}
return nil
}

// BindInterface defines a container configuration for an explicit interface binding of inTypeName to outTypeName
// in global scope. The example below demonstrates a configuration where the container always provides a Canvasback
// instance when an interface of type Duck is requested as an input.
Expand Down
Loading

0 comments on commit 165e612

Please sign in to comment.