/
msg_server_undelegate_from_gateway.go
69 lines (55 loc) · 2.48 KB
/
msg_server_undelegate_from_gateway.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
68
69
package keeper
import (
"context"
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/pokt-network/poktroll/telemetry"
"github.com/pokt-network/poktroll/x/application/types"
)
func (k msgServer) UndelegateFromGateway(ctx context.Context, msg *types.MsgUndelegateFromGateway) (*types.MsgUndelegateFromGatewayResponse, error) {
isSuccessful := false
defer telemetry.EventSuccessCounter(
"undelegate_from_gateway",
telemetry.DefaultCounterFn,
func() bool { return isSuccessful },
)
logger := k.Logger().With("method", "UndelegateFromGateway")
logger.Info(fmt.Sprintf("About to undelegate application from gateway with msg: %v", msg))
if err := msg.ValidateBasic(); err != nil {
logger.Error(fmt.Sprintf("Undelegation Message failed basic validation: %v", err))
return nil, err
}
// Retrieve the application from the store
foundApp, isAppFound := k.GetApplication(ctx, msg.AppAddress)
if !isAppFound {
logger.Info(fmt.Sprintf("Application not found with address [%s]", msg.AppAddress))
return nil, types.ErrAppNotFound.Wrapf("application not found with address: %s", msg.AppAddress)
}
logger.Info(fmt.Sprintf("Application found with address [%s]", msg.AppAddress))
// Check if the application is already delegated to the gateway
foundIdx := -1
for i, gatewayAddr := range foundApp.DelegateeGatewayAddresses {
if gatewayAddr == msg.GatewayAddress {
foundIdx = i
}
}
if foundIdx == -1 {
logger.Info(fmt.Sprintf("Application not delegated to gateway with address [%s]", msg.GatewayAddress))
return nil, types.ErrAppNotDelegated.Wrapf("application not delegated to gateway with address: %s", msg.GatewayAddress)
}
// Remove the gateway from the application's delegatee gateway public keys
foundApp.DelegateeGatewayAddresses = append(foundApp.DelegateeGatewayAddresses[:foundIdx], foundApp.DelegateeGatewayAddresses[foundIdx+1:]...)
// Update the application store with the new delegation
k.SetApplication(ctx, foundApp)
logger.Info(fmt.Sprintf("Successfully undelegated application from gateway for app: %+v", foundApp))
// Emit the application redelegation event
event := msg.NewRedelegationEvent()
logger.Info(fmt.Sprintf("Emitting application redelegation event %v", event))
sdkCtx := sdk.UnwrapSDKContext(ctx)
if err := sdkCtx.EventManager().EmitTypedEvent(event); err != nil {
logger.Error(fmt.Sprintf("Failed to emit application redelegation event: %v", err))
return nil, err
}
isSuccessful = true
return &types.MsgUndelegateFromGatewayResponse{}, nil
}