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

shuffle pending accounts in the transaction pool #501

Closed
wants to merge 3 commits into from
Closed
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
28 changes: 28 additions & 0 deletions common/random/shuffle.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package random

import (
"math/rand"
)

func Shuffle[T any](slice []T) {
//nolint:gosec
r := rand.New(CryptoSource{}) //CryptoSource is a cryptographic source of randomness

r.Shuffle(len(slice), func(i, j int) {
slice[i], slice[j] = slice[j], slice[i]
})
}

// ShuffleMap returns a shuffled slice of keys to iterate by
func ShuffleMap[K comparable, V any](m map[K]V) []K {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't deterministically random I guess?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as it uses crypto/rand, it isn't.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like a good explanation: https://stackoverflow.com/a/55925880

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeap, although it only depends on non cryptographically strong hash and a seed. The PR introduces cryptographically strong solution.

keys := make([]K, len(m))

i := 0

for key := range m {
keys[i] = key
i++
}

return keys
}
20 changes: 20 additions & 0 deletions common/random/source.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package random

import (
"crypto/rand"
"encoding/binary"
)

type CryptoSource struct{}

func (CryptoSource) Seed(_ int64) {}

func (s CryptoSource) Int63() int64 {
return int64(s.Uint64() & ^uint64(1<<63))
}

func (CryptoSource) Uint64() (v uint64) {
_ = binary.Read(rand.Reader, binary.BigEndian, &v)

return v
}

This file was deleted.

8 changes: 7 additions & 1 deletion core/tx_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/prque"
"github.com/ethereum/go-ethereum/common/random"
"github.com/ethereum/go-ethereum/consensus/misc"
"github.com/ethereum/go-ethereum/core/state"
"github.com/ethereum/go-ethereum/core/types"
Expand Down Expand Up @@ -538,7 +539,11 @@ func (pool *TxPool) Pending(enforceTips bool) map[common.Address]types.Transacti
defer pool.mu.Unlock()

pending := make(map[common.Address]types.Transactions)
for addr, list := range pool.pending {
addresses := random.ShuffleMap(pool.pending)

for _, addr := range addresses {
list := pool.pending[addr]

txs := list.Flatten()

// If the miner requests tip enforcement, cap the lists now
Expand All @@ -554,6 +559,7 @@ func (pool *TxPool) Pending(enforceTips bool) map[common.Address]types.Transacti
pending[addr] = txs
}
}

return pending
}

Expand Down