/
exec.go
110 lines (94 loc) · 3.53 KB
/
exec.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package internal
import (
"fmt"
sdk "github.com/Finschia/finschia-sdk/types"
sdkerrors "github.com/Finschia/finschia-sdk/types/errors"
"github.com/Finschia/finschia-sdk/x/foundation"
)
// ensureMsgAuthz checks that if a message requires signers that all of them are equal to the given account address of the authority.
func ensureMsgAuthz(msgs []sdk.Msg, authority sdk.AccAddress) error {
for _, msg := range msgs {
// In practice, GetSigners() should return a non-empty array without
// duplicates, so the code below is equivalent to:
// `msgs[i].GetSigners()[0] == authority`
// but we prefer to loop through all GetSigners just to be sure.
for _, signer := range msg.GetSigners() {
if !authority.Equals(signer) {
return sdkerrors.ErrUnauthorized.Wrapf("bad signer; expected %s, got %s", authority, signer)
}
}
}
return nil
}
func (k Keeper) Exec(ctx sdk.Context, proposalID uint64) error {
proposal, err := k.GetProposal(ctx, proposalID)
if err != nil {
return err
}
if proposal.Status != foundation.PROPOSAL_STATUS_SUBMITTED &&
proposal.Status != foundation.PROPOSAL_STATUS_ACCEPTED {
return sdkerrors.ErrInvalidRequest.Wrapf("not possible with proposal status: %s", proposal.Status)
}
if proposal.Status == foundation.PROPOSAL_STATUS_SUBMITTED {
if err := k.doTallyAndUpdate(ctx, proposal); err != nil {
return err
}
}
// Execute proposal payload.
var logs string
if proposal.Status == foundation.PROPOSAL_STATUS_ACCEPTED &&
proposal.ExecutorResult != foundation.PROPOSAL_EXECUTOR_RESULT_SUCCESS {
logger := ctx.Logger().With("module", fmt.Sprintf("x/%s", foundation.ModuleName))
// Caching context so that we don't update the store in case of failure.
cacheCtx, flush := ctx.CacheContext()
if results, err := k.doExecuteMsgs(cacheCtx, *proposal); err != nil {
proposal.ExecutorResult = foundation.PROPOSAL_EXECUTOR_RESULT_FAILURE
logs = fmt.Sprintf("proposal execution failed on proposal %d, because of error %s", proposalID, err.Error())
logger.Info("proposal execution failed", "cause", err, "proposalID", proposal.Id)
} else {
proposal.ExecutorResult = foundation.PROPOSAL_EXECUTOR_RESULT_SUCCESS
flush()
for _, res := range results {
// NOTE: The sdk msg handler creates a new EventManager, so events must be correctly propagated back to the current context
ctx.EventManager().EmitEvents(res.GetEvents())
}
}
}
// If proposal has successfully run, delete it from state.
if proposal.ExecutorResult == foundation.PROPOSAL_EXECUTOR_RESULT_SUCCESS {
k.pruneProposal(ctx, *proposal)
} else {
k.setProposal(ctx, *proposal)
}
if err := ctx.EventManager().EmitTypedEvent(&foundation.EventExec{
ProposalId: proposal.Id,
Logs: logs,
Result: proposal.ExecutorResult,
}); err != nil {
panic(err)
}
return nil
}
// doExecuteMsgs routes the messages to the registered handlers.
func (k Keeper) doExecuteMsgs(ctx sdk.Context, proposal foundation.Proposal) ([]sdk.Result, error) {
msgs := proposal.GetMsgs()
results := make([]sdk.Result, len(msgs))
authority := sdk.MustAccAddressFromBech32(k.GetAuthority())
if err := ensureMsgAuthz(msgs, authority); err != nil {
return nil, err
}
for i, msg := range msgs {
handler := k.router.Handler(msg)
if handler == nil {
return nil, sdkerrors.ErrUnknownRequest.Wrapf("no message handler found for %q", sdk.MsgTypeURL(msg))
}
r, err := handler(ctx, msg)
if err != nil {
return nil, sdkerrors.Wrapf(err, "message %q at position %d", msg, i)
}
if r != nil {
results[i] = *r
}
}
return results, nil
}