-
Notifications
You must be signed in to change notification settings - Fork 107
/
messages.go
80 lines (70 loc) · 2.91 KB
/
messages.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
package governance
import (
"fmt"
"github.com/oasisprotocol/oasis-core/go/common/cbor"
"github.com/oasisprotocol/oasis-core/go/consensus/cometbft/api"
governanceState "github.com/oasisprotocol/oasis-core/go/consensus/cometbft/apps/governance/state"
governance "github.com/oasisprotocol/oasis-core/go/governance/api"
upgrade "github.com/oasisprotocol/oasis-core/go/upgrade/api"
)
func (app *governanceApplication) completeStateSync(ctx *api.Context) (interface{}, error) {
// State sync has just completed, check whether there are any pending upgrades to make
// sure we don't miss them after the sync.
state := governanceState.NewMutableState(ctx.State())
pendingUpgrades, err := state.PendingUpgrades(ctx)
if err != nil {
return nil, fmt.Errorf("cometbft/governance: couldn't get pending upgrades: %w", err)
}
// Apply all pending upgrades locally.
if upgrader := ctx.AppState().Upgrader(); upgrader != nil {
for _, pu := range pendingUpgrades {
switch err = upgrader.SubmitDescriptor(pu); err {
case nil, upgrade.ErrAlreadyPending:
default:
ctx.Logger().Error("failed to locally apply the upgrade descriptor",
"err", err,
"descriptor", pu,
)
}
}
}
// No execute message results at this time.
return nil, nil
}
func (app *governanceApplication) changeParameters(ctx *api.Context, msg interface{}, apply bool) (interface{}, error) {
// Unmarshal changes and check if they should be applied to this module.
proposal, ok := msg.(*governance.ChangeParametersProposal)
if !ok {
return nil, fmt.Errorf("cometbft/governance: failed to type assert change parameters proposal")
}
if proposal.Module != governance.ModuleName {
return nil, nil
}
var changes governance.ConsensusParameterChanges
if err := cbor.Unmarshal(proposal.Changes, &changes); err != nil {
return nil, fmt.Errorf("cometbft/governance: failed to unmarshal consensus parameter changes: %w", err)
}
// Validate changes against current parameters.
state := governanceState.NewMutableState(ctx.State())
params, err := state.ConsensusParameters(ctx)
if err != nil {
return nil, fmt.Errorf("cometbft/governance: failed to load consensus parameters: %w", err)
}
if err = changes.SanityCheck(); err != nil {
return nil, fmt.Errorf("cometbft/governance: failed to validate consensus parameter changes: %w", err)
}
if err = changes.Apply(params); err != nil {
return nil, fmt.Errorf("cometbft/governance: failed to apply consensus parameter changes: %w", err)
}
if err = params.SanityCheck(); err != nil {
return nil, fmt.Errorf("cometbft/governance: failed to validate consensus parameters: %w", err)
}
// Apply changes.
if apply {
if err = state.SetConsensusParameters(ctx, params); err != nil {
return nil, fmt.Errorf("cometbft/governance: failed to update consensus parameters: %w", err)
}
}
// Non-nil response signals that changes are valid and were successfully applied (if required).
return struct{}{}, nil
}