diff --git a/core/forkid/forkid.go b/core/forkid/forkid.go
new file mode 100644
index 000000000000..3be2b4249a0b
--- /dev/null
+++ b/core/forkid/forkid.go
@@ -0,0 +1,238 @@
+// Copyright 2019 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see .
+
+// Package forkid implements EIP-2124 (https://eips.ethereum.org/EIPS/eip-2124).
+package forkid
+
+import (
+ "encoding/binary"
+ "errors"
+ "fmt"
+ "hash/crc32"
+ "math"
+ "math/big"
+ "reflect"
+ "strings"
+
+ "github.com/ethereum/go-ethereum/common"
+ "github.com/ethereum/go-ethereum/core"
+ "github.com/ethereum/go-ethereum/log"
+ "github.com/ethereum/go-ethereum/params"
+)
+
+var (
+ // ErrRemoteStale is returned by the validator if a remote fork checksum is a
+ // subset of our already applied forks, but the announced next fork block is
+ // not on our already passed chain.
+ ErrRemoteStale = errors.New("remote needs update")
+
+ // ErrLocalIncompatibleOrStale is returned by the validator if a remote fork
+ // checksum does not match any local checksum variation, signalling that the
+ // two chains have diverged in the past at some point (possibly at genesis).
+ ErrLocalIncompatibleOrStale = errors.New("local incompatible or needs update")
+)
+
+// ID is a 2x4-byte tuple containing:
+// - forkhash: CRC32 checksum of the genesis block and passed fork block numbers
+// - forknext: CRC32 checksum of the next fork block number (or 0, if no known)
+type ID [8]byte
+
+// NewID calculates the Ethereum fork ID from the chain config and head.
+func NewID(chain *core.BlockChain) ID {
+ return newID(
+ chain.Config(),
+ chain.Genesis().Hash(),
+ chain.CurrentHeader().Number.Uint64(),
+ )
+}
+
+// newID is the internal version of NewID, which takes extracted values as its
+// arguments instead of a chain. The reason is to allow testing the IDs without
+// having to simulate an entire blockchain.
+func newID(config *params.ChainConfig, genesis common.Hash, head uint64) ID {
+ // Calculate the starting checksum from the genesis hash
+ forkHash := crc32.ChecksumIEEE(genesis[:])
+
+ // Calculate the current fork checksum and the next fork block
+ var forkNext uint32
+ for _, fork := range gatherForks(config) {
+ if fork <= head {
+ // Fork already passed, checksum the previous hash and the fork number
+ forkHash = checksumUpdate(forkHash, fork)
+ continue
+ }
+ forkNext = checksum(fork)
+ break
+ }
+ // Aggregate everything into a single binary blob
+ var entry ID
+ binary.BigEndian.PutUint32(entry[0:], forkHash)
+ binary.BigEndian.PutUint32(entry[4:], forkNext)
+ return entry
+}
+
+// NewFilter creates an filter that returns if a fork ID should be rejected or not
+// based on the local chain's status.
+func NewFilter(chain *core.BlockChain) func(id ID) error {
+ return newFilter(
+ chain.Config(),
+ chain.Genesis().Hash(),
+ func() uint64 {
+ return chain.CurrentHeader().Number.Uint64()
+ },
+ )
+}
+
+// newFilter is the internal version of NewFilter, taking closures as its arguments
+// instead of a chain. The reason is to allow testing it without having to simulate
+// an entire blockchain.
+func newFilter(config *params.ChainConfig, genesis common.Hash, headfn func() uint64) func(id ID) error {
+ // Calculate the all the valid fork hash and fork next combos
+ var (
+ forks = gatherForks(config)
+ sums = make([]uint32, len(forks)+1) // 0th is the genesis
+ next = make([]uint32, len(forks))
+ )
+ sums[0] = crc32.ChecksumIEEE(genesis[:])
+ for i, fork := range forks {
+ sums[i+1] = checksumUpdate(sums[i], fork)
+ next[i] = checksum(fork)
+ }
+ // Add two sentries to simplify the fork checks and don't require special
+ // casing the last one.
+ forks = append(forks, math.MaxUint64) // Last fork will never be passed
+ next = append(next, 0) // Last fork is all 0
+
+ // Create a validator that will filter out incompatible chains
+ return func(id ID) error {
+ // Run the fork checksum validation ruleset:
+ // 1. If local and remote FORK_CSUM matches, connect.
+ // The two nodes are in the same fork state currently. They might know
+ // of differing future forks, but that's not relevant until the fork
+ // triggers (might be postponed, nodes might be updated to match).
+ // 2. If the remote FORK_CSUM is a subset of the local past forks and the
+ // remote FORK_NEXT matches with the locally following fork block number,
+ // connect.
+ // Remote node is currently syncing. It might eventually diverge from
+ // us, but at this current point in time we don't have enough information.
+ // 3. If the remote FORK_CSUM is a superset of the local past forks and can
+ // be completed with locally known future forks, connect.
+ // Local node is currently syncing. It might eventually diverge from
+ // the remote, but at this current point in time we don't have enough
+ // information.
+ // 4. Reject in all other cases.
+ var (
+ head = headfn()
+ remoteSum = binary.BigEndian.Uint32(id[0:4])
+ remoteNext = binary.BigEndian.Uint32(id[4:8])
+ )
+ for i, fork := range forks {
+ // If our head is beyond this fork, continue to the next (we have a dummy
+ // fork of maxuint64 as the last item to always fail this check eventually).
+ if head > fork {
+ continue
+ }
+ // Found the first unpassed fork block, check if our current state matches
+ // the remote checksum (rule #1).
+ if sums[i] == remoteSum {
+ // Yay, fork checksum matched, ignore any upcoming fork
+ return nil
+ }
+ // The local and remote nodes are in different forks currently, check if the
+ // remote checksum is a subset of our local forks (rule #2).
+ for j := 0; j < i; j++ {
+ if sums[j] == remoteSum {
+ // Remote checksum is a subset, validate based on the announced next fork
+ if next[j] != remoteNext {
+ return ErrRemoteStale
+ }
+ return nil
+ }
+ }
+ // Remote chain is not a subset of our local one, check if it's a superset by
+ // any chance, signalling that we're simply out of sync (rule #3).
+ for j := i + 1; j < len(sums); j++ {
+ if sums[j] == remoteSum {
+ // Yay, remote checksum is a superset, ignore upcoming forks
+ return nil
+ }
+ }
+ // No exact, subset or superset match. We are on differing chains, reject.
+ return ErrLocalIncompatibleOrStale
+ }
+ log.Error("Impossible fork ID validation", "id", fmt.Sprintf("%x", id))
+ return nil // Something's very wrong, accept rather than reject
+ }
+}
+
+// checksum calculates the IEEE CRC32 checksum of a block number.
+func checksum(fork uint64) uint32 {
+ var blob [8]byte
+ binary.BigEndian.PutUint64(blob[:], fork)
+ return crc32.ChecksumIEEE(blob[:])
+}
+
+// checksumUpdate calculates the next IEEE CRC32 checksum based on the previous
+// one and a fork block number (equivalent to CRC32(original-blob || fork)).
+func checksumUpdate(hash uint32, fork uint64) uint32 {
+ var blob [8]byte
+ binary.BigEndian.PutUint64(blob[:], fork)
+ return crc32.Update(hash, crc32.IEEETable, blob[:])
+}
+
+// gatherForks gathers all the known forks and creates a sorted list out of them.
+func gatherForks(config *params.ChainConfig) []uint64 {
+ // Gather all the fork block numbers via reflection
+ kind := reflect.TypeOf(params.ChainConfig{})
+ conf := reflect.ValueOf(config).Elem()
+
+ var forks []uint64
+ for i := 0; i < kind.NumField(); i++ {
+ // Fetch the next field and skip non-fork rules
+ field := kind.Field(i)
+ if !strings.HasSuffix(field.Name, "Block") {
+ continue
+ }
+ if field.Type != reflect.TypeOf(new(big.Int)) {
+ continue
+ }
+ // Extract the fork rule block number and aggregate it
+ rule := conf.Field(i).Interface().(*big.Int)
+ if rule != nil {
+ forks = append(forks, rule.Uint64())
+ }
+ }
+ // Sort the fork block numbers to permit chronologival XOR
+ for i := 0; i < len(forks); i++ {
+ for j := i + 1; j < len(forks); j++ {
+ if forks[i] > forks[j] {
+ forks[i], forks[j] = forks[j], forks[i]
+ }
+ }
+ }
+ // Deduplicate block numbers applying multiple forks
+ for i := 1; i < len(forks); i++ {
+ if forks[i] == forks[i-1] {
+ forks = append(forks[:i], forks[i+1:]...)
+ i--
+ }
+ }
+ // Skip any forks in block 0, that's the genesis ruleset
+ if len(forks) > 0 && forks[0] == 0 {
+ forks = forks[1:]
+ }
+ return forks
+}
diff --git a/core/forkid/forkid_test.go b/core/forkid/forkid_test.go
new file mode 100644
index 000000000000..e241e0b12aaa
--- /dev/null
+++ b/core/forkid/forkid_test.go
@@ -0,0 +1,179 @@
+// Copyright 2019 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see .
+
+package forkid
+
+import (
+ "testing"
+
+ "github.com/ethereum/go-ethereum/common"
+ "github.com/ethereum/go-ethereum/params"
+)
+
+// TestCreation tests that different genesis and fork rule combinations result in
+// the correct fork ID.
+func TestCreation(t *testing.T) {
+ type testcase struct {
+ head uint64
+ want ID
+ }
+ tests := []struct {
+ config *params.ChainConfig
+ genesis common.Hash
+ cases []testcase
+ }{
+ // Mainnet test cases
+ {
+ params.MainnetChainConfig,
+ params.MainnetGenesisHash,
+ []testcase{
+ {0, ID{0xfc, 0x64, 0xec, 0x04, 0xc9, 0x29, 0xf1, 0xc5}}, // Unsynced
+ {1149999, ID{0xfc, 0x64, 0xec, 0x04, 0xc9, 0x29, 0xf1, 0xc5}}, // Last Frontier block
+ {1150000, ID{0x97, 0xc2, 0xc3, 0x4c, 0x2d, 0x10, 0xef, 0x43}}, // First Homestead block
+ {1919999, ID{0x97, 0xc2, 0xc3, 0x4c, 0x2d, 0x10, 0xef, 0x43}}, // Last Homestead block
+ {1920000, ID{0x91, 0xd1, 0xf9, 0x48, 0x44, 0xfe, 0xbd, 0x6b}}, // First DAO block
+ {2462999, ID{0x91, 0xd1, 0xf9, 0x48, 0x44, 0xfe, 0xbd, 0x6b}}, // Last DAO block
+ {2463000, ID{0x7a, 0x64, 0xda, 0x13, 0xe3, 0x5d, 0x84, 0xf1}}, // First Tangerine block
+ {2674999, ID{0x7a, 0x64, 0xda, 0x13, 0xe3, 0x5d, 0x84, 0xf1}}, // Last Tangerine block
+ {2675000, ID{0x3e, 0xdd, 0x5b, 0x10, 0x4d, 0xd3, 0x46, 0x54}}, // First Spurious block
+ {4369999, ID{0x3e, 0xdd, 0x5b, 0x10, 0x4d, 0xd3, 0x46, 0x54}}, // Last Spurious block
+ {4370000, ID{0xa0, 0x0b, 0xc3, 0x24, 0xfc, 0xa4, 0x36, 0x40}}, // First Byzantium block
+ {7279999, ID{0xa0, 0x0b, 0xc3, 0x24, 0xfc, 0xa4, 0x36, 0x40}}, // Last Byzantium block
+ {7280000, ID{0x66, 0x8d, 0xb0, 0xaf, 0x00, 0x00, 0x00, 0x00}}, // First and last Constantinople, first Petersburg block
+ {7987396, ID{0x66, 0x8d, 0xb0, 0xaf, 0x00, 0x00, 0x00, 0x00}}, // Today Petersburg block
+ },
+ },
+ // Ropsten test cases
+ {
+ params.TestnetChainConfig,
+ params.TestnetGenesisHash,
+ []testcase{
+ {0, ID{0x30, 0xc7, 0xdd, 0xbc, 0x85, 0xf7, 0x36, 0x77}}, // Unsynced, last Frontier, Homestead and first Tangerine block
+ {9, ID{0x30, 0xc7, 0xdd, 0xbc, 0x85, 0xf7, 0x36, 0x77}}, // Last Tangerine block
+ {10, ID{0x63, 0x76, 0x01, 0x90, 0xb4, 0xbf, 0x05, 0xc3}}, // First Spurious block
+ {1699999, ID{0x63, 0x76, 0x01, 0x90, 0xb4, 0xbf, 0x05, 0xc3}}, // Last Spurious block
+ {1700000, ID{0x3e, 0xa1, 0x59, 0xc7, 0x9d, 0xca, 0x62, 0x15}}, // First Byzantium block
+ {4229999, ID{0x3e, 0xa1, 0x59, 0xc7, 0x9d, 0xca, 0x62, 0x15}}, // Last Byzantium block
+ {4230000, ID{0x97, 0xb5, 0x44, 0xf3, 0x3e, 0x63, 0x2f, 0x9e}}, // First Constantinople block
+ {4939393, ID{0x97, 0xb5, 0x44, 0xf3, 0x3e, 0x63, 0x2f, 0x9e}}, // Last Constantinople block
+ {4939394, ID{0xd6, 0xe2, 0x14, 0x9b, 0x00, 0x00, 0x00, 0x00}}, // First Petersburg block
+ {5822692, ID{0xd6, 0xe2, 0x14, 0x9b, 0x00, 0x00, 0x00, 0x00}}, // Today Petersburg block
+ },
+ },
+ // Rinkeby test cases
+ {
+ params.RinkebyChainConfig,
+ params.RinkebyGenesisHash,
+ []testcase{
+ {0, ID{0x3b, 0x8e, 0x06, 0x91, 0x12, 0x25, 0xef, 0xff}}, // Unsynced, last Frontier block
+ {1, ID{0x60, 0x94, 0x92, 0x95, 0x8b, 0x2c, 0xbe, 0x45}}, // First and last Homestead block
+ {2, ID{0x8b, 0xde, 0x40, 0xdd, 0xfc, 0x2b, 0x8e, 0xd3}}, // First and last Tangerine block
+ {3, ID{0xcb, 0x3a, 0x64, 0xbb, 0x42, 0x35, 0xd4, 0x51}}, // First Spurious block
+ {1035300, ID{0xcb, 0x3a, 0x64, 0xbb, 0x42, 0x35, 0xd4, 0x51}}, // Last Spurious block
+ {1035301, ID{0x8d, 0x74, 0x8b, 0x57, 0xe8, 0xab, 0xd4, 0x37}}, // First Byzantium block
+ {3660662, ID{0x8d, 0x74, 0x8b, 0x57, 0xe8, 0xab, 0xd4, 0x37}}, // Last Byzantium block
+ {3660663, ID{0xe4, 0x9c, 0xab, 0x14, 0xa5, 0x41, 0x64, 0x45}}, // First Constantinople block
+ {4321233, ID{0xe4, 0x9c, 0xab, 0x14, 0xa5, 0x41, 0x64, 0x45}}, // Last Constantinople block
+ {4321234, ID{0xaf, 0xec, 0x6b, 0x27, 0x00, 0x00, 0x00, 0x00}}, // First Petersburg block
+ {4586649, ID{0xaf, 0xec, 0x6b, 0x27, 0x00, 0x00, 0x00, 0x00}}, // Today Petersburg block
+ },
+ },
+ // Goerli test cases
+ {
+ params.GoerliChainConfig,
+ params.GoerliGenesisHash,
+ []testcase{
+ {0, ID{0xa3, 0xf5, 0xab, 0x08, 0x00, 0x00, 0x00, 0x00}}, // Unsynced, last Frontier, Homestead, Tangerine, Spurious, Byzantium, Constantinople and first Petersburg block
+ {795329, ID{0xa3, 0xf5, 0xab, 0x08, 0x00, 0x00, 0x00, 0x00}}, // Today Petersburg block
+ },
+ },
+ }
+ for i, tt := range tests {
+ for j, ttt := range tt.cases {
+ if have := newID(tt.config, tt.genesis, ttt.head); have != ttt.want {
+ t.Errorf("test %d, case %d: fork ID mismatch: have %x, want %x", i, j, have, ttt.want)
+ }
+ }
+ }
+}
+
+// TestValidation tests that a local peer correctly validates and accepts a remote
+// fork ID.
+func TestValidation(t *testing.T) {
+ tests := []struct {
+ head uint64
+ id ID
+ err error
+ }{
+ // Local is mainnet Petersburg, remote announces the same. No future fork is announced.
+ {7987396, ID{0x66, 0x8d, 0xb0, 0xaf, 0x00, 0x00, 0x00, 0x00}, nil},
+
+ // Local is mainnet Petersburg, remote announces the same. Remote also announces a next fork
+ // at block 0xffffffff, but that is uncertain.
+ {7987396, ID{0x66, 0x8d, 0xb0, 0xaf, 0xbb, 0x99, 0xff, 0x8a}, nil},
+
+ // Local is mainnet currently in Byzantium only (so it's aware of Petersburg), remote announces
+ // also Byzantium, but it's not yet aware of Petersburg (e.g. non updated node before the fork).
+ // In this case we don't know if Petersburg passed yet or not.
+ {7279999, ID{0xa0, 0x0b, 0xc3, 0x24, 0x00, 0x00, 0x00, 0x00}, nil},
+
+ // Local is mainnet currently in Byzantium only (so it's aware of Petersburg), remote announces
+ // also Byzantium, and it's also aware of Petersburg (e.g. updated node before the fork). We
+ // don't know if Petersburg passed yet (will pass) or not.
+ {7279999, ID{0xa0, 0x0b, 0xc3, 0x24, 0xfc, 0xa4, 0x36, 0x40}, nil},
+
+ // Local is mainnet currently in Byzantium only (so it's aware of Petersburg), remote announces
+ // also Byzantium, and it's also aware of some random fork (e.g. misconfigured Petersburg). As
+ // neither forks passed at neither nodes, they may mismatch, but we still connect for now.
+ {7279999, ID{0xa0, 0x0b, 0xc3, 0x24, 0xbb, 0x99, 0xff, 0x8a}, nil},
+
+ // Local is mainnet Petersburg, remote announces Byzantium + knowledge about Petersburg. Remote
+ // is simply out of sync, accept.
+ {7987396, ID{0x66, 0x8d, 0xb0, 0xaf, 0xfc, 0xa4, 0x36, 0x40}, nil},
+
+ // Local is mainnet Petersburg, remote announces Spurious + knowledge about Byzantium. Remote
+ // is definitely out of sync. It may or may not need the Petersburg update, we don't know yet.
+ {7987396, ID{0x3e, 0xdd, 0x5b, 0x10, 0x4d, 0xd3, 0x46, 0x54}, nil},
+
+ // Local is mainnet Byzantium, remote announces Petersburg. Local is out of sync, accept.
+ {7279999, ID{0x66, 0x8d, 0xb0, 0xaf, 0x00, 0x00, 0x00, 0x00}, nil},
+
+ // Local is mainnet Spurious, remote announces Byzantium, but is not aware of Petersburg. Local
+ // out of sync. Local also knows about a future fork, but that is uncertain yet.
+ {4369999, ID{0xa0, 0x0b, 0xc3, 0x24, 0x00, 0x00, 0x00, 0x00}, nil},
+
+ // Local is mainnet Petersburg. remote announces Byzantium but is not aware of further forks.
+ // Remote needs software update.
+ {7987396, ID{0xa0, 0x0b, 0xc3, 0x24, 0x00, 0x00, 0x00, 0x00}, ErrRemoteStale},
+
+ // Local is mainnet Petersburg, and isn't aware of more forks. Remote announces Petersburg +
+ // 0xffffffff. Local needs software update, reject.
+ {7987396, ID{0x5c, 0xdd, 0xc0, 0xe1, 0x00, 0x00, 0x00, 0x00}, ErrLocalIncompatibleOrStale},
+
+ // Local is mainnet Byzantium, and is aware of Petersburg. Remote announces Petersburg +
+ // 0xffffffff. Local needs software update, reject.
+ {7279999, ID{0x5c, 0xdd, 0xc0, 0xe1, 0x00, 0x00, 0x00, 0x00}, ErrLocalIncompatibleOrStale},
+
+ // Local is mainnet Petersburg, remote is Rinkeby Petersburg.
+ {7987396, ID{0xaf, 0xec, 0x6b, 0x27, 0x00, 0x00, 0x00, 0x00}, ErrLocalIncompatibleOrStale},
+ }
+ for i, tt := range tests {
+ filter := newFilter(params.MainnetChainConfig, params.MainnetGenesisHash, func() uint64 { return tt.head })
+ if err := filter(tt.id); err != tt.err {
+ t.Errorf("test %d: validation error mismatch: have %v, want %v", i, err, tt.err)
+ }
+ }
+}
diff --git a/eth/enr.go b/eth/enr.go
index 7486c71d1dc8..629571e5c329 100644
--- a/eth/enr.go
+++ b/eth/enr.go
@@ -17,76 +17,18 @@
package eth
import (
- "encoding/binary"
- "errors"
- "fmt"
- "math"
- "math/big"
- "reflect"
- "strings"
-
- "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core"
- "github.com/ethereum/go-ethereum/log"
+ "github.com/ethereum/go-ethereum/core/forkid"
"github.com/ethereum/go-ethereum/p2p/enr"
- "github.com/ethereum/go-ethereum/params"
-)
-
-var (
- // errENRGenesisMismatch is returned by the ENR validator if the local genesis
- // checksum doesn't match with one contained in a remote ENR record.
- errENRGenesisMismatch = errors.New("genesis mismatch")
-
- // errENRRemoteStale is returned by the ENR validator if a remote fork checksum
- // is a subset of our already applied forks, but the announced next fork block is
- // not on our already passed chain.
- errENRRemoteStale = errors.New("remote needs update")
-
- // errENRLocalStale is returned by the ENR validator if a remote fork checksum
- // does not match any local checksum variation, signalling that the two chains have
- // diverged in the past at some point.
- errENRLocalStale = errors.New("local needs update")
)
-// ENR is the "eth" Ethereum Node Record, holding the genesis hash checksum,
-// the enabled fork checksum and the next scheduled fork number.
-type ENR [12]byte
+// ENR is the "eth" Ethereum Node Record, holding the fork id as specified by
+// EIP-2124 (https://eips.ethereum.org/EIPS/eip-2124).
+type ENR forkid.ID
-// NewENR calculates the Ethereum network ENR from the chain config and head.
-// https://eips.ethereum.org/EIPS/eip-2124
+// NewENR calculates the Ethereum network ENR from the fork ID.
func NewENR(chain *core.BlockChain) ENR {
- return newENR(
- chain.Config(),
- chain.Genesis().Hash(),
- chain.CurrentHeader().Number.Uint64(),
- )
-}
-
-// newENR is the internal version of NewENR, which takes extracted values as its
-// arguments instead of a chain. The reason is to allow testing the ENRs without
-// having to simulate an entire blockchain.
-func newENR(config *params.ChainConfig, genesis common.Hash, head uint64) ENR {
- // Calculate the fork checksum and the next fork block
- var (
- forkSum uint32
- forkNext uint32
- )
- for _, fork := range gatherForks(config) {
- if fork <= head {
- forkSum ^= uint32(fork)
- continue
- }
- forkNext = uint32(fork)
- break
- }
- // Aggregate everything into a single binary blob
- var entry ENR
-
- binary.BigEndian.PutUint32(entry[0:], makeGenesisChecksum(genesis))
- binary.BigEndian.PutUint32(entry[4:], forkSum)
- binary.BigEndian.PutUint32(entry[8:], forkNext)
-
- return entry
+ return ENR(forkid.NewID(chain))
}
// ENRKey implements enr.Entry, returning the key for the chain config.
@@ -95,151 +37,15 @@ func (e ENR) ENRKey() string { return "eth" }
// NewENRFilter creates an ENR filter that returns if a record should be rejected
// or not (may be rejected by another filter).
func NewENRFilter(chain *core.BlockChain) func(r *enr.Record) error {
- return newENRFilter(
- chain.Config(),
- chain.Genesis().Hash(),
- func() uint64 {
- return chain.CurrentHeader().Number.Uint64()
- },
- )
-}
+ filter := forkid.NewFilter(chain)
-// newENRFilter is the internal version of NewENRFilter which takes closures as
-// its arguments instead of a chain. The reason is to allow testing it without
-// having to simulate an entire blockchain.
-func newENRFilter(config *params.ChainConfig, genesis common.Hash, headfn func() uint64) func(r *enr.Record) error {
- // Calculate the genesis checksum and all the valid fork checksum and block combos
- var (
- gensum = makeGenesisChecksum(genesis)
- forks = gatherForks(config)
- sums = make([]uint32, len(forks)+1) // 0th is no-forks
- )
- for i, fork := range forks {
- sums[i+1] = sums[i] ^ uint32(fork)
- }
- // Add two sentries to simplify the fork checks and don't require special
- // casing the last one.
- forks = append(forks, math.MaxUint32) // Last fork will never be passed
- sums = append(sums, sums[len(sums)-1]) // Last checksum is a noop
-
- // Create a validator that will filter out incompatible chains
return func(r *enr.Record) error {
// Retrieve the remote chain ENR entry, accept record if not found
var entry ENR
if err := r.Load(&entry); err != nil {
return nil
}
- // Cross reference the genesis checksum and reject on mismatch
- if binary.BigEndian.Uint32(entry[:4]) != gensum {
- return errENRGenesisMismatch
- }
- // Run the fork checksum validation ruleset:
- // 1. If local and remote FORK_CSUM matches, connect.
- // The two nodes are in the same fork state currently. They might know
- // of differing future forks, but that's not relevant until the fork
- // triggers (might be postponed, nodes might be updated to match).
- // 2. If the remote FORK_CSUM is a subset of the local past forks and the
- // remote FORK_NEXT matches with the locally following fork block number,
- // connect.
- // Remote node is currently syncing. It might eventually diverge from
- // us, but at this current point in time we don't have enough information.
- // 3. If the remote FORK_CSUM is a superset of the local past forks and can
- // be completed with locally known future forks, connect.
- // Local node is currently syncing. It might eventually diverge from
- // the remote, but at this current point in time we don't have enough
- // information.
- // 4. Reject in all other cases.
- var (
- head = headfn()
- remoteSum = binary.BigEndian.Uint32(entry[4:8])
- remoteNext = binary.BigEndian.Uint32(entry[8:12])
- )
- for i, fork := range forks {
- // If our head is beyond this fork, continue to the next (we have a dummy
- // fork of maxuint32 as the last item to always fail this check eventually).
- if head > fork {
- continue
- }
- // Found the first unpassed fork block, check if our current state matches
- // the remote checksum (rule #1).
- if sums[i] == remoteSum {
- // Yay, fork checksum matched, ignore any upcoming fork
- return nil
- }
- // The local and remote nodes are in different forks currently, check if the
- // remote checksum is a subset of our local forks (rule #2).
- for j := 0; j < i; j++ {
- if sums[j] == remoteSum {
- // Remote checksum is a subset, validate based on the announced next fork
- if uint32(forks[j]) != remoteNext {
- return errENRRemoteStale
- }
- return nil
- }
- }
- // Remote chain is not a subset of our local one, check if it's a superset by
- // any chance, signalling that we're simply out of sync (rule #3).
- for j := i + 1; j < len(sums); j++ {
- if sums[j] == remoteSum {
- // Yay, remote checksum is a superset, ignore upcoming forks
- return nil
- }
- }
- // No exact, subset or superset match. We are on differing chains, reject.
- return errENRLocalStale
- }
- log.Error("Impossible eth ENR validation", "record", fmt.Sprintf("%x", entry))
- return nil // Something's very wrong, accept rather than reject
- }
-}
-
-// makeGenesisChecksum calculates the ENR checksum for the genesis block.
-func makeGenesisChecksum(hash common.Hash) uint32 {
- var checksum uint32
- for i := 0; i < 8; i++ {
- for j := 0; j < 4; j++ {
- checksum ^= uint32(hash[i*4+j]) << uint32(24-8*j)
- }
- }
- return checksum
-}
-
-// gatherForks gathers all the known forks and creates a sorted list out of them.
-func gatherForks(config *params.ChainConfig) []uint64 {
- // Gather all the fork block numbers via reflection
- kind := reflect.TypeOf(params.ChainConfig{})
- conf := reflect.ValueOf(config).Elem()
-
- var forks []uint64
- for i := 0; i < kind.NumField(); i++ {
- // Fetch the next field and skip non-fork rules
- field := kind.Field(i)
- if !strings.HasSuffix(field.Name, "Block") {
- continue
- }
- if field.Type != reflect.TypeOf(new(big.Int)) {
- continue
- }
- // Extract the fork rule block number and aggregate it
- rule := conf.Field(i).Interface().(*big.Int)
- if rule != nil {
- forks = append(forks, rule.Uint64())
- }
- }
- // Sort the fork block numbers to permit chronologival XOR
- for i := 0; i < len(forks); i++ {
- for j := i + 1; j < len(forks); j++ {
- if forks[i] > forks[j] {
- forks[i], forks[j] = forks[j], forks[i]
- }
- }
- }
- // Deduplicate block number applying multiple forks and return
- for i := 1; i < len(forks); i++ {
- if forks[i] == forks[i-1] {
- forks = append(forks[:i], forks[i+1:]...)
- i--
- }
+ // If found, run it across the fork ID validator
+ return filter(forkid.ID(entry))
}
- return forks
}
diff --git a/eth/enr_test.go b/eth/enr_test.go
deleted file mode 100644
index f9d9699f376e..000000000000
--- a/eth/enr_test.go
+++ /dev/null
@@ -1,184 +0,0 @@
-// Copyright 2019 The go-ethereum Authors
-// This file is part of the go-ethereum library.
-//
-// The go-ethereum library is free software: you can redistribute it and/or modify
-// it under the terms of the GNU Lesser General Public License as published by
-// the Free Software Foundation, either version 3 of the License, or
-// (at your option) any later version.
-//
-// The go-ethereum library is distributed in the hope that it will be useful,
-// but WITHOUT ANY WARRANTY; without even the implied warranty of
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-// GNU Lesser General Public License for more details.
-//
-// You should have received a copy of the GNU Lesser General Public License
-// along with the go-ethereum library. If not, see .
-
-package eth
-
-import (
- "testing"
-
- "github.com/ethereum/go-ethereum/common"
- "github.com/ethereum/go-ethereum/p2p/enr"
- "github.com/ethereum/go-ethereum/params"
-)
-
-// TestENRCreation tests that different genesis and fork rule combinations result
-// in the correct announced chain ENR entry.
-func TestENRCreation(t *testing.T) {
- type testcase struct {
- head uint64
- want ENR
- }
- tests := []struct {
- config *params.ChainConfig
- genesis common.Hash
- cases []testcase
- }{
- // Mainnet test cases
- {
- params.MainnetChainConfig,
- params.MainnetGenesisHash,
- []testcase{
- {0, ENR{0x52, 0xba, 0xab, 0x2d, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0x8c, 0x30}}, // Unsynced
- {1149999, ENR{0x52, 0xba, 0xab, 0x2d, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0x8c, 0x30}}, // Last Frontier block
- {1150000, ENR{0x52, 0xba, 0xab, 0x2d, 0x00, 0x11, 0x8c, 0x30, 0x00, 0x1d, 0x4c, 0x00}}, // First Homestead block
- {1919999, ENR{0x52, 0xba, 0xab, 0x2d, 0x00, 0x11, 0x8c, 0x30, 0x00, 0x1d, 0x4c, 0x00}}, // Last Homestead block
- {1920000, ENR{0x52, 0xba, 0xab, 0x2d, 0x00, 0x0c, 0xc0, 0x30, 0x00, 0x25, 0x95, 0x18}}, // First DAO block
- {2462999, ENR{0x52, 0xba, 0xab, 0x2d, 0x00, 0x0c, 0xc0, 0x30, 0x00, 0x25, 0x95, 0x18}}, // Last DAO block
- {2463000, ENR{0x52, 0xba, 0xab, 0x2d, 0x00, 0x29, 0x55, 0x28, 0x00, 0x28, 0xd1, 0x38}}, // First Tangerine block
- {2674999, ENR{0x52, 0xba, 0xab, 0x2d, 0x00, 0x29, 0x55, 0x28, 0x00, 0x28, 0xd1, 0x38}}, // Last Tangerine block
- {2675000, ENR{0x52, 0xba, 0xab, 0x2d, 0x00, 0x01, 0x84, 0x10, 0x00, 0x42, 0xae, 0x50}}, // First Spurious block
- {4369999, ENR{0x52, 0xba, 0xab, 0x2d, 0x00, 0x01, 0x84, 0x10, 0x00, 0x42, 0xae, 0x50}}, // Last Spurious block
- {4370000, ENR{0x52, 0xba, 0xab, 0x2d, 0x00, 0x43, 0x2a, 0x40, 0x00, 0x6f, 0x15, 0x80}}, // First Byzantium block
- {7279999, ENR{0x52, 0xba, 0xab, 0x2d, 0x00, 0x43, 0x2a, 0x40, 0x00, 0x6f, 0x15, 0x80}}, // Last Byzantium block
- {7280000, ENR{0x52, 0xba, 0xab, 0x2d, 0x00, 0x2c, 0x3f, 0xc0, 0x00, 0x00, 0x00, 0x00}}, // First and last Constantinople, first Petersburg block
- {7987396, ENR{0x52, 0xba, 0xab, 0x2d, 0x00, 0x2c, 0x3f, 0xc0, 0x00, 0x00, 0x00, 0x00}}, // Today Petersburg block
- },
- },
- // Ropsten test cases
- {
- params.TestnetChainConfig,
- params.TestnetGenesisHash,
- []testcase{
- {0, ENR{0xe4, 0x91, 0x9a, 0xeb, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0a}}, // Unsynced, last Frontier, Homestead and first Tangerine block
- {9, ENR{0xe4, 0x91, 0x9a, 0xeb, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0a}}, // Last Tangerine block
- {10, ENR{0xe4, 0x91, 0x9a, 0xeb, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x19, 0xf0, 0xa0}}, // First Spurious block
- {1699999, ENR{0xe4, 0x91, 0x9a, 0xeb, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x19, 0xf0, 0xa0}}, // Last Spurious block
- {1700000, ENR{0xe4, 0x91, 0x9a, 0xeb, 0x00, 0x19, 0xf0, 0xaa, 0x00, 0x40, 0x8b, 0x70}}, // First Byzantium block
- {4229999, ENR{0xe4, 0x91, 0x9a, 0xeb, 0x00, 0x19, 0xf0, 0xaa, 0x00, 0x40, 0x8b, 0x70}}, // Last Byzantium block
- {4230000, ENR{0xe4, 0x91, 0x9a, 0xeb, 0x00, 0x59, 0x7b, 0xda, 0x00, 0x4b, 0x5e, 0x82}}, // First Constantinople block
- {4939393, ENR{0xe4, 0x91, 0x9a, 0xeb, 0x00, 0x59, 0x7b, 0xda, 0x00, 0x4b, 0x5e, 0x82}}, // Last Constantinople block
- {4939394, ENR{0xe4, 0x91, 0x9a, 0xeb, 0x00, 0x12, 0x25, 0x58, 0x00, 0x00, 0x00, 0x00}}, // First Petersburg block
- {5822692, ENR{0xe4, 0x91, 0x9a, 0xeb, 0x00, 0x12, 0x25, 0x58, 0x00, 0x00, 0x00, 0x00}}, // Today Petersburg block
- },
- },
- // Rinkeby test cases
- {
- params.RinkebyChainConfig,
- params.RinkebyGenesisHash,
- []testcase{
- {0, ENR{0xca, 0xb4, 0xc3, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01}}, // Unsynced, last Frontier block
- {1, ENR{0xca, 0xb4, 0xc3, 0x84, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02}}, // First and last Homestead block
- {2, ENR{0xca, 0xb4, 0xc3, 0x84, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x03}}, // First and last Tangerine block
- {3, ENR{0xca, 0xb4, 0xc3, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0xcc, 0x25}}, // First Spurious block
- {1035300, ENR{0xca, 0xb4, 0xc3, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0xcc, 0x25}}, // Last Spurious block
- {1035301, ENR{0xca, 0xb4, 0xc3, 0x84, 0x00, 0x0f, 0xcc, 0x25, 0x00, 0x37, 0xdb, 0x77}}, // First Byzantium block
- {3660662, ENR{0xca, 0xb4, 0xc3, 0x84, 0x00, 0x0f, 0xcc, 0x25, 0x00, 0x37, 0xdb, 0x77}}, // Last Byzantium block
- {3660663, ENR{0xca, 0xb4, 0xc3, 0x84, 0x00, 0x38, 0x17, 0x52, 0x00, 0x41, 0xef, 0xd2}}, // First Constantinople block
- {4321233, ENR{0xca, 0xb4, 0xc3, 0x84, 0x00, 0x38, 0x17, 0x52, 0x00, 0x41, 0xef, 0xd2}}, // Last Constantinople block
- {4321234, ENR{0xca, 0xb4, 0xc3, 0x84, 0x00, 0x79, 0xf8, 0x80, 0x00, 0x00, 0x00, 0x00}}, // First Petersburg block
- {4586649, ENR{0xca, 0xb4, 0xc3, 0x84, 0x00, 0x79, 0xf8, 0x80, 0x00, 0x00, 0x00, 0x00}}, // Today Petersburg block
- },
- },
- // Goerli test cases
- {
- params.GoerliChainConfig,
- params.GoerliGenesisHash,
- []testcase{
- {0, ENR{0xc7, 0x24, 0x73, 0x98, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}}, // Unsynced, last Frontier, Homestead, Tangerine, Spurious, Byzantium, Constantinople and first Petersburg block
- {795329, ENR{0xc7, 0x24, 0x73, 0x98, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}}, // Today Petersburg block
- },
- },
- }
- for i, tt := range tests {
- for j, ttt := range tt.cases {
- if have := newENR(tt.config, tt.genesis, ttt.head); have != ttt.want {
- t.Errorf("test %d, case %d: chain ENR mismatch: have %x, want %x", i, j, have, ttt.want)
- }
- }
- }
-}
-
-// TestENRValidation tests that a local peer correctly validates and accets or
-// rejects a remotely announced ENR entry.
-func TestENRValidation(t *testing.T) {
- tests := []struct {
- head uint64
- enr ENR
- err error
- }{
- // Local is mainnet Petersburg, remote announces the same. No future fork is announced.
- {7987396, ENR{0x52, 0xba, 0xab, 0x2d, 0x00, 0x2c, 0x3f, 0xc0, 0x00, 0x00, 0x00, 0x00}, nil},
-
- // Local is mainnet Petersburg, remote announces the same. Remote also announces a next fork
- // at block 0xffffffff, but that is uncertain.
- {7987396, ENR{0x52, 0xba, 0xab, 0x2d, 0x00, 0x2c, 0x3f, 0xc0, 0xff, 0xff, 0xff, 0xff}, nil},
-
- // Local is mainnet currently in Byzantium only (so it's aware of Petersburg), remote announces
- // also Byzantium, but it's not yet aware of Petersburg (e.g. non updated node before the fork).
- // In this case we don't know if Petersburg passed yet or not.
- {7279999, ENR{0x52, 0xba, 0xab, 0x2d, 0x00, 0x43, 0x2a, 0x40, 0x00, 0x00, 0x00, 0x00}, nil},
-
- // Local is mainnet currently in Byzantium only (so it's aware of Petersburg), remote announces
- // also Byzantium, and it's also aware of Petersburg (e.g. updated node before the fork). We
- // don't know if Petersburg passed yet (will pass) or not.
- {7279999, ENR{0x52, 0xba, 0xab, 0x2d, 0x00, 0x43, 0x2a, 0x40, 0x00, 0x6f, 0x15, 0x80}, nil},
-
- // Local is mainnet currently in Byzantium only (so it's aware of Petersburg), remote announces
- // also Byzantium, and it's also aware of some random fork (e.g. misconfigured Petersburg). As
- // neither forks passed at neither nodes, they may mismatch, but we still connect for now.
- {7279999, ENR{0x52, 0xba, 0xab, 0x2d, 0x00, 0x43, 0x2a, 0x40, 0xff, 0xff, 0xff, 0xff}, nil},
-
- // Local is mainnet Petersburg, remote announces Byzantium + knowledge about Petersburg. Remote
- // is simply out of sync, accept.
- {7987396, ENR{0x52, 0xba, 0xab, 0x2d, 0x00, 0x43, 0x2a, 0x40, 0x00, 0x6f, 0x15, 0x80}, nil},
-
- // Local is mainnet Petersburg, remote announces Spurious + knowledge about Byzantium. Remote
- // is definitely out of sync. It may or may not need the Petersburg update, we don't know yet.
- {7987396, ENR{0x52, 0xba, 0xab, 0x2d, 0x00, 0x01, 0x84, 0x10, 0x00, 0x42, 0xae, 0x50}, nil},
-
- // Local is mainnet Byzantium, remote announces Petersburg. Local is out of sync, accept.
- {7279999, ENR{0x52, 0xba, 0xab, 0x2d, 0x00, 0x2c, 0x3f, 0xc0, 0x00, 0x00, 0x00, 0x00}, nil},
-
- // Local is mainnet Spurious, remote announces Byzantium, but is not aware of Petersburg. Local
- // out of sync. Local also knows about a future fork, but that is uncertain yet.
- {4369999, ENR{0x52, 0xba, 0xab, 0x2d, 0x00, 0x43, 0x2a, 0x40, 0x00, 0x00, 0x00, 0x00}, nil},
-
- // Local is mainnet Petersburg, and isn't aware of more forks. Remote announces Petersburg +
- // 0xffffffff. Local needs software update, reject.
- {7987396, ENR{0x52, 0xba, 0xab, 0x2d, 0xff, 0xd3, 0xc0, 0x3f, 0x00, 0x00, 0x00, 0x00}, errENRLocalStale},
-
- // Local is mainnet Byzantium, and is aware of Petersburg. Remote announces Petersburg +
- // 0xffffffff. Local needs software update, reject.
- {7279999, ENR{0x52, 0xba, 0xab, 0x2d, 0xff, 0xd3, 0xc0, 0x3f, 0x00, 0x00, 0x00, 0x00}, errENRLocalStale},
-
- // Local is mainnet Petersburg. remote announces Byzantium but is not aware of further forks.
- // Remote needs software update.
- {7987396, ENR{0x52, 0xba, 0xab, 0x2d, 0x00, 0x43, 0x2a, 0x40, 0x00, 0x00, 0x00, 0x00}, errENRRemoteStale},
-
- // Local is mainnet Petersburg, remote is Rinkeby Petersburg.
- {7987396, ENR{0xca, 0xb4, 0xc3, 0x84, 0x00, 0x79, 0xf8, 0x80, 0x00, 0x00, 0x00, 0x00}, errENRGenesisMismatch},
- }
- for i, tt := range tests {
- filter := newENRFilter(params.MainnetChainConfig, params.MainnetGenesisHash, func() uint64 { return tt.head })
-
- r := new(enr.Record)
- r.Set(tt.enr)
-
- if err := filter(r); err != tt.err {
- t.Errorf("test %d: validation error mismatch: have %v, want %v", i, err, tt.err)
- }
- }
-}