-
Notifications
You must be signed in to change notification settings - Fork 124
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(fullnode) Add filterOrders option to streaming subscription #2676
Closed
UnbornAztecKing
wants to merge
12
commits into
main
from
mattweeks/filtered-order-stream-specific-subaccounts
Closed
Changes from 5 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
05ff9f3
feat(fullnode) Add filterOrders option to streaming subscription
UnbornAztecKing 309f2ec
Move helper to util module. Test for streaming util.
UnbornAztecKing 39213e7
Full Node Streaming Order Filter utils and tests
UnbornAztecKing 97d08ce
Tests for pass-through of non-StreamUpdate_OrderbookUpdate UpdateMess…
UnbornAztecKing 457459b
Set filteredUpdateChannel size to maxSubscriptionChannelSize
UnbornAztecKing e632ed4
Update proto/dydxprotocol/clob/query.proto comment
UnbornAztecKing 3c71f85
Simplify streaming.FilterSubaccountStreamUpdates and forward entire S…
UnbornAztecKing 1ce9ffb
Fix proto comment width
UnbornAztecKing 0c482e4
proto-format: fix comment width
UnbornAztecKing 1c24dd6
literal constants
UnbornAztecKing 69e6d40
Rename function, drop else claus, use realistic exponent in test case
UnbornAztecKing e6603a1
Fix filterOrdersBySubaccountId param token and helper name
UnbornAztecKing File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -220,68 +220,59 @@ func (sm *FullNodeStreamingManagerImpl) getNextAvailableSubscriptionId() uint32 | |
return id | ||
} | ||
|
||
func doFilterSubaccountStreamUpdate( | ||
orderBookUpdate *clobtypes.StreamUpdate_OrderbookUpdate, | ||
subaccountIdNumbers []uint32, | ||
logger log.Logger, | ||
) bool { | ||
for _, orderBookUpdate := range orderBookUpdate.OrderbookUpdate.Updates { | ||
orderBookUpdateSubaccountIdNumber, err := streaming_util.GetOffChainUpdateV1SubaccountIdNumber(orderBookUpdate) | ||
if err == nil { | ||
if slices.Contains(subaccountIdNumbers, orderBookUpdateSubaccountIdNumber) { | ||
return true | ||
} | ||
} else { | ||
logger.Error(err.Error()) | ||
} | ||
} | ||
return false | ||
} | ||
|
||
// Filter StreamUpdates for subaccountIdNumbers | ||
// If a StreamUpdate_OrderUpdate contains no updates for subscribed subaccounts, drop message | ||
// If a StreamUpdate_OrderUpdate contains updates for subscribed subaccounts, construct a new | ||
// StreamUpdate_OrderUpdate with updates only for subscribed subaccounts | ||
func (sub *OrderbookSubscription) FilterSubaccountStreamUpdates( | ||
output chan []clobtypes.StreamUpdate, | ||
func FilterSubaccountStreamUpdates( | ||
updates []clobtypes.StreamUpdate, | ||
subaccountIdNumbers []uint32, | ||
logger log.Logger, | ||
) { | ||
subaccountIdNumbers := make([]uint32, len(sub.subaccountIds)) | ||
for i, subaccountId := range sub.subaccountIds { | ||
subaccountIdNumbers[i] = subaccountId.Number | ||
} | ||
|
||
) *[]clobtypes.StreamUpdate { | ||
// If reflection becomes too expensive, split updatesChannel by message type | ||
for updates := range sub.updatesChannel { | ||
filteredUpdates := []clobtypes.StreamUpdate{} | ||
for _, update := range updates { | ||
switch updateMessage := update.UpdateMessage.(type) { | ||
case *clobtypes.StreamUpdate_OrderbookUpdate: | ||
orderBookUpdates := []ocutypes.OffChainUpdateV1{} | ||
for _, orderBookUpdate := range updateMessage.OrderbookUpdate.Updates { | ||
orderBookUpdateSubaccountIdNumber, err := streaming_util.GetOffChainUpdateV1SubaccountIdNumber(orderBookUpdate) | ||
if err == nil { | ||
if slices.Contains(subaccountIdNumbers, orderBookUpdateSubaccountIdNumber) { | ||
orderBookUpdates = append(orderBookUpdates, orderBookUpdate) | ||
} | ||
} else { | ||
logger.Error(err.Error()) | ||
} | ||
} | ||
// Drop the StreamUpdate_OrderbookUpdate if all updates inside were dropped | ||
if len(orderBookUpdates) > 0 { | ||
if len(orderBookUpdates) < len(updateMessage.OrderbookUpdate.Updates) { | ||
update = clobtypes.StreamUpdate{ | ||
BlockHeight: update.BlockHeight, | ||
ExecMode: update.ExecMode, | ||
UpdateMessage: &clobtypes.StreamUpdate_OrderbookUpdate{ | ||
OrderbookUpdate: &clobtypes.StreamOrderbookUpdate{ | ||
Snapshot: updateMessage.OrderbookUpdate.Snapshot, | ||
Updates: orderBookUpdates, | ||
}, | ||
}, | ||
} | ||
} | ||
filteredUpdates = append(filteredUpdates, update) | ||
} | ||
default: | ||
filteredUpdates := []clobtypes.StreamUpdate{} | ||
for _, update := range updates { | ||
switch updateMessage := update.UpdateMessage.(type) { | ||
case *clobtypes.StreamUpdate_OrderbookUpdate: | ||
if doFilterSubaccountStreamUpdate(updateMessage, subaccountIdNumbers, logger) { | ||
filteredUpdates = append(filteredUpdates, update) | ||
} | ||
} | ||
if len(filteredUpdates) > 0 { | ||
output <- filteredUpdates | ||
default: | ||
filteredUpdates = append(filteredUpdates, update) | ||
} | ||
} | ||
|
||
if len(filteredUpdates) > 0 { | ||
return &filteredUpdates | ||
} else { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: no need for |
||
return nil | ||
} | ||
} | ||
|
||
// Subscribe subscribes to the orderbook updates stream. | ||
func (sm *FullNodeStreamingManagerImpl) Subscribe( | ||
clobPairIds []uint32, | ||
subaccountIds []*satypes.SubaccountId, | ||
marketIds []uint32, | ||
filterOrders bool, | ||
filterOrdersBySubAccountId bool, | ||
messageSender types.OutgoingMessageSender, | ||
) ( | ||
err error, | ||
|
@@ -290,11 +281,17 @@ func (sm *FullNodeStreamingManagerImpl) Subscribe( | |
if len(clobPairIds) == 0 && len(subaccountIds) == 0 && len(marketIds) == 0 { | ||
return types.ErrInvalidStreamingRequest | ||
} | ||
if filterOrdersBySubAccountId && (len(subaccountIds) == 0) { | ||
sm.logger.Error("filterOrdersBySubaccountId with no subaccountIds") | ||
return types.ErrInvalidStreamingRequest | ||
} | ||
|
||
sm.Lock() | ||
sIds := make([]satypes.SubaccountId, len(subaccountIds)) | ||
subaccountIdNumbers := make([]uint32, len(subaccountIds)) | ||
for i, subaccountId := range subaccountIds { | ||
sIds[i] = *subaccountId | ||
subaccountIdNumbers[i] = subaccountId.Number | ||
} | ||
|
||
subscription := sm.NewOrderbookSubscription(clobPairIds, sIds, marketIds, messageSender) | ||
|
@@ -346,27 +343,15 @@ func (sm *FullNodeStreamingManagerImpl) Subscribe( | |
sm.EmitMetrics() | ||
sm.Unlock() | ||
|
||
// If filterOrders, listen to filtered channel and start filter goroutine | ||
// Error if filterOrders but no subaccounts are subscribed | ||
filteredUpdateChannel := subscription.updatesChannel | ||
if filterOrders { | ||
if len(subaccountIds) == 0 { | ||
sm.logger.Error( | ||
fmt.Sprintf( | ||
"filterOrders requires subaccountIds for subscription id: %+v", | ||
subscription.subscriptionId, | ||
), | ||
) | ||
} else { | ||
filteredUpdateChannel = make(chan []clobtypes.StreamUpdate, sm.maxSubscriptionChannelSize) | ||
defer close(filteredUpdateChannel) | ||
go subscription.FilterSubaccountStreamUpdates(filteredUpdateChannel, sm.logger) | ||
} | ||
} | ||
|
||
// Use current goroutine to consistently poll subscription channel for updates | ||
// to send through stream. | ||
for updates := range filteredUpdateChannel { | ||
for updates := range subscription.updatesChannel { | ||
if filterOrdersBySubAccountId { | ||
filteredUpdates := FilterSubaccountStreamUpdates(updates, subaccountIdNumbers, sm.logger) | ||
if filteredUpdates != nil { | ||
updates = *filteredUpdates | ||
} | ||
} | ||
metrics.IncrCounterWithLabels( | ||
metrics.GrpcSendResponseToSubscriberCount, | ||
1, | ||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: rename to
FilterStreamUpdateBySubaccount
to disambiguate from "filtering SubaccountStreamUpdate"