Skip to content

Commit

Permalink
[CORE-542] - Update delaymsg module to execute messages with a cached…
Browse files Browse the repository at this point in the history
… context (#214)
  • Loading branch information
clemire authored Sep 13, 2023
1 parent 82d1cbd commit 1befa27
Show file tree
Hide file tree
Showing 8 changed files with 608 additions and 11 deletions.
1 change: 1 addition & 0 deletions protocol/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -1168,6 +1168,7 @@ func (app *App) InitChainer(ctx sdk.Context, req abci.RequestInitChain) abci.Res
app.IndexerEventManager.SendOnchainData(block)
app.IndexerEventManager.ClearEvents(ctx)

app.Logger().Info("Initialized chain", "blockHeight", ctx.BlockHeight())
return initResponse
}

Expand Down
31 changes: 31 additions & 0 deletions protocol/lib/abci/util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package abci

import (
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/pkg/errors"
)

// RunCached wraps a function with a cache context and writes the cache to the context if the
// function call succeeds. If the function call fails, the cache is discarded.
func RunCached(
c sdk.Context,
f func(sdk.Context) error,
) (
err error,
) {
ctx, writeCache := c.CacheContext()

defer func() {
if r := recover(); r != nil {
err = errors.WithStack(fmt.Errorf("recovered from panic in cached context: %v", r))
}
}()

if err := f(ctx); err != nil {
return err
}

writeCache()
return nil
}
70 changes: 70 additions & 0 deletions protocol/lib/abci/util_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package abci_test

import (
"fmt"
"github.com/cometbft/cometbft/libs/log"
tmproto "github.com/cometbft/cometbft/proto/tendermint/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/dydxprotocol/v4-chain/protocol/lib/abci"
"github.com/dydxprotocol/v4-chain/protocol/mocks"
"github.com/stretchr/testify/require"
"testing"
)

func TestRunCached_Mixed(t *testing.T) {
testEvent := sdk.NewEvent("test", sdk.NewAttribute("key", "value"))
tests := map[string]struct {
f func(ctx sdk.Context) error
expectedError error
}{
"success": {
f: func(ctx sdk.Context) error {
ctx.EventManager().EmitEvent(testEvent)
return nil
},
},
"failure": {
f: func(ctx sdk.Context) error {
ctx.EventManager().EmitEvent(testEvent)
return fmt.Errorf("failure")
},
expectedError: fmt.Errorf("failure"),
},
"panic": {
f: func(ctx sdk.Context) error {
ctx.EventManager().EmitEvent(testEvent)
panic("panic")
},
expectedError: fmt.Errorf("panic"),
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
ms := &mocks.MultiStore{}
cms := &mocks.CacheMultiStore{}

// Expect that the cached store is created and returned.
ms.On("CacheMultiStore").Return(cms).Once()

if tc.expectedError == nil {
// For non-error cases, expect that the cache is written to the underlying store.
cms.On("Write").Return(nil).Once()
}

ctx := sdk.NewContext(ms, tmproto.Header{}, false, log.NewNopLogger())

err := abci.RunCached(ctx, tc.f)

if tc.expectedError != nil {
require.ErrorContains(t, err, tc.expectedError.Error())
require.Len(t, ctx.EventManager().Events(), 0)
} else {
require.NoError(t, err)
require.Equal(t, ctx.EventManager().Events(), sdk.Events{testEvent})
}

ms.AssertExpectations(t)
cms.AssertExpectations(t)
})
}
}
212 changes: 212 additions & 0 deletions protocol/mocks/CacheMultiStore.go

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

2 changes: 2 additions & 0 deletions protocol/mocks/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ mock-clean:
mock-gen:
@go run github.com/vektra/mockery/v2 --name=InterfaceRegistry --dir=$(GOPATH)/pkg/mod/github.com/dydxprotocol/cosmos-sdk@$(COSMOS_VERSION)/codec --recursive --output=./mocks
@go run github.com/vektra/mockery/v2 --name=Configurator --dir=$(GOPATH)/pkg/mod/github.com/dydxprotocol/cosmos-sdk@$(COSMOS_VERSION)/types --recursive --output=./mocks
@go run github.com/vektra/mockery/v2 --name=MultiStore --dir=$(GOPATH)/pkg/mod/github.com/dydxprotocol/cosmos-sdk@$(COSMOS_VERSION)/store/types --recursive --output=./mocks
@go run github.com/vektra/mockery/v2 --name=CacheMultiStore --dir=$(GOPATH)/pkg/mod/github.com/dydxprotocol/cosmos-sdk@$(COSMOS_VERSION)/store/types --recursive --output=./mocks
@go run github.com/vektra/mockery/v2 --name=AnteDecorator --dir=$(GOPATH)/pkg/mod/github.com/dydxprotocol/cosmos-sdk@$(COSMOS_VERSION)/types --recursive --output=./mocks
@go run github.com/vektra/mockery/v2 --name=TxConfig --dir=$(GOPATH)/pkg/mod/github.com/dydxprotocol/cosmos-sdk@$(COSMOS_VERSION)/client --recursive --output=./mocks
@go run github.com/vektra/mockery/v2 --name=TxBuilder --dir=$(GOPATH)/pkg/mod/github.com/dydxprotocol/cosmos-sdk@$(COSMOS_VERSION)/client --recursive --output=./mocks
Expand Down
Loading

0 comments on commit 1befa27

Please sign in to comment.