Skip to content

Commit

Permalink
save
Browse files Browse the repository at this point in the history
  • Loading branch information
Giulio2002 committed Sep 22, 2023
1 parent 507d742 commit f91162c
Show file tree
Hide file tree
Showing 10 changed files with 42 additions and 25 deletions.
4 changes: 2 additions & 2 deletions core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ import (
"fmt"
"time"

cmp2 "github.com/ledgerwatch/erigon-lib/common/cmp"
"golang.org/x/crypto/sha3"
"golang.org/x/exp/slices"

"github.com/ledgerwatch/erigon-lib/chain"
libcommon "github.com/ledgerwatch/erigon-lib/common"
"github.com/ledgerwatch/erigon-lib/common/cmp"
"github.com/ledgerwatch/erigon-lib/common/fixedgas"

"github.com/ledgerwatch/erigon/common/math"
Expand Down Expand Up @@ -185,7 +185,7 @@ func ExecuteBlockEphemerally(

stateSyncReceipt := &types.Receipt{}
if chainConfig.Consensus == chain.BorConsensus && len(blockLogs) > 0 {
slices.SortStableFunc(blockLogs, func(i, j *types.Log) int { return cmp2.Compare(i.Index, j.Index) })
slices.SortStableFunc(blockLogs, func(i, j *types.Log) int { return cmp.Cmp(i.Index, j.Index) })
if len(blockLogs) > len(logs) {
stateSyncReceipt.Logs = blockLogs[len(logs):] // get state-sync logs from `state.Logs()`

Expand Down
7 changes: 7 additions & 0 deletions erigon-lib/common/cmp/cmp.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,10 @@ func Max[T constraints.Ordered](a, b T) T {
}
return b
}

func Cmp[T constraints.Ordered](a, b T) int {
if a <= b {
return -1
}
return 1
}
19 changes: 10 additions & 9 deletions erigon-lib/compress/compress.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (

"github.com/c2h5oh/datasize"
"github.com/ledgerwatch/erigon-lib/common"
"github.com/ledgerwatch/erigon-lib/common/cmp"
dir2 "github.com/ledgerwatch/erigon-lib/common/dir"
"github.com/ledgerwatch/erigon-lib/etl"
"github.com/ledgerwatch/log/v3"
Expand Down Expand Up @@ -300,11 +301,11 @@ func (db *DictionaryBuilder) Less(i, j int) bool {
return db.items[i].score < db.items[j].score
}

func dictionaryBuilderLess(i, j *Pattern) bool {
func dictionaryBuilderLess(i, j *Pattern) int {
if i.score == j.score {
return bytes.Compare(i.word, j.word) < 0
return bytes.Compare(i.word, j.word)
}
return i.score < j.score
return cmp.Cmp(i.score, j.score)
}

func (db *DictionaryBuilder) Swap(i, j int) {
Expand Down Expand Up @@ -383,11 +384,11 @@ type Pattern struct {
type PatternList []*Pattern

func (pl PatternList) Len() int { return len(pl) }
func patternListLess(i, j *Pattern) bool {
func patternListLess(i, j *Pattern) int {
if i.uses == j.uses {
return bits.Reverse64(i.code) < bits.Reverse64(j.code)
return cmp.Cmp(bits.Reverse64(i.code), bits.Reverse64(j.code))
}
return i.uses < j.uses
return cmp.Cmp(i.uses, j.uses)
}

// PatternHuff is an intermediate node in a huffman tree of patterns
Expand Down Expand Up @@ -555,11 +556,11 @@ type PositionList []*Position

func (pl PositionList) Len() int { return len(pl) }

func positionListLess(i, j *Position) bool {
func positionListLess(i, j *Position) int {
if i.uses == j.uses {
return bits.Reverse64(i.code) < bits.Reverse64(j.code)
return cmp.Cmp(bits.Reverse64(i.code), bits.Reverse64(j.code))
}
return i.uses < j.uses
return cmp.Cmp(i.uses, j.uses)
}

type PositionHeap []*PositionHuff
Expand Down
18 changes: 11 additions & 7 deletions erigon-lib/downloader/snaptype/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@ import (
"encoding/hex"
"errors"
"fmt"
"github.com/anacrolix/torrent/metainfo"
"os"
"path/filepath"
"strconv"
"strings"

"github.com/anacrolix/torrent/metainfo"

"github.com/ledgerwatch/erigon-lib/common/dir"
"golang.org/x/exp/slices"
)
Expand Down Expand Up @@ -209,20 +210,23 @@ func ParseDir(dir string) (res []FileInfo, err error) {
}
res = append(res, meta)
}
slices.SortFunc(res, func(i, j FileInfo) bool {
slices.SortFunc(res, func(i, j FileInfo) int {
if i.Version != j.Version {
return i.Version < j.Version
return int(i.Version - j.Version)
}
if i.From != j.From {
return i.From < j.From
return int(i.From - j.From)
}
if i.To != j.To {
return i.To < j.To
return int(i.To - j.To)
}
if i.T != j.T {
return i.T < j.T
return int(i.T - j.T)
}
if i.Ext < j.Ext {
return -1
}
return i.Ext < j.Ext
return 1
})

return res, nil
Expand Down
5 changes: 4 additions & 1 deletion erigon-lib/patricia/patricia.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"math/bits"
"strings"

"github.com/ledgerwatch/erigon-lib/common/cmp"
"github.com/ledgerwatch/erigon-lib/sais"
"golang.org/x/exp/slices"
)
Expand Down Expand Up @@ -699,7 +700,9 @@ func (mf2 *MatchFinder2) FindLongestMatches(data []byte) []Match {
return mf2.matches
}
//sort.Sort(&mf2.matches)
slices.SortFunc(mf2.matches, func(i, j Match) bool { return i.Start < j.Start })
slices.SortFunc(mf2.matches, func(i, j Match) bool {

Check failure on line 703 in erigon-lib/patricia/patricia.go

View workflow job for this annotation

GitHub Actions / tests-windows (windows-2022, 1.21)

type func(i Match, j Match) bool of func(i, j Match) bool {…} does not match inferred type func(a Match, b Match) int for func(a E, b E) int
return cmp.Cmp(i.Start, j.Start) < 0
})

lastEnd := mf2.matches[0].End
j := 1
Expand Down
6 changes: 4 additions & 2 deletions eth/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"net"
"os"
"path/filepath"
"sort"
"strconv"
"strings"
"sync"
Expand Down Expand Up @@ -55,7 +56,6 @@ import (

"github.com/holiman/uint256"
"github.com/ledgerwatch/log/v3"
"golang.org/x/exp/slices"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/protobuf/types/known/emptypb"
Expand Down Expand Up @@ -1092,7 +1092,9 @@ func (s *Ethereum) NodesInfo(limit int) (*remote.NodesInfoReply, error) {
}

nodesInfo := &remote.NodesInfoReply{NodesInfo: nodes}
slices.SortFunc(nodesInfo.NodesInfo, remote.NodeInfoReplyCmp)
sort.Slice(nodesInfo.NodesInfo, func(i, j int) bool {
return remote.NodeInfoReplyLess(nodesInfo.NodesInfo[i], nodesInfo.NodesInfo[j])
})

return nodesInfo, nil
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ go 1.19

require (
github.com/erigontech/mdbx-go v0.27.14
github.com/ledgerwatch/erigon-lib v0.0.0-20230920112310-93d9c9d9fe4b
github.com/ledgerwatch/erigon-snapshot v1.2.1-0.20230911054727-4e865b051314
github.com/ledgerwatch/log/v3 v3.9.0
github.com/ledgerwatch/secp256k1 v1.0.0
Expand Down Expand Up @@ -56,6 +55,7 @@ require (
github.com/jedib0t/go-pretty/v6 v6.4.6
github.com/json-iterator/go v1.1.12
github.com/julienschmidt/httprouter v1.3.0
github.com/ledgerwatch/erigon-lib v0.0.0-00010101000000-000000000000
github.com/libp2p/go-libp2p v0.31.0
github.com/libp2p/go-libp2p-pubsub v0.9.3
github.com/libp2p/go-mplex v0.7.0
Expand Down
2 changes: 1 addition & 1 deletion turbo/snapshotsync/freezeblocks/block_snapshots.go
Original file line number Diff line number Diff line change
Expand Up @@ -2146,7 +2146,7 @@ func (*Merger) FindMergeRanges(currentRanges []Range) (toMerge []Range) {
break
}
}
slices.SortFunc(toMerge, func(i, j Range) int { return cmp.Compare(i.from, j.from) })
slices.SortFunc(toMerge, func(i, j Range) int { return cmp.Cmp(i.from, j.from) })
return toMerge
}

Expand Down
2 changes: 1 addition & 1 deletion turbo/snapshotsync/freezeblocks/bor_snapshots.go
Original file line number Diff line number Diff line change
Expand Up @@ -1097,7 +1097,7 @@ func (*BorMerger) FindMergeRanges(currentRanges []Range) (toMerge []Range) {
break
}
}
slices.SortFunc(toMerge, func(i, j Range) int { return cmp.Compare(i.from, j.from) })
slices.SortFunc(toMerge, func(i, j Range) int { return cmp.Cmp(i.from, j.from) })
return toMerge
}

Expand Down
2 changes: 1 addition & 1 deletion turbo/snapshotsync/snapcfg/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func doSort(in preverified) Preverified {
for k, v := range in {
out = append(out, PreverifiedItem{k, v})
}
slices.SortFunc(out, func(i, j PreverifiedItem) int { return cmp.Compare(i.Name, j.Name) })
slices.SortFunc(out, func(i, j PreverifiedItem) int { return cmp.Cmp(i.Name, j.Name) })
return out
}

Expand Down

0 comments on commit f91162c

Please sign in to comment.