Skip to content

Commit

Permalink
review fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
swelf19 committed Sep 6, 2023
1 parent 788b4d7 commit c288fa9
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 44 deletions.
49 changes: 30 additions & 19 deletions app/upgrades/sdk47/upgrades.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,16 @@ import (
upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types"
v6 "github.com/cosmos/ibc-go/v7/modules/apps/27-interchain-accounts/controller/migrations/v6"
"github.com/neutron-org/neutron/app/upgrades"
contractmanagerkeeper "github.com/neutron-org/neutron/x/contractmanager/keeper"
contractmanagertypes "github.com/neutron-org/neutron/x/contractmanager/types"
crontypes "github.com/neutron-org/neutron/x/cron/types"
feeburnerkeeper "github.com/neutron-org/neutron/x/feeburner/keeper"
feeburnertypes "github.com/neutron-org/neutron/x/feeburner/types"
feerefundertypes "github.com/neutron-org/neutron/x/feerefunder/types"
icqtypes "github.com/neutron-org/neutron/x/interchainqueries/types"
interchaintxstypes "github.com/neutron-org/neutron/x/interchaintxs/types"
tokenfactorytypes "github.com/neutron-org/neutron/x/tokenfactory/types"
builderkeeper "github.com/skip-mev/pob/x/builder/keeper"
buildertypes "github.com/skip-mev/pob/x/builder/types"
)

Expand Down Expand Up @@ -73,30 +76,13 @@ func CreateUpgradeHandler(
}

ctx.Logger().Info("Setting pob params...")
treasury := keepers.FeeBurnerKeeper.GetParams(ctx).TreasuryAddress
_, data, err := bech32.DecodeAndConvert(treasury)
if err != nil {
return nil, err
}

builderParams := buildertypes.Params{
MaxBundleSize: 2,
EscrowAccountAddress: data,
ReserveFee: sdk.Coin{Denom: "untrn", Amount: sdk.NewInt(1_000_000)},
MinBidIncrement: sdk.Coin{Denom: "untrn", Amount: sdk.NewInt(1_000_000)},
FrontRunningProtection: true,
ProposerFee: math.LegacyNewDecWithPrec(25, 2),
}
err = keepers.BuilderKeeper.SetParams(ctx, builderParams)
err = setPobParams(ctx, keepers.FeeBurnerKeeper, keepers.BuilderKeeper)
if err != nil {
return nil, err
}

ctx.Logger().Info("Setting sudo callback limit...")
cmParams := contractmanagertypes.Params{
SudoCallGasLimit: 1_000_000,
}
err = keepers.ContractManager.SetParams(ctx, cmParams)
err = setContractManagerParams(ctx, keepers.ContractManager)
if err != nil {
return nil, err
}
Expand All @@ -106,6 +92,31 @@ func CreateUpgradeHandler(
}
}

func setPobParams(ctx sdk.Context, feeBurnerKeeper *feeburnerkeeper.Keeper, builderKeeper builderkeeper.Keeper) error {
treasury := feeBurnerKeeper.GetParams(ctx).TreasuryAddress
_, data, err := bech32.DecodeAndConvert(treasury)
if err != nil {
return err
}

builderParams := buildertypes.Params{
MaxBundleSize: 2,
EscrowAccountAddress: data,
ReserveFee: sdk.Coin{Denom: "untrn", Amount: sdk.NewInt(1_000_000)},
MinBidIncrement: sdk.Coin{Denom: "untrn", Amount: sdk.NewInt(1_000_000)},
FrontRunningProtection: true,
ProposerFee: math.LegacyNewDecWithPrec(25, 2),
}
return builderKeeper.SetParams(ctx, builderParams)
}

func setContractManagerParams(ctx sdk.Context, keeper contractmanagerkeeper.Keeper) error {
cmParams := contractmanagertypes.Params{
SudoCallGasLimit: contractmanagertypes.DefaultSudoCallGasLimit,
}
return keeper.SetParams(ctx, cmParams)
}

func migrateCronParams(ctx sdk.Context, paramsKeepers paramskeeper.Keeper, storeKey storetypes.StoreKey, codec codec.Codec) error {
store := ctx.KVStore(storeKey)
var currParams crontypes.Params
Expand Down
2 changes: 1 addition & 1 deletion neutronutils/errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
)

