-
Notifications
You must be signed in to change notification settings - Fork 115
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[CORE-542] - Update delaymsg module to execute messages with a cached…
… context (#214)
- Loading branch information
Showing
8 changed files
with
608 additions
and
11 deletions.
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
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 | ||
} |
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,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) | ||
}) | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Oops, something went wrong.