From 7bb3fb1481acbffd91afe19f802c29b1ae6ea60c Mon Sep 17 00:00:00 2001 From: cui <523516579@qq.com> Date: Wed, 3 Apr 2024 14:08:52 +0800 Subject: [PATCH 01/12] eth: simplify peer counting logic (#29420) --- eth/handler.go | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/eth/handler.go b/eth/handler.go index 0d27e061c4..c7c582af40 100644 --- a/eth/handler.go +++ b/eth/handler.go @@ -466,9 +466,7 @@ func (h *handler) BroadcastTransactions(txs types.Transactions) { largeTxs int // Number of large transactions to announce only directCount int // Number of transactions sent directly to peers (duplicates included) - directPeers int // Number of peers that were sent transactions directly annCount int // Number of transactions announced across all peers (duplicates included) - annPeers int // Number of peers announced about transactions txset = make(map[*ethPeer][]common.Hash) // Set peer->hash to transfer directly annos = make(map[*ethPeer][]common.Hash) // Set peer->hash to announce @@ -525,17 +523,15 @@ func (h *handler) BroadcastTransactions(txs types.Transactions) { } } for peer, hashes := range txset { - directPeers++ directCount += len(hashes) peer.AsyncSendTransactions(hashes) } for peer, hashes := range annos { - annPeers++ annCount += len(hashes) peer.AsyncSendPooledTransactionHashes(hashes) } log.Debug("Distributed transactions", "plaintxs", len(txs)-blobTxs-largeTxs, "blobtxs", blobTxs, "largetxs", largeTxs, - "bcastpeers", directPeers, "bcastcount", directCount, "annpeers", annPeers, "anncount", annCount) + "bcastpeers", len(txset), "bcastcount", directCount, "annpeers", len(annos), "anncount", annCount) } // txBroadcastLoop announces new transactions to connected peers. From 1f8f1377e62d2ca8aba04f0df7772ed665662bf7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marius=20Kj=C3=A6rstad?= Date: Thu, 4 Apr 2024 11:00:27 +0200 Subject: [PATCH 02/12] build: upgrade -dlgo version to Go 1.22.2 (#29448) --- build/checksums.txt | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/build/checksums.txt b/build/checksums.txt index f92f739a2f..27577285b8 100644 --- a/build/checksums.txt +++ b/build/checksums.txt @@ -5,22 +5,22 @@ # https://github.com/ethereum/execution-spec-tests/releases/download/v2.1.0/ ca89c76851b0900bfcc3cbb9a26cbece1f3d7c64a3bed38723e914713290df6c fixtures_develop.tar.gz -# version:golang 1.22.1 +# version:golang 1.22.2 # https://go.dev/dl/ -79c9b91d7f109515a25fc3ecdaad125d67e6bdb54f6d4d98580f46799caea321 go1.22.1.src.tar.gz -3bc971772f4712fec0364f4bc3de06af22a00a12daab10b6f717fdcd13156cc0 go1.22.1.darwin-amd64.tar.gz -f6a9cec6b8a002fcc9c0ee24ec04d67f430a52abc3cfd613836986bcc00d8383 go1.22.1.darwin-arm64.tar.gz -99f81c10d5a3f8a886faf8fa86aaa2aaf929fbed54a972ae5eec3c5e0bdb961a go1.22.1.freebsd-386.tar.gz -51c614ddd92ee4a9913a14c39bf80508d9cfba08561f24d2f075fd00f3cfb067 go1.22.1.freebsd-amd64.tar.gz -8484df36d3d40139eaf0fe5e647b006435d826cc12f9ae72973bf7ec265e0ae4 go1.22.1.linux-386.tar.gz -aab8e15785c997ae20f9c88422ee35d962c4562212bb0f879d052a35c8307c7f go1.22.1.linux-amd64.tar.gz -e56685a245b6a0c592fc4a55f0b7803af5b3f827aaa29feab1f40e491acf35b8 go1.22.1.linux-arm64.tar.gz -8cb7a90e48c20daed39a6ac8b8a40760030ba5e93c12274c42191d868687c281 go1.22.1.linux-armv6l.tar.gz -ac775e19d93cc1668999b77cfe8c8964abfbc658718feccfe6e0eb87663cd668 go1.22.1.linux-ppc64le.tar.gz -7bb7dd8e10f95c9a4cc4f6bef44c816a6e7c9e03f56ac6af6efbb082b19b379f go1.22.1.linux-s390x.tar.gz -0c5ebb7eb39b7884ec99f92b425d4c03a96a72443562aafbf6e7d15c42a3108a go1.22.1.windows-386.zip -cf9c66a208a106402a527f5b956269ca506cfe535fc388e828d249ea88ed28ba go1.22.1.windows-amd64.zip -85b8511b298c9f4199ecae26afafcc3d46155bac934d43f2357b9224bcaa310f go1.22.1.windows-arm64.zip +374ea82b289ec738e968267cac59c7d5ff180f9492250254784b2044e90df5a9 go1.22.2.src.tar.gz +33e7f63077b1c5bce4f1ecadd4d990cf229667c40bfb00686990c950911b7ab7 go1.22.2.darwin-amd64.tar.gz +660298be38648723e783ba0398e90431de1cb288c637880cdb124f39bd977f0d go1.22.2.darwin-arm64.tar.gz +efc7162b0cad2f918ac566a923d4701feb29dc9c0ab625157d49b1cbcbba39da go1.22.2.freebsd-386.tar.gz +d753428296e6709527e291fd204700a587ffef2c0a472b21aebea11618245929 go1.22.2.freebsd-amd64.tar.gz +586d9eb7fe0489ab297ad80dd06414997df487c5cf536c490ffeaa8d8f1807a7 go1.22.2.linux-386.tar.gz +5901c52b7a78002aeff14a21f93e0f064f74ce1360fce51c6ee68cd471216a17 go1.22.2.linux-amd64.tar.gz +36e720b2d564980c162a48c7e97da2e407dfcc4239e1e58d98082dfa2486a0c1 go1.22.2.linux-arm64.tar.gz +9243dfafde06e1efe24d59df6701818e6786b4adfdf1191098050d6d023c5369 go1.22.2.linux-armv6l.tar.gz +251a8886c5113be6490bdbb955ddee98763b49c9b1bf4c8364c02d3b482dab00 go1.22.2.linux-ppc64le.tar.gz +2b39019481c28c560d65e9811a478ae10e3ef765e0f59af362031d386a71bfef go1.22.2.linux-s390x.tar.gz +651753c06df037020ef4d162c5b273452e9ba976ed17ae39e66ef7ee89d8147e go1.22.2.windows-386.zip +8e581cf330f49d3266e936521a2d8263679ef7e2fc2cbbceb85659122d883596 go1.22.2.windows-amd64.zip +ddfca5beb9a0c62254266c3090c2555d899bf3e7aa26243e7de3621108f06875 go1.22.2.windows-arm64.zip # version:golangci 1.55.2 # https://github.com/golangci/golangci-lint/releases/ From 6b39e9236c278d9c4722505bb88a769fd21ca4b8 Mon Sep 17 00:00:00 2001 From: cui <523516579@qq.com> Date: Thu, 4 Apr 2024 17:58:44 +0800 Subject: [PATCH 03/12] beacon/engine: using slices.Contains (#29396) --- beacon/engine/types.go | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/beacon/engine/types.go b/beacon/engine/types.go index 8281fd794c..fc77c13af7 100644 --- a/beacon/engine/types.go +++ b/beacon/engine/types.go @@ -19,6 +19,7 @@ package engine import ( "fmt" "math/big" + "slices" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common/hexutil" @@ -132,12 +133,7 @@ func (b PayloadID) Version() PayloadVersion { // Is returns whether the identifier matches any of provided payload versions. func (b PayloadID) Is(versions ...PayloadVersion) bool { - for _, v := range versions { - if v == b.Version() { - return true - } - } - return false + return slices.Contains(versions, b.Version()) } func (b PayloadID) String() string { From eea0acc54959df779189dbfc972578ae56ac4d33 Mon Sep 17 00:00:00 2001 From: cui <523516579@qq.com> Date: Thu, 4 Apr 2024 17:59:54 +0800 Subject: [PATCH 04/12] log: using maps.Clone (#29392) --- log/handler_glog.go | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/log/handler_glog.go b/log/handler_glog.go index 608d955572..625a036403 100644 --- a/log/handler_glog.go +++ b/log/handler_glog.go @@ -21,6 +21,7 @@ import ( "errors" "fmt" "log/slog" + "maps" "regexp" "runtime" "strconv" @@ -145,10 +146,7 @@ func (h *GlogHandler) Enabled(ctx context.Context, lvl slog.Level) bool { func (h *GlogHandler) WithAttrs(attrs []slog.Attr) slog.Handler { h.lock.RLock() - siteCache := make(map[uintptr]slog.Level) - for k, v := range h.siteCache { - siteCache[k] = v - } + siteCache := maps.Clone(h.siteCache) h.lock.RUnlock() patterns := []pattern{} From 2e0c5e05ba355a722eb6eb9bc338de4949eee20d Mon Sep 17 00:00:00 2001 From: cui <523516579@qq.com> Date: Thu, 4 Apr 2024 18:19:48 +0800 Subject: [PATCH 05/12] p2p/dnsdisc: using clear builtin func (#29418) Co-authored-by: Felix Lange --- p2p/dnsdisc/client_test.go | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/p2p/dnsdisc/client_test.go b/p2p/dnsdisc/client_test.go index 77bfd8c131..01912e1eab 100644 --- a/p2p/dnsdisc/client_test.go +++ b/p2p/dnsdisc/client_test.go @@ -215,7 +215,7 @@ func TestIteratorNodeUpdates(t *testing.T) { // Ensure RandomNode returns the new nodes after the tree is updated. updateSomeNodes(keys, nodes) tree2, _ := makeTestTree("n", nodes, nil) - resolver.clear() + clear(resolver) resolver.add(tree2.ToTXT("n")) t.Log("tree updated") @@ -256,7 +256,7 @@ func TestIteratorRootRecheckOnFail(t *testing.T) { // Ensure RandomNode returns the new nodes after the tree is updated. updateSomeNodes(keys, nodes) tree2, _ := makeTestTree("n", nodes, nil) - resolver.clear() + clear(resolver) resolver.add(tree2.ToTXT("n")) t.Log("tree updated") @@ -447,12 +447,6 @@ func newMapResolver(maps ...map[string]string) mapResolver { return mr } -func (mr mapResolver) clear() { - for k := range mr { - delete(mr, k) - } -} - func (mr mapResolver) add(m map[string]string) { maps.Copy(mr, m) } From 8bd03341689c992d633f3988b3a7fbc15aec75e6 Mon Sep 17 00:00:00 2001 From: guangwu Date: Thu, 4 Apr 2024 18:20:54 +0800 Subject: [PATCH 06/12] crypto/signify: close tmp key file in test (#29444) --- crypto/signify/signify_fuzz.go | 1 + 1 file changed, 1 insertion(+) diff --git a/crypto/signify/signify_fuzz.go b/crypto/signify/signify_fuzz.go index 457af044d1..239a2134df 100644 --- a/crypto/signify/signify_fuzz.go +++ b/crypto/signify/signify_fuzz.go @@ -134,6 +134,7 @@ func createKeyPair() (string, string) { defer os.Remove(tmpKey.Name()) defer os.Remove(tmpKey.Name() + ".pub") defer os.Remove(tmpKey.Name() + ".sec") + defer tmpKey.Close() cmd := exec.Command("signify", "-G", "-n", "-p", tmpKey.Name()+".pub", "-s", tmpKey.Name()+".sec") if output, err := cmd.CombinedOutput(); err != nil { panic(fmt.Sprintf("could not verify the file: %v, output: \n%s", err, output)) From 9dfe728909bc7ff0709c69d3f090804d2516652c Mon Sep 17 00:00:00 2001 From: cui <523516579@qq.com> Date: Thu, 4 Apr 2024 18:24:49 +0800 Subject: [PATCH 07/12] p2p/discover: using slices.Contains (#29395) --- p2p/discover/v4_lookup_test.go | 2 +- p2p/discover/v5_udp.go | 12 ++---------- 2 files changed, 3 insertions(+), 11 deletions(-) diff --git a/p2p/discover/v4_lookup_test.go b/p2p/discover/v4_lookup_test.go index 691ce4be3f..5682f262be 100644 --- a/p2p/discover/v4_lookup_test.go +++ b/p2p/discover/v4_lookup_test.go @@ -285,7 +285,7 @@ func (tn *preminedTestnet) neighborsAtDistances(base *enode.Node, distances []ui for i := range lookupTestnet.dists[d] { n := lookupTestnet.node(d, i) d := enode.LogDist(base.ID(), n.ID()) - if containsUint(uint(d), distances) { + if slices.Contains(distances, uint(d)) { result = append(result, n) if len(result) >= elems { return result diff --git a/p2p/discover/v5_udp.go b/p2p/discover/v5_udp.go index 71f8d8dd08..20a8bccd05 100644 --- a/p2p/discover/v5_udp.go +++ b/p2p/discover/v5_udp.go @@ -25,6 +25,7 @@ import ( "fmt" "io" "net" + "slices" "sync" "time" @@ -437,7 +438,7 @@ func (t *UDPv5) verifyResponseNode(c *callV5, r *enr.Record, distances []uint, s } if distances != nil { nd := enode.LogDist(c.id, node.ID()) - if !containsUint(uint(nd), distances) { + if !slices.Contains(distances, uint(nd)) { return nil, errors.New("does not match any requested distance") } } @@ -448,15 +449,6 @@ func (t *UDPv5) verifyResponseNode(c *callV5, r *enr.Record, distances []uint, s return node, nil } -func containsUint(x uint, xs []uint) bool { - for _, v := range xs { - if x == v { - return true - } - } - return false -} - // callToNode sends the given call and sets up a handler for response packets (of message // type responseType). Responses are dispatched to the call's response channel. func (t *UDPv5) callToNode(n *enode.Node, responseType byte, req v5wire.Packet) *callV5 { From 9cb8de87037be7c38343b2f84c534887e7525c5d Mon Sep 17 00:00:00 2001 From: lightclient <14004106+lightclient@users.noreply.github.com> Date: Thu, 4 Apr 2024 06:26:10 -0400 Subject: [PATCH 08/12] internal/debug: convert legacy log level value in debug_verbosity (#29356) --- internal/debug/api.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/internal/debug/api.go b/internal/debug/api.go index c262201e3b..5e93585bf8 100644 --- a/internal/debug/api.go +++ b/internal/debug/api.go @@ -24,7 +24,6 @@ import ( "bytes" "errors" "io" - "log/slog" "os" "os/user" "path/filepath" @@ -57,7 +56,7 @@ type HandlerT struct { // Verbosity sets the log verbosity ceiling. The verbosity of individual packages // and source files can be raised using Vmodule. func (*HandlerT) Verbosity(level int) { - glogger.Verbosity(slog.Level(level)) + glogger.Verbosity(log.FromLegacyLevel(level)) } // Vmodule sets the log verbosity pattern. See package log for details on the From a851e39cbecf116ef2dc64f0b37b0300dc762931 Mon Sep 17 00:00:00 2001 From: lmittmann <3458786+lmittmann@users.noreply.github.com> Date: Thu, 4 Apr 2024 15:50:31 +0200 Subject: [PATCH 09/12] core/types: use new atomic types in caches (#29411) * use generic atomic types in tx caches * use generic atomic types in block caches * eth/catalyst: avoid copying tx in test --------- Co-authored-by: lmittmann Co-authored-by: Felix Lange --- core/types/block.go | 16 ++++++++-------- core/types/transaction.go | 14 +++++++------- core/types/transaction_signing.go | 5 ++--- eth/catalyst/simulated_beacon_test.go | 4 ++-- 4 files changed, 19 insertions(+), 20 deletions(-) diff --git a/core/types/block.go b/core/types/block.go index 1a357baa3a..53054f52d3 100644 --- a/core/types/block.go +++ b/core/types/block.go @@ -197,8 +197,8 @@ type Block struct { withdrawals Withdrawals // caches - hash atomic.Value - size atomic.Value + hash atomic.Pointer[common.Hash] + size atomic.Uint64 // These fields are used by package eth to track // inter-peer block relay. @@ -406,8 +406,8 @@ func (b *Block) BlobGasUsed() *uint64 { // Size returns the true RLP encoded storage size of the block, either by encoding // and returning it, or returning a previously cached value. func (b *Block) Size() uint64 { - if size := b.size.Load(); size != nil { - return size.(uint64) + if size := b.size.Load(); size > 0 { + return size } c := writeCounter(0) rlp.Encode(&c, b) @@ -486,11 +486,11 @@ func (b *Block) WithWithdrawals(withdrawals []*Withdrawal) *Block { // The hash is computed on the first call and cached thereafter. func (b *Block) Hash() common.Hash { if hash := b.hash.Load(); hash != nil { - return hash.(common.Hash) + return *hash } - v := b.header.Hash() - b.hash.Store(v) - return v + h := b.header.Hash() + b.hash.Store(&h) + return h } type Blocks []*Block diff --git a/core/types/transaction.go b/core/types/transaction.go index 7d2e9d5325..d6de9ae73e 100644 --- a/core/types/transaction.go +++ b/core/types/transaction.go @@ -57,9 +57,9 @@ type Transaction struct { time time.Time // Time first seen locally (spam avoidance) // caches - hash atomic.Value - size atomic.Value - from atomic.Value + hash atomic.Pointer[common.Hash] + size atomic.Uint64 + from atomic.Pointer[sigCache] } // NewTx creates a new transaction. @@ -462,7 +462,7 @@ func (tx *Transaction) Time() time.Time { // Hash returns the transaction hash. func (tx *Transaction) Hash() common.Hash { if hash := tx.hash.Load(); hash != nil { - return hash.(common.Hash) + return *hash } var h common.Hash @@ -471,15 +471,15 @@ func (tx *Transaction) Hash() common.Hash { } else { h = prefixedRlpHash(tx.Type(), tx.inner) } - tx.hash.Store(h) + tx.hash.Store(&h) return h } // Size returns the true encoded storage size of the transaction, either by encoding // and returning it, or returning a previously cached value. func (tx *Transaction) Size() uint64 { - if size := tx.size.Load(); size != nil { - return size.(uint64) + if size := tx.size.Load(); size > 0 { + return size } // Cache miss, encode and cache. diff --git a/core/types/transaction_signing.go b/core/types/transaction_signing.go index 70dee0776e..6e5f6712f8 100644 --- a/core/types/transaction_signing.go +++ b/core/types/transaction_signing.go @@ -128,8 +128,7 @@ func MustSignNewTx(prv *ecdsa.PrivateKey, s Signer, txdata TxData) *Transaction // signing method. The cache is invalidated if the cached signer does // not match the signer used in the current call. func Sender(signer Signer, tx *Transaction) (common.Address, error) { - if sc := tx.from.Load(); sc != nil { - sigCache := sc.(sigCache) + if sigCache := tx.from.Load(); sigCache != nil { // If the signer used to derive from in a previous // call is not the same as used current, invalidate // the cache. @@ -142,7 +141,7 @@ func Sender(signer Signer, tx *Transaction) (common.Address, error) { if err != nil { return common.Address{}, err } - tx.from.Store(sigCache{signer: signer, from: addr}) + tx.from.Store(&sigCache{signer: signer, from: addr}) return addr, nil } diff --git a/eth/catalyst/simulated_beacon_test.go b/eth/catalyst/simulated_beacon_test.go index df682b49d9..bb10938c35 100644 --- a/eth/catalyst/simulated_beacon_test.go +++ b/eth/catalyst/simulated_beacon_test.go @@ -74,7 +74,7 @@ func startSimulatedBeaconEthService(t *testing.T, genesis *core.Genesis) (*node. // send enough transactions to fill multiple blocks func TestSimulatedBeaconSendWithdrawals(t *testing.T) { var withdrawals []types.Withdrawal - txs := make(map[common.Hash]types.Transaction) + txs := make(map[common.Hash]*types.Transaction) var ( // testKey is a private key to use for funding a tester account. @@ -110,7 +110,7 @@ func TestSimulatedBeaconSendWithdrawals(t *testing.T) { if err != nil { t.Fatalf("error signing transaction, err=%v", err) } - txs[tx.Hash()] = *tx + txs[tx.Hash()] = tx if err := ethService.APIBackend.SendTx(context.Background(), tx); err != nil { t.Fatal("SendTx failed", err) From e3bdd84e9881041e6004ebc3e78c1211d58ebe83 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C3=A9ter=20Szil=C3=A1gyi?= Date: Thu, 4 Apr 2024 16:51:10 +0300 Subject: [PATCH 10/12] core/txpool: repair the limbo Billy too on unclean shutdowns (#29451) --- core/txpool/blobpool/limbo.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/txpool/blobpool/limbo.go b/core/txpool/blobpool/limbo.go index ec754f6894..32381a3936 100644 --- a/core/txpool/blobpool/limbo.go +++ b/core/txpool/blobpool/limbo.go @@ -60,7 +60,7 @@ func newLimbo(datadir string) (*limbo, error) { fails = append(fails, id) } } - store, err := billy.Open(billy.Options{Path: datadir}, newSlotter(), index) + store, err := billy.Open(billy.Options{Path: datadir, Repair: true}, newSlotter(), index) if err != nil { return nil, err } From 15ff066a24964ea16742420abecc7e4ae5e9bce0 Mon Sep 17 00:00:00 2001 From: Aaron Chen Date: Thu, 4 Apr 2024 21:52:38 +0800 Subject: [PATCH 11/12] trie/utils: change Div+Mod to DivMod (#29413) * trie/utils: change Div+Mod to DivMod * trie/utils: gofmt --- trie/utils/verkle.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/trie/utils/verkle.go b/trie/utils/verkle.go index ce059edc64..52e41f5243 100644 --- a/trie/utils/verkle.go +++ b/trie/utils/verkle.go @@ -206,9 +206,8 @@ func CodeSizeKey(address []byte) []byte { func codeChunkIndex(chunk *uint256.Int) (*uint256.Int, byte) { var ( - chunkOffset = new(uint256.Int).Add(codeOffset, chunk) - treeIndex = new(uint256.Int).Div(chunkOffset, verkleNodeWidth) - subIndexMod = new(uint256.Int).Mod(chunkOffset, verkleNodeWidth) + chunkOffset = new(uint256.Int).Add(codeOffset, chunk) + treeIndex, subIndexMod = new(uint256.Int).DivMod(chunkOffset, verkleNodeWidth, new(uint256.Int)) ) var subIndex byte if len(subIndexMod) != 0 { From 35fcf9c52b806d2a7eba0da4f65c97975200a2b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felf=C3=B6ldi=20Zsolt?= Date: Thu, 4 Apr 2024 16:30:27 +0200 Subject: [PATCH 12/12] beacon/types: enforce fork order based on known forks list (#29380) Co-authored-by: Felix Lange --- beacon/types/config.go | 36 +++++++++++++++++++++++++++--------- 1 file changed, 27 insertions(+), 9 deletions(-) diff --git a/beacon/types/config.go b/beacon/types/config.go index a52da5212e..7706e85f6c 100644 --- a/beacon/types/config.go +++ b/beacon/types/config.go @@ -19,7 +19,9 @@ package types import ( "crypto/sha256" "fmt" + "math" "os" + "slices" "sort" "strconv" "strings" @@ -27,6 +29,7 @@ import ( "github.com/ethereum/go-ethereum/beacon/merkle" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common/hexutil" + "github.com/ethereum/go-ethereum/log" "gopkg.in/yaml.v3" ) @@ -34,6 +37,8 @@ import ( // across signing different data structures. const syncCommitteeDomain = 7 +var knownForks = []string{"GENESIS", "ALTAIR", "BELLATRIX", "CAPELLA", "DENEB"} + // Fork describes a single beacon chain fork and also stores the calculated // signature domain used after this fork. type Fork struct { @@ -46,6 +51,9 @@ type Fork struct { // Fork version, see https://github.com/ethereum/consensus-specs/blob/dev/specs/phase0/beacon-chain.md#custom-types Version []byte + // index in list of known forks or MaxInt if unknown + knownIndex int + // calculated by computeDomain, based on fork version and genesis validators root domain merkle.Value } @@ -99,9 +107,14 @@ func (f Forks) SigningRoot(header Header) (common.Hash, error) { return signingRoot, nil } -func (f Forks) Len() int { return len(f) } -func (f Forks) Swap(i, j int) { f[i], f[j] = f[j], f[i] } -func (f Forks) Less(i, j int) bool { return f[i].Epoch < f[j].Epoch } +func (f Forks) Len() int { return len(f) } +func (f Forks) Swap(i, j int) { f[i], f[j] = f[j], f[i] } +func (f Forks) Less(i, j int) bool { + if f[i].Epoch != f[j].Epoch { + return f[i].Epoch < f[j].Epoch + } + return f[i].knownIndex < f[j].knownIndex +} // ChainConfig contains the beacon chain configuration. type ChainConfig struct { @@ -122,16 +135,22 @@ func (c *ChainConfig) ForkAtEpoch(epoch uint64) Fork { // AddFork adds a new item to the list of forks. func (c *ChainConfig) AddFork(name string, epoch uint64, version []byte) *ChainConfig { + knownIndex := slices.Index(knownForks, name) + if knownIndex == -1 { + knownIndex = math.MaxInt // assume that the unknown fork happens after the known ones + if epoch != math.MaxUint64 { + log.Warn("Unknown fork in config.yaml", "fork name", name, "known forks", knownForks) + } + } fork := &Fork{ - Name: name, - Epoch: epoch, - Version: version, + Name: name, + Epoch: epoch, + Version: version, + knownIndex: knownIndex, } fork.computeDomain(c.GenesisValidatorsRoot) - c.Forks = append(c.Forks, fork) sort.Sort(c.Forks) - return c } @@ -181,6 +200,5 @@ func (c *ChainConfig) LoadForks(path string) error { for name := range versions { return fmt.Errorf("epoch number missing for fork %q in beacon chain config file", name) } - sort.Sort(c.Forks) return nil }