Skip to content

Commit b0aa39d

Browse files
authored
Revert "eth: use slices package for sorting (ethereum#27490)"
This reverts commit 53e07ce.
1 parent 2897f99 commit b0aa39d

File tree

6 files changed

+104
-61
lines changed

6 files changed

+104
-61
lines changed

common/types.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,6 @@ func BigToHash(b *big.Int) Hash { return BytesToHash(b.Bytes()) }
6565
// If b is larger than len(h), b will be cropped from the left.
6666
func HexToHash(s string) Hash { return BytesToHash(FromHex(s)) }
6767

68-
// Less compares two hashes.
69-
func (h Hash) Less(other Hash) bool {
70-
return bytes.Compare(h[:], other[:]) < 0
71-
}
72-
7368
// Bytes gets the byte representation of the underlying hash.
7469
func (h Hash) Bytes() []byte { return h[:] }
7570

eth/api_debug_test.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"fmt"
2222
"math/big"
2323
"reflect"
24+
"sort"
2425
"testing"
2526

2627
"github.com/davecgh/go-spew/spew"
@@ -30,7 +31,6 @@ import (
3031
"github.com/ethereum/go-ethereum/core/types"
3132
"github.com/ethereum/go-ethereum/crypto"
3233
"github.com/ethereum/go-ethereum/trie"
33-
"golang.org/x/exp/slices"
3434
)
3535

3636
var dumper = spew.ConfigState{Indent: " "}
@@ -58,6 +58,12 @@ func accountRangeTest(t *testing.T, trie *state.Trie, statedb *state.StateDB, st
5858
return result
5959
}
6060

