From a7efa95e09e64cfa294225f1d2382b8aee2857ad Mon Sep 17 00:00:00 2001 From: tamirms Date: Thu, 26 Aug 2021 23:10:38 +0100 Subject: [PATCH] Fix staticcheck --- protocols/horizon/main.go | 1 + .../horizon/internal/actions_trade_test.go | 139 ++++++++++++++---- .../internal/db2/history/synt_offer_id.go | 39 ----- services/horizon/internal/test/t.go | 1 + services/horizon/internal/test/trades/main.go | 122 --------------- 5 files changed, 116 insertions(+), 186 deletions(-) delete mode 100644 services/horizon/internal/db2/history/synt_offer_id.go delete mode 100644 services/horizon/internal/test/trades/main.go diff --git a/protocols/horizon/main.go b/protocols/horizon/main.go index 16858aa752..102622415f 100644 --- a/protocols/horizon/main.go +++ b/protocols/horizon/main.go @@ -365,6 +365,7 @@ type Trade struct { LedgerCloseTime time.Time `json:"ledger_close_time"` OfferID string `json:"offer_id"` TradeType string `json:"trade_type"` + LiquidityPoolFeeBP int32 `json:"liquidity_pool_fee_bp,omitempty"` BaseLiquidityPoolID string `json:"base_liquidity_pool_id,omitempty"` BaseOfferID string `json:"base_offer_id,omitempty"` BaseAccount string `json:"base_account"` diff --git a/services/horizon/internal/actions_trade_test.go b/services/horizon/internal/actions_trade_test.go index 141e5bef1e..6d536d7cf3 100644 --- a/services/horizon/internal/actions_trade_test.go +++ b/services/horizon/internal/actions_trade_test.go @@ -1,8 +1,12 @@ +//lint:file-ignore U1001 Ignore all unused code, thinks the code is unused because of the test skips package horizon import ( + "context" "encoding/json" "fmt" + "github.com/guregu/null" + "github.com/stellar/go/keypair" "net/url" "strconv" "strings" @@ -12,7 +16,6 @@ import ( "github.com/stellar/go/protocols/horizon" "github.com/stellar/go/services/horizon/internal/db2/history" . "github.com/stellar/go/services/horizon/internal/db2/history" - . "github.com/stellar/go/services/horizon/internal/test/trades" "github.com/stellar/go/support/render/hal" stellarTime "github.com/stellar/go/support/time" "github.com/stellar/go/xdr" @@ -487,30 +490,6 @@ func TestTradeActions_AggregationOrdering(t *testing.T) { } } -func assertOfferType(ht *HTTPT, offerId string, idType OfferIDType) { - offerIdInt64, _ := strconv.ParseInt(offerId, 10, 64) - _, offerType := DecodeOfferID(offerIdInt64) - ht.Assert.Equal(offerType, idType) -} - -// TestTradeActions_SyntheticOfferIds loads the offer_ids scenario and ensures that synthetic offer -// ids are created when necessary and not when unnecessary -func TestTradeActions_SyntheticOfferIds(t *testing.T) { - ht := StartHTTPTest(t, "offer_ids") - defer ht.Finish() - var records []horizon.Trade - w := ht.Get("/trades") - if ht.Assert.Equal(200, w.Code) { - if ht.Assert.PageOf(4, w.Body) { - ht.UnmarshalPage(w.Body, &records) - assertOfferType(ht, records[0].BaseOfferID, TOIDType) - assertOfferType(ht, records[1].BaseOfferID, TOIDType) - assertOfferType(ht, records[2].BaseOfferID, CoreOfferIDType) - assertOfferType(ht, records[3].BaseOfferID, CoreOfferIDType) - } - } -} - func TestTradeActions_AssetValidation(t *testing.T) { ht := StartHTTPTest(t, "trades") defer ht.Finish() @@ -628,3 +607,113 @@ func TestTradeActions_AggregationOffset(t *testing.T) { }) } } + +//GetTestAsset generates an issuer on the fly and creates a CreditAlphanum4 Asset with given code +func GetTestAsset(code string) xdr.Asset { + var codeBytes [4]byte + copy(codeBytes[:], []byte(code)) + ca4 := xdr.AlphaNum4{Issuer: GetTestAccount(), AssetCode: codeBytes} + return xdr.Asset{Type: xdr.AssetTypeAssetTypeCreditAlphanum4, AlphaNum4: &ca4, AlphaNum12: nil} +} + +//Get generates and returns an account on the fly +func GetTestAccount() xdr.AccountId { + var key xdr.Uint256 + kp, _ := keypair.Random() + copy(key[:], kp.Address()) + acc, _ := xdr.NewAccountId(xdr.PublicKeyTypePublicKeyTypeEd25519, key) + return acc +} + +//IngestTestTrade mock ingests a trade +func IngestTestTrade( + q *Q, + assetSold xdr.Asset, + assetBought xdr.Asset, + seller xdr.AccountId, + buyer xdr.AccountId, + amountSold int64, + amountBought int64, + timestamp stellarTime.Millis, + opCounter int64) error { + + trade := xdr.ClaimAtom{ + Type: xdr.ClaimAtomTypeClaimAtomTypeOrderBook, + OrderBook: &xdr.ClaimOfferAtom{ + AmountBought: xdr.Int64(amountBought), + SellerId: seller, + AmountSold: xdr.Int64(amountSold), + AssetBought: assetBought, + AssetSold: assetSold, + OfferId: 100, + }, + } + + price := xdr.Price{ + N: xdr.Int32(amountBought), + D: xdr.Int32(amountSold), + } + + ctx := context.Background() + accounts, err := q.CreateAccounts(ctx, []string{seller.Address(), buyer.Address()}, 2) + if err != nil { + return err + } + assets, err := q.CreateAssets(ctx, []xdr.Asset{assetBought, assetSold}, 2) + if err != nil { + return err + } + + batch := q.NewTradeBatchInsertBuilder(0) + batch.Add(ctx, InsertTrade{ + HistoryOperationID: opCounter, + Order: 0, + CounterAssetID: assets[assetBought.String()].ID, + CounterAccountID: null.IntFrom(accounts[buyer.Address()]), + CounterAmount: amountBought, + + BaseAssetID: assets[assetSold.String()].ID, + BaseAccountID: null.IntFrom(accounts[seller.Address()]), + BaseAmount: amountSold, + BaseOfferID: null.IntFrom(int64(trade.OfferId())), + BaseIsSeller: true, + PriceN: int64(price.N), + PriceD: int64(price.D), + LedgerCloseTime: timestamp.ToTime(), + }) + err = batch.Exec(ctx) + if err != nil { + return err + } + + err = q.RebuildTradeAggregationTimes(context.Background(), timestamp, timestamp) + if err != nil { + return err + } + + return nil +} + +//PopulateTestTrades generates and ingests trades between two assets according to given parameters +func PopulateTestTrades( + q *Q, + startTs int64, + numOfTrades int, + delta int64, + opStart int64) (ass1 xdr.Asset, ass2 xdr.Asset, err error) { + + acc1 := GetTestAccount() + acc2 := GetTestAccount() + ass1 = GetTestAsset("usd") + ass2 = GetTestAsset("euro") + for i := 1; i <= numOfTrades; i++ { + timestamp := stellarTime.MillisFromInt64(startTs + (delta * int64(i-1))) + err = IngestTestTrade( + q, ass1, ass2, acc1, acc2, int64(i*100), int64(i*100)*int64(i), timestamp, opStart+int64(i)) + //tt.Assert.NoError(err) + if err != nil { + return + } + } + return +} diff --git a/services/horizon/internal/db2/history/synt_offer_id.go b/services/horizon/internal/db2/history/synt_offer_id.go deleted file mode 100644 index e38c26cf11..0000000000 --- a/services/horizon/internal/db2/history/synt_offer_id.go +++ /dev/null @@ -1,39 +0,0 @@ -package history - -type OfferIDType uint64 - -const ( - CoreOfferIDType OfferIDType = 0 - TOIDType OfferIDType = 1 - - mask uint64 = 0xC000000000000000 -) - -// EncodeOfferId creates synthetic offer ids to be used by trade resources -// -// This is required because stellar-core does not allocate offer ids for immediately filled offers, -// while clients expect them for aggregated views. -// -// The encoded value is of type int64 for sql compatibility. The 2nd bit is used to differentiate between stellar-core -// offer ids and operation ids, which are toids. -// -// Due to the 2nd bit being used, the largest possible toid is: -// 0011111111111111111111111111111100000000000000000001000000000001 -// \ ledger /\ transaction /\ op / -// = 1073741823 -// with avg. 5 sec close time will reach in ~170 years -func EncodeOfferId(id uint64, typ OfferIDType) int64 { - // First ensure the bits we're going to change are 0s - if id&mask != 0 { - panic("Value too big to encode") - } - return int64(id | uint64(typ)<<62) -} - -// DecodeOfferID performs the reverse operation of EncodeOfferID -func DecodeOfferID(encodedId int64) (uint64, OfferIDType) { - if encodedId < 0 { - panic("Negative offer ids can not be decoded") - } - return uint64(encodedId<<2) >> 2, OfferIDType(encodedId >> 62) -} diff --git a/services/horizon/internal/test/t.go b/services/horizon/internal/test/t.go index d1992bd142..e8cdf9bc14 100644 --- a/services/horizon/internal/test/t.go +++ b/services/horizon/internal/test/t.go @@ -1,3 +1,4 @@ +//lint:file-ignore U1001 Ignore all unused code, thinks the code is unused because of the test skips package test import ( diff --git a/services/horizon/internal/test/trades/main.go b/services/horizon/internal/test/trades/main.go deleted file mode 100644 index f08ed64ed2..0000000000 --- a/services/horizon/internal/test/trades/main.go +++ /dev/null @@ -1,122 +0,0 @@ -//Common infrastructure for testing Trades -package trades - -import ( - "context" - "github.com/guregu/null" - - "github.com/stellar/go/keypair" - "github.com/stellar/go/services/horizon/internal/db2/history" - "github.com/stellar/go/support/time" - "github.com/stellar/go/xdr" -) - -//GetTestAsset generates an issuer on the fly and creates a CreditAlphanum4 Asset with given code -func GetTestAsset(code string) xdr.Asset { - var codeBytes [4]byte - copy(codeBytes[:], []byte(code)) - ca4 := xdr.AlphaNum4{Issuer: GetTestAccount(), AssetCode: codeBytes} - return xdr.Asset{Type: xdr.AssetTypeAssetTypeCreditAlphanum4, AlphaNum4: &ca4, AlphaNum12: nil} -} - -//Get generates and returns an account on the fly -func GetTestAccount() xdr.AccountId { - var key xdr.Uint256 - kp, _ := keypair.Random() - copy(key[:], kp.Address()) - acc, _ := xdr.NewAccountId(xdr.PublicKeyTypePublicKeyTypeEd25519, key) - return acc -} - -//IngestTestTrade mock ingests a trade -func IngestTestTrade( - q *history.Q, - assetSold xdr.Asset, - assetBought xdr.Asset, - seller xdr.AccountId, - buyer xdr.AccountId, - amountSold int64, - amountBought int64, - timestamp time.Millis, - opCounter int64) error { - - trade := xdr.ClaimAtom{ - Type: xdr.ClaimAtomTypeClaimAtomTypeOrderBook, - OrderBook: &xdr.ClaimOfferAtom{ - AmountBought: xdr.Int64(amountBought), - SellerId: seller, - AmountSold: xdr.Int64(amountSold), - AssetBought: assetBought, - AssetSold: assetSold, - OfferId: 100, - }, - } - - price := xdr.Price{ - N: xdr.Int32(amountBought), - D: xdr.Int32(amountSold), - } - - ctx := context.Background() - accounts, err := q.CreateAccounts(ctx, []string{seller.Address(), buyer.Address()}, 2) - if err != nil { - return err - } - assets, err := q.CreateAssets(ctx, []xdr.Asset{assetBought, assetSold}, 2) - if err != nil { - return err - } - - batch := q.NewTradeBatchInsertBuilder(0) - batch.Add(ctx, history.InsertTrade{ - HistoryOperationID: opCounter, - Order: 0, - CounterAssetID: assets[assetBought.String()].ID, - CounterAccountID: null.IntFrom(accounts[buyer.Address()]), - CounterAmount: amountBought, - - BaseAssetID: assets[assetSold.String()].ID, - BaseAccountID: null.IntFrom(accounts[seller.Address()]), - BaseAmount: amountSold, - BaseOfferID: null.IntFrom(int64(trade.OfferId())), - BaseIsSeller: true, - PriceN: int64(price.N), - PriceD: int64(price.D), - LedgerCloseTime: timestamp.ToTime(), - }) - err = batch.Exec(ctx) - if err != nil { - return err - } - - err = q.RebuildTradeAggregationTimes(context.Background(), timestamp, timestamp) - if err != nil { - return err - } - - return nil -} - -//PopulateTestTrades generates and ingests trades between two assets according to given parameters -func PopulateTestTrades( - q *history.Q, - startTs int64, - numOfTrades int, - delta int64, - opStart int64) (ass1 xdr.Asset, ass2 xdr.Asset, err error) { - - acc1 := GetTestAccount() - acc2 := GetTestAccount() - ass1 = GetTestAsset("usd") - ass2 = GetTestAsset("euro") - for i := 1; i <= numOfTrades; i++ { - timestamp := time.MillisFromInt64(startTs + (delta * int64(i-1))) - err = IngestTestTrade( - q, ass1, ass2, acc1, acc2, int64(i*100), int64(i*100)*int64(i), timestamp, opStart+int64(i)) - //tt.Assert.NoError(err) - if err != nil { - return - } - } - return -}