Skip to content
This repository has been archived by the owner on Oct 11, 2024. It is now read-only.

Improve algorithm for sharing old orders #692

Merged
merged 52 commits into from
Feb 19, 2020

Conversation

albrow
Copy link
Contributor

@albrow albrow commented Jan 30, 2020

Fixes #638.

This PR introduces a new protocol for sharing existing orders between two peers, called the "ordersync" protocol. At a high-level, ordersync is a pull-based protocol rather than a push-based one. Upon initialization, a Mesh node will simply request orders from its peers. This PR also removes our old push-based strategy for sharing existing orders. GossipSub is now only used for sharing new orders.

The ordersync protocol is designed with forward-compatibility in mind and supports multiple "subprotocols" which define the details for what each request and response should look like. This PR includes only one subprotocol, in which all orders are returned using pagination. We can expand on this in the future to implement more efficient protocols (e.g. using cuckoo filters, a gossip layer, or prioritizing sharing orders with the best price).

Remaining TODOs:

  • Modify peer scores based on what happens during the ordersync protocol.
  • Add basic rate limiting to prevent abuse.
  • Add support for filters. ordersync providers should only give you orders that match your filter, but they currently return all orders.

@albrow albrow force-pushed the feature/better-old-order-sharing branch from 01e7a82 to 8ccc8c4 Compare January 30, 2020 21:51
@albrow albrow force-pushed the feature/better-old-order-sharing branch 2 times, most recently from b6564e8 to 0a3f365 Compare February 11, 2020 00:17
core/ordersync_subprotocols.go Outdated Show resolved Hide resolved
@albrow albrow self-assigned this Feb 18, 2020
@albrow albrow marked this pull request as ready for review February 18, 2020 22:47
@albrow
Copy link
Contributor Author

albrow commented Feb 18, 2020

@fabioberger @jalextowle I believe I addressed all your feedback and any remaining TODOs on my end. I want to leave some optimizations and improvements for future PRs. This one is ready for final review.

I'll create new GitHub issues for any important tasks that are remaining.

@albrow albrow changed the title [WIP] Improve algorithm for sharing old orders Improve algorithm for sharing old orders Feb 18, 2020
core/ordersync_subprotocols.go Outdated Show resolved Hide resolved
log.WithFields(map[string]interface{}{
"orderHash": acceptedOrderInfo.OrderHash.Hex(),
"from": res.ProviderID.Pretty(),
}).Info("received new valid order from peer via ordersync")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
}).Info("received new valid order from peer via ordersync")
}).Info("received new valid order from peer")

Let's not modify the log message since the data pipeline is querying for it exactly. If we want to distinguish between those discovered via ordersync and gossipsub, let's add a source field to the log.

} else if !matches {
p.app.handlePeerScoreEvent(res.ProviderID, psReceivedOrderDoesNotMatchFilter)
}
}
_, err := p.app.orderWatcher.ValidateAndStoreValidOrders(ctx, res.Orders, false, p.app.chainID)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
_, err := p.app.orderWatcher.ValidateAndStoreValidOrders(ctx, res.Orders, false, p.app.chainID)
_, err := p.app.orderWatcher.ValidateAndStoreValidOrders(ctx, filteredOrders, false, p.app.chainID)

// SupportedSubprotocols returns the subprotocols that are supported by the service.
func (s *Service) SupportedSubprotocols() []string {
sids := []string{}
for sid := range s.subprotocols {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This snippet suggests that this array can have a random order (unless I'm missing something): https://play.golang.org/p/s3WTo2LVuyl. With this in mind, we aren't actually enforcing a preference on these subprotocols. What do you think about keeping the original subprotocols array in the Service object?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Arrays and slices do not have randomized order. Maps have randomized order. When we implement more subprotocols we just need to add an additional field to Service to keep track of the original order for the subprotocols that were passed in.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your last sentence was the point of my comment. Is there a reason not to fix this now though?

core/ordersync/ordersync.go Show resolved Hide resolved
core/ordersync/ordersync.go Show resolved Hide resolved
if err != nil {
return err
}
s.handlePeerScoreEvent(providerID, psValidMessage)
Copy link
Contributor

@jalextowle jalextowle Feb 19, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see that we parse the response below with the subprotocol, but I still question whether we should give a peer a psValidMessage peer score when we cannot decode their response as a valid rawResponse. This relates to the two other comments about not propagating the error.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is addressed by the other changes. We only add the psValidMessage score when receiving a valid message.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, I just wanted to mention this in case not returning the err was intentional.

core/ordersync_subprotocols.go Outdated Show resolved Hide resolved
Copy link
Contributor

@jalextowle jalextowle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good to me once my outstanding comment is addressed

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants