Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CT-514] Clob MsgBatchCancel functionality #1110

Merged
merged 20 commits into from
Mar 4, 2024
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions indexer/packages/v4-protos/src/codegen/dydxprotocol/clob/tx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,11 @@ export interface MsgBatchCancelSDKType {
export interface OrderBatch {
/** The Clob Pair ID all orders in this order batch belong to. */
clobPairId: number;
/** List of client ids in this order batch. */
/**
* List of client ids in this order batch.
* Note that this is serialized as a uint32 instead of a fixed32 to
* avoid issues when decoding repeated packed fixed32.
*/

clientIds: number[];
}
Expand All @@ -180,7 +184,11 @@ export interface OrderBatch {
export interface OrderBatchSDKType {
/** The Clob Pair ID all orders in this order batch belong to. */
clob_pair_id: number;
/** List of client ids in this order batch. */
/**
* List of client ids in this order batch.
* Note that this is serialized as a uint32 instead of a fixed32 to
* avoid issues when decoding repeated packed fixed32.
*/

client_ids: number[];
}
Expand Down Expand Up @@ -802,7 +810,7 @@ export const OrderBatch = {
writer.uint32(18).fork();

for (const v of message.clientIds) {
writer.fixed32(v);
writer.uint32(v);
}

writer.ldelim();
Expand All @@ -827,10 +835,10 @@ export const OrderBatch = {
const end2 = reader.uint32() + reader.pos;

while (reader.pos < end2) {
message.clientIds.push(reader.fixed32());
message.clientIds.push(reader.uint32());
}
} else {
message.clientIds.push(reader.fixed32());
message.clientIds.push(reader.uint32());
}

break;
Expand Down
4 changes: 3 additions & 1 deletion proto/dydxprotocol/clob/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,9 @@ message OrderBatch {
// The Clob Pair ID all orders in this order batch belong to.
uint32 clob_pair_id = 1;
// List of client ids in this order batch.
repeated fixed32 client_ids = 2;
// Note that this is serialized as a uint32 instead of a fixed32 to
// avoid issues when decoding repeated packed fixed32.
repeated uint32 client_ids = 2;
}

// MsgBatchCancelResponse is a response type used for batch canceling orders.
Expand Down
6 changes: 5 additions & 1 deletion protocol/app/module/interface_registry.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package module

import (
"cosmossdk.io/x/tx/signing"
"fmt"

"cosmossdk.io/x/tx/signing"
addresscodec "github.com/cosmos/cosmos-sdk/codec/address"
"github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down Expand Up @@ -76,6 +77,9 @@ func NewInterfaceRegistry(addrPrefix string, valAddrPrefix string) (types.Interf
// https://github.com/cosmos/cosmos-sdk/issues/18722 is fixed, replace this with the cosmos.msg.v1.signing
// annotation on the protos.
CustomGetSigners: map[protoreflect.FullName]signing.GetSignersFunc{
"dydxprotocol.clob.MsgBatchCancel": getLegacyMsgSignerFn(
[]string{"subaccount_id", "owner"},
),
"dydxprotocol.clob.MsgCancelOrder": getLegacyMsgSignerFn(
[]string{"order_id", "subaccount_id", "owner"},
),
Expand Down
10 changes: 9 additions & 1 deletion protocol/app/process/other_msgs_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package process_test

import (
errorsmod "cosmossdk.io/errors"
"testing"

errorsmod "cosmossdk.io/errors"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/dydxprotocol/v4-chain/protocol/app/process"
"github.com/dydxprotocol/v4-chain/protocol/testutil/constants"
Expand Down Expand Up @@ -87,6 +88,13 @@ func TestDecodeOtherMsgsTx(t *testing.T) {
"Msg type *types.MsgCancelOrder is not allowed in OtherTxs",
),
},
"Error: batch cancel order is not allowed": {
txBytes: constants.Msg_BatchCancel_TxBtyes,
expectedErr: errorsmod.Wrap(
process.ErrUnexpectedMsgType,
"Msg type *types.MsgBatchCancel is not allowed in OtherTxs",
),
},
"Valid: single msg": {
txBytes: constants.Msg_Send_TxBytes,
expectedMsgs: []sdk.Msg{constants.Msg_Send},
Expand Down
2 changes: 2 additions & 0 deletions protocol/app/process/utils_disallow_msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ func IsDisallowClobOrderMsgInOtherTxs(targetMsg sdk.Msg) bool {
order := msg.GetOrder()
orderId := order.GetOrderId()
return !orderId.IsStatefulOrder() // not stateful -> returns true -> disallow
case *clobtypes.MsgBatchCancel:
return true
}
return false
}
2 changes: 1 addition & 1 deletion protocol/app/process/utils_disallow_msgs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func TestIsDisallowClobOrderMsgInOtherTxs(t *testing.T) {
for _, msg := range allMsgSamples {
result := process.IsDisallowClobOrderMsgInOtherTxs(msg)
switch msg.(type) {
case *clobtypes.MsgCancelOrder, *clobtypes.MsgPlaceOrder:
case *clobtypes.MsgCancelOrder, *clobtypes.MsgPlaceOrder, *clobtypes.MsgBatchCancel:
// The sample msgs are short-term orders, so we expect these to be disallowed.
require.True(t, result) // true -> disallow
default:
Expand Down
2 changes: 1 addition & 1 deletion protocol/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ replace (
// Use dYdX fork of Cosmos SDK/store
cosmossdk.io/store => github.com/dydxprotocol/cosmos-sdk/store v1.0.3-0.20240227194839-f4e166bc1057
// Use dYdX fork of CometBFT
github.com/cometbft/cometbft => github.com/dydxprotocol/cometbft v0.38.6-0.20240220185844-e704122c8540
github.com/cometbft/cometbft => github.com/dydxprotocol/cometbft v0.38.6-0.20240229050000-3b085f30b462
// Use dYdX fork of Cosmos SDK
github.com/cosmos/cosmos-sdk => github.com/dydxprotocol/cosmos-sdk v0.50.5-0.20240227194839-f4e166bc1057
)
Expand Down
4 changes: 2 additions & 2 deletions protocol/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -530,8 +530,8 @@ github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkp
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
github.com/dvsekhvalnov/jose2go v1.6.0 h1:Y9gnSnP4qEI0+/uQkHvFXeD2PLPJeXEL+ySMEA2EjTY=
github.com/dvsekhvalnov/jose2go v1.6.0/go.mod h1:QsHjhyTlD/lAVqn/NSbVZmSCGeDehTB/mPZadG+mhXU=
github.com/dydxprotocol/cometbft v0.38.6-0.20240220185844-e704122c8540 h1:pkYQbAdOAAoZBSId9kLupCgZHj8YvA9LzM31fVYpjlw=
github.com/dydxprotocol/cometbft v0.38.6-0.20240220185844-e704122c8540/go.mod h1:REQN+ObgfYxi39TcYR/Hv95C9bPxY3sYJCvghryj7vY=
github.com/dydxprotocol/cometbft v0.38.6-0.20240229050000-3b085f30b462 h1:ufl8wDrax5K/33NMX/2P54iJAb5LlHJA8CYIcdfeR+o=
github.com/dydxprotocol/cometbft v0.38.6-0.20240229050000-3b085f30b462/go.mod h1:REQN+ObgfYxi39TcYR/Hv95C9bPxY3sYJCvghryj7vY=
github.com/dydxprotocol/cosmos-sdk v0.50.5-0.20240227194839-f4e166bc1057 h1:5Aq2oldr7T5+PEo6HYWA2cTcD4tY5ZM6755pKxhOvNg=
github.com/dydxprotocol/cosmos-sdk v0.50.5-0.20240227194839-f4e166bc1057/go.mod h1:I2uLntIWztPOXW0abkUb3SRphofiLL5DzrvHObyJVkM=
github.com/dydxprotocol/cosmos-sdk/store v1.0.3-0.20240227194839-f4e166bc1057 h1:rY2cckHMrpk3+9ggVqp3Zsvfn7AwEjjazvyp4HK3lgw=
Expand Down
7 changes: 4 additions & 3 deletions protocol/lib/log/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,10 @@ const (
// Module tag values are prefixed with `x/`
Clob = "x/clob"

CheckTx = "check_tx"
RecheckTx = "recheck_tx"
DeliverTx = "deliver_tx"
CheckTx = "check_tx"
RecheckTx = "recheck_tx"
DeliverTx = "deliver_tx"
MsgBatchCancel = "msg_batch_cancel"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there a better group for this? doesn't seem related to CheckTx, DeliverTx, etc

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is the batch for tag values, i just haven't been bothered to move anything over to this constants file other than the callbacks

)

// Special tag values that should be PascalCased (i.e function names)
Expand Down
14 changes: 14 additions & 0 deletions protocol/mocks/ClobKeeper.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion protocol/testutil/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -1320,7 +1320,7 @@ func launchValidatorInDir(

// MustMakeCheckTxsWithClobMsg creates one signed RequestCheckTx for each msg passed in.
// The messsage must use one of the hard-coded well known subaccount owners otherwise this will panic.
func MustMakeCheckTxsWithClobMsg[T clobtypes.MsgPlaceOrder | clobtypes.MsgCancelOrder](
func MustMakeCheckTxsWithClobMsg[T clobtypes.MsgPlaceOrder | clobtypes.MsgCancelOrder | clobtypes.MsgBatchCancel](
ctx sdk.Context,
app *app.App,
messages ...T,
Expand All @@ -1336,6 +1336,9 @@ func MustMakeCheckTxsWithClobMsg[T clobtypes.MsgPlaceOrder | clobtypes.MsgCancel
case clobtypes.MsgCancelOrder:
signerAddress = v.OrderId.SubaccountId.Owner
m = &v
case clobtypes.MsgBatchCancel:
signerAddress = v.SubaccountId.Owner
m = &v
default:
panic(fmt.Errorf("MustMakeCheckTxsWithClobMsg: Unknown message type %T", msg))
}
Expand Down
15 changes: 15 additions & 0 deletions protocol/testutil/constants/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ func init() {
_ = TestTxBuilder.SetMsgs(Msg_CancelOrder)
Msg_CancelOrder_TxBtyes, _ = TestEncodingCfg.TxConfig.TxEncoder()(TestTxBuilder.GetTx())

_ = TestTxBuilder.SetMsgs(Msg_BatchCancel)
Msg_BatchCancel_TxBtyes, _ = TestEncodingCfg.TxConfig.TxEncoder()(TestTxBuilder.GetTx())

_ = TestTxBuilder.SetMsgs(Msg_Send)
Msg_Send_TxBytes, _ = TestEncodingCfg.TxConfig.TxEncoder()(TestTxBuilder.GetTx())

Expand Down Expand Up @@ -54,6 +57,18 @@ var (
}
Msg_PlaceOrder_TxBtyes []byte

Msg_BatchCancel = &clobtypes.MsgBatchCancel{
SubaccountId: Alice_Num0,
ShortTermCancels: []clobtypes.OrderBatch{
{
ClobPairId: 0,
ClientIds: []uint32{0},
},
},
GoodTilBlock: 5,
}
Msg_BatchCancel_TxBtyes []byte

Msg_PlaceOrder_LongTerm = &clobtypes.MsgPlaceOrder{
Order: LongTermOrder_Alice_Num0_Id0_Clob0_Buy5_Price10_GTBT15,
}
Expand Down
4 changes: 3 additions & 1 deletion protocol/testutil/encoding/utils.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package encoding

import (
"github.com/dydxprotocol/v4-chain/protocol/testutil/ante"
"testing"

"github.com/dydxprotocol/v4-chain/protocol/testutil/ante"

feegrantmodule "cosmossdk.io/x/feegrant/module"
"cosmossdk.io/x/upgrade"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
Expand Down Expand Up @@ -73,6 +74,7 @@ func GetTestEncodingCfg() testutil.TestEncodingConfig {
&clobtypes.MsgProposedOperations{},
&clobtypes.MsgPlaceOrder{},
&clobtypes.MsgCancelOrder{},
&clobtypes.MsgBatchCancel{},

// Perpetuals.
&perpetualtypes.MsgAddPremiumVotes{},
Expand Down
43 changes: 37 additions & 6 deletions protocol/x/clob/ante/clob.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package ante

import (
"fmt"

errorsmod "cosmossdk.io/errors"
"github.com/cometbft/cometbft/crypto/tmhash"
cometbftlog "github.com/cometbft/cometbft/libs/log"
Expand Down Expand Up @@ -44,6 +46,7 @@ func (cd ClobDecorator) AnteHandle(
simulate bool,
next sdk.AnteHandler,
) (sdk.Context, error) {
fmt.Println("clob ante handler")
// No need to process during `DeliverTx` or simulation, call next `AnteHandler`.
if lib.IsDeliverTxMode(ctx) || simulate {
return next(ctx, tx, simulate)
Expand Down Expand Up @@ -130,6 +133,29 @@ func (cd ClobDecorator) AnteHandle(
log.Error, err,
)
}
case *types.MsgBatchCancel:
// MsgBatchCancel currently only processes short-term cancels right now.
// No need to process short term orders on `ReCheckTx`.
if ctx.IsReCheckTx() {
return next(ctx, tx, simulate)
}

ctx = log.AddPersistentTagsToLogger(
ctx,
log.Handler, log.MsgBatchCancel,
)

err := cd.clobKeeper.BatchCancelShortTermOrder(
ctx,
msg,
)

log.DebugLog(
ctx,
"Received new batch cancellation",
log.Tx, cometbftlog.NewLazySprintf("%X", tmhash.Sum(ctx.TxBytes())),
log.Error, err,
)
}
if err != nil {
return ctx, err
Expand All @@ -139,15 +165,15 @@ func (cd ClobDecorator) AnteHandle(
}

// IsSingleClobMsgTx returns `true` if the supplied `tx` consist of a single clob message
// (`MsgPlaceOrder` or `MsgCancelOrder`). If `msgs` consist of multiple clob messages,
// or a mix of on-chain and clob messages, an error is returned.
// (`MsgPlaceOrder` or `MsgCancelOrder` or `MsgBatchCancel`). If `msgs` consist of multiple
// clob messages, or a mix of on-chain and clob messages, an error is returned.
func IsSingleClobMsgTx(tx sdk.Tx) (bool, error) {
msgs := tx.GetMsgs()
var hasMessage = false

for _, msg := range msgs {
switch msg.(type) {
case *types.MsgCancelOrder, *types.MsgPlaceOrder:
case *types.MsgCancelOrder, *types.MsgPlaceOrder, *types.MsgBatchCancel:
hasMessage = true
}

Expand All @@ -164,16 +190,16 @@ func IsSingleClobMsgTx(tx sdk.Tx) (bool, error) {
if numMsgs > 1 {
return false, errorsmod.Wrap(
sdkerrors.ErrInvalidRequest,
"a transaction containing MsgCancelOrder or MsgPlaceOrder may not contain more than one message",
"a transaction containing MsgCancelOrder or MsgPlaceOrder or MsgBatchCancel may not contain more than one message",
)
}

return true, nil
}

// IsShortTermClobMsgTx returns `true` if the supplied `tx` consist of a single clob message
// (`MsgPlaceOrder` or `MsgCancelOrder`) which references a Short-Term Order. If `msgs` consist of multiple
// clob messages, or a mix of on-chain and clob messages, an error is returned.
// (`MsgPlaceOrder` or `MsgCancelOrder` or `MsgBatchCancel`) which references a Short-Term Order.
// If `msgs` consist of multiple clob messages, or a mix of on-chain and clob messages, an error is returned.
func IsShortTermClobMsgTx(ctx sdk.Context, tx sdk.Tx) (bool, error) {
msgs := tx.GetMsgs()

Expand All @@ -193,6 +219,11 @@ func IsShortTermClobMsgTx(ctx sdk.Context, tx sdk.Tx) (bool, error) {
isShortTermOrder = true
}
}
case *types.MsgBatchCancel:
{
// MsgBatchCancel processes only short term orders for now.
isShortTermOrder = true
}
}

if isShortTermOrder {
Expand Down
Loading
Loading