Skip to content

Commit

Permalink
temporarily readded antehandler changes
Browse files Browse the repository at this point in the history
  • Loading branch information
avendauz committed Aug 14, 2021
1 parent cef4947 commit 2e4c687
Show file tree
Hide file tree
Showing 10 changed files with 1,178 additions and 2,407 deletions.
48 changes: 42 additions & 6 deletions app/ante/NewSetUpContextDecorator.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ package ante
import (
"fmt"
"github.com/bluzelle/curium/app/ante/gasmeter"
"github.com/bluzelle/curium/x/crud"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/x/auth/ante"
acctypes "github.com/cosmos/cosmos-sdk/x/auth/keeper"
"github.com/cosmos/cosmos-sdk/x/auth/types"
banktypes "github.com/cosmos/cosmos-sdk/x/auth/types"
"strings"
)

var (
Expand All @@ -30,14 +32,16 @@ type SetUpContextDecorator struct{
gasMeterKeeper *gasmeter.GasMeterKeeper
supplyKeeper banktypes.SupplyKeeper
accountKeeper acctypes.AccountKeeper
crudKeeper crud.Keeper
minGasPriceCoins sdk.DecCoins
}

func NewSetUpContextDecorator(gasMeterKeeper *gasmeter.GasMeterKeeper, supplyKeeper banktypes.SupplyKeeper, accountKeeper acctypes.AccountKeeper, minGasPriceCoins sdk.DecCoins) SetUpContextDecorator {
func NewSetUpContextDecorator(gasMeterKeeper *gasmeter.GasMeterKeeper, supplyKeeper banktypes.SupplyKeeper, accountKeeper acctypes.AccountKeeper, crudKeeper crud.Keeper, minGasPriceCoins sdk.DecCoins) SetUpContextDecorator {
return SetUpContextDecorator{
gasMeterKeeper: gasMeterKeeper,
supplyKeeper: supplyKeeper,
accountKeeper: accountKeeper,
crudKeeper: crudKeeper,
minGasPriceCoins: minGasPriceCoins,
}
}
Expand All @@ -51,11 +55,11 @@ func (sud SetUpContextDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate
if !ok {
// Set a gas meter with limit 0 as to prevent an infinite gas meter attack
// during runTx.
newCtx, _ = SetGasMeter(simulate, ctx, 0, tx, sud.gasMeterKeeper, sud.supplyKeeper, sud.accountKeeper, sud.minGasPriceCoins)
newCtx, _ = SetGasMeter(simulate, ctx, 0, tx, sud.gasMeterKeeper, sud.supplyKeeper, sud.accountKeeper, sud.crudKeeper, sud.minGasPriceCoins)
return newCtx, sdkerrors.Wrap(sdkerrors.ErrTxDecode, "Tx must be GasTx")
}

newCtx, err = SetGasMeter(simulate, ctx, gasTx.GetGas(), tx, sud.gasMeterKeeper, sud.supplyKeeper, sud.accountKeeper, sud.minGasPriceCoins)
newCtx, err = SetGasMeter(simulate, ctx, gasTx.GetGas(), tx, sud.gasMeterKeeper, sud.supplyKeeper, sud.accountKeeper, sud.crudKeeper, sud.minGasPriceCoins)

if err != nil {
return ctx, err
Expand Down Expand Up @@ -85,7 +89,7 @@ func (sud SetUpContextDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate
}

// SetGasMeter returns a new context with a gas meter set from a given context.
func SetGasMeter(simulate bool, ctx sdk.Context, gasLimit uint64, tx sdk.Tx, gk *gasmeter.GasMeterKeeper, supplyKeeper banktypes.SupplyKeeper, accountKeeper acctypes.AccountKeeper, minGasPriceCoins sdk.DecCoins) (sdk.Context, error) {
func SetGasMeter(simulate bool, ctx sdk.Context, gasLimit uint64, tx sdk.Tx, gk *gasmeter.GasMeterKeeper, supplyKeeper banktypes.SupplyKeeper, accountKeeper acctypes.AccountKeeper, crudKeeper crud.Keeper, minGasPriceCoins sdk.DecCoins) (sdk.Context, error) {
// In various cases such as simulation and during the genesis block, we do not
// meter any gas utilization.
if simulate || ctx.BlockHeight() == 0 {
Expand All @@ -105,13 +109,23 @@ func SetGasMeter(simulate bool, ctx sdk.Context, gasLimit uint64, tx sdk.Tx, gk

feePayer := feeTx.FeePayer()

whiteList := getWhitelist(ctx, crudKeeper)

msgModule := tx.GetMsgs()[0].Route()


if gasPriceCoins.AmountOf("ubnt").LT(minGasPriceCoins.AmountOf("ubnt")) {
return ctx, sdkerrors.New("curium", 2, "Specified gas price too low")
}

msgModule := tx.GetMsgs()[0].Route()

if msgModule == "crud" && !simulate && !ctx.IsCheckTx() { //TODO msg module for nft
if isOnWhiteList(msgModule, feePayer.String(), whiteList) && !simulate && !ctx.IsCheckTx() {
gm := gasmeter.NewFreeGasMeter(gasLimit)
gk.AddGasMeter(&gm)
return ctx.WithGasMeter(gm), nil
}

if isAChargingModule(msgModule) && !simulate && !ctx.IsCheckTx() {
gm := gasmeter.NewChargingGasMeter(supplyKeeper, accountKeeper, gasLimit, feePayer, gasPriceCoins)

gk.AddGasMeter(&gm)
Expand All @@ -123,3 +137,25 @@ func SetGasMeter(simulate bool, ctx sdk.Context, gasLimit uint64, tx sdk.Tx, gk

return ctx.WithGasMeter(gm), nil
}

func getWhitelist (ctx sdk.Context, crudKeeper crud.Keeper) (string) {
store := crudKeeper.GetKVStore(ctx)
whiteListBlzValue := crudKeeper.GetValue(ctx, store, "bluzelle", "bluzelle")

if len(whiteListBlzValue.Value) == 0 {
return ""
}

return whiteListBlzValue.Value
}

func isOnWhiteList (msgModule string, sender string, whiteList string) bool {
onWhiteList := strings.Contains(whiteList, sender)
return isAChargingModule(msgModule) && onWhiteList
}

func isAChargingModule (msgModule string) bool {
return msgModule == "crud" || msgModule == "oracle" || msgModule == "nft"
}


4 changes: 3 additions & 1 deletion app/ante/ante.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ante

import (
"github.com/bluzelle/curium/app/ante/gasmeter"
"github.com/bluzelle/curium/x/crud"
"github.com/bluzelle/curium/x/tax"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth/ante"
Expand All @@ -15,12 +16,13 @@ func NewAnteHandler(
supplyKeeper types.SupplyKeeper,
taxKeeper tax.Keeper,
bankKeeper bank.Keeper,
crudKeeper crud.Keeper,
sigGasConsumer ante.SignatureVerificationGasConsumer,
gasMeterKeeper *gasmeter.GasMeterKeeper,
minGasPriceCoins sdk.DecCoins,
) sdk.AnteHandler {
return sdk.ChainAnteDecorators(
NewSetUpContextDecorator(gasMeterKeeper, supplyKeeper, accountKeeper, minGasPriceCoins), // outermost AnteDecorator. SetUpContext must be called first
NewSetUpContextDecorator(gasMeterKeeper, supplyKeeper, accountKeeper, crudKeeper, minGasPriceCoins), // outermost AnteDecorator. SetUpContext must be called first
NewMempoolFeeDecorator(),
ante.NewValidateBasicDecorator(),
ante.NewValidateMemoDecorator(accountKeeper),
Expand Down
1 change: 1 addition & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,7 @@ func addAnteHandler(app *CRUDApp) {
app.supplyKeeper,
app.taxKeeper,
app.bankKeeper,
app.crudKeeper,
auth.DefaultSigVerificationGasConsumer,
app.gasMeterKeeper,
minGasPriceCoins,
Expand Down
4 changes: 4 additions & 0 deletions test/integration/helpers/bluzelle-client.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {bluzelle, BluzelleConfig} from "bluzelle";
import {memoize} from 'lodash'
import {GasInfo} from "bluzelle";

export const getBzClient = memoize((config: Partial<BluzelleConfig> = {}) =>
bluzelle({
Expand All @@ -9,3 +10,6 @@ export const getBzClient = memoize((config: Partial<BluzelleConfig> = {}) =>
...config
})
);

export const defaultGasParams = (gasInfo: GasInfo = {}): GasInfo => ({gas_price: 10, max_gas: 100000000, ...gasInfo})

0 comments on commit 2e4c687

Please sign in to comment.