-
Notifications
You must be signed in to change notification settings - Fork 6
/
proposal_handler.go
67 lines (55 loc) · 2.14 KB
/
proposal_handler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package keeper
import (
errorsmod "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
errortypes "github.com/cosmos/cosmos-sdk/types/errors"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
"github.com/xpladev/xpla/x/volunteer/types"
)
func NewVolunteerValidatorProposalHandler(k Keeper) govtypes.Handler {
return func(ctx sdk.Context, content govtypes.Content) error {
switch c := content.(type) {
case *types.RegisterVolunteerValidatorProposal:
return handlerRegisterVolunteerValidatorProposal(ctx, k, c)
case *types.UnregisterVolunteerValidatorProposal:
return handlerUnregisterVolunteerValidatorProposal(ctx, k, c)
default:
return sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "unrecognized volunteer validator proposal content type: %T", c)
}
}
}
func handlerRegisterVolunteerValidatorProposal(ctx sdk.Context, k Keeper, p *types.RegisterVolunteerValidatorProposal) error {
valAddress, err := sdk.ValAddressFromBech32(p.ValidatorAddress)
if err != nil {
return err
}
if _, found := k.stakingKeeper.GetValidator(ctx, valAddress); found {
return stakingtypes.ErrValidatorOwnerExists
}
k.SetVolunteerValidator(ctx, valAddress, types.NewVolunteerValidator(valAddress, 0))
createValidatorMsg := p.ToCreateValidator()
if err := k.CreateValidator(ctx, createValidatorMsg); err != nil {
return err
}
return nil
}
func handlerUnregisterVolunteerValidatorProposal(ctx sdk.Context, k Keeper, p *types.UnregisterVolunteerValidatorProposal) error {
valAddress, err := sdk.ValAddressFromBech32(p.ValidatorAddress)
if err != nil {
return err
}
_, found := k.GetVolunteerValidator(ctx, valAddress)
if !found {
return errorsmod.Wrapf(errortypes.ErrNotFound, `volunteer validator (%s)`, valAddress.String())
}
if validator, found := k.stakingKeeper.GetValidator(ctx, valAddress); found {
_, err := k.stakingKeeper.Undelegate(ctx, sdk.AccAddress(valAddress), valAddress, validator.DelegatorShares)
if err != nil {
return err
}
k.DeleteVolunteerValidator(ctx, valAddress)
}
return nil
}