Skip to content

Commit

Permalink
Add new cosmos-sdk tag for msg based fee support, add fee handler, ad…
Browse files Browse the repository at this point in the history
…d aggregate events function
  • Loading branch information
nullpointer0x00 committed Mar 4, 2024
1 parent c59e54e commit 4828238
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 106 deletions.
29 changes: 14 additions & 15 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -1055,25 +1055,24 @@ func New(
}

app.SetAnteHandler(anteHandler)
// TODO[1760]: fee-handler: Add the msgfeehandler back to the app.
/*
msgFeeHandler, err := piohandlers.NewAdditionalMsgFeeHandler(piohandlers.PioBaseAppKeeperOptions{
AccountKeeper: app.AccountKeeper,
BankKeeper: app.BankKeeper,
FeegrantKeeper: app.FeeGrantKeeper,
MsgFeesKeeper: app.MsgFeesKeeper,
Decoder: encodingConfig.TxConfig.TxDecoder(),
})

if err != nil {
panic(err)
}
app.SetFeeHandler(msgFeeHandler)
*/
msgFeeHandler, err := piohandlers.NewAdditionalMsgFeeHandler(piohandlers.PioBaseAppKeeperOptions{
AccountKeeper: app.AccountKeeper,
BankKeeper: app.BankKeeper,
FeegrantKeeper: app.FeeGrantKeeper,
MsgFeesKeeper: app.MsgFeesKeeper,
Decoder: encodingConfig.TxConfig.TxDecoder(),
})

if err != nil {
panic(err)
}

app.SetFeeHandler(msgFeeHandler)

app.SetEndBlocker(app.EndBlocker)

// app.SetAggregateEventsFunc(piohandlers.AggregateEvents) // TODO[1760]: event-history
app.SetAggregateEventsFunc(piohandlers.AggregateEvents) // TODO[1760]: event-history

