Skip to content
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 39 commits into from
Sep 18, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
a57c937
feat: Optimistic Execution
facundomedica Jun 15, 2023
023256e
fix panic recovery
facundomedica Jun 15, 2023
14b80c4
remove test changes
facundomedica Jun 15, 2023
47b8a1c
Merge branch 'main' of https://github.com/cosmos/cosmos-sdk into facu/oe
facundomedica Jun 16, 2023
deaf6b7
fix test
facundomedica Jun 16, 2023
f30e4a7
make comet panic instead of sdk
facundomedica Jun 16, 2023
573d107
add abort channel
facundomedica Jun 20, 2023
17b5ca4
fix abort
facundomedica Jun 20, 2023
d371c16
clean up phase1
facundomedica Jun 20, 2023
c9dbc9a
merge
facundomedica Jun 30, 2023
20f0325
testing testing
facundomedica Jun 30, 2023
6aec99a
Merge branch 'main' of https://github.com/cosmos/cosmos-sdk into facu/oe
facundomedica Jul 13, 2023
e920201
merge main
facundomedica Jul 20, 2023
b855c1a
progress
facundomedica Jul 20, 2023
265e32d
fix
facundomedica Jul 21, 2023
2830366
Merge branch 'main' of https://github.com/cosmos/cosmos-sdk into facu/oe
facundomedica Jul 26, 2023
c835fa7
progress
facundomedica Jul 27, 2023
b26cfe8
Merge branch 'main' into facu/oe
facundomedica Jul 27, 2023
06cb990
lint
facundomedica Jul 27, 2023
f2aec1d
progress
facundomedica Jul 27, 2023
64988fa
Merge branch 'main' of https://github.com/cosmos/cosmos-sdk into facu/oe
facundomedica Jul 31, 2023
18b666e
fix race condition
facundomedica Jul 31, 2023
125e942
progress
facundomedica Jul 31, 2023
0f1ad3b
progress
facundomedica Aug 1, 2023
35ae374
Merge branch 'main' of https://github.com/cosmos/cosmos-sdk into facu/oe
facundomedica Aug 2, 2023
c798e17
progress
facundomedica Aug 2, 2023
655dde4
merge main
facundomedica Aug 17, 2023
74147f1
added mutext to mempools
facundomedica Aug 17, 2023
0d45c3c
add test and do some refactor
facundomedica Aug 27, 2023
b008a8a
undo test changes
facundomedica Aug 27, 2023
78b233d
fix
facundomedica Aug 27, 2023
4f90f04
Update baseapp/abci.go
facundomedica Aug 29, 2023
2b574d5
only start optimistic execution if processProposal resp is accepted
facundomedica Sep 9, 2023
f91b715
Merge branch 'main' into facu/oe
facundomedica Sep 14, 2023
1c4743a
godoc + tests
facundomedica Sep 18, 2023
8cda1f1
add file
facundomedica Sep 18, 2023
0065196
cl++
facundomedica Sep 18, 2023
9d6c8b1
cl++
facundomedica Sep 18, 2023
8bdd23d
lint
facundomedica Sep 18, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 68 additions & 11 deletions baseapp/abci.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package baseapp

import (
"bytes"
"context"
"crypto/sha256"
"fmt"
"log"
"os"
"sort"
"strings"
Expand Down Expand Up @@ -532,6 +534,40 @@ func (app *BaseApp) ProcessProposal(req *abci.RequestProcessProposal) (resp *abc
return &abci.ResponseProcessProposal{Status: abci.ResponseProcessProposal_REJECT}, nil
}

if app.oeEnabled {
if app.oeInfo == nil {
completionSignal := make(chan struct{})
app.oeInfo = &OptimisticExecutionInfo{
Completion: completionSignal,
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,
},
}

go func() {
defer func() {
if r := recover(); r != nil {
log.Println("⚡️⚡️⚡️ RECOVERED PANIC IN OE")
app.oeInfo.Panic = r
}
}()
log.Println("Running OE ✅")
app.oeInfo.Response, app.oeInfo.Error = app.internalFinalizeBlock(app.oeInfo.Request)
app.oeInfo.Completion <- struct{}{}
}()
Fixed Show fixed Hide fixed

} else if !bytes.Equal(app.oeInfo.Request.Hash, req.Hash) {
app.oeInfo.Aborted = true
}
}

