Skip to content

Commit

Permalink
feat: add support for trading transaction ordering
Browse files Browse the repository at this point in the history
  • Loading branch information
ze97286 committed May 18, 2024
1 parent bc7cf9e commit bb3e963
Show file tree
Hide file tree
Showing 15 changed files with 507 additions and 112 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
- [11196](https://github.com/vegaprotocol/vega/issues/11196) - Add an active field in the price monitoring bounds payload.
- [11211](https://github.com/vegaprotocol/vega/issues/11211) - Liquidation engine includes `vAMM` shapes as available volume.
- [11217](https://github.com/vegaprotocol/vega/issues/11217) - Allow market proposals to override risk factors.
- [11285](https://github.com/vegaprotocol/vega/issues/11285) - Add support for trading transaction ordering.

### 🐛 Fixes

Expand Down
2 changes: 2 additions & 0 deletions commands/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,8 @@ func CheckInputData(rawInputData []byte) (*commandspb.InputData, Errors) {
errs.Merge(checkAmendAMM(cmd.AmendAmm))
case *commandspb.InputData_CancelAmm:
errs.Merge(checkCancelAMM(cmd.CancelAmm))
case *commandspb.InputData_DelayedTransactionsWrapper:
break
default:
errs.AddForProperty("tx.input_data.command", ErrIsNotSupported)
}
Expand Down
9 changes: 9 additions & 0 deletions core/blockchain/abci/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ func (app *App) InitChain(_ context.Context, req *types.RequestInitChain) (*type
return &types.ResponseInitChain{}, nil
}

func (app *App) GetTx(tx []byte) (Tx, error) {
txx, _, err := app.getTx(tx)
return txx, err
}

// PrepareProposal will take the given transactions from the mempool and attempts to prepare a
// proposal from them when it's our turn to do so while keeping the size, gas, pow, and spam constraints.
func (app *App) PrepareProposal(_ context.Context, req *types.RequestPrepareProposal) (*types.ResponsePrepareProposal, error) {
Expand Down Expand Up @@ -148,11 +153,14 @@ func (app *App) FinalizeBlock(_ context.Context, req *types.RequestFinalizeBlock
blockHeight := uint64(req.Height)
blockTime := req.Time

println("FinalizeBlock", req.Height)

txs := make([]Tx, 0, len(req.Txs))
for _, rtx := range req.Txs {
// getTx can't fail at this point as we've verified on processProposal, however as it can fail in nullblockchain, handle it here
tx, _, err := app.getTx(rtx)
if err != nil {
println("getTx failed!!!!")
continue
}
app.removeTxFromCache(rtx)
Expand All @@ -167,6 +175,7 @@ func (app *App) FinalizeBlock(_ context.Context, req *types.RequestFinalizeBlock
// there must be a handling function at this point
fn := app.deliverTxs[tx.Command()]
txHash := hex.EncodeToString(tx.Hash())
println("processing transaction", txHash)
ctx := vgcontext.WithTxHash(app.ctx, txHash)
// process the transaction and handle errors
var result *types.ExecTxResult
Expand Down
2 changes: 2 additions & 0 deletions core/events/transaction_result.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ func NewTransactionResultEventFailure(

func (t *TransactionResult) setTx(tx interface{}) *TransactionResult {
switch tv := tx.(type) {
case *commandspb.DelayedTransactionsWrapper:
break
case *commandspb.OrderSubmission:
t.evt.Transaction = &eventspb.TransactionResult_OrderSubmission{
OrderSubmission: tv,
Expand Down
36 changes: 36 additions & 0 deletions core/nodewallets/commander.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,36 @@ func NewCommander(cfg Config, log *logging.Logger, bc Chain, w *vega.Wallet, bst
}, nil
}

func (c *Commander) NewTransaction(ctx context.Context, cmd txn.Command, payload proto.Message) ([]byte, error) {
chainID, err := c.bc.GetChainID(ctx)
if err != nil {
c.log.Error("couldn't retrieve chain ID",
logging.Error(err),
)
return nil, err
}
inputData := commands.NewInputData(c.bstats.Height())
wrapPayloadIntoInputData(inputData, cmd, payload)
marshalInputData, err := commands.MarshalInputData(inputData)
if err != nil {
// this should never be possible
c.log.Panic("could not marshal core transaction", logging.Error(err))
}

signature, err := c.sign(commands.BundleInputDataForSigning(marshalInputData, chainID))
if err != nil {
// this should never be possible too
c.log.Panic("could not sign command", logging.Error(err))
}

tx := commands.NewTransaction(c.wallet.PubKey().Hex(), marshalInputData, signature)
marshalledTx, err := proto.Marshal(tx)
if err != nil {
return nil, err
}
return marshalledTx, nil
}

// Command - send command to chain.
// Note: beware when passing in an exponential back off since the done function may be called many times.
func (c *Commander) Command(ctx context.Context, cmd txn.Command, payload proto.Message, done func(string, error), bo *backoff.ExponentialBackOff) {
Expand Down Expand Up @@ -165,6 +195,12 @@ func wrapPayloadIntoInputData(data *commandspb.InputData, cmd txn.Command, paylo
switch cmd {
case txn.SubmitOrderCommand, txn.CancelOrderCommand, txn.AmendOrderCommand, txn.VoteCommand, txn.WithdrawCommand, txn.LiquidityProvisionCommand, txn.ProposeCommand, txn.BatchProposeCommand, txn.SubmitOracleDataCommand, txn.StopOrdersCancellationCommand, txn.StopOrdersSubmissionCommand:
panic("command is not supported to be sent by a node.")
case txn.DelayedTransactionsWrapper:
if underlyingCmd, ok := payload.(*commandspb.DelayedTransactionsWrapper); ok {
data.Command = &commandspb.InputData_DelayedTransactionsWrapper{
DelayedTransactionsWrapper: underlyingCmd,
}
}
case txn.ProtocolUpgradeCommand:
if underlyingCmd, ok := payload.(*commandspb.ProtocolUpgradeProposal); ok {
data.Command = &commandspb.InputData_ProtocolUpgradeProposal{
Expand Down
Loading

0 comments on commit bb3e963

Please sign in to comment.