// Add upgrade plans for each release. This must be done before the baseapp seals via LoadLatestVersion() down below.
InstallCustomUpgradeHandlers(app)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ replace (
// TODO[1760]: wasm: Put this CosmWasm/wasmd replace back with an updated version (or delete it).
// github.com/CosmWasm/wasmd => github.com/provenance-io/wasmd v0.30.0-pio-5
// TODO[1760]: sdk: Put this replace back with an updated version of the sdk from our fork.
github.com/cosmos/cosmos-sdk => github.com/provenance-io/cosmos-sdk v0.50.5-nullpointer0x00-msg-router
github.com/cosmos/cosmos-sdk => github.com/provenance-io/cosmos-sdk v0.50.5-nullpointer0x00-msg-based-fee-support
// TODO[1760]: ibc: Put this ibc-go replace back with an updated version (or delete it).
// github.com/cosmos/ibc-go/v6 => github.com/provenance-io/ibc-go/v6 v6.2.0-pio-1
// dgrijalva/jwt-go is deprecated and doesn't receive security updates.
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -905,8 +905,8 @@ github.com/prometheus/procfs v0.1.3/go.mod h1:lV6e/gmhEcM9IjHGsFOCxxuZ+z1YqCvr4O
github.com/prometheus/procfs v0.3.0/go.mod h1:lV6e/gmhEcM9IjHGsFOCxxuZ+z1YqCvr4OA4YeYWdaU=
github.com/prometheus/procfs v0.12.0 h1:jluTpSng7V9hY0O2R9DzzJHYb2xULk9VTR1V1R/k6Bo=
github.com/prometheus/procfs v0.12.0/go.mod h1:pcuDEFsWDnvcgNzo4EEweacyhjeA9Zk3cnaOZAZEfOo=
github.com/provenance-io/cosmos-sdk v0.50.5-nullpointer0x00-msg-router h1:DznZMkfRr23Q4CZXR9WBgP8zfn9fQf5xiqK4kaQqZXs=
github.com/provenance-io/cosmos-sdk v0.50.5-nullpointer0x00-msg-router/go.mod h1:UbShFs6P8Ly29xxJvkNGaNaL/UGj5a686NRtb1Cqra0=
github.com/provenance-io/cosmos-sdk v0.50.5-nullpointer0x00-msg-based-fee-support h1:eVD2/kZ4KJN34a+EXZocVmNaROjMVnVE0BAo4rAxRYg=
github.com/provenance-io/cosmos-sdk v0.50.5-nullpointer0x00-msg-based-fee-support/go.mod h1:UbShFs6P8Ly29xxJvkNGaNaL/UGj5a686NRtb1Cqra0=
github.com/rakyll/statik v0.1.7 h1:OF3QCZUuyPxuGEP7B4ypUa7sB/iHtqOTDYZXGM8KOdQ=
github.com/rakyll/statik v0.1.7/go.mod h1:AlZONWzMtEnMs7W4e/1LURLiI49pIMmp6V9Unghqrcc=
github.com/rcrowley/go-metrics v0.0.0-20181016184325-3113b8401b8a/go.mod h1:bCqnVzQkZxMG4s8nGwiZ5l3QUCyqpo9Y+/ZMZ9VjZe4=
Expand Down
4 changes: 1 addition & 3 deletions internal/handlers/msg_fee_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package handlers

import (
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"

msgfeestypes "github.com/provenance-io/provenance/x/msgfees/types"
Expand All @@ -15,8 +16,6 @@ type PioBaseAppKeeperOptions struct {
Decoder sdk.TxDecoder
}

// TODO[1760]: fee-handler: Uncomment NewAdditionalMsgFeeHandler once the FeeHandler type is back in the SDK.
/*
func NewAdditionalMsgFeeHandler(options PioBaseAppKeeperOptions) (sdk.FeeHandler, error) {
if options.AccountKeeper == nil {
return nil, sdkerrors.ErrLogic.Wrap("account keeper is required for AdditionalMsgFeeHandler builder")
Expand All @@ -41,4 +40,3 @@ func NewAdditionalMsgFeeHandler(options PioBaseAppKeeperOptions) (sdk.FeeHandler
return NewMsgFeeInvoker(options.BankKeeper, options.AccountKeeper, options.FeegrantKeeper,
options.MsgFeesKeeper, options.Decoder).Invoke, nil
}
*/
3 changes: 0 additions & 3 deletions internal/handlers/msg_fee_handler_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package handlers_test

// TODO[1760]: fee-handler: Uncomment these tests.
/*
import (
sdk "github.com/cosmos/cosmos-sdk/types"

Expand Down Expand Up @@ -69,4 +67,3 @@ func (s *HandlerTestSuite) TestMsgFeeHandlerSetUpIncorrect() {
})
s.Require().Error(err)
}
*/
157 changes: 75 additions & 82 deletions internal/handlers/msg_fee_invoker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

simapp "github.com/provenance-io/provenance/app"
"github.com/provenance-io/provenance/internal/antewrapper"
piohandlers "github.com/provenance-io/provenance/internal/handlers"
"github.com/provenance-io/provenance/internal/pioconfig"
msgfeetype "github.com/provenance-io/provenance/x/msgfees/types"
)
Expand All @@ -47,35 +48,31 @@ func (s *HandlerTestSuite) TestMsgFeeHandlerFeeChargedNoRemainingBaseFee() {
feeGasMeter.ConsumeBaseFee(sdk.Coins{sdk.NewInt64Coin("atom", 100_000)})
}, "panicked on adding fees")
s.ctx = s.ctx.WithGasMeter(feeGasMeter)
// TODO[1760]: fee-handler: Uncomment all this once the FeeHandler type is back in the SDK.
_ = acct1
/*
feeChargeFn, err := piohandlers.NewAdditionalMsgFeeHandler(piohandlers.PioBaseAppKeeperOptions{
AccountKeeper: s.app.AccountKeeper,
BankKeeper: s.app.BankKeeper,
FeegrantKeeper: s.app.FeeGrantKeeper,
MsgFeesKeeper: s.app.MsgFeesKeeper,
Decoder: encodingConfig.TxConfig.TxDecoder(),
})
s.Require().NoError(err)
coins, _, err := feeChargeFn(s.ctx, false)
s.Require().ErrorContains(err, "spendable balance 0nhash is smaller than 1000000nhash: insufficient funds", "feeChargeFn 1")
// fee gas meter has nothing to charge, so nothing should have been charged.
s.Require().True(coins.IsZero(), "coins.IsZero() 1")
s.Require().NoError(testutil.FundAccount(s.ctx, s.app.BankKeeper, acct1.GetAddress(), sdk.NewCoins(sdk.NewInt64Coin(NHash, 900_000))), "fund account")
coins, _, err = feeChargeFn(s.ctx, false)
s.Require().ErrorContains(err, "900000nhash is smaller than 1000000nhash: insufficient funds: insufficient funds", "feeChargeFn 2")
// fee gas meter has nothing to charge, so nothing should have been charged.
s.Require().True(coins.IsZero(), "coins.IsZero() 2")
s.Require().NoError(testutil.FundAccount(s.ctx, s.app.BankKeeper, acct1.GetAddress(), sdk.NewCoins(sdk.NewInt64Coin(NHash, 100_000))), "fund account again")
coins, _, err = feeChargeFn(s.ctx, false)
s.Require().NoError(err, "feeChargeFn 3")
// fee gas meter has nothing to charge, so nothing should have been charged.
s.Require().True(coins.IsAllGTE(sdk.Coins{sdk.NewInt64Coin(NHash, 1_000_000)}), "coins all gt 1000000nhash")
*/
feeChargeFn, err := piohandlers.NewAdditionalMsgFeeHandler(piohandlers.PioBaseAppKeeperOptions{
AccountKeeper: s.app.AccountKeeper,
BankKeeper: s.app.BankKeeper,
FeegrantKeeper: s.app.FeeGrantKeeper,
MsgFeesKeeper: s.app.MsgFeesKeeper,
Decoder: encodingConfig.TxConfig.TxDecoder(),
})
s.Require().NoError(err)
coins, _, err := feeChargeFn(s.ctx, false)

