-
Notifications
You must be signed in to change notification settings - Fork 107
/
registry.go
222 lines (185 loc) · 6.98 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
// Package registry implements the registry application.
package registry
import (
"fmt"
"math"
"github.com/tendermint/tendermint/abci/types"
"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"
registryState "github.com/oasisprotocol/oasis-core/go/consensus/tendermint/apps/registry/state"
stakingapp "github.com/oasisprotocol/oasis-core/go/consensus/tendermint/apps/staking"
stakingState "github.com/oasisprotocol/oasis-core/go/consensus/tendermint/apps/staking/state"
epochtime "github.com/oasisprotocol/oasis-core/go/epochtime/api"
registry "github.com/oasisprotocol/oasis-core/go/registry/api"
staking "github.com/oasisprotocol/oasis-core/go/staking/api"
)
var _ api.Application = (*registryApplication)(nil)
type registryApplication struct {
state api.ApplicationState
}
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) {
app.state = state
}
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) ExecuteTx(ctx *api.Context, tx *transaction.Transaction) error {
state := registryState.NewMutableState(ctx.State())
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
}
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 sigRt registry.SignedRuntime
if err := cbor.Unmarshal(tx.Body, &sigRt); err != nil {
return err
}
return app.registerRuntime(ctx, state, &sigRt)
default:
return registry.ErrInvalidArgument
}
}
func (app *registryApplication) ForeignExecuteTx(ctx *api.Context, other api.Application, tx *transaction.Transaction) error {
return nil
}
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 epochtime.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 epochtime.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 RegistryNodeListEpoch notification event.
evb := api.NewEventBuilder(app.Name())
// (Dummy value, should be ignored.)
evb = evb.Attribute(KeyRegistryNodeListEpoch, []byte("1"))
if len(expiredNodes) > 0 {
// Iff any nodes have expired, force-emit the NodesExpired event
// so the change is picked up.
evb = evb.Attribute(KeyNodesExpired, cbor.Marshal(expiredNodes))
}
ctx.EmitEvent(evb)
return nil
}
// New constructs a new registry application instance.
func New() api.Application {
return ®istryApplication{}
}