From 3b92b795e1ee4064e0326a318ab93f94bdde78cf Mon Sep 17 00:00:00 2001 From: Teddy Ding Date: Wed, 2 Oct 2024 19:33:43 -0400 Subject: [PATCH] Stage events and process in Precommitter --- proto/dydxprotocol/clob/finalize_block.proto | 13 + protocol/x/clob/abci.go | 2 + protocol/x/clob/keeper/clob_pair.go | 96 ++--- protocol/x/clob/keeper/keeper.go | 41 +- protocol/x/clob/types/finalize_block.pb.go | 387 +++++++++++++++++++ protocol/x/listing/keeper/listing.go | 38 +- protocol/x/listing/types/expected_keepers.go | 2 +- 7 files changed, 504 insertions(+), 75 deletions(-) create mode 100644 proto/dydxprotocol/clob/finalize_block.proto create mode 100644 protocol/x/clob/types/finalize_block.pb.go diff --git a/proto/dydxprotocol/clob/finalize_block.proto b/proto/dydxprotocol/clob/finalize_block.proto new file mode 100644 index 0000000000..d3b1995757 --- /dev/null +++ b/proto/dydxprotocol/clob/finalize_block.proto @@ -0,0 +1,13 @@ +syntax = "proto3"; +package dydxprotocol.clob; + +import "dydxprotocol/clob/clob_pair.proto"; + +option go_package = "github.com/dydxprotocol/v4-chain/protocol/x/clob/types"; +3 +// ClobStagedFinalizeBlockEvent defines a CLOB event staged during FinalizeBlock. +message ClobStagedFinalizeBlockEvent { + oneof event { + ClobPair create_clob_pair = 1; + } +} diff --git a/protocol/x/clob/abci.go b/protocol/x/clob/abci.go index 9fd5f4a0b5..d47b62f86a 100644 --- a/protocol/x/clob/abci.go +++ b/protocol/x/clob/abci.go @@ -51,6 +51,8 @@ func Precommit( ctx sdk.Context, keeper keeper.Keeper, ) { + keeper.ProcessStagedFinalizeBlockEvents(ctx) + if streamingManager := keeper.GetFullNodeStreamingManager(); !streamingManager.Enabled() { return } diff --git a/protocol/x/clob/keeper/clob_pair.go b/protocol/x/clob/keeper/clob_pair.go index 3b49d7c246..fc40b796de 100644 --- a/protocol/x/clob/keeper/clob_pair.go +++ b/protocol/x/clob/keeper/clob_pair.go @@ -69,11 +69,42 @@ func (k Keeper) CreatePerpetualClobPair( // Write the `ClobPair` to state. k.SetClobPair(ctx, clobPair) - err := k.CreateClobPairStructures(ctx, clobPair) + if lib.IsDeliverTxMode(ctx) { + if err := k.StageNewClobPairSideEffects(ctx, clobPair); err != nil { + return clobPair, err + } + } + + perpetualId, err := clobPair.GetPerpetualId() if err != nil { - return clobPair, err + panic(err) + } + perpetual, err := k.perpetualsKeeper.GetPerpetual(ctx, perpetualId) + if err != nil { + return types.ClobPair{}, err } + k.GetIndexerEventManager().AddTxnEvent( + ctx, + indexerevents.SubtypePerpetualMarket, + indexerevents.PerpetualMarketEventVersion, + indexer_manager.GetBytes( + indexerevents.NewPerpetualMarketCreateEvent( + perpetualId, + clobPair.Id, + perpetual.Params.Ticker, + perpetual.Params.MarketId, + clobPair.Status, + clobPair.QuantumConversionExponent, + perpetual.Params.AtomicResolution, + clobPair.SubticksPerTick, + clobPair.StepBaseQuantums, + perpetual.Params.LiquidityTier, + perpetual.Params.MarketType, + ), + ), + ) + return clobPair, nil } @@ -159,51 +190,29 @@ func (k Keeper) maybeCreateOrderbook(ctx sdk.Context, clobPair types.ClobPair) { k.MemClob.MaybeCreateOrderbook(clobPair) } -// createOrderbook creates a new orderbook in the memclob. -func (k Keeper) createOrderbook(ctx sdk.Context, clobPair types.ClobPair) { - // Create the corresponding orderbook in the memclob. +func (k Keeper) ApplySideEffectsForCNewlobPair( + ctx sdk.Context, + clobPair types.ClobPair, +) { k.MemClob.CreateOrderbook(clobPair) + k.SetClobPairIdForPerpetual(clobPair) } -// CreateClobPair performs all non stateful operations to create a CLOB pair. -// These include creating the corresponding orderbook in the memclob, the mapping between -// the CLOB pair and the perpetual and the indexer event. -// This function returns an error if a value for the ClobPair's id already exists in state. -func (k Keeper) CreateClobPairStructures(ctx sdk.Context, clobPair types.ClobPair) error { - // Create the corresponding orderbook in the memclob. - k.createOrderbook(ctx, clobPair) - - // Create the mapping between clob pair and perpetual. - k.SetClobPairIdForPerpetual(ctx, clobPair) +// StageNewClobPairSideEffects stages a ClobPair creation event, so that any in-memory side effects +// can happen later when the transaction and block is committed. +// Note the staged event will be processed only if below are both true: +// - The current transaction is committed. +// - The current block is agreed upon and committed by consensus. +func (k Keeper) StageNewClobPairSideEffects(ctx sdk.Context, clobPair types.ClobPair) error { + lib.AssertDeliverTxMode(ctx) - perpetualId, err := clobPair.GetPerpetualId() - if err != nil { - panic(err) - } - perpetual, err := k.perpetualsKeeper.GetPerpetual(ctx, perpetualId) - if err != nil { - return err - } - - k.GetIndexerEventManager().AddTxnEvent( + k.finalizeBlockEventStager.StageFinalizeBlockEvent( ctx, - indexerevents.SubtypePerpetualMarket, - indexerevents.PerpetualMarketEventVersion, - indexer_manager.GetBytes( - indexerevents.NewPerpetualMarketCreateEvent( - perpetualId, - clobPair.Id, - perpetual.Params.Ticker, - perpetual.Params.MarketId, - clobPair.Status, - clobPair.QuantumConversionExponent, - perpetual.Params.AtomicResolution, - clobPair.SubticksPerTick, - clobPair.StepBaseQuantums, - perpetual.Params.LiquidityTier, - perpetual.Params.MarketType, - ), - ), + &types.ClobStagedFinalizeBlockEvent{ + Event: &types.ClobStagedFinalizeBlockEvent_CreateClobPair{ + CreateClobPair: &clobPair, + }, + }, ) return nil @@ -234,14 +243,13 @@ func (k Keeper) HydrateClobPairAndPerpetualMapping(ctx sdk.Context) { for _, clobPair := range clobPairs { // Create the corresponding mapping between clob pair and perpetual. k.SetClobPairIdForPerpetual( - ctx, clobPair, ) } } // SetClobPairIdForPerpetual sets the mapping between clob pair and perpetual. -func (k Keeper) SetClobPairIdForPerpetual(ctx sdk.Context, clobPair types.ClobPair) { +func (k Keeper) SetClobPairIdForPerpetual(clobPair types.ClobPair) { // If this `ClobPair` is for a perpetual, add the `clobPairId` to the list of CLOB pair IDs // that facilitate trading of this perpetual. if perpetualClobMetadata := clobPair.GetPerpetualClobMetadata(); perpetualClobMetadata != nil { diff --git a/protocol/x/clob/keeper/keeper.go b/protocol/x/clob/keeper/keeper.go index dbdfdbc0c2..b74e3fdc34 100644 --- a/protocol/x/clob/keeper/keeper.go +++ b/protocol/x/clob/keeper/keeper.go @@ -5,6 +5,7 @@ import ( "fmt" "sync/atomic" + "github.com/dydxprotocol/v4-chain/protocol/finalizeblock" satypes "github.com/dydxprotocol/v4-chain/protocol/x/subaccounts/types" "cosmossdk.io/log" @@ -15,6 +16,7 @@ import ( liquidationtypes "github.com/dydxprotocol/v4-chain/protocol/daemons/server/types/liquidations" "github.com/dydxprotocol/v4-chain/protocol/indexer/indexer_manager" "github.com/dydxprotocol/v4-chain/protocol/lib" + dydxlog "github.com/dydxprotocol/v4-chain/protocol/lib/log" "github.com/dydxprotocol/v4-chain/protocol/lib/metrics" streamingtypes "github.com/dydxprotocol/v4-chain/protocol/streaming/types" flags "github.com/dydxprotocol/v4-chain/protocol/x/clob/flags" @@ -46,8 +48,9 @@ type ( revshareKeeper types.RevShareKeeper accountPlusKeeper types.AccountPlusKeeper - indexerEventManager indexer_manager.IndexerEventManager - streamingManager streamingtypes.FullNodeStreamingManager + indexerEventManager indexer_manager.IndexerEventManager + streamingManager streamingtypes.FullNodeStreamingManager + finalizeBlockEventStager finalizeblock.EventStager[*types.ClobStagedFinalizeBlockEvent] initialized *atomic.Bool memStoreInitialized *atomic.Bool @@ -199,6 +202,40 @@ func (k Keeper) Initialize(ctx sdk.Context) { k.HydrateClobPairAndPerpetualMapping(checkCtx) } +func (k Keeper) ProcessStagedFinalizeBlockEvents(ctx sdk.Context) { + stagedEvents := k.finalizeBlockEventStager.GetStagedFinalizeBlockEvents( + ctx, + func() *types.ClobStagedFinalizeBlockEvent { + return &types.ClobStagedFinalizeBlockEvent{} + }, + ) + + for _, stagedEvent := range stagedEvents { + if stagedEvent == nil { + // We don't ever expect this. However, should not panic since we are in Precommit. + dydxlog.ErrorLog( + ctx, + "got nil ClobStagedFinalizeBlockEvent, skipping", + "staged_events", + stagedEvents, + ) + continue + } + + switch event := stagedEvent.Event.(type) { + case *types.ClobStagedFinalizeBlockEvent_CreateClobPair: + k.ApplySideEffectsForCNewlobPair(ctx, *event.CreateClobPair) + default: + dydxlog.ErrorLog( + ctx, + "got unknown ClobStagedFinalizeBlockEvent", + "event", + event, + ) + } + } +} + // InitMemStore initializes the memstore of the `clob` keeper. // This is called during app initialization in `app.go`, before any ABCI calls are received. func (k Keeper) InitMemStore(ctx sdk.Context) { diff --git a/protocol/x/clob/types/finalize_block.pb.go b/protocol/x/clob/types/finalize_block.pb.go new file mode 100644 index 0000000000..d44ba184d9 --- /dev/null +++ b/protocol/x/clob/types/finalize_block.pb.go @@ -0,0 +1,387 @@ +// Code generated by protoc-gen-gogo. DO NOT EDIT. +// source: dydxprotocol/clob/finalize_block.proto + +package types + +import ( + fmt "fmt" + proto "github.com/cosmos/gogoproto/proto" + io "io" + math "math" + math_bits "math/bits" +) + +// Reference imports to suppress errors if they are not otherwise used. +var _ = proto.Marshal +var _ = fmt.Errorf +var _ = math.Inf + +// This is a compile-time assertion to ensure that this generated file +// is compatible with the proto package it is being compiled against. +// A compilation error at this line likely means your copy of the +// proto package needs to be updated. +const _ = proto.GoGoProtoPackageIsVersion3 // please upgrade the proto package + +// ClobStagedFinalizeBlockEvent defines a CLOB event staged during FinalizeBlock. +type ClobStagedFinalizeBlockEvent struct { + // Types that are valid to be assigned to Event: + // *ClobStagedFinalizeBlockEvent_CreateClobPair + Event isClobStagedFinalizeBlockEvent_Event `protobuf_oneof:"event"` +} + +func (m *ClobStagedFinalizeBlockEvent) Reset() { *m = ClobStagedFinalizeBlockEvent{} } +func (m *ClobStagedFinalizeBlockEvent) String() string { return proto.CompactTextString(m) } +func (*ClobStagedFinalizeBlockEvent) ProtoMessage() {} +func (*ClobStagedFinalizeBlockEvent) Descriptor() ([]byte, []int) { + return fileDescriptor_ce1d49660993e938, []int{0} +} +func (m *ClobStagedFinalizeBlockEvent) XXX_Unmarshal(b []byte) error { + return m.Unmarshal(b) +} +func (m *ClobStagedFinalizeBlockEvent) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + if deterministic { + return xxx_messageInfo_ClobStagedFinalizeBlockEvent.Marshal(b, m, deterministic) + } else { + b = b[:cap(b)] + n, err := m.MarshalToSizedBuffer(b) + if err != nil { + return nil, err + } + return b[:n], nil + } +} +func (m *ClobStagedFinalizeBlockEvent) XXX_Merge(src proto.Message) { + xxx_messageInfo_ClobStagedFinalizeBlockEvent.Merge(m, src) +} +func (m *ClobStagedFinalizeBlockEvent) XXX_Size() int { + return m.Size() +} +func (m *ClobStagedFinalizeBlockEvent) XXX_DiscardUnknown() { + xxx_messageInfo_ClobStagedFinalizeBlockEvent.DiscardUnknown(m) +} + +var xxx_messageInfo_ClobStagedFinalizeBlockEvent proto.InternalMessageInfo + +type isClobStagedFinalizeBlockEvent_Event interface { + isClobStagedFinalizeBlockEvent_Event() + MarshalTo([]byte) (int, error) + Size() int +} + +type ClobStagedFinalizeBlockEvent_CreateClobPair struct { + CreateClobPair *ClobPair `protobuf:"bytes,1,opt,name=create_clob_pair,json=createClobPair,proto3,oneof" json:"create_clob_pair,omitempty"` +} + +func (*ClobStagedFinalizeBlockEvent_CreateClobPair) isClobStagedFinalizeBlockEvent_Event() {} + +func (m *ClobStagedFinalizeBlockEvent) GetEvent() isClobStagedFinalizeBlockEvent_Event { + if m != nil { + return m.Event + } + return nil +} + +func (m *ClobStagedFinalizeBlockEvent) GetCreateClobPair() *ClobPair { + if x, ok := m.GetEvent().(*ClobStagedFinalizeBlockEvent_CreateClobPair); ok { + return x.CreateClobPair + } + return nil +} + +// XXX_OneofWrappers is for the internal use of the proto package. +func (*ClobStagedFinalizeBlockEvent) XXX_OneofWrappers() []interface{} { + return []interface{}{ + (*ClobStagedFinalizeBlockEvent_CreateClobPair)(nil), + } +} + +func init() { + proto.RegisterType((*ClobStagedFinalizeBlockEvent)(nil), "dydxprotocol.clob.ClobStagedFinalizeBlockEvent") +} + +func init() { + proto.RegisterFile("dydxprotocol/clob/finalize_block.proto", fileDescriptor_ce1d49660993e938) +} + +var fileDescriptor_ce1d49660993e938 = []byte{ + // 219 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0x52, 0x4b, 0xa9, 0x4c, 0xa9, + 0x28, 0x28, 0xca, 0x2f, 0xc9, 0x4f, 0xce, 0xcf, 0xd1, 0x4f, 0xce, 0xc9, 0x4f, 0xd2, 0x4f, 0xcb, + 0xcc, 0x4b, 0xcc, 0xc9, 0xac, 0x4a, 0x8d, 0x4f, 0xca, 0xc9, 0x4f, 0xce, 0xd6, 0x03, 0x4b, 0x0a, + 0x09, 0x22, 0xab, 0xd3, 0x03, 0xa9, 0x93, 0x52, 0xc4, 0xd4, 0x0a, 0x22, 0xe2, 0x0b, 0x12, 0x33, + 0x8b, 0x20, 0xba, 0x94, 0x0a, 0xb8, 0x64, 0x82, 0x4b, 0x12, 0xd3, 0x53, 0x53, 0xdc, 0xa0, 0x66, + 0x3a, 0x81, 0x8c, 0x74, 0xce, 0xc9, 0x4f, 0x72, 0x2d, 0x4b, 0xcd, 0x2b, 0x11, 0x72, 0xe7, 0x12, + 0x48, 0x2e, 0x4a, 0x4d, 0x2c, 0x49, 0x8d, 0x87, 0xeb, 0x94, 0x60, 0x54, 0x60, 0xd4, 0xe0, 0x36, + 0x92, 0xd6, 0xc3, 0xb0, 0x50, 0x0f, 0xa4, 0x2f, 0x20, 0x31, 0xb3, 0xc8, 0x83, 0x21, 0x88, 0x0f, + 0xa2, 0x0d, 0x26, 0xe2, 0xc4, 0xce, 0xc5, 0x9a, 0x0a, 0x32, 0xd1, 0x29, 0xe0, 0xc4, 0x23, 0x39, + 0xc6, 0x0b, 0x8f, 0xe4, 0x18, 0x1f, 0x3c, 0x92, 0x63, 0x9c, 0xf0, 0x58, 0x8e, 0xe1, 0xc2, 0x63, + 0x39, 0x86, 0x1b, 0x8f, 0xe5, 0x18, 0xa2, 0xcc, 0xd2, 0x33, 0x4b, 0x32, 0x4a, 0x93, 0xf4, 0x92, + 0xf3, 0x73, 0xf5, 0x51, 0x5c, 0x5e, 0x66, 0xa2, 0x9b, 0x9c, 0x91, 0x98, 0x99, 0xa7, 0x0f, 0x17, + 0xa9, 0x80, 0xf8, 0xa6, 0xa4, 0xb2, 0x20, 0xb5, 0x38, 0x89, 0x0d, 0x2c, 0x6c, 0x0c, 0x08, 0x00, + 0x00, 0xff, 0xff, 0xf6, 0x77, 0x28, 0xc3, 0x2a, 0x01, 0x00, 0x00, +} + +func (m *ClobStagedFinalizeBlockEvent) Marshal() (dAtA []byte, err error) { + size := m.Size() + dAtA = make([]byte, size) + n, err := m.MarshalToSizedBuffer(dAtA[:size]) + if err != nil { + return nil, err + } + return dAtA[:n], nil +} + +func (m *ClobStagedFinalizeBlockEvent) MarshalTo(dAtA []byte) (int, error) { + size := m.Size() + return m.MarshalToSizedBuffer(dAtA[:size]) +} + +func (m *ClobStagedFinalizeBlockEvent) MarshalToSizedBuffer(dAtA []byte) (int, error) { + i := len(dAtA) + _ = i + var l int + _ = l + if m.Event != nil { + { + size := m.Event.Size() + i -= size + if _, err := m.Event.MarshalTo(dAtA[i:]); err != nil { + return 0, err + } + } + } + return len(dAtA) - i, nil +} + +func (m *ClobStagedFinalizeBlockEvent_CreateClobPair) MarshalTo(dAtA []byte) (int, error) { + size := m.Size() + return m.MarshalToSizedBuffer(dAtA[:size]) +} + +func (m *ClobStagedFinalizeBlockEvent_CreateClobPair) MarshalToSizedBuffer(dAtA []byte) (int, error) { + i := len(dAtA) + if m.CreateClobPair != nil { + { + size, err := m.CreateClobPair.MarshalToSizedBuffer(dAtA[:i]) + if err != nil { + return 0, err + } + i -= size + i = encodeVarintFinalizeBlock(dAtA, i, uint64(size)) + } + i-- + dAtA[i] = 0xa + } + return len(dAtA) - i, nil +} +func encodeVarintFinalizeBlock(dAtA []byte, offset int, v uint64) int { + offset -= sovFinalizeBlock(v) + base := offset + for v >= 1<<7 { + dAtA[offset] = uint8(v&0x7f | 0x80) + v >>= 7 + offset++ + } + dAtA[offset] = uint8(v) + return base +} +func (m *ClobStagedFinalizeBlockEvent) Size() (n int) { + if m == nil { + return 0 + } + var l int + _ = l + if m.Event != nil { + n += m.Event.Size() + } + return n +} + +func (m *ClobStagedFinalizeBlockEvent_CreateClobPair) Size() (n int) { + if m == nil { + return 0 + } + var l int + _ = l + if m.CreateClobPair != nil { + l = m.CreateClobPair.Size() + n += 1 + l + sovFinalizeBlock(uint64(l)) + } + return n +} + +func sovFinalizeBlock(x uint64) (n int) { + return (math_bits.Len64(x|1) + 6) / 7 +} +func sozFinalizeBlock(x uint64) (n int) { + return sovFinalizeBlock(uint64((x << 1) ^ uint64((int64(x) >> 63)))) +} +func (m *ClobStagedFinalizeBlockEvent) Unmarshal(dAtA []byte) error { + l := len(dAtA) + iNdEx := 0 + for iNdEx < l { + preIndex := iNdEx + var wire uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowFinalizeBlock + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + wire |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + fieldNum := int32(wire >> 3) + wireType := int(wire & 0x7) + if wireType == 4 { + return fmt.Errorf("proto: ClobStagedFinalizeBlockEvent: wiretype end group for non-group") + } + if fieldNum <= 0 { + return fmt.Errorf("proto: ClobStagedFinalizeBlockEvent: illegal tag %d (wire type %d)", fieldNum, wire) + } + switch fieldNum { + case 1: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field CreateClobPair", wireType) + } + var msglen int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowFinalizeBlock + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + msglen |= int(b&0x7F) << shift + if b < 0x80 { + break + } + } + if msglen < 0 { + return ErrInvalidLengthFinalizeBlock + } + postIndex := iNdEx + msglen + if postIndex < 0 { + return ErrInvalidLengthFinalizeBlock + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + v := &ClobPair{} + if err := v.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + return err + } + m.Event = &ClobStagedFinalizeBlockEvent_CreateClobPair{v} + iNdEx = postIndex + default: + iNdEx = preIndex + skippy, err := skipFinalizeBlock(dAtA[iNdEx:]) + if err != nil { + return err + } + if (skippy < 0) || (iNdEx+skippy) < 0 { + return ErrInvalidLengthFinalizeBlock + } + if (iNdEx + skippy) > l { + return io.ErrUnexpectedEOF + } + iNdEx += skippy + } + } + + if iNdEx > l { + return io.ErrUnexpectedEOF + } + return nil +} +func skipFinalizeBlock(dAtA []byte) (n int, err error) { + l := len(dAtA) + iNdEx := 0 + depth := 0 + for iNdEx < l { + var wire uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return 0, ErrIntOverflowFinalizeBlock + } + if iNdEx >= l { + return 0, io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + wire |= (uint64(b) & 0x7F) << shift + if b < 0x80 { + break + } + } + wireType := int(wire & 0x7) + switch wireType { + case 0: + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return 0, ErrIntOverflowFinalizeBlock + } + if iNdEx >= l { + return 0, io.ErrUnexpectedEOF + } + iNdEx++ + if dAtA[iNdEx-1] < 0x80 { + break + } + } + case 1: + iNdEx += 8 + case 2: + var length int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return 0, ErrIntOverflowFinalizeBlock + } + if iNdEx >= l { + return 0, io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + length |= (int(b) & 0x7F) << shift + if b < 0x80 { + break + } + } + if length < 0 { + return 0, ErrInvalidLengthFinalizeBlock + } + iNdEx += length + case 3: + depth++ + case 4: + if depth == 0 { + return 0, ErrUnexpectedEndOfGroupFinalizeBlock + } + depth-- + case 5: + iNdEx += 4 + default: + return 0, fmt.Errorf("proto: illegal wireType %d", wireType) + } + if iNdEx < 0 { + return 0, ErrInvalidLengthFinalizeBlock + } + if depth == 0 { + return iNdEx, nil + } + } + return 0, io.ErrUnexpectedEOF +} + +var ( + ErrInvalidLengthFinalizeBlock = fmt.Errorf("proto: negative length found during unmarshaling") + ErrIntOverflowFinalizeBlock = fmt.Errorf("proto: integer overflow") + ErrUnexpectedEndOfGroupFinalizeBlock = fmt.Errorf("proto: unexpected end of group") +) diff --git a/protocol/x/listing/keeper/listing.go b/protocol/x/listing/keeper/listing.go index 54cab6e467..8fd37188c6 100644 --- a/protocol/x/listing/keeper/listing.go +++ b/protocol/x/listing/keeper/listing.go @@ -5,7 +5,6 @@ import ( "math" "math/big" - "github.com/dydxprotocol/v4-chain/protocol/lib" "github.com/dydxprotocol/v4-chain/protocol/lib/slinky" satypes "github.com/dydxprotocol/v4-chain/protocol/x/subaccounts/types" vaulttypes "github.com/dydxprotocol/v4-chain/protocol/x/vault/types" @@ -90,34 +89,17 @@ func (k Keeper) CreateClobPair( ) (clobPairId uint32, err error) { clobPairId = k.ClobKeeper.AcquireNextClobPairID(ctx) - clobPair := clobtypes.ClobPair{ - Metadata: &clobtypes.ClobPair_PerpetualClobMetadata{ - PerpetualClobMetadata: &clobtypes.PerpetualClobMetadata{ - PerpetualId: perpetualId, - }, - }, - Id: clobPairId, - StepBaseQuantums: types.DefaultStepBaseQuantums, - QuantumConversionExponent: types.DefaultQuantumConversionExponent, - SubticksPerTick: types.SubticksPerTick_LongTail, - Status: clobtypes.ClobPair_STATUS_ACTIVE, - } - if err := k.ClobKeeper.ValidateClobPairCreation(ctx, &clobPair); err != nil { - return 0, err - } - - k.ClobKeeper.SetClobPair(ctx, clobPair) - - // Only create the clob pair if we are in deliver tx mode. This is to prevent populating - // in memory data structures in the CLOB during simulation mode. - if lib.IsDeliverTxMode(ctx) { - err := k.ClobKeeper.CreateClobPairStructures(ctx, clobPair) - if err != nil { - return 0, err - } - } + k.ClobKeeper.CreatePerpetualClobPair( + ctx, + clobPairId, + perpetualId, + satypes.BaseQuantums(types.DefaultStepBaseQuantums), + types.DefaultQuantumConversionExponent, + types.SubticksPerTick_LongTail, + clobtypes.ClobPair_STATUS_ACTIVE, + ) - return clobPair.Id, nil + return clobPairId, nil } // Function to wrap the creation of a new perpetual diff --git a/protocol/x/listing/types/expected_keepers.go b/protocol/x/listing/types/expected_keepers.go index 7676db370e..25e3e29f95 100644 --- a/protocol/x/listing/types/expected_keepers.go +++ b/protocol/x/listing/types/expected_keepers.go @@ -35,7 +35,7 @@ type ClobKeeper interface { ) (clobtypes.ClobPair, error) AcquireNextClobPairID(ctx sdk.Context) uint32 ValidateClobPairCreation(ctx sdk.Context, clobPair *clobtypes.ClobPair) error - CreateClobPairStructures(ctx sdk.Context, clobPair clobtypes.ClobPair) error + StageNewClobPairSideEffects(ctx sdk.Context, clobPair clobtypes.ClobPair) error SetClobPair(ctx sdk.Context, clobPair clobtypes.ClobPair) }