Skip to content

Commit

Permalink
Prepare and Process logic for Slinky
Browse files Browse the repository at this point in the history
  • Loading branch information
Eric-Warehime committed Mar 4, 2024
1 parent 7c6b3d9 commit 382d45c
Show file tree
Hide file tree
Showing 27 changed files with 1,670 additions and 306 deletions.
8 changes: 6 additions & 2 deletions protocol/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package app
import (
"context"
"encoding/json"
"github.com/dydxprotocol/v4-chain/protocol/app/prepare/prices"
"io"
"math/big"
"net/http"
Expand Down Expand Up @@ -1298,6 +1299,7 @@ func New(
app.SetPrepareCheckStater(app.PrepareCheckStater)

// PrepareProposal setup.
priceUpdateGenerator := prices.NewDefaultPriceUpdateGenerator(app.PricesKeeper)
if appFlags.NonValidatingFullNode {
app.SetPrepareProposal(prepare.FullNodePrepareProposalHandler())
} else {
Expand All @@ -1306,13 +1308,14 @@ func New(
txConfig,
app.BridgeKeeper,
app.ClobKeeper,
app.PricesKeeper,
app.PerpetualsKeeper,
priceUpdateGenerator,
),
)
}

// ProcessProposal setup.
priceUpdateDecoder := process.NewDefaultUpdateMarketPriceTxDecoder(app.PricesKeeper, app.txConfig.TxDecoder())
if appFlags.NonValidatingFullNode {
// Note: If the command-line flag `--non-validating-full-node` is enabled, this node will use
// an implementation of `ProcessProposal` which always returns `abci.ResponseProcessProposal_ACCEPT`.
Expand All @@ -1324,7 +1327,7 @@ func New(
app.ClobKeeper,
app.StakingKeeper,
app.PerpetualsKeeper,
app.PricesKeeper,
priceUpdateDecoder,
),
)
} else {
Expand All @@ -1336,6 +1339,7 @@ func New(
app.StakingKeeper,
app.PerpetualsKeeper,
app.PricesKeeper,
priceUpdateDecoder,
),
)
}
Expand Down
6 changes: 0 additions & 6 deletions protocol/app/prepare/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
bridgetypes "github.com/dydxprotocol/v4-chain/protocol/x/bridge/types"
clobtypes "github.com/dydxprotocol/v4-chain/protocol/x/clob/types"
perpstypes "github.com/dydxprotocol/v4-chain/protocol/x/perpetuals/types"
pricestypes "github.com/dydxprotocol/v4-chain/protocol/x/prices/types"
)

// PrepareClobKeeper defines the expected CLOB keeper used for `PrepareProposal`.
Expand All @@ -20,11 +19,6 @@ type PreparePerpetualsKeeper interface {
GetAddPremiumVotes(ctx sdk.Context) *perpstypes.MsgAddPremiumVotes
}

// PreparePricesKeeper defines the expected Prices keeper used for `PrepareProposal`.
type PreparePricesKeeper interface {
GetValidMarketPriceUpdates(ctx sdk.Context) *pricestypes.MsgUpdateMarketPrices
}

// PrepareBridgeKeeper defines the expected Bridge keeper used for `PrepareProposal`.
type PrepareBridgeKeeper interface {
GetAcknowledgeBridges(ctx sdk.Context, blockTimestamp time.Time) *bridgetypes.MsgAcknowledgeBridges
Expand Down
37 changes: 23 additions & 14 deletions protocol/app/prepare/prepare_proposal.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ import (
"github.com/cosmos/cosmos-sdk/telemetry"
sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/dydxprotocol/v4-chain/protocol/app/prepare/prices"
"github.com/dydxprotocol/v4-chain/protocol/lib/metrics"
pricetypes "github.com/dydxprotocol/v4-chain/protocol/x/prices/types"
slinkyabci "github.com/skip-mev/slinky/abci/types"
)

var (
Expand Down Expand Up @@ -52,8 +55,8 @@ func PrepareProposalHandler(
txConfig client.TxConfig,
bridgeKeeper PrepareBridgeKeeper,
clobKeeper PrepareClobKeeper,
pricesKeeper PreparePricesKeeper,
perpetualKeeper PreparePerpetualsKeeper,
priceUpdateGenerator prices.PriceUpdateGenerator,
) sdk.PrepareProposalHandler {
return func(ctx sdk.Context, req *abci.RequestPrepareProposal) (*abci.ResponsePrepareProposal, error) {
defer telemetry.ModuleMeasureSince(
Expand All @@ -71,8 +74,21 @@ func PrepareProposalHandler(
return &EmptyResponse, nil
}

var extCommitBzTx []byte
if len(req.Txs) >= slinkyabci.NumInjectedTxs {
extCommitBzTx = req.Txs[slinkyabci.OracleInfoIndex]
}

// get the update market prices tx
msg, err := priceUpdateGenerator.GetValidMarketPriceUpdates(ctx, extCommitBzTx)
if err != nil {
ctx.Logger().Error(fmt.Sprintf("GetValidMarketPriceUpdates error: %v", err))
recordErrorMetricsWithLabel(metrics.PricesTx)
return &EmptyResponse, nil
}

// Gather "FixedSize" group messages.
pricesTxResp, err := GetUpdateMarketPricesTx(ctx, txConfig, pricesKeeper)
pricesTxResp, err := EncodeMarketPriceUpdates(txConfig, msg)
if err != nil {
ctx.Logger().Error(fmt.Sprintf("GetUpdateMarketPricesTx error: %v", err))
recordErrorMetricsWithLabel(metrics.PricesTx)
Expand Down Expand Up @@ -180,19 +196,12 @@ func PrepareProposalHandler(
}
}

// GetUpdateMarketPricesTx returns a tx containing `MsgUpdateMarketPrices`.
func GetUpdateMarketPricesTx(
ctx sdk.Context,
// EncodeMarketPriceUpdates returns a tx containing `MsgUpdateMarketPrices`.
func EncodeMarketPriceUpdates(
txConfig client.TxConfig,
pricesKeeper PreparePricesKeeper,
msg *pricetypes.MsgUpdateMarketPrices,
) (PricesTxResponse, error) {
// Get prices to update.
msgUpdateMarketPrices := pricesKeeper.GetValidMarketPriceUpdates(ctx)
if msgUpdateMarketPrices == nil {
return PricesTxResponse{}, fmt.Errorf("MsgUpdateMarketPrices cannot be nil")
}

tx, err := EncodeMsgsIntoTxBytes(txConfig, msgUpdateMarketPrices)
tx, err := EncodeMsgsIntoTxBytes(txConfig, msg)
if err != nil {
return PricesTxResponse{}, err
}
Expand All @@ -202,7 +211,7 @@ func GetUpdateMarketPricesTx(

return PricesTxResponse{
Tx: tx,
NumMarkets: len(msgUpdateMarketPrices.MarketPriceUpdates),
NumMarkets: len(msg.MarketPriceUpdates),
}, nil
}

Expand Down
Loading

0 comments on commit 382d45c

Please sign in to comment.