61+
type resultHash []common.Hash
62+
63+
func (h resultHash) Len() int { return len(h) }
64+
func (h resultHash) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
65+
func (h resultHash) Less(i, j int) bool { return bytes.Compare(h[i].Bytes(), h[j].Bytes()) < 0 }
66+
6167
func TestAccountRange(t *testing.T) {
6268
t.Parallel()
6369

@@ -91,7 +97,7 @@ func TestAccountRange(t *testing.T) {
9197
firstResult := accountRangeTest(t, &trie, state, common.Hash{}, AccountRangeMaxResults, AccountRangeMaxResults)
9298
secondResult := accountRangeTest(t, &trie, state, common.BytesToHash(firstResult.Next), AccountRangeMaxResults, AccountRangeMaxResults)
9399

94-
hList := make([]common.Hash, 0)
100+
hList := make(resultHash, 0)
95101
for addr1 := range firstResult.Accounts {
96102
// If address is empty, then it makes no sense to compare
97103
// them as they might be two different accounts.
@@ -105,7 +111,7 @@ func TestAccountRange(t *testing.T) {
105111
}
106112
// Test to see if it's possible to recover from the middle of the previous
107113
// set and get an even split between the first and second sets.
108-
slices.SortFunc(hList, common.Hash.Less)
114+
sort.Sort(hList)
109115
middleH := hList[AccountRangeMaxResults/2]
110116
middleResult := accountRangeTest(t, &trie, state, middleH, AccountRangeMaxResults, AccountRangeMaxResults)
111117
missing, infirst, insecond := 0, 0, 0

eth/gasprice/feehistory.go

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@ import (
2323
"fmt"
2424
"math"
2525
"math/big"
26+
"sort"
2627
"sync/atomic"
2728

2829
"github.com/ethereum/go-ethereum/common"
2930
"github.com/ethereum/go-ethereum/consensus/misc"
3031
"github.com/ethereum/go-ethereum/core/types"
3132
"github.com/ethereum/go-ethereum/log"
3233
"github.com/ethereum/go-ethereum/rpc"
33-
"golang.org/x/exp/slices"
3434
)
3535

3636
var (
@@ -69,9 +69,20 @@ type processedFees struct {
6969
}
7070

7171
// txGasAndReward is sorted in ascending order based on reward
72-
type txGasAndReward struct {
73-
gasUsed uint64
74-
reward *big.Int
72+
type (
73+
txGasAndReward struct {
74+
gasUsed uint64
75+
reward *big.Int
76+
}
77+
sortGasAndReward []txGasAndReward
78+
)
79+
80+
func (s sortGasAndReward) Len() int { return len(s) }
81+
func (s sortGasAndReward) Swap(i, j int) {
82+
s[i], s[j] = s[j], s[i]
83+
}
84+
func (s sortGasAndReward) Less(i, j int) bool {
85+
return s[i].reward.Cmp(s[j].reward) < 0
7586
}
7687

7788
// processBlock takes a blockFees structure with the blockNumber, the header and optionally
@@ -106,14 +117,12 @@ func (oracle *Oracle) processBlock(bf *blockFees, percentiles []float64) {
106117
return
107118
}
108119

109-
sorter := make([]txGasAndReward, len(bf.block.Transactions()))
120+
sorter := make(sortGasAndReward, len(bf.block.Transactions()))
110121
for i, tx := range bf.block.Transactions() {
111122
reward, _ := tx.EffectiveGasTip(bf.block.BaseFee())
112123
sorter[i] = txGasAndReward{gasUsed: bf.receipts[i].GasUsed, reward: reward}
113124
}
114-
slices.SortStableFunc(sorter, func(a, b txGasAndReward) bool {
115-
return a.reward.Cmp(b.reward) < 0
116-
})
125+
sort.Stable(sorter)
117126

118127
var txIndex int
119128
sumGasUsed := sorter[0].gasUsed

eth/gasprice/gasprice.go

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package gasprice
1919
import (
2020
"context"
2121
"math/big"
22+
"sort"
2223
"sync"
2324

2425
"github.com/ethereum/go-ethereum/common"
@@ -29,7 +30,6 @@ import (
2930
"github.com/ethereum/go-ethereum/log"
3031
"github.com/ethereum/go-ethereum/params"
3132
"github.com/ethereum/go-ethereum/rpc"
32-
"golang.org/x/exp/slices"
3333
)
3434

3535
const sampleNumber = 3 // Number of transactions sampled in a block
@@ -208,7 +208,7 @@ func (oracle *Oracle) SuggestTipCap(ctx context.Context) (*big.Int, error) {
208208
}
209209
price := lastPrice
210210
if len(results) > 0 {
211-
slices.SortFunc(results, func(a, b *big.Int) bool { return a.Cmp(b) < 0 })
211+
sort.Sort(bigIntArray(results))
212212
price = results[(len(results)-1)*oracle.percentile/100]
213213
}
214214
if price.Cmp(oracle.maxPrice) > 0 {
@@ -227,6 +227,30 @@ type results struct {
227227
err error
228228
}
229229

230+
type txSorter struct {
231+
txs []*types.Transaction
232+
baseFee *big.Int
233+
}
234+
235+
func newSorter(txs []*types.Transaction, baseFee *big.Int) *txSorter {
236+
return &txSorter{
237+
txs: txs,
238+
baseFee: baseFee,
239+
}
240+
}
241+
242+
func (s *txSorter) Len() int { return len(s.txs) }
243+
func (s *txSorter) Swap(i, j int) {
244+
s.txs[i], s.txs[j] = s.txs[j], s.txs[i]
245+
}
246+
func (s *txSorter) Less(i, j int) bool {
247+
// It's okay to discard the error because a tx would never be
248+
// accepted into a block with an invalid effective tip.
249+
tip1, _ := s.txs[i].EffectiveGasTip(s.baseFee)
250+
tip2, _ := s.txs[j].EffectiveGasTip(s.baseFee)
251+
return tip1.Cmp(tip2) < 0
252+
}
253+
230254
// getBlockValues calculates the lowest transaction gas price in a given block
231255
// and sends it to the result channel. If the block is empty or all transactions
232256
// are sent by the miner itself(it doesn't make any sense to include this kind of
@@ -243,21 +267,14 @@ func (oracle *Oracle) getBlockValues(ctx context.Context, blockNum uint64, limit
243267
signer := types.MakeSigner(oracle.backend.ChainConfig(), block.Number(), block.Time())
244268

245269
// Sort the transaction by effective tip in ascending sort.
246-
txs := block.Transactions()
247-
sortedTxs := make([]*types.Transaction, len(txs))
248-
copy(sortedTxs, txs)
249-
baseFee := block.BaseFee()
250-
slices.SortFunc(sortedTxs, func(a, b *types.Transaction) bool {
251-
// It's okay to discard the error because a tx would never be
252-
// accepted into a block with an invalid effective tip.
253-
tip1, _ := a.EffectiveGasTip(baseFee)
254-
tip2, _ := b.EffectiveGasTip(baseFee)
255-
return tip1.Cmp(tip2) < 0
256-
})
270+
txs := make([]*types.Transaction, len(block.Transactions()))
271+
copy(txs, block.Transactions())
272+
sorter := newSorter(txs, block.BaseFee())
273+
sort.Sort(sorter)
257274

258275
var prices []*big.Int
259-
for _, tx := range sortedTxs {
260-
tip, _ := tx.EffectiveGasTip(baseFee)
276+
for _, tx := range sorter.txs {
277+
tip, _ := tx.EffectiveGasTip(block.BaseFee())
261278
if ignoreUnder != nil && tip.Cmp(ignoreUnder) == -1 {
262279
continue
263280
}
@@ -274,3 +291,9 @@ func (oracle *Oracle) getBlockValues(ctx context.Context, blockNum uint64, limit
274291
case <-quit:
275292
}
276293
}
294+
295+
type bigIntArray []*big.Int
296+
297+
func (s bigIntArray) Len() int { return len(s) }
298+
func (s bigIntArray) Less(i, j int) bool { return s[i].Cmp(s[j]) < 0 }
299+
func (s bigIntArray) Swap(i, j int) { s[i], s[j] = s[j], s[i] }

0 commit comments

Comments
 (0)