s.Require().ErrorContains(err, "spendable balance 0nhash is smaller than 1000000nhash: insufficient funds", "feeChargeFn 1")
// fee gas meter has nothing to charge, so nothing should have been charged.
s.Require().True(coins.IsZero(), "coins.IsZero() 1")

s.Require().NoError(testutil.FundAccount(s.ctx, s.app.BankKeeper, acct1.GetAddress(), sdk.NewCoins(sdk.NewInt64Coin(NHash, 900_000))), "fund account")
coins, _, err = feeChargeFn(s.ctx, false)
s.Require().ErrorContains(err, "900000nhash is smaller than 1000000nhash: insufficient funds: insufficient funds", "feeChargeFn 2")
// fee gas meter has nothing to charge, so nothing should have been charged.
s.Require().True(coins.IsZero(), "coins.IsZero() 2")

s.Require().NoError(testutil.FundAccount(s.ctx, s.app.BankKeeper, acct1.GetAddress(), sdk.NewCoins(sdk.NewInt64Coin(NHash, 100_000))), "fund account again")
coins, _, err = feeChargeFn(s.ctx, false)
s.Require().NoError(err, "feeChargeFn 3")
// fee gas meter has nothing to charge, so nothing should have been charged.
s.Require().True(coins.IsAllGTE(sdk.Coins{sdk.NewInt64Coin(NHash, 1_000_000)}), "coins all gt 1000000nhash")
}