return resp, nil
}

Expand Down Expand Up @@ -635,17 +671,7 @@ func (app *BaseApp) VerifyVoteExtension(req *abci.RequestVerifyVoteExtension) (r
return resp, err
}

// FinalizeBlock will execute the block proposal provided by RequestFinalizeBlock.
// Specifically, it will execute an application's BeginBlock (if defined), followed
// by the transactions in the proposal, finally followed by the application's
// EndBlock (if defined).
//
// For each raw transaction, i.e. a byte slice, BaseApp will only execute it if
// it adheres to the sdk.Tx interface. Otherwise, the raw transaction will be
// skipped. This is to support compatibility with proposers injecting vote
// extensions into the proposal, which should not themselves be executed in cases
// where they adhere to the sdk.Tx interface.
func (app *BaseApp) FinalizeBlock(req *abci.RequestFinalizeBlock) (*abci.ResponseFinalizeBlock, error) {
func (app *BaseApp) internalFinalizeBlock(req *abci.RequestFinalizeBlock) (*abci.ResponseFinalizeBlock, error) {
var events []abci.Event

if err := app.validateFinalizeBlockHeight(req); err != nil {
Expand Down Expand Up @@ -712,6 +738,7 @@ func (app *BaseApp) FinalizeBlock(req *abci.RequestFinalizeBlock) (*abci.Respons
}

beginBlock := app.beginBlock(req)

events = append(events, beginBlock.Events...)

// Iterate over all raw transactions in the proposal and attempt to execute
Expand Down Expand Up @@ -747,6 +774,36 @@ func (app *BaseApp) FinalizeBlock(req *abci.RequestFinalizeBlock) (*abci.Respons
}, nil
}

// FinalizeBlock will execute the block proposal provided by RequestFinalizeBlock.
// Specifically, it will execute an application's BeginBlock (if defined), followed
// by the transactions in the proposal, finally followed by the application's
// EndBlock (if defined).
//
// For each raw transaction, i.e. a byte slice, BaseApp will only execute it if
// it adheres to the sdk.Tx interface. Otherwise, the raw transaction will be
// skipped. This is to support compatibility with proposers injecting vote
// extensions into the proposal, which should not themselves be executed in cases
// where they adhere to the sdk.Tx interface.
func (app *BaseApp) FinalizeBlock(req *abci.RequestFinalizeBlock) (*abci.ResponseFinalizeBlock, error) {
defer func() {
app.oeInfo = nil
}()

if app.oeInfo != nil && app.oeEnabled {
if app.oeInfo.Panic != nil {
panic(app.oeInfo.Panic)
}

<-app.oeInfo.Completion
if !app.oeInfo.Aborted && bytes.Equal(app.oeInfo.Request.Hash, req.Hash) {
return app.oeInfo.Response, app.oeInfo.Error
}
}

log.Println("NOT running OE ❌")
return app.internalFinalizeBlock(req)
}

// Commit implements the ABCI interface. It will commit all state that exists in
// the deliver state's multi-store and includes the resulting commit ID in the
// returned abci.ResponseCommit. Commit will set the check state based on the
Expand Down
4 changes: 4 additions & 0 deletions baseapp/baseapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,9 @@ type BaseApp struct {
chainID string

cdc codec.Codec

oeInfo *OptimisticExecutionInfo
oeEnabled bool
}

// NewBaseApp returns a reference to an initialized BaseApp. It accepts a
Expand All @@ -197,6 +200,7 @@ func NewBaseApp(
msgServiceRouter: NewMsgServiceRouter(),
txDecoder: txDecoder,
fauxMerkleMode: false,
oeEnabled: true,
}

for _, option := range options {
Expand Down
12 changes: 12 additions & 0 deletions baseapp/optimistic_execution.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package baseapp

import abci "github.com/cometbft/cometbft/abci/types"

type OptimisticExecutionInfo struct {
Aborted bool
Completion chan struct{}
Request *abci.RequestFinalizeBlock
Response *abci.ResponseFinalizeBlock
Error error
Panic interface{}
}