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

all: Add some generics and incorporate them into the codebase. #4532

Merged
merged 7 commits into from
Aug 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion exp/orderbook/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"

"github.com/stellar/go/price"
"github.com/stellar/go/support/ordered"
"github.com/stellar/go/xdr"
)

Expand Down Expand Up @@ -399,7 +400,7 @@ func (state *buyingGraphSearchState) consumeOffers(
) (xdr.Int64, error) {
nextAmount, err := consumeOffersForBuyingAsset(offers, currentAssetAmount)

return max(nextAmount, currentBestAmount), err
return ordered.Max(nextAmount, currentBestAmount), err
}

func (state *buyingGraphSearchState) considerPools() bool {
Expand Down
7 changes: 0 additions & 7 deletions exp/orderbook/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,6 @@ func getPoolAssets(pool xdr.LiquidityPoolEntry) (xdr.Asset, xdr.Asset) {
return params.AssetA, params.AssetB
}

func max(a, b xdr.Int64) xdr.Int64 {
if a < b {
return b
}
return a
}

// positiveMin returns the smallest positive value possible
func positiveMin(a, b xdr.Int64) xdr.Int64 {
if b <= 0 {
Expand Down
7 changes: 4 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ require (
github.com/gobuffalo/packr v1.12.1 // indirect
github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/go-cmp v0.5.6 // indirect
github.com/google/go-cmp v0.5.8 // indirect
github.com/google/go-querystring v0.0.0-20160401233042-9235644dd9e5 // indirect
github.com/googleapis/gax-go/v2 v2.0.5 // indirect
github.com/hashicorp/golang-lru v0.5.1 // indirect
Expand Down Expand Up @@ -107,15 +107,16 @@ require (
github.com/ziutek/mymysql v1.5.4 // indirect
go.opencensus.io v0.23.0 // indirect
golang.org/x/crypto v0.0.0-20220214200702-86341886e292 // indirect
golang.org/x/exp v0.0.0-20220722155223-a9213eeb770e
golang.org/x/lint v0.0.0-20210508222113-6edffad5e616 // indirect
golang.org/x/mod v0.4.2 // indirect
golang.org/x/mod v0.6.0-dev.0.20220106191415-9b9b3d81d5e3 // indirect
golang.org/x/net v0.0.0-20220225172249-27dd8689420f // indirect
golang.org/x/oauth2 v0.0.0-20210628180205-a41e5a781914 // indirect
golang.org/x/sys v0.0.0-20220227234510-4e6760a101f9 // indirect
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/time v0.0.0-20220224211638-0e9765cccd65
golang.org/x/tools v0.1.4 // indirect
golang.org/x/tools v0.1.10 // indirect
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20210624195500-8bfb893ecb84 // indirect
Expand Down
11 changes: 8 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,9 @@ github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
github.com/google/go-cmp v0.5.3/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ=
github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg=
github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/go-querystring v0.0.0-20160401233042-9235644dd9e5 h1:oERTZ1buOUYlpmKaqlO5fYmz8cZ1rYu5DieJzF4ZVmU=
github.com/google/go-querystring v0.0.0-20160401233042-9235644dd9e5/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck=
github.com/google/martian v2.1.0+incompatible h1:/CP5g8u/VJHijgedC/Legn3BAbAaWPgecwXBIDzw5no=
Expand Down Expand Up @@ -388,6 +389,8 @@ golang.org/x/exp v0.0.0-20191227195350-da58074b4299/go.mod h1:2RIsYlXP63K8oxa1u0
golang.org/x/exp v0.0.0-20200119233911-0405dc783f0a/go.mod h1:2RIsYlXP63K8oxa1u096TMicItID8zy7Y6sNkU49FU4=
golang.org/x/exp v0.0.0-20200207192155-f17229e696bd/go.mod h1:J/WKrq2StrnmMY6+EHIKF9dgMWnmCNThgcyBT1FY9mM=
golang.org/x/exp v0.0.0-20200224162631-6cc2880d07d6/go.mod h1:3jZMyOhIsHpP37uCMkUooju7aAi5cS1Q23tOzKc+0MU=
golang.org/x/exp v0.0.0-20220722155223-a9213eeb770e h1:+WEEuIdZHnUeJJmEUjyYC2gfUMj69yZXw17EnHg/otA=
golang.org/x/exp v0.0.0-20220722155223-a9213eeb770e/go.mod h1:Kr81I6Kryrl9sr8s2FK3vxD90NdsKWRuOIl2O4CvYbA=
golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js=
golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
Expand All @@ -413,8 +416,9 @@ golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.4.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.4.1/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.4.2 h1:Gz96sIWK3OalVv/I/qNygP42zyoKp3xptRVCWRFEBvo=
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.6.0-dev.0.20220106191415-9b9b3d81d5e3 h1:kQgndtyPBW/JIYERgdxfwMYh3AVStj88WQTlNDi2a+o=
golang.org/x/mod v0.6.0-dev.0.20220106191415-9b9b3d81d5e3/go.mod h1:3p9vT2HGsQu2K1YbXdKPJLVgG5VJdoTa1poYQBtP1AY=
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
Expand Down Expand Up @@ -602,8 +606,9 @@ golang.org/x/tools v0.1.0/go.mod h1:xkSsbof2nBLbhDlRMhhhyNLN/zl3eTqcnHD5viDpcZ0=
golang.org/x/tools v0.1.1/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/tools v0.1.2/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/tools v0.1.3/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/tools v0.1.4 h1:cVngSRcfgyZCzys3KYOpCFa+4dqX/Oub9tAq00ttGVs=
golang.org/x/tools v0.1.4/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/tools v0.1.10 h1:QjFRCZxdOhBJ/UNgnBZLbNV13DlbnK0quyivTnXJM20=
golang.org/x/tools v0.1.10/go.mod h1:Uh6Zz+xoGYZom868N8YTex3t7RhtHDBrE8Gzo9bV56E=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
Expand Down
37 changes: 11 additions & 26 deletions historyarchive/range.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ package historyarchive

import (
"fmt"
"sort"
"strings"

"github.com/stellar/go/support/ordered"
"golang.org/x/exp/slices"
)

const DefaultCheckpointFrequency = uint32(64)
Expand Down Expand Up @@ -54,10 +56,8 @@ func (c CheckpointManager) NextCheckpoint(i uint32) uint32 {
freq := uint64(c.checkpointFreq)
v := uint64(i)
n := (((v + freq) / freq) * freq) - 1
if n >= 0xffffffff {
return 0xffffffff
}
return uint32(n)

return uint32(ordered.Min(n, 0xffffffff))
}

// GetCheckPoint gets the checkpoint containing information about the given ledger sequence
Expand All @@ -80,24 +80,16 @@ func (c CheckpointManager) GetCheckpointRange(i uint32) Range {
}

func (c CheckpointManager) MakeRange(low uint32, high uint32) Range {
if high < low {
high = low
}
high = ordered.Max(high, low)
return Range{
Low: c.PrevCheckpoint(low),
High: c.NextCheckpoint(high),
}
}

func (r Range) clamp(other Range, cManager CheckpointManager) Range {
low := r.Low
high := r.High
if low < other.Low {
low = other.Low
}
if high > other.High {
high = other.High
}
low := ordered.Max(r.Low, other.Low)
high := ordered.Min(r.High, other.High)
return cManager.MakeRange(low, high)
}

Expand All @@ -123,24 +115,17 @@ func (r Range) SizeInCheckPoints(cManager CheckpointManager) int {
func (r Range) collapsedString() string {
if r.Low == r.High {
return fmt.Sprintf("0x%8.8x", r.Low)
} else {
return fmt.Sprintf("[0x%8.8x-0x%8.8x]", r.Low, r.High)
}

return fmt.Sprintf("[0x%8.8x-0x%8.8x]", r.Low, r.High)
}

func (r Range) InRange(sequence uint32) bool {
return sequence >= r.Low && sequence <= r.High
}

type byUint32 []uint32

func (a byUint32) Len() int { return len(a) }
func (a byUint32) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a byUint32) Less(i, j int) bool { return a[i] < a[j] }

func fmtRangeList(vs []uint32, cManager CheckpointManager) string {

sort.Sort(byUint32(vs))
slices.Sort(vs)

s := make([]string, 0, 10)
var curr *Range
Expand Down
11 changes: 2 additions & 9 deletions price/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"regexp"
"strconv"

"github.com/stellar/go/support/ordered"
"github.com/stellar/go/xdr"
)

Expand Down Expand Up @@ -163,7 +164,7 @@ func ConvertToBuyingUnits(sellingOfferAmount int64, sellingUnitsNeeded int64, pr
}

// pathPaymentAmountBought
result = min(result, sellingUnitsNeeded)
result = ordered.Min(result, sellingUnitsNeeded)
sellingUnitsExtracted := result

// pathPaymentAmountSold
Expand Down Expand Up @@ -218,11 +219,3 @@ func mulFractionRoundUp(x int64, n int64, d int64) (int64, error) {

return int64(q), nil
}

// min impl for int64
func min(x int64, y int64) int64 {
if x <= y {
return x
}
return y
}
4 changes: 2 additions & 2 deletions services/horizon/docker/verify-range/dependencies
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ echo "deb https://apt.stellar.org $(lsb_release -cs) stable" | sudo tee -a /etc/
apt-get update
apt-get install -y stellar-core=${STELLAR_CORE_VERSION}

wget -q https://dl.google.com/go/go1.17.linux-amd64.tar.gz
tar -C /usr/local -xzf go1.17.linux-amd64.tar.gz
wget -q https://dl.google.com/go/go1.18.linux-amd64.tar.gz
tar -C /usr/local -xzf go1.18.linux-amd64.tar.gz

git clone https://github.com/stellar/go.git stellar-go
cd stellar-go
Expand Down
23 changes: 5 additions & 18 deletions services/horizon/internal/db2/history/ledger.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/guregu/null"
"github.com/stellar/go/services/horizon/internal/db2"
"github.com/stellar/go/support/errors"
"github.com/stellar/go/support/ordered"
"github.com/stellar/go/toid"
"github.com/stellar/go/xdr"
)
Expand Down Expand Up @@ -153,20 +154,6 @@ func (q *Q) GetLedgerGaps(ctx context.Context) ([]LedgerRange, error) {
return gaps, nil
}

func max(a, b uint32) uint32 {
if a > b {
return a
}
return b
}

func min(a, b uint32) uint32 {
if a > b {
return b
}
return a
}

// GetLedgerGapsInRange obtains ingestion gaps in the history_ledgers table within the given range.
// Returns the gaps and error.
func (q *Q) GetLedgerGapsInRange(ctx context.Context, start, end uint32) ([]LedgerRange, error) {
Expand All @@ -189,7 +176,7 @@ func (q *Q) GetLedgerGapsInRange(ctx context.Context, start, end uint32) ([]Ledg
if start < oldestLedger {
result = append(result, LedgerRange{
StartSequence: start,
EndSequence: min(end, oldestLedger-1),
EndSequence: ordered.Min(end, oldestLedger-1),
})
}
if end <= oldestLedger {
Expand All @@ -209,14 +196,14 @@ func (q *Q) GetLedgerGapsInRange(ctx context.Context, start, end uint32) ([]Ledg
break
}
result = append(result, LedgerRange{
StartSequence: max(gap.StartSequence, start),
EndSequence: min(gap.EndSequence, end),
StartSequence: ordered.Max(gap.StartSequence, start),
EndSequence: ordered.Min(gap.EndSequence, end),
})
}

if latestLedger < end {
result = append(result, LedgerRange{
StartSequence: max(latestLedger+1, start),
StartSequence: ordered.Max(latestLedger+1, start),
EndSequence: end,
})
}
Expand Down
5 changes: 3 additions & 2 deletions services/horizon/internal/db2/history/ledger_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package history
import (
"context"

"github.com/stellar/go/support/collections/set"
"github.com/stellar/go/support/errors"
)

Expand All @@ -11,10 +12,10 @@ func (lc *LedgerCache) Queue(seq int32) {
lc.lock.Lock()

if lc.queued == nil {
lc.queued = map[int32]struct{}{}
lc.queued = set.Set[int32]{}
}

lc.queued[seq] = struct{}{}
lc.queued.Add(seq)
lc.lock.Unlock()
}

Expand Down
3 changes: 2 additions & 1 deletion services/horizon/internal/db2/history/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/lib/pq"

"github.com/stellar/go/services/horizon/internal/db2"
"github.com/stellar/go/support/collections/set"
"github.com/stellar/go/support/db"
"github.com/stellar/go/support/errors"
strtime "github.com/stellar/go/support/time"
Expand Down Expand Up @@ -580,7 +581,7 @@ type LedgerCache struct {
Records map[int32]Ledger

lock sync.Mutex
queued map[int32]struct{}
queued set.Set[int32]
}

type LedgerRange struct {
Expand Down
9 changes: 5 additions & 4 deletions services/horizon/internal/ingest/filters/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ import (
"github.com/stellar/go/ingest"
"github.com/stellar/go/services/horizon/internal/db2/history"
"github.com/stellar/go/services/horizon/internal/ingest/processors"
"github.com/stellar/go/support/collections/set"
)

type accountFilter struct {
whitelistedAccountsSet map[string]struct{}
whitelistedAccountsSet set.Set[string]
lastModified int64
enabled bool
}
Expand All @@ -21,7 +22,7 @@ type AccountFilter interface {

func NewAccountFilter() AccountFilter {
return &accountFilter{
whitelistedAccountsSet: map[string]struct{}{},
whitelistedAccountsSet: set.Set[string]{},
}
}

Expand All @@ -32,7 +33,7 @@ func (filter *accountFilter) RefreshAccountFilter(filterConfig *history.AccountF
logger.Infof("New Account Filter config detected, reloading new config %v ", *filterConfig)

filter.enabled = filterConfig.Enabled
filter.whitelistedAccountsSet = listToMap(filterConfig.Whitelist)
filter.whitelistedAccountsSet = listToSet(filterConfig.Whitelist)
filter.lastModified = filterConfig.LastModified
}

Expand All @@ -53,7 +54,7 @@ func (f *accountFilter) FilterTransaction(ctx context.Context, transaction inges
// NOTE: this assumes that the participant list has a small memory footprint
// otherwise, we should be doing the filtering on the DB side
for _, p := range participants {
if _, ok := f.whitelistedAccountsSet[p.Address()]; ok {
if f.whitelistedAccountsSet.Contains(p.Address()) {
return true, nil
}
}
Expand Down
Loading