Skip to content

Commit

Permalink
patch 2
Browse files Browse the repository at this point in the history
  • Loading branch information
VictorTrustyDev committed Sep 26, 2024
1 parent 757bb77 commit e2bdcaa
Show file tree
Hide file tree
Showing 18 changed files with 402 additions and 10 deletions.
16 changes: 14 additions & 2 deletions x/dymns/keeper/msg_server.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package keeper

import (
"fmt"

sdk "github.com/cosmos/cosmos-sdk/types"
dymnstypes "github.com/dymensionxyz/dymension/v3/x/dymns/types"
)
Expand All @@ -19,9 +21,19 @@ func NewMsgServerImpl(keeper Keeper) dymnstypes.MsgServer {

// consumeMinimumGas consumes the minimum gas
// if the consumed gas during tx is less than the minimum gas.
func consumeMinimumGas(ctx sdk.Context, minimumGas uint64, actionName string) {
// The original consumed gas should be captured from gas meter before invoke message execution.
// This function will panic if the gas meter consumed gas is less than the original consumed gas.
func consumeMinimumGas(ctx sdk.Context, minimumGas, originalConsumedGas uint64, actionName string) {
if minimumGas > 0 {
if consumedGas := ctx.GasMeter().GasConsumed(); consumedGas < minimumGas {
laterConsumedGas := ctx.GasMeter().GasConsumed()
if laterConsumedGas < originalConsumedGas {
// unexpect gas consumption
panic(fmt.Sprintf(
"later gas is less than original gas: %d < %d",
laterConsumedGas, originalConsumedGas,
))
}
if consumedGas := laterConsumedGas - originalConsumedGas; consumedGas < minimumGas {
// we only consume the gas that is needed to reach the target minimum gas
gasToConsume := minimumGas - consumedGas

Expand Down
3 changes: 2 additions & 1 deletion x/dymns/keeper/msg_server_accept_buy_order.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
// performed by the owner of the asset.
func (k msgServer) AcceptBuyOrder(goCtx context.Context, msg *dymnstypes.MsgAcceptBuyOrder) (*dymnstypes.MsgAcceptBuyOrderResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
originalConsumedGas := ctx.GasMeter().GasConsumed()

if err := msg.ValidateBasic(); err != nil {
return nil, err
Expand Down Expand Up @@ -47,7 +48,7 @@ func (k msgServer) AcceptBuyOrder(goCtx context.Context, msg *dymnstypes.MsgAcce
}

// charge protocol fee
consumeMinimumGas(ctx, dymnstypes.OpGasUpdateBuyOrder, "AcceptBuyOrder")
consumeMinimumGas(ctx, dymnstypes.OpGasUpdateBuyOrder, originalConsumedGas, "AcceptBuyOrder")

return resp, nil
}
Expand Down
52 changes: 52 additions & 0 deletions x/dymns/keeper/msg_server_accept_buy_order_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,30 @@ func (s *KeeperTestSuite) Test_msgServer_AcceptBuyOrder_Type_DymName() {
wantLaterOwnerBalance: sdkmath.NewInt(2).Mul(priceMultiplier),
wantMinConsumeGas: dymnstypes.OpGasUpdateBuyOrder,
},
{
name: "pass - independently charge gas",
existingDymName: dymName,
existingOffer: offer,
buyOrderId: offer.Id,
owner: dymName.Owner,
minAccept: offer.OfferPrice,
originalModuleBalance: offer.OfferPrice.Amount,
originalOwnerBalance: sdk.NewInt(0),
preRunSetupFunc: func(s *KeeperTestSuite) {
s.ctx.GasMeter().ConsumeGas(100_000_000, "simulate previous run")
},
wantErr: false,
wantLaterOffer: nil,
wantLaterDymName: &dymnstypes.DymName{
Name: dymName.Name,
Owner: offer.Buyer,
Controller: offer.Buyer,
ExpireAt: dymName.ExpireAt,
},
wantLaterModuleBalance: sdkmath.ZeroInt(),
wantLaterOwnerBalance: offer.OfferPrice.Amount,
wantMinConsumeGas: 100_000_000 + dymnstypes.OpGasUpdateBuyOrder,
},
}
for _, tt := range tests {
s.Run(tt.name, func() {
Expand Down Expand Up @@ -1453,6 +1477,34 @@ func (s *KeeperTestSuite) Test_msgServer_AcceptBuyOrder_Type_Alias() {
wantLaterOwnerBalance: sdk.NewInt(2),
wantMinConsumeGas: dymnstypes.OpGasUpdateBuyOrder,
},
{
name: "pass - independently charge gas",
existingRollApps: []rollapp{rollApp_One_By1_SingleAlias, rollApp_Two_By2_SingleAlias},
existingOffer: offerAliasOfRollAppOne,
buyOrderId: offerAliasOfRollAppOne.Id,
owner: rollApp_One_By1_SingleAlias.owner,
minAccept: offerAliasOfRollAppOne.OfferPrice,
originalModuleBalance: offerAliasOfRollAppOne.OfferPrice.Amount,
originalOwnerBalance: sdk.NewInt(0),
preRunSetupFunc: func(s *KeeperTestSuite) {
s.ctx.GasMeter().ConsumeGas(100_000_000, "simulate previous run")
},
wantErr: false,
wantLaterOffer: nil,
wantLaterRollApps: []rollapp{
{
rollAppId: rollApp_One_By1_SingleAlias.rollAppId,
aliases: []string{},
},
{
rollAppId: rollApp_Two_By2_SingleAlias.rollAppId,
aliases: append(rollApp_Two_By2_SingleAlias.aliases, offerAliasOfRollAppOne.AssetId),
},
},
wantLaterModuleBalance: sdk.NewInt(0),
wantLaterOwnerBalance: offerAliasOfRollAppOne.OfferPrice.Amount,
wantMinConsumeGas: 100_000_000 + dymnstypes.OpGasUpdateBuyOrder,
},
}
for _, tt := range tests {
s.Run(tt.name, func() {
Expand Down
3 changes: 2 additions & 1 deletion x/dymns/keeper/msg_server_cancel_buy_order.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
// handles canceling a Buy-Order, performed by the buyer who placed the offer.
func (k msgServer) CancelBuyOrder(goCtx context.Context, msg *dymnstypes.MsgCancelBuyOrder) (*dymnstypes.MsgCancelBuyOrderResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
originalConsumedGas := ctx.GasMeter().GasConsumed()

if err := msg.ValidateBasic(); err != nil {
return nil, err
Expand Down Expand Up @@ -41,7 +42,7 @@ func (k msgServer) CancelBuyOrder(goCtx context.Context, msg *dymnstypes.MsgCanc
}

// charge protocol fee
consumeMinimumGas(ctx, dymnstypes.OpGasCloseBuyOrder, "CancelBuyOrder")
consumeMinimumGas(ctx, dymnstypes.OpGasCloseBuyOrder, originalConsumedGas, "CancelBuyOrder")

return resp, nil
}
Expand Down
34 changes: 34 additions & 0 deletions x/dymns/keeper/msg_server_cancel_buy_order_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,23 @@ func (s *KeeperTestSuite) Test_msgServer_CancelBuyOrder_DymName() {
wantLaterBuyerBalance: sdk.NewInt(2),
wantMinConsumeGas: 1,
},
{
name: "pass - independently charge gas",
existingDymName: dymName,
existingOffer: offer,
buyOrderId: offer.Id,
buyer: offer.Buyer,
originalModuleBalance: offer.OfferPrice.Amount,
originalBuyerBalance: sdk.NewInt(0),
preRunSetupFunc: func(s *KeeperTestSuite) {
s.ctx.GasMeter().ConsumeGas(100_000_000, "simulate previous run")
},
wantErr: false,
wantLaterOffer: nil,
wantLaterModuleBalance: sdk.NewInt(0),
wantLaterBuyerBalance: offer.OfferPrice.Amount,
wantMinConsumeGas: 100_000_000 + dymnstypes.OpGasCloseBuyOrder,
},
}
for _, tt := range tests {
s.Run(tt.name, func() {
Expand Down Expand Up @@ -615,6 +632,23 @@ func (s *KeeperTestSuite) Test_msgServer_CancelBuyOrder_Alias() {
wantLaterBuyerBalance: sdk.NewInt(2),
wantMinConsumeGas: 1,
},
{
name: "pass - independently charge gas",
existingRollApps: []rollapp{rollApp_One_By1, rollApp_Two_By2},
existingOffer: offerAliasOfRollAppOne,
buyOrderId: offerAliasOfRollAppOne.Id,
buyer: offerAliasOfRollAppOne.Buyer,
originalModuleBalance: offerAliasOfRollAppOne.OfferPrice.Amount,
originalBuyerBalance: sdk.NewInt(0),
preRunSetupFunc: func(s *KeeperTestSuite) {
s.ctx.GasMeter().ConsumeGas(100_000_000, "simulate previous run")
},
wantErr: false,
wantLaterOffer: nil,
wantLaterModuleBalance: sdk.NewInt(0),
wantLaterBuyerBalance: offerAliasOfRollAppOne.OfferPrice.Amount,
wantMinConsumeGas: 100_000_000 + dymnstypes.OpGasCloseBuyOrder,
},
}
for _, tt := range tests {
s.Run(tt.name, func() {
Expand Down
3 changes: 2 additions & 1 deletion x/dymns/keeper/msg_server_cancel_sell_order.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
// Can only be performed if no one has placed a bid on the asset.
func (k msgServer) CancelSellOrder(goCtx context.Context, msg *dymnstypes.MsgCancelSellOrder) (*dymnstypes.MsgCancelSellOrderResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
originalConsumedGas := ctx.GasMeter().GasConsumed()

if err := msg.ValidateBasic(); err != nil {
return nil, err
Expand All @@ -38,7 +39,7 @@ func (k msgServer) CancelSellOrder(goCtx context.Context, msg *dymnstypes.MsgCan
}

// charge protocol fee
consumeMinimumGas(ctx, dymnstypes.OpGasCloseSellOrder, "CancelSellOrder")
consumeMinimumGas(ctx, dymnstypes.OpGasCloseSellOrder, originalConsumedGas, "CancelSellOrder")

return resp, nil
}
Expand Down
14 changes: 14 additions & 0 deletions x/dymns/keeper/msg_server_cancel_sell_order_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,9 @@ func (s *KeeperTestSuite) Test_msgServer_CancelSellOrder_DymName() {
})

s.Run("can cancel if satisfied conditions", func() {
const previousRunGasConsumed = 100_000_000
s.ctx.GasMeter().ConsumeGas(previousRunGasConsumed, "simulate previous run")

moduleParams := s.dymNsKeeper.GetParams(s.ctx)
moduleParams.Misc.EnableTradingName = false // allowed to cancel even if trading is disabled
s.Require().NoError(s.dymNsKeeper.SetParams(s.ctx, moduleParams))
Expand Down Expand Up @@ -256,6 +259,10 @@ func (s *KeeperTestSuite) Test_msgServer_CancelSellOrder_DymName() {
s.ctx.GasMeter().GasConsumed(), dymnstypes.OpGasCloseSellOrder,
"should consume params gas",
)
s.GreaterOrEqual(
s.ctx.GasMeter().GasConsumed(), previousRunGasConsumed+dymnstypes.OpGasCloseSellOrder,
"gas consumption should be stacked with previous run",
)
})
}

Expand Down Expand Up @@ -438,6 +445,9 @@ func (s *KeeperTestSuite) Test_msgServer_CancelSellOrder_Alias() {
})

s.Run("can cancel if satisfied conditions", func() {
const previousRunGasConsumed = 100_000_000
s.ctx.GasMeter().ConsumeGas(previousRunGasConsumed, "simulate previous run")

moduleParams := s.dymNsKeeper.GetParams(s.ctx)
moduleParams.Misc.EnableTradingAlias = false // allowed to cancel even if trading is disabled
s.Require().NoError(s.dymNsKeeper.SetParams(s.ctx, moduleParams))
Expand Down Expand Up @@ -480,6 +490,10 @@ func (s *KeeperTestSuite) Test_msgServer_CancelSellOrder_Alias() {
s.ctx.GasMeter().GasConsumed(), dymnstypes.OpGasCloseSellOrder,
"should consume params gas",
)
s.GreaterOrEqual(
s.ctx.GasMeter().GasConsumed(), previousRunGasConsumed+dymnstypes.OpGasCloseSellOrder,
"gas consumption should be stacked with previous run",
)

s.requireAlias(rollapp_1_ofOwner.alias).LinkedToRollApp(rollapp_1_ofOwner.rollAppId)
})
Expand Down
3 changes: 2 additions & 1 deletion x/dymns/keeper/msg_server_place_buy_order.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
// handles creating an offer to buy a Dym-Name/Alias, performed by the buyer.
func (k msgServer) PlaceBuyOrder(goCtx context.Context, msg *dymnstypes.MsgPlaceBuyOrder) (*dymnstypes.MsgPlaceBuyOrderResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
originalConsumedGas := ctx.GasMeter().GasConsumed()

if err := msg.ValidateBasic(); err != nil {
return nil, err
Expand Down Expand Up @@ -45,7 +46,7 @@ func (k msgServer) PlaceBuyOrder(goCtx context.Context, msg *dymnstypes.MsgPlace
} else {
minimumTxGasRequired = dymnstypes.OpGasPutBuyOrder
}
consumeMinimumGas(ctx, minimumTxGasRequired, "PlaceBuyOrder")
consumeMinimumGas(ctx, minimumTxGasRequired, originalConsumedGas, "PlaceBuyOrder")

return resp, nil
}
Expand Down
54 changes: 54 additions & 0 deletions x/dymns/keeper/msg_server_place_buy_order_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -797,6 +797,32 @@ func (s *KeeperTestSuite) Test_msgServer_PlaceBuyOrder_DymName() {
s.Equal([]string{"102", "103"}, orderIds.OrderIds)
},
},
{
name: "pass - independently charge gas",
existingDymName: dymName,
existingOffer: nil,
dymName: dymName.Name,
buyer: buyerA,
offer: minOfferPriceCoin,
existingBuyOrderId: "",
originalModuleBalance: sdk.NewInt(5),
originalBuyerBalance: minOfferPriceCoin.Amount.AddRaw(2),
preRunSetupFunc: func(s *KeeperTestSuite) {
s.ctx.GasMeter().ConsumeGas(100_000_000, "simulate previous run")
},
wantErr: false,
wantBuyOrderId: "101",
wantLaterOffer: &dymnstypes.BuyOrder{
Id: "101",
AssetId: dymName.Name,
AssetType: dymnstypes.TypeName,
Buyer: buyerA,
OfferPrice: minOfferPriceCoin,
},
wantLaterModuleBalance: minOfferPriceCoin.Amount.AddRaw(5),
wantLaterBuyerBalance: sdk.NewInt(2),
wantMinConsumeGas: 100_000_000 + dymnstypes.OpGasPutBuyOrder,
},
}
for _, tt := range tests {
s.Run(tt.name, func() {
Expand Down Expand Up @@ -1856,6 +1882,34 @@ func (s *KeeperTestSuite) Test_msgServer_PlaceBuyOrder_Alias() {
s.Equal([]string{"202", "203"}, orderIds.OrderIds)
},
},
{
name: "pass - independently charge gas",
existingRollApps: []rollapp{rollApp_1_by1_asSrc, rollApp_2_by2_asDest},
existingOffer: nil,
alias: rollApp_1_by1_asSrc.alias,
buyer: rollApp_2_by2_asDest.creator,
dstRollAppId: rollApp_2_by2_asDest.rollAppID,
offer: minOfferPriceCoin,
existingBuyOrderId: "",
originalModuleBalance: sdk.NewInt(5),
originalBuyerBalance: minOfferPriceCoin.Amount.AddRaw(2),
preRunSetupFunc: func(s *KeeperTestSuite) {
s.ctx.GasMeter().ConsumeGas(100_000_000, "simulate previous run")
},
wantErr: false,
wantBuyOrderId: "201",
wantLaterOffer: &dymnstypes.BuyOrder{
Id: "201",
AssetId: rollApp_1_by1_asSrc.alias,
AssetType: dymnstypes.TypeAlias,
Params: []string{rollApp_2_by2_asDest.rollAppID},
Buyer: rollApp_2_by2_asDest.creator,
OfferPrice: minOfferPriceCoin,
},
wantLaterModuleBalance: minOfferPriceCoin.Amount.AddRaw(5),
wantLaterBuyerBalance: sdk.NewInt(2),
wantMinConsumeGas: 100_000_000 + dymnstypes.OpGasPutBuyOrder,
},
}
for _, tt := range tests {
s.Run(tt.name, func() {
Expand Down
3 changes: 2 additions & 1 deletion x/dymns/keeper/msg_server_place_sell_order.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
// handles creating a Sell-Order that advertise a Dym-Name/Alias is for sale, performed by the owner.
func (k msgServer) PlaceSellOrder(goCtx context.Context, msg *dymnstypes.MsgPlaceSellOrder) (*dymnstypes.MsgPlaceSellOrderResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
originalConsumedGas := ctx.GasMeter().GasConsumed()

if err := msg.ValidateBasic(); err != nil {
return nil, err
Expand All @@ -40,7 +41,7 @@ func (k msgServer) PlaceSellOrder(goCtx context.Context, msg *dymnstypes.MsgPlac

// Charge protocol fee.
// The protocol fee mechanism is used to prevent spamming to the network.
consumeMinimumGas(ctx, dymnstypes.OpGasPlaceSellOrder, "PlaceSellOrder")
consumeMinimumGas(ctx, dymnstypes.OpGasPlaceSellOrder, originalConsumedGas, "PlaceSellOrder")

return resp, nil
}
Expand Down
Loading

0 comments on commit e2bdcaa

Please sign in to comment.