From 0b93735ecb4d735e9727df269b3b24909e2670d8 Mon Sep 17 00:00:00 2001 From: tamirms Date: Sun, 19 Dec 2021 13:30:06 +0000 Subject: [PATCH 1/5] Add trade_type column and index --- .../horizon/internal/db2/schema/bindata.go | 23 +++++++++++++++++++ .../migrations/52_add_trade_type_index.sql | 10 ++++++++ 2 files changed, 33 insertions(+) create mode 100644 services/horizon/internal/db2/schema/migrations/52_add_trade_type_index.sql diff --git a/services/horizon/internal/db2/schema/bindata.go b/services/horizon/internal/db2/schema/bindata.go index a009368f8a..446a848a05 100644 --- a/services/horizon/internal/db2/schema/bindata.go +++ b/services/horizon/internal/db2/schema/bindata.go @@ -47,6 +47,7 @@ // migrations/4_add_protocol_version.sql (188B) // migrations/50_liquidity_pools.sql (3.876kB) // migrations/51_remove_ht_unused_indexes.sql (321B) +// migrations/52_add_trade_type_index.sql (424B) // migrations/5_create_trades_table.sql (1.1kB) // migrations/6_create_assets_table.sql (366B) // migrations/7_modify_trades_table.sql (2.303kB) @@ -1061,6 +1062,26 @@ func migrations51_remove_ht_unused_indexesSql() (*asset, error) { return a, nil } +var _migrations52_add_trade_type_indexSql = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\x7c\x90\x41\x4b\xc3\x30\x1c\xc5\xef\xf9\x14\x8f\x9d\x1c\x6e\xa0\x5e\x8b\x42\xb7\xfc\x75\xc5\xd2\x8e\x34\x45\x6f\x21\x33\xc1\x05\xba\xa6\xa6\x19\xd2\x6f\x2f\x4e\x91\xa0\xb8\x5b\x0e\xbf\xbc\xdf\xfb\xbf\xe5\x12\x97\x07\xf7\x1a\x74\xb4\x68\x07\xc6\xf2\x52\x92\x80\xcc\x57\x25\x61\xef\xc6\xe8\xc3\xa4\x62\xd0\xc6\x8e\xc8\x39\xc7\xe9\xa9\xe2\x34\x58\x8c\x07\xdd\x75\xae\x8f\xe0\x74\x9f\xb7\xa5\xc4\x35\xd6\x1b\x5a\x3f\x5e\x24\xcc\x1d\xae\xe6\x19\x6b\xb7\x3c\x97\x7f\xe2\x1a\x92\x69\xdc\x2d\x6e\xf0\xb4\x21\x41\xd8\xe9\xd1\xaa\xce\xbd\x1d\x9d\x71\x71\x52\x83\xf7\x9d\x72\x06\x45\x83\xaa\x96\xa8\xda\xb2\x44\x2d\xf0\xe2\x8f\x7d\xb4\xe1\x3c\x98\xb1\xb5\xa0\x4f\x77\x51\x71\x7a\xc6\x3e\x06\xa3\x76\xdf\x0d\xbe\xb4\x75\xf5\xbb\x57\xdb\x14\xd5\x03\x56\x52\x10\x25\xa7\x2c\x7e\x30\x3f\xd8\xa0\xa3\xf3\xbd\x72\x66\x81\x99\x0f\xc6\x86\xd9\x3c\x63\x2c\x9d\x92\xfb\xf7\x9e\x31\x2e\xea\xed\xbf\xea\xec\xdc\xd6\xa7\x9f\x29\xfb\x11\x00\x00\xff\xff\x80\x41\xbf\x62\xa8\x01\x00\x00") + +func migrations52_add_trade_type_indexSqlBytes() ([]byte, error) { + return bindataRead( + _migrations52_add_trade_type_indexSql, + "migrations/52_add_trade_type_index.sql", + ) +} + +func migrations52_add_trade_type_indexSql() (*asset, error) { + bytes, err := migrations52_add_trade_type_indexSqlBytes() + if err != nil { + return nil, err + } + + info := bindataFileInfo{name: "migrations/52_add_trade_type_index.sql", size: 0, mode: os.FileMode(0), modTime: time.Unix(0, 0)} + a := &asset{bytes: bytes, info: info, digest: [32]uint8{0xa7, 0xbc, 0x9b, 0x94, 0xfb, 0x5b, 0x77, 0x33, 0x5b, 0x67, 0x5a, 0x64, 0x6e, 0x6c, 0x5f, 0xee, 0xe0, 0xd2, 0xcd, 0x61, 0xd6, 0x29, 0x8c, 0x74, 0xd0, 0xec, 0xaf, 0x32, 0x38, 0xb6, 0xd9, 0x71}} + return a, nil +} + var _migrations5_create_trades_tableSql = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\x94\x94\x51\x6f\xaa\x40\x10\x85\xdf\xf9\x15\x13\x9f\x30\x17\x93\x7b\x6f\x5a\x5f\x4c\x9a\x58\x25\xad\xa9\xc1\xd6\x4a\xd2\x37\xb2\xb0\x23\x6c\xa2\x2c\x99\x1d\xda\xf0\xef\x1b\x68\x69\x10\x57\xad\xaf\x9c\x39\x67\x38\xbb\x5f\x76\x34\x82\x3f\x7b\x95\x92\x60\x84\xb0\x70\x66\x6b\x7f\xba\xf1\x61\x33\xbd\x5f\xfa\x90\x29\xc3\x9a\xaa\x88\x49\x48\x34\xe0\x3a\x00\xf0\xf3\x51\x17\x48\x82\x95\xce\x23\x25\x21\x56\xa9\xca\x19\x82\xd5\x06\x82\x70\xb9\xf4\x9a\xc9\x81\x26\x89\x34\x00\x95\x33\xa6\x48\x1d\xb5\x91\xf5\x76\x8b\x64\x35\x37\xb2\xc1\xdd\xee\x84\x5e\xcb\x71\x59\x9d\x75\xeb\x9d\x8c\x84\x31\xc8\x11\x57\x05\x42\x92\x09\x12\x09\x23\xc1\xbb\xa0\x4a\xe5\xa9\x3b\xbe\x19\xf6\x22\x3b\x1e\x65\x4c\x89\x64\x71\xdd\x8e\xcf\xb8\x12\x2d\x6d\x9b\xfe\xfd\xb7\x7b\xf6\xba\xcc\xb9\xff\xff\x30\x7b\xf4\x67\x4f\xe0\x76\x47\xee\xe0\xef\xf0\xbb\x57\xac\xcb\x34\xe3\x6b\x9b\x1d\xb8\xae\xe8\x76\xe0\xfb\x75\xbb\xd6\x75\xb6\xdf\xe1\x50\xdd\xd0\x19\x4e\x9c\x96\xbf\x30\x58\xbc\x84\x3e\x2c\x82\xb9\xff\x06\x19\x93\x8c\x0a\x25\x61\x15\xf4\x91\x0c\x5f\x17\xc1\x03\xc4\x4c\x88\xe0\xda\xc8\xf4\x5a\x0a\x3b\xe1\x9d\xd4\xb8\x8a\x1a\x0c\x2f\x45\xb7\xac\xda\x52\xea\x90\xfa\xb6\x2e\x65\xf4\x90\xf4\xfa\xe4\x78\xc7\x00\x9e\x5a\xf7\x75\x78\x97\x16\x1e\xb1\xe2\x1d\x5f\xa8\x67\x63\xa3\x5e\xdb\x7d\x17\xe6\xfa\x23\x77\xe6\xeb\xd5\xb3\xfd\x5d\x48\x84\x49\x84\xc4\x89\xf3\x19\x00\x00\xff\xff\x79\x87\x24\x6b\x4c\x04\x00\x00") func migrations5_create_trades_tableSqlBytes() ([]byte, error) { @@ -1319,6 +1340,7 @@ var _bindata = map[string]func() (*asset, error){ "migrations/4_add_protocol_version.sql": migrations4_add_protocol_versionSql, "migrations/50_liquidity_pools.sql": migrations50_liquidity_poolsSql, "migrations/51_remove_ht_unused_indexes.sql": migrations51_remove_ht_unused_indexesSql, + "migrations/52_add_trade_type_index.sql": migrations52_add_trade_type_indexSql, "migrations/5_create_trades_table.sql": migrations5_create_trades_tableSql, "migrations/6_create_assets_table.sql": migrations6_create_assets_tableSql, "migrations/7_modify_trades_table.sql": migrations7_modify_trades_tableSql, @@ -1416,6 +1438,7 @@ var _bintree = &bintree{nil, map[string]*bintree{ "4_add_protocol_version.sql": &bintree{migrations4_add_protocol_versionSql, map[string]*bintree{}}, "50_liquidity_pools.sql": &bintree{migrations50_liquidity_poolsSql, map[string]*bintree{}}, "51_remove_ht_unused_indexes.sql": &bintree{migrations51_remove_ht_unused_indexesSql, map[string]*bintree{}}, + "52_add_trade_type_index.sql": &bintree{migrations52_add_trade_type_indexSql, map[string]*bintree{}}, "5_create_trades_table.sql": &bintree{migrations5_create_trades_tableSql, map[string]*bintree{}}, "6_create_assets_table.sql": &bintree{migrations6_create_assets_tableSql, map[string]*bintree{}}, "7_modify_trades_table.sql": &bintree{migrations7_modify_trades_tableSql, map[string]*bintree{}}, diff --git a/services/horizon/internal/db2/schema/migrations/52_add_trade_type_index.sql b/services/horizon/internal/db2/schema/migrations/52_add_trade_type_index.sql new file mode 100644 index 0000000000..0abcd95c6f --- /dev/null +++ b/services/horizon/internal/db2/schema/migrations/52_add_trade_type_index.sql @@ -0,0 +1,10 @@ +-- +migrate Up + +ALTER TABLE history_trades ADD trade_type smallint DEFAULT 1 CHECK(trade_type > 0); +UPDATE history_trades SET trade_type = 2 WHERE base_liquidity_pool_id IS NOT NULL OR counter_liquidity_pool_id IS NOT NULL; +CREATE INDEX htrd_by_trade_type ON history_trades USING BTREE(trade_type, history_operation_id, "order"); + +-- +migrate Down + +DROP INDEX htrd_by_trade_type; +ALTER TABLE history_trades DROP trade_type; From bf8a9c4f6a438ef3e4009b7c70829e4811fb088f Mon Sep 17 00:00:00 2001 From: tamirms Date: Sun, 19 Dec 2021 14:40:41 +0000 Subject: [PATCH 2/5] Populate trade type column when ingesting trades --- services/horizon/internal/db2/history/trade.go | 4 ++-- .../db2/history/trade_batch_insert_builder.go | 12 ++++++++++++ .../horizon/internal/db2/history/trade_scenario.go | 4 ++++ .../internal/ingest/processors/trades_processor.go | 2 ++ 4 files changed, 20 insertions(+), 2 deletions(-) diff --git a/services/horizon/internal/db2/history/trade.go b/services/horizon/internal/db2/history/trade.go index ca8b42df41..a44b038683 100644 --- a/services/horizon/internal/db2/history/trade.go +++ b/services/horizon/internal/db2/history/trade.go @@ -171,9 +171,9 @@ func createTradesSQL(page db2.PageQuery, query historyTradesQuery) (string, []in switch query.tradeType { case OrderbookTrades: - sql = sql.Where(sq.Eq{"htrd.base_liquidity_pool_id": nil, "htrd.counter_liquidity_pool_id": nil}) + sql = sql.Where(sq.Eq{"htrd.trade_type": OrderbookTradeType}) case LiquidityPoolTrades: - sql = sql.Where(sq.Eq{"htrd.base_offer_id": nil, "htrd.counter_offer_id": nil}) + sql = sql.Where(sq.Eq{"htrd.trade_type": LiquidityPoolTradeType}) case AllTrades: default: return "", nil, errors.Errorf("Invalid trade type: %v", query.tradeType) diff --git a/services/horizon/internal/db2/history/trade_batch_insert_builder.go b/services/horizon/internal/db2/history/trade_batch_insert_builder.go index 3836e5397d..e53b7f4194 100644 --- a/services/horizon/internal/db2/history/trade_batch_insert_builder.go +++ b/services/horizon/internal/db2/history/trade_batch_insert_builder.go @@ -10,6 +10,16 @@ import ( "github.com/stellar/go/support/errors" ) +// TradeType is an enum which indicates the type of trade +type TradeType int16 + +const ( + // OrderbookTradeType is a trade which exercises an offer on the orderbook. + OrderbookTradeType = TradeType(1) + // LiquidityPoolTradeType is a trade which exercises a liquidity pool. + LiquidityPoolTradeType = TradeType(2) +) + // InsertTrade represents the arguments to TradeBatchInsertBuilder.Add() which is used to insert // rows into the history_trades table type InsertTrade struct { @@ -33,6 +43,8 @@ type InsertTrade struct { BaseIsSeller bool `db:"base_is_seller"` + Type TradeType `db:"trade_type"` + PriceN int64 `db:"price_n"` PriceD int64 `db:"price_d"` } diff --git a/services/horizon/internal/db2/history/trade_scenario.go b/services/horizon/internal/db2/history/trade_scenario.go index f81ba3f6ce..fae8f5e01a 100644 --- a/services/horizon/internal/db2/history/trade_scenario.go +++ b/services/horizon/internal/db2/history/trade_scenario.go @@ -30,6 +30,7 @@ func createInsertTrades( CounterAmount: 896, PriceN: 1, PriceD: 3, + Type: OrderbookTradeType, } second := first @@ -51,6 +52,7 @@ func createInsertTrades( CounterAmount: 6, PriceN: 1156, PriceD: 3, + Type: OrderbookTradeType, } fourth := InsertTrade{ @@ -67,6 +69,7 @@ func createInsertTrades( BaseIsSeller: true, PriceN: 675, PriceD: 981, + Type: LiquidityPoolTradeType, } fifth := InsertTrade{ @@ -83,6 +86,7 @@ func createInsertTrades( BaseIsSeller: true, PriceN: 43, PriceD: 56, + Type: LiquidityPoolTradeType, } return []InsertTrade{ diff --git a/services/horizon/internal/ingest/processors/trades_processor.go b/services/horizon/internal/ingest/processors/trades_processor.go index 474d6ac67c..fa51c381da 100644 --- a/services/horizon/internal/ingest/processors/trades_processor.go +++ b/services/horizon/internal/ingest/processors/trades_processor.go @@ -299,9 +299,11 @@ func (p *TradeProcessor) extractTrades( return nil, err } row.LiquidityPoolFee = null.IntFrom(int64(fee)) + row.Type = history.LiquidityPoolTradeType } else { row.BaseOfferID = null.IntFrom(int64(trade.OfferId())) sellerAccount = trade.SellerId().Address() + row.Type = history.OrderbookTradeType } if buyOfferExists { From 70f993c694cc4c7efb3330fb7fc6d1b9eedbd0bc Mon Sep 17 00:00:00 2001 From: tamirms Date: Sun, 19 Dec 2021 20:48:55 +0000 Subject: [PATCH 3/5] Fix tests --- services/horizon/internal/actions_trade_test.go | 11 ++++++++--- services/horizon/internal/db2/history/main.go | 1 + services/horizon/internal/db2/history/trade.go | 2 ++ .../horizon/internal/db2/history/trade_scenario.go | 5 +++++ .../ingest/processors/trades_processor_test.go | 8 ++++++++ services/horizon/internal/resourceadapter/trade.go | 12 +++++------- 6 files changed, 29 insertions(+), 10 deletions(-) diff --git a/services/horizon/internal/actions_trade_test.go b/services/horizon/internal/actions_trade_test.go index 6646ec1fa5..c4871d4d42 100644 --- a/services/horizon/internal/actions_trade_test.go +++ b/services/horizon/internal/actions_trade_test.go @@ -315,10 +315,13 @@ func assertResponseTradeEqualsDBTrade(ht *HTTPT, row history.Trade, record horiz ht.Assert.Equal(row.PriceN.Int64, record.Price.N) ht.Assert.Equal(row.PriceD.Int64, record.Price.D) - if row.BaseLiquidityPoolID.Valid || row.CounterLiquidityPoolID.Valid { - ht.Assert.Equal(history.LiquidityPoolTrades, record.TradeType) - } else { + switch row.Type { + case history.OrderbookTradeType: ht.Assert.Equal(history.OrderbookTrades, record.TradeType) + case history.LiquidityPoolTradeType: + ht.Assert.Equal(history.LiquidityPoolTrades, record.TradeType) + default: + ht.Assert.Fail("invalid trade type %v", row.Type) } } @@ -833,6 +836,8 @@ func IngestTestTrade( PriceN: int64(price.N), PriceD: int64(price.D), LedgerCloseTime: timestamp.ToTime(), + + Type: history.OrderbookTradeType, }) err = batch.Exec(ctx) if err != nil { diff --git a/services/horizon/internal/db2/history/main.go b/services/horizon/internal/db2/history/main.go index 1a46fa5a17..2ebf2d812d 100644 --- a/services/horizon/internal/db2/history/main.go +++ b/services/horizon/internal/db2/history/main.go @@ -704,6 +704,7 @@ type Trade struct { BaseIsSeller bool `db:"base_is_seller"` PriceN null.Int `db:"price_n"` PriceD null.Int `db:"price_d"` + Type TradeType `db:"trade_type"` } // Transaction is a row of data from the `history_transactions` table diff --git a/services/horizon/internal/db2/history/trade.go b/services/horizon/internal/db2/history/trade.go index a44b038683..1d1048cfe0 100644 --- a/services/horizon/internal/db2/history/trade.go +++ b/services/horizon/internal/db2/history/trade.go @@ -308,6 +308,7 @@ var selectTradeFields = sq.Select( "htrd.base_is_seller", "htrd.price_n", "htrd.price_d", + "htrd.trade_type", ) var selectReverseTradeFields = sq.Select( @@ -332,6 +333,7 @@ var selectReverseTradeFields = sq.Select( "NOT(htrd.base_is_seller) as base_is_seller", "htrd.price_d as price_n", "htrd.price_n as price_d", + "htrd.trade_type", ) func getCanonicalAssetOrder( diff --git a/services/horizon/internal/db2/history/trade_scenario.go b/services/horizon/internal/db2/history/trade_scenario.go index fae8f5e01a..5ba118dd83 100644 --- a/services/horizon/internal/db2/history/trade_scenario.go +++ b/services/horizon/internal/db2/history/trade_scenario.go @@ -297,6 +297,7 @@ func TradeScenario(tt *test.T, q *Q) TradeFixtures { BaseIsSeller: true, PriceN: null.IntFrom(inserts[0].PriceN), PriceD: null.IntFrom(inserts[0].PriceD), + Type: OrderbookTradeType, }, { HistoryOperationID: inserts[1].HistoryOperationID, @@ -317,6 +318,7 @@ func TradeScenario(tt *test.T, q *Q) TradeFixtures { BaseIsSeller: true, PriceN: null.IntFrom(inserts[1].PriceN), PriceD: null.IntFrom(inserts[1].PriceD), + Type: OrderbookTradeType, }, { HistoryOperationID: inserts[2].HistoryOperationID, @@ -337,6 +339,7 @@ func TradeScenario(tt *test.T, q *Q) TradeFixtures { BaseIsSeller: false, PriceN: null.IntFrom(inserts[2].PriceN), PriceD: null.IntFrom(inserts[2].PriceD), + Type: OrderbookTradeType, }, { HistoryOperationID: inserts[3].HistoryOperationID, @@ -358,6 +361,7 @@ func TradeScenario(tt *test.T, q *Q) TradeFixtures { LiquidityPoolFee: inserts[3].LiquidityPoolFee, PriceN: null.IntFrom(inserts[3].PriceN), PriceD: null.IntFrom(inserts[3].PriceD), + Type: LiquidityPoolTradeType, }, { HistoryOperationID: inserts[4].HistoryOperationID, @@ -379,6 +383,7 @@ func TradeScenario(tt *test.T, q *Q) TradeFixtures { LiquidityPoolFee: inserts[4].LiquidityPoolFee, PriceN: null.IntFrom(inserts[4].PriceN), PriceD: null.IntFrom(inserts[4].PriceD), + Type: LiquidityPoolTradeType, }, } diff --git a/services/horizon/internal/ingest/processors/trades_processor_test.go b/services/horizon/internal/ingest/processors/trades_processor_test.go index 17a392cda5..31baf425ad 100644 --- a/services/horizon/internal/ingest/processors/trades_processor_test.go +++ b/services/horizon/internal/ingest/processors/trades_processor_test.go @@ -241,6 +241,7 @@ func (s *TradeProcessorTestSuiteLedger) mockReadTradeTransactions( BaseIsSeller: false, PriceN: int64(s.sellPrices[0].D), PriceD: int64(s.sellPrices[0].N), + Type: history.OrderbookTradeType, }, { HistoryOperationID: toid.New(int32(ledger.Header.LedgerSeq), 1, 3).ToInt64(), @@ -257,6 +258,7 @@ func (s *TradeProcessorTestSuiteLedger) mockReadTradeTransactions( BaseOfferID: null.IntFrom(int64(s.strictSendTrade.OfferId())), PriceN: int64(s.sellPrices[1].N), PriceD: int64(s.sellPrices[1].D), + Type: history.OrderbookTradeType, }, { HistoryOperationID: toid.New(int32(ledger.Header.LedgerSeq), 1, 4).ToInt64(), @@ -273,6 +275,7 @@ func (s *TradeProcessorTestSuiteLedger) mockReadTradeTransactions( CounterOfferID: null.IntFrom(int64(s.buyOfferTrade.OfferId())), PriceN: int64(s.sellPrices[2].D), PriceD: int64(s.sellPrices[2].N), + Type: history.OrderbookTradeType, }, { HistoryOperationID: toid.New(int32(ledger.Header.LedgerSeq), 1, 5).ToInt64(), @@ -289,6 +292,7 @@ func (s *TradeProcessorTestSuiteLedger) mockReadTradeTransactions( BaseOfferID: null.IntFrom(int64(s.sellOfferTrade.OfferId())), PriceN: int64(s.sellPrices[3].N), PriceD: int64(s.sellPrices[3].D), + Type: history.OrderbookTradeType, }, { HistoryOperationID: toid.New(int32(ledger.Header.LedgerSeq), 1, 6).ToInt64(), @@ -305,6 +309,7 @@ func (s *TradeProcessorTestSuiteLedger) mockReadTradeTransactions( CounterOfferID: null.IntFrom(int64(s.passiveSellOfferTrade.OfferId())), PriceN: int64(s.sellPrices[4].D), PriceD: int64(s.sellPrices[4].N), + Type: history.OrderbookTradeType, }, { @@ -323,6 +328,7 @@ func (s *TradeProcessorTestSuiteLedger) mockReadTradeTransactions( BaseOfferID: null.IntFrom(int64(s.otherPassiveSellOfferTrade.OfferId())), PriceN: int64(s.sellPrices[5].N), PriceD: int64(s.sellPrices[5].D), + Type: history.OrderbookTradeType, }, { HistoryOperationID: toid.New(int32(ledger.Header.LedgerSeq), 1, 8).ToInt64(), @@ -339,6 +345,7 @@ func (s *TradeProcessorTestSuiteLedger) mockReadTradeTransactions( LiquidityPoolFee: null.IntFrom(int64(xdr.LiquidityPoolFeeV18)), PriceN: int64(s.sellPrices[6].D), PriceD: int64(s.sellPrices[6].N), + Type: history.LiquidityPoolTradeType, }, { HistoryOperationID: toid.New(int32(ledger.Header.LedgerSeq), 1, 9).ToInt64(), @@ -355,6 +362,7 @@ func (s *TradeProcessorTestSuiteLedger) mockReadTradeTransactions( LiquidityPoolFee: null.IntFrom(int64(xdr.LiquidityPoolFeeV18)), PriceN: int64(s.sellPrices[7].N), PriceD: int64(s.sellPrices[7].D), + Type: history.LiquidityPoolTradeType, }, } diff --git a/services/horizon/internal/resourceadapter/trade.go b/services/horizon/internal/resourceadapter/trade.go index 69466fecb7..280e40dba3 100644 --- a/services/horizon/internal/resourceadapter/trade.go +++ b/services/horizon/internal/resourceadapter/trade.go @@ -29,10 +29,8 @@ func PopulateTrade( if row.BaseAccount.Valid { dest.BaseAccount = row.BaseAccount.String } - var isLiquidityPoolTrade bool if row.BaseLiquidityPoolID.Valid { dest.BaseLiquidityPoolID = row.BaseLiquidityPoolID.String - isLiquidityPoolTrade = true } dest.BaseAssetType = row.BaseAssetType dest.BaseAssetCode = row.BaseAssetCode @@ -47,7 +45,6 @@ func PopulateTrade( } if row.CounterLiquidityPoolID.Valid { dest.CounterLiquidityPoolID = row.CounterLiquidityPoolID.String - isLiquidityPoolTrade = true } dest.CounterAssetType = row.CounterAssetType dest.CounterAssetCode = row.CounterAssetCode @@ -60,10 +57,11 @@ func PopulateTrade( dest.LiquidityPoolFeeBP = uint32(row.LiquidityPoolFee.Int64) } - if isLiquidityPoolTrade { - dest.TradeType = "liquidity_pool" - } else { - dest.TradeType = "orderbook" + switch row.Type { + case history.OrderbookTradeType: + dest.TradeType = history.OrderbookTrades + case history.LiquidityPoolTradeType: + dest.TradeType = history.LiquidityPoolTrades } if row.HasPrice() { From 0ca601d57dd93db0b60d5f609ece4eaebceba946 Mon Sep 17 00:00:00 2001 From: tamirms Date: Sun, 19 Dec 2021 20:59:05 +0000 Subject: [PATCH 4/5] Update changelog --- services/horizon/CHANGELOG.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/services/horizon/CHANGELOG.md b/services/horizon/CHANGELOG.md index 1e761f9f4c..4d6973abe0 100644 --- a/services/horizon/CHANGELOG.md +++ b/services/horizon/CHANGELOG.md @@ -7,6 +7,11 @@ file. This project adheres to [Semantic Versioning](http://semver.org/). * Generate Http Status code of 499 for Client Disconnects, should propagate into `horizon_http_requests_duration_seconds_count` metric key with status=499 label. ([4098](horizon_http_requests_duration_seconds_count)) +* Improve performance of `/trades?trade_type=liquidity_pool` requests. ([4149](https://github.com/stellar/go/pull/4149)) + +### DB Schema Migration + +* DB migrations add a column and index to the `history_trades` table. This is very large table so migration may take a long time (depending on your DB hardware). Please test the migrations execution time on the copy of your production DB first. ## v2.12.1 From 53176ff84a4b5f140f9af40131089b8d075ea6a7 Mon Sep 17 00:00:00 2001 From: tamirms Date: Sun, 19 Dec 2021 21:10:43 +0000 Subject: [PATCH 5/5] Fix TestTradeAggregations --- .../horizon/internal/integration/trade_aggregations_test.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/services/horizon/internal/integration/trade_aggregations_test.go b/services/horizon/internal/integration/trade_aggregations_test.go index 12ffec3603..5c278b1bae 100644 --- a/services/horizon/internal/integration/trade_aggregations_test.go +++ b/services/horizon/internal/integration/trade_aggregations_test.go @@ -76,6 +76,7 @@ func TestTradeAggregations(t *testing.T) { CounterAssetID: counterAssetId, PriceN: 23456, PriceD: 10000, + Type: history.OrderbookTradeType, }, }, resolution: 60_000, @@ -115,6 +116,7 @@ func TestTradeAggregations(t *testing.T) { CounterAssetID: counterAssetId, PriceN: 23456, PriceD: 10000, + Type: history.OrderbookTradeType, }, { HistoryOperationID: 0, @@ -130,6 +132,7 @@ func TestTradeAggregations(t *testing.T) { CounterAssetID: counterAssetId, PriceN: 13456, PriceD: 10000, + Type: history.OrderbookTradeType, }, }, resolution: 60_000, @@ -169,6 +172,7 @@ func TestTradeAggregations(t *testing.T) { CounterAssetID: counterAssetId, PriceN: 23456, PriceD: 10000, + Type: history.OrderbookTradeType, }, { HistoryOperationID: 0, @@ -184,6 +188,7 @@ func TestTradeAggregations(t *testing.T) { CounterAssetID: counterAssetId, PriceN: 13456, PriceD: 10000, + Type: history.OrderbookTradeType, }, }, resolution: 86_400_000,