-
Notifications
You must be signed in to change notification settings - Fork 107
/
registry.go
265 lines (226 loc) · 8.59 KB
/
registry.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
// Package registry implements the registry application.
package registry
import (
"fmt"
"math"
"github.com/tendermint/tendermint/abci/types"
beacon "github.com/oasisprotocol/oasis-core/go/beacon/api"
"github.com/oasisprotocol/oasis-core/go/common/cbor"
"github.com/oasisprotocol/oasis-core/go/common/entity"
"github.com/oasisprotocol/oasis-core/go/common/node"
"github.com/oasisprotocol/oasis-core/go/consensus/api/transaction"
"github.com/oasisprotocol/oasis-core/go/consensus/tendermint/api"
governanceApi "github.com/oasisprotocol/oasis-core/go/consensus/tendermint/apps/governance/api"
registryState "github.com/oasisprotocol/oasis-core/go/consensus/tendermint/apps/registry/state"
roothashApi "github.com/oasisprotocol/oasis-core/go/consensus/tendermint/apps/roothash/api"
stakingapp "github.com/oasisprotocol/oasis-core/go/consensus/tendermint/apps/staking"
stakingState "github.com/oasisprotocol/oasis-core/go/consensus/tendermint/apps/staking/state"
registry "github.com/oasisprotocol/oasis-core/go/registry/api"
"github.com/oasisprotocol/oasis-core/go/roothash/api/message"
staking "github.com/oasisprotocol/oasis-core/go/staking/api"
)
var _ api.Application = (*registryApplication)(nil)
type registryApplication struct {
state api.ApplicationState
md api.MessageDispatcher
}
func (app *registryApplication) Name() string {
return AppName
}
func (app *registryApplication) ID() uint8 {
return AppID
}
func (app *registryApplication) Methods() []transaction.MethodName {
return registry.Methods
}
func (app *registryApplication) Blessed() bool {
return false
}
func (app *registryApplication) Dependencies() []string {
return []string{stakingapp.AppName}
}
func (app *registryApplication) OnRegister(state api.ApplicationState, md api.MessageDispatcher) {
app.state = state
app.md = md
// Subscribe to messages emitted by other apps.
md.Subscribe(roothashApi.RuntimeMessageRegistry, app)
md.Subscribe(governanceApi.MessageChangeParameters, app)
md.Subscribe(governanceApi.MessageValidateParameterChanges, app)
}
func (app *registryApplication) OnCleanup() {
}
func (app *registryApplication) BeginBlock(ctx *api.Context, request types.RequestBeginBlock) error {
// XXX: With PR#1889 this can be a differnet interval.
if changed, registryEpoch := app.state.EpochChanged(ctx); changed {
return app.onRegistryEpochChanged(ctx, registryEpoch)
}
return nil
}
func (app *registryApplication) ExecuteMessage(ctx *api.Context, kind, msg interface{}) (interface{}, error) {
switch kind {
case roothashApi.RuntimeMessageRegistry:
m := msg.(*message.RegistryMessage)
switch {
case m.UpdateRuntime != nil:
state := registryState.NewMutableState(ctx.State())
return app.registerRuntime(ctx, state, m.UpdateRuntime)
default:
return nil, registry.ErrInvalidArgument
}
case governanceApi.MessageValidateParameterChanges:
// A change parameters proposal is about to be submitted. Validate changes.
return app.changeParameters(ctx, msg, false)
case governanceApi.MessageChangeParameters:
// A change parameters proposal has just been accepted and closed. Validate and apply
// changes.
return app.changeParameters(ctx, msg, true)
default:
return nil, registry.ErrInvalidArgument
}
}
func (app *registryApplication) ExecuteTx(ctx *api.Context, tx *transaction.Transaction) error {
state := registryState.NewMutableState(ctx.State())
ctx.SetPriority(AppPriority)
switch tx.Method {
case registry.MethodRegisterEntity:
var sigEnt entity.SignedEntity
if err := cbor.Unmarshal(tx.Body, &sigEnt); err != nil {
return err
}
return app.registerEntity(ctx, state, &sigEnt)
case registry.MethodDeregisterEntity:
return app.deregisterEntity(ctx, state)
case registry.MethodRegisterNode:
var sigNode node.MultiSignedNode
if err := cbor.Unmarshal(tx.Body, &sigNode); err != nil {
return err
}
ctx.SetPriority(AppPriority + 10000)
return app.registerNode(ctx, state, &sigNode)
case registry.MethodUnfreezeNode:
var unfreeze registry.UnfreezeNode
if err := cbor.Unmarshal(tx.Body, &unfreeze); err != nil {
return err
}
return app.unfreezeNode(ctx, state, &unfreeze)
case registry.MethodRegisterRuntime:
var rt registry.Runtime
if err := cbor.Unmarshal(tx.Body, &rt); err != nil {
return err
}
if _, err := app.registerRuntime(ctx, state, &rt); err != nil {
return err
}
return nil
case registry.MethodProveFreshness:
var blob [32]byte
if err := cbor.Unmarshal(tx.Body, &blob); err != nil {
ctx.Logger().Error("ExecuteTx: failed to unmarshal blob for freshness proof",
"err", err,
)
return registry.ErrInvalidArgument
}
if err := app.proveFreshness(ctx, state, blob); err != nil {
return err
}
return nil
default:
return registry.ErrInvalidArgument
}
}
func (app *registryApplication) EndBlock(ctx *api.Context, request types.RequestEndBlock) (types.ResponseEndBlock, error) {
return types.ResponseEndBlock{}, nil
}
func (app *registryApplication) onRegistryEpochChanged(ctx *api.Context, registryEpoch beacon.EpochTime) (err error) {
state := registryState.NewMutableState(ctx.State())
stakeState := stakingState.NewMutableState(ctx.State())
nodes, err := state.Nodes(ctx)
if err != nil {
ctx.Logger().Error("onRegistryEpochChanged: failed to get nodes",
"err", err,
)
return fmt.Errorf("registry: onRegistryEpochChanged: failed to get nodes: %w", err)
}
debondingInterval, err := stakeState.DebondingInterval(ctx)
if err != nil {
ctx.Logger().Error("onRegistryEpochChanged: failed to get debonding interval",
"err", err,
)
return fmt.Errorf("registry: onRegistryEpochChanged: failed to get debonding interval: %w", err)
}
params, err := state.ConsensusParameters(ctx)
if err != nil {
ctx.Logger().Error("onRegistryEpochChanged: failed to fetch consensus parameters",
"err", err,
)
return fmt.Errorf("registry: onRegistryEpochChanged: failed to fetch consensus parameters: %w", err)
}
var stakeAcc *stakingState.StakeAccumulatorCache
if !params.DebugBypassStake {
stakeAcc, err = stakingState.NewStakeAccumulatorCache(ctx)
if err != nil {
return fmt.Errorf("failed to create stake accumulator cache: %w", err)
}
}
// When a node expires, it is kept around for up to the debonding
// period and then removed. This is required so that expired nodes
// can still get slashed while inside the debonding interval as
// otherwise the nodes could not be resolved.
var expiredNodes []*node.Node
for _, node := range nodes {
if !node.IsExpired(uint64(registryEpoch)) {
continue
}
// Fetch node status to check whether we have already processed the
// node expiration (this is required so that we don't emit expiration
// events every epoch).
var status *registry.NodeStatus
status, err = state.NodeStatus(ctx, node.ID)
if err != nil {
return fmt.Errorf("registry: onRegistryEpochChanged: couldn't get node status: %w", err)
}
if !status.ExpirationProcessed {
expiredNodes = append(expiredNodes, node)
status.ExpirationProcessed = true
if err = state.SetNodeStatus(ctx, node.ID, status); err != nil {
return fmt.Errorf("registry: onRegistryEpochChanged: couldn't set node status: %w", err)
}
}
// If node has been expired for the debonding interval, finally remove it.
if math.MaxUint64-node.Expiration < uint64(debondingInterval) {
// Overflow, the node will never be removed.
continue
}
if beacon.EpochTime(node.Expiration)+debondingInterval < registryEpoch {
ctx.Logger().Debug("removing expired node",
"node_id", node.ID,
)
if err = state.RemoveNode(ctx, node); err != nil {
return fmt.Errorf("registry: onRegistryEpochChanged: couldn't remove node: %w", err)
}
// Remove the stake claim for the given node.
if !params.DebugBypassStake {
acctAddr := staking.NewAddress(node.EntityID)
if err = stakeAcc.RemoveStakeClaim(acctAddr, registry.StakeClaimForNode(node.ID)); err != nil {
return fmt.Errorf("registry: onRegistryEpochChanged: couldn't remove stake claim: %w", err)
}
}
}
}
if !params.DebugBypassStake {
if err = stakeAcc.Commit(); err != nil {
return fmt.Errorf("registry: onRegistryEpochChanged: failed to commit stake accumulator: %w", err)
}
}
// Emit the expired node event for all expired nodes.
for _, expiredNode := range expiredNodes {
ctx.EmitEvent(api.NewEventBuilder(app.Name()).TypedAttribute(®istry.NodeEvent{Node: expiredNode, IsRegistration: false}))
}
// Emit the node list epoch event.
ctx.EmitEvent(api.NewEventBuilder(app.Name()).TypedAttribute(®istry.NodeListEpochEvent{}))
return nil
}
// New constructs a new registry application instance.
func New() api.Application {
return ®istryApplication{}
}