-
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: Optimistic Execution #16581
Merged
Merged
feat: Optimistic Execution #16581
Changes from 14 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
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,100 @@ | ||
package baseapp | ||
|
||
import ( | ||
"bytes" | ||
"log" | ||
"math/rand" | ||
"sync" | ||
"time" | ||
|
||
abci "github.com/cometbft/cometbft/abci/types" | ||
) | ||
|
||
type OptimisticExecutionInfo struct { | ||
mtx sync.RWMutex | ||
stopCh chan struct{} | ||
shouldAbort bool | ||
running bool | ||
|
||
// we could use generics here in the future to allow other types of req/resp | ||
fn func(*abci.RequestFinalizeBlock) (*abci.ResponseFinalizeBlock, error) | ||
Request *abci.RequestFinalizeBlock | ||
Response *abci.ResponseFinalizeBlock | ||
Error error | ||
executionTime time.Duration | ||
} | ||
|
||
func SetupOptimisticExecution( | ||
req *abci.RequestProcessProposal, | ||
fn func(*abci.RequestFinalizeBlock) (*abci.ResponseFinalizeBlock, error), | ||
) *OptimisticExecutionInfo { | ||
return &OptimisticExecutionInfo{ | ||
stopCh: make(chan struct{}), | ||
fn: fn, | ||
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, | ||
}, | ||
} | ||
} | ||
|
||
func (oe *OptimisticExecutionInfo) Execute() { | ||
log.Println("Start OE ✅") | ||
start := time.Now() | ||
|
||
oe.running = true | ||
go func() { | ||
resp, err := oe.fn(oe.Request) | ||
oe.mtx.Lock() | ||
oe.executionTime = time.Since(start) | ||
oe.Response, oe.Error = resp, err | ||
oe.running = false | ||
close(oe.stopCh) | ||
oe.mtx.Unlock() | ||
}() | ||
|
||
} | ||
|
||
// AbortIfNeeded | ||
// If the request hash is not the same as the one in the OE, then abort the OE | ||
// and wait for the abort to happen. Returns true if the OE was aborted. | ||
func (oe *OptimisticExecutionInfo) AbortIfNeeded(reqHash []byte) bool { | ||
oe.mtx.Lock() | ||
defer oe.mtx.Unlock() | ||
if rand.Intn(100) > 80 || !bytes.Equal(oe.Request.Hash, reqHash) { | ||
|
||
log.Println("OE aborted ❌") | ||
oe.shouldAbort = true | ||
} | ||
return oe.shouldAbort | ||
} | ||
|
||
func (oe *OptimisticExecutionInfo) 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. | ||
// TODO: figure out a better name, maybe ReturnEarly? | ||
func (oe *OptimisticExecutionInfo) ShouldAbort() bool { | ||
defer oe.mtx.RUnlock() | ||
oe.mtx.RLock() | ||
return oe.shouldAbort | ||
} | ||
|
||
func (oe *OptimisticExecutionInfo) Running() bool { | ||
defer oe.mtx.RUnlock() | ||
oe.mtx.RLock() | ||
return oe.running | ||
} | ||
|
||
func (oe *OptimisticExecutionInfo) WaitResult() (*abci.ResponseFinalizeBlock, error) { | ||
<-oe.stopCh | ||
log.Println("OE took ⏱", oe.executionTime) | ||
return oe.Response, oe.Error | ||
} |
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: