Skip to content

Commit

Permalink
Merge commit '8c288b528d3c9653c84321387e78687ed02134d7' into merge-v1…
Browse files Browse the repository at this point in the history
….12.1-pre

internal/ethapi/api.go: Upstream, the body of ethapi.DoCall was mostly
moved into ethapi.doCall, leaving DoCall as a wrapper to get the state
only once, fixing the issue described in
ethereum/go-ethereum#27505, and this conflicts
with our addition of the core.MessageRunMode parameter to DoCall.
Resolved by adding that parameter to the wrapper and the wrapped
functions.
  • Loading branch information
Tristan-Wilson committed Aug 3, 2023
2 parents dfa78ab + 8c288b5 commit 47a9c63
Show file tree
Hide file tree
Showing 43 changed files with 320 additions and 455 deletions.
10 changes: 10 additions & 0 deletions common/types.go
Expand Up @@ -65,6 +65,11 @@ func BigToHash(b *big.Int) Hash { return BytesToHash(b.Bytes()) }
// If b is larger than len(h), b will be cropped from the left.
func HexToHash(s string) Hash { return BytesToHash(FromHex(s)) }

// Less compares two hashes.
func (h Hash) Less(other Hash) bool {
return bytes.Compare(h[:], other[:]) < 0
}

// Bytes gets the byte representation of the underlying hash.
func (h Hash) Bytes() []byte { return h[:] }

Expand Down Expand Up @@ -226,6 +231,11 @@ func IsHexAddress(s string) bool {
return len(s) == 2*AddressLength && isHex(s)
}

// Less compares two addresses.
func (a Address) Less(other Address) bool {
return bytes.Compare(a[:], other[:]) < 0
}

// Bytes gets the string representation of the underlying address.
func (a Address) Bytes() []byte { return a[:] }

