Skip to content
This repository has been archived by the owner on Jan 22, 2020. It is now read-only.

Commit

Permalink
Merge pull request #320 from nullstyle/trade-fixes
Browse files Browse the repository at this point in the history
Various fixes related to trades, move to new, common db package
  • Loading branch information
nullstyle authored Jan 10, 2017
2 parents 7039486 + 9592c31 commit 2353b16
Show file tree
Hide file tree
Showing 21 changed files with 318 additions and 477 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ docs/public
/vendor/src
*.bts
*.swp
*.swo

/test.go
/tls/*.crt
Expand Down
18 changes: 16 additions & 2 deletions src/github.com/stellar/horizon/actions_account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,25 @@ func TestAccountActions_ShowRegressions(t *testing.T) {
ht := StartHTTPTest(t, "base")
defer ht.Finish()

w := ht.Get(
"/accounts/GBRPYHIL2CI3FNQ4BXLFMNDLFJUNPU2HY3ZMFSHONUCEOASW7QC7OX2H",
)
var result resource.Account
err := json.Unmarshal(w.Body.Bytes(), &result)
ht.Require.NoError(err)

// Regression: no trades link
ht.Assert.Contains(result.Links.Trades.Href, "/trades")

// Regression: no data link
ht.Assert.Contains(result.Links.Data.Href, "/data/{key}")
ht.Assert.True(result.Links.Data.Templated)

// Regression: return 200 ok even when the history record cannot be found.

// overwrite history with blank,
// overwrite history with blank
ht.T.ScenarioWithoutHorizon("base")
w := ht.Get(
w = ht.Get(
"/accounts/GBRPYHIL2CI3FNQ4BXLFMNDLFJUNPU2HY3ZMFSHONUCEOASW7QC7OX2H",
)
ht.Assert.Equal(200, w.Code)
Expand Down
2 changes: 1 addition & 1 deletion src/github.com/stellar/horizon/actions_trade.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (action *TradeIndexAction) loadRecords() {
trades = trades.ForAccount(action.AccountFilter)
}

if (action.Selling != xdr.Asset{} && action.Buying != xdr.Asset{}) {
if (action.Selling != xdr.Asset{} || action.Buying != xdr.Asset{}) {
trades = trades.ForOrderBook(action.Selling, action.Buying)
}

Expand Down
17 changes: 17 additions & 0 deletions src/github.com/stellar/horizon/actions_trade_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,20 @@ func TestTradeActions_Index(t *testing.T) {
ht.Assert.PageOf(1, w.Body)
}
}

func TestTradeActions_IndexRegressions(t *testing.T) {
ht := StartHTTPTest(t, "trades")
defer ht.Finish()

// Regression: https://github.com/stellar/horizon/issues/318
var q = make(url.Values)
q.Add("selling_asset_type", "credit_alphanum4")
q.Add("selling_asset_code", "EUR")
q.Add("selling_asset_issuer", "GCQPYGH4K57XBDENKKX55KDTWOTK5WDWRQOH2LHEDX3EKVIQRLMESGBG")
q.Add("buying_asset_type", "native")

w := ht.Get("/order_book/trades?" + q.Encode())
if ht.Assert.Equal(200, w.Code) {
ht.Assert.PageOf(0, w.Body)
}
}
16 changes: 8 additions & 8 deletions src/github.com/stellar/horizon/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import (
"time"

"github.com/garyburd/redigo/redis"
"github.com/rcrowley/go-metrics"
metrics "github.com/rcrowley/go-metrics"
"github.com/stellar/go/build"
"github.com/stellar/horizon/db2"
"github.com/stellar/go/support/db"
"github.com/stellar/horizon/db2/core"
"github.com/stellar/horizon/db2/history"
"github.com/stellar/horizon/friendbot"
Expand All @@ -25,7 +25,7 @@ import (
"github.com/stellar/horizon/txsub"
"golang.org/x/net/context"
"golang.org/x/net/http2"
"gopkg.in/tylerb/graceful.v1"
graceful "gopkg.in/tylerb/graceful.v1"
)

// You can override this variable using: gb build -ldflags "-X main.version aabbccdd"
Expand Down Expand Up @@ -72,7 +72,7 @@ func NewApp(config Config) (*App, error) {

result := &App{config: config}
result.horizonVersion = version
result.networkPassphrase = build.DefaultNetwork.Passphrase
result.networkPassphrase = build.TestNetwork.Passphrase
result.ticks = time.NewTicker(1 * time.Second)
result.init()
return result, nil
Expand Down Expand Up @@ -138,14 +138,14 @@ func (a *App) HistoryQ() *history.Q {

// HorizonRepo returns a new repo that loads data from the horizon database. The
// returned repo is bound to `ctx`.
func (a *App) HorizonRepo(ctx context.Context) *db2.Repo {
return &db2.Repo{DB: a.historyQ.Repo.DB, Ctx: ctx}
func (a *App) HorizonRepo(ctx context.Context) *db.Repo {
return &db.Repo{DB: a.historyQ.Repo.DB, Ctx: ctx}
}

// CoreRepo returns a new repo that loads data from the stellar core
// database. The returned repo is bound to `ctx`.
func (a *App) CoreRepo(ctx context.Context) *db2.Repo {
return &db2.Repo{DB: a.coreQ.Repo.DB, Ctx: ctx}
func (a *App) CoreRepo(ctx context.Context) *db.Repo {
return &db.Repo{DB: a.coreQ.Repo.DB, Ctx: ctx}
}

// CoreQ returns a helper object for performing sql queries aginst the
Expand Down
8 changes: 4 additions & 4 deletions src/github.com/stellar/horizon/cmd/horizon/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/stellar/horizon/db2"
"github.com/stellar/go/support/db"
"github.com/stellar/horizon/db2/schema"
"github.com/stellar/horizon/ingest"
hlog "github.com/stellar/horizon/log"
Expand All @@ -26,7 +26,7 @@ var dbInitCmd = &cobra.Command{
Short: "install schema",
Long: "init initializes the postgres database used by horizon.",
Run: func(cmd *cobra.Command, args []string) {
db, err := db2.Open(viper.GetString("db-url"))
db, err := db.Open("postgres", viper.GetString("db-url"))
if err != nil {
hlog.Error(err)
os.Exit(1)
Expand Down Expand Up @@ -101,12 +101,12 @@ var dbReingestCmd = &cobra.Command{
initConfig()
hlog.DefaultLogger.Logger.Level = config.LogLevel

hdb, err := db2.Open(config.DatabaseURL)
hdb, err := db.Open("postgres", config.DatabaseURL)
if err != nil {
log.Fatal(err)
}

cdb, err := db2.Open(config.StellarCoreDatabaseURL)
cdb, err := db.Open("postgres", config.StellarCoreDatabaseURL)
if err != nil {
log.Fatal(err)
}
Expand Down
4 changes: 2 additions & 2 deletions src/github.com/stellar/horizon/db2/core/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ package core
import (
"github.com/guregu/null"
"github.com/stellar/go/strkey"
"github.com/stellar/go/support/db"
"github.com/stellar/go/xdr"
"github.com/stellar/horizon/db2"
)

// Account is a row of data from the `accounts` table
Expand Down Expand Up @@ -73,7 +73,7 @@ type OrderBookSummary []OrderBookSummaryPriceLevel
// Q is a helper struct on which to hang common queries against a stellar
// core database.
type Q struct {
*db2.Repo
*db.Repo
}

// PriceLevel represents an aggregation of offers to trade at a certain
Expand Down
4 changes: 2 additions & 2 deletions src/github.com/stellar/horizon/db2/history/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (

"github.com/guregu/null"
sq "github.com/lann/squirrel"
"github.com/stellar/go/support/db"
"github.com/stellar/go/xdr"
"github.com/stellar/horizon/db2"
)

const (
Expand Down Expand Up @@ -183,7 +183,7 @@ type OperationsQ struct {
// Q is a helper struct on which to hang common queries against a history
// portion of the horizon database.
type Q struct {
*db2.Repo
*db.Repo
}

// TotalOrderID represents the ID portion of rows that are identified by the
Expand Down
71 changes: 0 additions & 71 deletions src/github.com/stellar/horizon/db2/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,6 @@
// and query capabilities.
package db2

import (
"database/sql"
"regexp"
"strings"

"github.com/go-errors/errors"
"github.com/jmoiron/sqlx"
_ "github.com/lib/pq"
"golang.org/x/net/context"
)

// Conn represents a connection to a single database.
type Conn interface {
Exec(query string, args ...interface{}) (sql.Result, error)
Get(dest interface{}, query string, args ...interface{}) error
Rebind(sql string) string
Queryx(query string, args ...interface{}) (*sqlx.Rows, error)
Select(dest interface{}, query string, args ...interface{}) error
}

// Pageable records have a defined order, and the place withing that order
// is determined by the paging token
type Pageable interface {
Expand All @@ -35,54 +15,3 @@ type PageQuery struct {
Order string
Limit uint64
}

// Repo provides helper methods for making queries against `Conn`, such as
// logging.
type Repo struct {
// Conn is the database connection that queries should be executed against.
DB *sqlx.DB

// Ctx is the optional context in which the repo is operating under.
Ctx context.Context

tx *sqlx.Tx
}

// AllStatements takes a sql script, possibly containing comments and multiple
// statements, and returns a slice of strings that correspond to each individual
// SQL statement within the script
func AllStatements(script string) (ret []string) {
for _, s := range strings.Split(removeComments(script), ";") {
s = strings.TrimSpace(s)
if s == "" {
continue
}
ret = append(ret, s)
}
return
}

// Open the postgres database at `url` and returns a new *Repo using it.
func Open(url string) (*Repo, error) {
db, err := sqlx.Connect("postgres", url)
if err != nil {
return nil, errors.Wrap(err, 1)
}

return &Repo{DB: db}, nil
}

// ensure various types conform to Conn interface
var _ Conn = (*sqlx.Tx)(nil)
var _ Conn = (*sqlx.DB)(nil)

// SQLBlockComments is a regex that matches against SQL block comments
var sqlBlockComments = regexp.MustCompile(`/\*.*?\*/`)

// SQLLineComments is a regex that matches against SQL line comments
var sqlLineComments = regexp.MustCompile("--.*?\n")

func removeComments(script string) string {
withoutBlocks := sqlBlockComments.ReplaceAllString(script, "")
return sqlLineComments.ReplaceAllString(withoutBlocks, "")
}
Loading

0 comments on commit 2353b16

Please sign in to comment.