// OutOfGasRecovery converts `out of gas` panic into an error
// leave unprocessed any other kinds of panics
// leaving unprocessed any other kinds of panics
func OutOfGasRecovery(
gasMeter sdk.GasMeter,
err *error,
Expand Down
4 changes: 2 additions & 2 deletions neutronutils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package neutronutils
import sdk "github.com/cosmos/cosmos-sdk/types"

// CreateCachedContext creates a cached context with a limited gas meter.
func CreateCachedContext(ctx sdk.Context, gasLimit uint64) (sdk.Context, func(), sdk.GasMeter) {
func CreateCachedContext(ctx sdk.Context, gasLimit uint64) (sdk.Context, func()) {
cacheCtx, writeFn := ctx.CacheContext()
gasMeter := sdk.NewGasMeter(gasLimit)
cacheCtx = cacheCtx.WithGasMeter(gasMeter)
return cacheCtx, writeFn, gasMeter
return cacheCtx, writeFn
}
16 changes: 6 additions & 10 deletions x/interchaintxs/keeper/ibc_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,11 @@ func (k *Keeper) HandleAcknowledgement(ctx sdk.Context, packet channeltypes.Pack
return errors.Wrapf(sdkerrors.ErrUnknownRequest, "cannot unmarshal ICS-27 packet acknowledgement: %v", err)
}

cacheCtx, writeFn, newGasMeter := neutronutils.CreateCachedContext(ctx, k.contractManagerKeeper.GetParams(ctx).SudoCallGasLimit)

k.feeKeeper.DistributeAcknowledgementFee(ctx, relayer, feetypes.NewPacketID(packet.SourcePort, packet.SourceChannel, packet.Sequence))

cacheCtx, writeFn := neutronutils.CreateCachedContext(ctx, k.contractManagerKeeper.GetParams(ctx).SudoCallGasLimit)
func() {
defer neutronerrors.OutOfGasRecovery(newGasMeter, &err)
defer neutronerrors.OutOfGasRecovery(cacheCtx.GasMeter(), &err)
// Actually we have only one kind of error returned from acknowledgement
// maybe later we'll retrieve actual errors from events
if ack.GetError() != "" {
Expand All @@ -49,7 +48,6 @@ func (k *Keeper) HandleAcknowledgement(ctx sdk.Context, packet channeltypes.Pack
_, err = k.contractManagerKeeper.SudoResponse(cacheCtx, icaOwner.GetContract(), packet, ack.GetResult())
}
}()

if err != nil {
// the contract either returned an error or panicked with `out of gas`
k.contractManagerKeeper.AddContractFailure(ctx, &packet, icaOwner.GetContract().String(), contractmanagertypes.Ack, &ack)
Expand All @@ -58,7 +56,7 @@ func (k *Keeper) HandleAcknowledgement(ctx sdk.Context, packet channeltypes.Pack
writeFn()
}

ctx.GasMeter().ConsumeGas(newGasMeter.GasConsumedToLimit(), "consume gas from cached context")
ctx.GasMeter().ConsumeGas(cacheCtx.GasMeter().GasConsumedToLimit(), "consume gas from cached context")

return nil
}
Expand All @@ -75,15 +73,13 @@ func (k *Keeper) HandleTimeout(ctx sdk.Context, packet channeltypes.Packet, rela
return errors.Wrap(err, "failed to get ica owner from port")
}

cacheCtx, writeFn, newGasMeter := neutronutils.CreateCachedContext(ctx, k.contractManagerKeeper.GetParams(ctx).SudoCallGasLimit)

k.feeKeeper.DistributeTimeoutFee(ctx, relayer, feetypes.NewPacketID(packet.SourcePort, packet.SourceChannel, packet.Sequence))

cacheCtx, writeFn := neutronutils.CreateCachedContext(ctx, k.contractManagerKeeper.GetParams(ctx).SudoCallGasLimit)
func() {
defer neutronerrors.OutOfGasRecovery(newGasMeter, &err)
defer neutronerrors.OutOfGasRecovery(cacheCtx.GasMeter(), &err)
_, err = k.contractManagerKeeper.SudoTimeout(cacheCtx, icaOwner.GetContract(), packet)
}()

if err != nil {
// the contract either returned an error or panicked with `out of gas`
k.contractManagerKeeper.AddContractFailure(ctx, &packet, icaOwner.GetContract().String(), contractmanagertypes.Timeout, nil)
Expand All @@ -92,7 +88,7 @@ func (k *Keeper) HandleTimeout(ctx sdk.Context, packet channeltypes.Packet, rela
writeFn()
}

ctx.GasMeter().ConsumeGas(newGasMeter.GasConsumedToLimit(), "consume gas from cached context")
ctx.GasMeter().ConsumeGas(cacheCtx.GasMeter().GasConsumedToLimit(), "consume gas from cached context")

return nil
}
Expand Down
19 changes: 7 additions & 12 deletions x/transfer/ibc_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,11 @@ func (im IBCModule) HandleAcknowledgement(ctx sdk.Context, packet channeltypes.P
return nil
}

cacheCtx, writeFn, newGasMeter := neutronutils.CreateCachedContext(ctx, im.ContractManagerKeeper.GetParams(ctx).SudoCallGasLimit)

// distribute fee
im.wrappedKeeper.FeeKeeper.DistributeAcknowledgementFee(ctx, relayer, feetypes.NewPacketID(packet.SourcePort, packet.SourceChannel, packet.Sequence))

cacheCtx, writeFn := neutronutils.CreateCachedContext(ctx, im.ContractManagerKeeper.GetParams(ctx).SudoCallGasLimit)
func() {
defer neutronerrors.OutOfGasRecovery(newGasMeter, &err)
defer neutronerrors.OutOfGasRecovery(cacheCtx.GasMeter(), &err)
if ack.Success() {
_, err = im.ContractManagerKeeper.SudoResponse(cacheCtx, senderAddress, packet, ack.GetResult())
} else {
Expand All @@ -50,7 +48,6 @@ func (im IBCModule) HandleAcknowledgement(ctx sdk.Context, packet channeltypes.P
_, err = im.ContractManagerKeeper.SudoError(cacheCtx, senderAddress, packet, ack.GetError())
}
}()

if err != nil {
// the contract either returned an error or panicked with `out of gas`
im.ContractManagerKeeper.AddContractFailure(ctx, &packet, senderAddress.String(), contractmanagertypes.Ack, &ack)
Expand All @@ -59,7 +56,7 @@ func (im IBCModule) HandleAcknowledgement(ctx sdk.Context, packet channeltypes.P
writeFn()
}

ctx.GasMeter().ConsumeGas(newGasMeter.GasConsumedToLimit(), "consume gas from cached context")
ctx.GasMeter().ConsumeGas(cacheCtx.GasMeter().GasConsumedToLimit(), "consume gas from cached context")

im.keeper.Logger(ctx).Debug("acknowledgement received", "Packet data", data, "CheckTx", ctx.IsCheckTx())

Expand All @@ -81,15 +78,13 @@ func (im IBCModule) HandleTimeout(ctx sdk.Context, packet channeltypes.Packet, r
return nil
}

cacheCtx, writeFn, newGasMeter := neutronutils.CreateCachedContext(ctx, im.ContractManagerKeeper.GetParams(ctx).SudoCallGasLimit)

// distribute fee
im.wrappedKeeper.FeeKeeper.DistributeTimeoutFee(ctx, relayer, feetypes.NewPacketID(packet.SourcePort, packet.SourceChannel, packet.Sequence))

cacheCtx, writeFn := neutronutils.CreateCachedContext(ctx, im.ContractManagerKeeper.GetParams(ctx).SudoCallGasLimit)
func() {
defer neutronerrors.OutOfGasRecovery(newGasMeter, &err)
defer neutronerrors.OutOfGasRecovery(cacheCtx.GasMeter(), &err)
_, err = im.ContractManagerKeeper.SudoTimeout(cacheCtx, senderAddress, packet)
}()

if err != nil {
// the contract either returned an error or panicked with `out of gas`
im.ContractManagerKeeper.AddContractFailure(ctx, &packet, senderAddress.String(), contractmanagertypes.Timeout, nil)
Expand All @@ -98,7 +93,7 @@ func (im IBCModule) HandleTimeout(ctx sdk.Context, packet channeltypes.Packet, r
writeFn()
}

ctx.GasMeter().ConsumeGas(newGasMeter.GasConsumedToLimit(), "consume gas from cached context")
ctx.GasMeter().ConsumeGas(cacheCtx.GasMeter().GasConsumedToLimit(), "consume gas from cached context")

return nil
}

0 comments on commit c288fa9

Please sign in to comment.