Skip to content
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

services/horizon/internal/actions: Add /order_book ingestion endpoint #1761

Merged

Conversation

tamirms
Copy link
Contributor

@tamirms tamirms commented Sep 19, 2019

PR Checklist

PR Structure

  • This PR has reasonably narrow scope (if not, break it down into smaller PRs).
  • This PR avoids mixing refactoring changes with feature changes (split into two PRs
    otherwise).
  • This PR's title starts with name of package that is most changed in the PR, ex.
    services/friendbot, or all or doc if the changes are broad or impact many
    packages.

Thoroughness

  • This PR adds tests for the most critical parts of the new functionality or fixes.
  • I've updated any docs (developer docs, .md
    files, etc... affected by this change). Take a look in the docs folder for a given service,
    like this one.

Release planning

  • I've updated the relevant CHANGELOG (here for Horizon) if
    needed with deprecations, added features, breaking changes, and DB schema changes.
  • I've decided if this PR requires a new major/minor version according to
    semver, or if it's mainly a patch change. The PR is targeted at the next
    release branch if it's not a patch change.

Summary

Implement /order_book implement using the in memory order book graph populated by the experimental ingestion system.

Fixes #1705

Goal and scope

The current /order_book endpoint queries Stellar Core's database to find all the bids and asks for a given trading pair. The experimental ingestion system provides us with an in memory order book graph. This allows us to implement the /order_book endpoint without querying Stellar Core's database at all.

The goal of the experimental ingestion system is that we reduce the coupling between horizon and Stellar Core's database. Implementing the /order_book endpoint using the in memory order book brings us one step closer towards that goal.

Note that I've still kept the old /order_book handler which queries Stellar Core's database. The old handler will still be used by horizon instances that choose not to run the experimental ingestion system.

Summary of changes

  • add function to order book graph to find all offers spanning a given number of price levels
  • implement action for the /order_book endpoint which can be used by both the JSON and SSE handlers
  • add tests

@codecov
Copy link

codecov bot commented Sep 19, 2019

Codecov Report

❗ No coverage uploaded for pull request base (release-horizon-v0.22.0@3c46f3c). Click here to learn what that means.
The diff coverage is 69.48%.

Impacted file tree graph

@@                    Coverage Diff                     @@
##             release-horizon-v0.22.0    #1761   +/-   ##
==========================================================
  Coverage                           ?   45.31%           
==========================================================
  Files                              ?      393           
  Lines                              ?    26853           
  Branches                           ?        0           
==========================================================
  Hits                               ?    12168           
  Misses                             ?    13443           
  Partials                           ?     1242
Impacted Files Coverage Δ
services/horizon/internal/ledger/ledger_source.go 22.91% <0%> (ø)
services/horizon/internal/web.go 86.13% <100%> (ø)
services/horizon/internal/app.go 57.01% <100%> (ø)
services/horizon/internal/handler.go 40.78% <38.7%> (ø)
services/horizon/internal/actions/orderbook.go 83.09% <83.09%> (ø)
exp/orderbook/graph.go 88.23% <91.3%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 3c46f3c...73bd86d. Read the comment docs.

@tamirms tamirms force-pushed the orderbook-ingest-endpoint branch 2 times, most recently from 7bc46ba to 4f2034f Compare September 20, 2019 13:58
@tamirms tamirms marked this pull request as ready for review September 20, 2019 14:29
@tamirms
Copy link
Contributor Author

tamirms commented Sep 20, 2019

@abuiles please ignore this PR while you are on vacation :) . I added you so you can skim through the PR when you get back

Copy link
Contributor

@bartekn bartekn left a comment

Choose a reason for hiding this comment

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

Looks good but requires some changes. Haven't checked tests yet.

Amount: amount.String(total),
})

delete(amountForPrice, offerPrice)
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't we use offer.Price here? offerPrice can be inverted.

Suggested change
delete(amountForPrice, offerPrice)
delete(amountForPrice, offer.Price)

response.Bids = offersToPriceLevels(
handler.OrderBookGraph.FindOffers(buying, selling, limit),
true,
)
Copy link
Contributor

Choose a reason for hiding this comment

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

I think it's possible that the first call to OrderBookGraph.FindOffers will get offers for ledger X and the second one for ledger X+1. I think we need to make a new function (GetOrderbook) that calls two FindOffers in a locked section of code.

}

response.Asks = offersToPriceLevels(
handler.OrderBookGraph.FindOffers(selling, buying, limit),
Copy link
Contributor

Choose a reason for hiding this comment

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

I think limit determines the max number of price levels not offers in the old code. If the limit is, say, 10 and there are more than 10 offers at a the first price level the result will be invalid. We need to create GetOrderbook on order book graph anyway (check the other comment) so should be easy to fix.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

limit determines the max number of price levels in the new code as well

Copy link
Contributor

Choose a reason for hiding this comment

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

You're right, sorry!

@tamirms
Copy link
Contributor Author

tamirms commented Sep 25, 2019

@bartekn I pushed a commit which addressed your PR comments

Copy link
Contributor

@bartekn bartekn left a comment

Choose a reason for hiding this comment

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

LGTM except overflow issue. Will do a last check after fix is ready.

services/horizon/internal/actions/orderbook.go Outdated Show resolved Hide resolved

amountForPrice := map[xdr.Price]xdr.Int64{}
for _, offer := range offers {
amountForPrice[offer.Price] += offer.Amount
Copy link
Contributor

Choose a reason for hiding this comment

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

This reminded me of a similar issue from the past: #436. Basically issuers can send MAX_INT64 to any of it's trustors. If two of them with MAX_INT64 create offers with the same price this can overflow. Looks like we may need to use math/big here and then convert numbers using amount.IntStringToAmount.

It seems that existing /order_book endpoint deals with it because postgres handles overflows properly (so doesn't overflow).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@bartekn great catch, I've pushed a commit which fixes this

Copy link
Contributor

@bartekn bartekn left a comment

Choose a reason for hiding this comment

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

Thanks, looks great!

@tamirms tamirms merged commit 6675d2d into stellar:release-horizon-v0.22.0 Sep 26, 2019
@tamirms tamirms deleted the orderbook-ingest-endpoint branch September 26, 2019 12:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants