Skip to content

Commit 8952bab

Browse files
authored
Revert "merge finalized subaccount updates #2230" (#2348)
1 parent 58f503c commit 8952bab

File tree

12 files changed

+108
-160
lines changed

12 files changed

+108
-160
lines changed

protocol/lib/metrics/metric_keys.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -66,19 +66,19 @@ const (
6666
GateWithdrawalsIfNegativeTncSubaccountSeenLatency = "gate_withdrawals_if_negative_tnc_subaccount_seen_latency"
6767

6868
// Full node grpc
69-
FullNodeGrpc = "full_node_grpc"
70-
GrpcSendOrderbookUpdatesLatency = "grpc_send_orderbook_updates_latency"
71-
GrpcSendOrderbookSnapshotLatency = "grpc_send_orderbook_snapshot_latency"
72-
GrpcSendSubaccountSnapshotLatency = "grpc_send_subaccount_snapshot_latency"
73-
GrpcSendOrderbookFillsLatency = "grpc_send_orderbook_fills_latency"
74-
GrpcSendFinalizedSubaccountUpdatesLatency = "grpc_send_finalized_subaccount_updates_latency"
75-
GrpcAddUpdateToBufferCount = "grpc_add_update_to_buffer_count"
76-
GrpcAddToSubscriptionChannelCount = "grpc_add_to_subscription_channel_count"
77-
GrpcSendResponseToSubscriberCount = "grpc_send_response_to_subscriber_count"
78-
GrpcStreamSubscriberCount = "grpc_stream_subscriber_count"
79-
GrpcStreamNumUpdatesBuffered = "grpc_stream_num_updates_buffered"
80-
GrpcFlushUpdatesLatency = "grpc_flush_updates_latency"
81-
GrpcSubscriptionChannelLength = "grpc_subscription_channel_length"
69+
FullNodeGrpc = "full_node_grpc"
70+
GrpcSendOrderbookUpdatesLatency = "grpc_send_orderbook_updates_latency"
71+
GrpcSendOrderbookSnapshotLatency = "grpc_send_orderbook_snapshot_latency"
72+
GrpcSendSubaccountSnapshotLatency = "grpc_send_subaccount_snapshot_latency"
73+
GrpcSendOrderbookFillsLatency = "grpc_send_orderbook_fills_latency"
74+
GrpcSendSubaccountUpdatesLatency = "grpc_send_subaccount_updates_latency"
75+
GrpcAddUpdateToBufferCount = "grpc_add_update_to_buffer_count"
76+
GrpcAddToSubscriptionChannelCount = "grpc_add_to_subscription_channel_count"
77+
GrpcSendResponseToSubscriberCount = "grpc_send_response_to_subscriber_count"
78+
GrpcStreamSubscriberCount = "grpc_stream_subscriber_count"
79+
GrpcStreamNumUpdatesBuffered = "grpc_stream_num_updates_buffered"
80+
GrpcFlushUpdatesLatency = "grpc_flush_updates_latency"
81+
GrpcSubscriptionChannelLength = "grpc_subscription_channel_length"
8282

8383
EndBlocker = "end_blocker"
8484
EndBlockerLag = "end_blocker_lag"

protocol/mocks/ClobKeeper.go

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

protocol/streaming/full_node_streaming_manager.go

Lines changed: 31 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package streaming
33
import (
44
"fmt"
55
"sync"
6-
"sync/atomic"
76
"time"
87

98
satypes "github.com/dydxprotocol/v4-chain/protocol/x/subaccounts/types"
@@ -56,8 +55,8 @@ type FullNodeStreamingManagerImpl struct {
5655
type OrderbookSubscription struct {
5756
subscriptionId uint32
5857

59-
// Whether the subscription is initialized with snapshot.
60-
initialized *atomic.Bool
58+
// Initialize the subscription with orderbook snapshots.
59+
initialize *sync.Once
6160

6261
// Clob pair ids to subscribe to.
6362
clobPairIds []uint32
@@ -76,10 +75,6 @@ type OrderbookSubscription struct {
7675
nextSnapshotBlock uint32
7776
}
7877

79-
func (sub *OrderbookSubscription) IsInitialized() bool {
80-
return sub.initialized.Load()
81-
}
82-
8378
func NewFullNodeStreamingManager(
8479
logger log.Logger,
8580
flushIntervalMs uint32,
@@ -164,7 +159,7 @@ func (sm *FullNodeStreamingManagerImpl) Subscribe(
164159
}
165160
subscription := &OrderbookSubscription{
166161
subscriptionId: sm.nextSubscriptionId,
167-
initialized: &atomic.Bool{}, // False by default.
162+
initialize: &sync.Once{},
168163
clobPairIds: clobPairIds,
169164
subaccountIds: sIds,
170165
messageSender: messageSender,
@@ -516,23 +511,19 @@ func (sm *FullNodeStreamingManagerImpl) SendTakerOrderStatus(
516511
)
517512
}
518513

519-
// SendFinalizedSubaccountUpdates groups subaccount updates by their subaccount ids and
514+
// SendSubaccountUpdates groups subaccount updates by their subaccount ids and
520515
// sends messages to the subscribers.
521-
func (sm *FullNodeStreamingManagerImpl) SendFinalizedSubaccountUpdates(
516+
func (sm *FullNodeStreamingManagerImpl) SendSubaccountUpdates(
522517
subaccountUpdates []satypes.StreamSubaccountUpdate,
523518
blockHeight uint32,
524519
execMode sdk.ExecMode,
525520
) {
526521
defer metrics.ModuleMeasureSince(
527522
metrics.FullNodeGrpc,
528-
metrics.GrpcSendFinalizedSubaccountUpdatesLatency,
523+
metrics.GrpcSendSubaccountUpdatesLatency,
529524
time.Now(),
530525
)
531526

532-
if execMode != sdk.ExecModeFinalize {
533-
panic("SendFinalizedSubaccountUpdates should only be called in ExecModeFinalize")
534-
}
535-
536527
// Group subaccount updates by subaccount id.
537528
streamUpdates := make([]clobtypes.StreamUpdate, 0)
538529
subaccountIds := make([]*satypes.SubaccountId, 0)
@@ -679,33 +670,9 @@ func (sm *FullNodeStreamingManagerImpl) FlushStreamUpdatesWithLock() {
679670
sm.EmitMetrics()
680671
}
681672

682-
func (sm *FullNodeStreamingManagerImpl) GetSubaccountSnapshotsForInitStreams(
683-
getSubaccountSnapshot func(subaccountId satypes.SubaccountId) *satypes.StreamSubaccountUpdate,
684-
) map[satypes.SubaccountId]*satypes.StreamSubaccountUpdate {
685-
sm.Lock()
686-
defer sm.Unlock()
687-
688-
ret := make(map[satypes.SubaccountId]*satypes.StreamSubaccountUpdate)
689-
for _, subscription := range sm.orderbookSubscriptions {
690-
// If the subscription has been initialized, no need to grab the subaccount snapshot.
691-
if alreadyInitialized := subscription.initialized.Load(); alreadyInitialized {
692-
continue
693-
}
694-
695-
for _, subaccountId := range subscription.subaccountIds {
696-
if _, exists := ret[subaccountId]; exists {
697-
continue
698-
}
699-
700-
ret[subaccountId] = getSubaccountSnapshot(subaccountId)
701-
}
702-
}
703-
return ret
704-
}
705-
706673
func (sm *FullNodeStreamingManagerImpl) InitializeNewStreams(
707674
getOrderbookSnapshot func(clobPairId clobtypes.ClobPairId) *clobtypes.OffchainUpdates,
708-
subaccountSnapshots map[satypes.SubaccountId]*satypes.StreamSubaccountUpdate,
675+
getSubaccountSnapshot func(subaccountId satypes.SubaccountId) *satypes.StreamSubaccountUpdate,
709676
blockHeight uint32,
710677
execMode sdk.ExecMode,
711678
) {
@@ -719,40 +686,31 @@ func (sm *FullNodeStreamingManagerImpl) InitializeNewStreams(
719686
updatesByClobPairId := make(map[uint32]*clobtypes.OffchainUpdates)
720687

721688
for subscriptionId, subscription := range sm.orderbookSubscriptions {
722-
if alreadyInitialized := subscription.initialized.Swap(true); !alreadyInitialized {
723-
allUpdates := clobtypes.NewOffchainUpdates()
724-
for _, clobPairId := range subscription.clobPairIds {
725-
if _, ok := updatesByClobPairId[clobPairId]; !ok {
726-
updatesByClobPairId[clobPairId] = getOrderbookSnapshot(clobtypes.ClobPairId(clobPairId))
727-
}
728-
allUpdates.Append(updatesByClobPairId[clobPairId])
729-
}
730-
731-
saUpdates := []*satypes.StreamSubaccountUpdate{}
732-
for _, subaccountId := range subscription.subaccountIds {
733-
// The subaccount snapshot may not exist due to the following race condition
734-
// 1. At beginning of PrepareCheckState we get snapshot for all subscribed subaccounts.
735-
// 2. A new subaccount is subscribed to by a new subscription.
736-
// 3. InitializeNewStreams is called.
737-
// Then the new subaccount would not be included in the snapshot.
738-
// We are okay with this behavior.
739-
if saUpdate, ok := subaccountSnapshots[subaccountId]; ok {
740-
saUpdates = append(saUpdates, saUpdate)
741-
}
742-
}
743-
744-
sm.SendCombinedSnapshot(allUpdates, saUpdates, subscriptionId, blockHeight, execMode)
745-
746-
if sm.snapshotBlockInterval != 0 {
747-
subscription.nextSnapshotBlock = blockHeight + sm.snapshotBlockInterval
748-
}
749-
}
750-
751-
// If the snapshot block interval is enabled and the next block is a snapshot block,
752-
// reset the `atomic.Bool` so snapshots are sent for the next block.
689+
// If the snapshot block interval is enabled, reset the sync.Once in order to
690+
// re-send snapshots out.
753691
if sm.snapshotBlockInterval > 0 &&
754-
blockHeight+1 == subscription.nextSnapshotBlock {
755-
subscription.initialized = &atomic.Bool{} // False by default.
692+
blockHeight == subscription.nextSnapshotBlock {
693+
subscription.initialize = &sync.Once{}
756694
}
695+
696+
subscription.initialize.Do(
697+
func() {
698+
allUpdates := clobtypes.NewOffchainUpdates()
699+
for _, clobPairId := range subscription.clobPairIds {
700+
if _, ok := updatesByClobPairId[clobPairId]; !ok {
701+
updatesByClobPairId[clobPairId] = getOrderbookSnapshot(clobtypes.ClobPairId(clobPairId))
702+
}
703+
allUpdates.Append(updatesByClobPairId[clobPairId])
704+
}
705+
saUpdates := []*satypes.StreamSubaccountUpdate{}
706+
for _, subaccountId := range subscription.subaccountIds {
707+
saUpdates = append(saUpdates, getSubaccountSnapshot(subaccountId))
708+
}
709+
sm.SendCombinedSnapshot(allUpdates, saUpdates, subscriptionId, blockHeight, execMode)
710+
if sm.snapshotBlockInterval != 0 {
711+
subscription.nextSnapshotBlock = blockHeight + sm.snapshotBlockInterval
712+
}
713+
},
714+
)
757715
}
758716
}

protocol/streaming/noop_streaming_manager.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func (sm *NoopGrpcStreamingManager) SendTakerOrderStatus(
5151
) {
5252
}
5353

54-
func (sm *NoopGrpcStreamingManager) SendFinalizedSubaccountUpdates(
54+
func (sm *NoopGrpcStreamingManager) SendSubaccountUpdates(
5555
subaccountUpdates []satypes.StreamSubaccountUpdate,
5656
blockHeight uint32,
5757
execMode sdk.ExecMode,
@@ -62,15 +62,9 @@ func (sm *NoopGrpcStreamingManager) TracksSubaccountId(id satypes.SubaccountId)
6262
return false
6363
}
6464

65-
func (sm *NoopGrpcStreamingManager) GetSubaccountSnapshotsForInitStreams(
66-
getSubaccountSnapshot func(subaccountId satypes.SubaccountId) *satypes.StreamSubaccountUpdate,
67-
) map[satypes.SubaccountId]*satypes.StreamSubaccountUpdate {
68-
return nil
69-
}
70-
7165
func (sm *NoopGrpcStreamingManager) InitializeNewStreams(
7266
getOrderbookSnapshot func(clobPairId clobtypes.ClobPairId) *clobtypes.OffchainUpdates,
73-
subaccountSnapshots map[satypes.SubaccountId]*satypes.StreamSubaccountUpdate,
67+
getSubaccountSnapshot func(subaccountId satypes.SubaccountId) *satypes.StreamSubaccountUpdate,
7468
blockHeight uint32,
7569
execMode sdk.ExecMode,
7670
) {

protocol/streaming/types/interface.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,10 @@ type FullNodeStreamingManager interface {
2222
// L3+ Orderbook updates.
2323
InitializeNewStreams(
2424
getOrderbookSnapshot func(clobPairId clobtypes.ClobPairId) *clobtypes.OffchainUpdates,
25-
subaccountSnapshots map[satypes.SubaccountId]*satypes.StreamSubaccountUpdate,
25+
getSubaccountSnapshot func(subaccountId satypes.SubaccountId) *satypes.StreamSubaccountUpdate,
2626
blockHeight uint32,
2727
execMode sdk.ExecMode,
2828
)
29-
GetSubaccountSnapshotsForInitStreams(
30-
getSubaccountSnapshot func(subaccountId satypes.SubaccountId) *satypes.StreamSubaccountUpdate,
31-
) map[satypes.SubaccountId]*satypes.StreamSubaccountUpdate
3229
SendOrderbookUpdates(
3330
offchainUpdates *clobtypes.OffchainUpdates,
3431
blockHeight uint32,
@@ -45,7 +42,7 @@ type FullNodeStreamingManager interface {
4542
blockHeight uint32,
4643
execMode sdk.ExecMode,
4744
)
48-
SendFinalizedSubaccountUpdates(
45+
SendSubaccountUpdates(
4946
subaccountUpdates []satypes.StreamSubaccountUpdate,
5047
blockHeight uint32,
5148
execMode sdk.ExecMode,

protocol/x/clob/abci.go

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import (
1414
"github.com/dydxprotocol/v4-chain/protocol/lib/metrics"
1515
"github.com/dydxprotocol/v4-chain/protocol/x/clob/keeper"
1616
"github.com/dydxprotocol/v4-chain/protocol/x/clob/types"
17-
satypes "github.com/dydxprotocol/v4-chain/protocol/x/subaccounts/types"
1817
)
1918

2019
// PreBlocker executes all ABCI PreBlock logic respective to the clob module.
@@ -147,15 +146,6 @@ func PrepareCheckState(
147146
log.BlockHeight, ctx.BlockHeight()+1,
148147
)
149148

150-
// We just committed block `h`, preparing `CheckState` of `h+1`
151-
// Before we modify the `CheckState`, we first take the snapshot of
152-
// the subscribed subaccounts at the end of block `h`. This we send finalized state of
153-
// the subaccounts below in `InitializeNewStreams`.
154-
var subaccountSnapshots map[satypes.SubaccountId]*satypes.StreamSubaccountUpdate
155-
if keeper.GetFullNodeStreamingManager().Enabled() {
156-
subaccountSnapshots = keeper.GetSubaccountSnapshotsForInitStreams(ctx)
157-
}
158-
159149
// Get the events generated from processing the matches in the latest block.
160150
processProposerMatchesEvents := keeper.GetProcessProposerMatchesEvents(ctx)
161151
if ctx.BlockHeight() != int64(processProposerMatchesEvents.BlockHeight) {
@@ -269,11 +259,7 @@ func PrepareCheckState(
269259
)
270260

271261
// Initialize new streams with orderbook snapshots, if any.
272-
keeper.InitializeNewStreams(
273-
ctx,
274-
// Use the subaccount snapshot at the top of function to initialize the streams.
275-
subaccountSnapshots,
276-
)
262+
keeper.InitializeNewStreams(ctx)
277263

278264
// Set per-orderbook gauges.
279265
keeper.MemClob.SetMemclobGauges(ctx)

protocol/x/clob/keeper/keeper.go

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@ package keeper
33
import (
44
"errors"
55
"fmt"
6-
"sync/atomic"
7-
86
satypes "github.com/dydxprotocol/v4-chain/protocol/x/subaccounts/types"
7+
"sync/atomic"
98

109
"cosmossdk.io/log"
1110
"cosmossdk.io/store/prefix"
@@ -257,31 +256,9 @@ func (k *Keeper) SetAnteHandler(anteHandler sdk.AnteHandler) {
257256
k.antehandler = anteHandler
258257
}
259258

260-
func (k Keeper) GetSubaccountSnapshotsForInitStreams(
261-
ctx sdk.Context,
262-
) (
263-
subaccountSnapshots map[satypes.SubaccountId]*satypes.StreamSubaccountUpdate,
264-
) {
265-
lib.AssertCheckTxMode(ctx)
266-
267-
return k.GetFullNodeStreamingManager().GetSubaccountSnapshotsForInitStreams(
268-
func(subaccountId satypes.SubaccountId) *satypes.StreamSubaccountUpdate {
269-
subaccountUpdate := k.subaccountsKeeper.GetStreamSubaccountUpdate(
270-
ctx,
271-
subaccountId,
272-
true,
273-
)
274-
return &subaccountUpdate
275-
},
276-
)
277-
}
278-
279259
// InitializeNewStreams initializes new streams for all uninitialized clob pairs
280260
// by sending the corresponding orderbook snapshots.
281-
func (k Keeper) InitializeNewStreams(
282-
ctx sdk.Context,
283-
subaccountSnapshots map[satypes.SubaccountId]*satypes.StreamSubaccountUpdate,
284-
) {
261+
func (k Keeper) InitializeNewStreams(ctx sdk.Context) {
285262
streamingManager := k.GetFullNodeStreamingManager()
286263

287264
streamingManager.InitializeNewStreams(
@@ -291,7 +268,14 @@ func (k Keeper) InitializeNewStreams(
291268
clobPairId,
292269
)
293270
},
294-
subaccountSnapshots,
271+
func(subaccountId satypes.SubaccountId) *satypes.StreamSubaccountUpdate {
272+
subaccountUpdate := k.subaccountsKeeper.GetStreamSubaccountUpdate(
273+
ctx,
274+
subaccountId,
275+
true,
276+
)
277+
return &subaccountUpdate
278+
},
295279
lib.MustConvertIntegerToUint32(ctx.BlockHeight()),
296280
ctx.ExecMode(),
297281
)

0 commit comments

Comments
 (0)