diff --git a/.dockerignore b/.dockerignore index a5b4d1f..d06ec8b 100644 --- a/.dockerignore +++ b/.dockerignore @@ -4,6 +4,7 @@ .gitignore testdata/ docker/nginx +docker/postgres docker-compose.override.yml p2pool.cache p2pool_peers.txt diff --git a/.drone.yml b/.drone.yml index b96a2c6..cc9a540 100644 --- a/.drone.yml +++ b/.drone.yml @@ -11,6 +11,7 @@ environment: GOARCH: amd64 GOAMD64: v3 GOOS: linux + GOEXPERIMENT: loopvar CFLAGS: "-march=native -Ofast" LDFLAGS: "-flto" PKG_CONFIG_PATH: "/drone/src/build_deps/lib/pkgconfig" @@ -35,7 +36,7 @@ steps: - cd /tmp - git clone --depth 1 --branch master https://github.com/tevador/RandomX.git && cd RandomX && mkdir build && cd build && cmake .. -DCMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX:PATH=/drone/src/build_deps && make -j$(nproc) && make install && cd ../.. - name: test-cgo - image: golang:1.20-alpine + image: golang:1.21-rc-alpine3.18 depends_on: - init-tests - build-sources diff --git a/cmd/api/api.go b/cmd/api/api.go index 5ffb2fa..c9ee0ad 100644 --- a/cmd/api/api.go +++ b/cmd/api/api.go @@ -18,10 +18,9 @@ import ( "git.gammaspectra.live/P2Pool/p2pool-observer/utils" "github.com/ake-persson/mapslice-json" "github.com/gorilla/mux" - "golang.org/x/exp/maps" - "golang.org/x/exp/slices" "io" "log" + "maps" "math" "net/http" _ "net/http/pprof" @@ -29,6 +28,7 @@ import ( "net/url" "os" "runtime/pprof" + "slices" "strconv" "strings" "sync/atomic" @@ -163,7 +163,7 @@ func main() { //no changes! //this race is ok - oldPoolInfo.SideChain.SecondsSinceLastBlock = utils.Max(0, time.Now().Unix()-int64(tip.Timestamp)) + oldPoolInfo.SideChain.SecondsSinceLastBlock = max(0, time.Now().Unix()-int64(tip.Timestamp)) return } @@ -209,13 +209,13 @@ func main() { // got error, just update last pool //this race is ok - oldPoolInfo.SideChain.SecondsSinceLastBlock = utils.Max(0, time.Now().Unix()-int64(tip.Timestamp)) + oldPoolInfo.SideChain.SecondsSinceLastBlock = max(0, time.Now().Unix()-int64(tip.Timestamp)) } return } - slices.SortFunc(versions, func(a, b cmdutils.SideChainVersionEntry) bool { - return a.Weight.Cmp(b.Weight) > 0 + slices.SortFunc(versions, func(a, b cmdutils.SideChainVersionEntry) int { + return a.Weight.Cmp(b.Weight) * -1 }) for i := range versions { @@ -277,7 +277,7 @@ func main() { } averageEffort := func(limit int) (result float64) { - maxI := utils.Min(limit, len(blockEfforts)) + maxI := min(limit, len(blockEfforts)) for i, e := range blockEfforts { result += e.Value.(float64) if i+1 == maxI { @@ -296,7 +296,7 @@ func main() { Difficulty: types.DifficultyFrom64(tip.Difficulty), CumulativeDifficulty: tip.CumulativeDifficulty, Timestamp: tip.Timestamp, - SecondsSinceLastBlock: utils.Max(0, time.Now().Unix()-int64(tip.Timestamp)), + SecondsSinceLastBlock: max(0, time.Now().Unix()-int64(tip.Timestamp)), Effort: cmdutils.PoolInfoResultSideChainEffort{ Current: currentEffort, Average10: averageEffort(10), @@ -1422,8 +1422,8 @@ func main() { sortedAddresses := maps.Keys(addresses) //Sort based on addresses - slices.SortFunc(sortedAddresses, func(a address.PackedAddress, b address.PackedAddress) bool { - return a.Compare(&b) < 0 + slices.SortFunc(sortedAddresses, func(a address.PackedAddress, b address.PackedAddress) int { + return a.Compare(&b) }) //Shuffle shares diff --git a/cmd/api/payout_hint.go b/cmd/api/payout_hint.go index a2ffe3c..4b48401 100644 --- a/cmd/api/payout_hint.go +++ b/cmd/api/payout_hint.go @@ -4,7 +4,7 @@ import ( "git.gammaspectra.live/P2Pool/p2pool-observer/index" "git.gammaspectra.live/P2Pool/p2pool-observer/p2pool/api" "git.gammaspectra.live/P2Pool/p2pool-observer/types" - "golang.org/x/exp/slices" + "slices" ) func PayoutAmountHint(shares map[uint64]types.Difficulty, reward uint64) map[uint64]uint64 { diff --git a/cmd/archivetoindex/archivetoindex.go b/cmd/archivetoindex/archivetoindex.go index cf399ae..b8607e1 100644 --- a/cmd/archivetoindex/archivetoindex.go +++ b/cmd/archivetoindex/archivetoindex.go @@ -12,7 +12,6 @@ import ( "git.gammaspectra.live/P2Pool/p2pool-observer/p2pool/cache/archive" "git.gammaspectra.live/P2Pool/p2pool-observer/p2pool/sidechain" "git.gammaspectra.live/P2Pool/p2pool-observer/types" - "git.gammaspectra.live/P2Pool/p2pool-observer/utils" "github.com/floatdrop/lru" "log" "math" @@ -322,7 +321,7 @@ func main() { for stride := uint64(0); stride <= strides; stride++ { start := minHeight + stride*strideSize - end := utils.Min(maxHeight, minHeight+stride*strideSize+strideSize) + end := min(maxHeight, minHeight+stride*strideSize+strideSize) log.Printf("checking %d to %d", start, end) if headers, err := client.GetDefaultClient().GetBlockHeadersRangeResult(start, end, ctx); err != nil { log.Panic(err) diff --git a/cmd/daemon/daemon.go b/cmd/daemon/daemon.go index 0eae7f5..543a655 100644 --- a/cmd/daemon/daemon.go +++ b/cmd/daemon/daemon.go @@ -13,7 +13,6 @@ import ( p2poolapi "git.gammaspectra.live/P2Pool/p2pool-observer/p2pool/api" "git.gammaspectra.live/P2Pool/p2pool-observer/p2pool/sidechain" "git.gammaspectra.live/P2Pool/p2pool-observer/types" - "git.gammaspectra.live/P2Pool/p2pool-observer/utils" "log" "net/http" _ "net/http/pprof" @@ -184,7 +183,7 @@ func main() { //backfill headers for stride := uint64(0); stride <= strides; stride++ { start := currentHeight + stride*strideSize - end := utils.Min(maxHeight-1, currentHeight+stride*strideSize+strideSize) + end := min(maxHeight-1, currentHeight+stride*strideSize+strideSize) log.Printf("checking %d to %d", start, end) if headers, err := client.GetDefaultClient().GetBlockHeadersRangeResult(start, end, ctx); err != nil { log.Panic(err) diff --git a/cmd/daemon/ws.go b/cmd/daemon/ws.go index 2e6fb45..46c848f 100644 --- a/cmd/daemon/ws.go +++ b/cmd/daemon/ws.go @@ -7,10 +7,10 @@ import ( "git.gammaspectra.live/P2Pool/p2pool-observer/p2pool/api" types2 "git.gammaspectra.live/P2Pool/p2pool-observer/p2pool/types" "git.gammaspectra.live/P2Pool/p2pool-observer/utils" - "golang.org/x/exp/slices" "log" "net/http" "nhooyr.io/websocket" + "slices" "sync" "sync/atomic" "time" @@ -40,9 +40,9 @@ func (b *SortedBuf[T]) Insert(value T) bool { return false } b.buf = append(b.buf, value) - slices.SortFunc(b.buf, func(i, j T) bool { + slices.SortFunc(b.buf, func(i, j T) int { //keep highest value at 0 - return b.compare(i, j) > 0 + return b.compare(i, j) * -1 }) b.buf = b.buf[:len(b.buf)-1] return b.Has(value) @@ -55,9 +55,9 @@ func (b *SortedBuf[T]) Remove(value T) { b.buf[i] = zeroValue } } - slices.SortFunc(b.buf, func(i, j T) bool { + slices.SortFunc(b.buf, func(i, j T) int { //keep highest value at index 0 - return b.compare(i, j) > 0 + return b.compare(i, j) * -1 }) } @@ -298,19 +298,19 @@ func setupEventHandler(p2api *api.P2PoolApi, indexDb *index.Index) { }() //sort for proper order - slices.SortFunc(blocksToReport, func(a, b *index.SideBlock) bool { + slices.SortFunc(blocksToReport, func(a, b *index.SideBlock) int { if a.EffectiveHeight < b.EffectiveHeight { - return true + return -1 } else if a.EffectiveHeight > b.EffectiveHeight { - return false + return 1 } if a.SideHeight < b.SideHeight { - return true + return -1 } else if a.SideHeight > b.SideHeight { - return false + return 1 } //same height, sort by main id - return a.MainId.Compare(b.MainId) < 0 + return a.MainId.Compare(b.MainId) }) func() { @@ -368,45 +368,45 @@ func setupEventHandler(p2api *api.P2PoolApi, indexDb *index.Index) { }() //sort for proper order - slices.SortFunc(blocksToReport, func(a, b *index.FoundBlock) bool { + slices.SortFunc(blocksToReport, func(a, b *index.FoundBlock) int { if a.MainBlock.Height < b.MainBlock.Height { - return true + return -1 } else if a.MainBlock.Height > b.MainBlock.Height { - return false + return 1 } if a.EffectiveHeight < b.EffectiveHeight { - return true + return -1 } else if a.EffectiveHeight > b.EffectiveHeight { - return false + return 1 } if a.SideHeight < b.SideHeight { - return true + return -1 } else if a.SideHeight > b.SideHeight { - return false + return 1 } //same height, sort by main id - return a.MainBlock.Id.Compare(b.MainBlock.Id) < 0 + return a.MainBlock.Id.Compare(b.MainBlock.Id) }) //sort for proper order - slices.SortFunc(unfoundBlocksToReport, func(a, b *index.SideBlock) bool { + slices.SortFunc(unfoundBlocksToReport, func(a, b *index.SideBlock) int { if a.MainHeight < b.MainHeight { - return true + return -1 } else if a.MainHeight > b.MainHeight { - return false + return 1 } if a.EffectiveHeight < b.EffectiveHeight { - return true + return -1 } else if a.EffectiveHeight > b.EffectiveHeight { - return false + return 1 } if a.SideHeight < b.SideHeight { - return true + return -1 } else if a.SideHeight > b.SideHeight { - return false + return 1 } //same height, sort by main id - return a.MainId.Compare(b.MainId) < 0 + return a.MainId.Compare(b.MainId) }) func() { diff --git a/cmd/p2pool/p2pool.go b/cmd/p2pool/p2pool.go index e775c44..0217c02 100644 --- a/cmd/p2pool/p2pool.go +++ b/cmd/p2pool/p2pool.go @@ -10,7 +10,6 @@ import ( "git.gammaspectra.live/P2Pool/p2pool-observer/p2pool/sidechain" "git.gammaspectra.live/P2Pool/p2pool-observer/p2pool/types" "git.gammaspectra.live/P2Pool/p2pool-observer/utils" - "golang.org/x/exp/slices" "log" "net" "net/http" @@ -20,6 +19,7 @@ import ( "os/signal" "runtime" "runtime/debug" + "slices" "strconv" "strings" "sync" @@ -248,8 +248,8 @@ func main() { for range utils.ContextTick(instance.Context(), time.Minute*1) { contents = contents[:0] peerListEntries := instance.Server().PeerList() - slices.SortFunc(peerListEntries, func(a, b *p2p.PeerListEntry) bool { - return a.AddressPort.Addr().Compare(b.AddressPort.Addr()) < 0 + slices.SortFunc(peerListEntries, func(a, b *p2p.PeerListEntry) int { + return a.AddressPort.Addr().Compare(b.AddressPort.Addr()) }) for _, addrPort := range peerListEntries { contents = append(contents, []byte(addrPort.AddressPort.String())...) diff --git a/cmd/recoverpoolblock/recoverpoolblock.go b/cmd/recoverpoolblock/recoverpoolblock.go index 9e75112..2079baf 100644 --- a/cmd/recoverpoolblock/recoverpoolblock.go +++ b/cmd/recoverpoolblock/recoverpoolblock.go @@ -23,11 +23,11 @@ import ( "git.gammaspectra.live/P2Pool/p2pool-observer/utils" "git.gammaspectra.live/P2Pool/sha3" "github.com/floatdrop/lru" - "golang.org/x/exp/slices" "log" "math" "os" "runtime" + "slices" "sync" "sync/atomic" ) diff --git a/cmd/utils/position_chart.go b/cmd/utils/position_chart.go index 6f3f015..0bc8739 100644 --- a/cmd/utils/position_chart.go +++ b/cmd/utils/position_chart.go @@ -2,7 +2,7 @@ package utils import ( "git.gammaspectra.live/P2Pool/p2pool-observer/utils" - "golang.org/x/exp/slices" + "slices" ) type PositionChart struct { diff --git a/cmd/web/views/funcs.go b/cmd/web/views/funcs.go index 1c67f01..4956d93 100644 --- a/cmd/web/views/funcs.go +++ b/cmd/web/views/funcs.go @@ -10,18 +10,17 @@ import ( types2 "git.gammaspectra.live/P2Pool/p2pool-observer/p2pool/types" "git.gammaspectra.live/P2Pool/p2pool-observer/types" "git.gammaspectra.live/P2Pool/p2pool-observer/utils" - "golang.org/x/exp/constraints" "log" "strconv" "strings" "time" ) -func utc_date[T constraints.Integer | constraints.Float](v T) string { +func utc_date[T int64 | uint64 | int | float64](v T) string { return time.Unix(int64(v), 0).UTC().Format("02-01-2006 15:04:05 MST") } -func time_elapsed_short[T constraints.Integer | constraints.Float](v T) string { +func time_elapsed_short[T int64 | uint64 | int | float64](v T) string { diff := time.Since(time.Unix(int64(v), 0).UTC()) days := int64(diff.Hours() / 24) @@ -101,14 +100,14 @@ func side_block_valuation(b any, consensus *sidechain.Consensus) string { } } -func si_units[T constraints.Integer | constraints.Float](v T, n ...int) string { +func si_units[T int64 | uint64 | int | float64](v T, n ...int) string { if len(n) > 0 { return utils.SiUnits(float64(v), n[0]) } else { return utils.SiUnits(float64(v), 3) } } -func date_diff_short[T constraints.Integer | constraints.Float](v T) string { +func date_diff_short[T int64 | uint64 | int | float64](v T) string { diff := time.Since(time.Unix(int64(v), 0).UTC()) s := fmt.Sprintf("%02d:%02d:%02d", int64(diff.Hours())%24, int64(diff.Minutes())%60, int64(diff.Seconds())%60) @@ -120,7 +119,7 @@ func date_diff_short[T constraints.Integer | constraints.Float](v T) string { return s } -func time_duration_long[T constraints.Integer | constraints.Float](v T) string { +func time_duration_long[T int64 | uint64 | int | float64](v T) string { diff := time.Second * time.Duration(v) diff += time.Microsecond * time.Duration((float64(uint64(v))-float64(v))*1000000) days := int64(diff.Hours() / 24) diff --git a/cmd/web/views/tpl_found_blocks.qtpl b/cmd/web/views/tpl_found_blocks.qtpl index e0a78a3..f9e0d4b 100644 --- a/cmd/web/views/tpl_found_blocks.qtpl +++ b/cmd/web/views/tpl_found_blocks.qtpl @@ -1,6 +1,6 @@ {% import "git.gammaspectra.live/P2Pool/p2pool-observer/index" %} {% import "git.gammaspectra.live/P2Pool/p2pool-observer/types" %} -{% import "golang.org/x/exp/slices" %} +{% import "slices" %} {% import "github.com/ake-persson/mapslice-json" %} {% func TemplateFoundBlocks(ctx *GlobalRequestContext, foundBlocks []*index.FoundBlock, isMiner bool) %} diff --git a/cmd/web/web.go b/cmd/web/web.go index df367ff..675f72f 100644 --- a/cmd/web/web.go +++ b/cmd/web/web.go @@ -17,10 +17,9 @@ import ( "git.gammaspectra.live/P2Pool/p2pool-observer/utils" "github.com/gorilla/mux" "github.com/valyala/quicktemplate" - "golang.org/x/exp/maps" - "golang.org/x/exp/slices" "io" "log" + "maps" "math" "net/http" _ "net/http/pprof" @@ -28,6 +27,7 @@ import ( "net/url" "os" "runtime/pprof" + "slices" "strconv" "strings" "sync" @@ -707,8 +707,8 @@ func main() { } minerKeys := maps.Keys(miners) - slices.SortFunc(minerKeys, func(a uint64, b uint64) bool { - return miners[a].Weight.Cmp(miners[b].Weight) > 0 + slices.SortFunc(minerKeys, func(a uint64, b uint64) int { + return miners[a].Weight.Cmp(miners[b].Weight) * -1 }) sortedMiners := make([]*views.MinersPageMinerEntry, len(minerKeys)) @@ -1037,7 +1037,7 @@ func main() { } previous := lastShares[i+1] - timeDelta := uint64(utils.Max(int64(s.Timestamp)-int64(previous.Timestamp), 0)) + timeDelta := uint64(max(int64(s.Timestamp)-int64(previous.Timestamp), 0)) expectedCumDiff := types.DifficultyFrom64(dailyHashRate).Mul64(timeDelta) diff --git a/docker-compose.yml b/docker-compose.yml index e5c59eb..c6792e5 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -99,8 +99,6 @@ services: - BUILD_BINARY=p2pool - GOPROXY=${GOPROXY} restart: always - depends_on: - - db security_opt: - no-new-privileges:true volumes: @@ -198,7 +196,7 @@ services: -api-host "http://p2pool:3131" -db="host=db port=5432 dbname=p2pool user=p2pool password=p2pool sslmode=disable" pgo-collector: - image: golang:1.20-alpine + image: golang:1.21-rc-alpine restart: always environment: - GOPROXY=${GOPROXY} diff --git a/docker/golang/Dockerfile b/docker/golang/Dockerfile index 8c780e0..59b60c7 100644 --- a/docker/golang/Dockerfile +++ b/docker/golang/Dockerfile @@ -1,4 +1,4 @@ -FROM golang:1.20-alpine +FROM golang:1.21-rc-alpine3.18 AS builder ENV CFLAGS="-march=native -Ofast" @@ -35,6 +35,15 @@ RUN go generate -v ./... ARG BUILD_BINARY +ENV GOEXPERIMENT=loopvar + RUN go build -buildvcs=false -trimpath -v -pgo=/tmp/pgo-profile-latest.pprof -o /usr/bin/${BUILD_BINARY} git.gammaspectra.live/P2Pool/p2pool-observer/cmd/${BUILD_BINARY} +FROM alpine:3.18 + +RUN apk update && apk add --no-cache \ + libstdc++ libgcc + +COPY --from=builder /usr/bin/${BUILD_BINARY} /usr/bin/${BUILD_BINARY} + WORKDIR /data \ No newline at end of file diff --git a/docker/nginx/Dockerfile b/docker/nginx/Dockerfile index ecbe16a..1bbcec4 100644 --- a/docker/nginx/Dockerfile +++ b/docker/nginx/Dockerfile @@ -1,4 +1,4 @@ -FROM nginx:1.24-alpine +FROM nginx:1.25-alpine COPY static /web COPY snippets/*.conf /etc/nginx/snippets/ diff --git a/go.mod b/go.mod index 40991c2..4ebf1b2 100644 --- a/go.mod +++ b/go.mod @@ -1,9 +1,9 @@ module git.gammaspectra.live/P2Pool/p2pool-observer -go 1.20 +go 1.21 require ( - git.gammaspectra.live/P2Pool/edwards25519 v0.0.0-20230520084036-0f06fa066019 + git.gammaspectra.live/P2Pool/edwards25519 v0.0.0-20230701100949-027561bd2a33 git.gammaspectra.live/P2Pool/go-monero v0.0.0-20230410011208-910450c4a523 git.gammaspectra.live/P2Pool/go-randomx v0.0.0-20221027085532-f46adfce03a7 git.gammaspectra.live/P2Pool/moneroutil v0.0.0-20230527152251-7b24ed2d11ce @@ -21,7 +21,6 @@ require ( github.com/stretchr/testify v1.8.1 github.com/valyala/quicktemplate v1.7.0 go.etcd.io/bbolt v1.3.7 - golang.org/x/exp v0.0.0-20230522175609-2e198f4a06a1 golang.org/x/sys v0.9.0 lukechampine.com/uint128 v1.3.0 nhooyr.io/websocket v1.8.7 @@ -32,11 +31,12 @@ require ( github.com/davecgh/go-spew v1.1.1 // indirect github.com/dolthub/maphash v0.1.0 // indirect github.com/go-zeromq/goczmq/v4 v4.2.2 // indirect + github.com/google/go-cmp v0.5.8 // indirect github.com/klauspost/compress v1.16.6 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect github.com/valyala/bytebufferpool v1.0.0 // indirect golang.org/x/crypto v0.10.0 // indirect - golang.org/x/sync v0.2.0 // indirect + golang.org/x/sync v0.3.0 // indirect golang.org/x/text v0.10.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index a1e676a..d3c7727 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,5 @@ -git.gammaspectra.live/P2Pool/edwards25519 v0.0.0-20230520084036-0f06fa066019 h1:A256rPumLHvYEJdgOQQAHZX7zOZTSYiGcsT5rykpcH4= -git.gammaspectra.live/P2Pool/edwards25519 v0.0.0-20230520084036-0f06fa066019/go.mod h1:336HUKX25mQ1qUtzkwV9Wrqi153tTgUOKcIhpYuF2ts= +git.gammaspectra.live/P2Pool/edwards25519 v0.0.0-20230701100949-027561bd2a33 h1:BPV7iIiv8T+X7gg9/JfNmEBoH4HXOkw8CR7FN6bBwB8= +git.gammaspectra.live/P2Pool/edwards25519 v0.0.0-20230701100949-027561bd2a33/go.mod h1:336HUKX25mQ1qUtzkwV9Wrqi153tTgUOKcIhpYuF2ts= git.gammaspectra.live/P2Pool/go-monero v0.0.0-20230410011208-910450c4a523 h1:oIJzm7kQyASS0xlJ79VSWRvvfXp2Qt7M05+E20o9gwE= git.gammaspectra.live/P2Pool/go-monero v0.0.0-20230410011208-910450c4a523/go.mod h1:TAOAAV972JNDkCzyV5SkbYkKCRvcfhvvFa8LHH4Dg6g= git.gammaspectra.live/P2Pool/go-randomx v0.0.0-20221027085532-f46adfce03a7 h1:bzHDuu1IgETKqPBOlIdCE2LaZIJ+ZpROSprNn+fnzd8= @@ -56,6 +56,7 @@ github.com/golang/protobuf v1.3.5/go.mod h1:6O5/vntMXwX2lRkT1hjjk0nAC1IDOTvTlVgj github.com/golang/snappy v0.0.3/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= github.com/google/go-cmp v0.4.0/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/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/gordonklaus/ineffassign v0.0.0-20210522101830-0589229737b2/go.mod h1:M9mZEtGIsR1oDaZagNPNG9iq9n2HrhZ17dsXk73V3Lw= github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI= @@ -75,20 +76,24 @@ github.com/klauspost/compress v1.13.5/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47e github.com/klauspost/compress v1.16.6 h1:91SKEy4K37vkp255cJ8QesJhjyRO0hn9i9G0GoUwLsk= github.com/klauspost/compress v1.16.6/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= +github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/leodido/go-urn v1.2.0 h1:hpXL4XnriNwQ/ABnpepYM/1vCLWNDfUNts8dX3xTG6Y= github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII= github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw= github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= github.com/mattn/go-isatty v0.0.18 h1:DOKFKCQ7FNG2L1rbrmstDN4QVRdS89Nkh85u68Uwp98= +github.com/mattn/go-isatty v0.0.18/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc= github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 h1:Esafd1046DLDQ0W1YjYsBW+p8U2u7vzgW2SQVmlNazg= github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs= +github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/sclevine/spec v1.4.0 h1:z/Q9idDcay5m5irkZ28M7PtQM4aOISzOpj4bUPkDee8= +github.com/sclevine/spec v1.4.0/go.mod h1:LvpgJaFyvQzRvc1kaDs0bulYwzC70PbiYjC4QnFHkOM= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= @@ -99,6 +104,7 @@ github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk= github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= github.com/thepudds/swisstable v0.0.0-20221011152303-9c77dc657777 h1:5u+6YWU2faS+Sr/x8j9yalMpSDUkatNOZWXV3wMUCGQ= +github.com/thepudds/swisstable v0.0.0-20221011152303-9c77dc657777/go.mod h1:4af3KxEsswy6aTzsTcwa8QZUSh4V+80oHdp1QX9uJHA= github.com/ugorji/go v1.1.7 h1:/68gy2h+1mWMrwZFeD1kQialdSzAb432dtpeJ42ovdo= github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw= github.com/ugorji/go/codec v1.1.7 h1:2SvQaVZ1ouYrrKKwoSk2pzd4A9evlKJb9oTL+OaLUSs= @@ -120,8 +126,6 @@ golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPh golang.org/x/crypto v0.0.0-20210513164829-c07d793c2f9a/go.mod h1:P+XmwS30IXTQdn5tA2iutPOUgjI07+tq3H3K9MVA1s8= golang.org/x/crypto v0.10.0 h1:LKqV2xt9+kDzSTfOhx4FrkEBcMrAgHSYgzywV9zcGmM= golang.org/x/crypto v0.10.0/go.mod h1:o4eNf7Ede1fv+hwOwZsTHl9EsPFO6q6ZvYR8vYfY45I= -golang.org/x/exp v0.0.0-20230522175609-2e198f4a06a1 h1:k/i9J1pBpvlfR+9QsetwPyERsqu1GIbi967PQMq3Ivc= -golang.org/x/exp v0.0.0-20230522175609-2e198f4a06a1/go.mod h1:V1LtkGg67GoY2N1AnLN78QLrzxkLyJw7RJb1gzOOz9w= golang.org/x/lint v0.0.0-20210508222113-6edffad5e616/go.mod h1:3xt1FjdF8hUf6vQPIChWIBhFzV8gjjsPE/fR3IyQdNY= golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg= golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= @@ -138,8 +142,8 @@ golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJ golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.2.0 h1:PUR+T4wwASmuSTYdKjYHI5TD22Wy5ogLU5qZCOLxBrI= -golang.org/x/sync v0.2.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.3.0 h1:ftCYgMx6zT/asHUrPw8BLLscYtGznsLAnjq5RH9P66E= +golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= @@ -173,6 +177,7 @@ golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8T golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f h1:BLraFXnmrev5lT+xlilqcH8XK9/i0At2xKjWk4p6zsU= +gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10= gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/index/index.go b/index/index.go index 19292c8..5ef0265 100644 --- a/index/index.go +++ b/index/index.go @@ -15,12 +15,11 @@ import ( "git.gammaspectra.live/P2Pool/p2pool-observer/utils" "github.com/floatdrop/lru" "github.com/lib/pq" - _ "github.com/lib/pq" - "golang.org/x/exp/slices" "log" "reflect" "regexp" "runtime/pprof" + "slices" "strings" "sync" ) @@ -1055,8 +1054,8 @@ func (r TransactionInputQueryResults) Match() (result TransactionInputQueryResul result = append(result, *v) } - slices.SortFunc(result, func(a, b TransactionInputQueryResultsMatch) bool { - return a.Count > b.Count + slices.SortFunc(result, func(a, b TransactionInputQueryResultsMatch) int { + return int(a.Count) - int(b.Count) }) return result diff --git a/index/utils.go b/index/utils.go index ac19858..70cb4e6 100644 --- a/index/utils.go +++ b/index/utils.go @@ -6,7 +6,7 @@ import ( "git.gammaspectra.live/P2Pool/p2pool-observer/monero/randomx" "git.gammaspectra.live/P2Pool/p2pool-observer/p2pool/sidechain" "git.gammaspectra.live/P2Pool/p2pool-observer/types" - "golang.org/x/exp/slices" + "slices" ) type GetByTemplateIdFunc func(h types.Hash) *SideBlock diff --git a/monero/address/address.go b/monero/address/address.go index 1d7b875..8a541fa 100644 --- a/monero/address/address.go +++ b/monero/address/address.go @@ -5,7 +5,7 @@ import ( "errors" "git.gammaspectra.live/P2Pool/moneroutil" "git.gammaspectra.live/P2Pool/p2pool-observer/monero/crypto" - "golang.org/x/exp/slices" + "slices" ) type Address struct { diff --git a/monero/client/zmq/zmq.go b/monero/client/zmq/zmq.go index 78662ff..c9f0cb4 100644 --- a/monero/client/zmq/zmq.go +++ b/monero/client/zmq/zmq.go @@ -5,7 +5,7 @@ import ( "context" "fmt" "git.gammaspectra.live/P2Pool/p2pool-observer/utils" - "golang.org/x/exp/slices" + "slices" "strings" "github.com/go-zeromq/zmq4" diff --git a/monero/randomx/randomx_cgo.go b/monero/randomx/randomx_cgo.go index 4d63b16..58bd67b 100644 --- a/monero/randomx/randomx_cgo.go +++ b/monero/randomx/randomx_cgo.go @@ -10,9 +10,9 @@ import ( "git.gammaspectra.live/P2Pool/p2pool-observer/monero/crypto" "git.gammaspectra.live/P2Pool/p2pool-observer/types" "git.gammaspectra.live/P2Pool/randomx-go-bindings" - "golang.org/x/exp/slices" "log" "runtime" + "slices" "sync" "unsafe" ) diff --git a/p2pool/cache/archive/archive.go b/p2pool/cache/archive/archive.go index 222f925..662f3ab 100644 --- a/p2pool/cache/archive/archive.go +++ b/p2pool/cache/archive/archive.go @@ -8,9 +8,9 @@ import ( "git.gammaspectra.live/P2Pool/p2pool-observer/p2pool/sidechain" "git.gammaspectra.live/P2Pool/p2pool-observer/types" bolt "go.etcd.io/bbolt" - "golang.org/x/exp/slices" "log" "math" + "slices" "time" "unsafe" ) diff --git a/p2pool/mainchain/mainchain.go b/p2pool/mainchain/mainchain.go index ec644c0..c4c9405 100644 --- a/p2pool/mainchain/mainchain.go +++ b/p2pool/mainchain/mainchain.go @@ -15,8 +15,8 @@ import ( "git.gammaspectra.live/P2Pool/p2pool-observer/types" "git.gammaspectra.live/P2Pool/p2pool-observer/utils" "github.com/dolthub/swiss" - "golang.org/x/exp/slices" "log" + "slices" "sync" "sync/atomic" "time" diff --git a/p2pool/mempool/mempool.go b/p2pool/mempool/mempool.go index 4d5baa7..5c06f39 100644 --- a/p2pool/mempool/mempool.go +++ b/p2pool/mempool/mempool.go @@ -3,10 +3,9 @@ package mempool import ( "git.gammaspectra.live/P2Pool/go-monero/pkg/rpc/daemon" "git.gammaspectra.live/P2Pool/p2pool-observer/types" - "git.gammaspectra.live/P2Pool/p2pool-observer/utils" - "golang.org/x/exp/slices" "log" "lukechampine.com/uint128" + "slices" ) type MempoolEntry struct { @@ -21,8 +20,8 @@ type Mempool []*MempoolEntry func (m Mempool) Sort() { // Sort all transactions by fee per byte (highest to lowest) - slices.SortFunc(m, func(a, b *MempoolEntry) bool { - return a.Compare(b) < 0 + slices.SortFunc(m, func(a, b *MempoolEntry) int { + return a.Compare(b) }) } @@ -72,7 +71,7 @@ func (m Mempool) Pick(baseReward, minerTxWeight, medianWeight uint64) Mempool { if finalWeight+tx.Weight > medianWeight { // Don't check more than 100 transactions deep because they have higher and higher fee/byte n := len(m2) - for j, j1 := n-1, utils.Max(0, n-100); j >= j1; j-- { + for j, j1 := n-1, max(0, n-100); j >= j1; j-- { prevTx := m2[j] reward2 := GetBlockReward(baseReward, medianWeight, finalFees+prevTx.Fee, finalWeight+prevTx.Weight) if reward2 > finalReward { diff --git a/p2pool/p2p/client.go b/p2pool/p2p/client.go index f526aa3..c222625 100644 --- a/p2pool/p2p/client.go +++ b/p2pool/p2p/client.go @@ -11,12 +11,12 @@ import ( p2pooltypes "git.gammaspectra.live/P2Pool/p2pool-observer/p2pool/types" "git.gammaspectra.live/P2Pool/p2pool-observer/types" "git.gammaspectra.live/P2Pool/p2pool-observer/utils" - "golang.org/x/exp/slices" "io" "log" unsafeRandom "math/rand" "net" "net/netip" + "slices" "sync" "sync/atomic" "time" @@ -583,7 +583,7 @@ func (c *Client) OnConnection() { } //Atomic max, not necessary as no external writers exist - topHeight := utils.Max(c.BroadcastMaxHeight.Load(), block.Side.Height) + topHeight := max(c.BroadcastMaxHeight.Load(), block.Side.Height) for { if oldHeight := c.BroadcastMaxHeight.Swap(topHeight); oldHeight <= topHeight { break @@ -659,7 +659,7 @@ func (c *Client) OnConnection() { entriesToSend := make([]netip.AddrPort, 0, PeerListResponseMaxPeers) // Send every 4th peer on average, selected at random - peersToSendTarget := utils.Min(PeerListResponseMaxPeers, utils.Max(len(connectedPeerList)/4, 1)) + peersToSendTarget := min(PeerListResponseMaxPeers, max(len(connectedPeerList)/4, 1)) n := 0 for _, peer := range connectedPeerList { if peer.AddressPort.Addr().IsLoopback() || peer.AddressPort.Addr().IsPrivate() || !peer.IsGood() || peer.AddressPort.Addr().Compare(c.AddressPort.Addr()) == 0 { @@ -741,7 +741,7 @@ func (c *Client) OnConnection() { c.Ban(DefaultBanTime, fmt.Errorf("too many peers on PEER_LIST_RESPONSE num_peers = %d", numPeers)) return } else { - firstPeerResponse := c.PingDuration.Swap(uint64(utils.Max(time.Now().Sub(time.UnixMicro(int64(c.LastPeerListRequestTimestamp.Load()))), 0))) == 0 + firstPeerResponse := c.PingDuration.Swap(uint64(max(time.Now().Sub(time.UnixMicro(int64(c.LastPeerListRequestTimestamp.Load()))), 0))) == 0 var rawIp [16]byte var port uint16 diff --git a/p2pool/p2p/server.go b/p2pool/p2p/server.go index 7e8a1e7..9763d85 100644 --- a/p2pool/p2p/server.go +++ b/p2pool/p2p/server.go @@ -12,11 +12,11 @@ import ( p2pooltypes "git.gammaspectra.live/P2Pool/p2pool-observer/p2pool/types" "git.gammaspectra.live/P2Pool/p2pool-observer/types" "git.gammaspectra.live/P2Pool/p2pool-observer/utils" - "golang.org/x/exp/slices" "log" unsafeRandom "math/rand" "net" "net/netip" + "slices" "sync" "sync/atomic" "time" @@ -126,8 +126,8 @@ func NewServer(p2pool P2PoolInterface, listenAddress string, externalListenPort listenAddress: addrPort, externalListenPort: externalListenPort, peerId: binary.LittleEndian.Uint64(peerId), - MaxOutgoingPeers: utils.Min(maxOutgoingPeers, 450), - MaxIncomingPeers: utils.Min(maxIncomingPeers, 450), + MaxOutgoingPeers: min(maxOutgoingPeers, 450), + MaxIncomingPeers: min(maxIncomingPeers, 450), cachedBlocks: make(map[types.Hash]*sidechain.PoolBlock, p2pool.Consensus().ChainWindowSize*3), versionInformation: p2pooltypes.PeerVersionInformation{ SoftwareId: p2pooltypes.CurrentSoftwareId, @@ -319,7 +319,7 @@ func (s *Server) UpdateClientConnections() { // - It's been at least 10 seconds since the last block request (peer is not syncing) // - Peer should have sent a broadcast by now - if lastUpdated > 0 && (currentTime >= utils.Max(lastUpdated, client.LastBlockRequestTimestamp.Load())+10) && (lastUpdated >= client.LastBroadcastTimestamp.Load()+900) { + if lastUpdated > 0 && (currentTime >= max(lastUpdated, client.LastBlockRequestTimestamp.Load())+10) && (lastUpdated >= client.LastBroadcastTimestamp.Load()+900) { dt := lastUpdated - client.LastBroadcastTimestamp.Load() client.Ban(DefaultBanTime, fmt.Errorf("not broadcasting blocks (last update %d seconds ago)", dt)) client.Close() @@ -421,8 +421,14 @@ func (s *Server) UpdateClientConnections() { s.moneroPeerList = append(s.moneroPeerList, e) } } - slices.SortFunc(s.moneroPeerList, func(a, b *PeerListEntry) bool { - return a.LastSeenTimestamp.Load() < b.LastSeenTimestamp.Load() + slices.SortFunc(s.moneroPeerList, func(a, b *PeerListEntry) int { + aValue, bValue := a.LastSeenTimestamp.Load(), b.LastSeenTimestamp.Load() + if aValue < bValue { + return -1 + } else if aValue > bValue { + return 1 + } + return 0 }) log.Printf("[P2PServer] monerod peer list loaded (%d peers)", len(s.moneroPeerList)) } diff --git a/p2pool/p2pool.go b/p2pool/p2pool.go index fdd160c..8b2dbd0 100644 --- a/p2pool/p2pool.go +++ b/p2pool/p2pool.go @@ -20,8 +20,8 @@ import ( p2pooltypes "git.gammaspectra.live/P2Pool/p2pool-observer/p2pool/types" "git.gammaspectra.live/P2Pool/p2pool-observer/types" "git.gammaspectra.live/P2Pool/p2pool-observer/utils" - "golang.org/x/exp/slices" "log" + "slices" "strconv" "sync" "sync/atomic" diff --git a/p2pool/sidechain/blobcache.go b/p2pool/sidechain/blobcache.go index 77601d8..9375ce2 100644 --- a/p2pool/sidechain/blobcache.go +++ b/p2pool/sidechain/blobcache.go @@ -5,8 +5,8 @@ import ( "encoding/binary" "git.gammaspectra.live/P2Pool/p2pool-observer/monero" "git.gammaspectra.live/P2Pool/p2pool-observer/types" - "golang.org/x/exp/slices" "log" + "slices" ) // BlockSaveEpochSize could be up to 256? diff --git a/p2pool/sidechain/consensus.go b/p2pool/sidechain/consensus.go index 72396a4..d81b662 100644 --- a/p2pool/sidechain/consensus.go +++ b/p2pool/sidechain/consensus.go @@ -188,7 +188,7 @@ func (c *Consensus) verify() bool { } func (c *Consensus) CalculateSideTemplateId(share *PoolBlock) (result types.Hash) { - return c.CalculateSideTemplateIdPreAllocated(share, make([]byte, 0, utils.Max(share.Main.BufferLength(), share.Side.BufferLength()))) + return c.CalculateSideTemplateIdPreAllocated(share, make([]byte, 0, max(share.Main.BufferLength(), share.Side.BufferLength()))) } func (c *Consensus) CalculateSideTemplateIdPreAllocated(share *PoolBlock, buf []byte) (result types.Hash) { diff --git a/p2pool/sidechain/poolblock.go b/p2pool/sidechain/poolblock.go index a22c900..9618b05 100644 --- a/p2pool/sidechain/poolblock.go +++ b/p2pool/sidechain/poolblock.go @@ -15,8 +15,8 @@ import ( p2poolcrypto "git.gammaspectra.live/P2Pool/p2pool-observer/p2pool/crypto" "git.gammaspectra.live/P2Pool/p2pool-observer/types" "git.gammaspectra.live/P2Pool/p2pool-observer/utils" - "golang.org/x/exp/slices" "io" + "slices" "sync/atomic" "unsafe" ) diff --git a/p2pool/sidechain/share.go b/p2pool/sidechain/share.go index 2ec74fb..2f136c5 100644 --- a/p2pool/sidechain/share.go +++ b/p2pool/sidechain/share.go @@ -3,8 +3,7 @@ package sidechain import ( "git.gammaspectra.live/P2Pool/p2pool-observer/monero/address" "git.gammaspectra.live/P2Pool/p2pool-observer/types" - "golang.org/x/exp/constraints" - "golang.org/x/exp/slices" + "slices" "sync" ) @@ -27,8 +26,8 @@ func (s Shares) Clone() (o Shares) { // Compact Removes dupe Share entries func (s Shares) Compact() Shares { // Sort shares based on address - slices.SortFunc(s, func(a *Share, b *Share) bool { - return a.Address.ComparePacked(b.Address) < 0 + slices.SortFunc(s, func(a *Share, b *Share) int { + return a.Address.ComparePacked(b.Address) }) index := 0 @@ -52,7 +51,7 @@ type PreAllocatedSharesPool struct { pool sync.Pool } -func NewPreAllocatedSharesPool[T constraints.Integer](n T) *PreAllocatedSharesPool { +func NewPreAllocatedSharesPool[T uint64 | int](n T) *PreAllocatedSharesPool { p := &PreAllocatedSharesPool{} p.pool.New = func() any { return PreAllocateShares(n) @@ -68,7 +67,7 @@ func (p *PreAllocatedSharesPool) Put(s Shares) { p.pool.Put(s) } -func PreAllocateShares[T constraints.Integer](n T) Shares { +func PreAllocateShares[T uint64 | int](n T) Shares { preAllocatedShares := make(Shares, n) for i := range preAllocatedShares { preAllocatedShares[i] = &Share{} diff --git a/p2pool/sidechain/sidechain.go b/p2pool/sidechain/sidechain.go index f2e3d62..c0a3a88 100644 --- a/p2pool/sidechain/sidechain.go +++ b/p2pool/sidechain/sidechain.go @@ -17,9 +17,9 @@ import ( "git.gammaspectra.live/P2Pool/p2pool-observer/utils" "git.gammaspectra.live/P2Pool/sha3" "github.com/dolthub/swiss" - "golang.org/x/exp/slices" "io" "log" + "slices" "sync" "sync/atomic" "time" @@ -86,6 +86,7 @@ type SideChain struct { preAllocatedSharesPool *PreAllocatedSharesPool preAllocatedDifficultyData []DifficultyData preAllocatedDifficultyDifferences []uint32 + preAllocatedMinedBlocks []types.Hash } func NewSideChain(server P2PoolInterface) *SideChain { @@ -99,6 +100,7 @@ func NewSideChain(server P2PoolInterface) *SideChain { preAllocatedDifficultyDifferences: make([]uint32, server.Consensus().ChainWindowSize*2), preAllocatedSharesPool: NewPreAllocatedSharesPool(server.Consensus().ChainWindowSize * 2), preAllocatedBuffer: make([]byte, 0, PoolBlockMaxTemplateSize), + preAllocatedMinedBlocks: make([]types.Hash, 0, 6*UncleBlockDepth*2+1), seenBlocks: swiss.NewMap[FullId, struct{}](uint32(server.Consensus().ChainWindowSize*2 + 300)), } minDiff := types.DifficultyFrom64(server.Consensus().MinimumDifficulty) @@ -163,7 +165,7 @@ func (c *SideChain) GetPossibleUncles(tip *PoolBlock, forHeight uint64) (uncles tmp := tip c.sidechainLock.RLock() defer c.sidechainLock.RUnlock() - for i, n := uint64(0), utils.Min(UncleBlockDepth, tip.Side.Height+1); tmp != nil && (i < n); i++ { + for i, n := uint64(0), min(UncleBlockDepth, tip.Side.Height+1); tmp != nil && (i < n); i++ { minedBlocks = append(minedBlocks, tmp.SideTemplateId(c.Consensus())) for _, uncleId := range tmp.Side.Uncles { minedBlocks = append(minedBlocks, uncleId) @@ -171,7 +173,7 @@ func (c *SideChain) GetPossibleUncles(tip *PoolBlock, forHeight uint64) (uncles tmp = c.getParent(tmp) } - for i, n := uint64(0), utils.Min(UncleBlockDepth, tip.Side.Height+1); i < n; i++ { + for i, n := uint64(0), min(UncleBlockDepth, tip.Side.Height+1); i < n; i++ { for _, uncle := range c.getPoolBlocksByHeight(tip.Side.Height - i) { // Only add verified and valid blocks if !uncle.Verified.Load() || uncle.Invalid.Load() { @@ -208,8 +210,8 @@ func (c *SideChain) GetPossibleUncles(tip *PoolBlock, forHeight uint64) (uncles if len(uncles) > 0 { // Sort hashes, consensus - slices.SortFunc(uncles, func(a, b types.Hash) bool { - return a.Compare(b) < 0 + slices.SortFunc(uncles, func(a, b types.Hash) int { + return a.Compare(b) }) } @@ -587,10 +589,10 @@ func (c *SideChain) verifyBlock(block *PoolBlock) (verification error, invalid e //check uncles - minedBlocks := make([]types.Hash, 0, len(block.Side.Uncles)*UncleBlockDepth*2+1) + minedBlocks := c.preAllocatedMinedBlocks[:0] { tmp := parent - n := utils.Min(UncleBlockDepth, block.Side.Height+1) + n := min(UncleBlockDepth, block.Side.Height+1) for i := uint64(0); tmp != nil && i < n; i++ { minedBlocks = append(minedBlocks, tmp.SideTemplateId(c.Consensus())) for _, uncleId := range tmp.Side.Uncles { @@ -750,12 +752,12 @@ func (c *SideChain) updateDepths(block *PoolBlock) { log.Printf("[SideChain] Block %s side height %d is inconsistent with parent's side_height %d", block.SideTemplateId(c.Consensus()), block.Side.Height, child.Side.Height) return } else { - block.Depth.Store(utils.Max(block.Depth.Load(), child.Depth.Load()+1)) + block.Depth.Store(max(block.Depth.Load(), child.Depth.Load()+1)) } } if ix := slices.Index(child.Side.Uncles, block.SideTemplateId(c.Consensus())); ix != 1 { - block.Depth.Store(utils.Max(block.Depth.Load(), child.Depth.Load()+1)) + block.Depth.Store(max(block.Depth.Load(), child.Depth.Load()+1)) } } } @@ -1040,7 +1042,7 @@ func (c *SideChain) GetPoolBlocksFromTip(id types.Hash) (chain, uncles UniquePoo } func (c *SideChain) GetPoolBlocksFromTipWithDepth(id types.Hash, depth uint64) (chain, uncles UniquePoolBlockSlice) { - chain = make([]*PoolBlock, 0, utils.Min(depth, c.Consensus().ChainWindowSize*2+monero.BlockTime/c.Consensus().TargetBlockTime)) + chain = make([]*PoolBlock, 0, min(depth, c.Consensus().ChainWindowSize*2+monero.BlockTime/c.Consensus().TargetBlockTime)) uncles = make([]*PoolBlock, 0, len(chain)/20) c.sidechainLock.RLock() diff --git a/p2pool/sidechain/utils.go b/p2pool/sidechain/utils.go index 6c32452..889d189 100644 --- a/p2pool/sidechain/utils.go +++ b/p2pool/sidechain/utils.go @@ -12,10 +12,10 @@ import ( "git.gammaspectra.live/P2Pool/p2pool-observer/types" "git.gammaspectra.live/P2Pool/p2pool-observer/utils" "git.gammaspectra.live/P2Pool/sha3" - "golang.org/x/exp/slices" "log" "math" "math/bits" + "slices" ) type GetByMainIdFunc func(h types.Hash) *PoolBlock @@ -365,7 +365,7 @@ func GetDifficulty(tip *PoolBlock, consensus *Consensus, getByTemplateId GetByTe var blockDepth uint64 var oldestTimestamp uint64 = math.MaxUint64 for { - oldestTimestamp = utils.Min(oldestTimestamp, cur.Main.Timestamp) + oldestTimestamp = min(oldestTimestamp, cur.Main.Timestamp) insertDifficultyData(cur.Side.CumulativeDifficulty, cur.Main.Timestamp) for _, uncleId := range cur.Side.Uncles { @@ -378,7 +378,7 @@ func GetDifficulty(tip *PoolBlock, consensus *Consensus, getByTemplateId GetByTe continue } - oldestTimestamp = utils.Min(oldestTimestamp, uncle.Main.Timestamp) + oldestTimestamp = min(oldestTimestamp, uncle.Main.Timestamp) insertDifficultyData(uncle.Side.CumulativeDifficulty, uncle.Main.Timestamp) } } @@ -562,7 +562,7 @@ func IsLongerChain(block, candidate *PoolBlock, consensus *Consensus, getByTempl if newChain != nil { if candidateMainchainMinHeight != 0 { - candidateMainchainMinHeight = utils.Min(candidateMainchainMinHeight, newChain.Main.Coinbase.GenHeight) + candidateMainchainMinHeight = min(candidateMainchainMinHeight, newChain.Main.Coinbase.GenHeight) } else { candidateMainchainMinHeight = newChain.Main.Coinbase.GenHeight } @@ -575,7 +575,7 @@ func IsLongerChain(block, candidate *PoolBlock, consensus *Consensus, getByTempl if !slices.Contains(candidateChainMoneroBlocks, newChain.Main.PreviousId) { if data := getChainMainByHash(newChain.Main.PreviousId); data != nil { candidateChainMoneroBlocks = append(candidateChainMoneroBlocks, newChain.Main.PreviousId) - candidateMainchainHeight = utils.Max(candidateMainchainHeight, data.Height) + candidateMainchainHeight = max(candidateMainchainHeight, data.Height) } } diff --git a/types/difficulty.go b/types/difficulty.go index 86abfb9..ea2ffd9 100644 --- a/types/difficulty.go +++ b/types/difficulty.go @@ -206,7 +206,6 @@ func (d Difficulty) MarshalJSON() ([]byte, error) { func MustDifficultyFromString(s string) Difficulty { if d, err := DifficultyFromString(s); err != nil { panic(err) - return ZeroDifficulty } else { return d } diff --git a/types/types.go b/types/types.go index 9a6ab40..781fee7 100644 --- a/types/types.go +++ b/types/types.go @@ -27,7 +27,6 @@ func (h Hash) MarshalJSON() ([]byte, error) { func MustHashFromString(s string) Hash { if h, err := HashFromString(s); err != nil { panic(err) - return ZeroHash } else { return h } diff --git a/utils/circular_buffer.go b/utils/circular_buffer.go index 86b7063..cdf2b56 100644 --- a/utils/circular_buffer.go +++ b/utils/circular_buffer.go @@ -1,7 +1,7 @@ package utils import ( - "golang.org/x/exp/slices" + "slices" "sync" "sync/atomic" ) diff --git a/utils/concurrency.go b/utils/concurrency.go index e1010ef..5b3a7a5 100644 --- a/utils/concurrency.go +++ b/utils/concurrency.go @@ -8,7 +8,7 @@ import ( func SplitWork(routines int, workSize uint64, do func(workIndex uint64, routineIndex int) error, init func(routines, routineIndex int) error, errorFunc func(routineIndex int, err error)) bool { if routines <= 0 { - routines = Max(runtime.NumCPU()-routines, 4) + routines = max(runtime.NumCPU()-routines, 4) } if workSize < uint64(routines) { @@ -18,15 +18,14 @@ func SplitWork(routines int, workSize uint64, do func(workIndex uint64, routineI var counter atomic.Uint64 var wg sync.WaitGroup - var allOk atomic.Bool - allOk.Store(true) + var failed atomic.Bool for routineIndex := 0; routineIndex < routines; routineIndex++ { if init != nil { if err := init(routines, routineIndex); err != nil { if errorFunc != nil { errorFunc(routineIndex, err) } - allOk.Store(false) + failed.Store(true) continue } } @@ -43,7 +42,7 @@ func SplitWork(routines int, workSize uint64, do func(workIndex uint64, routineI if err = do(workIndex-1, routineIndex); err != nil { if errorFunc != nil { errorFunc(routineIndex, err) - allOk.Store(false) + failed.Store(true) } return } @@ -52,5 +51,5 @@ func SplitWork(routines int, workSize uint64, do func(workIndex uint64, routineI } wg.Wait() - return allOk.Load() + return !failed.Load() } diff --git a/utils/limitreader.go b/utils/limitreader.go index da42c2e..cab31a3 100644 --- a/utils/limitreader.go +++ b/utils/limitreader.go @@ -35,7 +35,7 @@ func (l *LimitedByteReader) Read(p []byte) (n int, err error) { return } -func (l *LimitedByteReader) ReadByte() (v uint8, err error) { +func (l *LimitedByteReader) ReadByte() (v byte, err error) { if l.N <= 0 { return 0, io.EOF } diff --git a/utils/number.go b/utils/number.go index 599fd78..f6cf2b9 100644 --- a/utils/number.go +++ b/utils/number.go @@ -3,7 +3,6 @@ package utils import ( "encoding/hex" "github.com/jxskiss/base62" - "golang.org/x/exp/constraints" "math/bits" "strconv" "strings" @@ -65,26 +64,6 @@ func EncodeHexBinaryNumber(v2 string) string { return v1 } -func Min[T constraints.Ordered](v0 T, values ...T) (result T) { - result = v0 - for _, v := range values { - if v < result { - result = v - } - } - return -} - -func Max[T constraints.Ordered](v0 T, values ...T) (result T) { - result = v0 - for _, v := range values { - if v > result { - result = v - } - } - return -} - func PreviousPowerOfTwo(x uint64) int { if x == 0 { return 0 @@ -104,14 +83,14 @@ const ( VarIntLen9 ) -func UVarInt64SliceSize[T constraints.Integer](v []T) (n int) { +func UVarInt64SliceSize[T uint64 | int](v []T) (n int) { for i := range v { n += UVarInt64Size(v[i]) } return } -func UVarInt64Size[T constraints.Integer](v T) (n int) { +func UVarInt64Size[T uint64 | int](v T) (n int) { x := uint64(v) if x < VarIntLen1 { diff --git a/utils/slice.go b/utils/slice.go index 1bb7b02..5b14da2 100644 --- a/utils/slice.go +++ b/utils/slice.go @@ -1,9 +1,9 @@ package utils +import "slices" + func ReverseSlice[S ~[]E, E any](s S) S { - for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 { - s[i], s[j] = s[j], s[i] - } + slices.Reverse(s) return s }