func (s *HandlerTestSuite) TestMsgFeeHandlerFeeChargedWithRemainingBaseFee() {
Expand All @@ -96,32 +93,30 @@ func (s *HandlerTestSuite) TestMsgFeeHandlerFeeChargedWithRemainingBaseFee() {
feeGasMeter.ConsumeBaseFee(sdk.Coins{sdk.NewInt64Coin("atom", 100_000)}) // fee consumed at ante handler
}, "panicked on adding fees")
s.ctx = s.ctx.WithGasMeter(feeGasMeter)
// TODO[1760]: fee-handler: Uncomment all this once the FeeHandler type is back in the SDK.
_ = acct1
/*
feeChargeFn, err := piohandlers.NewAdditionalMsgFeeHandler(piohandlers.PioBaseAppKeeperOptions{
AccountKeeper: s.app.AccountKeeper,
BankKeeper: s.app.BankKeeper,
FeegrantKeeper: s.app.FeeGrantKeeper,
MsgFeesKeeper: s.app.MsgFeesKeeper,
Decoder: encodingConfig.TxConfig.TxDecoder(),
})
s.Require().NoError(err, "NewAdditionalMsgFeeHandler")
s.Require().NoError(testutil.FundAccount(s.ctx, s.app.BankKeeper, acct1.GetAddress(), sdk.NewCoins(sdk.NewInt64Coin(NHash, 1_000_000))), "funding account")
coins, _, err := feeChargeFn(s.ctx, false)
s.Require().ErrorContains(err, "spendable balance 0atom is smaller than 20000atom: insufficient funds", "feeChargeFn 1")
// fee gas meter has nothing to charge, so nothing should have been charged.
s.Require().True(coins.IsZero(), "coins.IsZero() 1")
s.Require().NoError(testutil.FundAccount(s.ctx, s.app.BankKeeper, acct1.GetAddress(), sdk.NewCoins(sdk.NewInt64Coin("atom", 20000), sdk.NewInt64Coin(NHash, 1000000))), "funding account again")
coins, _, err = feeChargeFn(s.ctx, false)
s.Require().Nil(err, "Got error when should have successfully paid all msg fees and swept remaining base fees")
s.Require().True(coins.Equal(sdk.Coins{sdk.NewInt64Coin(NHash, 1000000), sdk.NewInt64Coin("atom", 20000)}))
s.Require().NoError(err, "feeChargeFn 2")
expected := sdk.Coins{sdk.NewInt64Coin("atom", 20000), sdk.NewInt64Coin(NHash, 1000000)}
s.Require().Equal(expected, coins, "final coins")
*/

feeChargeFn, err := piohandlers.NewAdditionalMsgFeeHandler(piohandlers.PioBaseAppKeeperOptions{
AccountKeeper: s.app.AccountKeeper,
BankKeeper: s.app.BankKeeper,
FeegrantKeeper: s.app.FeeGrantKeeper,
MsgFeesKeeper: s.app.MsgFeesKeeper,
Decoder: encodingConfig.TxConfig.TxDecoder(),
})
s.Require().NoError(err, "NewAdditionalMsgFeeHandler")

s.Require().NoError(testutil.FundAccount(s.ctx, s.app.BankKeeper, acct1.GetAddress(), sdk.NewCoins(sdk.NewInt64Coin(NHash, 1_000_000))), "funding account")
coins, _, err := feeChargeFn(s.ctx, false)
s.Require().ErrorContains(err, "spendable balance 0atom is smaller than 20000atom: insufficient funds", "feeChargeFn 1")
// fee gas meter has nothing to charge, so nothing should have been charged.
s.Require().True(coins.IsZero(), "coins.IsZero() 1")

s.Require().NoError(testutil.FundAccount(s.ctx, s.app.BankKeeper, acct1.GetAddress(), sdk.NewCoins(sdk.NewInt64Coin("atom", 20000), sdk.NewInt64Coin(NHash, 1000000))), "funding account again")
coins, _, err = feeChargeFn(s.ctx, false)
s.Require().Nil(err, "Got error when should have successfully paid all msg fees and swept remaining base fees")
s.Require().True(coins.Equal(sdk.Coins{sdk.NewInt64Coin(NHash, 1000000), sdk.NewInt64Coin("atom", 20000)}))
s.Require().NoError(err, "feeChargeFn 2")
expected := sdk.Coins{sdk.NewInt64Coin("atom", 20000), sdk.NewInt64Coin(NHash, 1000000)}
s.Require().Equal(expected, coins, "final coins")

}

