From c4a5b4e4ab6711e27142635952d6dcd8d88f0766 Mon Sep 17 00:00:00 2001 From: Carlton Hanna Date: Thu, 4 Nov 2021 19:30:01 -0600 Subject: [PATCH 01/13] convert message router to interface --- baseapp/baseapp.go | 4 ++-- baseapp/msg_service_router.go | 7 +++++++ x/authz/keeper/keeper.go | 4 ++-- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/baseapp/baseapp.go b/baseapp/baseapp.go index ba5ec2696e72..aff2ad02f9e8 100644 --- a/baseapp/baseapp.go +++ b/baseapp/baseapp.go @@ -55,7 +55,7 @@ type BaseApp struct { // nolint: maligned router sdk.Router // handle any kind of message queryRouter sdk.QueryRouter // router for redirecting query calls grpcQueryRouter *GRPCQueryRouter // router for redirecting gRPC query calls - msgServiceRouter *MsgServiceRouter // router for redirecting Msg service messages + msgServiceRouter IMsgServiceRouter // router for redirecting Msg service messages interfaceRegistry types.InterfaceRegistry txDecoder sdk.TxDecoder // unmarshal []byte into sdk.Tx @@ -196,7 +196,7 @@ func (app *BaseApp) Trace() bool { } // MsgServiceRouter returns the MsgServiceRouter of a BaseApp. -func (app *BaseApp) MsgServiceRouter() *MsgServiceRouter { return app.msgServiceRouter } +func (app *BaseApp) MsgServiceRouter() IMsgServiceRouter { return app.msgServiceRouter } // MountStores mounts all IAVL or DB stores to the provided keys in the BaseApp // multistore. diff --git a/baseapp/msg_service_router.go b/baseapp/msg_service_router.go index 1b7f8f89bf73..9dba81f74b2b 100644 --- a/baseapp/msg_service_router.go +++ b/baseapp/msg_service_router.go @@ -31,6 +31,13 @@ func NewMsgServiceRouter() *MsgServiceRouter { // MsgServiceHandler defines a function type which handles Msg service message. type MsgServiceHandler = func(ctx sdk.Context, req sdk.Msg) (*sdk.Result, error) +type IMsgServiceRouter interface { + Handler(msg sdk.Msg) MsgServiceHandler + HandlerByTypeURL(typeURL string) MsgServiceHandler + RegisterService(sd *grpc.ServiceDesc, handler interface{}) + SetInterfaceRegistry(interfaceRegistry codectypes.InterfaceRegistry) +} + // Handler returns the MsgServiceHandler for a given msg or nil if not found. func (msr *MsgServiceRouter) Handler(msg sdk.Msg) MsgServiceHandler { return msr.routes[sdk.MsgTypeURL(msg)] diff --git a/x/authz/keeper/keeper.go b/x/authz/keeper/keeper.go index ce4e421c1259..ea6d2fc2de54 100644 --- a/x/authz/keeper/keeper.go +++ b/x/authz/keeper/keeper.go @@ -19,11 +19,11 @@ import ( type Keeper struct { storeKey sdk.StoreKey cdc codec.BinaryCodec - router *baseapp.MsgServiceRouter + router baseapp.IMsgServiceRouter } // NewKeeper constructs a message authorization Keeper -func NewKeeper(storeKey sdk.StoreKey, cdc codec.BinaryCodec, router *baseapp.MsgServiceRouter) Keeper { +func NewKeeper(storeKey sdk.StoreKey, cdc codec.BinaryCodec, router baseapp.IMsgServiceRouter) Keeper { return Keeper{ storeKey: storeKey, cdc: cdc, From 04d223ce3c24acd60db4f32340e3f2e444777a41 Mon Sep 17 00:00:00 2001 From: Carlton Hanna Date: Fri, 5 Nov 2021 09:48:24 -0600 Subject: [PATCH 02/13] Add setter for msg service interface --- baseapp/baseapp.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/baseapp/baseapp.go b/baseapp/baseapp.go index aff2ad02f9e8..e4a65f2af1fc 100644 --- a/baseapp/baseapp.go +++ b/baseapp/baseapp.go @@ -195,6 +195,11 @@ func (app *BaseApp) Trace() bool { return app.trace } +// sets MsgServiceRouter of the BaseApp. +func (app *BaseApp) SetMsgServiceRouter(msgServiceRouter IMsgServiceRouter) { + app.msgServiceRouter = msgServiceRouter +} + // MsgServiceRouter returns the MsgServiceRouter of a BaseApp. func (app *BaseApp) MsgServiceRouter() IMsgServiceRouter { return app.msgServiceRouter } From a73478e2f554035266fe4d9610e4a98911734868 Mon Sep 17 00:00:00 2001 From: arnabmitra Date: Fri, 5 Nov 2021 13:48:41 -0600 Subject: [PATCH 03/13] Adding a custom fee charging handler, so that if provenance want's they can charge fees for certain msg types. --- baseapp/baseapp.go | 9 +++++++++ baseapp/options.go | 9 +++++++++ types/msgfee_handler.go | 4 ++++ 3 files changed, 22 insertions(+) create mode 100644 types/msgfee_handler.go diff --git a/baseapp/baseapp.go b/baseapp/baseapp.go index e4a65f2af1fc..4af88a8eae2a 100644 --- a/baseapp/baseapp.go +++ b/baseapp/baseapp.go @@ -133,6 +133,10 @@ type BaseApp struct { // nolint: maligned // indexEvents defines the set of events in the form {eventType}.{attributeKey}, // which informs Tendermint what to index. If empty, all events will be indexed. indexEvents map[string]struct{} + + // msg fee handler + // custom fee handler for provenance + msgFeeCalculateHandler sdk.AdditionalMsgFeeHandler // ante handler for fee and auth } // NewBaseApp returns a reference to an initialized BaseApp. It accepts a @@ -683,6 +687,11 @@ func (app *BaseApp) runTx(mode runTxMode, txBytes []byte) (gInfo sdk.GasInfo, re // Result if any single message fails or does not have a registered Handler. result, err = app.runMsgs(runMsgCtx, msgs, mode) if err == nil && mode == runTxModeDeliver { + // charge additional fee if logic calls for it + if app.msgFeeCalculateHandler != nil { + app.msgFeeCalculateHandler(runMsgCtx, false) + } + msCache.Write() if len(events) > 0 { diff --git a/baseapp/options.go b/baseapp/options.go index be9fbdc659a0..5cf2ac1526e3 100644 --- a/baseapp/options.go +++ b/baseapp/options.go @@ -237,3 +237,12 @@ func (app *BaseApp) SetInterfaceRegistry(registry types.InterfaceRegistry) { app.grpcQueryRouter.SetInterfaceRegistry(registry) app.msgServiceRouter.SetInterfaceRegistry(registry) } + +// SetKeeperHandler sets the Keepers needed by baseapp for msg fees. +func (app *BaseApp) SetMsgFeeHandler(msgFeeHandler sdk.AdditionalMsgFeeHandler) { + if app.sealed { + panic("SetKeeperHandler() on sealed BaseApp") + } + + app.msgFeeCalculateHandler = msgFeeHandler +} diff --git a/types/msgfee_handler.go b/types/msgfee_handler.go new file mode 100644 index 000000000000..50f1797b7f0a --- /dev/null +++ b/types/msgfee_handler.go @@ -0,0 +1,4 @@ +package types + +type AdditionalMsgFeeHandler func(ctx Context, simulate bool) (coins Coins, err error) + From 0e3ae7b3eeed0d1915240c34c75aa4f0c289ecbe Mon Sep 17 00:00:00 2001 From: arnabmitra Date: Fri, 5 Nov 2021 15:47:45 -0600 Subject: [PATCH 04/13] refactored variable name. --- baseapp/baseapp.go | 9 +++++---- baseapp/options.go | 2 +- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/baseapp/baseapp.go b/baseapp/baseapp.go index 4af88a8eae2a..fdcf1b9219a5 100644 --- a/baseapp/baseapp.go +++ b/baseapp/baseapp.go @@ -135,8 +135,8 @@ type BaseApp struct { // nolint: maligned indexEvents map[string]struct{} // msg fee handler - // custom fee handler for provenance - msgFeeCalculateHandler sdk.AdditionalMsgFeeHandler // ante handler for fee and auth + // custom fee handler for provenance, which will calculate and charge fee(if not run in simulation mode) + additionalMsgFeeHandler sdk.AdditionalMsgFeeHandler // ante handler for fee and auth } // NewBaseApp returns a reference to an initialized BaseApp. It accepts a @@ -688,8 +688,9 @@ func (app *BaseApp) runTx(mode runTxMode, txBytes []byte) (gInfo sdk.GasInfo, re result, err = app.runMsgs(runMsgCtx, msgs, mode) if err == nil && mode == runTxModeDeliver { // charge additional fee if logic calls for it - if app.msgFeeCalculateHandler != nil { - app.msgFeeCalculateHandler(runMsgCtx, false) + if app.additionalMsgFeeHandler != nil { + // call the msgFee + app.additionalMsgFeeHandler(runMsgCtx, mode == runTxModeSimulate) } msCache.Write() diff --git a/baseapp/options.go b/baseapp/options.go index 5cf2ac1526e3..6b03e919c284 100644 --- a/baseapp/options.go +++ b/baseapp/options.go @@ -244,5 +244,5 @@ func (app *BaseApp) SetMsgFeeHandler(msgFeeHandler sdk.AdditionalMsgFeeHandler) panic("SetKeeperHandler() on sealed BaseApp") } - app.msgFeeCalculateHandler = msgFeeHandler + app.additionalMsgFeeHandler = msgFeeHandler } From 0922e21e08d47744fab4abaa49e26562eee3fe0f Mon Sep 17 00:00:00 2001 From: arnabmitra Date: Fri, 19 Nov 2021 09:52:29 -0700 Subject: [PATCH 05/13] Fixing comments on changed files. --- baseapp/options.go | 2 +- types/msgfee_handler.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/baseapp/options.go b/baseapp/options.go index 6b03e919c284..74c043f46b7e 100644 --- a/baseapp/options.go +++ b/baseapp/options.go @@ -238,7 +238,7 @@ func (app *BaseApp) SetInterfaceRegistry(registry types.InterfaceRegistry) { app.msgServiceRouter.SetInterfaceRegistry(registry) } -// SetKeeperHandler sets the Keepers needed by baseapp for msg fees. +// SetMsgFeeHandler sets the AdditionalMsgFeeHandler which if set will charge additional fee for a msgType(if configured via governance). func (app *BaseApp) SetMsgFeeHandler(msgFeeHandler sdk.AdditionalMsgFeeHandler) { if app.sealed { panic("SetKeeperHandler() on sealed BaseApp") diff --git a/types/msgfee_handler.go b/types/msgfee_handler.go index 50f1797b7f0a..f7f29622f162 100644 --- a/types/msgfee_handler.go +++ b/types/msgfee_handler.go @@ -1,4 +1,4 @@ package types +// AdditionalMsgFeeHandler optional, which if set will charge additional fee for a msgType(if configured via governance) type AdditionalMsgFeeHandler func(ctx Context, simulate bool) (coins Coins, err error) - From 1d9f2ea3976431533491c8f906bdd497702acc00 Mon Sep 17 00:00:00 2001 From: Carlton Hanna Date: Mon, 22 Nov 2021 15:17:57 -0700 Subject: [PATCH 06/13] Return transaction context from runTx --- baseapp/abci.go | 6 +++--- baseapp/baseapp.go | 12 ++++++------ baseapp/test_helpers.go | 8 +++++--- simapp/test_helpers.go | 2 +- x/auth/tx/service.go | 4 ++-- 5 files changed, 17 insertions(+), 15 deletions(-) diff --git a/baseapp/abci.go b/baseapp/abci.go index 3464a5d43688..de17b295a481 100644 --- a/baseapp/abci.go +++ b/baseapp/abci.go @@ -240,7 +240,7 @@ func (app *BaseApp) CheckTx(req abci.RequestCheckTx) abci.ResponseCheckTx { panic(fmt.Sprintf("unknown RequestCheckTx type: %s", req.Type)) } - gInfo, result, err := app.runTx(mode, req.Tx) + gInfo, result, err, _ := app.runTx(mode, req.Tx) if err != nil { return sdkerrors.ResponseCheckTx(err, gInfo.GasWanted, gInfo.GasUsed, app.trace) } @@ -272,7 +272,7 @@ func (app *BaseApp) DeliverTx(req abci.RequestDeliverTx) abci.ResponseDeliverTx telemetry.SetGauge(float32(gInfo.GasWanted), "tx", "gas", "wanted") }() - gInfo, result, err := app.runTx(runTxModeDeliver, req.Tx) + gInfo, result, err, _ := app.runTx(runTxModeDeliver, req.Tx) if err != nil { resultStr = "failed" return sdkerrors.ResponseDeliverTx(err, gInfo.GasWanted, gInfo.GasUsed, app.trace) @@ -744,7 +744,7 @@ func handleQueryApp(app *BaseApp, path []string, req abci.RequestQuery) abci.Res case "simulate": txBytes := req.Data - gInfo, res, err := app.Simulate(txBytes) + gInfo, res, err, _ := app.Simulate(txBytes) if err != nil { return sdkerrors.QueryResult(sdkerrors.Wrap(err, "failed to simulate tx")) } diff --git a/baseapp/baseapp.go b/baseapp/baseapp.go index fdcf1b9219a5..a0d8717aef4a 100644 --- a/baseapp/baseapp.go +++ b/baseapp/baseapp.go @@ -581,7 +581,7 @@ func (app *BaseApp) cacheTxContext(ctx sdk.Context, txBytes []byte) (sdk.Context // Note, gas execution info is always returned. A reference to a Result is // returned if the tx does not run out of gas and if all the messages are valid // and execute successfully. An error is returned otherwise. -func (app *BaseApp) runTx(mode runTxMode, txBytes []byte) (gInfo sdk.GasInfo, result *sdk.Result, err error) { +func (app *BaseApp) runTx(mode runTxMode, txBytes []byte) (gInfo sdk.GasInfo, result *sdk.Result, err error, txCtx sdk.Context) { // NOTE: GasWanted should be returned by the AnteHandler. GasUsed is // determined by the GasMeter. We need access to the context to get the gas // meter so we initialize upfront. @@ -593,7 +593,7 @@ func (app *BaseApp) runTx(mode runTxMode, txBytes []byte) (gInfo sdk.GasInfo, re // only run the tx if there is block gas remaining if mode == runTxModeDeliver && ctx.BlockGasMeter().IsOutOfGas() { gInfo = sdk.GasInfo{GasUsed: ctx.BlockGasMeter().GasConsumed()} - return gInfo, nil, sdkerrors.Wrap(sdkerrors.ErrOutOfGas, "no block gas left to run tx") + return gInfo, nil, sdkerrors.Wrap(sdkerrors.ErrOutOfGas, "no block gas left to run tx"), ctx } var startingGas uint64 @@ -629,12 +629,12 @@ func (app *BaseApp) runTx(mode runTxMode, txBytes []byte) (gInfo sdk.GasInfo, re tx, err := app.txDecoder(txBytes) if err != nil { - return sdk.GasInfo{}, nil, err + return sdk.GasInfo{}, nil, err, ctx } msgs := tx.GetMsgs() if err := validateBasicTxMsgs(msgs); err != nil { - return sdk.GasInfo{}, nil, err + return sdk.GasInfo{}, nil, err, ctx } var events sdk.Events @@ -671,7 +671,7 @@ func (app *BaseApp) runTx(mode runTxMode, txBytes []byte) (gInfo sdk.GasInfo, re gasWanted = ctx.GasMeter().Limit() if err != nil { - return gInfo, nil, err + return gInfo, nil, err, ctx } msCache.Write() @@ -701,7 +701,7 @@ func (app *BaseApp) runTx(mode runTxMode, txBytes []byte) (gInfo sdk.GasInfo, re } } - return gInfo, result, err + return gInfo, result, err, ctx } // runMsgs iterates through a list of messages and executes them with the provided diff --git a/baseapp/test_helpers.go b/baseapp/test_helpers.go index 407ebd9a7cd9..bf65abb90923 100644 --- a/baseapp/test_helpers.go +++ b/baseapp/test_helpers.go @@ -15,10 +15,11 @@ func (app *BaseApp) Check(txEncoder sdk.TxEncoder, tx sdk.Tx) (sdk.GasInfo, *sdk if err != nil { return sdk.GasInfo{}, nil, sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "%s", err) } - return app.runTx(runTxModeCheck, bz) + gasInfo, result, err, _ := app.runTx(runTxModeCheck, bz) + return gasInfo, result, err } -func (app *BaseApp) Simulate(txBytes []byte) (sdk.GasInfo, *sdk.Result, error) { +func (app *BaseApp) Simulate(txBytes []byte) (sdk.GasInfo, *sdk.Result, error, sdk.Context) { return app.runTx(runTxModeSimulate, txBytes) } @@ -28,7 +29,8 @@ func (app *BaseApp) Deliver(txEncoder sdk.TxEncoder, tx sdk.Tx) (sdk.GasInfo, *s if err != nil { return sdk.GasInfo{}, nil, sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "%s", err) } - return app.runTx(runTxModeDeliver, bz) + gasInfo, result, err, _ := app.runTx(runTxModeDeliver, bz) + return gasInfo, result, err } // Context with current {check, deliver}State of the app used by tests. diff --git a/simapp/test_helpers.go b/simapp/test_helpers.go index 4f45b23eacd2..bb3467ad0fd3 100644 --- a/simapp/test_helpers.go +++ b/simapp/test_helpers.go @@ -341,7 +341,7 @@ func SignCheckDeliver( require.Nil(t, err) // Must simulate now as CheckTx doesn't run Msgs anymore - _, res, err := app.Simulate(txBytes) + _, res, err, _ := app.Simulate(txBytes) if expSimPass { require.NoError(t, err) diff --git a/x/auth/tx/service.go b/x/auth/tx/service.go index a0deedae6aa2..8c9cff33dfb4 100644 --- a/x/auth/tx/service.go +++ b/x/auth/tx/service.go @@ -19,7 +19,7 @@ import ( ) // baseAppSimulateFn is the signature of the Baseapp#Simulate function. -type baseAppSimulateFn func(txBytes []byte) (sdk.GasInfo, *sdk.Result, error) +type baseAppSimulateFn func(txBytes []byte) (sdk.GasInfo, *sdk.Result, error, sdk.Context) // txServer is the server for the protobuf Tx service. type txServer struct { @@ -114,7 +114,7 @@ func (s txServer) Simulate(ctx context.Context, req *txtypes.SimulateRequest) (* return nil, status.Errorf(codes.InvalidArgument, "empty txBytes is not allowed") } - gasInfo, result, err := s.simulate(txBytes) + gasInfo, result, err, _ := s.simulate(txBytes) if err != nil { return nil, err } From 378b0dfed31f1b2bc418b72671c2fbde24579ff3 Mon Sep 17 00:00:00 2001 From: arnabmitra Date: Tue, 7 Dec 2021 20:12:29 -0700 Subject: [PATCH 07/13] emitting events from AdditionalMsgFeeHandler, since authz and wasm calls need the event too. --- baseapp/baseapp.go | 18 ++++++++++++++---- types/msgfee_handler.go | 2 +- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/baseapp/baseapp.go b/baseapp/baseapp.go index a0d8717aef4a..23f8c96aa10b 100644 --- a/baseapp/baseapp.go +++ b/baseapp/baseapp.go @@ -688,10 +688,7 @@ func (app *BaseApp) runTx(mode runTxMode, txBytes []byte) (gInfo sdk.GasInfo, re result, err = app.runMsgs(runMsgCtx, msgs, mode) if err == nil && mode == runTxModeDeliver { // charge additional fee if logic calls for it - if app.additionalMsgFeeHandler != nil { - // call the msgFee - app.additionalMsgFeeHandler(runMsgCtx, mode == runTxModeSimulate) - } + events = AdditionalMsgBasedFeeInvoke(mode, app, runMsgCtx, events) msCache.Write() @@ -704,6 +701,19 @@ func (app *BaseApp) runTx(mode runTxMode, txBytes []byte) (gInfo sdk.GasInfo, re return gInfo, result, err, ctx } +// AdditionalMsgBasedFeeInvoke charge additional fee if logic calls for it. This method is Provenance specific. +func AdditionalMsgBasedFeeInvoke(mode runTxMode, app *BaseApp, runMsgCtx sdk.Context, events sdk.Events) sdk.Events { + if app.additionalMsgFeeHandler != nil { + // call the msgFee + _, eventsFromAdditionalFeeHandler, _ := app.additionalMsgFeeHandler(runMsgCtx, mode == runTxModeSimulate) + // append any events emitted by AdditionalMsgFeeHandler + if events != nil && len(events) > 0 { + events = events.AppendEvents(eventsFromAdditionalFeeHandler) + } + } + return events +} + // runMsgs iterates through a list of messages and executes them with the provided // Context and execution mode. Messages will only be executed during simulation // and DeliverTx. An error is returned if any single message fails or if a diff --git a/types/msgfee_handler.go b/types/msgfee_handler.go index f7f29622f162..239d7da9bd01 100644 --- a/types/msgfee_handler.go +++ b/types/msgfee_handler.go @@ -1,4 +1,4 @@ package types // AdditionalMsgFeeHandler optional, which if set will charge additional fee for a msgType(if configured via governance) -type AdditionalMsgFeeHandler func(ctx Context, simulate bool) (coins Coins, err error) +type AdditionalMsgFeeHandler func(ctx Context, simulate bool) (coins Coins, events Events, err error) From 85d189f0719caabdb52277d1e8ecc5db23533c89 Mon Sep 17 00:00:00 2001 From: arnabmitra Date: Tue, 7 Dec 2021 20:49:51 -0700 Subject: [PATCH 08/13] test: Fix comppile issues on existing tests --- baseapp/baseapp_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/baseapp/baseapp_test.go b/baseapp/baseapp_test.go index ff307b105ff0..4d6b20fb6672 100644 --- a/baseapp/baseapp_test.go +++ b/baseapp/baseapp_test.go @@ -1131,13 +1131,13 @@ func TestSimulateTx(t *testing.T) { require.Nil(t, err) // simulate a message, check gas reported - gInfo, result, err := app.Simulate(txBytes) + gInfo, result, err, _ := app.Simulate(txBytes) require.NoError(t, err) require.NotNil(t, result) require.Equal(t, gasConsumed, gInfo.GasUsed) // simulate again, same result - gInfo, result, err = app.Simulate(txBytes) + gInfo, result, err, _ = app.Simulate(txBytes) require.NoError(t, err) require.NotNil(t, result) require.Equal(t, gasConsumed, gInfo.GasUsed) From 3817fa1aeb9a93c39f190be713c7b16040fa094b Mon Sep 17 00:00:00 2001 From: arnabmitra Date: Fri, 10 Dec 2021 16:29:12 -0700 Subject: [PATCH 09/13] handling error on failure to charge additional fee. --- baseapp/baseapp.go | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/baseapp/baseapp.go b/baseapp/baseapp.go index 23f8c96aa10b..f8d2adb3e1f7 100644 --- a/baseapp/baseapp.go +++ b/baseapp/baseapp.go @@ -688,13 +688,14 @@ func (app *BaseApp) runTx(mode runTxMode, txBytes []byte) (gInfo sdk.GasInfo, re result, err = app.runMsgs(runMsgCtx, msgs, mode) if err == nil && mode == runTxModeDeliver { // charge additional fee if logic calls for it - events = AdditionalMsgBasedFeeInvoke(mode, app, runMsgCtx, events) - - msCache.Write() - - if len(events) > 0 { - // append the events in the order of occurrence - result.Events = append(events.ToABCIEvents(), result.Events...) + events, err = AdditionalMsgBasedFeeInvoke(mode, app, runMsgCtx, events) + // if err from AdditionalMsgBasedFeeInvoke then don't write to cache + if err != nil { + msCache.Write() + if len(events) > 0 { + // append the events in the order of occurrence + result.Events = append(events.ToABCIEvents(), result.Events...) + } } } @@ -702,16 +703,19 @@ func (app *BaseApp) runTx(mode runTxMode, txBytes []byte) (gInfo sdk.GasInfo, re } // AdditionalMsgBasedFeeInvoke charge additional fee if logic calls for it. This method is Provenance specific. -func AdditionalMsgBasedFeeInvoke(mode runTxMode, app *BaseApp, runMsgCtx sdk.Context, events sdk.Events) sdk.Events { +func AdditionalMsgBasedFeeInvoke(mode runTxMode, app *BaseApp, runMsgCtx sdk.Context, events sdk.Events) (sdk.Events, error) { if app.additionalMsgFeeHandler != nil { // call the msgFee - _, eventsFromAdditionalFeeHandler, _ := app.additionalMsgFeeHandler(runMsgCtx, mode == runTxModeSimulate) + _, eventsFromAdditionalFeeHandler, err := app.additionalMsgFeeHandler(runMsgCtx, mode == runTxModeSimulate) + if err != nil { + return nil, err + } // append any events emitted by AdditionalMsgFeeHandler if events != nil && len(events) > 0 { events = events.AppendEvents(eventsFromAdditionalFeeHandler) } } - return events + return events, nil } // runMsgs iterates through a list of messages and executes them with the provided From c84ec4f5ba7b731ca5c7ec52152d338da50c581a Mon Sep 17 00:00:00 2001 From: arnabmitra Date: Fri, 10 Dec 2021 17:52:35 -0700 Subject: [PATCH 10/13] fixing err while deliverTx. --- baseapp/baseapp.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/baseapp/baseapp.go b/baseapp/baseapp.go index f8d2adb3e1f7..ea4b2a71f512 100644 --- a/baseapp/baseapp.go +++ b/baseapp/baseapp.go @@ -690,7 +690,7 @@ func (app *BaseApp) runTx(mode runTxMode, txBytes []byte) (gInfo sdk.GasInfo, re // charge additional fee if logic calls for it events, err = AdditionalMsgBasedFeeInvoke(mode, app, runMsgCtx, events) // if err from AdditionalMsgBasedFeeInvoke then don't write to cache - if err != nil { + if err == nil { msCache.Write() if len(events) > 0 { // append the events in the order of occurrence From a4e5f0a68fead6ca1da36a0b2fcc7d1bd556d158 Mon Sep 17 00:00:00 2001 From: Carlton Hanna Date: Fri, 17 Dec 2021 10:30:40 -0700 Subject: [PATCH 11/13] Update return values to have err at end --- baseapp/abci.go | 6 +++--- baseapp/baseapp.go | 13 ++++++------- baseapp/test_helpers.go | 6 +++--- simapp/test_helpers.go | 2 +- x/auth/tx/service.go | 4 ++-- 5 files changed, 15 insertions(+), 16 deletions(-) diff --git a/baseapp/abci.go b/baseapp/abci.go index de17b295a481..6b71ffc4abd5 100644 --- a/baseapp/abci.go +++ b/baseapp/abci.go @@ -240,7 +240,7 @@ func (app *BaseApp) CheckTx(req abci.RequestCheckTx) abci.ResponseCheckTx { panic(fmt.Sprintf("unknown RequestCheckTx type: %s", req.Type)) } - gInfo, result, err, _ := app.runTx(mode, req.Tx) + gInfo, result, _, err := app.runTx(mode, req.Tx) if err != nil { return sdkerrors.ResponseCheckTx(err, gInfo.GasWanted, gInfo.GasUsed, app.trace) } @@ -272,7 +272,7 @@ func (app *BaseApp) DeliverTx(req abci.RequestDeliverTx) abci.ResponseDeliverTx telemetry.SetGauge(float32(gInfo.GasWanted), "tx", "gas", "wanted") }() - gInfo, result, err, _ := app.runTx(runTxModeDeliver, req.Tx) + gInfo, result, _, err := app.runTx(runTxModeDeliver, req.Tx) if err != nil { resultStr = "failed" return sdkerrors.ResponseDeliverTx(err, gInfo.GasWanted, gInfo.GasUsed, app.trace) @@ -744,7 +744,7 @@ func handleQueryApp(app *BaseApp, path []string, req abci.RequestQuery) abci.Res case "simulate": txBytes := req.Data - gInfo, res, err, _ := app.Simulate(txBytes) + gInfo, res, _, err := app.Simulate(txBytes) if err != nil { return sdkerrors.QueryResult(sdkerrors.Wrap(err, "failed to simulate tx")) } diff --git a/baseapp/baseapp.go b/baseapp/baseapp.go index ea4b2a71f512..753eab24d38a 100644 --- a/baseapp/baseapp.go +++ b/baseapp/baseapp.go @@ -135,7 +135,6 @@ type BaseApp struct { // nolint: maligned indexEvents map[string]struct{} // msg fee handler - // custom fee handler for provenance, which will calculate and charge fee(if not run in simulation mode) additionalMsgFeeHandler sdk.AdditionalMsgFeeHandler // ante handler for fee and auth } @@ -581,7 +580,7 @@ func (app *BaseApp) cacheTxContext(ctx sdk.Context, txBytes []byte) (sdk.Context // Note, gas execution info is always returned. A reference to a Result is // returned if the tx does not run out of gas and if all the messages are valid // and execute successfully. An error is returned otherwise. -func (app *BaseApp) runTx(mode runTxMode, txBytes []byte) (gInfo sdk.GasInfo, result *sdk.Result, err error, txCtx sdk.Context) { +func (app *BaseApp) runTx(mode runTxMode, txBytes []byte) (gInfo sdk.GasInfo, result *sdk.Result, txCtx sdk.Context, err error) { // NOTE: GasWanted should be returned by the AnteHandler. GasUsed is // determined by the GasMeter. We need access to the context to get the gas // meter so we initialize upfront. @@ -593,7 +592,7 @@ func (app *BaseApp) runTx(mode runTxMode, txBytes []byte) (gInfo sdk.GasInfo, re // only run the tx if there is block gas remaining if mode == runTxModeDeliver && ctx.BlockGasMeter().IsOutOfGas() { gInfo = sdk.GasInfo{GasUsed: ctx.BlockGasMeter().GasConsumed()} - return gInfo, nil, sdkerrors.Wrap(sdkerrors.ErrOutOfGas, "no block gas left to run tx"), ctx + return gInfo, nil, ctx, sdkerrors.Wrap(sdkerrors.ErrOutOfGas, "no block gas left to run tx") } var startingGas uint64 @@ -629,12 +628,12 @@ func (app *BaseApp) runTx(mode runTxMode, txBytes []byte) (gInfo sdk.GasInfo, re tx, err := app.txDecoder(txBytes) if err != nil { - return sdk.GasInfo{}, nil, err, ctx + return sdk.GasInfo{}, nil, ctx, err } msgs := tx.GetMsgs() if err := validateBasicTxMsgs(msgs); err != nil { - return sdk.GasInfo{}, nil, err, ctx + return sdk.GasInfo{}, nil, ctx, err } var events sdk.Events @@ -671,7 +670,7 @@ func (app *BaseApp) runTx(mode runTxMode, txBytes []byte) (gInfo sdk.GasInfo, re gasWanted = ctx.GasMeter().Limit() if err != nil { - return gInfo, nil, err, ctx + return gInfo, nil, ctx, err } msCache.Write() @@ -699,7 +698,7 @@ func (app *BaseApp) runTx(mode runTxMode, txBytes []byte) (gInfo sdk.GasInfo, re } } - return gInfo, result, err, ctx + return gInfo, result, ctx, err } // AdditionalMsgBasedFeeInvoke charge additional fee if logic calls for it. This method is Provenance specific. diff --git a/baseapp/test_helpers.go b/baseapp/test_helpers.go index bf65abb90923..3c21ad44dce0 100644 --- a/baseapp/test_helpers.go +++ b/baseapp/test_helpers.go @@ -15,11 +15,11 @@ func (app *BaseApp) Check(txEncoder sdk.TxEncoder, tx sdk.Tx) (sdk.GasInfo, *sdk if err != nil { return sdk.GasInfo{}, nil, sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "%s", err) } - gasInfo, result, err, _ := app.runTx(runTxModeCheck, bz) + gasInfo, result, _, err := app.runTx(runTxModeCheck, bz) return gasInfo, result, err } -func (app *BaseApp) Simulate(txBytes []byte) (sdk.GasInfo, *sdk.Result, error, sdk.Context) { +func (app *BaseApp) Simulate(txBytes []byte) (sdk.GasInfo, *sdk.Result, sdk.Context, error) { return app.runTx(runTxModeSimulate, txBytes) } @@ -29,7 +29,7 @@ func (app *BaseApp) Deliver(txEncoder sdk.TxEncoder, tx sdk.Tx) (sdk.GasInfo, *s if err != nil { return sdk.GasInfo{}, nil, sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "%s", err) } - gasInfo, result, err, _ := app.runTx(runTxModeDeliver, bz) + gasInfo, result, _, err := app.runTx(runTxModeDeliver, bz) return gasInfo, result, err } diff --git a/simapp/test_helpers.go b/simapp/test_helpers.go index bb3467ad0fd3..3b07d7f45af5 100644 --- a/simapp/test_helpers.go +++ b/simapp/test_helpers.go @@ -341,7 +341,7 @@ func SignCheckDeliver( require.Nil(t, err) // Must simulate now as CheckTx doesn't run Msgs anymore - _, res, err, _ := app.Simulate(txBytes) + _, res, _, err := app.Simulate(txBytes) if expSimPass { require.NoError(t, err) diff --git a/x/auth/tx/service.go b/x/auth/tx/service.go index 8c9cff33dfb4..2c5e617793df 100644 --- a/x/auth/tx/service.go +++ b/x/auth/tx/service.go @@ -19,7 +19,7 @@ import ( ) // baseAppSimulateFn is the signature of the Baseapp#Simulate function. -type baseAppSimulateFn func(txBytes []byte) (sdk.GasInfo, *sdk.Result, error, sdk.Context) +type baseAppSimulateFn func(txBytes []byte) (sdk.GasInfo, *sdk.Result, sdk.Context, error) // txServer is the server for the protobuf Tx service. type txServer struct { @@ -114,7 +114,7 @@ func (s txServer) Simulate(ctx context.Context, req *txtypes.SimulateRequest) (* return nil, status.Errorf(codes.InvalidArgument, "empty txBytes is not allowed") } - gasInfo, result, err, _ := s.simulate(txBytes) + gasInfo, result, _, err := s.simulate(txBytes) if err != nil { return nil, err } From 28f7e261f5e34cfefc1d97642ac8041a8e5cbd3a Mon Sep 17 00:00:00 2001 From: Carlton Hanna Date: Fri, 17 Dec 2021 10:48:52 -0700 Subject: [PATCH 12/13] Fix tests --- baseapp/baseapp_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/baseapp/baseapp_test.go b/baseapp/baseapp_test.go index 4d6b20fb6672..4704efbf3f5a 100644 --- a/baseapp/baseapp_test.go +++ b/baseapp/baseapp_test.go @@ -1131,13 +1131,13 @@ func TestSimulateTx(t *testing.T) { require.Nil(t, err) // simulate a message, check gas reported - gInfo, result, err, _ := app.Simulate(txBytes) + gInfo, result, _, err := app.Simulate(txBytes) require.NoError(t, err) require.NotNil(t, result) require.Equal(t, gasConsumed, gInfo.GasUsed) // simulate again, same result - gInfo, result, err, _ = app.Simulate(txBytes) + gInfo, result, _, err = app.Simulate(txBytes) require.NoError(t, err) require.NotNil(t, result) require.Equal(t, gasConsumed, gInfo.GasUsed) From fa7e5f240ae0125fa239adeac4ce69824197aa4e Mon Sep 17 00:00:00 2001 From: Carlton Hanna Date: Wed, 22 Dec 2021 15:29:25 -0700 Subject: [PATCH 13/13] Refactor to remove pio specific stuff --- baseapp/baseapp.go | 22 +++++++++++----------- baseapp/options.go | 6 +++--- types/fee_handler.go | 4 ++++ types/msgfee_handler.go | 4 ---- 4 files changed, 18 insertions(+), 18 deletions(-) create mode 100644 types/fee_handler.go delete mode 100644 types/msgfee_handler.go diff --git a/baseapp/baseapp.go b/baseapp/baseapp.go index 753eab24d38a..bfd515bcc336 100644 --- a/baseapp/baseapp.go +++ b/baseapp/baseapp.go @@ -134,8 +134,8 @@ type BaseApp struct { // nolint: maligned // which informs Tendermint what to index. If empty, all events will be indexed. indexEvents map[string]struct{} - // msg fee handler - additionalMsgFeeHandler sdk.AdditionalMsgFeeHandler // ante handler for fee and auth + // fee handler + feeHandler sdk.FeeHandler // ante handler for fee and auth } // NewBaseApp returns a reference to an initialized BaseApp. It accepts a @@ -686,9 +686,9 @@ func (app *BaseApp) runTx(mode runTxMode, txBytes []byte) (gInfo sdk.GasInfo, re // Result if any single message fails or does not have a registered Handler. result, err = app.runMsgs(runMsgCtx, msgs, mode) if err == nil && mode == runTxModeDeliver { - // charge additional fee if logic calls for it - events, err = AdditionalMsgBasedFeeInvoke(mode, app, runMsgCtx, events) - // if err from AdditionalMsgBasedFeeInvoke then don't write to cache + // apply fee logic calls + events, err = FeeInvoke(mode, app, runMsgCtx, events) + // if err from FeeInvoke then don't write to cache if err == nil { msCache.Write() if len(events) > 0 { @@ -701,17 +701,17 @@ func (app *BaseApp) runTx(mode runTxMode, txBytes []byte) (gInfo sdk.GasInfo, re return gInfo, result, ctx, err } -// AdditionalMsgBasedFeeInvoke charge additional fee if logic calls for it. This method is Provenance specific. -func AdditionalMsgBasedFeeInvoke(mode runTxMode, app *BaseApp, runMsgCtx sdk.Context, events sdk.Events) (sdk.Events, error) { - if app.additionalMsgFeeHandler != nil { +// FeeInvoke apply fee logic and append events +func FeeInvoke(mode runTxMode, app *BaseApp, runMsgCtx sdk.Context, events sdk.Events) (sdk.Events, error) { + if app.feeHandler != nil { // call the msgFee - _, eventsFromAdditionalFeeHandler, err := app.additionalMsgFeeHandler(runMsgCtx, mode == runTxModeSimulate) + _, eventsFromFeeHandler, err := app.feeHandler(runMsgCtx, mode == runTxModeSimulate) if err != nil { return nil, err } - // append any events emitted by AdditionalMsgFeeHandler + // append any events emitted by FeeHandler if events != nil && len(events) > 0 { - events = events.AppendEvents(eventsFromAdditionalFeeHandler) + events = events.AppendEvents(eventsFromFeeHandler) } } return events, nil diff --git a/baseapp/options.go b/baseapp/options.go index 74c043f46b7e..94d06c7409f9 100644 --- a/baseapp/options.go +++ b/baseapp/options.go @@ -238,11 +238,11 @@ func (app *BaseApp) SetInterfaceRegistry(registry types.InterfaceRegistry) { app.msgServiceRouter.SetInterfaceRegistry(registry) } -// SetMsgFeeHandler sets the AdditionalMsgFeeHandler which if set will charge additional fee for a msgType(if configured via governance). -func (app *BaseApp) SetMsgFeeHandler(msgFeeHandler sdk.AdditionalMsgFeeHandler) { +// SetFeeHandler sets the FeeHandler which if set will change the behavior of fee handling +func (app *BaseApp) SetFeeHandler(feeHandler sdk.FeeHandler) { if app.sealed { panic("SetKeeperHandler() on sealed BaseApp") } - app.additionalMsgFeeHandler = msgFeeHandler + app.feeHandler = feeHandler } diff --git a/types/fee_handler.go b/types/fee_handler.go new file mode 100644 index 000000000000..7f1a61363e93 --- /dev/null +++ b/types/fee_handler.go @@ -0,0 +1,4 @@ +package types + +// FeeHandler optional custom fee handling implementations +type FeeHandler func(ctx Context, simulate bool) (coins Coins, events Events, err error) diff --git a/types/msgfee_handler.go b/types/msgfee_handler.go deleted file mode 100644 index 239d7da9bd01..000000000000 --- a/types/msgfee_handler.go +++ /dev/null @@ -1,4 +0,0 @@ -package types - -// AdditionalMsgFeeHandler optional, which if set will charge additional fee for a msgType(if configured via governance) -type AdditionalMsgFeeHandler func(ctx Context, simulate bool) (coins Coins, events Events, err error)