-
Notifications
You must be signed in to change notification settings - Fork 151
Expand file tree
/
Copy pathtransactions.go
More file actions
387 lines (334 loc) · 11 KB
/
Copy pathtransactions.go
File metadata and controls
387 lines (334 loc) · 11 KB
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
package roothash
import (
"fmt"
"github.com/oasisprotocol/oasis-core/go/common"
"github.com/oasisprotocol/oasis-core/go/common/crypto/signature"
abciAPI "github.com/oasisprotocol/oasis-core/go/consensus/cometbft/api"
registryState "github.com/oasisprotocol/oasis-core/go/consensus/cometbft/apps/registry/state"
"github.com/oasisprotocol/oasis-core/go/consensus/cometbft/apps/roothash/api"
roothashState "github.com/oasisprotocol/oasis-core/go/consensus/cometbft/apps/roothash/state"
stakingState "github.com/oasisprotocol/oasis-core/go/consensus/cometbft/apps/staking/state"
roothash "github.com/oasisprotocol/oasis-core/go/roothash/api"
"github.com/oasisprotocol/oasis-core/go/roothash/api/commitment"
"github.com/oasisprotocol/oasis-core/go/roothash/api/message"
staking "github.com/oasisprotocol/oasis-core/go/staking/api"
)
// getRuntimeState fetches the current runtime state and performs common
// processing and error handling.
func (app *Application) getRuntimeState(
ctx *abciAPI.Context,
state *roothashState.MutableState,
id common.Namespace,
) (*roothash.RuntimeState, error) {
// Fetch current runtime state.
rtState, err := state.RuntimeState(ctx, id)
if err != nil {
return nil, fmt.Errorf("roothash: failed to fetch runtime state: %w", err)
}
if rtState.Suspended {
return nil, roothash.ErrRuntimeSuspended
}
if rtState.Committee == nil {
return nil, roothash.ErrNoCommittee
}
if rtState.CommitmentPool == nil {
return nil, roothash.ErrNoExecutorPool
}
return rtState, nil
}
func (app *Application) executorCommit(
ctx *abciAPI.Context,
state *roothashState.MutableState,
cc *roothash.ExecutorCommit,
) (err error) {
if ctx.IsCheckOnly() {
// Notify subscribers about observed commitments.
for _, ec := range cc.Commits {
app.ecn.DeliverExecutorCommitment(cc.ID, &ec)
}
return nil
}
// Charge gas for this transaction.
params, err := state.ConsensusParameters(ctx)
if err != nil {
ctx.Logger().Error("ComputeCommit: failed to fetch consensus parameters",
"err", err,
)
return err
}
if err = ctx.Gas().UseGas(1, roothash.GasOpComputeCommit, params.GasCosts); err != nil {
return err
}
// Return early if there are no commitments.
if len(cc.Commits) == 0 {
return nil
}
// Fetch the latest runtime state.
rtState, err := app.getRuntimeState(ctx, state, cc.ID)
if err != nil {
return err
}
prevRank := rtState.CommitmentPool.HighestRank
// Node lookup needed for RAK-attestation.
nl := registryState.NewMutableState(ctx.State())
// Account for gas consumed by messages.
msgGasAccountant := func(msgs []message.Message) error {
// Deliver messages in the simulation context to estimate gas.
msgCtx := ctx.WithSimulation()
defer msgCtx.Close()
_, msgErr := app.processRuntimeMessages(msgCtx, rtState, msgs)
return msgErr
}
// Verify and add commitments to the pool.
for _, commit := range cc.Commits {
if err = commitment.VerifyExecutorCommitment(ctx, rtState.LastBlock, rtState.Runtime, rtState.Committee.ValidFor, &commit, msgGasAccountant, nl); err != nil { // nolint: gosec
ctx.Logger().Debug("failed to verify executor commitment",
"err", err,
"runtime_id", cc.ID,
"round", commit.Header.Header.Round,
)
return err
}
if err := rtState.CommitmentPool.AddVerifiedExecutorCommitment(rtState.Committee, &commit); err != nil { // nolint: gosec
ctx.Logger().Debug("failed to add executor commitment",
"err", err,
"runtime_id", cc.ID,
"round", commit.Header.Header.Round,
)
return err
}
ctx.Logger().Debug("executor commitment added to pool",
"runtime_id", cc.ID,
"round", commit.Header.Header.Round,
"node_id", commit.NodeID,
"scheduler_id", commit.Header.SchedulerID,
"hash", commit.Header.Header.EncodedHash(),
"failure", commit.IsIndicatingFailure(),
)
}
// Return early for simulation as we only need gas accounting.
if ctx.IsSimulation() {
return nil
}
// Commit changes made to the pool.
ctx = ctx.NewTransaction()
defer ctx.Close()
state = roothashState.NewMutableState(ctx.State())
// Check if higher-ranked scheduler submitted a commitment.
if prevRank != rtState.CommitmentPool.HighestRank {
round := rtState.LastBlock.Header.Round + 1
ctx.Logger().Debug("transaction scheduler has changed",
"runtime_id", cc.ID,
"round", round,
"prev_rank", prevRank,
"new_rank", rtState.CommitmentPool.HighestRank,
)
// Re-arm round timeout. Give workers enough time to submit commitments.
prevTimeout := rtState.NextTimeout
rtState.NextTimeout = ctx.CurrentHeight() + rtState.Runtime.Executor.RoundTimeout
if err := rearmRoundTimeout(ctx, cc.ID, round, prevTimeout, rtState.NextTimeout); err != nil {
return err
}
}
// Update runtime state.
if err := state.SetRuntimeState(ctx, rtState); err != nil {
return fmt.Errorf("failed to set runtime state: %w", err)
}
// Emit events for all accepted commits.
for _, commit := range cc.Commits {
ctx.EmitEvent(
abciAPI.NewEventBuilder(app.Name()).
TypedAttribute(&roothash.ExecutorCommittedEvent{Commit: commit}).
TypedAttribute(&roothash.RuntimeIDAttribute{ID: cc.ID}),
)
}
ctx.Commit()
// Try to finalize the runtime during the end block.
api.RegisterRuntimeForFinalization(ctx, cc.ID)
return nil
}
func (app *Application) submitEvidence(
ctx *abciAPI.Context,
state *roothashState.MutableState,
evidence *roothash.Evidence,
) error {
// Validate proposal content basics.
if err := evidence.ValidateBasic(); err != nil {
ctx.Logger().Debug("Evidence: submitted evidence not valid",
"evidence", evidence,
"err", err,
)
return fmt.Errorf("%w: %v", roothash.ErrInvalidEvidence, err)
}
if ctx.IsCheckOnly() {
return nil
}
// Charge gas for this transaction.
params, err := state.ConsensusParameters(ctx)
if err != nil {
ctx.Logger().Error("Evidence: failed to fetch consensus parameters",
"err", err,
)
return err
}
if err = ctx.Gas().UseGas(1, roothash.GasOpEvidence, params.GasCosts); err != nil {
return err
}
// Return early for simulation as we only need gas accounting.
if ctx.IsSimulation() {
return nil
}
rtState, err := app.getRuntimeState(ctx, state, evidence.ID)
if err != nil {
return err
}
if len(rtState.Runtime.Staking.Slashing) == 0 {
// No slashing instructions for runtime, no point in collecting evidence.
ctx.Logger().Debug("Evidence: runtime has no slashing instructions",
"err", roothash.ErrRuntimeDoesNotSlash,
)
return roothash.ErrRuntimeDoesNotSlash
}
slash := rtState.Runtime.Staking.Slashing[staking.SlashRuntimeEquivocation].Amount
if slash.IsZero() {
// Slash amount is zero for runtime, no point in collecting evidence.
ctx.Logger().Debug("Evidence: runtime has no slashing instructions for equivocation",
"err", roothash.ErrRuntimeDoesNotSlash,
)
return roothash.ErrRuntimeDoesNotSlash
}
// Ensure evidence is not expired.
var round uint64
var pk signature.PublicKey
switch {
case evidence.EquivocationExecutor != nil:
commitA := evidence.EquivocationExecutor.CommitA
if commitA.Header.Header.Round+params.MaxEvidenceAge < rtState.LastBlock.Header.Round {
ctx.Logger().Debug("Evidence: commitment equivocation evidence expired",
"evidence", evidence.EquivocationExecutor,
"current_round", rtState.LastBlock.Header.Round,
"max_evidence_age", params.MaxEvidenceAge,
)
return fmt.Errorf("%w: equivocation evidence expired", roothash.ErrInvalidEvidence)
}
round = commitA.Header.Header.Round
pk = commitA.NodeID
case evidence.EquivocationProposal != nil:
proposalA := evidence.EquivocationProposal.ProposalA
if proposalA.Header.Round+params.MaxEvidenceAge < rtState.LastBlock.Header.Round {
ctx.Logger().Debug("Evidence: proposal equivocation evidence expired",
"evidence", evidence.EquivocationExecutor,
"current_round", rtState.LastBlock.Header.Round,
"max_evidence_age", params.MaxEvidenceAge,
)
return fmt.Errorf("%w: equivocation evidence expired", roothash.ErrInvalidEvidence)
}
round = proposalA.Header.Round
pk = proposalA.NodeID
default:
// This should never happen due to ValidateBasic check above.
return roothash.ErrInvalidEvidence
}
// Evidence is valid. Store the evidence and slash the node.
evHash, err := evidence.Hash()
if err != nil {
return fmt.Errorf("error computing evidence hash: %w", err)
}
b, err := state.ImmutableState.EvidenceHashExists(ctx, rtState.Runtime.ID, round, evHash)
if err != nil {
return fmt.Errorf("error querying evidence hash: %w", err)
}
if b {
return roothash.ErrDuplicateEvidence
}
if err = state.SetEvidenceHash(ctx, rtState.Runtime.ID, round, evHash); err != nil {
return err
}
if err = onEvidenceRuntimeEquivocation(
ctx,
pk,
rtState.Runtime,
&slash,
); err != nil {
return fmt.Errorf("error slashing runtime node: %w", err)
}
return nil
}
func (app *Application) submitMsg(
ctx *abciAPI.Context,
state *roothashState.MutableState,
msg *roothash.SubmitMsg,
) error {
if ctx.IsCheckOnly() {
return nil
}
// Charge gas for this transaction.
params, err := state.ConsensusParameters(ctx)
if err != nil {
ctx.Logger().Error("failed to fetch consensus parameters",
"err", err,
)
return err
}
if err = ctx.Gas().UseGas(1, roothash.GasOpSubmitMsg, params.GasCosts); err != nil {
return err
}
// Return early for simulation as we only need gas accounting.
if ctx.IsSimulation() {
return nil
}
rtState, err := app.getRuntimeState(ctx, state, msg.ID)
if err != nil {
return err
}
// If the maximum size of the queue is set to zero, bail early.
if rtState.Runtime.TxnScheduler.MaxInMessages == 0 {
return roothash.ErrIncomingMessageQueueFull
}
// If the submitted fee is smaller than the minimum fee, bail early.
if msg.Fee.Cmp(&rtState.Runtime.Staking.MinInMessageFee) < 0 {
return roothash.ErrIncomingMessageInsufficientFee
}
// Create a new transaction context and rollback in case we fail.
ctx = ctx.NewTransaction()
defer ctx.Close()
// Transfer the given amount (fee + tokens) into the runtime account.
totalAmount := msg.Fee.Clone()
if err = totalAmount.Add(&msg.Tokens); err != nil {
return err
}
st := stakingState.NewMutableState(ctx.State())
rtAddress := staking.NewRuntimeAddress(rtState.Runtime.ID)
if err = st.Transfer(ctx, ctx.CallerAddress(), rtAddress, totalAmount); err != nil {
return err
}
// Fetch current incoming queue metadata.
meta, err := state.IncomingMessageQueueMeta(ctx, rtState.Runtime.ID)
if err != nil {
return err
}
// Check if the queue is already full.
if meta.Size >= rtState.Runtime.TxnScheduler.MaxInMessages {
return roothash.ErrIncomingMessageQueueFull
}
// Queue message.
inMsg := &message.IncomingMessage{
ID: meta.NextSequenceNumber,
Caller: ctx.CallerAddress(),
Tag: msg.Tag,
Fee: msg.Fee,
Tokens: msg.Tokens,
Data: msg.Data,
}
if err = state.SetIncomingMessageInQueue(ctx, rtState.Runtime.ID, inMsg); err != nil {
return err
}
// Update next sequence number.
meta.Size++
meta.NextSequenceNumber++
if err = state.SetIncomingMessageQueueMeta(ctx, rtState.Runtime.ID, meta); err != nil {
return err
}
ctx.Commit()
return nil
}