Skip to content

Commit

Permalink
services/horizon/interal/actions: Drop db2/core dependency in path fi…
Browse files Browse the repository at this point in the history
…nding actions. (#2675)

- Drop `db2/core` dependency on path finding actions.
- Simplify tests for path finding, removing dependency on pre-built scenario.
- Add mock for `paths.Finder`

### Why

We are in the process of removing Horizon's dependency on `core`'s db. This PR removes such dependency on the path finding end-points and changes the tests to not depend on pre-built SQL scenarios.

This is part of the work to drop `db2/core` (#2643).
  • Loading branch information
abuiles authored Jun 8, 2020
1 parent 976f2f5 commit 954ddeb
Show file tree
Hide file tree
Showing 6 changed files with 335 additions and 154 deletions.
27 changes: 18 additions & 9 deletions services/horizon/internal/actions_path.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ package horizon

import (
"context"
"database/sql"
"fmt"
"net/http"
"strings"

"github.com/stellar/go/amount"
"github.com/stellar/go/protocols/horizon"
"github.com/stellar/go/services/horizon/internal/actions"
"github.com/stellar/go/services/horizon/internal/db2/core"
"github.com/stellar/go/services/horizon/internal/db2/history"
"github.com/stellar/go/services/horizon/internal/ledger"
"github.com/stellar/go/services/horizon/internal/paths"
hProblem "github.com/stellar/go/services/horizon/internal/render/problem"
Expand All @@ -30,7 +31,7 @@ type FindPathsHandler struct {
setLastLedgerHeader bool
maxAssetsParamLength int
pathFinder paths.Finder
coreQ *core.Q
historyQ *history.Q
}

// StrictReceivePathsQuery query struct for paths/strict-send end-point
Expand Down Expand Up @@ -154,9 +155,7 @@ func (handler FindPathsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request
sourceAccount := xdr.MustAddress(sourceAccount)
query.SourceAccount = &sourceAccount
query.ValidateSourceBalance = true
query.SourceAssets, query.SourceAssetBalances, err = handler.coreQ.AssetsForAddress(
query.SourceAccount.Address(),
)
query.SourceAssets, query.SourceAssetBalances, err = assetsForAddress(handler.historyQ, query.SourceAccount.Address())
if err != nil {
problem.Render(ctx, w, err)
return
Expand Down Expand Up @@ -212,7 +211,7 @@ type FindFixedPathsHandler struct {
maxAssetsParamLength int
setLastLedgerHeader bool
pathFinder paths.Finder
coreQ *core.Q
historyQ *history.Q
}

var destinationAssetsOrDestinationAccount = problem.P{
Expand Down Expand Up @@ -320,9 +319,7 @@ func (handler FindFixedPathsHandler) ServeHTTP(w http.ResponseWriter, r *http.Re
}

if destinationAccount != "" {
destinationAssets, _, err = handler.coreQ.AssetsForAddress(
destinationAccount,
)
destinationAssets, _, err = assetsForAddress(handler.historyQ, destinationAccount)
if err != nil {
problem.Render(ctx, w, err)
return
Expand Down Expand Up @@ -359,3 +356,15 @@ func (handler FindFixedPathsHandler) ServeHTTP(w http.ResponseWriter, r *http.Re

renderPaths(ctx, records, w)
}

func assetsForAddress(historyQ *history.Q, addy string) ([]xdr.Asset, []xdr.Int64, error) {
err := historyQ.BeginTx(&sql.TxOptions{
Isolation: sql.LevelRepeatableRead,
ReadOnly: true,
})
if err != nil {
return nil, nil, err
}
defer historyQ.Rollback()
return historyQ.AssetsForAddress(addy)
}
Loading

0 comments on commit 954ddeb

Please sign in to comment.