Skip to content

Commit

Permalink
[CLOB-930] Remove Reset method and swap to appOptions/baseAppOptions …
Browse files Browse the repository at this point in the history
…being explicitly set instead of an appCreatorFn (#697)

This will simplify creating e2e test framework that better tests non-determinism/flags/... since the e2e test framework will want to control how the App instance is created.
  • Loading branch information
lcwik authored Oct 24, 2023
1 parent 1f065b4 commit ed6429f
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 60 deletions.
8 changes: 4 additions & 4 deletions protocol/app/ante/gas_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,10 +181,10 @@ func TestSubmitTxnWithGas(t *testing.T) {
}

tApp := testapp.NewTestAppBuilder(t).
WithAppCreatorFn(
testapp.DefaultTestAppCreatorFn(map[string]interface{}{},
baseapp.SetMinGasPrices(cmd.MinGasPrice),
)).
WithAppOptions(
map[string]interface{}{},
baseapp.SetMinGasPrices(cmd.MinGasPrice),
).
Build()
ctx := tApp.InitChain()

Expand Down
2 changes: 1 addition & 1 deletion protocol/app/prepare/full_node_prepare_proposal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func TestFullNodePrepareProposalHandler(t *testing.T) {
flags.NonValidatingFullNodeFlag: true,
testlog.LoggerInstanceForTest: logger,
}
tApp := testApp.NewTestAppBuilder(t).WithAppCreatorFn(testApp.DefaultTestAppCreatorFn(appOpts)).Build()
tApp := testApp.NewTestAppBuilder(t).WithAppOptions(appOpts).Build()

found := false
tApp.AdvanceToBlock(2, testApp.AdvanceToBlockOptions{
Expand Down
45 changes: 12 additions & 33 deletions protocol/testutil/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,17 +123,6 @@ func DefaultTestApp(customFlags map[string]interface{}, baseAppOptions ...func(*
return dydxApp
}

// DefaultTestAppCreatorFn is a wrapper function around DefaultTestApp using the specified custom flags, and allowing
// for optional base app options.
func DefaultTestAppCreatorFn(
customFlags map[string]interface{},
baseAppOptions ...func(*baseapp.BaseApp),
) AppCreatorFn {
return func() *app.App {
return DefaultTestApp(customFlags, baseAppOptions...)
}
}

// DefaultGenesis returns a genesis doc using configuration from the local net with a genesis time
// equivalent to unix epoch + 1 nanosecond. We specifically use non-zero because stateful orders
// validate that block time is non-zero (https://github.com/dydxprotocol/v4-chain/protocol/blob/
Expand Down Expand Up @@ -257,8 +246,8 @@ func NewTestAppBuilder(t testing.TB) TestAppBuilder {
}
return TestAppBuilder{
genesisDocFn: DefaultGenesis,
appCreatorFn: DefaultTestAppCreatorFn(nil),
usesDefaultAppConfig: true,
appOptions: make(map[string]interface{}),
executeCheckTxs: func(ctx sdk.Context, app *app.App) (stop bool) {
return true
},
Expand All @@ -272,8 +261,9 @@ func NewTestAppBuilder(t testing.TB) TestAppBuilder {
// immutable.
type TestAppBuilder struct {
genesisDocFn GenesisDocCreatorFn
appCreatorFn func() *app.App
usesDefaultAppConfig bool
appOptions map[string]interface{}
baseAppOptions []func(*baseapp.BaseApp)
executeCheckTxs ExecuteCheckTxs
t testing.TB
}
Expand All @@ -285,10 +275,13 @@ func (tApp TestAppBuilder) WithGenesisDocFn(fn GenesisDocCreatorFn) TestAppBuild
return tApp
}

// WithAppCreatorFn returns a builder like this one with the specified function that will be used to create
// the application.
func (tApp TestAppBuilder) WithAppCreatorFn(fn AppCreatorFn) TestAppBuilder {
tApp.appCreatorFn = fn
// WithAppOptions returns a builder like this one with the specified app options.
func (tApp TestAppBuilder) WithAppOptions(
appOptions map[string]interface{},
baseAppOptions ...func(*baseapp.BaseApp),
) TestAppBuilder {
tApp.appOptions = appOptions
tApp.baseAppOptions = baseAppOptions
tApp.usesDefaultAppConfig = false
return tApp
}
Expand Down Expand Up @@ -334,7 +327,7 @@ func (tApp *TestApp) Builder() TestAppBuilder {
// InitChain initializes the chain. Will panic if initialized more than once.
func (tApp *TestApp) InitChain() sdk.Context {
if tApp.App != nil {
panic(errors.New("Cannot initialize chain that has been initialized already. Missing a Reset()?"))
panic(errors.New("Cannot initialize chain that has been initialized already."))
}
tApp.initChainIfNeeded()
return tApp.App.NewContext(true, tApp.header)
Expand All @@ -347,7 +340,7 @@ func (tApp *TestApp) initChainIfNeeded() {

// Get the initial genesis state and initialize the chain and commit the results of the initialization.
tApp.genesis = tApp.builder.genesisDocFn()
tApp.App = tApp.builder.appCreatorFn()
tApp.App = DefaultTestApp(tApp.builder.appOptions, tApp.builder.baseAppOptions...)
if tApp.builder.usesDefaultAppConfig {
tApp.App.Server.DisableUpdateMonitoringForTesting()
}
Expand Down Expand Up @@ -544,20 +537,6 @@ func (tApp *TestApp) AdvanceToBlock(
return tApp.App.NewContext(true, tApp.header)
}

// Reset resets the chain such that it can be initialized and executed again.
func (tApp *TestApp) Reset() {
if tApp.App != nil {
if err := tApp.App.Close(); err != nil {
tApp.builder.t.Fatal(err)
}
}
tApp.App = nil
tApp.genesis = types.GenesisDoc{}
tApp.header = tmproto.Header{}
tApp.passingCheckTxs = nil
tApp.halted = false
}

// GetHeader fetches the current header of the test app.
func (tApp *TestApp) GetHeader() tmproto.Header {
return tApp.header
Expand Down
6 changes: 3 additions & 3 deletions protocol/x/clob/e2e/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ func TestFailsDeliverTxWithIncorrectlySignedPlaceOrderTx(t *testing.T) {
appOpts := map[string]interface{}{
indexer.MsgSenderInstanceForTest: msgSender,
}
tApp := testapp.NewTestAppBuilder(t).WithAppCreatorFn(testapp.DefaultTestAppCreatorFn(appOpts)).Build()
tApp := testapp.NewTestAppBuilder(t).WithAppOptions(appOpts).Build()
tApp.InitChain()
ctx := tApp.AdvanceToBlock(2, testapp.AdvanceToBlockOptions{})

Expand Down Expand Up @@ -474,7 +474,7 @@ func TestFailsDeliverTxWithUnsignedTransactions(t *testing.T) {
appOpts := map[string]interface{}{
indexer.MsgSenderInstanceForTest: msgSender,
}
tApp := testapp.NewTestAppBuilder(t).WithAppCreatorFn(testapp.DefaultTestAppCreatorFn(appOpts)).Build()
tApp := testapp.NewTestAppBuilder(t).WithAppOptions(appOpts).Build()
tApp.InitChain()
tApp.AdvanceToBlock(2, testapp.AdvanceToBlockOptions{})

Expand Down Expand Up @@ -506,7 +506,7 @@ func TestStats(t *testing.T) {
appOpts := map[string]interface{}{
indexer.MsgSenderInstanceForTest: msgSender,
}
tApp := testapp.NewTestAppBuilder(t).WithAppCreatorFn(testapp.DefaultTestAppCreatorFn(appOpts)).Build()
tApp := testapp.NewTestAppBuilder(t).WithAppOptions(appOpts).Build()

// Epochs start at block height 2.
startTime := time.Unix(10, 0).UTC()
Expand Down
2 changes: 1 addition & 1 deletion protocol/x/clob/e2e/long_term_orders_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1221,7 +1221,7 @@ func TestPlaceLongTermOrder(t *testing.T) {
appOpts := map[string]interface{}{
indexer.MsgSenderInstanceForTest: msgSender,
}
tApp := testapp.NewTestAppBuilder(t).WithAppCreatorFn(testapp.DefaultTestAppCreatorFn(appOpts)).Build()
tApp := testapp.NewTestAppBuilder(t).WithAppOptions(appOpts).Build()
ctx := tApp.InitChain()

for _, ordersAndExpectations := range tc.ordersAndExpectationsPerBlock {
Expand Down
17 changes: 8 additions & 9 deletions protocol/x/clob/e2e/short_term_orders_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package clob_test

import (
"github.com/dydxprotocol/v4-chain/protocol/indexer"
"testing"

"golang.org/x/exp/slices"
Expand All @@ -9,7 +10,6 @@ import (
"github.com/dydxprotocol/v4-chain/protocol/dtypes"
"github.com/dydxprotocol/v4-chain/protocol/lib"

"github.com/dydxprotocol/v4-chain/protocol/indexer"
indexerevents "github.com/dydxprotocol/v4-chain/protocol/indexer/events"
"github.com/dydxprotocol/v4-chain/protocol/indexer/indexer_manager"
"github.com/dydxprotocol/v4-chain/protocol/indexer/msgsender"
Expand All @@ -25,11 +25,7 @@ import (
)

func TestPlaceOrder(t *testing.T) {
msgSender := msgsender.NewIndexerMessageSenderInMemoryCollector()
appOpts := map[string]interface{}{
indexer.MsgSenderInstanceForTest: msgSender,
}
tApp := testapp.NewTestAppBuilder(t).WithAppCreatorFn(testapp.DefaultTestAppCreatorFn(appOpts)).Build()
tApp := testapp.NewTestAppBuilder(t).Build()
ctx := tApp.InitChain()

aliceSubaccount := tApp.App.SubaccountsKeeper.GetSubaccount(ctx, constants.Alice_Num0)
Expand Down Expand Up @@ -568,10 +564,13 @@ func TestPlaceOrder(t *testing.T) {

for name, tc := range tests {
t.Run(name, func(t *testing.T) {
// Reset for each iteration of the loop
tApp.Reset()

msgSender := msgsender.NewIndexerMessageSenderInMemoryCollector()
appOpts := map[string]interface{}{
indexer.MsgSenderInstanceForTest: msgSender,
}
tApp = testapp.NewTestAppBuilder(t).WithAppOptions(appOpts).Build()
ctx = tApp.InitChain()

// Clear any messages produced prior to these checkTx calls.
msgSender.Clear()
for _, order := range tc.orders {
Expand Down
9 changes: 2 additions & 7 deletions protocol/x/rewards/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,6 @@ func TestSetRewardShare_FailsWithNonpositiveWeight(t *testing.T) {
}

func TestAddRewardShareToAddress(t *testing.T) {
tApp := testapp.NewTestAppBuilder(t).Build()

tests := map[string]struct {
prevRewardShare *types.RewardShare // nil if no previous share
newWeight *big.Int
Expand Down Expand Up @@ -120,7 +118,7 @@ func TestAddRewardShareToAddress(t *testing.T) {
// Run tests.
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
tApp.Reset()
tApp := testapp.NewTestAppBuilder(t).Build()
ctx := tApp.InitChain()
k := tApp.App.RewardsKeeper

Expand All @@ -143,7 +141,6 @@ func TestAddRewardShareToAddress(t *testing.T) {
}

func TestAddRewardSharesForFill(t *testing.T) {
tApp := testapp.NewTestAppBuilder(t).Build()
makerAddress := TestAddress1
takerAdderss := TestAddress2

Expand Down Expand Up @@ -268,7 +265,7 @@ func TestAddRewardSharesForFill(t *testing.T) {
// Run tests.
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
tApp.Reset()
tApp := testapp.NewTestAppBuilder(t).Build()
ctx := tApp.InitChain()
k := tApp.App.RewardsKeeper

Expand Down Expand Up @@ -653,8 +650,6 @@ func TestProcessRewardsForBlock(t *testing.T) {
)
return genesis
}).Build()

tApp.Reset()
ctx := tApp.InitChain()
k := tApp.App.RewardsKeeper

Expand Down
4 changes: 2 additions & 2 deletions protocol/x/sending/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func TestMsgDepositToSubaccount(t *testing.T) {
appOpts := map[string]interface{}{
indexer.MsgSenderInstanceForTest: msgSender,
}
tApp := testapp.NewTestAppBuilder(t).WithAppCreatorFn(testapp.DefaultTestAppCreatorFn(appOpts)).Build()
tApp := testapp.NewTestAppBuilder(t).WithAppOptions(appOpts).Build()
ctx := tApp.AdvanceToBlock(2, testapp.AdvanceToBlockOptions{})
// Clear any messages produced prior to CheckTx calls.
msgSender.Clear()
Expand Down Expand Up @@ -284,7 +284,7 @@ func TestMsgWithdrawFromSubaccount(t *testing.T) {
appOpts := map[string]interface{}{
indexer.MsgSenderInstanceForTest: msgSender,
}
tApp := testapp.NewTestAppBuilder(t).WithAppCreatorFn(testapp.DefaultTestAppCreatorFn(appOpts)).Build()
tApp := testapp.NewTestAppBuilder(t).WithAppOptions(appOpts).Build()
ctx := tApp.AdvanceToBlock(2, testapp.AdvanceToBlockOptions{})
// Clear any messages produced prior to CheckTx calls.
msgSender.Clear()
Expand Down

0 comments on commit ed6429f

Please sign in to comment.