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

Move clob hydration to preblocker (backport #1412) #1439

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
61 changes: 10 additions & 51 deletions protocol/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ import (
"github.com/cosmos/ibc-go/modules/capability"
capabilitykeeper "github.com/cosmos/ibc-go/modules/capability/keeper"
capabilitytypes "github.com/cosmos/ibc-go/modules/capability/types"
antetypes "github.com/dydxprotocol/v4-chain/protocol/app/ante/types"
"github.com/gorilla/mux"
"github.com/rakyll/statik/fs"
"github.com/spf13/cast"
Expand Down Expand Up @@ -1163,7 +1164,8 @@ func New(
)

app.ModuleManager.SetOrderPreBlockers(
upgradetypes.ModuleName,
upgradetypes.ModuleName, // Must be first since upgrades may be state schema breaking.
clobmoduletypes.ModuleName,
pricesmoduletypes.ModuleName,
)

Expand Down Expand Up @@ -1399,16 +1401,6 @@ func New(
if err := app.LoadLatestVersion(); err != nil {
tmos.Exit(err.Error())
}

// Hydrate memStores used for caching state.
app.hydrateMemStores()

// Hydrate the `memclob` with all ordersbooks from state,
// and hydrate the next `checkState` as well as the `memclob` with stateful orders.
app.hydrateMemclobWithOrderbooksAndStatefulOrders()

// Hydrate the keeper in-memory data structures.
app.hydrateKeeperInMemoryDataStructures()
}
app.initializeRateLimiters()

Expand Down Expand Up @@ -1613,15 +1605,6 @@ func (app *App) DisableHealthMonitorForTesting() {
app.DaemonHealthMonitor.DisableForTesting()
}

// hydrateMemStores hydrates the memStores used for caching state.
func (app *App) hydrateMemStores() {
// Create an `uncachedCtx` where the underlying MultiStore is the `rootMultiStore`.
// We use this to hydrate the `memStore` state with values from the underlying `rootMultiStore`.
uncachedCtx := app.BaseApp.NewUncachedContext(true, tmproto.Header{})
// Initialize memstore in clobKeeper with order fill amounts and stateful orders.
app.ClobKeeper.InitMemStore(uncachedCtx)
}

// initializeRateLimiters initializes the rate limiters from state if the application is
// not started from genesis.
func (app *App) initializeRateLimiters() {
Expand All @@ -1631,42 +1614,18 @@ func (app *App) initializeRateLimiters() {
app.ClobKeeper.InitalizeBlockRateLimitFromStateIfExists(uncachedCtx)
}

// hydrateMemclobWithOrderbooksAndStatefulOrders hydrates the memclob with orderbooks and stateful orders
// from state.
func (app *App) hydrateMemclobWithOrderbooksAndStatefulOrders() {
// Create a `checkStateCtx` where the underlying MultiStore is the `CacheMultiStore` for
// the `checkState`. We do this to avoid performing any state writes to the `rootMultiStore`
// directly.
checkStateCtx := app.BaseApp.NewContext(true)

// Initialize memclob in clobKeeper with orderbooks using `ClobPairs` in state.
app.ClobKeeper.InitMemClobOrderbooks(checkStateCtx)
// Initialize memclob with all existing stateful orders.
// TODO(DEC-1348): Emit indexer messages to indicate that application restarted.
app.ClobKeeper.InitStatefulOrders(checkStateCtx)
}

// hydrateKeeperInMemoryDataStructures hydrates the keeper with ClobPairId and PerpetualId mapping
// and untriggered conditional orders from state.
func (app *App) hydrateKeeperInMemoryDataStructures() {
// Create a `checkStateCtx` where the underlying MultiStore is the `CacheMultiStore` for
// the `checkState`. We do this to avoid performing any state writes to the `rootMultiStore`
// directly.
checkStateCtx := app.BaseApp.NewContext(true)

// Initialize the untriggered conditional orders data structure with untriggered
// conditional orders in state.
app.ClobKeeper.HydrateClobPairAndPerpetualMapping(checkStateCtx)
// Initialize the untriggered conditional orders data structure with untriggered
// conditional orders in state.
app.ClobKeeper.HydrateUntriggeredConditionalOrders(checkStateCtx)
}

// GetBaseApp returns the base app of the application
func (app *App) GetBaseApp() *baseapp.BaseApp { return app.BaseApp }

// PreBlocker application updates before each begin block.
func (app *App) PreBlocker(ctx sdk.Context, _ *abci.RequestFinalizeBlock) (*sdk.ResponsePreBlock, error) {
// Set gas meter to the free gas meter.
// This is because there is currently non-deterministic gas usage in the
// pre-blocker, e.g. due to hydration of in-memory data structures.
//
// Note that we don't need to reset the gas meter after the pre-blocker
// because Go is pass by value.
ctx = ctx.WithGasMeter(antetypes.NewFreeInfiniteGasMeter())
return app.ModuleManager.PreBlock(ctx)
}

Expand Down
10 changes: 0 additions & 10 deletions protocol/app/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
evidencemodule "cosmossdk.io/x/evidence"
feegrantmodule "cosmossdk.io/x/feegrant/module"
"cosmossdk.io/x/upgrade"
tmproto "github.com/cometbft/cometbft/proto/tendermint/types"
"github.com/cosmos/cosmos-sdk/types/module"
"github.com/cosmos/cosmos-sdk/x/auth"
authzmodule "github.com/cosmos/cosmos-sdk/x/authz/module"
Expand Down Expand Up @@ -137,15 +136,6 @@ func TestAppPanicsWithGrpcDisabled(t *testing.T) {
require.Panics(t, func() { testapp.DefaultTestApp(customFlags) })
}

func TestClobKeeperMemStoreHasBeenInitialized(t *testing.T) {
dydxApp := testapp.DefaultTestApp(nil)
ctx := dydxApp.NewUncachedContext(true, tmproto.Header{})

// The memstore panics if initialized twice so initializing again outside of application
// start-up should cause a panic.
require.Panics(t, func() { dydxApp.ClobKeeper.InitMemStore(ctx) })
}

func TestBaseApp(t *testing.T) {
dydxApp := testapp.DefaultTestApp(nil)
require.NotNil(t, dydxApp.GetBaseApp(), "Expected non-nil BaseApp")
Expand Down
23 changes: 23 additions & 0 deletions protocol/mocks/ClobKeeper.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions protocol/mocks/MemClob.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading