-
Notifications
You must be signed in to change notification settings - Fork 107
/
messages.go
54 lines (46 loc) · 1.96 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
package registry
import (
"fmt"
"github.com/oasisprotocol/oasis-core/go/common/cbor"
"github.com/oasisprotocol/oasis-core/go/consensus/tendermint/api"
registryState "github.com/oasisprotocol/oasis-core/go/consensus/tendermint/apps/registry/state"
governance "github.com/oasisprotocol/oasis-core/go/governance/api"
registry "github.com/oasisprotocol/oasis-core/go/registry/api"
)
func (app *registryApplication) 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("registry: failed to type assert change parameters proposal")
}
if proposal.Module != registry.ModuleName {
return nil, nil
}
var changes registry.ConsensusParameterChanges
if err := cbor.Unmarshal(proposal.Changes, &changes); err != nil {
return nil, fmt.Errorf("registry: failed to unmarshal consensus parameter changes: %w", err)
}
// Validate changes against current parameters.
state := registryState.NewMutableState(ctx.State())
params, err := state.ConsensusParameters(ctx)
if err != nil {
return nil, fmt.Errorf("registry: failed to load consensus parameters: %w", err)
}
if err = changes.SanityCheck(); err != nil {
return nil, fmt.Errorf("registry: failed to validate consensus parameter changes: %w", err)
}
if err = changes.Apply(params); err != nil {
return nil, fmt.Errorf("registry: failed to apply consensus parameter changes: %w", err)
}
if err = params.SanityCheck(); err != nil {
return nil, fmt.Errorf("registry: failed to validate consensus parameters: %w", err)
}
// Apply changes.
if apply {
if err = state.SetConsensusParameters(ctx, params); err != nil {
return nil, fmt.Errorf("registry: failed to update consensus parameters: %w", err)
}
}
// Non-nil response signals that changes are valid and were successfully applied (if required).
return struct{}{}, nil
}