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 28 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
77 changes: 65 additions & 12 deletions baseapp/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,12 @@ func (app *BaseApp) ProcessProposal(req *abci.RequestProcessProposal) (resp *abc
// processed the first block, as we want to avoid overwriting the finalizeState
// after state changes during InitChain.
if req.Height > app.initialHeight {
// abort any running OE
if app.optimisticExec.Running() {
app.optimisticExec.Abort()
_, _ = app.optimisticExec.WaitResult() // ignore the result
Copy link
Contributor

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:

(*github.com/cosmos/cosmos-sdk/baseapp.BaseApp).ProcessProposal (baseapp/abci.go:467)

Copy link
Member

@kocubinski kocubinski Aug 17, 2023

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.

Copy link
Member Author

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

}

app.setState(execModeFinalize, header)
}

Expand Down Expand Up @@ -534,6 +540,12 @@ func (app *BaseApp) ProcessProposal(req *abci.RequestProcessProposal) (resp *abc
return &abci.ResponseProcessProposal{Status: abci.ResponseProcessProposal_REJECT}, nil
}

// Only execute optimistic execution if OE is enabled and the block height is greater than the initial height.
// During the first block we'll be carrying state from InitChain, so it would be impossible for us to easily revert.
facundomedica marked this conversation as resolved.
Show resolved Hide resolved
if app.optimisticExec.Enabled() && req.Height > app.initialHeight {
app.optimisticExec.Execute(req)
}

return resp, nil
}

Expand Down Expand Up @@ -647,17 +659,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.checkHalt(req.Height, req.Time); err != nil {
Expand Down Expand Up @@ -732,6 +734,12 @@ func (app *BaseApp) FinalizeBlock(req *abci.RequestFinalizeBlock) (*abci.Respons
return nil, err
}

// First check for an abort signal after beginBlock, as it's the first place
// we spend any significant amount of time.
if app.optimisticExec.Running() && app.optimisticExec.ShouldAbort() {
return nil, nil
}

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

// Iterate over all raw transactions in the proposal and attempt to execute
Expand All @@ -758,6 +766,11 @@ func (app *BaseApp) FinalizeBlock(req *abci.RequestFinalizeBlock) (*abci.Respons
)
}

// check after every tx if we should abort
if app.optimisticExec.Running() && app.optimisticExec.ShouldAbort() {
return nil, nil
}

txResults = append(txResults, response)
}

Expand All @@ -770,6 +783,11 @@ func (app *BaseApp) FinalizeBlock(req *abci.RequestFinalizeBlock) (*abci.Respons
return nil, err
}

// check after endBlock if we should abort, to avoid propagating the result
if app.optimisticExec.Running() && app.optimisticExec.ShouldAbort() {
return nil, nil
}

events = append(events, endBlock.Events...)
cp := app.GetConsensusParams(app.finalizeBlockState.ctx)

Expand All @@ -778,10 +796,45 @@ func (app *BaseApp) FinalizeBlock(req *abci.RequestFinalizeBlock) (*abci.Respons
TxResults: txResults,
ValidatorUpdates: endBlock.ValidatorUpdates,
ConsensusParamUpdates: &cp,
AppHash: app.workingHash(),
}, 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) {
if app.optimisticExec.Initialized() {
// check if the hash we got is the same as the one we are executing
aborted := app.optimisticExec.AbortIfNeeded(req.Hash)
// Wait for the OE to finish, regardless of whether it was aborted or not
res, err := app.optimisticExec.WaitResult()

// only return if we are not aborting
if !aborted {
res.AppHash = app.workingHash()
return res, err
}

// if it was aborted, we need to reset the state
app.finalizeBlockState = nil
app.optimisticExec.Reset()
}

// if no OE is running, just run the block (this is either a block replay or a OE that got aborted)
res, err := app.internalFinalizeBlock(req)
if res != nil {
res.AppHash = app.workingHash()
}
return res, err
}

// checkHalt checkes if height or time exceeds halt-height or halt-time respectively.
func (app *BaseApp) checkHalt(height int64, time time.Time) error {
var halt bool
Expand Down
3 changes: 3 additions & 0 deletions baseapp/baseapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"cosmossdk.io/store/snapshots"
storetypes "cosmossdk.io/store/types"

"github.com/cosmos/cosmos-sdk/baseapp/oe"
"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
servertypes "github.com/cosmos/cosmos-sdk/server/types"
Expand Down Expand Up @@ -187,6 +188,8 @@ type BaseApp struct {
chainID string

cdc codec.Codec

optimisticExec *oe.OptimisticExecution
facundomedica marked this conversation as resolved.
Show resolved Hide resolved
}

// NewBaseApp returns a reference to an initialized BaseApp. It accepts a
Expand Down
174 changes: 174 additions & 0 deletions baseapp/oe/optimistic_execution.go
facundomedica marked this conversation as resolved.
Show resolved Hide resolved
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()
Fixed Show fixed Hide fixed
Fixed Show fixed Hide fixed
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
Fixed Show fixed Hide fixed
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
}
8 changes: 8 additions & 0 deletions baseapp/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
snapshottypes "cosmossdk.io/store/snapshots/types"
storetypes "cosmossdk.io/store/types"

"github.com/cosmos/cosmos-sdk/baseapp/oe"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down Expand Up @@ -104,6 +105,13 @@ func SetChainID(chainID string) func(*BaseApp) {
return func(app *BaseApp) { app.chainID = chainID }
}

// SetOptimisticExecution enables optimistic execution.
func SetOptimisticExecution(opts ...func(*oe.OptimisticExecution)) func(*BaseApp) {
return func(app *BaseApp) {
app.optimisticExec = oe.NewOptimisticExecution(app.logger, app.internalFinalizeBlock, opts...)
}
}

func (app *BaseApp) SetName(name string) {
if app.sealed {
panic("SetName() on sealed BaseApp")
Expand Down
1 change: 1 addition & 0 deletions server/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,7 @@ func DefaultBaseappOptions(appOpts types.AppOptions) []func(*baseapp.BaseApp) {
defaultMempool,
baseapp.SetChainID(chainID),
baseapp.SetQueryGasLimit(cast.ToUint64(appOpts.Get(FlagQueryGasLimit))),
baseapp.SetOptimisticExecution(),
}
}

Expand Down
Loading