func (s *HandlerTestSuite) TestMsgFeeHandlerFeeChargedFeeGranter() {
Expand All @@ -142,21 +137,20 @@ func (s *HandlerTestSuite) TestMsgFeeHandlerFeeChargedFeeGranter() {
feeGasMeter.ConsumeBaseFee(sdk.Coins{sdk.NewInt64Coin("atom", 100_000)})
}, "panicked on adding fees")
s.ctx = s.ctx.WithGasMeter(feeGasMeter)
// TODO[1760]: fee-handler: Uncomment all this once the FeeHandler type is back in the SDK.
/*
feeChargeFn, err := piohandlers.NewAdditionalMsgFeeHandler(piohandlers.PioBaseAppKeeperOptions{
AccountKeeper: s.app.AccountKeeper,
BankKeeper: s.app.BankKeeper,
FeegrantKeeper: s.app.FeeGrantKeeper,
MsgFeesKeeper: s.app.MsgFeesKeeper,
Decoder: encodingConfig.TxConfig.TxDecoder(),
})
coins, _, err := feeChargeFn(s.ctx, false)
s.Require().Nil(err, "Got error when should not have.")
// fee gas meter has nothing to charge, so nothing should have been charged.
s.Require().True(coins.IsAllGTE(sdk.Coins{sdk.NewInt64Coin(NHash, 1000000)}))
*/

feeChargeFn, err := piohandlers.NewAdditionalMsgFeeHandler(piohandlers.PioBaseAppKeeperOptions{
AccountKeeper: s.app.AccountKeeper,
BankKeeper: s.app.BankKeeper,
FeegrantKeeper: s.app.FeeGrantKeeper,
MsgFeesKeeper: s.app.MsgFeesKeeper,
Decoder: encodingConfig.TxConfig.TxDecoder(),
})

coins, _, err := feeChargeFn(s.ctx, false)
s.Require().Nil(err, "Got error when should not have.")
// fee gas meter has nothing to charge, so nothing should have been charged.
s.Require().True(coins.IsAllGTE(sdk.Coins{sdk.NewInt64Coin(NHash, 1000000)}))

}

func (s *HandlerTestSuite) TestMsgFeeHandlerBadDecoder() {
Expand All @@ -172,18 +166,17 @@ func (s *HandlerTestSuite) TestMsgFeeHandlerBadDecoder() {
s.ctx = s.ctx.WithTxBytes(bz)
feeGasMeter := antewrapper.NewFeeGasMeterWrapper(log.NewTestLogger(s.T()), storetypes.NewGasMeter(100), false).(*antewrapper.FeeGasMeter)
s.ctx = s.ctx.WithGasMeter(feeGasMeter)
// TODO[1760]: fee-handler: Uncomment all this once the FeeHandler type is back in the SDK.
/*
feeChargeFn, err := piohandlers.NewAdditionalMsgFeeHandler(piohandlers.PioBaseAppKeeperOptions{
AccountKeeper: s.app.AccountKeeper,
BankKeeper: s.app.BankKeeper,
FeegrantKeeper: s.app.FeeGrantKeeper,
MsgFeesKeeper: s.app.MsgFeesKeeper,
Decoder: moduletestutil.MakeTestEncodingConfig().TxConfig.TxDecoder(),
})
s.Require().NoError(err)
s.Require().Panics(func() { feeChargeFn(s.ctx, false) }, "Bad decoder while setting up app.")
*/

feeChargeFn, err := piohandlers.NewAdditionalMsgFeeHandler(piohandlers.PioBaseAppKeeperOptions{
AccountKeeper: s.app.AccountKeeper,
BankKeeper: s.app.BankKeeper,
FeegrantKeeper: s.app.FeeGrantKeeper,
MsgFeesKeeper: s.app.MsgFeesKeeper,
Decoder: moduletestutil.MakeTestEncodingConfig().TxConfig.TxDecoder(),
})
s.Require().NoError(err)
s.Require().Panics(func() { feeChargeFn(s.ctx, false) }, "Bad decoder while setting up app.")

}

func setUpApp(s *HandlerTestSuite, additionalFeeCoinDenom string, additionalFeeCoinAmt int64) (moduletestutil.TestEncodingConfig, error) {
Expand Down

0 comments on commit 4828238

Please sign in to comment.