Skip to content

Commit

Permalink
[CLOB-547] IsClobOrderTransaction only filter out Short-Term orders…
Browse files Browse the repository at this point in the history
…/cancels. (#7)

* generate protos for dydx

* add the matches and tx protos

* add dydx order id logic and constants

* IsClobOrderTransaction => IsShortTermClobOrderTransaction

* disable lint for dydx order id constants

* more specific linter disables

* rename proto package due to global proto registry name clash

* Update import paths for dydx clob types

* helpful text string function for order id

* minor pr comments, no logic changes

* README updates
  • Loading branch information
lcwik committed Dec 4, 2023
1 parent bc8f952 commit 6ccae02
Show file tree
Hide file tree
Showing 19 changed files with 6,847 additions and 15 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,19 @@ Note that this doesn't pull in upstream tags, so in order to do this follow thes
1. `git fetch upstream`
2. `git push --tags`

## dYdX Proto maintenance

In order to support some of our custom functionality, we require some dydx protobuf files to be copied into this repository. Currently, the source of truth for protos is in `dydxprotocol/v4`, and any changes that require updates to any of the protos in this repository should be sync'd over as well. Here are steps for updating and compiling the protos here.

1. Modify the protos in `proto/dydxcometbft`.
2. `make proto-gen`

Note that the protos cannot be copied over directly. golang protobufs share a global namespace, and we have changed the package name slightly to avoid a name clash.

We've also included a new dependency in the `buf.yaml` file for `"cosmos_proto/cosmos.proto"`. If this needs to be updated, run `buf build`. For more information, read [here](https://github.com/dydxprotocol/v4/tree/main/proto#update-protos).

In the future, we will aim to have a single source of truth for protos.

## Updating CometBFT to new versions

When a new version of CometBFT is published, we may want to adopt the changes in our fork. This process can be somewhat tedious, but below are the recommended steps to accomplish this.
Expand Down
14 changes: 8 additions & 6 deletions mempool/clist_mempool.go
Original file line number Diff line number Diff line change
Expand Up @@ -431,14 +431,14 @@ func (mem *CListMempool) resCbFirstTime(
"total", mem.Size(),
)

// If this transaction is a `PlaceOrder` or `CancelOrder` transaction,
// If this transaction is a short term `PlaceOrder` or `CancelOrder` transaction,
// don't call `notifyTxsAvailable()`. The `notifyTxsAvailable()` function
// uses a channel in the mempool called `txsAvailable` to signal to the
// consensus algorithm that transactions are available to be included in
// the next proposal. If no transactions are available for inclusion in
// the next proposal, the consensus algorithm will wait for `create_empty_blocks_interval`
// before proposing an empty block instead.
if IsClobOrderTransaction(memTx.tx, mem.logger) {
if IsShortTermClobOrderTransaction(memTx.tx, mem.logger) {
return
}

Expand Down Expand Up @@ -574,9 +574,10 @@ func (mem *CListMempool) ReapMaxBytesMaxGas(maxBytes, maxGas int64) types.Txs {
for e := mem.txs.Front(); e != nil; e = e.Next() {
memTx := e.Value.(*mempoolTx)

// If this transaction is Cosmos transaction containing a `PlaceOrder` or `CancelOrder` message,
// If this transaction is Cosmos transaction containing a
// short term `PlaceOrder` or `CancelOrder` message,
// don't include it in the next proposed block.
if IsClobOrderTransaction(memTx.tx, mem.logger) {
if IsShortTermClobOrderTransaction(memTx.tx, mem.logger) {
continue
}

Expand Down Expand Up @@ -728,9 +729,10 @@ func (mem *CListMempool) recheckTxs() {

for e := mem.txs.Front(); e != nil; e = e.Next() {
memTx := e.Value.(*mempoolTx)
// If this transaction is Cosmos transaction containing a `PlaceOrder` or `CancelOrder` message,
// If this transaction is Cosmos transaction containing a
// short term `PlaceOrder` or `CancelOrder` message,
// remove it from the mempool instead of rechecking.
if IsClobOrderTransaction(memTx.tx, mem.logger) {
if IsShortTermClobOrderTransaction(memTx.tx, mem.logger) {
if err := mem.RemoveTxByKey(memTx.tx.Key()); err != nil {
mem.logger.Debug("Recheck failed to remove short term CLOB transaction from mempool", "err", err)
}
Expand Down
38 changes: 29 additions & 9 deletions mempool/dydx_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ package mempool

import (
"github.com/cometbft/cometbft/libs/log"
"github.com/cometbft/cometbft/proto/dydxcometbft/clob"
"github.com/cometbft/cometbft/types"
cosmostx "github.com/cosmos/cosmos-sdk/types/tx"
)

// isClobOrderTransaction returns true if the provided `tx` is a
// Cosmos transaction containing a `MsgPlaceOrder` or `MsgCancelOrder` message.
func IsClobOrderTransaction(
// IsShortTermClobOrderTransaction returns true if the provided `tx` is a
// Cosmos transaction containing a short-term `MsgPlaceOrder` or
// short-term `MsgCancelOrder` message.
func IsShortTermClobOrderTransaction(
tx types.Tx,
mempoolLogger log.Logger,
) bool {
Expand All @@ -18,12 +20,30 @@ func IsClobOrderTransaction(
mempoolLogger.Error("isClobOrderTransaction error. Invalid Cosmos Transaction.")
return false
}

if cosmosTx.Body != nil &&
len(cosmosTx.Body.Messages) == 1 &&
(cosmosTx.Body.Messages[0].TypeUrl == "/dydxprotocol.clob.MsgPlaceOrder" ||
cosmosTx.Body.Messages[0].TypeUrl == "/dydxprotocol.clob.MsgCancelOrder") {
return true
if cosmosTx.Body != nil && len(cosmosTx.Body.Messages) == 1 {
bytes := cosmosTx.Body.Messages[0].Value
if cosmosTx.Body.Messages[0].TypeUrl == "/dydxprotocol.clob.MsgPlaceOrder" {
msgPlaceOrder := &clob.MsgPlaceOrder{}
err := msgPlaceOrder.Unmarshal(bytes)
// In the case of an unmarshalling error, panic.
// Chances are, the protos are out of sync with the dydx v4 repo.
if err != nil {
panic(
"Failed to unmarshal MsgPlaceOrder from Cosmos transaction in CometBFT mempool.",
)
}
return msgPlaceOrder.Order.OrderId.IsShortTermOrder()
}
if cosmosTx.Body.Messages[0].TypeUrl == "/dydxprotocol.clob.MsgCancelOrder" {
msgCancelOrder := &clob.MsgCancelOrder{}
err := msgCancelOrder.Unmarshal(bytes)
// In the case of an unmarshalling error, panic.
// Chances are, the protos are out of sync with the dydx v4 repo.
if err != nil {
panic("Failed to unmarshal MsgCancelOrder from Cosmos transaction.")
}
return msgCancelOrder.OrderId.IsShortTermOrder()
}
}

return false
Expand Down
5 changes: 5 additions & 0 deletions proto/buf.lock
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
# Generated by buf. DO NOT EDIT.
version: v1
deps:
- remote: buf.build
owner: cosmos
repository: cosmos-proto
commit: 1935555c206d4afb9e94615dfd0fad31
digest: shake256:c74d91a3ac7ae07d579e90eee33abf9b29664047ac8816500cf22c081fec0d72d62c89ce0bebafc1f6fec7aa5315be72606717740ca95007248425102c365377
- remote: buf.build
owner: cosmos
repository: gogo-proto
Expand Down
2 changes: 2 additions & 0 deletions proto/buf.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ version: v1
name: buf.build/tendermint/tendermint
deps:
- buf.build/cosmos/gogo-proto
# Latest release was in Dec 2021, https://buf.build/cosmos/cosmos-proto/commits/main
- buf.build/cosmos/cosmos-proto:1935555c206d4afb9e94615dfd0fad31
breaking:
use:
- FILE
Expand Down
Loading

0 comments on commit 6ccae02

Please sign in to comment.