-
Notifications
You must be signed in to change notification settings - Fork 3.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(stf): change router service to extract the router at runtime rather than build time #20724
Merged
Merged
Changes from 1 commit
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
93ec87c
port stf
ed0415f
apply comment from matt
a457284
godoc
0157f1c
Merge branch 'main' into tip/simappv2/stf
testinginprod 8c5ce3d
Merge branch 'main' into tip/simappv2/stf
testinginprod b92eb8c
fix stf
5c900c8
Merge branch 'main' into tip/stf/change_routerservice
testinginprod 8aac86f
lint gods
25f5294
Merge remote-tracking branch 'origin/tip/stf/change_routerservice' in…
b75456e
separate merge logic
abedc73
merge fix
a5444e9
lint
24bcce5
fix logger
de79a44
Merge branch 'main' into tip/stf/change_routerservice
testinginprod 5ce7a41
Merge remote-tracking branch 'origin/tip/stf/change_routerservice' in…
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
fix stf
- Loading branch information
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,7 @@ import ( | |
"cosmossdk.io/core/gas" | ||
"cosmossdk.io/core/header" | ||
"cosmossdk.io/core/log" | ||
"cosmossdk.io/core/router" | ||
"cosmossdk.io/core/store" | ||
"cosmossdk.io/core/transaction" | ||
stfgas "cosmossdk.io/server/v2/stf/gas" | ||
|
@@ -23,9 +24,10 @@ var Identity = []byte("stf") | |
|
||
// STF is a struct that manages the state transition component of the app. | ||
type STF[T transaction.Tx] struct { | ||
logger log.Logger | ||
handleMsg func(ctx context.Context, msg transaction.Msg) (transaction.Msg, error) | ||
handleQuery func(ctx context.Context, req transaction.Msg) (transaction.Msg, error) | ||
logger log.Logger | ||
|
||
msgRouter Router | ||
queryRouter Router | ||
|
||
doPreBlock func(ctx context.Context, txs []T) error | ||
doBeginBlock func(ctx context.Context) error | ||
|
@@ -42,29 +44,40 @@ type STF[T transaction.Tx] struct { | |
|
||
// NewSTF returns a new STF instance. | ||
func NewSTF[T transaction.Tx]( | ||
handleMsg func(ctx context.Context, msg transaction.Msg) (transaction.Msg, error), | ||
handleQuery func(ctx context.Context, req transaction.Msg) (transaction.Msg, error), | ||
msgRouterBuilder *MsgRouterBuilder, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit, can we add the logger here so it closes this as well: #20663 |
||
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] { | ||
) (*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]{ | ||
handleMsg: handleMsg, | ||
handleQuery: handleQuery, | ||
logger: nil, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. see above |
||
msgRouter: msgRouter, | ||
queryRouter: queryRouter, | ||
doPreBlock: doPreBlock, | ||
doBeginBlock: doBeginBlock, | ||
doEndBlock: doEndBlock, | ||
doTxValidation: doTxValidation, | ||
doValidatorUpdate: doValidatorUpdate, | ||
doTxValidation: doTxValidation, | ||
postTxExec: postTxExec, // TODO | ||
branchFn: branch, | ||
makeGasMeter: stfgas.DefaultGasMeter, | ||
makeGasMeteredState: stfgas.DefaultWrapWithGasMeter, | ||
} | ||
}, nil | ||
} | ||
|
||
// DeliverBlock is our state transition function. | ||
|
@@ -310,7 +323,7 @@ func (s STF[T]) runTxMsgs( | |
execCtx.setGasLimit(gasLimit) | ||
for i, msg := range msgs { | ||
execCtx.sender = txSenders[i] | ||
resp, err := s.handleMsg(execCtx, msg) | ||
resp, err := s.msgRouter.InvokeUntyped(execCtx, msg) | ||
if err != nil { | ||
return nil, 0, nil, fmt.Errorf("message execution at index %d failed: %w", i, err) | ||
} | ||
|
@@ -346,7 +359,7 @@ func (s STF[T]) runConsensusMessages( | |
) ([]transaction.Msg, error) { | ||
responses := make([]transaction.Msg, len(messages)) | ||
for i := range messages { | ||
resp, err := s.handleMsg(ctx, messages[i]) | ||
resp, err := s.msgRouter.InvokeUntyped(ctx, messages[i]) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
@@ -498,11 +511,7 @@ func (s STF[T]) Query( | |
queryCtx := s.makeContext(ctx, nil, queryState, internal.ExecModeSimulate) | ||
queryCtx.setHeaderInfo(hi) | ||
queryCtx.setGasLimit(gasLimit) | ||
return s.handleQuery(queryCtx, req) | ||
} | ||
|
||
func (s STF[T]) Message(ctx context.Context, msg transaction.Msg) (response transaction.Msg, err error) { | ||
return s.handleMsg(ctx, msg) | ||
return s.queryRouter.InvokeUntyped(queryCtx, req) | ||
} | ||
|
||
// RunWithCtx is made to support genesis, if genesis was just the execution of messages instead | ||
|
@@ -521,8 +530,9 @@ func (s STF[T]) RunWithCtx( | |
// clone clones STF. | ||
func (s STF[T]) clone() STF[T] { | ||
return STF[T]{ | ||
handleMsg: s.handleMsg, | ||
handleQuery: s.handleQuery, | ||
logger: s.logger, | ||
msgRouter: s.msgRouter, | ||
queryRouter: s.queryRouter, | ||
doPreBlock: s.doPreBlock, | ||
doBeginBlock: s.doBeginBlock, | ||
doEndBlock: s.doEndBlock, | ||
|
@@ -558,6 +568,9 @@ type executionContext struct { | |
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. | ||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Refactor suggestion for
msgRouterService
: Consider using the identity field in future implementations.The
identity
field in themsgRouterService
struct is currently unused. It's a good practice to avoid leaving unused code as it can lead to confusion and maintenance issues. If there are plans to use this field in the future, consider documenting the intended use more explicitly or implementing a placeholder functionality that can be expanded later.