Expand Down
11 changes: 2 additions & 9 deletions consensus/clique/snapshot.go
Expand Up @@ -19,7 +19,6 @@ package clique
import (
"bytes"
"encoding/json"
"sort"
"time"

"github.com/ethereum/go-ethereum/common"
Expand All @@ -29,6 +28,7 @@ import (
"github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/params"
"golang.org/x/exp/slices"
)

// Vote represents a single vote that an authorized signer made to modify the
Expand Down Expand Up @@ -62,13 +62,6 @@ type Snapshot struct {
Tally map[common.Address]Tally `json:"tally"` // Current vote tally to avoid recalculating
}

// signersAscending implements the sort interface to allow sorting a list of addresses
type signersAscending []common.Address

func (s signersAscending) Len() int { return len(s) }
func (s signersAscending) Less(i, j int) bool { return bytes.Compare(s[i][:], s[j][:]) < 0 }
func (s signersAscending) Swap(i, j int) { s[i], s[j] = s[j], s[i] }

// newSnapshot creates a new snapshot with the specified startup parameters. This
// method does not initialize the set of recent signers, so only ever use if for
// the genesis block.
Expand Down Expand Up @@ -315,7 +308,7 @@ func (s *Snapshot) signers() []common.Address {
for sig := range s.Signers {
sigs = append(sigs, sig)
}
sort.Sort(signersAscending(sigs))
slices.SortFunc(sigs, common.Address.Less)
return sigs
}

Expand Down
4 changes: 2 additions & 2 deletions consensus/clique/snapshot_test.go
Expand Up @@ -21,7 +21,6 @@ import (
"crypto/ecdsa"
"fmt"
"math/big"
"sort"
"testing"

"github.com/ethereum/go-ethereum/common"
Expand All @@ -31,6 +30,7 @@ import (
"github.com/ethereum/go-ethereum/core/vm"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/params"
"golang.org/x/exp/slices"
)

// testerAccountPool is a pool to maintain currently active tester accounts,
Expand All @@ -53,7 +53,7 @@ func (ap *testerAccountPool) checkpoint(header *types.Header, signers []string)
for i, signer := range signers {
auths[i] = ap.address(signer)
}
sort.Sort(signersAscending(auths))
slices.SortFunc(auths, common.Address.Less)
for i, auth := range auths {
copy(header.Extra[extraVanity+i*common.AddressLength:], auth.Bytes())
}
Expand Down
6 changes: 3 additions & 3 deletions core/blockchain.go
Expand Up @@ -24,7 +24,6 @@ import (
"math"
"math/big"
"runtime"
"sort"
"strings"
"sync"
"sync/atomic"
Expand All @@ -49,6 +48,7 @@ import (
"github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/rlp"
"github.com/ethereum/go-ethereum/trie"
"golang.org/x/exp/slices"
)

var (
Expand Down Expand Up @@ -1096,8 +1096,8 @@ func (bc *BlockChain) procFutureBlocks() {
}
}
if len(blocks) > 0 {
sort.Slice(blocks, func(i, j int) bool {
return blocks[i].NumberU64() < blocks[j].NumberU64()
slices.SortFunc(blocks, func(a, b *types.Block) bool {
return a.NumberU64() < b.NumberU64()
})
// Insert one by one as chain insertion needs contiguous ancestry between blocks
for i := range blocks {
Expand Down
6 changes: 3 additions & 3 deletions core/forkid/forkid.go
Expand Up @@ -24,13 +24,13 @@ import (
"math"
"math/big"
"reflect"
"sort"
"strings"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/params"
"golang.org/x/exp/slices"
)

var (
Expand Down Expand Up @@ -270,8 +270,8 @@ func gatherForks(config *params.ChainConfig) ([]uint64, []uint64) {
}
}
}
sort.Slice(forksByBlock, func(i, j int) bool { return forksByBlock[i] < forksByBlock[j] })
sort.Slice(forksByTime, func(i, j int) bool { return forksByTime[i] < forksByTime[j] })
slices.Sort(forksByBlock)
slices.Sort(forksByTime)

// Deduplicate fork identifiers applying multiple forks
for i := 1; i < len(forksByBlock); i++ {
Expand Down
20 changes: 8 additions & 12 deletions core/mkalloc.go
Expand Up @@ -30,32 +30,28 @@ import (
"fmt"
"math/big"
"os"
"sort"
"strconv"

"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/rlp"
"golang.org/x/exp/slices"
)

type allocItem struct{ Addr, Balance *big.Int }

type allocList []allocItem

func (a allocList) Len() int { return len(a) }
func (a allocList) Less(i, j int) bool { return a[i].Addr.Cmp(a[j].Addr) < 0 }
func (a allocList) Swap(i, j int) { a[i], a[j] = a[j], a[i] }

func makelist(g *core.Genesis) allocList {
a := make(allocList, 0, len(g.Alloc))
func makelist(g *core.Genesis) []allocItem {
items := make([]allocItem, 0, len(g.Alloc))
for addr, account := range g.Alloc {
if len(account.Storage) > 0 || len(account.Code) > 0 || account.Nonce != 0 {
panic(fmt.Sprintf("can't encode account %x", addr))
}
bigAddr := new(big.Int).SetBytes(addr.Bytes())
a = append(a, allocItem{bigAddr, account.Balance})
items = append(items, allocItem{bigAddr, account.Balance})
}
sort.Sort(a)
return a
slices.SortFunc(items, func(a, b allocItem) bool {
return a.Addr.Cmp(b.Addr) < 0
})
return items
}

func makealloc(g *core.Genesis) string {
Expand Down
23 changes: 8 additions & 15 deletions core/rawdb/accessors_chain.go
Expand Up @@ -22,7 +22,6 @@ import (
"errors"
"fmt"
"math/big"
"sort"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
Expand All @@ -31,6 +30,7 @@ import (
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/rlp"
"golang.org/x/exp/slices"
)

// ReadCanonicalHash retrieves the hash assigned to a canonical block number.
Expand Down Expand Up @@ -860,23 +860,13 @@ type badBlock struct {
Body *types.Body
}

// badBlockList implements the sort interface to allow sorting a list of
// bad blocks by their number in the reverse order.
type badBlockList []*badBlock

func (s badBlockList) Len() int { return len(s) }
func (s badBlockList) Less(i, j int) bool {
return s[i].Header.Number.Uint64() < s[j].Header.Number.Uint64()
}
func (s badBlockList) Swap(i, j int) { s[i], s[j] = s[j], s[i] }

// ReadBadBlock retrieves the bad block with the corresponding block hash.
func ReadBadBlock(db ethdb.Reader, hash common.Hash) *types.Block {
blob, err := db.Get(badBlockKey)
if err != nil {
return nil
}
var badBlocks badBlockList
var badBlocks []*badBlock
if err := rlp.DecodeBytes(blob, &badBlocks); err != nil {
return nil
}
Expand All @@ -895,7 +885,7 @@ func ReadAllBadBlocks(db ethdb.Reader) []*types.Block {
if err != nil {
return nil
}
var badBlocks badBlockList
var badBlocks []*badBlock
if err := rlp.DecodeBytes(blob, &badBlocks); err != nil {
return nil
}
Expand All @@ -913,7 +903,7 @@ func WriteBadBlock(db ethdb.KeyValueStore, block *types.Block) {
if err != nil {
log.Warn("Failed to load old bad blocks", "error", err)
}
var badBlocks badBlockList
var badBlocks []*badBlock
if len(blob) > 0 {
if err := rlp.DecodeBytes(blob, &badBlocks); err != nil {
log.Crit("Failed to decode old bad blocks", "error", err)
Expand All @@ -929,7 +919,10 @@ func WriteBadBlock(db ethdb.KeyValueStore, block *types.Block) {
Header: block.Header(),
Body: block.Body(),
})
sort.Sort(sort.Reverse(badBlocks))
slices.SortFunc(badBlocks, func(a, b *badBlock) bool {
// Note: sorting in descending number order.
return a.Header.Number.Uint64() >= b.Header.Number.Uint64()
})
if len(badBlocks) > badBlockToKeep {
badBlocks = badBlocks[:badBlockToKeep]
}
Expand Down
8 changes: 5 additions & 3 deletions core/rawdb/chain_iterator_test.go
Expand Up @@ -19,12 +19,12 @@ package rawdb
import (
"math/big"
"reflect"
"sort"
"sync"
"testing"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"golang.org/x/exp/slices"
)

func TestChainIterator(t *testing.T) {
Expand Down Expand Up @@ -92,9 +92,11 @@ func TestChainIterator(t *testing.T) {
}
}
if !c.reverse {
sort.Ints(numbers)
slices.Sort(numbers)
} else {
sort.Sort(sort.Reverse(sort.IntSlice(numbers)))
slices.SortFunc(numbers, func(a, b int) bool {
return a > b // Sort descending
})
}
if !reflect.DeepEqual(numbers, c.expect) {
t.Fatalf("Case %d failed, visit element mismatch, want %v, got %v", i, c.expect, numbers)
Expand Down
2 changes: 1 addition & 1 deletion core/state/database.go
Expand Up @@ -123,7 +123,7 @@ type Trie interface {
// If the trie does not contain a value for key, the returned proof contains all
// nodes of the longest existing prefix of the key (at least the root), ending
// with the node that proves the absence of the key.
Prove(key []byte, fromLevel uint, proofDb ethdb.KeyValueWriter) error
Prove(key []byte, proofDb ethdb.KeyValueWriter) error
}

// NewDatabase creates a backing store for state. The returned database is safe for
Expand Down
6 changes: 3 additions & 3 deletions core/state/snapshot/difflayer.go
Expand Up @@ -21,7 +21,6 @@ import (
"fmt"
"math"
"math/rand"
"sort"
"sync"
"sync/atomic"
"time"
Expand All @@ -30,6 +29,7 @@ import (
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/rlp"
bloomfilter "github.com/holiman/bloomfilter/v2"
"golang.org/x/exp/slices"
)

var (
Expand Down Expand Up @@ -525,7 +525,7 @@ func (dl *diffLayer) AccountList() []common.Hash {
dl.accountList = append(dl.accountList, hash)
}
}
sort.Sort(hashes(dl.accountList))
slices.SortFunc(dl.accountList, common.Hash.Less)
dl.memory += uint64(len(dl.accountList) * common.HashLength)
return dl.accountList
}
Expand Down Expand Up @@ -563,7 +563,7 @@ func (dl *diffLayer) StorageList(accountHash common.Hash) ([]common.Hash, bool)
for k := range storageMap {
storageList = append(storageList, k)
}
sort.Sort(hashes(storageList))
slices.SortFunc(storageList, common.Hash.Less)
dl.storageList[accountHash] = storageList
dl.memory += uint64(len(dl.storageList)*common.HashLength + common.HashLength)
return storageList, destructed
Expand Down
4 changes: 2 additions & 2 deletions core/state/snapshot/generate.go
Expand Up @@ -256,7 +256,7 @@ func (dl *diskLayer) proveRange(ctx *generatorContext, trieId *trie.ID, prefix [
if origin == nil {
origin = common.Hash{}.Bytes()
}
if err := tr.Prove(origin, 0, proof); err != nil {
if err := tr.Prove(origin, proof); err != nil {
log.Debug("Failed to prove range", "kind", kind, "origin", origin, "err", err)
return &proofResult{
keys: keys,
Expand All @@ -267,7 +267,7 @@ func (dl *diskLayer) proveRange(ctx *generatorContext, trieId *trie.ID, prefix [
}, nil
}
if last != nil {
if err := tr.Prove(last, 0, proof); err != nil {
if err := tr.Prove(last, proof); err != nil {
log.Debug("Failed to prove range", "kind", kind, "last", last, "err", err)
return &proofResult{
keys: keys,
Expand Down

0 comments on commit 47a9c63

Please sign in to comment.