-
Notifications
You must be signed in to change notification settings - Fork 3.7k
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: Optimistic Execution #16581
Merged
Merged
feat: Optimistic Execution #16581
Changes from 28 commits
Commits
Show all changes
39 commits
Select commit
Hold shift + click to select a range
a57c937
feat: Optimistic Execution
facundomedica 023256e
fix panic recovery
facundomedica 14b80c4
remove test changes
facundomedica 47b8a1c
Merge branch 'main' of https://github.com/cosmos/cosmos-sdk into facu/oe
facundomedica deaf6b7
fix test
facundomedica f30e4a7
make comet panic instead of sdk
facundomedica 573d107
add abort channel
facundomedica 17b5ca4
fix abort
facundomedica d371c16
clean up phase1
facundomedica c9dbc9a
merge
facundomedica 20f0325
testing testing
facundomedica 6aec99a
Merge branch 'main' of https://github.com/cosmos/cosmos-sdk into facu/oe
facundomedica e920201
merge main
facundomedica b855c1a
progress
facundomedica 265e32d
fix
facundomedica 2830366
Merge branch 'main' of https://github.com/cosmos/cosmos-sdk into facu/oe
facundomedica c835fa7
progress
facundomedica b26cfe8
Merge branch 'main' into facu/oe
facundomedica 06cb990
lint
facundomedica f2aec1d
progress
facundomedica 64988fa
Merge branch 'main' of https://github.com/cosmos/cosmos-sdk into facu/oe
facundomedica 18b666e
fix race condition
facundomedica 125e942
progress
facundomedica 0f1ad3b
progress
facundomedica 35ae374
Merge branch 'main' of https://github.com/cosmos/cosmos-sdk into facu/oe
facundomedica c798e17
progress
facundomedica 655dde4
merge main
facundomedica 74147f1
added mutext to mempools
facundomedica 0d45c3c
add test and do some refactor
facundomedica b008a8a
undo test changes
facundomedica 78b233d
fix
facundomedica 4f90f04
Update baseapp/abci.go
facundomedica 2b574d5
only start optimistic execution if processProposal resp is accepted
facundomedica f91b715
Merge branch 'main' into facu/oe
facundomedica 1c4743a
godoc + tests
facundomedica 8cda1f1
add file
facundomedica 0065196
cl++
facundomedica 9d6c8b1
cl++
facundomedica 8bdd23d
lint
facundomedica 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
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
facundomedica marked this conversation as resolved.
Show resolved
Hide resolved
|
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 |
---|---|---|
@@ -0,0 +1,174 @@ | ||
package oe | ||
|
||
import ( | ||
"bytes" | ||
"math/rand" | ||
"sync" | ||
"time" | ||
|
||
abci "github.com/cometbft/cometbft/abci/types" | ||
|
||
"cosmossdk.io/log" | ||
) | ||
|
||
type OptimisticExecution struct { | ||
facundomedica marked this conversation as resolved.
Show resolved
Hide resolved
|
||
mtx sync.RWMutex | ||
facundomedica marked this conversation as resolved.
Show resolved
Hide resolved
|
||
stopCh chan struct{} | ||
shouldAbort bool | ||
running bool | ||
initialized bool | ||
|
||
// we could use generics here in the future to allow other types of req/resp | ||
fn func(*abci.RequestFinalizeBlock) (*abci.ResponseFinalizeBlock, error) | ||
facundomedica marked this conversation as resolved.
Show resolved
Hide resolved
|
||
request *abci.RequestFinalizeBlock | ||
response *abci.ResponseFinalizeBlock | ||
err error | ||
executionTime time.Duration | ||
logger log.Logger | ||
|
||
// debugging options | ||
abortRate int // number from 0 to 100 | ||
} | ||
|
||
func NewOptimisticExecution(logger log.Logger, fn func(*abci.RequestFinalizeBlock) (*abci.ResponseFinalizeBlock, error), opts ...func(*OptimisticExecution)) *OptimisticExecution { | ||
facundomedica marked this conversation as resolved.
Show resolved
Hide resolved
|
||
oe := &OptimisticExecution{logger: logger, fn: fn} | ||
for _, opt := range opts { | ||
opt(oe) | ||
} | ||
return oe | ||
} | ||
|
||
func WithAbortRate(rate int) func(*OptimisticExecution) { | ||
return func(oe *OptimisticExecution) { | ||
oe.abortRate = rate | ||
} | ||
} | ||
|
||
// Reset resets the OE context. Must be called whenever we want to invalidate | ||
// the current OE. For example when on FinalizeBlock we want to process the | ||
// block async, we run Reset() to make sure ShouldAbort() returns always false. | ||
func (oe *OptimisticExecution) Reset() { | ||
oe.mtx.Lock() | ||
defer oe.mtx.Unlock() | ||
oe.request = nil | ||
oe.response = nil | ||
oe.err = nil | ||
oe.executionTime = 0 | ||
oe.shouldAbort = false | ||
oe.running = false | ||
oe.initialized = false | ||
} | ||
|
||
func (oe *OptimisticExecution) Enabled() bool { | ||
return oe != nil | ||
} | ||
|
||
// Initialized returns true if the OE was initialized, meaning that it contains | ||
// a request and it was run or it is running. | ||
func (oe *OptimisticExecution) Initialized() bool { | ||
if oe == nil { | ||
return false | ||
} | ||
oe.mtx.RLock() | ||
defer oe.mtx.RUnlock() | ||
|
||
return oe.initialized | ||
} | ||
|
||
// Execute initializes the OE and starts it in a goroutine. | ||
func (oe *OptimisticExecution) Execute( | ||
req *abci.RequestProcessProposal, | ||
) { | ||
facundomedica marked this conversation as resolved.
Show resolved
Hide resolved
|
||
oe.mtx.Lock() | ||
defer oe.mtx.Unlock() | ||
|
||
oe.stopCh = make(chan struct{}) | ||
oe.request = &abci.RequestFinalizeBlock{ | ||
Txs: req.Txs, | ||
DecidedLastCommit: req.ProposedLastCommit, | ||
Misbehavior: req.Misbehavior, | ||
Hash: req.Hash, | ||
Height: req.Height, | ||
Time: req.Time, | ||
NextValidatorsHash: req.NextValidatorsHash, | ||
ProposerAddress: req.ProposerAddress, | ||
} | ||
|
||
oe.logger.Debug("OE started") | ||
facundomedica marked this conversation as resolved.
Show resolved
Hide resolved
|
||
start := time.Now() | ||
|
||
oe.running = true | ||
oe.initialized = true | ||
|
||
go func() { | ||
resp, err := oe.fn(oe.request) | ||
oe.mtx.Lock() | ||
oe.executionTime = time.Since(start) | ||
oe.logger.Debug("OE finished", "duration", oe.executionTime.String()) | ||
oe.response, oe.err = resp, err | ||
oe.running = false | ||
close(oe.stopCh) | ||
oe.mtx.Unlock() | ||
}() | ||
} | ||
|
||
// AbortIfNeeded aborts the OE if the request hash is not the same as the one in | ||
// the running OE. Returns true if the OE was aborted. | ||
func (oe *OptimisticExecution) AbortIfNeeded(reqHash []byte) bool { | ||
if oe == nil { | ||
return false | ||
} | ||
|
||
oe.mtx.Lock() | ||
defer oe.mtx.Unlock() | ||
|
||
if !bytes.Equal(oe.request.Hash, reqHash) { | ||
oe.logger.Debug("OE aborted due to hash mismatch", "oe_hash", oe.request.Hash, "req_hash", reqHash) | ||
oe.shouldAbort = true | ||
} | ||
|
||
// test abort rate | ||
facundomedica marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if oe.abortRate > 0 && !oe.shouldAbort { | ||
oe.shouldAbort = rand.Intn(100) < oe.abortRate | ||
|
||
if oe.shouldAbort { | ||
oe.logger.Debug("OE aborted due to test abort rate") | ||
} | ||
} | ||
|
||
return oe.shouldAbort | ||
} | ||
|
||
// Abort aborts the OE unconditionally. | ||
func (oe *OptimisticExecution) Abort() { | ||
oe.mtx.Lock() | ||
defer oe.mtx.Unlock() | ||
oe.shouldAbort = true | ||
} | ||
|
||
// ShouldAbort must only be used in the fn passed to SetupOptimisticExecution to | ||
// check if the OE was aborted and return as soon as possible. | ||
func (oe *OptimisticExecution) ShouldAbort() bool { | ||
if oe == nil { | ||
return false | ||
} | ||
|
||
oe.mtx.RLock() | ||
defer oe.mtx.RUnlock() | ||
return oe.shouldAbort | ||
} | ||
|
||
// Running returns true if the OE is still running. | ||
func (oe *OptimisticExecution) Running() bool { | ||
if oe == nil { | ||
return false | ||
} | ||
|
||
oe.mtx.RLock() | ||
defer oe.mtx.RUnlock() | ||
return oe.running | ||
} | ||
|
||
// WaitResult waits for the OE to finish and returns the result. | ||
func (oe *OptimisticExecution) WaitResult() (*abci.ResponseFinalizeBlock, error) { | ||
<-oe.stopCh | ||
return oe.response, oe.err | ||
} |
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
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.
Change potentially affects state.
Call sequence:
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.
Any comment on this warning? I think the bot has a point.
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.
I think it's just detecting that a change was introduced to ProcessProposal