-
Notifications
You must be signed in to change notification settings - Fork 3.7k
/
stf.go
683 lines (616 loc) · 20.1 KB
/
stf.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
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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
package stf
import (
"context"
"encoding/json"
"errors"
"fmt"
"strings"
appmodulev2 "cosmossdk.io/core/appmodule/v2"
corecontext "cosmossdk.io/core/context"
"cosmossdk.io/core/event"
"cosmossdk.io/core/gas"
"cosmossdk.io/core/header"
"cosmossdk.io/core/log"
"cosmossdk.io/core/router"
"cosmossdk.io/core/server"
"cosmossdk.io/core/store"
"cosmossdk.io/core/transaction"
"cosmossdk.io/schema/appdata"
stfgas "cosmossdk.io/server/v2/stf/gas"
"cosmossdk.io/server/v2/stf/internal"
)
type eContextKey struct{}
var executionContextKey = eContextKey{}
// STF is a struct that manages the state transition component of the app.
type STF[T transaction.Tx] struct {
logger log.Logger
msgRouter coreRouterImpl
queryRouter coreRouterImpl
doPreBlock func(ctx context.Context, txs []T) error
doBeginBlock func(ctx context.Context) error
doEndBlock func(ctx context.Context) error
doValidatorUpdate func(ctx context.Context) ([]appmodulev2.ValidatorUpdate, error)
doTxValidation func(ctx context.Context, tx T) error
postTxExec func(ctx context.Context, tx T, success bool) error
branchFn branchFn // branchFn is a function that given a readonly state it returns a writable version of it.
makeGasMeter makeGasMeterFn
makeGasMeteredState makeGasMeteredStateFn
}
// New returns a new STF instance.
func New[T transaction.Tx](
logger log.Logger,
msgRouterBuilder *MsgRouterBuilder,
queryRouterBuilder *MsgRouterBuilder,
doPreBlock func(ctx context.Context, txs []T) error,
doBeginBlock func(ctx context.Context) error,
doEndBlock func(ctx context.Context) error,
doTxValidation func(ctx context.Context, tx T) error,
doValidatorUpdate func(ctx context.Context) ([]appmodulev2.ValidatorUpdate, error),
postTxExec func(ctx context.Context, tx T, success bool) error,
branch func(store store.ReaderMap) store.WriterMap,
) (*STF[T], error) {
msgRouter, err := msgRouterBuilder.build()
if err != nil {
return nil, fmt.Errorf("build msg router: %w", err)
}
queryRouter, err := queryRouterBuilder.build()
if err != nil {
return nil, fmt.Errorf("build query router: %w", err)
}
return &STF[T]{
logger: logger,
msgRouter: msgRouter,
queryRouter: queryRouter,
doPreBlock: doPreBlock,
doBeginBlock: doBeginBlock,
doEndBlock: doEndBlock,
doValidatorUpdate: doValidatorUpdate,
doTxValidation: doTxValidation,
postTxExec: postTxExec, // TODO
branchFn: branch,
makeGasMeter: stfgas.DefaultGasMeter,
makeGasMeteredState: stfgas.DefaultWrapWithGasMeter,
}, nil
}
// DeliverBlock is our state transition function.
// It takes a read only view of the state to apply the block to,
// executes the block and returns the block results and the new state.
func (s STF[T]) DeliverBlock(
ctx context.Context,
block *server.BlockRequest[T],
state store.ReaderMap,
) (blockResult *server.BlockResponse, newState store.WriterMap, err error) {
// creates a new branchFn state, from the readonly view of the state
// that can be written to.
newState = s.branchFn(state)
hi := header.Info{
Hash: block.Hash,
AppHash: block.AppHash,
ChainID: block.ChainId,
Time: block.Time,
Height: int64(block.Height),
}
// set header info
err = s.setHeaderInfo(newState, hi)
if err != nil {
return nil, nil, fmt.Errorf("unable to set initial header info, %w", err)
}
exCtx := s.makeContext(ctx, ConsensusIdentity, newState, internal.ExecModeFinalize)
exCtx.setHeaderInfo(hi)
// reset events
exCtx.events = make([]event.Event, 0)
// pre block is called separate from begin block in order to prepopulate state
preBlockEvents, err := s.preBlock(exCtx, block.Txs)
if err != nil {
return nil, nil, err
}
if err = isCtxCancelled(ctx); err != nil {
return nil, nil, err
}
// reset events
exCtx.events = make([]event.Event, 0)
// begin block
var beginBlockEvents []event.Event
if !block.IsGenesis {
// begin block
beginBlockEvents, err = s.beginBlock(exCtx)
if err != nil {
return nil, nil, err
}
}
// check if we need to return early
if err = isCtxCancelled(ctx); err != nil {
return nil, nil, err
}
// execute txs
txResults := make([]server.TxResult, len(block.Txs))
// TODO: skip first tx if vote extensions are enabled (marko)
for i, txBytes := range block.Txs {
// check if we need to return early or continue delivering txs
if err = isCtxCancelled(ctx); err != nil {
return nil, nil, err
}
txResults[i] = s.deliverTx(exCtx, newState, txBytes, transaction.ExecModeFinalize, hi, int32(i+1))
}
// reset events
exCtx.events = make([]event.Event, 0)
// end block
endBlockEvents, valset, err := s.endBlock(exCtx)
if err != nil {
return nil, nil, err
}
return &server.BlockResponse{
ValidatorUpdates: valset,
PreBlockEvents: preBlockEvents,
BeginBlockEvents: beginBlockEvents,
TxResults: txResults,
EndBlockEvents: endBlockEvents,
}, newState, nil
}
// deliverTx executes a TX and returns the result.
func (s STF[T]) deliverTx(
ctx context.Context,
state store.WriterMap,
tx T,
execMode transaction.ExecMode,
hi header.Info,
txIndex int32,
) server.TxResult {
// recover in the case of a panic
var recoveryError error
defer func() {
if r := recover(); r != nil {
recoveryError = fmt.Errorf("panic during transaction execution: %s", r)
s.logger.Error("panic during transaction execution", "error", recoveryError)
}
}()
// handle error from GetGasLimit
gasLimit, gasLimitErr := tx.GetGasLimit()
if gasLimitErr != nil {
return server.TxResult{
Error: gasLimitErr,
}
}
if recoveryError != nil {
return server.TxResult{
Error: recoveryError,
}
}
validateGas, validationEvents, err := s.validateTx(ctx, state, gasLimit, tx, execMode)
if err != nil {
return server.TxResult{
Error: err,
}
}
events := make([]event.Event, 0)
// set the event indexes, set MsgIndex to 0 in validation events
for i, e := range validationEvents {
e.BlockNumber = uint64(hi.Height)
e.BlockStage = appdata.TxProcessingStage
e.TxIndex = txIndex
e.MsgIndex = 0
e.EventIndex = int32(i + 1)
events = append(events, e)
}
execResp, execGas, execEvents, err := s.execTx(ctx, state, gasLimit-validateGas, tx, execMode, hi)
// set the TxIndex in the exec events
for _, e := range execEvents {
e.BlockNumber = uint64(hi.Height)
e.BlockStage = appdata.TxProcessingStage
e.TxIndex = txIndex
events = append(events, e)
}
return server.TxResult{
Events: events,
GasUsed: execGas + validateGas,
GasWanted: gasLimit,
Resp: execResp,
Error: err,
}
}
// validateTx validates a transaction given the provided WritableState and gas limit.
// If the validation is successful, state is committed
func (s STF[T]) validateTx(
ctx context.Context,
state store.WriterMap,
gasLimit uint64,
tx T,
execMode transaction.ExecMode,
) (gasUsed uint64, events []event.Event, err error) {
validateState := s.branchFn(state)
hi, err := s.getHeaderInfo(validateState)
if err != nil {
return 0, nil, err
}
validateCtx := s.makeContext(ctx, RuntimeIdentity, validateState, execMode)
validateCtx.setHeaderInfo(hi)
validateCtx.setGasLimit(gasLimit)
err = s.doTxValidation(validateCtx, tx)
if err != nil {
return 0, nil, err
}
consumed := validateCtx.meter.Limit() - validateCtx.meter.Remaining()
return consumed, validateCtx.events, applyStateChanges(state, validateState)
}
// execTx executes the tx messages on the provided state. If the tx fails then the state is discarded.
func (s STF[T]) execTx(
ctx context.Context,
state store.WriterMap,
gasLimit uint64,
tx T,
execMode transaction.ExecMode,
hi header.Info,
) ([]transaction.Msg, uint64, []event.Event, error) {
execState := s.branchFn(state)
msgsResp, gasUsed, runTxMsgsEvents, txErr := s.runTxMsgs(ctx, execState, gasLimit, tx, execMode, hi)
if txErr != nil {
// in case of error during message execution, we do not apply the exec state.
// instead we run the post exec handler in a new branchFn from the initial state.
postTxState := s.branchFn(state)
postTxCtx := s.makeContext(ctx, RuntimeIdentity, postTxState, execMode)
postTxCtx.setHeaderInfo(hi)
postTxErr := s.postTxExec(postTxCtx, tx, false)
if postTxErr != nil {
// if the post tx handler fails, then we do not apply any state change to the initial state.
// we just return the exec gas used and a joined error from TX error and post TX error.
return nil, gasUsed, nil, errors.Join(txErr, postTxErr)
}
// in case post tx is successful, then we commit the post tx state to the initial state,
// and we return post tx events alongside exec gas used and the error of the tx.
applyErr := applyStateChanges(state, postTxState)
if applyErr != nil {
return nil, 0, nil, applyErr
}
// set the event indexes, set MsgIndex to -1 in post tx events
for i := range postTxCtx.events {
postTxCtx.events[i].EventIndex = int32(i + 1)
postTxCtx.events[i].MsgIndex = -1
}
return nil, gasUsed, postTxCtx.events, txErr
}
// tx execution went fine, now we use the same state to run the post tx exec handler,
// in case the execution of the post tx fails, then no state change is applied and the
// whole execution step is rolled back.
postTxCtx := s.makeContext(ctx, RuntimeIdentity, execState, execMode) // NO gas limit.
postTxCtx.setHeaderInfo(hi)
postTxErr := s.postTxExec(postTxCtx, tx, true)
if postTxErr != nil {
// if post tx fails, then we do not apply any state change, we return the post tx error,
// alongside the gas used.
return nil, gasUsed, nil, postTxErr
}
// both the execution and post tx execution step were successful, so we apply the state changes
// to the provided state, and we return responses, and events from exec tx and post tx exec.
applyErr := applyStateChanges(state, execState)
if applyErr != nil {
return nil, 0, nil, applyErr
}
// set the event indexes, set MsgIndex to -1 in post tx events
for i := range postTxCtx.events {
postTxCtx.events[i].EventIndex = int32(i + 1)
postTxCtx.events[i].MsgIndex = -1
}
return msgsResp, gasUsed, append(runTxMsgsEvents, postTxCtx.events...), nil
}
// runTxMsgs will execute the messages contained in the TX with the provided state.
func (s STF[T]) runTxMsgs(
ctx context.Context,
state store.WriterMap,
gasLimit uint64,
tx T,
execMode transaction.ExecMode,
hi header.Info,
) ([]transaction.Msg, uint64, []event.Event, error) {
txSenders, err := tx.GetSenders()
if err != nil {
return nil, 0, nil, err
}
msgs, err := tx.GetMessages()
if err != nil {
return nil, 0, nil, err
}
msgResps := make([]transaction.Msg, len(msgs))
execCtx := s.makeContext(ctx, RuntimeIdentity, state, execMode)
execCtx.setHeaderInfo(hi)
execCtx.setGasLimit(gasLimit)
events := make([]event.Event, 0)
for i, msg := range msgs {
execCtx.sender = txSenders[i]
execCtx.events = make([]event.Event, 0) // reset events
resp, err := s.msgRouter.Invoke(execCtx, msg)
if err != nil {
return nil, 0, nil, err // do not wrap the error or we lose the original error type
}
msgResps[i] = resp
for j, e := range execCtx.events {
e.MsgIndex = int32(i + 1)
e.EventIndex = int32(j + 1)
events = append(events, e)
}
// add message event
events = append(events, createMessageEvent(msg, int32(i+1), int32(len(execCtx.events)+1)))
}
consumed := execCtx.meter.Limit() - execCtx.meter.Remaining()
return msgResps, consumed, events, nil
}
// Create a message event, with two kv: action, the type url of the message
// and module, the module of the message.
func createMessageEvent(msg transaction.Msg, msgIndex, eventIndex int32) event.Event {
// Assumes that module name is the second element of the msg type URL
// e.g. "cosmos.bank.v1beta1.MsgSend" => "bank"
// It returns an empty string if the input is not a valid type URL
getModuleNameFromTypeURL := func(input string) string {
moduleName := strings.Split(input, ".")
if len(moduleName) > 1 {
return moduleName[1]
}
return ""
}
return event.Event{
MsgIndex: msgIndex,
EventIndex: eventIndex,
Type: "message",
Attributes: func() ([]appdata.EventAttribute, error) {
typeURL := msgTypeURL(msg)
return []appdata.EventAttribute{
{Key: "action", Value: "/" + typeURL},
{Key: "module", Value: getModuleNameFromTypeURL(typeURL)},
}, nil
},
Data: func() (json.RawMessage, error) {
typeURL := msgTypeURL(msg)
attrs := []appdata.EventAttribute{
{Key: "action", Value: "/" + typeURL},
{Key: "module", Value: getModuleNameFromTypeURL(typeURL)},
}
return json.Marshal(attrs)
},
}
}
// preBlock executes the pre block logic.
func (s STF[T]) preBlock(
ctx *executionContext,
txs []T,
) ([]event.Event, error) {
err := s.doPreBlock(ctx, txs)
if err != nil {
return nil, err
}
for i := range ctx.events {
ctx.events[i].BlockNumber = uint64(ctx.headerInfo.Height)
ctx.events[i].BlockStage = appdata.PreBlockStage
ctx.events[i].EventIndex = int32(i + 1)
}
return ctx.events, nil
}
// beginBlock executes the begin block logic.
func (s STF[T]) beginBlock(
ctx *executionContext,
) (beginBlockEvents []event.Event, err error) {
err = s.doBeginBlock(ctx)
if err != nil {
return nil, err
}
for i := range ctx.events {
ctx.events[i].BlockNumber = uint64(ctx.headerInfo.Height)
ctx.events[i].BlockStage = appdata.BeginBlockStage
ctx.events[i].EventIndex = int32(i + 1)
}
return ctx.events, nil
}
// endBlock executes the end block logic.
func (s STF[T]) endBlock(
ctx *executionContext,
) ([]event.Event, []appmodulev2.ValidatorUpdate, error) {
err := s.doEndBlock(ctx)
if err != nil {
return nil, nil, err
}
events := ctx.events
ctx.events = make([]event.Event, 0) // reset events
valsetUpdates, err := s.validatorUpdates(ctx)
if err != nil {
return nil, nil, err
}
events = append(events, ctx.events...)
for i := range events {
events[i].BlockNumber = uint64(ctx.headerInfo.Height)
events[i].BlockStage = appdata.EndBlockStage
events[i].EventIndex = int32(i + 1)
}
return events, valsetUpdates, nil
}
// validatorUpdates returns the validator updates for the current block. It is called by endBlock after the endblock execution has concluded
func (s STF[T]) validatorUpdates(
ctx *executionContext,
) ([]appmodulev2.ValidatorUpdate, error) {
valSetUpdates, err := s.doValidatorUpdate(ctx)
if err != nil {
return nil, err
}
return valSetUpdates, nil
}
// Simulate simulates the execution of a tx on the provided state.
func (s STF[T]) Simulate(
ctx context.Context,
state store.ReaderMap,
gasLimit uint64,
tx T,
) (server.TxResult, store.WriterMap) {
simulationState := s.branchFn(state)
hi, err := s.getHeaderInfo(simulationState)
if err != nil {
return server.TxResult{}, nil
}
txr := s.deliverTx(ctx, simulationState, tx, internal.ExecModeSimulate, hi, 0)
return txr, simulationState
}
// ValidateTx will run only the validation steps required for a transaction.
// Validations are run over the provided state, with the provided gas limit.
func (s STF[T]) ValidateTx(
ctx context.Context,
state store.ReaderMap,
gasLimit uint64,
tx T,
) server.TxResult {
validationState := s.branchFn(state)
gasUsed, events, err := s.validateTx(ctx, validationState, gasLimit, tx, transaction.ExecModeCheck)
return server.TxResult{
Events: events,
GasUsed: gasUsed,
Error: err,
}
}
// Query executes the query on the provided state with the provided gas limits.
func (s STF[T]) Query(
ctx context.Context,
state store.ReaderMap,
gasLimit uint64,
req transaction.Msg,
) (transaction.Msg, error) {
queryState := s.branchFn(state)
hi, err := s.getHeaderInfo(queryState)
if err != nil {
return nil, err
}
queryCtx := s.makeContext(ctx, nil, queryState, internal.ExecModeSimulate)
queryCtx.setHeaderInfo(hi)
queryCtx.setGasLimit(gasLimit)
return s.queryRouter.Invoke(queryCtx, req)
}
// clone clones STF.
func (s STF[T]) clone() STF[T] {
return STF[T]{
logger: s.logger,
msgRouter: s.msgRouter,
queryRouter: s.queryRouter,
doPreBlock: s.doPreBlock,
doBeginBlock: s.doBeginBlock,
doEndBlock: s.doEndBlock,
doValidatorUpdate: s.doValidatorUpdate,
doTxValidation: s.doTxValidation,
postTxExec: s.postTxExec,
branchFn: s.branchFn,
makeGasMeter: s.makeGasMeter,
makeGasMeteredState: s.makeGasMeteredState,
}
}
// executionContext is a struct that holds the context for the execution of a tx.
type executionContext struct {
context.Context
// unmeteredState is storage without metering. Changes here are propagated to state which is the metered
// version.
unmeteredState store.WriterMap
// state is the gas metered state.
state store.WriterMap
// meter is the gas meter.
meter gas.Meter
// events are the current events.
events []event.Event
// sender is the causer of the state transition.
sender transaction.Identity
// headerInfo contains the block info.
headerInfo header.Info
// execMode retains information about the exec mode.
execMode transaction.ExecMode
branchFn branchFn
makeGasMeter makeGasMeterFn
makeGasMeteredStore makeGasMeteredStateFn
msgRouter router.Service
queryRouter router.Service
}
// setHeaderInfo sets the header info in the state to be used by queries in the future.
func (e *executionContext) setHeaderInfo(hi header.Info) {
e.headerInfo = hi
}
// setGasLimit will update the gas limit of the *executionContext
func (e *executionContext) setGasLimit(limit uint64) {
meter := e.makeGasMeter(limit)
meteredState := e.makeGasMeteredStore(meter, e.unmeteredState)
e.meter = meter
e.state = meteredState
}
func (e *executionContext) Value(key any) any {
if key == executionContextKey {
return e
}
return e.Context.Value(key)
}
// TODO: too many calls to makeContext can be expensive
// makeContext creates and returns a new execution context for the STF[T] type.
// It takes in the following parameters:
// - ctx: The context.Context object for the execution.
// - sender: The transaction.Identity object representing the sender of the transaction.
// - state: The store.WriterMap object for accessing and modifying the state.
// - gasLimit: The maximum amount of gas allowed for the execution.
// - execMode: The corecontext.ExecMode object representing the execution mode.
//
// It returns a pointer to the executionContext struct
func (s STF[T]) makeContext(
ctx context.Context,
sender transaction.Identity,
store store.WriterMap,
execMode transaction.ExecMode,
) *executionContext {
valuedCtx := context.WithValue(ctx, corecontext.ExecModeKey, execMode)
return newExecutionContext(
valuedCtx,
s.makeGasMeter,
s.makeGasMeteredState,
s.branchFn,
sender,
store,
execMode,
s.msgRouter,
s.queryRouter,
)
}
func newExecutionContext(
ctx context.Context,
makeGasMeterFn makeGasMeterFn,
makeGasMeteredStoreFn makeGasMeteredStateFn,
branchFn branchFn,
sender transaction.Identity,
state store.WriterMap,
execMode transaction.ExecMode,
msgRouter coreRouterImpl,
queryRouter coreRouterImpl,
) *executionContext {
meter := makeGasMeterFn(gas.NoGasLimit)
meteredState := makeGasMeteredStoreFn(meter, state)
return &executionContext{
Context: ctx,
unmeteredState: state,
state: meteredState,
meter: meter,
events: make([]event.Event, 0),
headerInfo: header.Info{},
execMode: execMode,
sender: sender,
branchFn: branchFn,
makeGasMeter: makeGasMeterFn,
makeGasMeteredStore: makeGasMeteredStoreFn,
msgRouter: msgRouter,
queryRouter: queryRouter,
}
}
// applyStateChanges applies the state changes from the source store to the destination store.
// It retrieves the state changes from the source store using GetStateChanges method,
// and then applies those changes to the destination store using ApplyStateChanges method.
// If an error occurs during the retrieval or application of state changes, it is returned.
func applyStateChanges(dst, src store.WriterMap) error {
changes, err := src.GetStateChanges()
if err != nil {
return err
}
return dst.ApplyStateChanges(changes)
}
// isCtxCancelled reports if the context was canceled.
func isCtxCancelled(ctx context.Context) error {
select {
case <-ctx.Done():
return ctx.Err()
default:
return nil
}
}